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