]> git.proxmox.com Git - qemu.git/blame - net.c
Remove NICInfo from e1000 and mipsnet state (Mark McLoughlin)
[qemu.git] / net.c
CommitLineData
63a01ef8
AL
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 */
559b90fb 24#include "qemu-common.h"
63a01ef8
AL
25#include "net.h"
26#include "console.h"
27#include "sysemu.h"
63a01ef8
AL
28#include "qemu-timer.h"
29#include "qemu-char.h"
63a01ef8 30#include "audio/audio.h"
63a01ef8
AL
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>
24646c7e 46#include <sys/resource.h>
63a01ef8
AL
47#include <sys/socket.h>
48#include <netinet/in.h>
24646c7e
BS
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>
63a01ef8
AL
57#include <dirent.h>
58#include <netdb.h>
59#include <sys/select.h>
63a01ef8
AL
60#ifdef _BSD
61#include <sys/stat.h>
24646c7e 62#ifdef __FreeBSD__
63a01ef8 63#include <libutil.h>
24646c7e
BS
64#else
65#include <util.h>
63a01ef8
AL
66#endif
67#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
68#include <freebsd/stdlib.h>
69#else
70#ifdef __linux__
63a01ef8
AL
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
63a01ef8
AL
123static VLANState *first_vlan;
124
125/***********************************************************/
126/* network device redirectors */
127
128#if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
129static void hex_dump(FILE *f, const uint8_t *buf, int size)
130{
131 int len, i, j, c;
132
133 for(i=0;i<size;i+=16) {
134 len = size - i;
135 if (len > 16)
136 len = 16;
137 fprintf(f, "%08x ", i);
138 for(j=0;j<16;j++) {
139 if (j < len)
140 fprintf(f, " %02x", buf[i+j]);
141 else
142 fprintf(f, " ");
143 }
144 fprintf(f, " ");
145 for(j=0;j<len;j++) {
146 c = buf[i+j];
147 if (c < ' ' || c > '~')
148 c = '.';
149 fprintf(f, "%c", c);
150 }
151 fprintf(f, "\n");
152 }
153}
154#endif
155
156static int parse_macaddr(uint8_t *macaddr, const char *p)
157{
158 int i;
159 char *last_char;
160 long int offset;
161
162 errno = 0;
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;
169 return 0;
170 } else {
171 for(i = 0; i < 6; i++) {
172 macaddr[i] = strtol(p, (char **)&p, 16);
173 if (i == 5) {
174 if (*p != '\0')
175 return -1;
176 } else {
177 if (*p != ':' && *p != '-')
178 return -1;
179 p++;
180 }
181 }
182 return 0;
183 }
184
185 return -1;
186}
187
188static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
189{
190 const char *p, *p1;
191 int len;
192 p = *pp;
193 p1 = strchr(p, sep);
194 if (!p1)
195 return -1;
196 len = p1 - p;
197 p1++;
198 if (buf_size > 0) {
199 if (len > buf_size - 1)
200 len = buf_size - 1;
201 memcpy(buf, p, len);
202 buf[len] = '\0';
203 }
204 *pp = p1;
205 return 0;
206}
207
208int parse_host_src_port(struct sockaddr_in *haddr,
209 struct sockaddr_in *saddr,
210 const char *input_str)
211{
212 char *str = strdup(input_str);
213 char *host_str = str;
214 char *src_str;
215 const char *src_str2;
216 char *ptr;
217
218 /*
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".
222 */
223 if ((ptr = strchr(str,',')))
224 *ptr = '\0';
225
226 if ((src_str = strchr(input_str,'@'))) {
227 *src_str = '\0';
228 src_str++;
229 }
230
231 if (parse_host_port(haddr, host_str) < 0)
232 goto fail;
233
234 src_str2 = src_str;
235 if (!src_str || *src_str == '\0')
236 src_str2 = ":0";
237
238 if (parse_host_port(saddr, src_str2) < 0)
239 goto fail;
240
241 free(str);
242 return(0);
243
244fail:
245 free(str);
246 return -1;
247}
248
249int parse_host_port(struct sockaddr_in *saddr, const char *str)
250{
251 char buf[512];
252 struct hostent *he;
253 const char *p, *r;
254 int port;
255
256 p = str;
257 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
258 return -1;
259 saddr->sin_family = AF_INET;
260 if (buf[0] == '\0') {
261 saddr->sin_addr.s_addr = 0;
262 } else {
cd390083 263 if (qemu_isdigit(buf[0])) {
63a01ef8
AL
264 if (!inet_aton(buf, &saddr->sin_addr))
265 return -1;
266 } else {
267 if ((he = gethostbyname(buf)) == NULL)
268 return - 1;
269 saddr->sin_addr = *(struct in_addr *)he->h_addr;
270 }
271 }
272 port = strtol(p, (char **)&r, 0);
273 if (r == p)
274 return -1;
275 saddr->sin_port = htons(port);
276 return 0;
277}
278
69d6451c
BS
279#if !defined(_WIN32) && 0
280static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
63a01ef8
AL
281{
282 const char *p;
283 int len;
284
285 len = MIN(108, strlen(str));
286 p = strchr(str, ',');
287 if (p)
288 len = MIN(len, p - str);
289
290 memset(uaddr, 0, sizeof(*uaddr));
291
292 uaddr->sun_family = AF_UNIX;
293 memcpy(uaddr->sun_path, str, len);
294
295 return 0;
296}
297#endif
298
7cb7434b
AL
299void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
300{
301 snprintf(vc->info_str, sizeof(vc->info_str),
4dda4063
AL
302 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
303 vc->model,
7cb7434b
AL
304 macaddr[0], macaddr[1], macaddr[2],
305 macaddr[3], macaddr[4], macaddr[5]);
306}
307
676cff29
AL
308static char *assign_name(VLANClientState *vc1, const char *model)
309{
310 VLANState *vlan;
311 char buf[256];
312 int id = 0;
313
314 for (vlan = first_vlan; vlan; vlan = vlan->next) {
315 VLANClientState *vc;
316
317 for (vc = vlan->first_client; vc; vc = vc->next)
318 if (vc != vc1 && strcmp(vc->model, model) == 0)
319 id++;
320 }
321
322 snprintf(buf, sizeof(buf), "%s.%d", model, id);
323
324 return strdup(buf);
325}
326
63a01ef8 327VLANClientState *qemu_new_vlan_client(VLANState *vlan,
bf38c1a0 328 const char *model,
7a9f6e4a 329 const char *name,
63a01ef8
AL
330 IOReadHandler *fd_read,
331 IOCanRWHandler *fd_can_read,
332 void *opaque)
333{
334 VLANClientState *vc, **pvc;
335 vc = qemu_mallocz(sizeof(VLANClientState));
bf38c1a0 336 vc->model = strdup(model);
7a9f6e4a
AL
337 if (name)
338 vc->name = strdup(name);
339 else
340 vc->name = assign_name(vc, model);
63a01ef8
AL
341 vc->fd_read = fd_read;
342 vc->fd_can_read = fd_can_read;
343 vc->opaque = opaque;
344 vc->vlan = vlan;
345
346 vc->next = NULL;
347 pvc = &vlan->first_client;
348 while (*pvc != NULL)
349 pvc = &(*pvc)->next;
350 *pvc = vc;
351 return vc;
352}
353
354void qemu_del_vlan_client(VLANClientState *vc)
355{
356 VLANClientState **pvc = &vc->vlan->first_client;
357
358 while (*pvc != NULL)
359 if (*pvc == vc) {
360 *pvc = vc->next;
676cff29 361 free(vc->name);
bf38c1a0 362 free(vc->model);
63a01ef8
AL
363 free(vc);
364 break;
365 } else
366 pvc = &(*pvc)->next;
367}
368
8b13c4a7
AL
369VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque)
370{
371 VLANClientState **pvc = &vlan->first_client;
372
373 while (*pvc != NULL)
374 if ((*pvc)->opaque == opaque)
375 return *pvc;
376 else
377 pvc = &(*pvc)->next;
378
379 return NULL;
380}
381
63a01ef8
AL
382int qemu_can_send_packet(VLANClientState *vc1)
383{
384 VLANState *vlan = vc1->vlan;
385 VLANClientState *vc;
386
387 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
388 if (vc != vc1) {
389 if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
390 return 1;
391 }
392 }
393 return 0;
394}
395
396void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size)
397{
398 VLANState *vlan = vc1->vlan;
399 VLANClientState *vc;
400
436e5e53
AL
401 if (vc1->link_down)
402 return;
403
63a01ef8
AL
404#ifdef DEBUG_NET
405 printf("vlan %d send:\n", vlan->id);
406 hex_dump(stdout, buf, size);
407#endif
408 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
436e5e53 409 if (vc != vc1 && !vc->link_down) {
63a01ef8
AL
410 vc->fd_read(vc->opaque, buf, size);
411 }
412 }
413}
414
fbe78f4f
AL
415static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
416 int iovcnt)
417{
418 uint8_t buffer[4096];
419 size_t offset = 0;
420 int i;
421
422 for (i = 0; i < iovcnt; i++) {
423 size_t len;
424
425 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
426 memcpy(buffer + offset, iov[i].iov_base, len);
427 offset += len;
428 }
429
430 vc->fd_read(vc->opaque, buffer, offset);
431
432 return offset;
433}
434
e0e7877a
AL
435static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
436{
437 size_t offset = 0;
438 int i;
439
440 for (i = 0; i < iovcnt; i++)
441 offset += iov[i].iov_len;
442 return offset;
443}
444
fbe78f4f
AL
445ssize_t qemu_sendv_packet(VLANClientState *vc1, const struct iovec *iov,
446 int iovcnt)
447{
448 VLANState *vlan = vc1->vlan;
449 VLANClientState *vc;
450 ssize_t max_len = 0;
451
e0e7877a
AL
452 if (vc1->link_down)
453 return calc_iov_length(iov, iovcnt);
454
fbe78f4f
AL
455 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
456 ssize_t len = 0;
457
458 if (vc == vc1)
459 continue;
460
e0e7877a
AL
461 if (vc->link_down)
462 len = calc_iov_length(iov, iovcnt);
fbe78f4f
AL
463 if (vc->fd_readv)
464 len = vc->fd_readv(vc->opaque, iov, iovcnt);
465 else if (vc->fd_read)
466 len = vc_sendv_compat(vc, iov, iovcnt);
467
468 max_len = MAX(max_len, len);
469 }
470
471 return max_len;
472}
473
63a01ef8
AL
474#if defined(CONFIG_SLIRP)
475
476/* slirp network adapter */
477
478static int slirp_inited;
49ec9b40
AL
479static int slirp_restrict;
480static char *slirp_ip;
63a01ef8
AL
481static VLANClientState *slirp_vc;
482
483int slirp_can_output(void)
484{
485 return !slirp_vc || qemu_can_send_packet(slirp_vc);
486}
487
488void slirp_output(const uint8_t *pkt, int pkt_len)
489{
490#ifdef DEBUG_SLIRP
491 printf("slirp output:\n");
492 hex_dump(stdout, pkt, pkt_len);
493#endif
494 if (!slirp_vc)
495 return;
496 qemu_send_packet(slirp_vc, pkt, pkt_len);
497}
498
499int slirp_is_inited(void)
500{
501 return slirp_inited;
502}
503
504static void slirp_receive(void *opaque, const uint8_t *buf, int size)
505{
506#ifdef DEBUG_SLIRP
507 printf("slirp input:\n");
508 hex_dump(stdout, buf, size);
509#endif
510 slirp_input(buf, size);
511}
512
7a9f6e4a 513static int net_slirp_init(VLANState *vlan, const char *model, const char *name)
63a01ef8
AL
514{
515 if (!slirp_inited) {
516 slirp_inited = 1;
49ec9b40 517 slirp_init(slirp_restrict, slirp_ip);
63a01ef8 518 }
7a9f6e4a 519 slirp_vc = qemu_new_vlan_client(vlan, model, name,
63a01ef8 520 slirp_receive, NULL, NULL);
7cb7434b 521 slirp_vc->info_str[0] = '\0';
63a01ef8
AL
522 return 0;
523}
524
525void net_slirp_redir(const char *redir_str)
526{
527 int is_udp;
528 char buf[256], *r;
529 const char *p;
530 struct in_addr guest_addr;
531 int host_port, guest_port;
532
533 if (!slirp_inited) {
534 slirp_inited = 1;
49ec9b40 535 slirp_init(slirp_restrict, slirp_ip);
63a01ef8
AL
536 }
537
538 p = redir_str;
539 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
540 goto fail;
541 if (!strcmp(buf, "tcp")) {
542 is_udp = 0;
543 } else if (!strcmp(buf, "udp")) {
544 is_udp = 1;
545 } else {
546 goto fail;
547 }
548
549 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
550 goto fail;
551 host_port = strtol(buf, &r, 0);
552 if (r == buf)
553 goto fail;
554
555 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
556 goto fail;
557 if (buf[0] == '\0') {
558 pstrcpy(buf, sizeof(buf), "10.0.2.15");
559 }
560 if (!inet_aton(buf, &guest_addr))
561 goto fail;
562
563 guest_port = strtol(p, &r, 0);
564 if (r == p)
565 goto fail;
566
567 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
568 fprintf(stderr, "qemu: could not set up redirection\n");
569 exit(1);
570 }
571 return;
572 fail:
573 fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
574 exit(1);
575}
576
577#ifndef _WIN32
578
579static char smb_dir[1024];
580
581static void erase_dir(char *dir_name)
582{
583 DIR *d;
584 struct dirent *de;
585 char filename[1024];
586
587 /* erase all the files in the directory */
588 if ((d = opendir(dir_name)) != 0) {
589 for(;;) {
590 de = readdir(d);
591 if (!de)
592 break;
593 if (strcmp(de->d_name, ".") != 0 &&
594 strcmp(de->d_name, "..") != 0) {
595 snprintf(filename, sizeof(filename), "%s/%s",
596 smb_dir, de->d_name);
597 if (unlink(filename) != 0) /* is it a directory? */
598 erase_dir(filename);
599 }
600 }
601 closedir(d);
602 rmdir(dir_name);
603 }
604}
605
606/* automatic user mode samba server configuration */
607static void smb_exit(void)
608{
609 erase_dir(smb_dir);
610}
611
612/* automatic user mode samba server configuration */
613void net_slirp_smb(const char *exported_dir)
614{
615 char smb_conf[1024];
616 char smb_cmdline[1024];
617 FILE *f;
618
619 if (!slirp_inited) {
620 slirp_inited = 1;
49ec9b40 621 slirp_init(slirp_restrict, slirp_ip);
63a01ef8
AL
622 }
623
624 /* XXX: better tmp dir construction */
625 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid());
626 if (mkdir(smb_dir, 0700) < 0) {
627 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
628 exit(1);
629 }
630 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
631
632 f = fopen(smb_conf, "w");
633 if (!f) {
634 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
635 exit(1);
636 }
637 fprintf(f,
638 "[global]\n"
639 "private dir=%s\n"
640 "smb ports=0\n"
641 "socket address=127.0.0.1\n"
642 "pid directory=%s\n"
643 "lock directory=%s\n"
644 "log file=%s/log.smbd\n"
645 "smb passwd file=%s/smbpasswd\n"
646 "security = share\n"
647 "[qemu]\n"
648 "path=%s\n"
649 "read only=no\n"
650 "guest ok=yes\n",
651 smb_dir,
652 smb_dir,
653 smb_dir,
654 smb_dir,
655 smb_dir,
656 exported_dir
657 );
658 fclose(f);
659 atexit(smb_exit);
660
661 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
662 SMBD_COMMAND, smb_conf);
663
664 slirp_add_exec(0, smb_cmdline, 4, 139);
665}
666
667#endif /* !defined(_WIN32) */
668void do_info_slirp(void)
669{
670 slirp_stats();
671}
672
8ca9217d
AL
673struct VMChannel {
674 CharDriverState *hd;
675 int port;
676} *vmchannels;
677
678static int vmchannel_can_read(void *opaque)
679{
680 struct VMChannel *vmc = (struct VMChannel*)opaque;
681 return slirp_socket_can_recv(4, vmc->port);
682}
683
684static void vmchannel_read(void *opaque, const uint8_t *buf, int size)
685{
686 struct VMChannel *vmc = (struct VMChannel*)opaque;
687 slirp_socket_recv(4, vmc->port, buf, size);
688}
689
63a01ef8
AL
690#endif /* CONFIG_SLIRP */
691
692#if !defined(_WIN32)
693
694typedef struct TAPState {
695 VLANClientState *vc;
696 int fd;
697 char down_script[1024];
973cbd37 698 char down_script_arg[128];
63a01ef8
AL
699} TAPState;
700
b535b7b2
AL
701static ssize_t tap_receive_iov(void *opaque, const struct iovec *iov,
702 int iovcnt)
703{
704 TAPState *s = opaque;
705 ssize_t len;
706
707 do {
708 len = writev(s->fd, iov, iovcnt);
709 } while (len == -1 && (errno == EINTR || errno == EAGAIN));
710
711 return len;
712}
b535b7b2 713
63a01ef8
AL
714static void tap_receive(void *opaque, const uint8_t *buf, int size)
715{
716 TAPState *s = opaque;
717 int ret;
718 for(;;) {
719 ret = write(s->fd, buf, size);
720 if (ret < 0 && (errno == EINTR || errno == EAGAIN)) {
721 } else {
722 break;
723 }
724 }
725}
726
727static void tap_send(void *opaque)
728{
729 TAPState *s = opaque;
730 uint8_t buf[4096];
731 int size;
732
733#ifdef __sun__
734 struct strbuf sbuf;
735 int f = 0;
736 sbuf.maxlen = sizeof(buf);
737 sbuf.buf = buf;
738 size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
739#else
740 size = read(s->fd, buf, sizeof(buf));
741#endif
742 if (size > 0) {
743 qemu_send_packet(s->vc, buf, size);
744 }
745}
746
747/* fd support */
748
7a9f6e4a
AL
749static TAPState *net_tap_fd_init(VLANState *vlan,
750 const char *model,
751 const char *name,
752 int fd)
63a01ef8
AL
753{
754 TAPState *s;
755
756 s = qemu_mallocz(sizeof(TAPState));
63a01ef8 757 s->fd = fd;
7a9f6e4a 758 s->vc = qemu_new_vlan_client(vlan, model, name, tap_receive, NULL, s);
b535b7b2 759 s->vc->fd_readv = tap_receive_iov;
63a01ef8 760 qemu_set_fd_handler(s->fd, tap_send, NULL, s);
7cb7434b 761 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd);
63a01ef8
AL
762 return s;
763}
764
765#if defined (_BSD) || defined (__FreeBSD_kernel__)
766static int tap_open(char *ifname, int ifname_size)
767{
768 int fd;
769 char *dev;
770 struct stat s;
771
772 TFR(fd = open("/dev/tap", O_RDWR));
773 if (fd < 0) {
774 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
775 return -1;
776 }
777
778 fstat(fd, &s);
779 dev = devname(s.st_rdev, S_IFCHR);
780 pstrcpy(ifname, ifname_size, dev);
781
782 fcntl(fd, F_SETFL, O_NONBLOCK);
783 return fd;
784}
785#elif defined(__sun__)
786#define TUNNEWPPA (('T'<<16) | 0x0001)
787/*
788 * Allocate TAP device, returns opened fd.
789 * Stores dev name in the first arg(must be large enough).
790 */
791int tap_alloc(char *dev, size_t dev_size)
792{
793 int tap_fd, if_fd, ppa = -1;
794 static int ip_fd = 0;
795 char *ptr;
796
797 static int arp_fd = 0;
798 int ip_muxid, arp_muxid;
799 struct strioctl strioc_if, strioc_ppa;
800 int link_type = I_PLINK;;
801 struct lifreq ifr;
802 char actual_name[32] = "";
803
804 memset(&ifr, 0x0, sizeof(ifr));
805
806 if( *dev ){
807 ptr = dev;
47398b9c 808 while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
63a01ef8
AL
809 ppa = atoi(ptr);
810 }
811
812 /* Check if IP device was opened */
813 if( ip_fd )
814 close(ip_fd);
815
816 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
817 if (ip_fd < 0) {
818 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
819 return -1;
820 }
821
822 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
823 if (tap_fd < 0) {
824 syslog(LOG_ERR, "Can't open /dev/tap");
825 return -1;
826 }
827
828 /* Assign a new PPA and get its unit number. */
829 strioc_ppa.ic_cmd = TUNNEWPPA;
830 strioc_ppa.ic_timout = 0;
831 strioc_ppa.ic_len = sizeof(ppa);
832 strioc_ppa.ic_dp = (char *)&ppa;
833 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
834 syslog (LOG_ERR, "Can't assign new interface");
835
836 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
837 if (if_fd < 0) {
838 syslog(LOG_ERR, "Can't open /dev/tap (2)");
839 return -1;
840 }
841 if(ioctl(if_fd, I_PUSH, "ip") < 0){
842 syslog(LOG_ERR, "Can't push IP module");
843 return -1;
844 }
845
846 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
847 syslog(LOG_ERR, "Can't get flags\n");
848
849 snprintf (actual_name, 32, "tap%d", ppa);
850 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
851
852 ifr.lifr_ppa = ppa;
853 /* Assign ppa according to the unit number returned by tun device */
854
855 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
856 syslog (LOG_ERR, "Can't set PPA %d", ppa);
857 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
858 syslog (LOG_ERR, "Can't get flags\n");
859 /* Push arp module to if_fd */
860 if (ioctl (if_fd, I_PUSH, "arp") < 0)
861 syslog (LOG_ERR, "Can't push ARP module (2)");
862
863 /* Push arp module to ip_fd */
864 if (ioctl (ip_fd, I_POP, NULL) < 0)
865 syslog (LOG_ERR, "I_POP failed\n");
866 if (ioctl (ip_fd, I_PUSH, "arp") < 0)
867 syslog (LOG_ERR, "Can't push ARP module (3)\n");
868 /* Open arp_fd */
869 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
870 if (arp_fd < 0)
871 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
872
873 /* Set ifname to arp */
874 strioc_if.ic_cmd = SIOCSLIFNAME;
875 strioc_if.ic_timout = 0;
876 strioc_if.ic_len = sizeof(ifr);
877 strioc_if.ic_dp = (char *)&ifr;
878 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
879 syslog (LOG_ERR, "Can't set ifname to arp\n");
880 }
881
882 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
883 syslog(LOG_ERR, "Can't link TAP device to IP");
884 return -1;
885 }
886
887 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
888 syslog (LOG_ERR, "Can't link TAP device to ARP");
889
890 close (if_fd);
891
892 memset(&ifr, 0x0, sizeof(ifr));
893 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
894 ifr.lifr_ip_muxid = ip_muxid;
895 ifr.lifr_arp_muxid = arp_muxid;
896
897 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
898 {
899 ioctl (ip_fd, I_PUNLINK , arp_muxid);
900 ioctl (ip_fd, I_PUNLINK, ip_muxid);
901 syslog (LOG_ERR, "Can't set multiplexor id");
902 }
903
904 snprintf(dev, dev_size, "tap%d", ppa);
905 return tap_fd;
906}
907
908static int tap_open(char *ifname, int ifname_size)
909{
910 char dev[10]="";
911 int fd;
912 if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
913 fprintf(stderr, "Cannot allocate TAP device\n");
914 return -1;
915 }
916 pstrcpy(ifname, ifname_size, dev);
917 fcntl(fd, F_SETFL, O_NONBLOCK);
918 return fd;
919}
b29fe3ed 920#elif defined (_AIX)
921static int tap_open(char *ifname, int ifname_size)
922{
923 fprintf (stderr, "no tap on AIX\n");
924 return -1;
925}
63a01ef8
AL
926#else
927static int tap_open(char *ifname, int ifname_size)
928{
929 struct ifreq ifr;
930 int fd, ret;
931
932 TFR(fd = open("/dev/net/tun", O_RDWR));
933 if (fd < 0) {
934 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
935 return -1;
936 }
937 memset(&ifr, 0, sizeof(ifr));
938 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
939 if (ifname[0] != '\0')
940 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
941 else
942 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
943 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
944 if (ret != 0) {
945 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
946 close(fd);
947 return -1;
948 }
949 pstrcpy(ifname, ifname_size, ifr.ifr_name);
950 fcntl(fd, F_SETFL, O_NONBLOCK);
951 return fd;
952}
953#endif
954
955static int launch_script(const char *setup_script, const char *ifname, int fd)
956{
957 int pid, status;
958 char *args[3];
959 char **parg;
960
961 /* try to launch network script */
962 pid = fork();
963 if (pid >= 0) {
964 if (pid == 0) {
965 int open_max = sysconf (_SC_OPEN_MAX), i;
966 for (i = 0; i < open_max; i++)
967 if (i != STDIN_FILENO &&
968 i != STDOUT_FILENO &&
969 i != STDERR_FILENO &&
970 i != fd)
971 close(i);
972
973 parg = args;
974 *parg++ = (char *)setup_script;
975 *parg++ = (char *)ifname;
976 *parg++ = NULL;
977 execv(setup_script, args);
978 _exit(1);
979 }
980 while (waitpid(pid, &status, 0) != pid);
981 if (!WIFEXITED(status) ||
982 WEXITSTATUS(status) != 0) {
983 fprintf(stderr, "%s: could not launch network script\n",
984 setup_script);
985 return -1;
986 }
987 }
988 return 0;
989}
990
7a9f6e4a
AL
991static int net_tap_init(VLANState *vlan, const char *model,
992 const char *name, const char *ifname1,
63a01ef8
AL
993 const char *setup_script, const char *down_script)
994{
995 TAPState *s;
996 int fd;
997 char ifname[128];
998
999 if (ifname1 != NULL)
1000 pstrcpy(ifname, sizeof(ifname), ifname1);
1001 else
1002 ifname[0] = '\0';
1003 TFR(fd = tap_open(ifname, sizeof(ifname)));
1004 if (fd < 0)
1005 return -1;
1006
1007 if (!setup_script || !strcmp(setup_script, "no"))
1008 setup_script = "";
1009 if (setup_script[0] != '\0') {
1010 if (launch_script(setup_script, ifname, fd))
1011 return -1;
1012 }
7a9f6e4a 1013 s = net_tap_fd_init(vlan, model, name, fd);
63a01ef8 1014 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
7cb7434b
AL
1015 "ifname=%s,script=%s,downscript=%s",
1016 ifname, setup_script, down_script);
973cbd37 1017 if (down_script && strcmp(down_script, "no")) {
63a01ef8 1018 snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
973cbd37
AL
1019 snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
1020 }
63a01ef8
AL
1021 return 0;
1022}
1023
1024#endif /* !_WIN32 */
1025
1026#if defined(CONFIG_VDE)
1027typedef struct VDEState {
1028 VLANClientState *vc;
1029 VDECONN *vde;
1030} VDEState;
1031
1032static void vde_to_qemu(void *opaque)
1033{
1034 VDEState *s = opaque;
1035 uint8_t buf[4096];
1036 int size;
1037
1038 size = vde_recv(s->vde, buf, sizeof(buf), 0);
1039 if (size > 0) {
1040 qemu_send_packet(s->vc, buf, size);
1041 }
1042}
1043
1044static void vde_from_qemu(void *opaque, const uint8_t *buf, int size)
1045{
1046 VDEState *s = opaque;
1047 int ret;
1048 for(;;) {
1049 ret = vde_send(s->vde, buf, size, 0);
1050 if (ret < 0 && errno == EINTR) {
1051 } else {
1052 break;
1053 }
1054 }
1055}
1056
7a9f6e4a
AL
1057static int net_vde_init(VLANState *vlan, const char *model,
1058 const char *name, const char *sock,
bf38c1a0 1059 int port, const char *group, int mode)
63a01ef8
AL
1060{
1061 VDEState *s;
1062 char *init_group = strlen(group) ? (char *)group : NULL;
1063 char *init_sock = strlen(sock) ? (char *)sock : NULL;
1064
1065 struct vde_open_args args = {
1066 .port = port,
1067 .group = init_group,
1068 .mode = mode,
1069 };
1070
1071 s = qemu_mallocz(sizeof(VDEState));
63a01ef8
AL
1072 s->vde = vde_open(init_sock, "QEMU", &args);
1073 if (!s->vde){
1074 free(s);
1075 return -1;
1076 }
7a9f6e4a 1077 s->vc = qemu_new_vlan_client(vlan, model, name, vde_from_qemu, NULL, s);
63a01ef8 1078 qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
7cb7434b 1079 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d",
63a01ef8
AL
1080 sock, vde_datafd(s->vde));
1081 return 0;
1082}
1083#endif
1084
1085/* network connection */
1086typedef struct NetSocketState {
1087 VLANClientState *vc;
1088 int fd;
1089 int state; /* 0 = getting length, 1 = getting data */
abcd2baa
AL
1090 unsigned int index;
1091 unsigned int packet_len;
63a01ef8
AL
1092 uint8_t buf[4096];
1093 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1094} NetSocketState;
1095
1096typedef struct NetSocketListenState {
1097 VLANState *vlan;
bf38c1a0 1098 char *model;
7a9f6e4a 1099 char *name;
63a01ef8
AL
1100 int fd;
1101} NetSocketListenState;
1102
1103/* XXX: we consider we can send the whole packet without blocking */
1104static void net_socket_receive(void *opaque, const uint8_t *buf, int size)
1105{
1106 NetSocketState *s = opaque;
1107 uint32_t len;
1108 len = htonl(size);
1109
1110 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
1111 send_all(s->fd, buf, size);
1112}
1113
1114static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size)
1115{
1116 NetSocketState *s = opaque;
1117 sendto(s->fd, buf, size, 0,
1118 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
1119}
1120
1121static void net_socket_send(void *opaque)
1122{
1123 NetSocketState *s = opaque;
abcd2baa
AL
1124 int size, err;
1125 unsigned l;
63a01ef8
AL
1126 uint8_t buf1[4096];
1127 const uint8_t *buf;
1128
1129 size = recv(s->fd, buf1, sizeof(buf1), 0);
1130 if (size < 0) {
1131 err = socket_error();
1132 if (err != EWOULDBLOCK)
1133 goto eoc;
1134 } else if (size == 0) {
1135 /* end of connection */
1136 eoc:
1137 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1138 closesocket(s->fd);
1139 return;
1140 }
1141 buf = buf1;
1142 while (size > 0) {
1143 /* reassemble a packet from the network */
1144 switch(s->state) {
1145 case 0:
1146 l = 4 - s->index;
1147 if (l > size)
1148 l = size;
1149 memcpy(s->buf + s->index, buf, l);
1150 buf += l;
1151 size -= l;
1152 s->index += l;
1153 if (s->index == 4) {
1154 /* got length */
1155 s->packet_len = ntohl(*(uint32_t *)s->buf);
1156 s->index = 0;
1157 s->state = 1;
1158 }
1159 break;
1160 case 1:
1161 l = s->packet_len - s->index;
1162 if (l > size)
1163 l = size;
abcd2baa
AL
1164 if (s->index + l <= sizeof(s->buf)) {
1165 memcpy(s->buf + s->index, buf, l);
1166 } else {
1167 fprintf(stderr, "serious error: oversized packet received,"
1168 "connection terminated.\n");
1169 s->state = 0;
1170 goto eoc;
1171 }
1172
63a01ef8
AL
1173 s->index += l;
1174 buf += l;
1175 size -= l;
1176 if (s->index >= s->packet_len) {
1177 qemu_send_packet(s->vc, s->buf, s->packet_len);
1178 s->index = 0;
1179 s->state = 0;
1180 }
1181 break;
1182 }
1183 }
1184}
1185
1186static void net_socket_send_dgram(void *opaque)
1187{
1188 NetSocketState *s = opaque;
1189 int size;
1190
1191 size = recv(s->fd, s->buf, sizeof(s->buf), 0);
1192 if (size < 0)
1193 return;
1194 if (size == 0) {
1195 /* end of connection */
1196 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1197 return;
1198 }
1199 qemu_send_packet(s->vc, s->buf, size);
1200}
1201
1202static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
1203{
1204 struct ip_mreq imr;
1205 int fd;
1206 int val, ret;
1207 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
1208 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1209 inet_ntoa(mcastaddr->sin_addr),
1210 (int)ntohl(mcastaddr->sin_addr.s_addr));
1211 return -1;
1212
1213 }
1214 fd = socket(PF_INET, SOCK_DGRAM, 0);
1215 if (fd < 0) {
1216 perror("socket(PF_INET, SOCK_DGRAM)");
1217 return -1;
1218 }
1219
1220 val = 1;
1221 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1222 (const char *)&val, sizeof(val));
1223 if (ret < 0) {
1224 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1225 goto fail;
1226 }
1227
1228 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
1229 if (ret < 0) {
1230 perror("bind");
1231 goto fail;
1232 }
1233
1234 /* Add host to multicast group */
1235 imr.imr_multiaddr = mcastaddr->sin_addr;
1236 imr.imr_interface.s_addr = htonl(INADDR_ANY);
1237
1238 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1239 (const char *)&imr, sizeof(struct ip_mreq));
1240 if (ret < 0) {
1241 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1242 goto fail;
1243 }
1244
1245 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1246 val = 1;
1247 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1248 (const char *)&val, sizeof(val));
1249 if (ret < 0) {
1250 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1251 goto fail;
1252 }
1253
1254 socket_set_nonblock(fd);
1255 return fd;
1256fail:
1257 if (fd >= 0)
1258 closesocket(fd);
1259 return -1;
1260}
1261
7a9f6e4a
AL
1262static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
1263 const char *model,
1264 const char *name,
bf38c1a0 1265 int fd, int is_connected)
63a01ef8
AL
1266{
1267 struct sockaddr_in saddr;
1268 int newfd;
1269 socklen_t saddr_len;
1270 NetSocketState *s;
1271
1272 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1273 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1274 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1275 */
1276
1277 if (is_connected) {
1278 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
1279 /* must be bound */
1280 if (saddr.sin_addr.s_addr==0) {
1281 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1282 fd);
1283 return NULL;
1284 }
1285 /* clone dgram socket */
1286 newfd = net_socket_mcast_create(&saddr);
1287 if (newfd < 0) {
1288 /* error already reported by net_socket_mcast_create() */
1289 close(fd);
1290 return NULL;
1291 }
1292 /* clone newfd to fd, close newfd */
1293 dup2(newfd, fd);
1294 close(newfd);
1295
1296 } else {
1297 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1298 fd, strerror(errno));
1299 return NULL;
1300 }
1301 }
1302
1303 s = qemu_mallocz(sizeof(NetSocketState));
63a01ef8
AL
1304 s->fd = fd;
1305
7a9f6e4a 1306 s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive_dgram, NULL, s);
63a01ef8
AL
1307 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
1308
1309 /* mcast: save bound address as dst */
1310 if (is_connected) s->dgram_dst=saddr;
1311
1312 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1313 "socket: fd=%d (%s mcast=%s:%d)",
1314 fd, is_connected? "cloned" : "",
1315 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1316 return s;
1317}
1318
1319static void net_socket_connect(void *opaque)
1320{
1321 NetSocketState *s = opaque;
1322 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
1323}
1324
7a9f6e4a
AL
1325static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
1326 const char *model,
1327 const char *name,
bf38c1a0 1328 int fd, int is_connected)
63a01ef8
AL
1329{
1330 NetSocketState *s;
1331 s = qemu_mallocz(sizeof(NetSocketState));
63a01ef8 1332 s->fd = fd;
7a9f6e4a 1333 s->vc = qemu_new_vlan_client(vlan, model, name,
63a01ef8
AL
1334 net_socket_receive, NULL, s);
1335 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1336 "socket: fd=%d", fd);
1337 if (is_connected) {
1338 net_socket_connect(s);
1339 } else {
1340 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
1341 }
1342 return s;
1343}
1344
7a9f6e4a
AL
1345static NetSocketState *net_socket_fd_init(VLANState *vlan,
1346 const char *model, const char *name,
bf38c1a0 1347 int fd, int is_connected)
63a01ef8
AL
1348{
1349 int so_type=-1, optlen=sizeof(so_type);
1350
1351 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
1352 (socklen_t *)&optlen)< 0) {
1353 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
1354 return NULL;
1355 }
1356 switch(so_type) {
1357 case SOCK_DGRAM:
7a9f6e4a 1358 return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected);
63a01ef8 1359 case SOCK_STREAM:
7a9f6e4a 1360 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
63a01ef8
AL
1361 default:
1362 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1363 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
7a9f6e4a 1364 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
63a01ef8
AL
1365 }
1366 return NULL;
1367}
1368
1369static void net_socket_accept(void *opaque)
1370{
1371 NetSocketListenState *s = opaque;
1372 NetSocketState *s1;
1373 struct sockaddr_in saddr;
1374 socklen_t len;
1375 int fd;
1376
1377 for(;;) {
1378 len = sizeof(saddr);
1379 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
1380 if (fd < 0 && errno != EINTR) {
1381 return;
1382 } else if (fd >= 0) {
1383 break;
1384 }
1385 }
7a9f6e4a 1386 s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1);
63a01ef8
AL
1387 if (!s1) {
1388 closesocket(fd);
1389 } else {
1390 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
1391 "socket: connection from %s:%d",
1392 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1393 }
1394}
1395
7a9f6e4a
AL
1396static int net_socket_listen_init(VLANState *vlan,
1397 const char *model,
1398 const char *name,
bf38c1a0 1399 const char *host_str)
63a01ef8
AL
1400{
1401 NetSocketListenState *s;
1402 int fd, val, ret;
1403 struct sockaddr_in saddr;
1404
1405 if (parse_host_port(&saddr, host_str) < 0)
1406 return -1;
1407
1408 s = qemu_mallocz(sizeof(NetSocketListenState));
63a01ef8
AL
1409
1410 fd = socket(PF_INET, SOCK_STREAM, 0);
1411 if (fd < 0) {
1412 perror("socket");
1413 return -1;
1414 }
1415 socket_set_nonblock(fd);
1416
1417 /* allow fast reuse */
1418 val = 1;
1419 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
1420
1421 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1422 if (ret < 0) {
1423 perror("bind");
1424 return -1;
1425 }
1426 ret = listen(fd, 0);
1427 if (ret < 0) {
1428 perror("listen");
1429 return -1;
1430 }
1431 s->vlan = vlan;
bf38c1a0 1432 s->model = strdup(model);
7a9f6e4a 1433 s->name = strdup(name);
63a01ef8
AL
1434 s->fd = fd;
1435 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
1436 return 0;
1437}
1438
7a9f6e4a
AL
1439static int net_socket_connect_init(VLANState *vlan,
1440 const char *model,
1441 const char *name,
bf38c1a0 1442 const char *host_str)
63a01ef8
AL
1443{
1444 NetSocketState *s;
1445 int fd, connected, ret, err;
1446 struct sockaddr_in saddr;
1447
1448 if (parse_host_port(&saddr, host_str) < 0)
1449 return -1;
1450
1451 fd = socket(PF_INET, SOCK_STREAM, 0);
1452 if (fd < 0) {
1453 perror("socket");
1454 return -1;
1455 }
1456 socket_set_nonblock(fd);
1457
1458 connected = 0;
1459 for(;;) {
1460 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1461 if (ret < 0) {
1462 err = socket_error();
1463 if (err == EINTR || err == EWOULDBLOCK) {
1464 } else if (err == EINPROGRESS) {
1465 break;
1466#ifdef _WIN32
1467 } else if (err == WSAEALREADY) {
1468 break;
1469#endif
1470 } else {
1471 perror("connect");
1472 closesocket(fd);
1473 return -1;
1474 }
1475 } else {
1476 connected = 1;
1477 break;
1478 }
1479 }
7a9f6e4a 1480 s = net_socket_fd_init(vlan, model, name, fd, connected);
63a01ef8
AL
1481 if (!s)
1482 return -1;
1483 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1484 "socket: connect to %s:%d",
1485 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1486 return 0;
1487}
1488
7a9f6e4a
AL
1489static int net_socket_mcast_init(VLANState *vlan,
1490 const char *model,
1491 const char *name,
bf38c1a0 1492 const char *host_str)
63a01ef8
AL
1493{
1494 NetSocketState *s;
1495 int fd;
1496 struct sockaddr_in saddr;
1497
1498 if (parse_host_port(&saddr, host_str) < 0)
1499 return -1;
1500
1501
1502 fd = net_socket_mcast_create(&saddr);
1503 if (fd < 0)
1504 return -1;
1505
7a9f6e4a 1506 s = net_socket_fd_init(vlan, model, name, fd, 0);
63a01ef8
AL
1507 if (!s)
1508 return -1;
1509
1510 s->dgram_dst = saddr;
1511
1512 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1513 "socket: mcast=%s:%d",
1514 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1515 return 0;
1516
1517}
1518
1519/* find or alloc a new VLAN */
1520VLANState *qemu_find_vlan(int id)
1521{
1522 VLANState **pvlan, *vlan;
1523 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1524 if (vlan->id == id)
1525 return vlan;
1526 }
1527 vlan = qemu_mallocz(sizeof(VLANState));
63a01ef8
AL
1528 vlan->id = id;
1529 vlan->next = NULL;
1530 pvlan = &first_vlan;
1531 while (*pvlan != NULL)
1532 pvlan = &(*pvlan)->next;
1533 *pvlan = vlan;
1534 return vlan;
1535}
1536
7697079b
AL
1537static int nic_get_free_idx(void)
1538{
1539 int index;
1540
1541 for (index = 0; index < MAX_NICS; index++)
1542 if (!nd_table[index].used)
1543 return index;
1544 return -1;
1545}
1546
d07f22c5
AL
1547void qemu_check_nic_model(NICInfo *nd, const char *model)
1548{
1549 const char *models[2];
1550
1551 models[0] = model;
1552 models[1] = NULL;
1553
1554 qemu_check_nic_model_list(nd, models, model);
1555}
1556
1557void qemu_check_nic_model_list(NICInfo *nd, const char * const *models,
1558 const char *default_model)
1559{
1560 int i, exit_status = 0;
1561
1562 if (!nd->model)
1563 nd->model = strdup(default_model);
1564
1565 if (strcmp(nd->model, "?") != 0) {
1566 for (i = 0 ; models[i]; i++)
1567 if (strcmp(nd->model, models[i]) == 0)
1568 return;
1569
1570 fprintf(stderr, "qemu: Unsupported NIC model: %s\n", nd->model);
1571 exit_status = 1;
1572 }
1573
1574 fprintf(stderr, "qemu: Supported NIC models: ");
1575 for (i = 0 ; models[i]; i++)
1576 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
1577
1578 exit(exit_status);
1579}
1580
63a01ef8
AL
1581int net_client_init(const char *device, const char *p)
1582{
1583 char buf[1024];
1584 int vlan_id, ret;
1585 VLANState *vlan;
7a9f6e4a 1586 char *name = NULL;
63a01ef8
AL
1587
1588 vlan_id = 0;
1589 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
1590 vlan_id = strtol(buf, NULL, 0);
1591 }
1592 vlan = qemu_find_vlan(vlan_id);
eb5951f3 1593
7a9f6e4a
AL
1594 if (get_param_value(buf, sizeof(buf), "name", p)) {
1595 name = strdup(buf);
1596 }
63a01ef8
AL
1597 if (!strcmp(device, "nic")) {
1598 NICInfo *nd;
1599 uint8_t *macaddr;
7697079b 1600 int idx = nic_get_free_idx();
63a01ef8 1601
7697079b 1602 if (idx == -1 || nb_nics >= MAX_NICS) {
63a01ef8 1603 fprintf(stderr, "Too Many NICs\n");
76dcd492
AL
1604 ret = -1;
1605 goto out;
63a01ef8 1606 }
7697079b 1607 nd = &nd_table[idx];
63a01ef8
AL
1608 macaddr = nd->macaddr;
1609 macaddr[0] = 0x52;
1610 macaddr[1] = 0x54;
1611 macaddr[2] = 0x00;
1612 macaddr[3] = 0x12;
1613 macaddr[4] = 0x34;
7697079b 1614 macaddr[5] = 0x56 + idx;
63a01ef8
AL
1615
1616 if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
1617 if (parse_macaddr(macaddr, buf) < 0) {
1618 fprintf(stderr, "invalid syntax for ethernet address\n");
76dcd492
AL
1619 ret = -1;
1620 goto out;
63a01ef8
AL
1621 }
1622 }
1623 if (get_param_value(buf, sizeof(buf), "model", p)) {
1624 nd->model = strdup(buf);
1625 }
1626 nd->vlan = vlan;
7a9f6e4a 1627 nd->name = name;
7697079b 1628 nd->used = 1;
7a9f6e4a 1629 name = NULL;
63a01ef8
AL
1630 nb_nics++;
1631 vlan->nb_guest_devs++;
4d73cd3b 1632 ret = idx;
63a01ef8
AL
1633 } else
1634 if (!strcmp(device, "none")) {
1635 /* does nothing. It is needed to signal that no network cards
1636 are wanted */
1637 ret = 0;
1638 } else
1639#ifdef CONFIG_SLIRP
1640 if (!strcmp(device, "user")) {
1641 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
1642 pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
1643 }
49ec9b40
AL
1644 if (get_param_value(buf, sizeof(buf), "restrict", p)) {
1645 slirp_restrict = (buf[0] == 'y') ? 1 : 0;
1646 }
1647 if (get_param_value(buf, sizeof(buf), "ip", p)) {
1648 slirp_ip = strdup(buf);
1649 }
63a01ef8 1650 vlan->nb_host_devs++;
7a9f6e4a 1651 ret = net_slirp_init(vlan, device, name);
8ca9217d
AL
1652 } else if (!strcmp(device, "channel")) {
1653 long port;
1654 char name[20], *devname;
1655 struct VMChannel *vmc;
1656
1657 port = strtol(p, &devname, 10);
1658 devname++;
1659 if (port < 1 || port > 65535) {
76dcd492
AL
1660 fprintf(stderr, "vmchannel wrong port number\n");
1661 ret = -1;
1662 goto out;
8ca9217d
AL
1663 }
1664 vmc = malloc(sizeof(struct VMChannel));
1665 snprintf(name, 20, "vmchannel%ld", port);
1666 vmc->hd = qemu_chr_open(name, devname, NULL);
1667 if (!vmc->hd) {
1668 fprintf(stderr, "qemu: could not open vmchannel device"
1669 "'%s'\n", devname);
76dcd492
AL
1670 ret = -1;
1671 goto out;
8ca9217d
AL
1672 }
1673 vmc->port = port;
1674 slirp_add_exec(3, vmc->hd, 4, port);
1675 qemu_chr_add_handlers(vmc->hd, vmchannel_can_read, vmchannel_read,
1676 NULL, vmc);
1677 ret = 0;
63a01ef8
AL
1678 } else
1679#endif
1680#ifdef _WIN32
1681 if (!strcmp(device, "tap")) {
1682 char ifname[64];
1683 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
1684 fprintf(stderr, "tap: no interface name\n");
76dcd492
AL
1685 ret = -1;
1686 goto out;
63a01ef8
AL
1687 }
1688 vlan->nb_host_devs++;
7a9f6e4a 1689 ret = tap_win32_init(vlan, device, name, ifname);
63a01ef8 1690 } else
b29fe3ed 1691#elif defined (_AIX)
63a01ef8
AL
1692#else
1693 if (!strcmp(device, "tap")) {
1694 char ifname[64];
1695 char setup_script[1024], down_script[1024];
1696 int fd;
1697 vlan->nb_host_devs++;
1698 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
1699 fd = strtol(buf, NULL, 0);
1700 fcntl(fd, F_SETFL, O_NONBLOCK);
eb5951f3
AL
1701 net_tap_fd_init(vlan, device, name, fd);
1702 ret = 0;
63a01ef8
AL
1703 } else {
1704 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
1705 ifname[0] = '\0';
1706 }
1707 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
1708 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
1709 }
1710 if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
1711 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
1712 }
7a9f6e4a 1713 ret = net_tap_init(vlan, device, name, ifname, setup_script, down_script);
63a01ef8
AL
1714 }
1715 } else
1716#endif
1717 if (!strcmp(device, "socket")) {
1718 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
1719 int fd;
1720 fd = strtol(buf, NULL, 0);
1721 ret = -1;
7a9f6e4a 1722 if (net_socket_fd_init(vlan, device, name, fd, 1))
63a01ef8
AL
1723 ret = 0;
1724 } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
7a9f6e4a 1725 ret = net_socket_listen_init(vlan, device, name, buf);
63a01ef8 1726 } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
7a9f6e4a 1727 ret = net_socket_connect_init(vlan, device, name, buf);
63a01ef8 1728 } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
7a9f6e4a 1729 ret = net_socket_mcast_init(vlan, device, name, buf);
63a01ef8
AL
1730 } else {
1731 fprintf(stderr, "Unknown socket options: %s\n", p);
76dcd492
AL
1732 ret = -1;
1733 goto out;
63a01ef8
AL
1734 }
1735 vlan->nb_host_devs++;
1736 } else
1737#ifdef CONFIG_VDE
1738 if (!strcmp(device, "vde")) {
1739 char vde_sock[1024], vde_group[512];
1740 int vde_port, vde_mode;
1741 vlan->nb_host_devs++;
1742 if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
1743 vde_sock[0] = '\0';
1744 }
1745 if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
1746 vde_port = strtol(buf, NULL, 10);
1747 } else {
1748 vde_port = 0;
1749 }
1750 if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
1751 vde_group[0] = '\0';
1752 }
1753 if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
1754 vde_mode = strtol(buf, NULL, 8);
1755 } else {
1756 vde_mode = 0700;
1757 }
7a9f6e4a 1758 ret = net_vde_init(vlan, device, name, vde_sock, vde_port, vde_group, vde_mode);
63a01ef8
AL
1759 } else
1760#endif
1761 {
1762 fprintf(stderr, "Unknown network device: %s\n", device);
76dcd492
AL
1763 ret = -1;
1764 goto out;
63a01ef8
AL
1765 }
1766 if (ret < 0) {
1767 fprintf(stderr, "Could not initialize device '%s'\n", device);
1768 }
76dcd492 1769out:
7a9f6e4a
AL
1770 if (name)
1771 free(name);
63a01ef8
AL
1772 return ret;
1773}
1774
8b13c4a7
AL
1775void net_client_uninit(NICInfo *nd)
1776{
1777 nd->vlan->nb_guest_devs--;
1778 nb_nics--;
1779 nd->used = 0;
1780 free((void *)nd->model);
1781}
1782
6f338c34
AL
1783static int net_host_check_device(const char *device)
1784{
1785 int i;
1786 const char *valid_param_list[] = { "tap", "socket"
1787#ifdef CONFIG_SLIRP
1788 ,"user"
1789#endif
1790#ifdef CONFIG_VDE
1791 ,"vde"
1792#endif
1793 };
1794 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
1795 if (!strncmp(valid_param_list[i], device,
1796 strlen(valid_param_list[i])))
1797 return 1;
1798 }
1799
1800 return 0;
1801}
1802
1803void net_host_device_add(const char *device, const char *opts)
1804{
1805 if (!net_host_check_device(device)) {
1806 term_printf("invalid host network device %s\n", device);
1807 return;
1808 }
1809 net_client_init(device, opts);
1810}
1811
1812void net_host_device_remove(int vlan_id, const char *device)
1813{
1814 VLANState *vlan;
1815 VLANClientState *vc;
1816
6f338c34 1817 vlan = qemu_find_vlan(vlan_id);
6f338c34
AL
1818
1819 for(vc = vlan->first_client; vc != NULL; vc = vc->next)
1820 if (!strcmp(vc->name, device))
1821 break;
1822
1823 if (!vc) {
1824 term_printf("can't find device %s\n", device);
1825 return;
1826 }
1827 qemu_del_vlan_client(vc);
1828}
1829
63a01ef8
AL
1830int net_client_parse(const char *str)
1831{
1832 const char *p;
1833 char *q;
1834 char device[64];
1835
1836 p = str;
1837 q = device;
1838 while (*p != '\0' && *p != ',') {
1839 if ((q - device) < sizeof(device) - 1)
1840 *q++ = *p;
1841 p++;
1842 }
1843 *q = '\0';
1844 if (*p == ',')
1845 p++;
1846
1847 return net_client_init(device, p);
1848}
1849
1850void do_info_network(void)
1851{
1852 VLANState *vlan;
1853 VLANClientState *vc;
1854
1855 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1856 term_printf("VLAN %d devices:\n", vlan->id);
1857 for(vc = vlan->first_client; vc != NULL; vc = vc->next)
7cb7434b 1858 term_printf(" %s: %s\n", vc->name, vc->info_str);
63a01ef8
AL
1859 }
1860}
1861
436e5e53
AL
1862int do_set_link(const char *name, const char *up_or_down)
1863{
1864 VLANState *vlan;
1865 VLANClientState *vc = NULL;
1866
1867 for (vlan = first_vlan; vlan != NULL; vlan = vlan->next)
1868 for (vc = vlan->first_client; vc != NULL; vc = vc->next)
1869 if (strcmp(vc->name, name) == 0)
dd5de373
EI
1870 goto done;
1871done:
436e5e53
AL
1872
1873 if (!vc) {
1874 term_printf("could not find network device '%s'", name);
1875 return 0;
1876 }
1877
1878 if (strcmp(up_or_down, "up") == 0)
1879 vc->link_down = 0;
1880 else if (strcmp(up_or_down, "down") == 0)
1881 vc->link_down = 1;
1882 else
1883 term_printf("invalid link status '%s'; only 'up' or 'down' valid\n",
1884 up_or_down);
1885
34b25ca7
AL
1886 if (vc->link_status_changed)
1887 vc->link_status_changed(vc);
1888
436e5e53
AL
1889 return 1;
1890}
1891
63a01ef8
AL
1892void net_cleanup(void)
1893{
1894 VLANState *vlan;
1895
1896#if !defined(_WIN32)
1897 /* close network clients */
1898 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1899 VLANClientState *vc;
1900
1901 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
1902 if (vc->fd_read == tap_receive) {
63a01ef8
AL
1903 TAPState *s = vc->opaque;
1904
973cbd37
AL
1905 if (s->down_script[0])
1906 launch_script(s->down_script, s->down_script_arg, s->fd);
63a01ef8
AL
1907 }
1908#if defined(CONFIG_VDE)
1909 if (vc->fd_read == vde_from_qemu) {
1910 VDEState *s = vc->opaque;
1911 vde_close(s->vde);
1912 }
1913#endif
1914 }
1915 }
1916#endif
1917}
1918
1919void net_client_check(void)
1920{
1921 VLANState *vlan;
1922
1923 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1924 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
1925 continue;
1926 if (vlan->nb_guest_devs == 0)
1927 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
1928 if (vlan->nb_host_devs == 0)
1929 fprintf(stderr,
1930 "Warning: vlan %d is not connected to host network\n",
1931 vlan->id);
1932 }
1933}