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