]> git.proxmox.com Git - qemu.git/blame - net.c
net: Improve parameter error reporting
[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 */
63a01ef8
AL
24#include <unistd.h>
25#include <fcntl.h>
26#include <signal.h>
27#include <time.h>
28#include <errno.h>
29#include <sys/time.h>
30#include <zlib.h>
31
179a2c19 32/* Needed early for HOST_BSD etc. */
d40cdb10
BS
33#include "config-host.h"
34
63a01ef8
AL
35#ifndef _WIN32
36#include <sys/times.h>
37#include <sys/wait.h>
38#include <termios.h>
39#include <sys/mman.h>
40#include <sys/ioctl.h>
24646c7e 41#include <sys/resource.h>
63a01ef8
AL
42#include <sys/socket.h>
43#include <netinet/in.h>
24646c7e
BS
44#include <net/if.h>
45#ifdef __NetBSD__
46#include <net/if_tap.h>
47#endif
48#ifdef __linux__
49#include <linux/if_tun.h>
50#endif
51#include <arpa/inet.h>
63a01ef8
AL
52#include <dirent.h>
53#include <netdb.h>
54#include <sys/select.h>
179a2c19 55#ifdef HOST_BSD
63a01ef8 56#include <sys/stat.h>
c5e97233 57#if defined(__FreeBSD__) || defined(__DragonFly__)
63a01ef8 58#include <libutil.h>
24646c7e
BS
59#else
60#include <util.h>
63a01ef8
AL
61#endif
62#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
63#include <freebsd/stdlib.h>
64#else
65#ifdef __linux__
63a01ef8
AL
66#include <pty.h>
67#include <malloc.h>
68#include <linux/rtc.h>
69
70/* For the benefit of older linux systems which don't supply it,
71 we use a local copy of hpet.h. */
72/* #include <linux/hpet.h> */
73#include "hpet.h"
74
75#include <linux/ppdev.h>
76#include <linux/parport.h>
77#endif
78#ifdef __sun__
79#include <sys/stat.h>
80#include <sys/ethernet.h>
81#include <sys/sockio.h>
82#include <netinet/arp.h>
83#include <netinet/in.h>
84#include <netinet/in_systm.h>
85#include <netinet/ip.h>
86#include <netinet/ip_icmp.h> // must come after ip.h
87#include <netinet/udp.h>
88#include <netinet/tcp.h>
89#include <net/if.h>
90#include <syslog.h>
91#include <stropts.h>
92#endif
93#endif
94#endif
95
63a01ef8
AL
96#if defined(__OpenBSD__)
97#include <util.h>
98#endif
99
100#if defined(CONFIG_VDE)
101#include <libvdeplug.h>
102#endif
103
104#ifdef _WIN32
49dc768d 105#include <windows.h>
63a01ef8
AL
106#include <malloc.h>
107#include <sys/timeb.h>
108#include <mmsystem.h>
109#define getopt_long_only getopt_long
110#define memalign(align, size) malloc(size)
111#endif
112
511d2b14
BS
113#include "qemu-common.h"
114#include "net.h"
115#include "monitor.h"
116#include "sysemu.h"
117#include "qemu-timer.h"
118#include "qemu-char.h"
119#include "audio/audio.h"
120#include "qemu_socket.h"
bb9ea79e 121#include "qemu-log.h"
511d2b14
BS
122
123#if defined(CONFIG_SLIRP)
124#include "libslirp.h"
125#endif
126
127
63a01ef8
AL
128static VLANState *first_vlan;
129
130/***********************************************************/
131/* network device redirectors */
132
133#if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
134static void hex_dump(FILE *f, const uint8_t *buf, int size)
135{
136 int len, i, j, c;
137
138 for(i=0;i<size;i+=16) {
139 len = size - i;
140 if (len > 16)
141 len = 16;
142 fprintf(f, "%08x ", i);
143 for(j=0;j<16;j++) {
144 if (j < len)
145 fprintf(f, " %02x", buf[i+j]);
146 else
147 fprintf(f, " ");
148 }
149 fprintf(f, " ");
150 for(j=0;j<len;j++) {
151 c = buf[i+j];
152 if (c < ' ' || c > '~')
153 c = '.';
154 fprintf(f, "%c", c);
155 }
156 fprintf(f, "\n");
157 }
158}
159#endif
160
161static int parse_macaddr(uint8_t *macaddr, const char *p)
162{
163 int i;
164 char *last_char;
165 long int offset;
166
167 errno = 0;
168 offset = strtol(p, &last_char, 0);
169 if (0 == errno && '\0' == *last_char &&
170 offset >= 0 && offset <= 0xFFFFFF) {
171 macaddr[3] = (offset & 0xFF0000) >> 16;
172 macaddr[4] = (offset & 0xFF00) >> 8;
173 macaddr[5] = offset & 0xFF;
174 return 0;
175 } else {
176 for(i = 0; i < 6; i++) {
177 macaddr[i] = strtol(p, (char **)&p, 16);
178 if (i == 5) {
179 if (*p != '\0')
180 return -1;
181 } else {
182 if (*p != ':' && *p != '-')
183 return -1;
184 p++;
185 }
186 }
187 return 0;
188 }
189
190 return -1;
191}
192
193static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
194{
195 const char *p, *p1;
196 int len;
197 p = *pp;
198 p1 = strchr(p, sep);
199 if (!p1)
200 return -1;
201 len = p1 - p;
202 p1++;
203 if (buf_size > 0) {
204 if (len > buf_size - 1)
205 len = buf_size - 1;
206 memcpy(buf, p, len);
207 buf[len] = '\0';
208 }
209 *pp = p1;
210 return 0;
211}
212
213int parse_host_src_port(struct sockaddr_in *haddr,
214 struct sockaddr_in *saddr,
215 const char *input_str)
216{
217 char *str = strdup(input_str);
218 char *host_str = str;
219 char *src_str;
220 const char *src_str2;
221 char *ptr;
222
223 /*
224 * Chop off any extra arguments at the end of the string which
225 * would start with a comma, then fill in the src port information
226 * if it was provided else use the "any address" and "any port".
227 */
228 if ((ptr = strchr(str,',')))
229 *ptr = '\0';
230
231 if ((src_str = strchr(input_str,'@'))) {
232 *src_str = '\0';
233 src_str++;
234 }
235
236 if (parse_host_port(haddr, host_str) < 0)
237 goto fail;
238
239 src_str2 = src_str;
240 if (!src_str || *src_str == '\0')
241 src_str2 = ":0";
242
243 if (parse_host_port(saddr, src_str2) < 0)
244 goto fail;
245
246 free(str);
247 return(0);
248
249fail:
250 free(str);
251 return -1;
252}
253
254int parse_host_port(struct sockaddr_in *saddr, const char *str)
255{
256 char buf[512];
257 struct hostent *he;
258 const char *p, *r;
259 int port;
260
261 p = str;
262 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
263 return -1;
264 saddr->sin_family = AF_INET;
265 if (buf[0] == '\0') {
266 saddr->sin_addr.s_addr = 0;
267 } else {
cd390083 268 if (qemu_isdigit(buf[0])) {
63a01ef8
AL
269 if (!inet_aton(buf, &saddr->sin_addr))
270 return -1;
271 } else {
272 if ((he = gethostbyname(buf)) == NULL)
273 return - 1;
274 saddr->sin_addr = *(struct in_addr *)he->h_addr;
275 }
276 }
277 port = strtol(p, (char **)&r, 0);
278 if (r == p)
279 return -1;
280 saddr->sin_port = htons(port);
281 return 0;
282}
283
69d6451c
BS
284#if !defined(_WIN32) && 0
285static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
63a01ef8
AL
286{
287 const char *p;
288 int len;
289
290 len = MIN(108, strlen(str));
291 p = strchr(str, ',');
292 if (p)
293 len = MIN(len, p - str);
294
295 memset(uaddr, 0, sizeof(*uaddr));
296
297 uaddr->sun_family = AF_UNIX;
298 memcpy(uaddr->sun_path, str, len);
299
300 return 0;
301}
302#endif
303
7cb7434b
AL
304void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
305{
306 snprintf(vc->info_str, sizeof(vc->info_str),
4dda4063
AL
307 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
308 vc->model,
7cb7434b
AL
309 macaddr[0], macaddr[1], macaddr[2],
310 macaddr[3], macaddr[4], macaddr[5]);
311}
312
676cff29
AL
313static char *assign_name(VLANClientState *vc1, const char *model)
314{
315 VLANState *vlan;
316 char buf[256];
317 int id = 0;
318
319 for (vlan = first_vlan; vlan; vlan = vlan->next) {
320 VLANClientState *vc;
321
322 for (vc = vlan->first_client; vc; vc = vc->next)
323 if (vc != vc1 && strcmp(vc->model, model) == 0)
324 id++;
325 }
326
327 snprintf(buf, sizeof(buf), "%s.%d", model, id);
328
329 return strdup(buf);
330}
331
63a01ef8 332VLANClientState *qemu_new_vlan_client(VLANState *vlan,
bf38c1a0 333 const char *model,
7a9f6e4a 334 const char *name,
63a01ef8
AL
335 IOReadHandler *fd_read,
336 IOCanRWHandler *fd_can_read,
b946a153 337 NetCleanup *cleanup,
63a01ef8
AL
338 void *opaque)
339{
340 VLANClientState *vc, **pvc;
341 vc = qemu_mallocz(sizeof(VLANClientState));
bf38c1a0 342 vc->model = strdup(model);
7a9f6e4a
AL
343 if (name)
344 vc->name = strdup(name);
345 else
346 vc->name = assign_name(vc, model);
63a01ef8
AL
347 vc->fd_read = fd_read;
348 vc->fd_can_read = fd_can_read;
b946a153 349 vc->cleanup = cleanup;
63a01ef8
AL
350 vc->opaque = opaque;
351 vc->vlan = vlan;
352
353 vc->next = NULL;
354 pvc = &vlan->first_client;
355 while (*pvc != NULL)
356 pvc = &(*pvc)->next;
357 *pvc = vc;
358 return vc;
359}
360
361void qemu_del_vlan_client(VLANClientState *vc)
362{
363 VLANClientState **pvc = &vc->vlan->first_client;
364
365 while (*pvc != NULL)
366 if (*pvc == vc) {
367 *pvc = vc->next;
b946a153
AL
368 if (vc->cleanup) {
369 vc->cleanup(vc);
370 }
676cff29 371 free(vc->name);
bf38c1a0 372 free(vc->model);
dad35419 373 qemu_free(vc);
63a01ef8
AL
374 break;
375 } else
376 pvc = &(*pvc)->next;
377}
378
8b13c4a7
AL
379VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque)
380{
381 VLANClientState **pvc = &vlan->first_client;
382
383 while (*pvc != NULL)
384 if ((*pvc)->opaque == opaque)
385 return *pvc;
386 else
387 pvc = &(*pvc)->next;
388
389 return NULL;
390}
391
63a01ef8
AL
392int qemu_can_send_packet(VLANClientState *vc1)
393{
394 VLANState *vlan = vc1->vlan;
395 VLANClientState *vc;
396
397 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
398 if (vc != vc1) {
399 if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
400 return 1;
401 }
402 }
403 return 0;
404}
405
764a4d1d
AL
406static void
407qemu_deliver_packet(VLANClientState *sender, const uint8_t *buf, int size)
63a01ef8 408{
63a01ef8
AL
409 VLANClientState *vc;
410
764a4d1d
AL
411 for (vc = sender->vlan->first_client; vc != NULL; vc = vc->next) {
412 if (vc != sender && !vc->link_down) {
413 vc->fd_read(vc->opaque, buf, size);
414 }
415 }
416}
417
418void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
419{
420 VLANState *vlan = vc->vlan;
421 VLANPacket *packet;
422
423 if (vc->link_down)
436e5e53
AL
424 return;
425
63a01ef8
AL
426#ifdef DEBUG_NET
427 printf("vlan %d send:\n", vlan->id);
428 hex_dump(stdout, buf, size);
429#endif
764a4d1d
AL
430 if (vlan->delivering) {
431 packet = qemu_malloc(sizeof(VLANPacket) + size);
432 packet->next = vlan->send_queue;
433 packet->sender = vc;
434 packet->size = size;
435 memcpy(packet->data, buf, size);
436 vlan->send_queue = packet;
437 } else {
438 vlan->delivering = 1;
439 qemu_deliver_packet(vc, buf, size);
440 while ((packet = vlan->send_queue) != NULL) {
764a4d1d 441 vlan->send_queue = packet->next;
c27ff608 442 qemu_deliver_packet(packet->sender, packet->data, packet->size);
764a4d1d 443 qemu_free(packet);
63a01ef8 444 }
764a4d1d 445 vlan->delivering = 0;
63a01ef8
AL
446 }
447}
448
fbe78f4f
AL
449static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
450 int iovcnt)
451{
452 uint8_t buffer[4096];
453 size_t offset = 0;
454 int i;
455
456 for (i = 0; i < iovcnt; i++) {
457 size_t len;
458
459 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
460 memcpy(buffer + offset, iov[i].iov_base, len);
461 offset += len;
462 }
463
464 vc->fd_read(vc->opaque, buffer, offset);
465
466 return offset;
467}
468
e0e7877a
AL
469static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
470{
471 size_t offset = 0;
472 int i;
473
474 for (i = 0; i < iovcnt; i++)
475 offset += iov[i].iov_len;
476 return offset;
477}
478
c27ff608 479ssize_t qemu_sendv_packet(VLANClientState *sender, const struct iovec *iov,
fbe78f4f
AL
480 int iovcnt)
481{
c27ff608 482 VLANState *vlan = sender->vlan;
fbe78f4f 483 VLANClientState *vc;
c27ff608 484 VLANPacket *packet;
fbe78f4f 485 ssize_t max_len = 0;
c27ff608 486 int i;
fbe78f4f 487
c27ff608 488 if (sender->link_down)
e0e7877a
AL
489 return calc_iov_length(iov, iovcnt);
490
c27ff608
JK
491 if (vlan->delivering) {
492 max_len = calc_iov_length(iov, iovcnt);
493
494 packet = qemu_malloc(sizeof(VLANPacket) + max_len);
495 packet->next = vlan->send_queue;
496 packet->sender = sender;
497 packet->size = 0;
498 for (i = 0; i < iovcnt; i++) {
499 size_t len = iov[i].iov_len;
500
501 memcpy(packet->data + packet->size, iov[i].iov_base, len);
502 packet->size += len;
503 }
504 vlan->send_queue = packet;
505 } else {
506 vlan->delivering = 1;
fbe78f4f 507
c27ff608
JK
508 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
509 ssize_t len = 0;
fbe78f4f 510
c27ff608
JK
511 if (vc == sender) {
512 continue;
513 }
514 if (vc->link_down) {
515 len = calc_iov_length(iov, iovcnt);
516 } else if (vc->fd_readv) {
517 len = vc->fd_readv(vc->opaque, iov, iovcnt);
518 } else if (vc->fd_read) {
519 len = vc_sendv_compat(vc, iov, iovcnt);
520 }
521 max_len = MAX(max_len, len);
522 }
fbe78f4f 523
c27ff608
JK
524 while ((packet = vlan->send_queue) != NULL) {
525 vlan->send_queue = packet->next;
526 qemu_deliver_packet(packet->sender, packet->data, packet->size);
527 qemu_free(packet);
528 }
529 vlan->delivering = 0;
fbe78f4f
AL
530 }
531
532 return max_len;
533}
534
10ae5a7a
JK
535static void config_error(Monitor *mon, const char *fmt, ...)
536{
537 va_list ap;
538
539 va_start(ap, fmt);
540 if (mon) {
541 monitor_vprintf(mon, fmt, ap);
542 } else {
543 fprintf(stderr, "qemu: ");
544 vfprintf(stderr, fmt, ap);
545 exit(1);
546 }
547 va_end(ap);
548}
549
63a01ef8
AL
550#if defined(CONFIG_SLIRP)
551
552/* slirp network adapter */
553
554static int slirp_inited;
49ec9b40
AL
555static int slirp_restrict;
556static char *slirp_ip;
63a01ef8
AL
557static VLANClientState *slirp_vc;
558
559int slirp_can_output(void)
560{
561 return !slirp_vc || qemu_can_send_packet(slirp_vc);
562}
563
564void slirp_output(const uint8_t *pkt, int pkt_len)
565{
566#ifdef DEBUG_SLIRP
567 printf("slirp output:\n");
568 hex_dump(stdout, pkt, pkt_len);
569#endif
570 if (!slirp_vc)
571 return;
572 qemu_send_packet(slirp_vc, pkt, pkt_len);
573}
574
575int slirp_is_inited(void)
576{
577 return slirp_inited;
578}
579
580static void slirp_receive(void *opaque, const uint8_t *buf, int size)
581{
582#ifdef DEBUG_SLIRP
583 printf("slirp input:\n");
584 hex_dump(stdout, buf, size);
585#endif
586 slirp_input(buf, size);
587}
588
8d6249a7
AL
589static int slirp_in_use;
590
591static void net_slirp_cleanup(VLANClientState *vc)
592{
593 slirp_in_use = 0;
594}
595
7a9f6e4a 596static int net_slirp_init(VLANState *vlan, const char *model, const char *name)
63a01ef8 597{
8d6249a7
AL
598 if (slirp_in_use) {
599 /* slirp only supports a single instance so far */
600 return -1;
601 }
63a01ef8
AL
602 if (!slirp_inited) {
603 slirp_inited = 1;
49ec9b40 604 slirp_init(slirp_restrict, slirp_ip);
63a01ef8 605 }
7a9f6e4a 606 slirp_vc = qemu_new_vlan_client(vlan, model, name,
8d6249a7 607 slirp_receive, NULL, net_slirp_cleanup, NULL);
7cb7434b 608 slirp_vc->info_str[0] = '\0';
8d6249a7 609 slirp_in_use = 1;
63a01ef8
AL
610 return 0;
611}
612
1c6ed9f3
AG
613static void net_slirp_redir_print(void *opaque, int is_udp,
614 struct in_addr *laddr, u_int lport,
615 struct in_addr *faddr, u_int fport)
616{
617 Monitor *mon = (Monitor *)opaque;
618 uint32_t h_addr;
619 uint32_t g_addr;
620 char buf[16];
621
622 h_addr = ntohl(faddr->s_addr);
623 g_addr = ntohl(laddr->s_addr);
624
625 monitor_printf(mon, " %s |", is_udp ? "udp" : "tcp" );
626 snprintf(buf, 15, "%d.%d.%d.%d", (h_addr >> 24) & 0xff,
627 (h_addr >> 16) & 0xff,
628 (h_addr >> 8) & 0xff,
629 (h_addr) & 0xff);
630 monitor_printf(mon, " %15s |", buf);
631 monitor_printf(mon, " %5d |", fport);
632
633 snprintf(buf, 15, "%d.%d.%d.%d", (g_addr >> 24) & 0xff,
634 (g_addr >> 16) & 0xff,
635 (g_addr >> 8) & 0xff,
636 (g_addr) & 0xff);
637 monitor_printf(mon, " %15s |", buf);
638 monitor_printf(mon, " %5d\n", lport);
639
640}
641
642static void net_slirp_redir_list(Monitor *mon)
643{
644 if (!mon)
645 return;
646
647 monitor_printf(mon, " Prot | Host Addr | HPort | Guest Addr | GPort\n");
648 monitor_printf(mon, " | | | | \n");
649 slirp_redir_loop(net_slirp_redir_print, mon);
650}
651
c1261d8d
AG
652static void net_slirp_redir_rm(Monitor *mon, const char *port_str)
653{
654 int host_port;
655 char buf[256] = "";
656 const char *p = port_str;
657 int is_udp = 0;
658 int n;
659
660 if (!mon)
661 return;
662
663 if (!port_str || !port_str[0])
664 goto fail_syntax;
665
666 get_str_sep(buf, sizeof(buf), &p, ':');
667
668 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
669 is_udp = 0;
670 } else if (!strcmp(buf, "udp")) {
671 is_udp = 1;
672 } else {
673 goto fail_syntax;
674 }
675
676 host_port = atoi(p);
677
678 n = slirp_redir_rm(is_udp, host_port);
679
680 monitor_printf(mon, "removed %d redirections to %s port %d\n", n,
681 is_udp ? "udp" : "tcp", host_port);
682 return;
683
684 fail_syntax:
685 monitor_printf(mon, "invalid format\n");
686}
687
688void net_slirp_redir(Monitor *mon, const char *redir_str, const char *redir_opt2)
63a01ef8
AL
689{
690 int is_udp;
691 char buf[256], *r;
10ae5a7a 692 const char *p;
63a01ef8
AL
693 struct in_addr guest_addr;
694 int host_port, guest_port;
695
696 if (!slirp_inited) {
697 slirp_inited = 1;
49ec9b40 698 slirp_init(slirp_restrict, slirp_ip);
63a01ef8
AL
699 }
700
c1261d8d
AG
701 if (!strcmp(redir_str, "remove")) {
702 net_slirp_redir_rm(mon, redir_opt2);
703 return;
704 }
705
1c6ed9f3
AG
706 if (!strcmp(redir_str, "list")) {
707 net_slirp_redir_list(mon);
708 return;
709 }
710
63a01ef8
AL
711 p = redir_str;
712 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
d4ebe193
AL
713 goto fail_syntax;
714 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
63a01ef8
AL
715 is_udp = 0;
716 } else if (!strcmp(buf, "udp")) {
717 is_udp = 1;
718 } else {
d4ebe193 719 goto fail_syntax;
63a01ef8
AL
720 }
721
722 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
d4ebe193 723 goto fail_syntax;
63a01ef8
AL
724 host_port = strtol(buf, &r, 0);
725 if (r == buf)
d4ebe193 726 goto fail_syntax;
63a01ef8
AL
727
728 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
d4ebe193 729 goto fail_syntax;
63a01ef8
AL
730 if (buf[0] == '\0') {
731 pstrcpy(buf, sizeof(buf), "10.0.2.15");
732 }
733 if (!inet_aton(buf, &guest_addr))
d4ebe193 734 goto fail_syntax;
63a01ef8
AL
735
736 guest_port = strtol(p, &r, 0);
737 if (r == p)
d4ebe193 738 goto fail_syntax;
63a01ef8
AL
739
740 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
10ae5a7a 741 config_error(mon, "could not set up redirection '%s'\n", redir_str);
63a01ef8
AL
742 }
743 return;
d4ebe193
AL
744
745 fail_syntax:
10ae5a7a 746 config_error(mon, "invalid redirection format '%s'\n", redir_str);
63a01ef8
AL
747}
748
749#ifndef _WIN32
750
751static char smb_dir[1024];
752
753static void erase_dir(char *dir_name)
754{
755 DIR *d;
756 struct dirent *de;
757 char filename[1024];
758
759 /* erase all the files in the directory */
511d2b14 760 if ((d = opendir(dir_name)) != NULL) {
63a01ef8
AL
761 for(;;) {
762 de = readdir(d);
763 if (!de)
764 break;
765 if (strcmp(de->d_name, ".") != 0 &&
766 strcmp(de->d_name, "..") != 0) {
767 snprintf(filename, sizeof(filename), "%s/%s",
768 smb_dir, de->d_name);
769 if (unlink(filename) != 0) /* is it a directory? */
770 erase_dir(filename);
771 }
772 }
773 closedir(d);
774 rmdir(dir_name);
775 }
776}
777
778/* automatic user mode samba server configuration */
779static void smb_exit(void)
780{
781 erase_dir(smb_dir);
782}
783
784/* automatic user mode samba server configuration */
785void net_slirp_smb(const char *exported_dir)
786{
787 char smb_conf[1024];
788 char smb_cmdline[1024];
789 FILE *f;
790
791 if (!slirp_inited) {
792 slirp_inited = 1;
49ec9b40 793 slirp_init(slirp_restrict, slirp_ip);
63a01ef8
AL
794 }
795
796 /* XXX: better tmp dir construction */
3f4cb3d3 797 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%ld", (long)getpid());
63a01ef8
AL
798 if (mkdir(smb_dir, 0700) < 0) {
799 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
800 exit(1);
801 }
802 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
803
804 f = fopen(smb_conf, "w");
805 if (!f) {
806 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
807 exit(1);
808 }
809 fprintf(f,
810 "[global]\n"
811 "private dir=%s\n"
812 "smb ports=0\n"
813 "socket address=127.0.0.1\n"
814 "pid directory=%s\n"
815 "lock directory=%s\n"
816 "log file=%s/log.smbd\n"
817 "smb passwd file=%s/smbpasswd\n"
818 "security = share\n"
819 "[qemu]\n"
820 "path=%s\n"
821 "read only=no\n"
822 "guest ok=yes\n",
823 smb_dir,
824 smb_dir,
825 smb_dir,
826 smb_dir,
827 smb_dir,
828 exported_dir
829 );
830 fclose(f);
831 atexit(smb_exit);
832
833 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
834 SMBD_COMMAND, smb_conf);
835
836 slirp_add_exec(0, smb_cmdline, 4, 139);
837}
838
839#endif /* !defined(_WIN32) */
376253ec 840void do_info_slirp(Monitor *mon)
63a01ef8
AL
841{
842 slirp_stats();
843}
844
8ca9217d
AL
845struct VMChannel {
846 CharDriverState *hd;
847 int port;
511d2b14 848};
8ca9217d
AL
849
850static int vmchannel_can_read(void *opaque)
851{
852 struct VMChannel *vmc = (struct VMChannel*)opaque;
853 return slirp_socket_can_recv(4, vmc->port);
854}
855
856static void vmchannel_read(void *opaque, const uint8_t *buf, int size)
857{
858 struct VMChannel *vmc = (struct VMChannel*)opaque;
859 slirp_socket_recv(4, vmc->port, buf, size);
860}
861
63a01ef8
AL
862#endif /* CONFIG_SLIRP */
863
864#if !defined(_WIN32)
865
866typedef struct TAPState {
867 VLANClientState *vc;
868 int fd;
869 char down_script[1024];
973cbd37 870 char down_script_arg[128];
63a01ef8
AL
871} TAPState;
872
b946a153
AL
873static int launch_script(const char *setup_script, const char *ifname, int fd);
874
b535b7b2
AL
875static ssize_t tap_receive_iov(void *opaque, const struct iovec *iov,
876 int iovcnt)
877{
878 TAPState *s = opaque;
879 ssize_t len;
880
881 do {
882 len = writev(s->fd, iov, iovcnt);
883 } while (len == -1 && (errno == EINTR || errno == EAGAIN));
884
885 return len;
886}
b535b7b2 887
63a01ef8
AL
888static void tap_receive(void *opaque, const uint8_t *buf, int size)
889{
890 TAPState *s = opaque;
891 int ret;
892 for(;;) {
893 ret = write(s->fd, buf, size);
894 if (ret < 0 && (errno == EINTR || errno == EAGAIN)) {
895 } else {
896 break;
897 }
898 }
899}
900
901static void tap_send(void *opaque)
902{
903 TAPState *s = opaque;
904 uint8_t buf[4096];
905 int size;
906
907#ifdef __sun__
908 struct strbuf sbuf;
909 int f = 0;
910 sbuf.maxlen = sizeof(buf);
3f4cb3d3 911 sbuf.buf = (char *)buf;
63a01ef8
AL
912 size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
913#else
914 size = read(s->fd, buf, sizeof(buf));
915#endif
916 if (size > 0) {
917 qemu_send_packet(s->vc, buf, size);
918 }
919}
920
b946a153
AL
921static void tap_cleanup(VLANClientState *vc)
922{
923 TAPState *s = vc->opaque;
924
925 if (s->down_script[0])
926 launch_script(s->down_script, s->down_script_arg, s->fd);
927
928 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
929 close(s->fd);
930 qemu_free(s);
931}
932
63a01ef8
AL
933/* fd support */
934
7a9f6e4a
AL
935static TAPState *net_tap_fd_init(VLANState *vlan,
936 const char *model,
937 const char *name,
938 int fd)
63a01ef8
AL
939{
940 TAPState *s;
941
942 s = qemu_mallocz(sizeof(TAPState));
63a01ef8 943 s->fd = fd;
b946a153
AL
944 s->vc = qemu_new_vlan_client(vlan, model, name, tap_receive,
945 NULL, tap_cleanup, s);
b535b7b2 946 s->vc->fd_readv = tap_receive_iov;
63a01ef8 947 qemu_set_fd_handler(s->fd, tap_send, NULL, s);
7cb7434b 948 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd);
63a01ef8
AL
949 return s;
950}
951
179a2c19 952#if defined (HOST_BSD) || defined (__FreeBSD_kernel__)
63a01ef8
AL
953static int tap_open(char *ifname, int ifname_size)
954{
955 int fd;
956 char *dev;
957 struct stat s;
958
959 TFR(fd = open("/dev/tap", O_RDWR));
960 if (fd < 0) {
961 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
962 return -1;
963 }
964
965 fstat(fd, &s);
966 dev = devname(s.st_rdev, S_IFCHR);
967 pstrcpy(ifname, ifname_size, dev);
968
969 fcntl(fd, F_SETFL, O_NONBLOCK);
970 return fd;
971}
972#elif defined(__sun__)
973#define TUNNEWPPA (('T'<<16) | 0x0001)
974/*
975 * Allocate TAP device, returns opened fd.
976 * Stores dev name in the first arg(must be large enough).
977 */
3f4cb3d3 978static int tap_alloc(char *dev, size_t dev_size)
63a01ef8
AL
979{
980 int tap_fd, if_fd, ppa = -1;
981 static int ip_fd = 0;
982 char *ptr;
983
984 static int arp_fd = 0;
985 int ip_muxid, arp_muxid;
986 struct strioctl strioc_if, strioc_ppa;
987 int link_type = I_PLINK;;
988 struct lifreq ifr;
989 char actual_name[32] = "";
990
991 memset(&ifr, 0x0, sizeof(ifr));
992
993 if( *dev ){
994 ptr = dev;
47398b9c 995 while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
63a01ef8
AL
996 ppa = atoi(ptr);
997 }
998
999 /* Check if IP device was opened */
1000 if( ip_fd )
1001 close(ip_fd);
1002
1003 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
1004 if (ip_fd < 0) {
1005 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
1006 return -1;
1007 }
1008
1009 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
1010 if (tap_fd < 0) {
1011 syslog(LOG_ERR, "Can't open /dev/tap");
1012 return -1;
1013 }
1014
1015 /* Assign a new PPA and get its unit number. */
1016 strioc_ppa.ic_cmd = TUNNEWPPA;
1017 strioc_ppa.ic_timout = 0;
1018 strioc_ppa.ic_len = sizeof(ppa);
1019 strioc_ppa.ic_dp = (char *)&ppa;
1020 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
1021 syslog (LOG_ERR, "Can't assign new interface");
1022
1023 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
1024 if (if_fd < 0) {
1025 syslog(LOG_ERR, "Can't open /dev/tap (2)");
1026 return -1;
1027 }
1028 if(ioctl(if_fd, I_PUSH, "ip") < 0){
1029 syslog(LOG_ERR, "Can't push IP module");
1030 return -1;
1031 }
1032
1033 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
1034 syslog(LOG_ERR, "Can't get flags\n");
1035
1036 snprintf (actual_name, 32, "tap%d", ppa);
1037 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1038
1039 ifr.lifr_ppa = ppa;
1040 /* Assign ppa according to the unit number returned by tun device */
1041
1042 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
1043 syslog (LOG_ERR, "Can't set PPA %d", ppa);
1044 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
1045 syslog (LOG_ERR, "Can't get flags\n");
1046 /* Push arp module to if_fd */
1047 if (ioctl (if_fd, I_PUSH, "arp") < 0)
1048 syslog (LOG_ERR, "Can't push ARP module (2)");
1049
1050 /* Push arp module to ip_fd */
1051 if (ioctl (ip_fd, I_POP, NULL) < 0)
1052 syslog (LOG_ERR, "I_POP failed\n");
1053 if (ioctl (ip_fd, I_PUSH, "arp") < 0)
1054 syslog (LOG_ERR, "Can't push ARP module (3)\n");
1055 /* Open arp_fd */
1056 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
1057 if (arp_fd < 0)
1058 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
1059
1060 /* Set ifname to arp */
1061 strioc_if.ic_cmd = SIOCSLIFNAME;
1062 strioc_if.ic_timout = 0;
1063 strioc_if.ic_len = sizeof(ifr);
1064 strioc_if.ic_dp = (char *)&ifr;
1065 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
1066 syslog (LOG_ERR, "Can't set ifname to arp\n");
1067 }
1068
1069 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
1070 syslog(LOG_ERR, "Can't link TAP device to IP");
1071 return -1;
1072 }
1073
1074 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
1075 syslog (LOG_ERR, "Can't link TAP device to ARP");
1076
1077 close (if_fd);
1078
1079 memset(&ifr, 0x0, sizeof(ifr));
1080 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1081 ifr.lifr_ip_muxid = ip_muxid;
1082 ifr.lifr_arp_muxid = arp_muxid;
1083
1084 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
1085 {
1086 ioctl (ip_fd, I_PUNLINK , arp_muxid);
1087 ioctl (ip_fd, I_PUNLINK, ip_muxid);
1088 syslog (LOG_ERR, "Can't set multiplexor id");
1089 }
1090
1091 snprintf(dev, dev_size, "tap%d", ppa);
1092 return tap_fd;
1093}
1094
1095static int tap_open(char *ifname, int ifname_size)
1096{
1097 char dev[10]="";
1098 int fd;
1099 if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
1100 fprintf(stderr, "Cannot allocate TAP device\n");
1101 return -1;
1102 }
1103 pstrcpy(ifname, ifname_size, dev);
1104 fcntl(fd, F_SETFL, O_NONBLOCK);
1105 return fd;
1106}
b29fe3ed 1107#elif defined (_AIX)
1108static int tap_open(char *ifname, int ifname_size)
1109{
1110 fprintf (stderr, "no tap on AIX\n");
1111 return -1;
1112}
63a01ef8
AL
1113#else
1114static int tap_open(char *ifname, int ifname_size)
1115{
1116 struct ifreq ifr;
1117 int fd, ret;
1118
1119 TFR(fd = open("/dev/net/tun", O_RDWR));
1120 if (fd < 0) {
1121 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1122 return -1;
1123 }
1124 memset(&ifr, 0, sizeof(ifr));
1125 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1126 if (ifname[0] != '\0')
1127 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
1128 else
1129 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
1130 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1131 if (ret != 0) {
1132 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1133 close(fd);
1134 return -1;
1135 }
1136 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1137 fcntl(fd, F_SETFL, O_NONBLOCK);
1138 return fd;
1139}
1140#endif
1141
1142static int launch_script(const char *setup_script, const char *ifname, int fd)
1143{
7c3370d4 1144 sigset_t oldmask, mask;
63a01ef8
AL
1145 int pid, status;
1146 char *args[3];
1147 char **parg;
1148
7c3370d4
JK
1149 sigemptyset(&mask);
1150 sigaddset(&mask, SIGCHLD);
1151 sigprocmask(SIG_BLOCK, &mask, &oldmask);
1152
1153 /* try to launch network script */
1154 pid = fork();
1155 if (pid == 0) {
1156 int open_max = sysconf(_SC_OPEN_MAX), i;
1157
1158 for (i = 0; i < open_max; i++) {
1159 if (i != STDIN_FILENO &&
1160 i != STDOUT_FILENO &&
1161 i != STDERR_FILENO &&
1162 i != fd) {
1163 close(i);
63a01ef8
AL
1164 }
1165 }
7c3370d4
JK
1166 parg = args;
1167 *parg++ = (char *)setup_script;
1168 *parg++ = (char *)ifname;
1169 *parg++ = NULL;
1170 execv(setup_script, args);
1171 _exit(1);
1172 } else if (pid > 0) {
1173 while (waitpid(pid, &status, 0) != pid) {
1174 /* loop */
1175 }
1176 sigprocmask(SIG_SETMASK, &oldmask, NULL);
1177
1178 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1179 return 0;
1180 }
1181 }
1182 fprintf(stderr, "%s: could not launch network script\n", setup_script);
1183 return -1;
63a01ef8
AL
1184}
1185
7a9f6e4a
AL
1186static int net_tap_init(VLANState *vlan, const char *model,
1187 const char *name, const char *ifname1,
63a01ef8
AL
1188 const char *setup_script, const char *down_script)
1189{
1190 TAPState *s;
1191 int fd;
1192 char ifname[128];
1193
1194 if (ifname1 != NULL)
1195 pstrcpy(ifname, sizeof(ifname), ifname1);
1196 else
1197 ifname[0] = '\0';
1198 TFR(fd = tap_open(ifname, sizeof(ifname)));
1199 if (fd < 0)
1200 return -1;
1201
1202 if (!setup_script || !strcmp(setup_script, "no"))
1203 setup_script = "";
1204 if (setup_script[0] != '\0') {
1205 if (launch_script(setup_script, ifname, fd))
1206 return -1;
1207 }
7a9f6e4a 1208 s = net_tap_fd_init(vlan, model, name, fd);
63a01ef8 1209 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
7cb7434b
AL
1210 "ifname=%s,script=%s,downscript=%s",
1211 ifname, setup_script, down_script);
973cbd37 1212 if (down_script && strcmp(down_script, "no")) {
63a01ef8 1213 snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
973cbd37
AL
1214 snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
1215 }
63a01ef8
AL
1216 return 0;
1217}
1218
1219#endif /* !_WIN32 */
1220
1221#if defined(CONFIG_VDE)
1222typedef struct VDEState {
1223 VLANClientState *vc;
1224 VDECONN *vde;
1225} VDEState;
1226
1227static void vde_to_qemu(void *opaque)
1228{
1229 VDEState *s = opaque;
1230 uint8_t buf[4096];
1231 int size;
1232
cc63bb0f 1233 size = vde_recv(s->vde, (char *)buf, sizeof(buf), 0);
63a01ef8
AL
1234 if (size > 0) {
1235 qemu_send_packet(s->vc, buf, size);
1236 }
1237}
1238
1239static void vde_from_qemu(void *opaque, const uint8_t *buf, int size)
1240{
1241 VDEState *s = opaque;
1242 int ret;
1243 for(;;) {
cc63bb0f 1244 ret = vde_send(s->vde, (const char *)buf, size, 0);
63a01ef8
AL
1245 if (ret < 0 && errno == EINTR) {
1246 } else {
1247 break;
1248 }
1249 }
1250}
1251
b946a153
AL
1252static void vde_cleanup(VLANClientState *vc)
1253{
1254 VDEState *s = vc->opaque;
1255 qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL);
1256 vde_close(s->vde);
1257 qemu_free(s);
1258}
1259
7a9f6e4a
AL
1260static int net_vde_init(VLANState *vlan, const char *model,
1261 const char *name, const char *sock,
bf38c1a0 1262 int port, const char *group, int mode)
63a01ef8
AL
1263{
1264 VDEState *s;
1265 char *init_group = strlen(group) ? (char *)group : NULL;
1266 char *init_sock = strlen(sock) ? (char *)sock : NULL;
1267
1268 struct vde_open_args args = {
1269 .port = port,
1270 .group = init_group,
1271 .mode = mode,
1272 };
1273
1274 s = qemu_mallocz(sizeof(VDEState));
cc63bb0f 1275 s->vde = vde_open(init_sock, (char *)"QEMU", &args);
63a01ef8
AL
1276 if (!s->vde){
1277 free(s);
1278 return -1;
1279 }
b946a153
AL
1280 s->vc = qemu_new_vlan_client(vlan, model, name, vde_from_qemu,
1281 NULL, vde_cleanup, s);
63a01ef8 1282 qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
7cb7434b 1283 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d",
63a01ef8
AL
1284 sock, vde_datafd(s->vde));
1285 return 0;
1286}
1287#endif
1288
1289/* network connection */
1290typedef struct NetSocketState {
1291 VLANClientState *vc;
1292 int fd;
1293 int state; /* 0 = getting length, 1 = getting data */
abcd2baa
AL
1294 unsigned int index;
1295 unsigned int packet_len;
63a01ef8
AL
1296 uint8_t buf[4096];
1297 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1298} NetSocketState;
1299
1300typedef struct NetSocketListenState {
1301 VLANState *vlan;
bf38c1a0 1302 char *model;
7a9f6e4a 1303 char *name;
63a01ef8
AL
1304 int fd;
1305} NetSocketListenState;
1306
1307/* XXX: we consider we can send the whole packet without blocking */
1308static void net_socket_receive(void *opaque, const uint8_t *buf, int size)
1309{
1310 NetSocketState *s = opaque;
1311 uint32_t len;
1312 len = htonl(size);
1313
1314 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
1315 send_all(s->fd, buf, size);
1316}
1317
1318static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size)
1319{
1320 NetSocketState *s = opaque;
1321 sendto(s->fd, buf, size, 0,
1322 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
1323}
1324
1325static void net_socket_send(void *opaque)
1326{
1327 NetSocketState *s = opaque;
abcd2baa
AL
1328 int size, err;
1329 unsigned l;
63a01ef8
AL
1330 uint8_t buf1[4096];
1331 const uint8_t *buf;
1332
1333 size = recv(s->fd, buf1, sizeof(buf1), 0);
1334 if (size < 0) {
1335 err = socket_error();
1336 if (err != EWOULDBLOCK)
1337 goto eoc;
1338 } else if (size == 0) {
1339 /* end of connection */
1340 eoc:
1341 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1342 closesocket(s->fd);
1343 return;
1344 }
1345 buf = buf1;
1346 while (size > 0) {
1347 /* reassemble a packet from the network */
1348 switch(s->state) {
1349 case 0:
1350 l = 4 - s->index;
1351 if (l > size)
1352 l = size;
1353 memcpy(s->buf + s->index, buf, l);
1354 buf += l;
1355 size -= l;
1356 s->index += l;
1357 if (s->index == 4) {
1358 /* got length */
1359 s->packet_len = ntohl(*(uint32_t *)s->buf);
1360 s->index = 0;
1361 s->state = 1;
1362 }
1363 break;
1364 case 1:
1365 l = s->packet_len - s->index;
1366 if (l > size)
1367 l = size;
abcd2baa
AL
1368 if (s->index + l <= sizeof(s->buf)) {
1369 memcpy(s->buf + s->index, buf, l);
1370 } else {
1371 fprintf(stderr, "serious error: oversized packet received,"
1372 "connection terminated.\n");
1373 s->state = 0;
1374 goto eoc;
1375 }
1376
63a01ef8
AL
1377 s->index += l;
1378 buf += l;
1379 size -= l;
1380 if (s->index >= s->packet_len) {
1381 qemu_send_packet(s->vc, s->buf, s->packet_len);
1382 s->index = 0;
1383 s->state = 0;
1384 }
1385 break;
1386 }
1387 }
1388}
1389
1390static void net_socket_send_dgram(void *opaque)
1391{
1392 NetSocketState *s = opaque;
1393 int size;
1394
1395 size = recv(s->fd, s->buf, sizeof(s->buf), 0);
1396 if (size < 0)
1397 return;
1398 if (size == 0) {
1399 /* end of connection */
1400 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1401 return;
1402 }
1403 qemu_send_packet(s->vc, s->buf, size);
1404}
1405
1406static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
1407{
1408 struct ip_mreq imr;
1409 int fd;
1410 int val, ret;
1411 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
1412 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1413 inet_ntoa(mcastaddr->sin_addr),
1414 (int)ntohl(mcastaddr->sin_addr.s_addr));
1415 return -1;
1416
1417 }
1418 fd = socket(PF_INET, SOCK_DGRAM, 0);
1419 if (fd < 0) {
1420 perror("socket(PF_INET, SOCK_DGRAM)");
1421 return -1;
1422 }
1423
1424 val = 1;
1425 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1426 (const char *)&val, sizeof(val));
1427 if (ret < 0) {
1428 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1429 goto fail;
1430 }
1431
1432 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
1433 if (ret < 0) {
1434 perror("bind");
1435 goto fail;
1436 }
1437
1438 /* Add host to multicast group */
1439 imr.imr_multiaddr = mcastaddr->sin_addr;
1440 imr.imr_interface.s_addr = htonl(INADDR_ANY);
1441
1442 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1443 (const char *)&imr, sizeof(struct ip_mreq));
1444 if (ret < 0) {
1445 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1446 goto fail;
1447 }
1448
1449 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1450 val = 1;
1451 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1452 (const char *)&val, sizeof(val));
1453 if (ret < 0) {
1454 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1455 goto fail;
1456 }
1457
1458 socket_set_nonblock(fd);
1459 return fd;
1460fail:
1461 if (fd >= 0)
1462 closesocket(fd);
1463 return -1;
1464}
1465
b946a153
AL
1466static void net_socket_cleanup(VLANClientState *vc)
1467{
1468 NetSocketState *s = vc->opaque;
1469 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1470 close(s->fd);
1471 qemu_free(s);
1472}
1473
7a9f6e4a
AL
1474static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
1475 const char *model,
1476 const char *name,
bf38c1a0 1477 int fd, int is_connected)
63a01ef8
AL
1478{
1479 struct sockaddr_in saddr;
1480 int newfd;
1481 socklen_t saddr_len;
1482 NetSocketState *s;
1483
1484 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1485 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1486 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1487 */
1488
1489 if (is_connected) {
1490 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
1491 /* must be bound */
1492 if (saddr.sin_addr.s_addr==0) {
1493 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1494 fd);
1495 return NULL;
1496 }
1497 /* clone dgram socket */
1498 newfd = net_socket_mcast_create(&saddr);
1499 if (newfd < 0) {
1500 /* error already reported by net_socket_mcast_create() */
1501 close(fd);
1502 return NULL;
1503 }
1504 /* clone newfd to fd, close newfd */
1505 dup2(newfd, fd);
1506 close(newfd);
1507
1508 } else {
1509 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1510 fd, strerror(errno));
1511 return NULL;
1512 }
1513 }
1514
1515 s = qemu_mallocz(sizeof(NetSocketState));
63a01ef8
AL
1516 s->fd = fd;
1517
b946a153
AL
1518 s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive_dgram,
1519 NULL, net_socket_cleanup, s);
63a01ef8
AL
1520 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
1521
1522 /* mcast: save bound address as dst */
1523 if (is_connected) s->dgram_dst=saddr;
1524
1525 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1526 "socket: fd=%d (%s mcast=%s:%d)",
1527 fd, is_connected? "cloned" : "",
1528 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1529 return s;
1530}
1531
1532static void net_socket_connect(void *opaque)
1533{
1534 NetSocketState *s = opaque;
1535 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
1536}
1537
7a9f6e4a
AL
1538static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
1539 const char *model,
1540 const char *name,
bf38c1a0 1541 int fd, int is_connected)
63a01ef8
AL
1542{
1543 NetSocketState *s;
1544 s = qemu_mallocz(sizeof(NetSocketState));
63a01ef8 1545 s->fd = fd;
b946a153
AL
1546 s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive,
1547 NULL, net_socket_cleanup, s);
63a01ef8
AL
1548 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1549 "socket: fd=%d", fd);
1550 if (is_connected) {
1551 net_socket_connect(s);
1552 } else {
1553 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
1554 }
1555 return s;
1556}
1557
7a9f6e4a
AL
1558static NetSocketState *net_socket_fd_init(VLANState *vlan,
1559 const char *model, const char *name,
bf38c1a0 1560 int fd, int is_connected)
63a01ef8
AL
1561{
1562 int so_type=-1, optlen=sizeof(so_type);
1563
1564 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
1565 (socklen_t *)&optlen)< 0) {
1566 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
1567 return NULL;
1568 }
1569 switch(so_type) {
1570 case SOCK_DGRAM:
7a9f6e4a 1571 return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected);
63a01ef8 1572 case SOCK_STREAM:
7a9f6e4a 1573 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
63a01ef8
AL
1574 default:
1575 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1576 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
7a9f6e4a 1577 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
63a01ef8
AL
1578 }
1579 return NULL;
1580}
1581
1582static void net_socket_accept(void *opaque)
1583{
1584 NetSocketListenState *s = opaque;
1585 NetSocketState *s1;
1586 struct sockaddr_in saddr;
1587 socklen_t len;
1588 int fd;
1589
1590 for(;;) {
1591 len = sizeof(saddr);
1592 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
1593 if (fd < 0 && errno != EINTR) {
1594 return;
1595 } else if (fd >= 0) {
1596 break;
1597 }
1598 }
7a9f6e4a 1599 s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1);
63a01ef8
AL
1600 if (!s1) {
1601 closesocket(fd);
1602 } else {
1603 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
1604 "socket: connection from %s:%d",
1605 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1606 }
1607}
1608
7a9f6e4a
AL
1609static int net_socket_listen_init(VLANState *vlan,
1610 const char *model,
1611 const char *name,
bf38c1a0 1612 const char *host_str)
63a01ef8
AL
1613{
1614 NetSocketListenState *s;
1615 int fd, val, ret;
1616 struct sockaddr_in saddr;
1617
1618 if (parse_host_port(&saddr, host_str) < 0)
1619 return -1;
1620
1621 s = qemu_mallocz(sizeof(NetSocketListenState));
63a01ef8
AL
1622
1623 fd = socket(PF_INET, SOCK_STREAM, 0);
1624 if (fd < 0) {
1625 perror("socket");
1626 return -1;
1627 }
1628 socket_set_nonblock(fd);
1629
1630 /* allow fast reuse */
1631 val = 1;
1632 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
1633
1634 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1635 if (ret < 0) {
1636 perror("bind");
1637 return -1;
1638 }
1639 ret = listen(fd, 0);
1640 if (ret < 0) {
1641 perror("listen");
1642 return -1;
1643 }
1644 s->vlan = vlan;
bf38c1a0 1645 s->model = strdup(model);
ea053add 1646 s->name = name ? strdup(name) : NULL;
63a01ef8
AL
1647 s->fd = fd;
1648 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
1649 return 0;
1650}
1651
7a9f6e4a
AL
1652static int net_socket_connect_init(VLANState *vlan,
1653 const char *model,
1654 const char *name,
bf38c1a0 1655 const char *host_str)
63a01ef8
AL
1656{
1657 NetSocketState *s;
1658 int fd, connected, ret, err;
1659 struct sockaddr_in saddr;
1660
1661 if (parse_host_port(&saddr, host_str) < 0)
1662 return -1;
1663
1664 fd = socket(PF_INET, SOCK_STREAM, 0);
1665 if (fd < 0) {
1666 perror("socket");
1667 return -1;
1668 }
1669 socket_set_nonblock(fd);
1670
1671 connected = 0;
1672 for(;;) {
1673 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1674 if (ret < 0) {
1675 err = socket_error();
1676 if (err == EINTR || err == EWOULDBLOCK) {
1677 } else if (err == EINPROGRESS) {
1678 break;
1679#ifdef _WIN32
1680 } else if (err == WSAEALREADY) {
1681 break;
1682#endif
1683 } else {
1684 perror("connect");
1685 closesocket(fd);
1686 return -1;
1687 }
1688 } else {
1689 connected = 1;
1690 break;
1691 }
1692 }
7a9f6e4a 1693 s = net_socket_fd_init(vlan, model, name, fd, connected);
63a01ef8
AL
1694 if (!s)
1695 return -1;
1696 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1697 "socket: connect to %s:%d",
1698 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1699 return 0;
1700}
1701
7a9f6e4a
AL
1702static int net_socket_mcast_init(VLANState *vlan,
1703 const char *model,
1704 const char *name,
bf38c1a0 1705 const char *host_str)
63a01ef8
AL
1706{
1707 NetSocketState *s;
1708 int fd;
1709 struct sockaddr_in saddr;
1710
1711 if (parse_host_port(&saddr, host_str) < 0)
1712 return -1;
1713
1714
1715 fd = net_socket_mcast_create(&saddr);
1716 if (fd < 0)
1717 return -1;
1718
7a9f6e4a 1719 s = net_socket_fd_init(vlan, model, name, fd, 0);
63a01ef8
AL
1720 if (!s)
1721 return -1;
1722
1723 s->dgram_dst = saddr;
1724
1725 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1726 "socket: mcast=%s:%d",
1727 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1728 return 0;
1729
1730}
1731
bb9ea79e
AL
1732typedef struct DumpState {
1733 VLANClientState *pcap_vc;
1734 int fd;
1735 int pcap_caplen;
1736} DumpState;
1737
1738#define PCAP_MAGIC 0xa1b2c3d4
1739
1740struct pcap_file_hdr {
1741 uint32_t magic;
1742 uint16_t version_major;
1743 uint16_t version_minor;
1744 int32_t thiszone;
1745 uint32_t sigfigs;
1746 uint32_t snaplen;
1747 uint32_t linktype;
1748};
1749
1750struct pcap_sf_pkthdr {
1751 struct {
1752 int32_t tv_sec;
1753 int32_t tv_usec;
1754 } ts;
1755 uint32_t caplen;
1756 uint32_t len;
1757};
1758
1759static void dump_receive(void *opaque, const uint8_t *buf, int size)
1760{
1761 DumpState *s = opaque;
1762 struct pcap_sf_pkthdr hdr;
1763 int64_t ts;
1764 int caplen;
1765
1766 /* Early return in case of previous error. */
1767 if (s->fd < 0) {
1768 return;
1769 }
1770
37cb6fc3 1771 ts = muldiv64(qemu_get_clock(vm_clock), 1000000, ticks_per_sec);
bb9ea79e
AL
1772 caplen = size > s->pcap_caplen ? s->pcap_caplen : size;
1773
37cb6fc3 1774 hdr.ts.tv_sec = ts / 1000000;
bb9ea79e
AL
1775 hdr.ts.tv_usec = ts % 1000000;
1776 hdr.caplen = caplen;
1777 hdr.len = size;
1778 if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) ||
1779 write(s->fd, buf, caplen) != caplen) {
1780 qemu_log("-net dump write error - stop dump\n");
1781 close(s->fd);
1782 s->fd = -1;
1783 }
1784}
1785
1786static void net_dump_cleanup(VLANClientState *vc)
1787{
1788 DumpState *s = vc->opaque;
1789
1790 close(s->fd);
1791 qemu_free(s);
1792}
1793
10ae5a7a 1794static int net_dump_init(Monitor *mon, VLANState *vlan, const char *device,
bb9ea79e
AL
1795 const char *name, const char *filename, int len)
1796{
1797 struct pcap_file_hdr hdr;
1798 DumpState *s;
1799
1800 s = qemu_malloc(sizeof(DumpState));
1801
1802 s->fd = open(filename, O_CREAT | O_WRONLY, 0644);
1803 if (s->fd < 0) {
10ae5a7a 1804 config_error(mon, "-net dump: can't open %s\n", filename);
bb9ea79e
AL
1805 return -1;
1806 }
1807
1808 s->pcap_caplen = len;
1809
1810 hdr.magic = PCAP_MAGIC;
1811 hdr.version_major = 2;
1812 hdr.version_minor = 4;
1813 hdr.thiszone = 0;
1814 hdr.sigfigs = 0;
1815 hdr.snaplen = s->pcap_caplen;
1816 hdr.linktype = 1;
1817
1818 if (write(s->fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
10ae5a7a 1819 config_error(mon, "-net dump write error: %s\n", strerror(errno));
bb9ea79e
AL
1820 close(s->fd);
1821 qemu_free(s);
1822 return -1;
1823 }
1824
1825 s->pcap_vc = qemu_new_vlan_client(vlan, device, name, dump_receive, NULL,
1826 net_dump_cleanup, s);
1827 snprintf(s->pcap_vc->info_str, sizeof(s->pcap_vc->info_str),
1828 "dump to %s (len=%d)", filename, len);
1829 return 0;
1830}
1831
63a01ef8
AL
1832/* find or alloc a new VLAN */
1833VLANState *qemu_find_vlan(int id)
1834{
1835 VLANState **pvlan, *vlan;
1836 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1837 if (vlan->id == id)
1838 return vlan;
1839 }
1840 vlan = qemu_mallocz(sizeof(VLANState));
63a01ef8
AL
1841 vlan->id = id;
1842 vlan->next = NULL;
1843 pvlan = &first_vlan;
1844 while (*pvlan != NULL)
1845 pvlan = &(*pvlan)->next;
1846 *pvlan = vlan;
1847 return vlan;
1848}
1849
7697079b
AL
1850static int nic_get_free_idx(void)
1851{
1852 int index;
1853
1854 for (index = 0; index < MAX_NICS; index++)
1855 if (!nd_table[index].used)
1856 return index;
1857 return -1;
1858}
1859
d07f22c5
AL
1860void qemu_check_nic_model(NICInfo *nd, const char *model)
1861{
1862 const char *models[2];
1863
1864 models[0] = model;
1865 models[1] = NULL;
1866
1867 qemu_check_nic_model_list(nd, models, model);
1868}
1869
1870void qemu_check_nic_model_list(NICInfo *nd, const char * const *models,
1871 const char *default_model)
1872{
1873 int i, exit_status = 0;
1874
1875 if (!nd->model)
1876 nd->model = strdup(default_model);
1877
1878 if (strcmp(nd->model, "?") != 0) {
1879 for (i = 0 ; models[i]; i++)
1880 if (strcmp(nd->model, models[i]) == 0)
1881 return;
1882
1883 fprintf(stderr, "qemu: Unsupported NIC model: %s\n", nd->model);
1884 exit_status = 1;
1885 }
1886
1887 fprintf(stderr, "qemu: Supported NIC models: ");
1888 for (i = 0 ; models[i]; i++)
1889 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
1890
1891 exit(exit_status);
1892}
1893
10ae5a7a 1894int net_client_init(Monitor *mon, const char *device, const char *p)
63a01ef8 1895{
8e4416af
AL
1896 static const char * const fd_params[] = {
1897 "vlan", "name", "fd", NULL
1898 };
63a01ef8
AL
1899 char buf[1024];
1900 int vlan_id, ret;
1901 VLANState *vlan;
7a9f6e4a 1902 char *name = NULL;
63a01ef8
AL
1903
1904 vlan_id = 0;
1905 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
1906 vlan_id = strtol(buf, NULL, 0);
1907 }
1908 vlan = qemu_find_vlan(vlan_id);
9036de1a 1909
7a9f6e4a 1910 if (get_param_value(buf, sizeof(buf), "name", p)) {
10ae5a7a 1911 name = qemu_strdup(buf);
7a9f6e4a 1912 }
63a01ef8 1913 if (!strcmp(device, "nic")) {
8e4416af
AL
1914 static const char * const nic_params[] = {
1915 "vlan", "name", "macaddr", "model", NULL
1916 };
63a01ef8
AL
1917 NICInfo *nd;
1918 uint8_t *macaddr;
7697079b 1919 int idx = nic_get_free_idx();
63a01ef8 1920
0aa7a205 1921 if (check_params(buf, sizeof(buf), nic_params, p) < 0) {
10ae5a7a
JK
1922 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
1923 ret = -1;
1924 goto out;
8e4416af 1925 }
7697079b 1926 if (idx == -1 || nb_nics >= MAX_NICS) {
10ae5a7a 1927 config_error(mon, "Too Many NICs\n");
771f1339
AL
1928 ret = -1;
1929 goto out;
63a01ef8 1930 }
7697079b 1931 nd = &nd_table[idx];
63a01ef8
AL
1932 macaddr = nd->macaddr;
1933 macaddr[0] = 0x52;
1934 macaddr[1] = 0x54;
1935 macaddr[2] = 0x00;
1936 macaddr[3] = 0x12;
1937 macaddr[4] = 0x34;
7697079b 1938 macaddr[5] = 0x56 + idx;
63a01ef8
AL
1939
1940 if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
1941 if (parse_macaddr(macaddr, buf) < 0) {
10ae5a7a 1942 config_error(mon, "invalid syntax for ethernet address\n");
771f1339
AL
1943 ret = -1;
1944 goto out;
63a01ef8
AL
1945 }
1946 }
1947 if (get_param_value(buf, sizeof(buf), "model", p)) {
1948 nd->model = strdup(buf);
1949 }
1950 nd->vlan = vlan;
7a9f6e4a 1951 nd->name = name;
7697079b 1952 nd->used = 1;
7a9f6e4a 1953 name = NULL;
63a01ef8
AL
1954 nb_nics++;
1955 vlan->nb_guest_devs++;
4d73cd3b 1956 ret = idx;
63a01ef8
AL
1957 } else
1958 if (!strcmp(device, "none")) {
8e4416af 1959 if (*p != '\0') {
10ae5a7a
JK
1960 config_error(mon, "'none' takes no parameters\n");
1961 ret = -1;
1962 goto out;
8e4416af 1963 }
63a01ef8
AL
1964 /* does nothing. It is needed to signal that no network cards
1965 are wanted */
1966 ret = 0;
1967 } else
1968#ifdef CONFIG_SLIRP
1969 if (!strcmp(device, "user")) {
8e4416af
AL
1970 static const char * const slirp_params[] = {
1971 "vlan", "name", "hostname", "restrict", "ip", NULL
1972 };
0aa7a205 1973 if (check_params(buf, sizeof(buf), slirp_params, p) < 0) {
10ae5a7a
JK
1974 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
1975 ret = -1;
1976 goto out;
8e4416af 1977 }
63a01ef8
AL
1978 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
1979 pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
1980 }
49ec9b40
AL
1981 if (get_param_value(buf, sizeof(buf), "restrict", p)) {
1982 slirp_restrict = (buf[0] == 'y') ? 1 : 0;
1983 }
1984 if (get_param_value(buf, sizeof(buf), "ip", p)) {
1985 slirp_ip = strdup(buf);
1986 }
63a01ef8 1987 vlan->nb_host_devs++;
7a9f6e4a 1988 ret = net_slirp_init(vlan, device, name);
8ca9217d
AL
1989 } else if (!strcmp(device, "channel")) {
1990 long port;
1991 char name[20], *devname;
1992 struct VMChannel *vmc;
1993
1994 port = strtol(p, &devname, 10);
1995 devname++;
1996 if (port < 1 || port > 65535) {
10ae5a7a 1997 config_error(mon, "vmchannel wrong port number\n");
771f1339
AL
1998 ret = -1;
1999 goto out;
8ca9217d
AL
2000 }
2001 vmc = malloc(sizeof(struct VMChannel));
2002 snprintf(name, 20, "vmchannel%ld", port);
2003 vmc->hd = qemu_chr_open(name, devname, NULL);
2004 if (!vmc->hd) {
10ae5a7a
JK
2005 config_error(mon, "could not open vmchannel device '%s'\n",
2006 devname);
771f1339
AL
2007 ret = -1;
2008 goto out;
8ca9217d
AL
2009 }
2010 vmc->port = port;
2011 slirp_add_exec(3, vmc->hd, 4, port);
2012 qemu_chr_add_handlers(vmc->hd, vmchannel_can_read, vmchannel_read,
2013 NULL, vmc);
2014 ret = 0;
63a01ef8
AL
2015 } else
2016#endif
2017#ifdef _WIN32
2018 if (!strcmp(device, "tap")) {
8e4416af
AL
2019 static const char * const tap_params[] = {
2020 "vlan", "name", "ifname", NULL
2021 };
63a01ef8 2022 char ifname[64];
8e4416af 2023
0aa7a205 2024 if (check_params(buf, sizeof(buf), tap_params, p) < 0) {
10ae5a7a
JK
2025 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
2026 ret = -1;
2027 goto out;
8e4416af 2028 }
63a01ef8 2029 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
10ae5a7a 2030 config_error(mon, "tap: no interface name\n");
771f1339
AL
2031 ret = -1;
2032 goto out;
63a01ef8
AL
2033 }
2034 vlan->nb_host_devs++;
7a9f6e4a 2035 ret = tap_win32_init(vlan, device, name, ifname);
63a01ef8 2036 } else
b29fe3ed 2037#elif defined (_AIX)
63a01ef8
AL
2038#else
2039 if (!strcmp(device, "tap")) {
0aa7a205 2040 char ifname[64], chkbuf[64];
63a01ef8
AL
2041 char setup_script[1024], down_script[1024];
2042 int fd;
2043 vlan->nb_host_devs++;
2044 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
0aa7a205 2045 if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
10ae5a7a
JK
2046 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2047 ret = -1;
2048 goto out;
8e4416af 2049 }
63a01ef8
AL
2050 fd = strtol(buf, NULL, 0);
2051 fcntl(fd, F_SETFL, O_NONBLOCK);
9036de1a
AL
2052 net_tap_fd_init(vlan, device, name, fd);
2053 ret = 0;
63a01ef8 2054 } else {
8e4416af
AL
2055 static const char * const tap_params[] = {
2056 "vlan", "name", "ifname", "script", "downscript", NULL
2057 };
0aa7a205 2058 if (check_params(chkbuf, sizeof(chkbuf), tap_params, p) < 0) {
10ae5a7a
JK
2059 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2060 ret = -1;
2061 goto out;
8e4416af 2062 }
63a01ef8
AL
2063 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
2064 ifname[0] = '\0';
2065 }
2066 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
2067 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
2068 }
2069 if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
2070 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
2071 }
7a9f6e4a 2072 ret = net_tap_init(vlan, device, name, ifname, setup_script, down_script);
63a01ef8
AL
2073 }
2074 } else
2075#endif
2076 if (!strcmp(device, "socket")) {
0aa7a205 2077 char chkbuf[64];
63a01ef8
AL
2078 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
2079 int fd;
0aa7a205 2080 if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
10ae5a7a
JK
2081 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2082 ret = -1;
2083 goto out;
8e4416af 2084 }
63a01ef8
AL
2085 fd = strtol(buf, NULL, 0);
2086 ret = -1;
7a9f6e4a 2087 if (net_socket_fd_init(vlan, device, name, fd, 1))
63a01ef8
AL
2088 ret = 0;
2089 } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
8e4416af
AL
2090 static const char * const listen_params[] = {
2091 "vlan", "name", "listen", NULL
2092 };
0aa7a205 2093 if (check_params(chkbuf, sizeof(chkbuf), listen_params, p) < 0) {
10ae5a7a
JK
2094 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2095 ret = -1;
2096 goto out;
8e4416af 2097 }
7a9f6e4a 2098 ret = net_socket_listen_init(vlan, device, name, buf);
63a01ef8 2099 } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
8e4416af
AL
2100 static const char * const connect_params[] = {
2101 "vlan", "name", "connect", NULL
2102 };
0aa7a205 2103 if (check_params(chkbuf, sizeof(chkbuf), connect_params, p) < 0) {
10ae5a7a
JK
2104 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2105 ret = -1;
2106 goto out;
8e4416af 2107 }
7a9f6e4a 2108 ret = net_socket_connect_init(vlan, device, name, buf);
63a01ef8 2109 } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
8e4416af
AL
2110 static const char * const mcast_params[] = {
2111 "vlan", "name", "mcast", NULL
2112 };
0aa7a205 2113 if (check_params(chkbuf, sizeof(chkbuf), mcast_params, p) < 0) {
10ae5a7a
JK
2114 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2115 ret = -1;
2116 goto out;
8e4416af 2117 }
7a9f6e4a 2118 ret = net_socket_mcast_init(vlan, device, name, buf);
63a01ef8 2119 } else {
10ae5a7a 2120 config_error(mon, "Unknown socket options: %s\n", p);
771f1339
AL
2121 ret = -1;
2122 goto out;
63a01ef8
AL
2123 }
2124 vlan->nb_host_devs++;
2125 } else
2126#ifdef CONFIG_VDE
2127 if (!strcmp(device, "vde")) {
8e4416af
AL
2128 static const char * const vde_params[] = {
2129 "vlan", "name", "sock", "port", "group", "mode", NULL
2130 };
63a01ef8
AL
2131 char vde_sock[1024], vde_group[512];
2132 int vde_port, vde_mode;
8e4416af 2133
0aa7a205 2134 if (check_params(buf, sizeof(buf), vde_params, p) < 0) {
10ae5a7a
JK
2135 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
2136 ret = -1;
2137 goto out;
8e4416af 2138 }
63a01ef8
AL
2139 vlan->nb_host_devs++;
2140 if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
2141 vde_sock[0] = '\0';
2142 }
2143 if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
2144 vde_port = strtol(buf, NULL, 10);
2145 } else {
2146 vde_port = 0;
2147 }
2148 if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
2149 vde_group[0] = '\0';
2150 }
2151 if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
2152 vde_mode = strtol(buf, NULL, 8);
2153 } else {
2154 vde_mode = 0700;
2155 }
7a9f6e4a 2156 ret = net_vde_init(vlan, device, name, vde_sock, vde_port, vde_group, vde_mode);
63a01ef8
AL
2157 } else
2158#endif
bb9ea79e
AL
2159 if (!strcmp(device, "dump")) {
2160 int len = 65536;
2161
2162 if (get_param_value(buf, sizeof(buf), "len", p) > 0) {
2163 len = strtol(buf, NULL, 0);
2164 }
2165 if (!get_param_value(buf, sizeof(buf), "file", p)) {
2166 snprintf(buf, sizeof(buf), "qemu-vlan%d.pcap", vlan_id);
2167 }
10ae5a7a 2168 ret = net_dump_init(mon, vlan, device, name, buf, len);
bb9ea79e 2169 } else {
10ae5a7a 2170 config_error(mon, "Unknown network device: %s\n", device);
771f1339
AL
2171 ret = -1;
2172 goto out;
63a01ef8
AL
2173 }
2174 if (ret < 0) {
10ae5a7a 2175 config_error(mon, "Could not initialize device '%s'\n", device);
63a01ef8 2176 }
771f1339 2177out:
10ae5a7a 2178 qemu_free(name);
63a01ef8
AL
2179 return ret;
2180}
2181
8b13c4a7
AL
2182void net_client_uninit(NICInfo *nd)
2183{
2184 nd->vlan->nb_guest_devs--;
2185 nb_nics--;
2186 nd->used = 0;
2187 free((void *)nd->model);
2188}
2189
6f338c34
AL
2190static int net_host_check_device(const char *device)
2191{
2192 int i;
bb9ea79e 2193 const char *valid_param_list[] = { "tap", "socket", "dump"
6f338c34
AL
2194#ifdef CONFIG_SLIRP
2195 ,"user"
2196#endif
2197#ifdef CONFIG_VDE
2198 ,"vde"
2199#endif
2200 };
2201 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
2202 if (!strncmp(valid_param_list[i], device,
2203 strlen(valid_param_list[i])))
2204 return 1;
2205 }
2206
2207 return 0;
2208}
2209
376253ec 2210void net_host_device_add(Monitor *mon, const char *device, const char *opts)
6f338c34
AL
2211{
2212 if (!net_host_check_device(device)) {
376253ec 2213 monitor_printf(mon, "invalid host network device %s\n", device);
6f338c34
AL
2214 return;
2215 }
10ae5a7a 2216 if (net_client_init(mon, device, opts ? opts : "") < 0) {
5c8be678
AL
2217 monitor_printf(mon, "adding host network device %s failed\n", device);
2218 }
6f338c34
AL
2219}
2220
376253ec 2221void net_host_device_remove(Monitor *mon, int vlan_id, const char *device)
6f338c34
AL
2222{
2223 VLANState *vlan;
2224 VLANClientState *vc;
2225
6f338c34 2226 vlan = qemu_find_vlan(vlan_id);
6f338c34 2227
e8f1f9db
AL
2228 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
2229 if (!strcmp(vc->name, device)) {
6f338c34 2230 break;
e8f1f9db
AL
2231 }
2232 }
6f338c34
AL
2233
2234 if (!vc) {
376253ec 2235 monitor_printf(mon, "can't find device %s\n", device);
6f338c34
AL
2236 return;
2237 }
e8f1f9db
AL
2238 if (!net_host_check_device(vc->model)) {
2239 monitor_printf(mon, "invalid host network device %s\n", device);
2240 return;
2241 }
6f338c34
AL
2242 qemu_del_vlan_client(vc);
2243}
2244
63a01ef8
AL
2245int net_client_parse(const char *str)
2246{
2247 const char *p;
2248 char *q;
2249 char device[64];
2250
2251 p = str;
2252 q = device;
2253 while (*p != '\0' && *p != ',') {
2254 if ((q - device) < sizeof(device) - 1)
2255 *q++ = *p;
2256 p++;
2257 }
2258 *q = '\0';
2259 if (*p == ',')
2260 p++;
2261
10ae5a7a 2262 return net_client_init(NULL, device, p);
63a01ef8
AL
2263}
2264
376253ec 2265void do_info_network(Monitor *mon)
63a01ef8
AL
2266{
2267 VLANState *vlan;
2268 VLANClientState *vc;
2269
2270 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
376253ec 2271 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
63a01ef8 2272 for(vc = vlan->first_client; vc != NULL; vc = vc->next)
376253ec 2273 monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
63a01ef8
AL
2274 }
2275}
2276
376253ec 2277int do_set_link(Monitor *mon, const char *name, const char *up_or_down)
436e5e53
AL
2278{
2279 VLANState *vlan;
2280 VLANClientState *vc = NULL;
2281
2282 for (vlan = first_vlan; vlan != NULL; vlan = vlan->next)
2283 for (vc = vlan->first_client; vc != NULL; vc = vc->next)
2284 if (strcmp(vc->name, name) == 0)
dd5de373
EI
2285 goto done;
2286done:
436e5e53
AL
2287
2288 if (!vc) {
376253ec 2289 monitor_printf(mon, "could not find network device '%s'", name);
436e5e53
AL
2290 return 0;
2291 }
2292
2293 if (strcmp(up_or_down, "up") == 0)
2294 vc->link_down = 0;
2295 else if (strcmp(up_or_down, "down") == 0)
2296 vc->link_down = 1;
2297 else
376253ec
AL
2298 monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' "
2299 "valid\n", up_or_down);
436e5e53 2300
34b25ca7
AL
2301 if (vc->link_status_changed)
2302 vc->link_status_changed(vc);
2303
436e5e53
AL
2304 return 1;
2305}
2306
63a01ef8
AL
2307void net_cleanup(void)
2308{
2309 VLANState *vlan;
2310
63a01ef8
AL
2311 /* close network clients */
2312 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
b946a153 2313 VLANClientState *vc = vlan->first_client;
63a01ef8 2314
b946a153
AL
2315 while (vc) {
2316 VLANClientState *next = vc->next;
63a01ef8 2317
b946a153
AL
2318 qemu_del_vlan_client(vc);
2319
2320 vc = next;
63a01ef8
AL
2321 }
2322 }
63a01ef8
AL
2323}
2324
2325void net_client_check(void)
2326{
2327 VLANState *vlan;
2328
2329 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2330 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
2331 continue;
2332 if (vlan->nb_guest_devs == 0)
2333 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
2334 if (vlan->nb_host_devs == 0)
2335 fprintf(stderr,
2336 "Warning: vlan %d is not connected to host network\n",
2337 vlan->id);
2338 }
2339}