]> git.proxmox.com Git - qemu.git/blame - net.c
qcow2: Bring synchronous read/write back to life
[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 122
d918f23e 123#include "slirp/libslirp.h"
511d2b14
BS
124
125
63a01ef8
AL
126static VLANState *first_vlan;
127
128/***********************************************************/
129/* network device redirectors */
130
131#if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
132static void hex_dump(FILE *f, const uint8_t *buf, int size)
133{
134 int len, i, j, c;
135
136 for(i=0;i<size;i+=16) {
137 len = size - i;
138 if (len > 16)
139 len = 16;
140 fprintf(f, "%08x ", i);
141 for(j=0;j<16;j++) {
142 if (j < len)
143 fprintf(f, " %02x", buf[i+j]);
144 else
145 fprintf(f, " ");
146 }
147 fprintf(f, " ");
148 for(j=0;j<len;j++) {
149 c = buf[i+j];
150 if (c < ' ' || c > '~')
151 c = '.';
152 fprintf(f, "%c", c);
153 }
154 fprintf(f, "\n");
155 }
156}
157#endif
158
159static int parse_macaddr(uint8_t *macaddr, const char *p)
160{
161 int i;
162 char *last_char;
163 long int offset;
164
165 errno = 0;
166 offset = strtol(p, &last_char, 0);
167 if (0 == errno && '\0' == *last_char &&
168 offset >= 0 && offset <= 0xFFFFFF) {
169 macaddr[3] = (offset & 0xFF0000) >> 16;
170 macaddr[4] = (offset & 0xFF00) >> 8;
171 macaddr[5] = offset & 0xFF;
172 return 0;
173 } else {
174 for(i = 0; i < 6; i++) {
175 macaddr[i] = strtol(p, (char **)&p, 16);
176 if (i == 5) {
177 if (*p != '\0')
178 return -1;
179 } else {
180 if (*p != ':' && *p != '-')
181 return -1;
182 p++;
183 }
184 }
185 return 0;
186 }
187
188 return -1;
189}
190
191static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
192{
193 const char *p, *p1;
194 int len;
195 p = *pp;
196 p1 = strchr(p, sep);
197 if (!p1)
198 return -1;
199 len = p1 - p;
200 p1++;
201 if (buf_size > 0) {
202 if (len > buf_size - 1)
203 len = buf_size - 1;
204 memcpy(buf, p, len);
205 buf[len] = '\0';
206 }
207 *pp = p1;
208 return 0;
209}
210
211int parse_host_src_port(struct sockaddr_in *haddr,
212 struct sockaddr_in *saddr,
213 const char *input_str)
214{
215 char *str = strdup(input_str);
216 char *host_str = str;
217 char *src_str;
218 const char *src_str2;
219 char *ptr;
220
221 /*
222 * Chop off any extra arguments at the end of the string which
223 * would start with a comma, then fill in the src port information
224 * if it was provided else use the "any address" and "any port".
225 */
226 if ((ptr = strchr(str,',')))
227 *ptr = '\0';
228
229 if ((src_str = strchr(input_str,'@'))) {
230 *src_str = '\0';
231 src_str++;
232 }
233
234 if (parse_host_port(haddr, host_str) < 0)
235 goto fail;
236
237 src_str2 = src_str;
238 if (!src_str || *src_str == '\0')
239 src_str2 = ":0";
240
241 if (parse_host_port(saddr, src_str2) < 0)
242 goto fail;
243
244 free(str);
245 return(0);
246
247fail:
248 free(str);
249 return -1;
250}
251
252int parse_host_port(struct sockaddr_in *saddr, const char *str)
253{
254 char buf[512];
255 struct hostent *he;
256 const char *p, *r;
257 int port;
258
259 p = str;
260 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
261 return -1;
262 saddr->sin_family = AF_INET;
263 if (buf[0] == '\0') {
264 saddr->sin_addr.s_addr = 0;
265 } else {
cd390083 266 if (qemu_isdigit(buf[0])) {
63a01ef8
AL
267 if (!inet_aton(buf, &saddr->sin_addr))
268 return -1;
269 } else {
270 if ((he = gethostbyname(buf)) == NULL)
271 return - 1;
272 saddr->sin_addr = *(struct in_addr *)he->h_addr;
273 }
274 }
275 port = strtol(p, (char **)&r, 0);
276 if (r == p)
277 return -1;
278 saddr->sin_port = htons(port);
279 return 0;
280}
281
69d6451c
BS
282#if !defined(_WIN32) && 0
283static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
63a01ef8
AL
284{
285 const char *p;
286 int len;
287
288 len = MIN(108, strlen(str));
289 p = strchr(str, ',');
290 if (p)
291 len = MIN(len, p - str);
292
293 memset(uaddr, 0, sizeof(*uaddr));
294
295 uaddr->sun_family = AF_UNIX;
296 memcpy(uaddr->sun_path, str, len);
297
298 return 0;
299}
300#endif
301
7cb7434b
AL
302void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
303{
304 snprintf(vc->info_str, sizeof(vc->info_str),
4dda4063
AL
305 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
306 vc->model,
7cb7434b
AL
307 macaddr[0], macaddr[1], macaddr[2],
308 macaddr[3], macaddr[4], macaddr[5]);
309}
310
676cff29
AL
311static char *assign_name(VLANClientState *vc1, const char *model)
312{
313 VLANState *vlan;
314 char buf[256];
315 int id = 0;
316
317 for (vlan = first_vlan; vlan; vlan = vlan->next) {
318 VLANClientState *vc;
319
320 for (vc = vlan->first_client; vc; vc = vc->next)
321 if (vc != vc1 && strcmp(vc->model, model) == 0)
322 id++;
323 }
324
325 snprintf(buf, sizeof(buf), "%s.%d", model, id);
326
327 return strdup(buf);
328}
329
63a01ef8 330VLANClientState *qemu_new_vlan_client(VLANState *vlan,
bf38c1a0 331 const char *model,
7a9f6e4a 332 const char *name,
cda9046b
MM
333 NetCanReceive *can_receive,
334 NetReceive *receive,
335 NetReceiveIOV *receive_iov,
b946a153 336 NetCleanup *cleanup,
63a01ef8
AL
337 void *opaque)
338{
339 VLANClientState *vc, **pvc;
340 vc = qemu_mallocz(sizeof(VLANClientState));
bf38c1a0 341 vc->model = strdup(model);
7a9f6e4a
AL
342 if (name)
343 vc->name = strdup(name);
344 else
345 vc->name = assign_name(vc, model);
cda9046b
MM
346 vc->can_receive = can_receive;
347 vc->receive = receive;
348 vc->receive_iov = receive_iov;
b946a153 349 vc->cleanup = cleanup;
63a01ef8
AL
350 vc->opaque = opaque;
351 vc->vlan = vlan;
352
353 vc->next = NULL;
354 pvc = &vlan->first_client;
355 while (*pvc != NULL)
356 pvc = &(*pvc)->next;
357 *pvc = vc;
358 return vc;
359}
360
361void qemu_del_vlan_client(VLANClientState *vc)
362{
363 VLANClientState **pvc = &vc->vlan->first_client;
364
365 while (*pvc != NULL)
366 if (*pvc == vc) {
367 *pvc = vc->next;
b946a153
AL
368 if (vc->cleanup) {
369 vc->cleanup(vc);
370 }
676cff29 371 free(vc->name);
bf38c1a0 372 free(vc->model);
dad35419 373 qemu_free(vc);
63a01ef8
AL
374 break;
375 } else
376 pvc = &(*pvc)->next;
377}
378
8b13c4a7
AL
379VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque)
380{
381 VLANClientState **pvc = &vlan->first_client;
382
383 while (*pvc != NULL)
384 if ((*pvc)->opaque == opaque)
385 return *pvc;
386 else
387 pvc = &(*pvc)->next;
388
389 return NULL;
390}
391
1a609520
JK
392static VLANClientState *
393qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
394 const char *client_str)
395{
396 VLANState *vlan;
397 VLANClientState *vc;
398
399 vlan = qemu_find_vlan(vlan_id, 0);
400 if (!vlan) {
401 monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
402 return NULL;
403 }
404
405 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
406 if (!strcmp(vc->name, client_str)) {
407 break;
408 }
409 }
410 if (!vc) {
411 monitor_printf(mon, "can't find device %s on VLAN %d\n",
412 client_str, vlan_id);
413 }
414
415 return vc;
416}
417
2e1e0641 418int qemu_can_send_packet(VLANClientState *sender)
63a01ef8 419{
2e1e0641 420 VLANState *vlan = sender->vlan;
63a01ef8
AL
421 VLANClientState *vc;
422
2e1e0641
MM
423 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
424 if (vc == sender) {
425 continue;
426 }
427
cda9046b 428 /* no can_receive() handler, they can always receive */
e3f5ec2b 429 if (!vc->can_receive || vc->can_receive(vc)) {
2e1e0641 430 return 1;
63a01ef8
AL
431 }
432 }
433 return 0;
434}
435
3e021d40 436static int
764a4d1d 437qemu_deliver_packet(VLANClientState *sender, const uint8_t *buf, int size)
63a01ef8 438{
63a01ef8 439 VLANClientState *vc;
3e021d40 440 int ret = -1;
63a01ef8 441
e94667b9
MM
442 sender->vlan->delivering = 1;
443
764a4d1d 444 for (vc = sender->vlan->first_client; vc != NULL; vc = vc->next) {
3e021d40
MM
445 ssize_t len;
446
447 if (vc == sender) {
448 continue;
764a4d1d 449 }
3e021d40
MM
450
451 if (vc->link_down) {
452 ret = size;
453 continue;
454 }
455
456 len = vc->receive(vc, buf, size);
457
458 ret = (ret >= 0) ? ret : len;
764a4d1d 459 }
3e021d40 460
e94667b9
MM
461 sender->vlan->delivering = 0;
462
3e021d40 463 return ret;
764a4d1d
AL
464}
465
8cad5516
MM
466void qemu_purge_queued_packets(VLANClientState *vc)
467{
67867f38 468 VLANPacket *packet, *next;
8cad5516 469
67867f38 470 TAILQ_FOREACH_SAFE(packet, &vc->vlan->send_queue, entry, next) {
8cad5516 471 if (packet->sender == vc) {
67867f38 472 TAILQ_REMOVE(&vc->vlan->send_queue, packet, entry);
8cad5516 473 qemu_free(packet);
8cad5516
MM
474 }
475 }
476}
477
f3b6c7fc 478void qemu_flush_queued_packets(VLANClientState *vc)
e94667b9 479{
67867f38
JK
480 while (!TAILQ_EMPTY(&vc->vlan->send_queue)) {
481 VLANPacket *packet;
f3b6c7fc
MM
482 int ret;
483
67867f38
JK
484 packet = TAILQ_FIRST(&vc->vlan->send_queue);
485 TAILQ_REMOVE(&vc->vlan->send_queue, packet, entry);
f3b6c7fc
MM
486
487 ret = qemu_deliver_packet(packet->sender, packet->data, packet->size);
488 if (ret == 0 && packet->sent_cb != NULL) {
67867f38 489 TAILQ_INSERT_HEAD(&vc->vlan->send_queue, packet, entry);
f3b6c7fc
MM
490 break;
491 }
492
493 if (packet->sent_cb)
783527a9 494 packet->sent_cb(packet->sender, ret);
f3b6c7fc 495
e94667b9
MM
496 qemu_free(packet);
497 }
498}
499
f3b6c7fc
MM
500static void qemu_enqueue_packet(VLANClientState *sender,
501 const uint8_t *buf, int size,
502 NetPacketSent *sent_cb)
e94667b9
MM
503{
504 VLANPacket *packet;
505
506 packet = qemu_malloc(sizeof(VLANPacket) + size);
e94667b9
MM
507 packet->sender = sender;
508 packet->size = size;
f3b6c7fc 509 packet->sent_cb = sent_cb;
e94667b9 510 memcpy(packet->data, buf, size);
67867f38
JK
511
512 TAILQ_INSERT_TAIL(&sender->vlan->send_queue, packet, entry);
e94667b9
MM
513}
514
f3b6c7fc
MM
515ssize_t qemu_send_packet_async(VLANClientState *sender,
516 const uint8_t *buf, int size,
517 NetPacketSent *sent_cb)
764a4d1d 518{
f3b6c7fc 519 int ret;
764a4d1d 520
f3b6c7fc
MM
521 if (sender->link_down) {
522 return size;
523 }
436e5e53 524
63a01ef8 525#ifdef DEBUG_NET
f3b6c7fc 526 printf("vlan %d send:\n", sender->vlan->id);
63a01ef8
AL
527 hex_dump(stdout, buf, size);
528#endif
f3b6c7fc
MM
529
530 if (sender->vlan->delivering) {
531 qemu_enqueue_packet(sender, buf, size, NULL);
532 return size;
533 }
534
535 ret = qemu_deliver_packet(sender, buf, size);
536 if (ret == 0 && sent_cb != NULL) {
537 qemu_enqueue_packet(sender, buf, size, sent_cb);
538 return 0;
63a01ef8 539 }
e94667b9 540
f3b6c7fc
MM
541 qemu_flush_queued_packets(sender);
542
543 return ret;
544}
545
546void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
547{
548 qemu_send_packet_async(vc, buf, size, NULL);
63a01ef8
AL
549}
550
fbe78f4f
AL
551static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
552 int iovcnt)
553{
554 uint8_t buffer[4096];
555 size_t offset = 0;
556 int i;
557
558 for (i = 0; i < iovcnt; i++) {
559 size_t len;
560
561 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
562 memcpy(buffer + offset, iov[i].iov_base, len);
563 offset += len;
564 }
565
f3b6c7fc 566 return vc->receive(vc, buffer, offset);
fbe78f4f
AL
567}
568
e0e7877a
AL
569static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
570{
571 size_t offset = 0;
572 int i;
573
574 for (i = 0; i < iovcnt; i++)
575 offset += iov[i].iov_len;
576 return offset;
577}
578
e94667b9
MM
579static int qemu_deliver_packet_iov(VLANClientState *sender,
580 const struct iovec *iov, int iovcnt)
fbe78f4f 581{
fbe78f4f 582 VLANClientState *vc;
e94667b9
MM
583 int ret = -1;
584
585 sender->vlan->delivering = 1;
586
587 for (vc = sender->vlan->first_client; vc != NULL; vc = vc->next) {
588 ssize_t len;
589
590 if (vc == sender) {
591 continue;
592 }
593
594 if (vc->link_down) {
595 ret = calc_iov_length(iov, iovcnt);
596 continue;
597 }
598
599 if (vc->receive_iov) {
600 len = vc->receive_iov(vc, iov, iovcnt);
601 } else {
602 len = vc_sendv_compat(vc, iov, iovcnt);
603 }
604
605 ret = (ret >= 0) ? ret : len;
606 }
607
608 sender->vlan->delivering = 0;
609
610 return ret;
611}
612
613static ssize_t qemu_enqueue_packet_iov(VLANClientState *sender,
f3b6c7fc
MM
614 const struct iovec *iov, int iovcnt,
615 NetPacketSent *sent_cb)
e94667b9 616{
c27ff608 617 VLANPacket *packet;
e94667b9 618 size_t max_len = 0;
c27ff608 619 int i;
fbe78f4f 620
e94667b9 621 max_len = calc_iov_length(iov, iovcnt);
e0e7877a 622
e94667b9 623 packet = qemu_malloc(sizeof(VLANPacket) + max_len);
e94667b9 624 packet->sender = sender;
f3b6c7fc 625 packet->sent_cb = sent_cb;
e94667b9 626 packet->size = 0;
c27ff608 627
e94667b9
MM
628 for (i = 0; i < iovcnt; i++) {
629 size_t len = iov[i].iov_len;
c27ff608 630
e94667b9
MM
631 memcpy(packet->data + packet->size, iov[i].iov_base, len);
632 packet->size += len;
633 }
fbe78f4f 634
67867f38 635 TAILQ_INSERT_TAIL(&sender->vlan->send_queue, packet, entry);
fbe78f4f 636
e94667b9
MM
637 return packet->size;
638}
fbe78f4f 639
f3b6c7fc
MM
640ssize_t qemu_sendv_packet_async(VLANClientState *sender,
641 const struct iovec *iov, int iovcnt,
642 NetPacketSent *sent_cb)
e94667b9
MM
643{
644 int ret;
645
646 if (sender->link_down) {
647 return calc_iov_length(iov, iovcnt);
648 }
649
650 if (sender->vlan->delivering) {
f3b6c7fc 651 return qemu_enqueue_packet_iov(sender, iov, iovcnt, NULL);
fbe78f4f
AL
652 }
653
e94667b9 654 ret = qemu_deliver_packet_iov(sender, iov, iovcnt);
f3b6c7fc
MM
655 if (ret == 0 && sent_cb != NULL) {
656 qemu_enqueue_packet_iov(sender, iov, iovcnt, sent_cb);
657 return 0;
658 }
e94667b9
MM
659
660 qemu_flush_queued_packets(sender);
661
662 return ret;
fbe78f4f
AL
663}
664
f3b6c7fc
MM
665ssize_t
666qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
667{
668 return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
669}
670
10ae5a7a
JK
671static void config_error(Monitor *mon, const char *fmt, ...)
672{
673 va_list ap;
674
675 va_start(ap, fmt);
676 if (mon) {
677 monitor_vprintf(mon, fmt, ap);
678 } else {
679 fprintf(stderr, "qemu: ");
680 vfprintf(stderr, fmt, ap);
681 exit(1);
682 }
683 va_end(ap);
684}
685
63a01ef8
AL
686#if defined(CONFIG_SLIRP)
687
688/* slirp network adapter */
689
c92ef6a2
JK
690#define SLIRP_CFG_HOSTFWD 1
691#define SLIRP_CFG_LEGACY 2
ad196a9d 692
b8e8af38
JK
693struct slirp_config_str {
694 struct slirp_config_str *next;
ad196a9d
JK
695 int flags;
696 char str[1024];
c92ef6a2 697 int legacy_format;
b8e8af38
JK
698};
699
9f8bd042 700typedef struct SlirpState {
b1c99fcd 701 TAILQ_ENTRY(SlirpState) entry;
9f8bd042
JK
702 VLANClientState *vc;
703 Slirp *slirp;
28432466
JK
704#ifndef _WIN32
705 char smb_dir[128];
706#endif
9f8bd042
JK
707} SlirpState;
708
ad196a9d
JK
709static struct slirp_config_str *slirp_configs;
710const char *legacy_tftp_prefix;
711const char *legacy_bootp_filename;
b1c99fcd
JK
712static TAILQ_HEAD(slirp_stacks, SlirpState) slirp_stacks =
713 TAILQ_HEAD_INITIALIZER(slirp_stacks);
63a01ef8 714
9f8bd042 715static void slirp_hostfwd(SlirpState *s, Monitor *mon, const char *redir_str,
3c6a0580 716 int legacy_format);
9f8bd042 717static void slirp_guestfwd(SlirpState *s, Monitor *mon, const char *config_str,
c92ef6a2 718 int legacy_format);
ad196a9d 719
c5b76b38 720#ifndef _WIN32
ad196a9d
JK
721static const char *legacy_smb_export;
722
492efabd 723static void slirp_smb(SlirpState *s, Monitor *mon, const char *exported_dir,
9f8bd042 724 struct in_addr vserver_addr);
28432466
JK
725static void slirp_smb_cleanup(SlirpState *s);
726#else
727static inline void slirp_smb_cleanup(SlirpState *s) { }
c5b76b38 728#endif
b8e8af38 729
9f8bd042 730int slirp_can_output(void *opaque)
63a01ef8 731{
9f8bd042
JK
732 SlirpState *s = opaque;
733
734 return qemu_can_send_packet(s->vc);
63a01ef8
AL
735}
736
9f8bd042 737void slirp_output(void *opaque, const uint8_t *pkt, int pkt_len)
63a01ef8 738{
9f8bd042
JK
739 SlirpState *s = opaque;
740
63a01ef8
AL
741#ifdef DEBUG_SLIRP
742 printf("slirp output:\n");
743 hex_dump(stdout, pkt, pkt_len);
744#endif
9f8bd042 745 qemu_send_packet(s->vc, pkt, pkt_len);
63a01ef8
AL
746}
747
4f1c942b 748static ssize_t slirp_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
63a01ef8 749{
9f8bd042
JK
750 SlirpState *s = vc->opaque;
751
63a01ef8
AL
752#ifdef DEBUG_SLIRP
753 printf("slirp input:\n");
754 hex_dump(stdout, buf, size);
755#endif
9f8bd042 756 slirp_input(s->slirp, buf, size);
4f1c942b 757 return size;
63a01ef8
AL
758}
759
8d6249a7
AL
760static void net_slirp_cleanup(VLANClientState *vc)
761{
ad0d8c4c
JK
762 SlirpState *s = vc->opaque;
763
764 slirp_cleanup(s->slirp);
28432466 765 slirp_smb_cleanup(s);
b1c99fcd 766 TAILQ_REMOVE(&slirp_stacks, s, entry);
ad0d8c4c 767 qemu_free(s);
8d6249a7
AL
768}
769
ad196a9d 770static int net_slirp_init(Monitor *mon, VLANState *vlan, const char *model,
c92ef6a2
JK
771 const char *name, int restricted,
772 const char *vnetwork, const char *vhost,
773 const char *vhostname, const char *tftp_export,
774 const char *bootfile, const char *vdhcp_start,
775 const char *vnameserver, const char *smb_export,
776 const char *vsmbserver)
63a01ef8 777{
ad0d8c4c 778 /* default settings according to historic slirp */
8389e7f4
AL
779 struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */
780 struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */
ad0d8c4c
JK
781 struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */
782 struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */
783 struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */
c92ef6a2 784#ifndef _WIN32
ad0d8c4c 785 struct in_addr smbsrv = { .s_addr = 0 };
c92ef6a2 786#endif
ad0d8c4c
JK
787 SlirpState *s;
788 char buf[20];
789 uint32_t addr;
790 int shift;
791 char *end;
792
793 if (!tftp_export) {
794 tftp_export = legacy_tftp_prefix;
795 }
796 if (!bootfile) {
797 bootfile = legacy_bootp_filename;
798 }
c92ef6a2 799
ad0d8c4c
JK
800 if (vnetwork) {
801 if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) {
802 if (!inet_aton(vnetwork, &net)) {
803 return -1;
804 }
805 addr = ntohl(net.s_addr);
806 if (!(addr & 0x80000000)) {
807 mask.s_addr = htonl(0xff000000); /* class A */
808 } else if ((addr & 0xfff00000) == 0xac100000) {
809 mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */
810 } else if ((addr & 0xc0000000) == 0x80000000) {
811 mask.s_addr = htonl(0xffff0000); /* class B */
812 } else if ((addr & 0xffff0000) == 0xc0a80000) {
813 mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */
814 } else if ((addr & 0xffff0000) == 0xc6120000) {
815 mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */
816 } else if ((addr & 0xe0000000) == 0xe0000000) {
817 mask.s_addr = htonl(0xffffff00); /* class C */
c92ef6a2 818 } else {
ad0d8c4c
JK
819 mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */
820 }
821 } else {
822 if (!inet_aton(buf, &net)) {
823 return -1;
824 }
825 shift = strtol(vnetwork, &end, 10);
826 if (*end != '\0') {
827 if (!inet_aton(vnetwork, &mask)) {
c92ef6a2 828 return -1;
c92ef6a2 829 }
ad0d8c4c
JK
830 } else if (shift < 4 || shift > 32) {
831 return -1;
832 } else {
833 mask.s_addr = htonl(0xffffffff << (32 - shift));
c92ef6a2 834 }
c92ef6a2 835 }
ad0d8c4c
JK
836 net.s_addr &= mask.s_addr;
837 host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr);
838 dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr);
839 dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr);
840 }
c92ef6a2 841
ad0d8c4c
JK
842 if (vhost && !inet_aton(vhost, &host)) {
843 return -1;
844 }
845 if ((host.s_addr & mask.s_addr) != net.s_addr) {
846 return -1;
847 }
c92ef6a2 848
ad0d8c4c
JK
849 if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) {
850 return -1;
851 }
852 if ((dhcp.s_addr & mask.s_addr) != net.s_addr ||
853 dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) {
854 return -1;
855 }
c92ef6a2 856
ad0d8c4c
JK
857 if (vnameserver && !inet_aton(vnameserver, &dns)) {
858 return -1;
859 }
860 if ((dns.s_addr & mask.s_addr) != net.s_addr ||
861 dns.s_addr == host.s_addr) {
862 return -1;
863 }
c92ef6a2
JK
864
865#ifndef _WIN32
ad0d8c4c
JK
866 if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
867 return -1;
868 }
c92ef6a2
JK
869#endif
870
ad0d8c4c
JK
871 s = qemu_mallocz(sizeof(SlirpState));
872 s->slirp = slirp_init(restricted, net, mask, host, vhostname,
873 tftp_export, bootfile, dhcp, dns, s);
b1c99fcd 874 TAILQ_INSERT_TAIL(&slirp_stacks, s, entry);
b8e8af38 875
ad0d8c4c
JK
876 while (slirp_configs) {
877 struct slirp_config_str *config = slirp_configs;
b8e8af38 878
ad0d8c4c
JK
879 if (config->flags & SLIRP_CFG_HOSTFWD) {
880 slirp_hostfwd(s, mon, config->str,
881 config->flags & SLIRP_CFG_LEGACY);
882 } else {
883 slirp_guestfwd(s, mon, config->str,
884 config->flags & SLIRP_CFG_LEGACY);
b8e8af38 885 }
ad0d8c4c
JK
886 slirp_configs = config->next;
887 qemu_free(config);
888 }
b8e8af38 889#ifndef _WIN32
ad0d8c4c
JK
890 if (!smb_export) {
891 smb_export = legacy_smb_export;
892 }
893 if (smb_export) {
492efabd 894 slirp_smb(s, mon, smb_export, smbsrv);
63a01ef8 895 }
ad0d8c4c 896#endif
b8e8af38 897
9f8bd042
JK
898 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, slirp_receive, NULL,
899 net_slirp_cleanup, s);
fc57bc57
JK
900 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
901 "net=%s, restricted=%c", inet_ntoa(net), restricted ? 'y' : 'n');
63a01ef8
AL
902 return 0;
903}
904
f13b572c
JK
905static SlirpState *slirp_lookup(Monitor *mon, const char *vlan,
906 const char *stack)
907{
908 VLANClientState *vc;
909
910 if (vlan) {
911 vc = qemu_find_vlan_client_by_name(mon, strtol(vlan, NULL, 0), stack);
912 if (!vc) {
913 return NULL;
914 }
915 if (strcmp(vc->model, "user")) {
916 monitor_printf(mon, "invalid device specified\n");
917 return NULL;
918 }
919 return vc->opaque;
920 } else {
921 if (TAILQ_EMPTY(&slirp_stacks)) {
922 monitor_printf(mon, "user mode network stack not in use\n");
923 return NULL;
924 }
925 return TAILQ_FIRST(&slirp_stacks);
926 }
927}
928
929void net_slirp_hostfwd_remove(Monitor *mon, const char *arg1,
930 const char *arg2, const char *arg3)
c1261d8d 931{
3c6a0580 932 struct in_addr host_addr = { .s_addr = INADDR_ANY };
c1261d8d
AG
933 int host_port;
934 char buf[256] = "";
f13b572c
JK
935 const char *src_str, *p;
936 SlirpState *s;
c1261d8d 937 int is_udp = 0;
9c12a6f2 938 int err;
c1261d8d 939
f13b572c
JK
940 if (arg2) {
941 s = slirp_lookup(mon, arg1, arg2);
942 src_str = arg3;
943 } else {
944 s = slirp_lookup(mon, NULL, NULL);
945 src_str = arg1;
946 }
947 if (!s) {
c1261d8d 948 return;
f3546deb 949 }
c1261d8d 950
3c6a0580 951 if (!src_str || !src_str[0])
c1261d8d
AG
952 goto fail_syntax;
953
f13b572c 954 p = src_str;
c1261d8d
AG
955 get_str_sep(buf, sizeof(buf), &p, ':');
956
957 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
958 is_udp = 0;
959 } else if (!strcmp(buf, "udp")) {
960 is_udp = 1;
961 } else {
962 goto fail_syntax;
963 }
964
3c6a0580
JK
965 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
966 goto fail_syntax;
967 }
968 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
969 goto fail_syntax;
970 }
971
c1261d8d
AG
972 host_port = atoi(p);
973
b1c99fcd 974 err = slirp_remove_hostfwd(TAILQ_FIRST(&slirp_stacks)->slirp, is_udp,
9f8bd042 975 host_addr, host_port);
c1261d8d 976
9c12a6f2
JK
977 monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
978 err ? "removed" : "not found");
c1261d8d
AG
979 return;
980
981 fail_syntax:
982 monitor_printf(mon, "invalid format\n");
983}
984
9f8bd042 985static void slirp_hostfwd(SlirpState *s, Monitor *mon, const char *redir_str,
3c6a0580 986 int legacy_format)
63a01ef8 987{
3c6a0580 988 struct in_addr host_addr = { .s_addr = INADDR_ANY };
c92ef6a2 989 struct in_addr guest_addr = { .s_addr = 0 };
63a01ef8 990 int host_port, guest_port;
b8e8af38 991 const char *p;
c92ef6a2 992 char buf[256];
b8e8af38 993 int is_udp;
c92ef6a2 994 char *end;
1c6ed9f3 995
63a01ef8 996 p = redir_str;
f13b572c 997 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
d4ebe193 998 goto fail_syntax;
b8e8af38 999 }
d4ebe193 1000 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
63a01ef8
AL
1001 is_udp = 0;
1002 } else if (!strcmp(buf, "udp")) {
1003 is_udp = 1;
1004 } else {
d4ebe193 1005 goto fail_syntax;
63a01ef8
AL
1006 }
1007
3c6a0580
JK
1008 if (!legacy_format) {
1009 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
1010 goto fail_syntax;
1011 }
1012 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
1013 goto fail_syntax;
1014 }
1015 }
1016
1017 if (get_str_sep(buf, sizeof(buf), &p, legacy_format ? ':' : '-') < 0) {
d4ebe193 1018 goto fail_syntax;
b8e8af38 1019 }
c92ef6a2
JK
1020 host_port = strtol(buf, &end, 0);
1021 if (*end != '\0' || host_port < 1 || host_port > 65535) {
d4ebe193 1022 goto fail_syntax;
b8e8af38 1023 }
63a01ef8 1024
b8e8af38 1025 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
d4ebe193 1026 goto fail_syntax;
b8e8af38 1027 }
c92ef6a2 1028 if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) {
d4ebe193 1029 goto fail_syntax;
b8e8af38 1030 }
63a01ef8 1031
c92ef6a2
JK
1032 guest_port = strtol(p, &end, 0);
1033 if (*end != '\0' || guest_port < 1 || guest_port > 65535) {
d4ebe193 1034 goto fail_syntax;
b8e8af38 1035 }
63a01ef8 1036
9f8bd042
JK
1037 if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr,
1038 guest_port) < 0) {
c92ef6a2
JK
1039 config_error(mon, "could not set up host forwarding rule '%s'\n",
1040 redir_str);
63a01ef8
AL
1041 }
1042 return;
d4ebe193
AL
1043
1044 fail_syntax:
c92ef6a2 1045 config_error(mon, "invalid host forwarding rule '%s'\n", redir_str);
63a01ef8
AL
1046}
1047
f13b572c
JK
1048void net_slirp_hostfwd_add(Monitor *mon, const char *arg1,
1049 const char *arg2, const char *arg3)
b8e8af38 1050{
f13b572c
JK
1051 const char *redir_str;
1052 SlirpState *s;
1053
1054 if (arg2) {
1055 s = slirp_lookup(mon, arg1, arg2);
1056 redir_str = arg3;
1057 } else {
1058 s = slirp_lookup(mon, NULL, NULL);
1059 redir_str = arg1;
1060 }
1061 if (s) {
1062 slirp_hostfwd(s, mon, redir_str, 0);
b8e8af38
JK
1063 }
1064
f3546deb
JK
1065}
1066
1067void net_slirp_redir(const char *redir_str)
1068{
1069 struct slirp_config_str *config;
1070
b1c99fcd 1071 if (TAILQ_EMPTY(&slirp_stacks)) {
f3546deb
JK
1072 config = qemu_malloc(sizeof(*config));
1073 pstrcpy(config->str, sizeof(config->str), redir_str);
3c6a0580 1074 config->flags = SLIRP_CFG_HOSTFWD | SLIRP_CFG_LEGACY;
f3546deb
JK
1075 config->next = slirp_configs;
1076 slirp_configs = config;
b8e8af38
JK
1077 return;
1078 }
1079
b1c99fcd 1080 slirp_hostfwd(TAILQ_FIRST(&slirp_stacks), NULL, redir_str, 1);
b8e8af38
JK
1081}
1082
63a01ef8
AL
1083#ifndef _WIN32
1084
63a01ef8 1085/* automatic user mode samba server configuration */
28432466 1086static void slirp_smb_cleanup(SlirpState *s)
63a01ef8 1087{
28432466 1088 char cmd[128];
09c18925 1089
28432466
JK
1090 if (s->smb_dir[0] != '\0') {
1091 snprintf(cmd, sizeof(cmd), "rm -rf %s", s->smb_dir);
1092 system(cmd);
1093 s->smb_dir[0] = '\0';
1094 }
63a01ef8
AL
1095}
1096
492efabd 1097static void slirp_smb(SlirpState* s, Monitor *mon, const char *exported_dir,
9f8bd042 1098 struct in_addr vserver_addr)
63a01ef8 1099{
28432466
JK
1100 static int instance;
1101 char smb_conf[128];
1102 char smb_cmdline[128];
63a01ef8
AL
1103 FILE *f;
1104
28432466
JK
1105 snprintf(s->smb_dir, sizeof(s->smb_dir), "/tmp/qemu-smb.%ld-%d",
1106 (long)getpid(), instance++);
1107 if (mkdir(s->smb_dir, 0700) < 0) {
1108 config_error(mon, "could not create samba server dir '%s'\n",
1109 s->smb_dir);
492efabd 1110 return;
63a01ef8 1111 }
28432466 1112 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", s->smb_dir, "smb.conf");
63a01ef8
AL
1113
1114 f = fopen(smb_conf, "w");
1115 if (!f) {
28432466 1116 slirp_smb_cleanup(s);
492efabd
JK
1117 config_error(mon, "could not create samba server "
1118 "configuration file '%s'\n", smb_conf);
1119 return;
63a01ef8
AL
1120 }
1121 fprintf(f,
1122 "[global]\n"
1123 "private dir=%s\n"
1124 "smb ports=0\n"
1125 "socket address=127.0.0.1\n"
1126 "pid directory=%s\n"
1127 "lock directory=%s\n"
1128 "log file=%s/log.smbd\n"
1129 "smb passwd file=%s/smbpasswd\n"
1130 "security = share\n"
1131 "[qemu]\n"
1132 "path=%s\n"
1133 "read only=no\n"
1134 "guest ok=yes\n",
28432466
JK
1135 s->smb_dir,
1136 s->smb_dir,
1137 s->smb_dir,
1138 s->smb_dir,
1139 s->smb_dir,
63a01ef8
AL
1140 exported_dir
1141 );
1142 fclose(f);
63a01ef8
AL
1143
1144 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
1145 SMBD_COMMAND, smb_conf);
1146
b0dc7873 1147 if (slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 139) < 0) {
28432466 1148 slirp_smb_cleanup(s);
492efabd 1149 config_error(mon, "conflicting/invalid smbserver address\n");
c92ef6a2 1150 }
63a01ef8
AL
1151}
1152
ad196a9d 1153/* automatic user mode samba server configuration (legacy interface) */
b8e8af38
JK
1154void net_slirp_smb(const char *exported_dir)
1155{
c92ef6a2
JK
1156 struct in_addr vserver_addr = { .s_addr = 0 };
1157
ad196a9d 1158 if (legacy_smb_export) {
b8e8af38
JK
1159 fprintf(stderr, "-smb given twice\n");
1160 exit(1);
1161 }
ad196a9d 1162 legacy_smb_export = exported_dir;
b1c99fcd 1163 if (!TAILQ_EMPTY(&slirp_stacks)) {
492efabd
JK
1164 slirp_smb(TAILQ_FIRST(&slirp_stacks), NULL, exported_dir,
1165 vserver_addr);
b8e8af38
JK
1166 }
1167}
1168
63a01ef8 1169#endif /* !defined(_WIN32) */
b8e8af38 1170
c92ef6a2 1171struct GuestFwd {
8ca9217d 1172 CharDriverState *hd;
c92ef6a2 1173 struct in_addr server;
8ca9217d 1174 int port;
9f8bd042 1175 Slirp *slirp;
511d2b14 1176};
8ca9217d 1177
c92ef6a2 1178static int guestfwd_can_read(void *opaque)
8ca9217d 1179{
c92ef6a2 1180 struct GuestFwd *fwd = opaque;
9f8bd042 1181 return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port);
8ca9217d
AL
1182}
1183
c92ef6a2 1184static void guestfwd_read(void *opaque, const uint8_t *buf, int size)
8ca9217d 1185{
c92ef6a2 1186 struct GuestFwd *fwd = opaque;
9f8bd042 1187 slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size);
8ca9217d
AL
1188}
1189
9f8bd042 1190static void slirp_guestfwd(SlirpState *s, Monitor *mon, const char *config_str,
c92ef6a2 1191 int legacy_format)
ad196a9d 1192{
c92ef6a2
JK
1193 struct in_addr server = { .s_addr = 0 };
1194 struct GuestFwd *fwd;
1195 const char *p;
1196 char buf[128];
1197 char *end;
ad196a9d
JK
1198 int port;
1199
c92ef6a2
JK
1200 p = config_str;
1201 if (legacy_format) {
1202 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
1203 goto fail_syntax;
1204 }
1205 } else {
1206 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
1207 goto fail_syntax;
1208 }
1209 if (strcmp(buf, "tcp") && buf[0] != '\0') {
1210 goto fail_syntax;
1211 }
1212 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
1213 goto fail_syntax;
1214 }
1215 if (buf[0] != '\0' && !inet_aton(buf, &server)) {
1216 goto fail_syntax;
1217 }
1218 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
1219 goto fail_syntax;
1220 }
1221 }
1222 port = strtol(buf, &end, 10);
1223 if (*end != '\0' || port < 1 || port > 65535) {
1224 goto fail_syntax;
ad196a9d 1225 }
ad196a9d 1226
c92ef6a2
JK
1227 fwd = qemu_malloc(sizeof(struct GuestFwd));
1228 snprintf(buf, sizeof(buf), "guestfwd.tcp:%d", port);
1229 fwd->hd = qemu_chr_open(buf, p, NULL);
1230 if (!fwd->hd) {
1231 config_error(mon, "could not open guest forwarding device '%s'\n",
1232 buf);
1233 qemu_free(fwd);
ad196a9d
JK
1234 return;
1235 }
ad196a9d 1236
b0dc7873 1237 if (slirp_add_exec(s->slirp, 3, fwd->hd, &server, port) < 0) {
c92ef6a2
JK
1238 config_error(mon, "conflicting/invalid host:port in guest forwarding "
1239 "rule '%s'\n", config_str);
1240 qemu_free(fwd);
1241 return;
1242 }
b0dc7873
JK
1243 fwd->server = server;
1244 fwd->port = port;
1245 fwd->slirp = s->slirp;
1246
c92ef6a2
JK
1247 qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read,
1248 NULL, fwd);
ad196a9d 1249 return;
c92ef6a2
JK
1250
1251 fail_syntax:
1252 config_error(mon, "invalid guest forwarding rule '%s'\n", config_str);
ad196a9d
JK
1253}
1254
6dbe553f
JK
1255void do_info_usernet(Monitor *mon)
1256{
b1c99fcd 1257 SlirpState *s;
9f8bd042 1258
b1c99fcd
JK
1259 TAILQ_FOREACH(s, &slirp_stacks, entry) {
1260 monitor_printf(mon, "VLAN %d (%s):\n", s->vc->vlan->id, s->vc->name);
1261 slirp_connection_info(s->slirp, mon);
9f8bd042 1262 }
6dbe553f
JK
1263}
1264
63a01ef8
AL
1265#endif /* CONFIG_SLIRP */
1266
1267#if !defined(_WIN32)
1268
1269typedef struct TAPState {
1270 VLANClientState *vc;
1271 int fd;
1272 char down_script[1024];
973cbd37 1273 char down_script_arg[128];
5b01e886 1274 uint8_t buf[4096];
b664e367 1275 unsigned int read_poll : 1;
1f7babf6 1276 unsigned int write_poll : 1;
63a01ef8
AL
1277} TAPState;
1278
b946a153
AL
1279static int launch_script(const char *setup_script, const char *ifname, int fd);
1280
b664e367
MM
1281static int tap_can_send(void *opaque);
1282static void tap_send(void *opaque);
1f7babf6 1283static void tap_writable(void *opaque);
b664e367
MM
1284
1285static void tap_update_fd_handler(TAPState *s)
1286{
1287 qemu_set_fd_handler2(s->fd,
1288 s->read_poll ? tap_can_send : NULL,
1289 s->read_poll ? tap_send : NULL,
1f7babf6 1290 s->write_poll ? tap_writable : NULL,
b664e367
MM
1291 s);
1292}
1293
1294static void tap_read_poll(TAPState *s, int enable)
1295{
1296 s->read_poll = !!enable;
1297 tap_update_fd_handler(s);
1298}
1299
1f7babf6
MM
1300static void tap_write_poll(TAPState *s, int enable)
1301{
1302 s->write_poll = !!enable;
1303 tap_update_fd_handler(s);
1304}
1305
1306static void tap_writable(void *opaque)
1307{
1308 TAPState *s = opaque;
1309
1310 tap_write_poll(s, 0);
1311
1312 qemu_flush_queued_packets(s->vc);
1313}
1314
e3f5ec2b 1315static ssize_t tap_receive_iov(VLANClientState *vc, const struct iovec *iov,
b535b7b2
AL
1316 int iovcnt)
1317{
e3f5ec2b 1318 TAPState *s = vc->opaque;
b535b7b2
AL
1319 ssize_t len;
1320
1321 do {
1322 len = writev(s->fd, iov, iovcnt);
1f7babf6
MM
1323 } while (len == -1 && errno == EINTR);
1324
1325 if (len == -1 && errno == EAGAIN) {
1326 tap_write_poll(s, 1);
1327 return 0;
1328 }
b535b7b2
AL
1329
1330 return len;
1331}
b535b7b2 1332
4f1c942b 1333static ssize_t tap_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
63a01ef8 1334{
e3f5ec2b 1335 TAPState *s = vc->opaque;
4f1c942b
MM
1336 ssize_t len;
1337
1338 do {
1339 len = write(s->fd, buf, size);
1340 } while (len == -1 && (errno == EINTR || errno == EAGAIN));
1341
1342 return len;
63a01ef8
AL
1343}
1344
3471b757
MM
1345static int tap_can_send(void *opaque)
1346{
1347 TAPState *s = opaque;
1348
1349 return qemu_can_send_packet(s->vc);
1350}
1351
63a01ef8 1352#ifdef __sun__
5a6d8815
MM
1353static ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
1354{
63a01ef8
AL
1355 struct strbuf sbuf;
1356 int f = 0;
5a6d8815
MM
1357
1358 sbuf.maxlen = maxlen;
3f4cb3d3 1359 sbuf.buf = (char *)buf;
5a6d8815
MM
1360
1361 return getmsg(tapfd, NULL, &sbuf, &f) >= 0 ? sbuf.len : -1;
1362}
63a01ef8 1363#else
5a6d8815
MM
1364static ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
1365{
1366 return read(tapfd, buf, maxlen);
1367}
63a01ef8 1368#endif
5a6d8815 1369
783527a9 1370static void tap_send_completed(VLANClientState *vc, ssize_t len)
e19eb224
MM
1371{
1372 TAPState *s = vc->opaque;
b664e367 1373 tap_read_poll(s, 1);
e19eb224
MM
1374}
1375
5a6d8815
MM
1376static void tap_send(void *opaque)
1377{
1378 TAPState *s = opaque;
5a6d8815
MM
1379 int size;
1380
3e01c7b9
MM
1381 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
1382 if (size <= 0) {
1383 return;
1384 }
e19eb224 1385
3e01c7b9
MM
1386 size = qemu_send_packet_async(s->vc, s->buf, size, tap_send_completed);
1387 if (size == 0) {
1388 tap_read_poll(s, 0);
1389 }
63a01ef8
AL
1390}
1391
0df0ff6d 1392#ifdef TUNSETSNDBUF
fc5b81d1
MM
1393/* sndbuf should be set to a value lower than the tx queue
1394 * capacity of any destination network interface.
1395 * Ethernet NICs generally have txqueuelen=1000, so 1Mb is
1396 * a good default, given a 1500 byte MTU.
1397 */
1398#define TAP_DEFAULT_SNDBUF 1024*1024
1399
1400static void tap_set_sndbuf(TAPState *s, const char *sndbuf_str, Monitor *mon)
1401{
1402 int sndbuf = TAP_DEFAULT_SNDBUF;
1403
1404 if (sndbuf_str) {
1405 sndbuf = atoi(sndbuf_str);
1406 }
1407
1408 if (!sndbuf) {
1409 sndbuf = INT_MAX;
1410 }
1411
d026fb6d 1412 if (ioctl(s->fd, TUNSETSNDBUF, &sndbuf) == -1 && sndbuf_str) {
0df0ff6d
MM
1413 config_error(mon, "TUNSETSNDBUF ioctl failed: %s\n",
1414 strerror(errno));
1415 }
fc5b81d1 1416}
0df0ff6d 1417#else
fc5b81d1
MM
1418static void tap_set_sndbuf(TAPState *s, const char *sndbuf_str, Monitor *mon)
1419{
1420 if (sndbuf_str) {
1421 config_error(mon, "No '-net tap,sndbuf=<nbytes>' support available\n");
1422 }
0df0ff6d 1423}
fc5b81d1 1424#endif /* TUNSETSNDBUF */
0df0ff6d 1425
b946a153
AL
1426static void tap_cleanup(VLANClientState *vc)
1427{
1428 TAPState *s = vc->opaque;
1429
b9adce2c
MM
1430 qemu_purge_queued_packets(vc);
1431
b946a153
AL
1432 if (s->down_script[0])
1433 launch_script(s->down_script, s->down_script_arg, s->fd);
1434
b664e367 1435 tap_read_poll(s, 0);
1f7babf6 1436 tap_write_poll(s, 0);
b946a153
AL
1437 close(s->fd);
1438 qemu_free(s);
1439}
1440
63a01ef8
AL
1441/* fd support */
1442
7a9f6e4a
AL
1443static TAPState *net_tap_fd_init(VLANState *vlan,
1444 const char *model,
1445 const char *name,
1446 int fd)
63a01ef8
AL
1447{
1448 TAPState *s;
1449
1450 s = qemu_mallocz(sizeof(TAPState));
63a01ef8 1451 s->fd = fd;
463af534
MM
1452 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, tap_receive,
1453 tap_receive_iov, tap_cleanup, s);
b664e367 1454 tap_read_poll(s, 1);
7cb7434b 1455 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd);
63a01ef8
AL
1456 return s;
1457}
1458
179a2c19 1459#if defined (HOST_BSD) || defined (__FreeBSD_kernel__)
63a01ef8
AL
1460static int tap_open(char *ifname, int ifname_size)
1461{
1462 int fd;
1463 char *dev;
1464 struct stat s;
1465
1466 TFR(fd = open("/dev/tap", O_RDWR));
1467 if (fd < 0) {
1468 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
1469 return -1;
1470 }
1471
1472 fstat(fd, &s);
1473 dev = devname(s.st_rdev, S_IFCHR);
1474 pstrcpy(ifname, ifname_size, dev);
1475
1476 fcntl(fd, F_SETFL, O_NONBLOCK);
1477 return fd;
1478}
1479#elif defined(__sun__)
1480#define TUNNEWPPA (('T'<<16) | 0x0001)
1481/*
1482 * Allocate TAP device, returns opened fd.
1483 * Stores dev name in the first arg(must be large enough).
1484 */
3f4cb3d3 1485static int tap_alloc(char *dev, size_t dev_size)
63a01ef8
AL
1486{
1487 int tap_fd, if_fd, ppa = -1;
1488 static int ip_fd = 0;
1489 char *ptr;
1490
1491 static int arp_fd = 0;
1492 int ip_muxid, arp_muxid;
1493 struct strioctl strioc_if, strioc_ppa;
1494 int link_type = I_PLINK;;
1495 struct lifreq ifr;
1496 char actual_name[32] = "";
1497
1498 memset(&ifr, 0x0, sizeof(ifr));
1499
1500 if( *dev ){
1501 ptr = dev;
47398b9c 1502 while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
63a01ef8
AL
1503 ppa = atoi(ptr);
1504 }
1505
1506 /* Check if IP device was opened */
1507 if( ip_fd )
1508 close(ip_fd);
1509
1510 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
1511 if (ip_fd < 0) {
1512 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
1513 return -1;
1514 }
1515
1516 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
1517 if (tap_fd < 0) {
1518 syslog(LOG_ERR, "Can't open /dev/tap");
1519 return -1;
1520 }
1521
1522 /* Assign a new PPA and get its unit number. */
1523 strioc_ppa.ic_cmd = TUNNEWPPA;
1524 strioc_ppa.ic_timout = 0;
1525 strioc_ppa.ic_len = sizeof(ppa);
1526 strioc_ppa.ic_dp = (char *)&ppa;
1527 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
1528 syslog (LOG_ERR, "Can't assign new interface");
1529
1530 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
1531 if (if_fd < 0) {
1532 syslog(LOG_ERR, "Can't open /dev/tap (2)");
1533 return -1;
1534 }
1535 if(ioctl(if_fd, I_PUSH, "ip") < 0){
1536 syslog(LOG_ERR, "Can't push IP module");
1537 return -1;
1538 }
1539
1540 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
1541 syslog(LOG_ERR, "Can't get flags\n");
1542
1543 snprintf (actual_name, 32, "tap%d", ppa);
1544 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1545
1546 ifr.lifr_ppa = ppa;
1547 /* Assign ppa according to the unit number returned by tun device */
1548
1549 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
1550 syslog (LOG_ERR, "Can't set PPA %d", ppa);
1551 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
1552 syslog (LOG_ERR, "Can't get flags\n");
1553 /* Push arp module to if_fd */
1554 if (ioctl (if_fd, I_PUSH, "arp") < 0)
1555 syslog (LOG_ERR, "Can't push ARP module (2)");
1556
1557 /* Push arp module to ip_fd */
1558 if (ioctl (ip_fd, I_POP, NULL) < 0)
1559 syslog (LOG_ERR, "I_POP failed\n");
1560 if (ioctl (ip_fd, I_PUSH, "arp") < 0)
1561 syslog (LOG_ERR, "Can't push ARP module (3)\n");
1562 /* Open arp_fd */
1563 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
1564 if (arp_fd < 0)
1565 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
1566
1567 /* Set ifname to arp */
1568 strioc_if.ic_cmd = SIOCSLIFNAME;
1569 strioc_if.ic_timout = 0;
1570 strioc_if.ic_len = sizeof(ifr);
1571 strioc_if.ic_dp = (char *)&ifr;
1572 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
1573 syslog (LOG_ERR, "Can't set ifname to arp\n");
1574 }
1575
1576 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
1577 syslog(LOG_ERR, "Can't link TAP device to IP");
1578 return -1;
1579 }
1580
1581 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
1582 syslog (LOG_ERR, "Can't link TAP device to ARP");
1583
1584 close (if_fd);
1585
1586 memset(&ifr, 0x0, sizeof(ifr));
1587 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1588 ifr.lifr_ip_muxid = ip_muxid;
1589 ifr.lifr_arp_muxid = arp_muxid;
1590
1591 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
1592 {
1593 ioctl (ip_fd, I_PUNLINK , arp_muxid);
1594 ioctl (ip_fd, I_PUNLINK, ip_muxid);
1595 syslog (LOG_ERR, "Can't set multiplexor id");
1596 }
1597
1598 snprintf(dev, dev_size, "tap%d", ppa);
1599 return tap_fd;
1600}
1601
1602static int tap_open(char *ifname, int ifname_size)
1603{
1604 char dev[10]="";
1605 int fd;
1606 if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
1607 fprintf(stderr, "Cannot allocate TAP device\n");
1608 return -1;
1609 }
1610 pstrcpy(ifname, ifname_size, dev);
1611 fcntl(fd, F_SETFL, O_NONBLOCK);
1612 return fd;
1613}
b29fe3ed 1614#elif defined (_AIX)
1615static int tap_open(char *ifname, int ifname_size)
1616{
1617 fprintf (stderr, "no tap on AIX\n");
1618 return -1;
1619}
63a01ef8
AL
1620#else
1621static int tap_open(char *ifname, int ifname_size)
1622{
1623 struct ifreq ifr;
1624 int fd, ret;
1625
1626 TFR(fd = open("/dev/net/tun", O_RDWR));
1627 if (fd < 0) {
1628 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1629 return -1;
1630 }
1631 memset(&ifr, 0, sizeof(ifr));
1632 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1633 if (ifname[0] != '\0')
1634 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
1635 else
1636 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
1637 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1638 if (ret != 0) {
1639 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1640 close(fd);
1641 return -1;
1642 }
1643 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1644 fcntl(fd, F_SETFL, O_NONBLOCK);
1645 return fd;
1646}
1647#endif
1648
1649static int launch_script(const char *setup_script, const char *ifname, int fd)
1650{
7c3370d4 1651 sigset_t oldmask, mask;
63a01ef8
AL
1652 int pid, status;
1653 char *args[3];
1654 char **parg;
1655
7c3370d4
JK
1656 sigemptyset(&mask);
1657 sigaddset(&mask, SIGCHLD);
1658 sigprocmask(SIG_BLOCK, &mask, &oldmask);
1659
1660 /* try to launch network script */
1661 pid = fork();
1662 if (pid == 0) {
1663 int open_max = sysconf(_SC_OPEN_MAX), i;
1664
1665 for (i = 0; i < open_max; i++) {
1666 if (i != STDIN_FILENO &&
1667 i != STDOUT_FILENO &&
1668 i != STDERR_FILENO &&
1669 i != fd) {
1670 close(i);
63a01ef8
AL
1671 }
1672 }
7c3370d4
JK
1673 parg = args;
1674 *parg++ = (char *)setup_script;
1675 *parg++ = (char *)ifname;
1676 *parg++ = NULL;
1677 execv(setup_script, args);
1678 _exit(1);
1679 } else if (pid > 0) {
1680 while (waitpid(pid, &status, 0) != pid) {
1681 /* loop */
1682 }
1683 sigprocmask(SIG_SETMASK, &oldmask, NULL);
1684
1685 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1686 return 0;
1687 }
1688 }
1689 fprintf(stderr, "%s: could not launch network script\n", setup_script);
1690 return -1;
63a01ef8
AL
1691}
1692
4a77b25e
MM
1693static TAPState *net_tap_init(VLANState *vlan, const char *model,
1694 const char *name, const char *ifname1,
1695 const char *setup_script, const char *down_script)
63a01ef8
AL
1696{
1697 TAPState *s;
1698 int fd;
1699 char ifname[128];
1700
1701 if (ifname1 != NULL)
1702 pstrcpy(ifname, sizeof(ifname), ifname1);
1703 else
1704 ifname[0] = '\0';
1705 TFR(fd = tap_open(ifname, sizeof(ifname)));
1706 if (fd < 0)
4a77b25e 1707 return NULL;
63a01ef8
AL
1708
1709 if (!setup_script || !strcmp(setup_script, "no"))
1710 setup_script = "";
4a77b25e
MM
1711 if (setup_script[0] != '\0' &&
1712 launch_script(setup_script, ifname, fd)) {
1713 return NULL;
63a01ef8 1714 }
7a9f6e4a 1715 s = net_tap_fd_init(vlan, model, name, fd);
63a01ef8 1716 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
7cb7434b
AL
1717 "ifname=%s,script=%s,downscript=%s",
1718 ifname, setup_script, down_script);
973cbd37 1719 if (down_script && strcmp(down_script, "no")) {
63a01ef8 1720 snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
973cbd37
AL
1721 snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
1722 }
4a77b25e 1723 return s;
63a01ef8
AL
1724}
1725
1726#endif /* !_WIN32 */
1727
1728#if defined(CONFIG_VDE)
1729typedef struct VDEState {
1730 VLANClientState *vc;
1731 VDECONN *vde;
1732} VDEState;
1733
1734static void vde_to_qemu(void *opaque)
1735{
1736 VDEState *s = opaque;
1737 uint8_t buf[4096];
1738 int size;
1739
cc63bb0f 1740 size = vde_recv(s->vde, (char *)buf, sizeof(buf), 0);
63a01ef8
AL
1741 if (size > 0) {
1742 qemu_send_packet(s->vc, buf, size);
1743 }
1744}
1745
4f1c942b 1746static ssize_t vde_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
63a01ef8 1747{
e3f5ec2b 1748 VDEState *s = vc->opaque;
068daedd 1749 ssize_t ret;
4f1c942b
MM
1750
1751 do {
1752 ret = vde_send(s->vde, (const char *)buf, size, 0);
1753 } while (ret < 0 && errno == EINTR);
1754
1755 return ret;
63a01ef8
AL
1756}
1757
b946a153
AL
1758static void vde_cleanup(VLANClientState *vc)
1759{
1760 VDEState *s = vc->opaque;
1761 qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL);
1762 vde_close(s->vde);
1763 qemu_free(s);
1764}
1765
7a9f6e4a
AL
1766static int net_vde_init(VLANState *vlan, const char *model,
1767 const char *name, const char *sock,
bf38c1a0 1768 int port, const char *group, int mode)
63a01ef8
AL
1769{
1770 VDEState *s;
1771 char *init_group = strlen(group) ? (char *)group : NULL;
1772 char *init_sock = strlen(sock) ? (char *)sock : NULL;
1773
1774 struct vde_open_args args = {
1775 .port = port,
1776 .group = init_group,
1777 .mode = mode,
1778 };
1779
1780 s = qemu_mallocz(sizeof(VDEState));
cc63bb0f 1781 s->vde = vde_open(init_sock, (char *)"QEMU", &args);
63a01ef8
AL
1782 if (!s->vde){
1783 free(s);
1784 return -1;
1785 }
e3f5ec2b 1786 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, vde_receive,
b946a153 1787 NULL, vde_cleanup, s);
63a01ef8 1788 qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
7cb7434b 1789 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d",
63a01ef8
AL
1790 sock, vde_datafd(s->vde));
1791 return 0;
1792}
1793#endif
1794
1795/* network connection */
1796typedef struct NetSocketState {
1797 VLANClientState *vc;
1798 int fd;
1799 int state; /* 0 = getting length, 1 = getting data */
abcd2baa
AL
1800 unsigned int index;
1801 unsigned int packet_len;
63a01ef8
AL
1802 uint8_t buf[4096];
1803 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1804} NetSocketState;
1805
1806typedef struct NetSocketListenState {
1807 VLANState *vlan;
bf38c1a0 1808 char *model;
7a9f6e4a 1809 char *name;
63a01ef8
AL
1810 int fd;
1811} NetSocketListenState;
1812
1813/* XXX: we consider we can send the whole packet without blocking */
4f1c942b 1814static ssize_t net_socket_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
63a01ef8 1815{
e3f5ec2b 1816 NetSocketState *s = vc->opaque;
63a01ef8
AL
1817 uint32_t len;
1818 len = htonl(size);
1819
1820 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
4f1c942b 1821 return send_all(s->fd, buf, size);
63a01ef8
AL
1822}
1823
4f1c942b 1824static ssize_t net_socket_receive_dgram(VLANClientState *vc, const uint8_t *buf, size_t size)
63a01ef8 1825{
e3f5ec2b 1826 NetSocketState *s = vc->opaque;
4f1c942b 1827
70503264 1828 return sendto(s->fd, (const void *)buf, size, 0,
4f1c942b 1829 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
63a01ef8
AL
1830}
1831
1832static void net_socket_send(void *opaque)
1833{
1834 NetSocketState *s = opaque;
abcd2baa
AL
1835 int size, err;
1836 unsigned l;
63a01ef8
AL
1837 uint8_t buf1[4096];
1838 const uint8_t *buf;
1839
c5b76b38 1840 size = recv(s->fd, (void *)buf1, sizeof(buf1), 0);
63a01ef8
AL
1841 if (size < 0) {
1842 err = socket_error();
1843 if (err != EWOULDBLOCK)
1844 goto eoc;
1845 } else if (size == 0) {
1846 /* end of connection */
1847 eoc:
1848 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1849 closesocket(s->fd);
1850 return;
1851 }
1852 buf = buf1;
1853 while (size > 0) {
1854 /* reassemble a packet from the network */
1855 switch(s->state) {
1856 case 0:
1857 l = 4 - s->index;
1858 if (l > size)
1859 l = size;
1860 memcpy(s->buf + s->index, buf, l);
1861 buf += l;
1862 size -= l;
1863 s->index += l;
1864 if (s->index == 4) {
1865 /* got length */
1866 s->packet_len = ntohl(*(uint32_t *)s->buf);
1867 s->index = 0;
1868 s->state = 1;
1869 }
1870 break;
1871 case 1:
1872 l = s->packet_len - s->index;
1873 if (l > size)
1874 l = size;
abcd2baa
AL
1875 if (s->index + l <= sizeof(s->buf)) {
1876 memcpy(s->buf + s->index, buf, l);
1877 } else {
1878 fprintf(stderr, "serious error: oversized packet received,"
1879 "connection terminated.\n");
1880 s->state = 0;
1881 goto eoc;
1882 }
1883
63a01ef8
AL
1884 s->index += l;
1885 buf += l;
1886 size -= l;
1887 if (s->index >= s->packet_len) {
1888 qemu_send_packet(s->vc, s->buf, s->packet_len);
1889 s->index = 0;
1890 s->state = 0;
1891 }
1892 break;
1893 }
1894 }
1895}
1896
1897static void net_socket_send_dgram(void *opaque)
1898{
1899 NetSocketState *s = opaque;
1900 int size;
1901
c5b76b38 1902 size = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0);
63a01ef8
AL
1903 if (size < 0)
1904 return;
1905 if (size == 0) {
1906 /* end of connection */
1907 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1908 return;
1909 }
1910 qemu_send_packet(s->vc, s->buf, size);
1911}
1912
1913static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
1914{
1915 struct ip_mreq imr;
1916 int fd;
1917 int val, ret;
1918 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
1919 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1920 inet_ntoa(mcastaddr->sin_addr),
1921 (int)ntohl(mcastaddr->sin_addr.s_addr));
1922 return -1;
1923
1924 }
1925 fd = socket(PF_INET, SOCK_DGRAM, 0);
1926 if (fd < 0) {
1927 perror("socket(PF_INET, SOCK_DGRAM)");
1928 return -1;
1929 }
1930
1931 val = 1;
1932 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1933 (const char *)&val, sizeof(val));
1934 if (ret < 0) {
1935 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1936 goto fail;
1937 }
1938
1939 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
1940 if (ret < 0) {
1941 perror("bind");
1942 goto fail;
1943 }
1944
1945 /* Add host to multicast group */
1946 imr.imr_multiaddr = mcastaddr->sin_addr;
1947 imr.imr_interface.s_addr = htonl(INADDR_ANY);
1948
1949 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1950 (const char *)&imr, sizeof(struct ip_mreq));
1951 if (ret < 0) {
1952 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1953 goto fail;
1954 }
1955
1956 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1957 val = 1;
1958 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1959 (const char *)&val, sizeof(val));
1960 if (ret < 0) {
1961 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1962 goto fail;
1963 }
1964
1965 socket_set_nonblock(fd);
1966 return fd;
1967fail:
1968 if (fd >= 0)
1969 closesocket(fd);
1970 return -1;
1971}
1972
b946a153
AL
1973static void net_socket_cleanup(VLANClientState *vc)
1974{
1975 NetSocketState *s = vc->opaque;
1976 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1977 close(s->fd);
1978 qemu_free(s);
1979}
1980
7a9f6e4a
AL
1981static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
1982 const char *model,
1983 const char *name,
bf38c1a0 1984 int fd, int is_connected)
63a01ef8
AL
1985{
1986 struct sockaddr_in saddr;
1987 int newfd;
1988 socklen_t saddr_len;
1989 NetSocketState *s;
1990
1991 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1992 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1993 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1994 */
1995
1996 if (is_connected) {
1997 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
1998 /* must be bound */
1999 if (saddr.sin_addr.s_addr==0) {
2000 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
2001 fd);
2002 return NULL;
2003 }
2004 /* clone dgram socket */
2005 newfd = net_socket_mcast_create(&saddr);
2006 if (newfd < 0) {
2007 /* error already reported by net_socket_mcast_create() */
2008 close(fd);
2009 return NULL;
2010 }
2011 /* clone newfd to fd, close newfd */
2012 dup2(newfd, fd);
2013 close(newfd);
2014
2015 } else {
2016 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
2017 fd, strerror(errno));
2018 return NULL;
2019 }
2020 }
2021
2022 s = qemu_mallocz(sizeof(NetSocketState));
63a01ef8
AL
2023 s->fd = fd;
2024
463af534 2025 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, net_socket_receive_dgram,
b946a153 2026 NULL, net_socket_cleanup, s);
63a01ef8
AL
2027 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
2028
2029 /* mcast: save bound address as dst */
2030 if (is_connected) s->dgram_dst=saddr;
2031
2032 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
2033 "socket: fd=%d (%s mcast=%s:%d)",
2034 fd, is_connected? "cloned" : "",
2035 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
2036 return s;
2037}
2038
2039static void net_socket_connect(void *opaque)
2040{
2041 NetSocketState *s = opaque;
2042 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
2043}
2044
7a9f6e4a
AL
2045static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
2046 const char *model,
2047 const char *name,
bf38c1a0 2048 int fd, int is_connected)
63a01ef8
AL
2049{
2050 NetSocketState *s;
2051 s = qemu_mallocz(sizeof(NetSocketState));
63a01ef8 2052 s->fd = fd;
463af534 2053 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, net_socket_receive,
b946a153 2054 NULL, net_socket_cleanup, s);
63a01ef8
AL
2055 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
2056 "socket: fd=%d", fd);
2057 if (is_connected) {
2058 net_socket_connect(s);
2059 } else {
2060 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
2061 }
2062 return s;
2063}
2064
7a9f6e4a
AL
2065static NetSocketState *net_socket_fd_init(VLANState *vlan,
2066 const char *model, const char *name,
bf38c1a0 2067 int fd, int is_connected)
63a01ef8
AL
2068{
2069 int so_type=-1, optlen=sizeof(so_type);
2070
2071 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
2072 (socklen_t *)&optlen)< 0) {
2073 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
2074 return NULL;
2075 }
2076 switch(so_type) {
2077 case SOCK_DGRAM:
7a9f6e4a 2078 return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected);
63a01ef8 2079 case SOCK_STREAM:
7a9f6e4a 2080 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
63a01ef8
AL
2081 default:
2082 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
2083 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
7a9f6e4a 2084 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
63a01ef8
AL
2085 }
2086 return NULL;
2087}
2088
2089static void net_socket_accept(void *opaque)
2090{
2091 NetSocketListenState *s = opaque;
2092 NetSocketState *s1;
2093 struct sockaddr_in saddr;
2094 socklen_t len;
2095 int fd;
2096
2097 for(;;) {
2098 len = sizeof(saddr);
2099 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
2100 if (fd < 0 && errno != EINTR) {
2101 return;
2102 } else if (fd >= 0) {
2103 break;
2104 }
2105 }
7a9f6e4a 2106 s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1);
63a01ef8
AL
2107 if (!s1) {
2108 closesocket(fd);
2109 } else {
2110 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
2111 "socket: connection from %s:%d",
2112 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
2113 }
2114}
2115
7a9f6e4a
AL
2116static int net_socket_listen_init(VLANState *vlan,
2117 const char *model,
2118 const char *name,
bf38c1a0 2119 const char *host_str)
63a01ef8
AL
2120{
2121 NetSocketListenState *s;
2122 int fd, val, ret;
2123 struct sockaddr_in saddr;
2124
2125 if (parse_host_port(&saddr, host_str) < 0)
2126 return -1;
2127
2128 s = qemu_mallocz(sizeof(NetSocketListenState));
63a01ef8
AL
2129
2130 fd = socket(PF_INET, SOCK_STREAM, 0);
2131 if (fd < 0) {
2132 perror("socket");
2133 return -1;
2134 }
2135 socket_set_nonblock(fd);
2136
2137 /* allow fast reuse */
2138 val = 1;
2139 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
2140
2141 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
2142 if (ret < 0) {
2143 perror("bind");
2144 return -1;
2145 }
2146 ret = listen(fd, 0);
2147 if (ret < 0) {
2148 perror("listen");
2149 return -1;
2150 }
2151 s->vlan = vlan;
bf38c1a0 2152 s->model = strdup(model);
ea053add 2153 s->name = name ? strdup(name) : NULL;
63a01ef8
AL
2154 s->fd = fd;
2155 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
2156 return 0;
2157}
2158
7a9f6e4a
AL
2159static int net_socket_connect_init(VLANState *vlan,
2160 const char *model,
2161 const char *name,
bf38c1a0 2162 const char *host_str)
63a01ef8
AL
2163{
2164 NetSocketState *s;
2165 int fd, connected, ret, err;
2166 struct sockaddr_in saddr;
2167
2168 if (parse_host_port(&saddr, host_str) < 0)
2169 return -1;
2170
2171 fd = socket(PF_INET, SOCK_STREAM, 0);
2172 if (fd < 0) {
2173 perror("socket");
2174 return -1;
2175 }
2176 socket_set_nonblock(fd);
2177
2178 connected = 0;
2179 for(;;) {
2180 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
2181 if (ret < 0) {
2182 err = socket_error();
2183 if (err == EINTR || err == EWOULDBLOCK) {
2184 } else if (err == EINPROGRESS) {
2185 break;
2186#ifdef _WIN32
2187 } else if (err == WSAEALREADY) {
2188 break;
2189#endif
2190 } else {
2191 perror("connect");
2192 closesocket(fd);
2193 return -1;
2194 }
2195 } else {
2196 connected = 1;
2197 break;
2198 }
2199 }
7a9f6e4a 2200 s = net_socket_fd_init(vlan, model, name, fd, connected);
63a01ef8
AL
2201 if (!s)
2202 return -1;
2203 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
2204 "socket: connect to %s:%d",
2205 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
2206 return 0;
2207}
2208
7a9f6e4a
AL
2209static int net_socket_mcast_init(VLANState *vlan,
2210 const char *model,
2211 const char *name,
bf38c1a0 2212 const char *host_str)
63a01ef8
AL
2213{
2214 NetSocketState *s;
2215 int fd;
2216 struct sockaddr_in saddr;
2217
2218 if (parse_host_port(&saddr, host_str) < 0)
2219 return -1;
2220
2221
2222 fd = net_socket_mcast_create(&saddr);
2223 if (fd < 0)
2224 return -1;
2225
7a9f6e4a 2226 s = net_socket_fd_init(vlan, model, name, fd, 0);
63a01ef8
AL
2227 if (!s)
2228 return -1;
2229
2230 s->dgram_dst = saddr;
2231
2232 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
2233 "socket: mcast=%s:%d",
2234 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
2235 return 0;
2236
2237}
2238
bb9ea79e
AL
2239typedef struct DumpState {
2240 VLANClientState *pcap_vc;
2241 int fd;
2242 int pcap_caplen;
2243} DumpState;
2244
2245#define PCAP_MAGIC 0xa1b2c3d4
2246
2247struct pcap_file_hdr {
2248 uint32_t magic;
2249 uint16_t version_major;
2250 uint16_t version_minor;
2251 int32_t thiszone;
2252 uint32_t sigfigs;
2253 uint32_t snaplen;
2254 uint32_t linktype;
2255};
2256
2257struct pcap_sf_pkthdr {
2258 struct {
2259 int32_t tv_sec;
2260 int32_t tv_usec;
2261 } ts;
2262 uint32_t caplen;
2263 uint32_t len;
2264};
2265
4f1c942b 2266static ssize_t dump_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
bb9ea79e 2267{
e3f5ec2b 2268 DumpState *s = vc->opaque;
bb9ea79e
AL
2269 struct pcap_sf_pkthdr hdr;
2270 int64_t ts;
2271 int caplen;
2272
2273 /* Early return in case of previous error. */
2274 if (s->fd < 0) {
4f1c942b 2275 return size;
bb9ea79e
AL
2276 }
2277
37cb6fc3 2278 ts = muldiv64(qemu_get_clock(vm_clock), 1000000, ticks_per_sec);
bb9ea79e
AL
2279 caplen = size > s->pcap_caplen ? s->pcap_caplen : size;
2280
37cb6fc3 2281 hdr.ts.tv_sec = ts / 1000000;
bb9ea79e
AL
2282 hdr.ts.tv_usec = ts % 1000000;
2283 hdr.caplen = caplen;
2284 hdr.len = size;
2285 if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) ||
2286 write(s->fd, buf, caplen) != caplen) {
2287 qemu_log("-net dump write error - stop dump\n");
2288 close(s->fd);
2289 s->fd = -1;
2290 }
4f1c942b
MM
2291
2292 return size;
bb9ea79e
AL
2293}
2294
2295static void net_dump_cleanup(VLANClientState *vc)
2296{
2297 DumpState *s = vc->opaque;
2298
2299 close(s->fd);
2300 qemu_free(s);
2301}
2302
10ae5a7a 2303static int net_dump_init(Monitor *mon, VLANState *vlan, const char *device,
bb9ea79e
AL
2304 const char *name, const char *filename, int len)
2305{
2306 struct pcap_file_hdr hdr;
2307 DumpState *s;
2308
2309 s = qemu_malloc(sizeof(DumpState));
2310
024431b3 2311 s->fd = open(filename, O_CREAT | O_WRONLY | O_BINARY, 0644);
bb9ea79e 2312 if (s->fd < 0) {
10ae5a7a 2313 config_error(mon, "-net dump: can't open %s\n", filename);
bb9ea79e
AL
2314 return -1;
2315 }
2316
2317 s->pcap_caplen = len;
2318
2319 hdr.magic = PCAP_MAGIC;
2320 hdr.version_major = 2;
2321 hdr.version_minor = 4;
2322 hdr.thiszone = 0;
2323 hdr.sigfigs = 0;
2324 hdr.snaplen = s->pcap_caplen;
2325 hdr.linktype = 1;
2326
2327 if (write(s->fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
10ae5a7a 2328 config_error(mon, "-net dump write error: %s\n", strerror(errno));
bb9ea79e
AL
2329 close(s->fd);
2330 qemu_free(s);
2331 return -1;
2332 }
2333
463af534 2334 s->pcap_vc = qemu_new_vlan_client(vlan, device, name, NULL, dump_receive, NULL,
bb9ea79e
AL
2335 net_dump_cleanup, s);
2336 snprintf(s->pcap_vc->info_str, sizeof(s->pcap_vc->info_str),
2337 "dump to %s (len=%d)", filename, len);
2338 return 0;
2339}
2340
63a01ef8 2341/* find or alloc a new VLAN */
1a609520 2342VLANState *qemu_find_vlan(int id, int allocate)
63a01ef8
AL
2343{
2344 VLANState **pvlan, *vlan;
2345 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2346 if (vlan->id == id)
2347 return vlan;
2348 }
1a609520
JK
2349 if (!allocate) {
2350 return NULL;
2351 }
63a01ef8 2352 vlan = qemu_mallocz(sizeof(VLANState));
63a01ef8 2353 vlan->id = id;
67867f38 2354 TAILQ_INIT(&vlan->send_queue);
63a01ef8
AL
2355 vlan->next = NULL;
2356 pvlan = &first_vlan;
2357 while (*pvlan != NULL)
2358 pvlan = &(*pvlan)->next;
2359 *pvlan = vlan;
2360 return vlan;
2361}
2362
7697079b
AL
2363static int nic_get_free_idx(void)
2364{
2365 int index;
2366
2367 for (index = 0; index < MAX_NICS; index++)
2368 if (!nd_table[index].used)
2369 return index;
2370 return -1;
2371}
2372
d07f22c5
AL
2373void qemu_check_nic_model(NICInfo *nd, const char *model)
2374{
2375 const char *models[2];
2376
2377 models[0] = model;
2378 models[1] = NULL;
2379
2380 qemu_check_nic_model_list(nd, models, model);
2381}
2382
2383void qemu_check_nic_model_list(NICInfo *nd, const char * const *models,
2384 const char *default_model)
2385{
2386 int i, exit_status = 0;
2387
2388 if (!nd->model)
2389 nd->model = strdup(default_model);
2390
2391 if (strcmp(nd->model, "?") != 0) {
2392 for (i = 0 ; models[i]; i++)
2393 if (strcmp(nd->model, models[i]) == 0)
2394 return;
2395
2396 fprintf(stderr, "qemu: Unsupported NIC model: %s\n", nd->model);
2397 exit_status = 1;
2398 }
2399
2400 fprintf(stderr, "qemu: Supported NIC models: ");
2401 for (i = 0 ; models[i]; i++)
2402 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
2403
2404 exit(exit_status);
2405}
2406
413fb241
MM
2407static int net_handle_fd_param(Monitor *mon, const char *param)
2408{
2409 if (!qemu_isdigit(param[0])) {
2410 int fd;
2411
2412 fd = monitor_get_fd(mon, param);
2413 if (fd == -1) {
2414 config_error(mon, "No file descriptor named %s found", param);
2415 return -1;
2416 }
2417
2418 return fd;
2419 } else {
2420 return strtol(param, NULL, 0);
2421 }
2422}
2423
10ae5a7a 2424int net_client_init(Monitor *mon, const char *device, const char *p)
63a01ef8
AL
2425{
2426 char buf[1024];
2427 int vlan_id, ret;
2428 VLANState *vlan;
7a9f6e4a 2429 char *name = NULL;
63a01ef8
AL
2430
2431 vlan_id = 0;
2432 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
2433 vlan_id = strtol(buf, NULL, 0);
2434 }
1a609520 2435 vlan = qemu_find_vlan(vlan_id, 1);
9036de1a 2436
7a9f6e4a 2437 if (get_param_value(buf, sizeof(buf), "name", p)) {
10ae5a7a 2438 name = qemu_strdup(buf);
7a9f6e4a 2439 }
63a01ef8 2440 if (!strcmp(device, "nic")) {
8e4416af 2441 static const char * const nic_params[] = {
eb54b6dc 2442 "vlan", "name", "macaddr", "model", "addr", "id", "vectors", NULL
8e4416af 2443 };
63a01ef8
AL
2444 NICInfo *nd;
2445 uint8_t *macaddr;
7697079b 2446 int idx = nic_get_free_idx();
63a01ef8 2447
0aa7a205 2448 if (check_params(buf, sizeof(buf), nic_params, p) < 0) {
10ae5a7a
JK
2449 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
2450 ret = -1;
2451 goto out;
8e4416af 2452 }
7697079b 2453 if (idx == -1 || nb_nics >= MAX_NICS) {
10ae5a7a 2454 config_error(mon, "Too Many NICs\n");
771f1339
AL
2455 ret = -1;
2456 goto out;
63a01ef8 2457 }
7697079b 2458 nd = &nd_table[idx];
63a01ef8
AL
2459 macaddr = nd->macaddr;
2460 macaddr[0] = 0x52;
2461 macaddr[1] = 0x54;
2462 macaddr[2] = 0x00;
2463 macaddr[3] = 0x12;
2464 macaddr[4] = 0x34;
7697079b 2465 macaddr[5] = 0x56 + idx;
63a01ef8
AL
2466
2467 if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
2468 if (parse_macaddr(macaddr, buf) < 0) {
10ae5a7a 2469 config_error(mon, "invalid syntax for ethernet address\n");
771f1339
AL
2470 ret = -1;
2471 goto out;
63a01ef8
AL
2472 }
2473 }
2474 if (get_param_value(buf, sizeof(buf), "model", p)) {
2475 nd->model = strdup(buf);
2476 }
5607c388
MA
2477 if (get_param_value(buf, sizeof(buf), "addr", p)) {
2478 nd->devaddr = strdup(buf);
2479 }
eb54b6dc
GH
2480 if (get_param_value(buf, sizeof(buf), "id", p)) {
2481 nd->id = strdup(buf);
2482 }
ffe6370c
MT
2483 nd->nvectors = NIC_NVECTORS_UNSPECIFIED;
2484 if (get_param_value(buf, sizeof(buf), "vectors", p)) {
2485 char *endptr;
2486 long vectors = strtol(buf, &endptr, 0);
2487 if (*endptr) {
2488 config_error(mon, "invalid syntax for # of vectors\n");
2489 ret = -1;
2490 goto out;
2491 }
2492 if (vectors < 0 || vectors > 0x7ffffff) {
2493 config_error(mon, "invalid # of vectors\n");
2494 ret = -1;
2495 goto out;
2496 }
2497 nd->nvectors = vectors;
2498 }
63a01ef8 2499 nd->vlan = vlan;
7a9f6e4a 2500 nd->name = name;
7697079b 2501 nd->used = 1;
7a9f6e4a 2502 name = NULL;
63a01ef8
AL
2503 nb_nics++;
2504 vlan->nb_guest_devs++;
4d73cd3b 2505 ret = idx;
63a01ef8
AL
2506 } else
2507 if (!strcmp(device, "none")) {
8e4416af 2508 if (*p != '\0') {
10ae5a7a
JK
2509 config_error(mon, "'none' takes no parameters\n");
2510 ret = -1;
2511 goto out;
8e4416af 2512 }
63a01ef8
AL
2513 /* does nothing. It is needed to signal that no network cards
2514 are wanted */
2515 ret = 0;
2516 } else
2517#ifdef CONFIG_SLIRP
2518 if (!strcmp(device, "user")) {
8e4416af 2519 static const char * const slirp_params[] = {
c92ef6a2
JK
2520 "vlan", "name", "hostname", "restrict", "ip", "net", "host",
2521 "tftp", "bootfile", "dhcpstart", "dns", "smb", "smbserver",
2522 "hostfwd", "guestfwd", NULL
8e4416af 2523 };
ad196a9d 2524 struct slirp_config_str *config;
c92ef6a2
JK
2525 int restricted = 0;
2526 char *vnet = NULL;
2527 char *vhost = NULL;
2528 char *vhostname = NULL;
ad196a9d
JK
2529 char *tftp_export = NULL;
2530 char *bootfile = NULL;
c92ef6a2
JK
2531 char *vdhcp_start = NULL;
2532 char *vnamesrv = NULL;
ad196a9d 2533 char *smb_export = NULL;
c92ef6a2 2534 char *vsmbsrv = NULL;
ad196a9d 2535 const char *q;
b8e8af38 2536
0aa7a205 2537 if (check_params(buf, sizeof(buf), slirp_params, p) < 0) {
10ae5a7a
JK
2538 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
2539 ret = -1;
2540 goto out;
8e4416af 2541 }
c92ef6a2 2542 if (get_param_value(buf, sizeof(buf), "ip", p)) {
3da6abd4 2543 int vnet_buflen = strlen(buf) + strlen("/24") + 1;
c92ef6a2 2544 /* emulate legacy parameter */
3da6abd4
BS
2545 vnet = qemu_malloc(vnet_buflen);
2546 pstrcpy(vnet, vnet_buflen, buf);
2547 pstrcat(vnet, vnet_buflen, "/24");
c92ef6a2
JK
2548 }
2549 if (get_param_value(buf, sizeof(buf), "net", p)) {
2550 vnet = qemu_strdup(buf);
2551 }
2552 if (get_param_value(buf, sizeof(buf), "host", p)) {
2553 vhost = qemu_strdup(buf);
2554 }
63a01ef8 2555 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
c92ef6a2 2556 vhostname = qemu_strdup(buf);
63a01ef8 2557 }
49ec9b40 2558 if (get_param_value(buf, sizeof(buf), "restrict", p)) {
b8e8af38 2559 restricted = (buf[0] == 'y') ? 1 : 0;
49ec9b40 2560 }
c92ef6a2
JK
2561 if (get_param_value(buf, sizeof(buf), "dhcpstart", p)) {
2562 vdhcp_start = qemu_strdup(buf);
2563 }
2564 if (get_param_value(buf, sizeof(buf), "dns", p)) {
2565 vnamesrv = qemu_strdup(buf);
49ec9b40 2566 }
ad196a9d
JK
2567 if (get_param_value(buf, sizeof(buf), "tftp", p)) {
2568 tftp_export = qemu_strdup(buf);
2569 }
2570 if (get_param_value(buf, sizeof(buf), "bootfile", p)) {
2571 bootfile = qemu_strdup(buf);
2572 }
2573 if (get_param_value(buf, sizeof(buf), "smb", p)) {
2574 smb_export = qemu_strdup(buf);
c92ef6a2
JK
2575 if (get_param_value(buf, sizeof(buf), "smbserver", p)) {
2576 vsmbsrv = qemu_strdup(buf);
2577 }
ad196a9d
JK
2578 }
2579 q = p;
2580 while (1) {
2581 config = qemu_malloc(sizeof(*config));
2582 if (!get_next_param_value(config->str, sizeof(config->str),
c92ef6a2 2583 "hostfwd", &q)) {
ad196a9d
JK
2584 break;
2585 }
c92ef6a2 2586 config->flags = SLIRP_CFG_HOSTFWD;
ad196a9d
JK
2587 config->next = slirp_configs;
2588 slirp_configs = config;
2589 config = NULL;
2590 }
2591 q = p;
2592 while (1) {
2593 config = qemu_malloc(sizeof(*config));
2594 if (!get_next_param_value(config->str, sizeof(config->str),
c92ef6a2 2595 "guestfwd", &q)) {
ad196a9d
JK
2596 break;
2597 }
2598 config->flags = 0;
2599 config->next = slirp_configs;
2600 slirp_configs = config;
2601 config = NULL;
2602 }
2603 qemu_free(config);
63a01ef8 2604 vlan->nb_host_devs++;
c92ef6a2
JK
2605 ret = net_slirp_init(mon, vlan, device, name, restricted, vnet, vhost,
2606 vhostname, tftp_export, bootfile, vdhcp_start,
2607 vnamesrv, smb_export, vsmbsrv);
2608 qemu_free(vnet);
2609 qemu_free(vhost);
2610 qemu_free(vhostname);
ad196a9d
JK
2611 qemu_free(tftp_export);
2612 qemu_free(bootfile);
c92ef6a2
JK
2613 qemu_free(vdhcp_start);
2614 qemu_free(vnamesrv);
ad196a9d 2615 qemu_free(smb_export);
c92ef6a2 2616 qemu_free(vsmbsrv);
8ca9217d 2617 } else if (!strcmp(device, "channel")) {
b1c99fcd 2618 if (TAILQ_EMPTY(&slirp_stacks)) {
ad196a9d
JK
2619 struct slirp_config_str *config;
2620
2621 config = qemu_malloc(sizeof(*config));
2622 pstrcpy(config->str, sizeof(config->str), p);
c92ef6a2 2623 config->flags = SLIRP_CFG_LEGACY;
ad196a9d
JK
2624 config->next = slirp_configs;
2625 slirp_configs = config;
2626 } else {
b1c99fcd 2627 slirp_guestfwd(TAILQ_FIRST(&slirp_stacks), mon, p, 1);
8ca9217d 2628 }
8ca9217d 2629 ret = 0;
63a01ef8
AL
2630 } else
2631#endif
2632#ifdef _WIN32
2633 if (!strcmp(device, "tap")) {
8e4416af
AL
2634 static const char * const tap_params[] = {
2635 "vlan", "name", "ifname", NULL
2636 };
63a01ef8 2637 char ifname[64];
8e4416af 2638
0aa7a205 2639 if (check_params(buf, sizeof(buf), tap_params, p) < 0) {
10ae5a7a
JK
2640 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
2641 ret = -1;
2642 goto out;
8e4416af 2643 }
63a01ef8 2644 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
10ae5a7a 2645 config_error(mon, "tap: no interface name\n");
771f1339
AL
2646 ret = -1;
2647 goto out;
63a01ef8
AL
2648 }
2649 vlan->nb_host_devs++;
7a9f6e4a 2650 ret = tap_win32_init(vlan, device, name, ifname);
63a01ef8 2651 } else
b29fe3ed 2652#elif defined (_AIX)
63a01ef8
AL
2653#else
2654 if (!strcmp(device, "tap")) {
0aa7a205 2655 char ifname[64], chkbuf[64];
63a01ef8 2656 char setup_script[1024], down_script[1024];
4a77b25e 2657 TAPState *s;
63a01ef8
AL
2658 int fd;
2659 vlan->nb_host_devs++;
2660 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
0df0ff6d
MM
2661 static const char * const fd_params[] = {
2662 "vlan", "name", "fd", "sndbuf", NULL
2663 };
413fb241 2664 ret = -1;
0aa7a205 2665 if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
10ae5a7a 2666 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
10ae5a7a 2667 goto out;
8e4416af 2668 }
413fb241
MM
2669 fd = net_handle_fd_param(mon, buf);
2670 if (fd == -1) {
2671 goto out;
2672 }
63a01ef8 2673 fcntl(fd, F_SETFL, O_NONBLOCK);
4a77b25e 2674 s = net_tap_fd_init(vlan, device, name, fd);
413fb241
MM
2675 if (!s) {
2676 close(fd);
2677 }
63a01ef8 2678 } else {
8e4416af 2679 static const char * const tap_params[] = {
0df0ff6d 2680 "vlan", "name", "ifname", "script", "downscript", "sndbuf", NULL
8e4416af 2681 };
0aa7a205 2682 if (check_params(chkbuf, sizeof(chkbuf), tap_params, p) < 0) {
10ae5a7a
JK
2683 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2684 ret = -1;
2685 goto out;
8e4416af 2686 }
63a01ef8
AL
2687 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
2688 ifname[0] = '\0';
2689 }
2690 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
2691 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
2692 }
2693 if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
2694 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
2695 }
4a77b25e
MM
2696 s = net_tap_init(vlan, device, name, ifname, setup_script, down_script);
2697 }
2698 if (s != NULL) {
fc5b81d1 2699 const char *sndbuf_str = NULL;
0df0ff6d 2700 if (get_param_value(buf, sizeof(buf), "sndbuf", p)) {
fc5b81d1 2701 sndbuf_str = buf;
0df0ff6d 2702 }
fc5b81d1 2703 tap_set_sndbuf(s, sndbuf_str, mon);
4a77b25e
MM
2704 ret = 0;
2705 } else {
2706 ret = -1;
63a01ef8
AL
2707 }
2708 } else
2709#endif
2710 if (!strcmp(device, "socket")) {
0aa7a205 2711 char chkbuf[64];
63a01ef8 2712 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
0df0ff6d
MM
2713 static const char * const fd_params[] = {
2714 "vlan", "name", "fd", NULL
2715 };
63a01ef8 2716 int fd;
413fb241 2717 ret = -1;
0aa7a205 2718 if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
10ae5a7a 2719 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
10ae5a7a 2720 goto out;
8e4416af 2721 }
413fb241
MM
2722 fd = net_handle_fd_param(mon, buf);
2723 if (fd == -1) {
2724 goto out;
2725 }
2726 if (!net_socket_fd_init(vlan, device, name, fd, 1)) {
2727 close(fd);
2728 goto out;
2729 }
2730 ret = 0;
63a01ef8 2731 } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
8e4416af
AL
2732 static const char * const listen_params[] = {
2733 "vlan", "name", "listen", NULL
2734 };
0aa7a205 2735 if (check_params(chkbuf, sizeof(chkbuf), listen_params, p) < 0) {
10ae5a7a
JK
2736 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2737 ret = -1;
2738 goto out;
8e4416af 2739 }
7a9f6e4a 2740 ret = net_socket_listen_init(vlan, device, name, buf);
63a01ef8 2741 } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
8e4416af
AL
2742 static const char * const connect_params[] = {
2743 "vlan", "name", "connect", NULL
2744 };
0aa7a205 2745 if (check_params(chkbuf, sizeof(chkbuf), connect_params, p) < 0) {
10ae5a7a
JK
2746 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2747 ret = -1;
2748 goto out;
8e4416af 2749 }
7a9f6e4a 2750 ret = net_socket_connect_init(vlan, device, name, buf);
63a01ef8 2751 } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
8e4416af
AL
2752 static const char * const mcast_params[] = {
2753 "vlan", "name", "mcast", NULL
2754 };
0aa7a205 2755 if (check_params(chkbuf, sizeof(chkbuf), mcast_params, p) < 0) {
10ae5a7a
JK
2756 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2757 ret = -1;
2758 goto out;
8e4416af 2759 }
7a9f6e4a 2760 ret = net_socket_mcast_init(vlan, device, name, buf);
63a01ef8 2761 } else {
10ae5a7a 2762 config_error(mon, "Unknown socket options: %s\n", p);
771f1339
AL
2763 ret = -1;
2764 goto out;
63a01ef8
AL
2765 }
2766 vlan->nb_host_devs++;
2767 } else
2768#ifdef CONFIG_VDE
2769 if (!strcmp(device, "vde")) {
8e4416af
AL
2770 static const char * const vde_params[] = {
2771 "vlan", "name", "sock", "port", "group", "mode", NULL
2772 };
63a01ef8
AL
2773 char vde_sock[1024], vde_group[512];
2774 int vde_port, vde_mode;
8e4416af 2775
0aa7a205 2776 if (check_params(buf, sizeof(buf), vde_params, p) < 0) {
10ae5a7a
JK
2777 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
2778 ret = -1;
2779 goto out;
8e4416af 2780 }
63a01ef8
AL
2781 vlan->nb_host_devs++;
2782 if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
2783 vde_sock[0] = '\0';
2784 }
2785 if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
2786 vde_port = strtol(buf, NULL, 10);
2787 } else {
2788 vde_port = 0;
2789 }
2790 if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
2791 vde_group[0] = '\0';
2792 }
2793 if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
2794 vde_mode = strtol(buf, NULL, 8);
2795 } else {
2796 vde_mode = 0700;
2797 }
7a9f6e4a 2798 ret = net_vde_init(vlan, device, name, vde_sock, vde_port, vde_group, vde_mode);
63a01ef8
AL
2799 } else
2800#endif
bb9ea79e
AL
2801 if (!strcmp(device, "dump")) {
2802 int len = 65536;
2803
2804 if (get_param_value(buf, sizeof(buf), "len", p) > 0) {
2805 len = strtol(buf, NULL, 0);
2806 }
2807 if (!get_param_value(buf, sizeof(buf), "file", p)) {
2808 snprintf(buf, sizeof(buf), "qemu-vlan%d.pcap", vlan_id);
2809 }
10ae5a7a 2810 ret = net_dump_init(mon, vlan, device, name, buf, len);
bb9ea79e 2811 } else {
10ae5a7a 2812 config_error(mon, "Unknown network device: %s\n", device);
771f1339
AL
2813 ret = -1;
2814 goto out;
63a01ef8
AL
2815 }
2816 if (ret < 0) {
10ae5a7a 2817 config_error(mon, "Could not initialize device '%s'\n", device);
63a01ef8 2818 }
771f1339 2819out:
10ae5a7a 2820 qemu_free(name);
63a01ef8
AL
2821 return ret;
2822}
2823
8b13c4a7
AL
2824void net_client_uninit(NICInfo *nd)
2825{
2826 nd->vlan->nb_guest_devs--;
2827 nb_nics--;
2828 nd->used = 0;
2829 free((void *)nd->model);
2830}
2831
6f338c34
AL
2832static int net_host_check_device(const char *device)
2833{
2834 int i;
bb9ea79e 2835 const char *valid_param_list[] = { "tap", "socket", "dump"
6f338c34
AL
2836#ifdef CONFIG_SLIRP
2837 ,"user"
2838#endif
2839#ifdef CONFIG_VDE
2840 ,"vde"
2841#endif
2842 };
2843 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
2844 if (!strncmp(valid_param_list[i], device,
2845 strlen(valid_param_list[i])))
2846 return 1;
2847 }
2848
2849 return 0;
2850}
2851
376253ec 2852void net_host_device_add(Monitor *mon, const char *device, const char *opts)
6f338c34
AL
2853{
2854 if (!net_host_check_device(device)) {
376253ec 2855 monitor_printf(mon, "invalid host network device %s\n", device);
6f338c34
AL
2856 return;
2857 }
10ae5a7a 2858 if (net_client_init(mon, device, opts ? opts : "") < 0) {
5c8be678
AL
2859 monitor_printf(mon, "adding host network device %s failed\n", device);
2860 }
6f338c34
AL
2861}
2862
376253ec 2863void net_host_device_remove(Monitor *mon, int vlan_id, const char *device)
6f338c34 2864{
6f338c34
AL
2865 VLANClientState *vc;
2866
1a609520 2867 vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
6f338c34 2868 if (!vc) {
6f338c34
AL
2869 return;
2870 }
e8f1f9db
AL
2871 if (!net_host_check_device(vc->model)) {
2872 monitor_printf(mon, "invalid host network device %s\n", device);
2873 return;
2874 }
6f338c34
AL
2875 qemu_del_vlan_client(vc);
2876}
2877
63a01ef8
AL
2878int net_client_parse(const char *str)
2879{
2880 const char *p;
2881 char *q;
2882 char device[64];
2883
2884 p = str;
2885 q = device;
2886 while (*p != '\0' && *p != ',') {
2887 if ((q - device) < sizeof(device) - 1)
2888 *q++ = *p;
2889 p++;
2890 }
2891 *q = '\0';
2892 if (*p == ',')
2893 p++;
2894
10ae5a7a 2895 return net_client_init(NULL, device, p);
63a01ef8
AL
2896}
2897
406c8df3
GC
2898void net_set_boot_mask(int net_boot_mask)
2899{
2900 int i;
2901
2902 /* Only the first four NICs may be bootable */
2903 net_boot_mask = net_boot_mask & 0xF;
2904
2905 for (i = 0; i < nb_nics; i++) {
2906 if (net_boot_mask & (1 << i)) {
2907 nd_table[i].bootable = 1;
2908 net_boot_mask &= ~(1 << i);
2909 }
2910 }
2911
2912 if (net_boot_mask) {
2913 fprintf(stderr, "Cannot boot from non-existent NIC\n");
2914 exit(1);
2915 }
2916}
2917
376253ec 2918void do_info_network(Monitor *mon)
63a01ef8
AL
2919{
2920 VLANState *vlan;
2921 VLANClientState *vc;
2922
2923 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
376253ec 2924 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
63a01ef8 2925 for(vc = vlan->first_client; vc != NULL; vc = vc->next)
376253ec 2926 monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
63a01ef8
AL
2927 }
2928}
2929
b8630e45 2930void do_set_link(Monitor *mon, const char *name, const char *up_or_down)
436e5e53
AL
2931{
2932 VLANState *vlan;
2933 VLANClientState *vc = NULL;
2934
2935 for (vlan = first_vlan; vlan != NULL; vlan = vlan->next)
2936 for (vc = vlan->first_client; vc != NULL; vc = vc->next)
2937 if (strcmp(vc->name, name) == 0)
dd5de373
EI
2938 goto done;
2939done:
436e5e53
AL
2940
2941 if (!vc) {
95c6409e 2942 monitor_printf(mon, "could not find network device '%s'\n", name);
b8630e45 2943 return;
436e5e53
AL
2944 }
2945
2946 if (strcmp(up_or_down, "up") == 0)
2947 vc->link_down = 0;
2948 else if (strcmp(up_or_down, "down") == 0)
2949 vc->link_down = 1;
2950 else
376253ec
AL
2951 monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' "
2952 "valid\n", up_or_down);
436e5e53 2953
34b25ca7
AL
2954 if (vc->link_status_changed)
2955 vc->link_status_changed(vc);
436e5e53
AL
2956}
2957
63a01ef8
AL
2958void net_cleanup(void)
2959{
2960 VLANState *vlan;
2961
63a01ef8
AL
2962 /* close network clients */
2963 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
b946a153 2964 VLANClientState *vc = vlan->first_client;
63a01ef8 2965
b946a153
AL
2966 while (vc) {
2967 VLANClientState *next = vc->next;
63a01ef8 2968
b946a153
AL
2969 qemu_del_vlan_client(vc);
2970
2971 vc = next;
63a01ef8
AL
2972 }
2973 }
63a01ef8
AL
2974}
2975
2976void net_client_check(void)
2977{
2978 VLANState *vlan;
2979
2980 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2981 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
2982 continue;
2983 if (vlan->nb_guest_devs == 0)
2984 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
2985 if (vlan->nb_host_devs == 0)
2986 fprintf(stderr,
2987 "Warning: vlan %d is not connected to host network\n",
2988 vlan->id);
2989 }
2990}