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