Statistics
| Branch: | Revision:

root / net.c @ 8e4416af

History | View | Annotate | Download (54.1 kB)

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
 */
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

    
32
/* Needed early for HOST_BSD etc. */
33
#include "config-host.h"
34

    
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>
41
#include <sys/resource.h>
42
#include <sys/socket.h>
43
#include <netinet/in.h>
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>
52
#include <dirent.h>
53
#include <netdb.h>
54
#include <sys/select.h>
55
#ifdef HOST_BSD
56
#include <sys/stat.h>
57
#if defined(__FreeBSD__) || defined(__DragonFly__)
58
#include <libutil.h>
59
#else
60
#include <util.h>
61
#endif
62
#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
63
#include <freebsd/stdlib.h>
64
#else
65
#ifdef __linux__
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

    
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
105
#include <windows.h>
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

    
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"
121

    
122
#if defined(CONFIG_SLIRP)
123
#include "libslirp.h"
124
#endif
125

    
126

    
127
static VLANState *first_vlan;
128

    
129
/***********************************************************/
130
/* network device redirectors */
131

    
132
#if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
133
static void hex_dump(FILE *f, const uint8_t *buf, int size)
134
{
135
    int len, i, j, c;
136

    
137
    for(i=0;i<size;i+=16) {
138
        len = size - i;
139
        if (len > 16)
140
            len = 16;
141
        fprintf(f, "%08x ", i);
142
        for(j=0;j<16;j++) {
143
            if (j < len)
144
                fprintf(f, " %02x", buf[i+j]);
145
            else
146
                fprintf(f, "   ");
147
        }
148
        fprintf(f, " ");
149
        for(j=0;j<len;j++) {
150
            c = buf[i+j];
151
            if (c < ' ' || c > '~')
152
                c = '.';
153
            fprintf(f, "%c", c);
154
        }
155
        fprintf(f, "\n");
156
    }
157
}
158
#endif
159

    
160
static int parse_macaddr(uint8_t *macaddr, const char *p)
161
{
162
    int i;
163
    char *last_char;
164
    long int offset;
165

    
166
    errno = 0;
167
    offset = strtol(p, &last_char, 0);    
168
    if (0 == errno && '\0' == *last_char &&
169
            offset >= 0 && offset <= 0xFFFFFF) {
170
        macaddr[3] = (offset & 0xFF0000) >> 16;
171
        macaddr[4] = (offset & 0xFF00) >> 8;
172
        macaddr[5] = offset & 0xFF;
173
        return 0;
174
    } else {
175
        for(i = 0; i < 6; i++) {
176
            macaddr[i] = strtol(p, (char **)&p, 16);
177
            if (i == 5) {
178
                if (*p != '\0')
179
                    return -1;
180
            } else {
181
                if (*p != ':' && *p != '-')
182
                    return -1;
183
                p++;
184
            }
185
        }
186
        return 0;    
187
    }
188

    
189
    return -1;
190
}
191

    
192
static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
193
{
194
    const char *p, *p1;
195
    int len;
196
    p = *pp;
197
    p1 = strchr(p, sep);
198
    if (!p1)
199
        return -1;
200
    len = p1 - p;
201
    p1++;
202
    if (buf_size > 0) {
203
        if (len > buf_size - 1)
204
            len = buf_size - 1;
205
        memcpy(buf, p, len);
206
        buf[len] = '\0';
207
    }
208
    *pp = p1;
209
    return 0;
210
}
211

    
212
int parse_host_src_port(struct sockaddr_in *haddr,
213
                        struct sockaddr_in *saddr,
214
                        const char *input_str)
215
{
216
    char *str = strdup(input_str);
217
    char *host_str = str;
218
    char *src_str;
219
    const char *src_str2;
220
    char *ptr;
221

    
222
    /*
223
     * Chop off any extra arguments at the end of the string which
224
     * would start with a comma, then fill in the src port information
225
     * if it was provided else use the "any address" and "any port".
226
     */
227
    if ((ptr = strchr(str,',')))
228
        *ptr = '\0';
229

    
230
    if ((src_str = strchr(input_str,'@'))) {
231
        *src_str = '\0';
232
        src_str++;
233
    }
234

    
235
    if (parse_host_port(haddr, host_str) < 0)
236
        goto fail;
237

    
238
    src_str2 = src_str;
239
    if (!src_str || *src_str == '\0')
240
        src_str2 = ":0";
241

    
242
    if (parse_host_port(saddr, src_str2) < 0)
243
        goto fail;
244

    
245
    free(str);
246
    return(0);
247

    
248
fail:
249
    free(str);
250
    return -1;
251
}
252

    
253
int parse_host_port(struct sockaddr_in *saddr, const char *str)
254
{
255
    char buf[512];
256
    struct hostent *he;
257
    const char *p, *r;
258
    int port;
259

    
260
    p = str;
261
    if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
262
        return -1;
263
    saddr->sin_family = AF_INET;
264
    if (buf[0] == '\0') {
265
        saddr->sin_addr.s_addr = 0;
266
    } else {
267
        if (qemu_isdigit(buf[0])) {
268
            if (!inet_aton(buf, &saddr->sin_addr))
269
                return -1;
270
        } else {
271
            if ((he = gethostbyname(buf)) == NULL)
272
                return - 1;
273
            saddr->sin_addr = *(struct in_addr *)he->h_addr;
274
        }
275
    }
276
    port = strtol(p, (char **)&r, 0);
277
    if (r == p)
278
        return -1;
279
    saddr->sin_port = htons(port);
280
    return 0;
281
}
282

    
283
#if !defined(_WIN32) && 0
284
static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
285
{
286
    const char *p;
287
    int len;
288

    
289
    len = MIN(108, strlen(str));
290
    p = strchr(str, ',');
291
    if (p)
292
        len = MIN(len, p - str);
293

    
294
    memset(uaddr, 0, sizeof(*uaddr));
295

    
296
    uaddr->sun_family = AF_UNIX;
297
    memcpy(uaddr->sun_path, str, len);
298

    
299
    return 0;
300
}
301
#endif
302

    
303
void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
304
{
305
    snprintf(vc->info_str, sizeof(vc->info_str),
306
             "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
307
             vc->model,
308
             macaddr[0], macaddr[1], macaddr[2],
309
             macaddr[3], macaddr[4], macaddr[5]);
310
}
311

    
312
static char *assign_name(VLANClientState *vc1, const char *model)
313
{
314
    VLANState *vlan;
315
    char buf[256];
316
    int id = 0;
317

    
318
    for (vlan = first_vlan; vlan; vlan = vlan->next) {
319
        VLANClientState *vc;
320

    
321
        for (vc = vlan->first_client; vc; vc = vc->next)
322
            if (vc != vc1 && strcmp(vc->model, model) == 0)
323
                id++;
324
    }
325

    
326
    snprintf(buf, sizeof(buf), "%s.%d", model, id);
327

    
328
    return strdup(buf);
329
}
330

    
331
VLANClientState *qemu_new_vlan_client(VLANState *vlan,
332
                                      const char *model,
333
                                      const char *name,
334
                                      IOReadHandler *fd_read,
335
                                      IOCanRWHandler *fd_can_read,
336
                                      NetCleanup *cleanup,
337
                                      void *opaque)
338
{
339
    VLANClientState *vc, **pvc;
340
    vc = qemu_mallocz(sizeof(VLANClientState));
341
    vc->model = strdup(model);
342
    if (name)
343
        vc->name = strdup(name);
344
    else
345
        vc->name = assign_name(vc, model);
346
    vc->fd_read = fd_read;
347
    vc->fd_can_read = fd_can_read;
348
    vc->cleanup = cleanup;
349
    vc->opaque = opaque;
350
    vc->vlan = vlan;
351

    
352
    vc->next = NULL;
353
    pvc = &vlan->first_client;
354
    while (*pvc != NULL)
355
        pvc = &(*pvc)->next;
356
    *pvc = vc;
357
    return vc;
358
}
359

    
360
void qemu_del_vlan_client(VLANClientState *vc)
361
{
362
    VLANClientState **pvc = &vc->vlan->first_client;
363

    
364
    while (*pvc != NULL)
365
        if (*pvc == vc) {
366
            *pvc = vc->next;
367
            if (vc->cleanup) {
368
                vc->cleanup(vc);
369
            }
370
            free(vc->name);
371
            free(vc->model);
372
            qemu_free(vc);
373
            break;
374
        } else
375
            pvc = &(*pvc)->next;
376
}
377

    
378
VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque)
379
{
380
    VLANClientState **pvc = &vlan->first_client;
381

    
382
    while (*pvc != NULL)
383
        if ((*pvc)->opaque == opaque)
384
            return *pvc;
385
        else
386
            pvc = &(*pvc)->next;
387

    
388
    return NULL;
389
}
390

    
391
int qemu_can_send_packet(VLANClientState *vc1)
392
{
393
    VLANState *vlan = vc1->vlan;
394
    VLANClientState *vc;
395

    
396
    for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
397
        if (vc != vc1) {
398
            if (vc->fd_can_read && vc->fd_can_read(vc->opaque))
399
                return 1;
400
        }
401
    }
402
    return 0;
403
}
404

    
405
void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size)
406
{
407
    VLANState *vlan = vc1->vlan;
408
    VLANClientState *vc;
409

    
410
    if (vc1->link_down)
411
        return;
412

    
413
#ifdef DEBUG_NET
414
    printf("vlan %d send:\n", vlan->id);
415
    hex_dump(stdout, buf, size);
416
#endif
417
    for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
418
        if (vc != vc1 && !vc->link_down) {
419
            vc->fd_read(vc->opaque, buf, size);
420
        }
421
    }
422
}
423

    
424
static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
425
                               int iovcnt)
426
{
427
    uint8_t buffer[4096];
428
    size_t offset = 0;
429
    int i;
430

    
431
    for (i = 0; i < iovcnt; i++) {
432
        size_t len;
433

    
434
        len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
435
        memcpy(buffer + offset, iov[i].iov_base, len);
436
        offset += len;
437
    }
438

    
439
    vc->fd_read(vc->opaque, buffer, offset);
440

    
441
    return offset;
442
}
443

    
444
static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
445
{
446
    size_t offset = 0;
447
    int i;
448

    
449
    for (i = 0; i < iovcnt; i++)
450
        offset += iov[i].iov_len;
451
    return offset;
452
}
453

    
454
ssize_t qemu_sendv_packet(VLANClientState *vc1, const struct iovec *iov,
455
                          int iovcnt)
456
{
457
    VLANState *vlan = vc1->vlan;
458
    VLANClientState *vc;
459
    ssize_t max_len = 0;
460

    
461
    if (vc1->link_down)
462
        return calc_iov_length(iov, iovcnt);
463

    
464
    for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
465
        ssize_t len = 0;
466

    
467
        if (vc == vc1)
468
            continue;
469

    
470
        if (vc->link_down)
471
            len = calc_iov_length(iov, iovcnt);
472
        if (vc->fd_readv)
473
            len = vc->fd_readv(vc->opaque, iov, iovcnt);
474
        else if (vc->fd_read)
475
            len = vc_sendv_compat(vc, iov, iovcnt);
476

    
477
        max_len = MAX(max_len, len);
478
    }
479

    
480
    return max_len;
481
}
482

    
483
#if defined(CONFIG_SLIRP)
484

    
485
/* slirp network adapter */
486

    
487
static int slirp_inited;
488
static int slirp_restrict;
489
static char *slirp_ip;
490
static VLANClientState *slirp_vc;
491

    
492
int slirp_can_output(void)
493
{
494
    return !slirp_vc || qemu_can_send_packet(slirp_vc);
495
}
496

    
497
void slirp_output(const uint8_t *pkt, int pkt_len)
498
{
499
#ifdef DEBUG_SLIRP
500
    printf("slirp output:\n");
501
    hex_dump(stdout, pkt, pkt_len);
502
#endif
503
    if (!slirp_vc)
504
        return;
505
    qemu_send_packet(slirp_vc, pkt, pkt_len);
506
}
507

    
508
int slirp_is_inited(void)
509
{
510
    return slirp_inited;
511
}
512

    
513
static void slirp_receive(void *opaque, const uint8_t *buf, int size)
514
{
515
#ifdef DEBUG_SLIRP
516
    printf("slirp input:\n");
517
    hex_dump(stdout, buf, size);
518
#endif
519
    slirp_input(buf, size);
520
}
521

    
522
static int net_slirp_init(VLANState *vlan, const char *model, const char *name)
523
{
524
    if (!slirp_inited) {
525
        slirp_inited = 1;
526
        slirp_init(slirp_restrict, slirp_ip);
527
    }
528
    slirp_vc = qemu_new_vlan_client(vlan, model, name,
529
                                    slirp_receive, NULL, NULL, NULL);
530
    slirp_vc->info_str[0] = '\0';
531
    return 0;
532
}
533

    
534
void net_slirp_redir(const char *redir_str)
535
{
536
    int is_udp;
537
    char buf[256], *r;
538
    const char *p;
539
    struct in_addr guest_addr;
540
    int host_port, guest_port;
541

    
542
    if (!slirp_inited) {
543
        slirp_inited = 1;
544
        slirp_init(slirp_restrict, slirp_ip);
545
    }
546

    
547
    p = redir_str;
548
    if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
549
        goto fail;
550
    if (!strcmp(buf, "tcp")) {
551
        is_udp = 0;
552
    } else if (!strcmp(buf, "udp")) {
553
        is_udp = 1;
554
    } else {
555
        goto fail;
556
    }
557

    
558
    if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
559
        goto fail;
560
    host_port = strtol(buf, &r, 0);
561
    if (r == buf)
562
        goto fail;
563

    
564
    if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
565
        goto fail;
566
    if (buf[0] == '\0') {
567
        pstrcpy(buf, sizeof(buf), "10.0.2.15");
568
    }
569
    if (!inet_aton(buf, &guest_addr))
570
        goto fail;
571

    
572
    guest_port = strtol(p, &r, 0);
573
    if (r == p)
574
        goto fail;
575

    
576
    if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
577
        fprintf(stderr, "qemu: could not set up redirection\n");
578
        exit(1);
579
    }
580
    return;
581
 fail:
582
    fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n");
583
    exit(1);
584
}
585

    
586
#ifndef _WIN32
587

    
588
static char smb_dir[1024];
589

    
590
static void erase_dir(char *dir_name)
591
{
592
    DIR *d;
593
    struct dirent *de;
594
    char filename[1024];
595

    
596
    /* erase all the files in the directory */
597
    if ((d = opendir(dir_name)) != NULL) {
598
        for(;;) {
599
            de = readdir(d);
600
            if (!de)
601
                break;
602
            if (strcmp(de->d_name, ".") != 0 &&
603
                strcmp(de->d_name, "..") != 0) {
604
                snprintf(filename, sizeof(filename), "%s/%s",
605
                         smb_dir, de->d_name);
606
                if (unlink(filename) != 0)  /* is it a directory? */
607
                    erase_dir(filename);
608
            }
609
        }
610
        closedir(d);
611
        rmdir(dir_name);
612
    }
613
}
614

    
615
/* automatic user mode samba server configuration */
616
static void smb_exit(void)
617
{
618
    erase_dir(smb_dir);
619
}
620

    
621
/* automatic user mode samba server configuration */
622
void net_slirp_smb(const char *exported_dir)
623
{
624
    char smb_conf[1024];
625
    char smb_cmdline[1024];
626
    FILE *f;
627

    
628
    if (!slirp_inited) {
629
        slirp_inited = 1;
630
        slirp_init(slirp_restrict, slirp_ip);
631
    }
632

    
633
    /* XXX: better tmp dir construction */
634
    snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%ld", (long)getpid());
635
    if (mkdir(smb_dir, 0700) < 0) {
636
        fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
637
        exit(1);
638
    }
639
    snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
640

    
641
    f = fopen(smb_conf, "w");
642
    if (!f) {
643
        fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
644
        exit(1);
645
    }
646
    fprintf(f,
647
            "[global]\n"
648
            "private dir=%s\n"
649
            "smb ports=0\n"
650
            "socket address=127.0.0.1\n"
651
            "pid directory=%s\n"
652
            "lock directory=%s\n"
653
            "log file=%s/log.smbd\n"
654
            "smb passwd file=%s/smbpasswd\n"
655
            "security = share\n"
656
            "[qemu]\n"
657
            "path=%s\n"
658
            "read only=no\n"
659
            "guest ok=yes\n",
660
            smb_dir,
661
            smb_dir,
662
            smb_dir,
663
            smb_dir,
664
            smb_dir,
665
            exported_dir
666
            );
667
    fclose(f);
668
    atexit(smb_exit);
669

    
670
    snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
671
             SMBD_COMMAND, smb_conf);
672

    
673
    slirp_add_exec(0, smb_cmdline, 4, 139);
674
}
675

    
676
#endif /* !defined(_WIN32) */
677
void do_info_slirp(Monitor *mon)
678
{
679
    slirp_stats();
680
}
681

    
682
struct VMChannel {
683
    CharDriverState *hd;
684
    int port;
685
};
686

    
687
static int vmchannel_can_read(void *opaque)
688
{
689
    struct VMChannel *vmc = (struct VMChannel*)opaque;
690
    return slirp_socket_can_recv(4, vmc->port);
691
}
692

    
693
static void vmchannel_read(void *opaque, const uint8_t *buf, int size)
694
{
695
    struct VMChannel *vmc = (struct VMChannel*)opaque;
696
    slirp_socket_recv(4, vmc->port, buf, size);
697
}
698

    
699
#endif /* CONFIG_SLIRP */
700

    
701
#if !defined(_WIN32)
702

    
703
typedef struct TAPState {
704
    VLANClientState *vc;
705
    int fd;
706
    char down_script[1024];
707
    char down_script_arg[128];
708
} TAPState;
709

    
710
static int launch_script(const char *setup_script, const char *ifname, int fd);
711

    
712
static ssize_t tap_receive_iov(void *opaque, const struct iovec *iov,
713
                               int iovcnt)
714
{
715
    TAPState *s = opaque;
716
    ssize_t len;
717

    
718
    do {
719
        len = writev(s->fd, iov, iovcnt);
720
    } while (len == -1 && (errno == EINTR || errno == EAGAIN));
721

    
722
    return len;
723
}
724

    
725
static void tap_receive(void *opaque, const uint8_t *buf, int size)
726
{
727
    TAPState *s = opaque;
728
    int ret;
729
    for(;;) {
730
        ret = write(s->fd, buf, size);
731
        if (ret < 0 && (errno == EINTR || errno == EAGAIN)) {
732
        } else {
733
            break;
734
        }
735
    }
736
}
737

    
738
static void tap_send(void *opaque)
739
{
740
    TAPState *s = opaque;
741
    uint8_t buf[4096];
742
    int size;
743

    
744
#ifdef __sun__
745
    struct strbuf sbuf;
746
    int f = 0;
747
    sbuf.maxlen = sizeof(buf);
748
    sbuf.buf = (char *)buf;
749
    size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1;
750
#else
751
    size = read(s->fd, buf, sizeof(buf));
752
#endif
753
    if (size > 0) {
754
        qemu_send_packet(s->vc, buf, size);
755
    }
756
}
757

    
758
static void tap_cleanup(VLANClientState *vc)
759
{
760
    TAPState *s = vc->opaque;
761

    
762
    if (s->down_script[0])
763
        launch_script(s->down_script, s->down_script_arg, s->fd);
764

    
765
    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
766
    close(s->fd);
767
    qemu_free(s);
768
}
769

    
770
/* fd support */
771

    
772
static TAPState *net_tap_fd_init(VLANState *vlan,
773
                                 const char *model,
774
                                 const char *name,
775
                                 int fd)
776
{
777
    TAPState *s;
778

    
779
    s = qemu_mallocz(sizeof(TAPState));
780
    s->fd = fd;
781
    s->vc = qemu_new_vlan_client(vlan, model, name, tap_receive,
782
                                 NULL, tap_cleanup, s);
783
    s->vc->fd_readv = tap_receive_iov;
784
    qemu_set_fd_handler(s->fd, tap_send, NULL, s);
785
    snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd);
786
    return s;
787
}
788

    
789
#if defined (HOST_BSD) || defined (__FreeBSD_kernel__)
790
static int tap_open(char *ifname, int ifname_size)
791
{
792
    int fd;
793
    char *dev;
794
    struct stat s;
795

    
796
    TFR(fd = open("/dev/tap", O_RDWR));
797
    if (fd < 0) {
798
        fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
799
        return -1;
800
    }
801

    
802
    fstat(fd, &s);
803
    dev = devname(s.st_rdev, S_IFCHR);
804
    pstrcpy(ifname, ifname_size, dev);
805

    
806
    fcntl(fd, F_SETFL, O_NONBLOCK);
807
    return fd;
808
}
809
#elif defined(__sun__)
810
#define TUNNEWPPA       (('T'<<16) | 0x0001)
811
/*
812
 * Allocate TAP device, returns opened fd.
813
 * Stores dev name in the first arg(must be large enough).
814
 */
815
static int tap_alloc(char *dev, size_t dev_size)
816
{
817
    int tap_fd, if_fd, ppa = -1;
818
    static int ip_fd = 0;
819
    char *ptr;
820

    
821
    static int arp_fd = 0;
822
    int ip_muxid, arp_muxid;
823
    struct strioctl  strioc_if, strioc_ppa;
824
    int link_type = I_PLINK;;
825
    struct lifreq ifr;
826
    char actual_name[32] = "";
827

    
828
    memset(&ifr, 0x0, sizeof(ifr));
829

    
830
    if( *dev ){
831
       ptr = dev;
832
       while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
833
       ppa = atoi(ptr);
834
    }
835

    
836
    /* Check if IP device was opened */
837
    if( ip_fd )
838
       close(ip_fd);
839

    
840
    TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
841
    if (ip_fd < 0) {
842
       syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
843
       return -1;
844
    }
845

    
846
    TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
847
    if (tap_fd < 0) {
848
       syslog(LOG_ERR, "Can't open /dev/tap");
849
       return -1;
850
    }
851

    
852
    /* Assign a new PPA and get its unit number. */
853
    strioc_ppa.ic_cmd = TUNNEWPPA;
854
    strioc_ppa.ic_timout = 0;
855
    strioc_ppa.ic_len = sizeof(ppa);
856
    strioc_ppa.ic_dp = (char *)&ppa;
857
    if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
858
       syslog (LOG_ERR, "Can't assign new interface");
859

    
860
    TFR(if_fd = open("/dev/tap", O_RDWR, 0));
861
    if (if_fd < 0) {
862
       syslog(LOG_ERR, "Can't open /dev/tap (2)");
863
       return -1;
864
    }
865
    if(ioctl(if_fd, I_PUSH, "ip") < 0){
866
       syslog(LOG_ERR, "Can't push IP module");
867
       return -1;
868
    }
869

    
870
    if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
871
        syslog(LOG_ERR, "Can't get flags\n");
872

    
873
    snprintf (actual_name, 32, "tap%d", ppa);
874
    pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
875

    
876
    ifr.lifr_ppa = ppa;
877
    /* Assign ppa according to the unit number returned by tun device */
878

    
879
    if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
880
        syslog (LOG_ERR, "Can't set PPA %d", ppa);
881
    if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
882
        syslog (LOG_ERR, "Can't get flags\n");
883
    /* Push arp module to if_fd */
884
    if (ioctl (if_fd, I_PUSH, "arp") < 0)
885
        syslog (LOG_ERR, "Can't push ARP module (2)");
886

    
887
    /* Push arp module to ip_fd */
888
    if (ioctl (ip_fd, I_POP, NULL) < 0)
889
        syslog (LOG_ERR, "I_POP failed\n");
890
    if (ioctl (ip_fd, I_PUSH, "arp") < 0)
891
        syslog (LOG_ERR, "Can't push ARP module (3)\n");
892
    /* Open arp_fd */
893
    TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
894
    if (arp_fd < 0)
895
       syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
896

    
897
    /* Set ifname to arp */
898
    strioc_if.ic_cmd = SIOCSLIFNAME;
899
    strioc_if.ic_timout = 0;
900
    strioc_if.ic_len = sizeof(ifr);
901
    strioc_if.ic_dp = (char *)&ifr;
902
    if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
903
        syslog (LOG_ERR, "Can't set ifname to arp\n");
904
    }
905

    
906
    if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
907
       syslog(LOG_ERR, "Can't link TAP device to IP");
908
       return -1;
909
    }
910

    
911
    if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
912
        syslog (LOG_ERR, "Can't link TAP device to ARP");
913

    
914
    close (if_fd);
915

    
916
    memset(&ifr, 0x0, sizeof(ifr));
917
    pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
918
    ifr.lifr_ip_muxid  = ip_muxid;
919
    ifr.lifr_arp_muxid = arp_muxid;
920

    
921
    if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
922
    {
923
      ioctl (ip_fd, I_PUNLINK , arp_muxid);
924
      ioctl (ip_fd, I_PUNLINK, ip_muxid);
925
      syslog (LOG_ERR, "Can't set multiplexor id");
926
    }
927

    
928
    snprintf(dev, dev_size, "tap%d", ppa);
929
    return tap_fd;
930
}
931

    
932
static int tap_open(char *ifname, int ifname_size)
933
{
934
    char  dev[10]="";
935
    int fd;
936
    if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
937
       fprintf(stderr, "Cannot allocate TAP device\n");
938
       return -1;
939
    }
940
    pstrcpy(ifname, ifname_size, dev);
941
    fcntl(fd, F_SETFL, O_NONBLOCK);
942
    return fd;
943
}
944
#elif defined (_AIX)
945
static int tap_open(char *ifname, int ifname_size)
946
{
947
    fprintf (stderr, "no tap on AIX\n");
948
    return -1;
949
}
950
#else
951
static int tap_open(char *ifname, int ifname_size)
952
{
953
    struct ifreq ifr;
954
    int fd, ret;
955

    
956
    TFR(fd = open("/dev/net/tun", O_RDWR));
957
    if (fd < 0) {
958
        fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
959
        return -1;
960
    }
961
    memset(&ifr, 0, sizeof(ifr));
962
    ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
963
    if (ifname[0] != '\0')
964
        pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
965
    else
966
        pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
967
    ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
968
    if (ret != 0) {
969
        fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
970
        close(fd);
971
        return -1;
972
    }
973
    pstrcpy(ifname, ifname_size, ifr.ifr_name);
974
    fcntl(fd, F_SETFL, O_NONBLOCK);
975
    return fd;
976
}
977
#endif
978

    
979
static int launch_script(const char *setup_script, const char *ifname, int fd)
980
{
981
    int pid, status;
982
    char *args[3];
983
    char **parg;
984

    
985
        /* try to launch network script */
986
        pid = fork();
987
        if (pid >= 0) {
988
            if (pid == 0) {
989
                int open_max = sysconf (_SC_OPEN_MAX), i;
990
                for (i = 0; i < open_max; i++)
991
                    if (i != STDIN_FILENO &&
992
                        i != STDOUT_FILENO &&
993
                        i != STDERR_FILENO &&
994
                        i != fd)
995
                        close(i);
996

    
997
                parg = args;
998
                *parg++ = (char *)setup_script;
999
                *parg++ = (char *)ifname;
1000
                *parg++ = NULL;
1001
                execv(setup_script, args);
1002
                _exit(1);
1003
            }
1004
            while (waitpid(pid, &status, 0) != pid);
1005
            if (!WIFEXITED(status) ||
1006
                WEXITSTATUS(status) != 0) {
1007
                fprintf(stderr, "%s: could not launch network script\n",
1008
                        setup_script);
1009
                return -1;
1010
            }
1011
        }
1012
    return 0;
1013
}
1014

    
1015
static int net_tap_init(VLANState *vlan, const char *model,
1016
                        const char *name, const char *ifname1,
1017
                        const char *setup_script, const char *down_script)
1018
{
1019
    TAPState *s;
1020
    int fd;
1021
    char ifname[128];
1022

    
1023
    if (ifname1 != NULL)
1024
        pstrcpy(ifname, sizeof(ifname), ifname1);
1025
    else
1026
        ifname[0] = '\0';
1027
    TFR(fd = tap_open(ifname, sizeof(ifname)));
1028
    if (fd < 0)
1029
        return -1;
1030

    
1031
    if (!setup_script || !strcmp(setup_script, "no"))
1032
        setup_script = "";
1033
    if (setup_script[0] != '\0') {
1034
        if (launch_script(setup_script, ifname, fd))
1035
            return -1;
1036
    }
1037
    s = net_tap_fd_init(vlan, model, name, fd);
1038
    snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1039
             "ifname=%s,script=%s,downscript=%s",
1040
             ifname, setup_script, down_script);
1041
    if (down_script && strcmp(down_script, "no")) {
1042
        snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
1043
        snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
1044
    }
1045
    return 0;
1046
}
1047

    
1048
#endif /* !_WIN32 */
1049

    
1050
#if defined(CONFIG_VDE)
1051
typedef struct VDEState {
1052
    VLANClientState *vc;
1053
    VDECONN *vde;
1054
} VDEState;
1055

    
1056
static void vde_to_qemu(void *opaque)
1057
{
1058
    VDEState *s = opaque;
1059
    uint8_t buf[4096];
1060
    int size;
1061

    
1062
    size = vde_recv(s->vde, buf, sizeof(buf), 0);
1063
    if (size > 0) {
1064
        qemu_send_packet(s->vc, buf, size);
1065
    }
1066
}
1067

    
1068
static void vde_from_qemu(void *opaque, const uint8_t *buf, int size)
1069
{
1070
    VDEState *s = opaque;
1071
    int ret;
1072
    for(;;) {
1073
        ret = vde_send(s->vde, buf, size, 0);
1074
        if (ret < 0 && errno == EINTR) {
1075
        } else {
1076
            break;
1077
        }
1078
    }
1079
}
1080

    
1081
static void vde_cleanup(VLANClientState *vc)
1082
{
1083
    VDEState *s = vc->opaque;
1084
    qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL);
1085
    vde_close(s->vde);
1086
    qemu_free(s);
1087
}
1088

    
1089
static int net_vde_init(VLANState *vlan, const char *model,
1090
                        const char *name, const char *sock,
1091
                        int port, const char *group, int mode)
1092
{
1093
    VDEState *s;
1094
    char *init_group = strlen(group) ? (char *)group : NULL;
1095
    char *init_sock = strlen(sock) ? (char *)sock : NULL;
1096

    
1097
    struct vde_open_args args = {
1098
        .port = port,
1099
        .group = init_group,
1100
        .mode = mode,
1101
    };
1102

    
1103
    s = qemu_mallocz(sizeof(VDEState));
1104
    s->vde = vde_open(init_sock, "QEMU", &args);
1105
    if (!s->vde){
1106
        free(s);
1107
        return -1;
1108
    }
1109
    s->vc = qemu_new_vlan_client(vlan, model, name, vde_from_qemu,
1110
                                 NULL, vde_cleanup, s);
1111
    qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
1112
    snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d",
1113
             sock, vde_datafd(s->vde));
1114
    return 0;
1115
}
1116
#endif
1117

    
1118
/* network connection */
1119
typedef struct NetSocketState {
1120
    VLANClientState *vc;
1121
    int fd;
1122
    int state; /* 0 = getting length, 1 = getting data */
1123
    unsigned int index;
1124
    unsigned int packet_len;
1125
    uint8_t buf[4096];
1126
    struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1127
} NetSocketState;
1128

    
1129
typedef struct NetSocketListenState {
1130
    VLANState *vlan;
1131
    char *model;
1132
    char *name;
1133
    int fd;
1134
} NetSocketListenState;
1135

    
1136
/* XXX: we consider we can send the whole packet without blocking */
1137
static void net_socket_receive(void *opaque, const uint8_t *buf, int size)
1138
{
1139
    NetSocketState *s = opaque;
1140
    uint32_t len;
1141
    len = htonl(size);
1142

    
1143
    send_all(s->fd, (const uint8_t *)&len, sizeof(len));
1144
    send_all(s->fd, buf, size);
1145
}
1146

    
1147
static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size)
1148
{
1149
    NetSocketState *s = opaque;
1150
    sendto(s->fd, buf, size, 0,
1151
           (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
1152
}
1153

    
1154
static void net_socket_send(void *opaque)
1155
{
1156
    NetSocketState *s = opaque;
1157
    int size, err;
1158
    unsigned l;
1159
    uint8_t buf1[4096];
1160
    const uint8_t *buf;
1161

    
1162
    size = recv(s->fd, buf1, sizeof(buf1), 0);
1163
    if (size < 0) {
1164
        err = socket_error();
1165
        if (err != EWOULDBLOCK)
1166
            goto eoc;
1167
    } else if (size == 0) {
1168
        /* end of connection */
1169
    eoc:
1170
        qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1171
        closesocket(s->fd);
1172
        return;
1173
    }
1174
    buf = buf1;
1175
    while (size > 0) {
1176
        /* reassemble a packet from the network */
1177
        switch(s->state) {
1178
        case 0:
1179
            l = 4 - s->index;
1180
            if (l > size)
1181
                l = size;
1182
            memcpy(s->buf + s->index, buf, l);
1183
            buf += l;
1184
            size -= l;
1185
            s->index += l;
1186
            if (s->index == 4) {
1187
                /* got length */
1188
                s->packet_len = ntohl(*(uint32_t *)s->buf);
1189
                s->index = 0;
1190
                s->state = 1;
1191
            }
1192
            break;
1193
        case 1:
1194
            l = s->packet_len - s->index;
1195
            if (l > size)
1196
                l = size;
1197
            if (s->index + l <= sizeof(s->buf)) {
1198
                memcpy(s->buf + s->index, buf, l);
1199
            } else {
1200
                fprintf(stderr, "serious error: oversized packet received,"
1201
                    "connection terminated.\n");
1202
                s->state = 0;
1203
                goto eoc;
1204
            }
1205

    
1206
            s->index += l;
1207
            buf += l;
1208
            size -= l;
1209
            if (s->index >= s->packet_len) {
1210
                qemu_send_packet(s->vc, s->buf, s->packet_len);
1211
                s->index = 0;
1212
                s->state = 0;
1213
            }
1214
            break;
1215
        }
1216
    }
1217
}
1218

    
1219
static void net_socket_send_dgram(void *opaque)
1220
{
1221
    NetSocketState *s = opaque;
1222
    int size;
1223

    
1224
    size = recv(s->fd, s->buf, sizeof(s->buf), 0);
1225
    if (size < 0)
1226
        return;
1227
    if (size == 0) {
1228
        /* end of connection */
1229
        qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1230
        return;
1231
    }
1232
    qemu_send_packet(s->vc, s->buf, size);
1233
}
1234

    
1235
static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
1236
{
1237
    struct ip_mreq imr;
1238
    int fd;
1239
    int val, ret;
1240
    if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
1241
        fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1242
                inet_ntoa(mcastaddr->sin_addr),
1243
                (int)ntohl(mcastaddr->sin_addr.s_addr));
1244
        return -1;
1245

    
1246
    }
1247
    fd = socket(PF_INET, SOCK_DGRAM, 0);
1248
    if (fd < 0) {
1249
        perror("socket(PF_INET, SOCK_DGRAM)");
1250
        return -1;
1251
    }
1252

    
1253
    val = 1;
1254
    ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1255
                   (const char *)&val, sizeof(val));
1256
    if (ret < 0) {
1257
        perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1258
        goto fail;
1259
    }
1260

    
1261
    ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
1262
    if (ret < 0) {
1263
        perror("bind");
1264
        goto fail;
1265
    }
1266

    
1267
    /* Add host to multicast group */
1268
    imr.imr_multiaddr = mcastaddr->sin_addr;
1269
    imr.imr_interface.s_addr = htonl(INADDR_ANY);
1270

    
1271
    ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1272
                     (const char *)&imr, sizeof(struct ip_mreq));
1273
    if (ret < 0) {
1274
        perror("setsockopt(IP_ADD_MEMBERSHIP)");
1275
        goto fail;
1276
    }
1277

    
1278
    /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1279
    val = 1;
1280
    ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1281
                   (const char *)&val, sizeof(val));
1282
    if (ret < 0) {
1283
        perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1284
        goto fail;
1285
    }
1286

    
1287
    socket_set_nonblock(fd);
1288
    return fd;
1289
fail:
1290
    if (fd >= 0)
1291
        closesocket(fd);
1292
    return -1;
1293
}
1294

    
1295
static void net_socket_cleanup(VLANClientState *vc)
1296
{
1297
    NetSocketState *s = vc->opaque;
1298
    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1299
    close(s->fd);
1300
    qemu_free(s);
1301
}
1302

    
1303
static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
1304
                                                const char *model,
1305
                                                const char *name,
1306
                                                int fd, int is_connected)
1307
{
1308
    struct sockaddr_in saddr;
1309
    int newfd;
1310
    socklen_t saddr_len;
1311
    NetSocketState *s;
1312

    
1313
    /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1314
     * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1315
     * by ONLY ONE process: we must "clone" this dgram socket --jjo
1316
     */
1317

    
1318
    if (is_connected) {
1319
        if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
1320
            /* must be bound */
1321
            if (saddr.sin_addr.s_addr==0) {
1322
                fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1323
                        fd);
1324
                return NULL;
1325
            }
1326
            /* clone dgram socket */
1327
            newfd = net_socket_mcast_create(&saddr);
1328
            if (newfd < 0) {
1329
                /* error already reported by net_socket_mcast_create() */
1330
                close(fd);
1331
                return NULL;
1332
            }
1333
            /* clone newfd to fd, close newfd */
1334
            dup2(newfd, fd);
1335
            close(newfd);
1336

    
1337
        } else {
1338
            fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1339
                    fd, strerror(errno));
1340
            return NULL;
1341
        }
1342
    }
1343

    
1344
    s = qemu_mallocz(sizeof(NetSocketState));
1345
    s->fd = fd;
1346

    
1347
    s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive_dgram,
1348
                                 NULL, net_socket_cleanup, s);
1349
    qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
1350

    
1351
    /* mcast: save bound address as dst */
1352
    if (is_connected) s->dgram_dst=saddr;
1353

    
1354
    snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1355
            "socket: fd=%d (%s mcast=%s:%d)",
1356
            fd, is_connected? "cloned" : "",
1357
            inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1358
    return s;
1359
}
1360

    
1361
static void net_socket_connect(void *opaque)
1362
{
1363
    NetSocketState *s = opaque;
1364
    qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
1365
}
1366

    
1367
static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
1368
                                                 const char *model,
1369
                                                 const char *name,
1370
                                                 int fd, int is_connected)
1371
{
1372
    NetSocketState *s;
1373
    s = qemu_mallocz(sizeof(NetSocketState));
1374
    s->fd = fd;
1375
    s->vc = qemu_new_vlan_client(vlan, model, name, net_socket_receive,
1376
                                 NULL, net_socket_cleanup, s);
1377
    snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1378
             "socket: fd=%d", fd);
1379
    if (is_connected) {
1380
        net_socket_connect(s);
1381
    } else {
1382
        qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
1383
    }
1384
    return s;
1385
}
1386

    
1387
static NetSocketState *net_socket_fd_init(VLANState *vlan,
1388
                                          const char *model, const char *name,
1389
                                          int fd, int is_connected)
1390
{
1391
    int so_type=-1, optlen=sizeof(so_type);
1392

    
1393
    if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
1394
        (socklen_t *)&optlen)< 0) {
1395
        fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
1396
        return NULL;
1397
    }
1398
    switch(so_type) {
1399
    case SOCK_DGRAM:
1400
        return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected);
1401
    case SOCK_STREAM:
1402
        return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
1403
    default:
1404
        /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1405
        fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
1406
        return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
1407
    }
1408
    return NULL;
1409
}
1410

    
1411
static void net_socket_accept(void *opaque)
1412
{
1413
    NetSocketListenState *s = opaque;
1414
    NetSocketState *s1;
1415
    struct sockaddr_in saddr;
1416
    socklen_t len;
1417
    int fd;
1418

    
1419
    for(;;) {
1420
        len = sizeof(saddr);
1421
        fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
1422
        if (fd < 0 && errno != EINTR) {
1423
            return;
1424
        } else if (fd >= 0) {
1425
            break;
1426
        }
1427
    }
1428
    s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1);
1429
    if (!s1) {
1430
        closesocket(fd);
1431
    } else {
1432
        snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
1433
                 "socket: connection from %s:%d",
1434
                 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1435
    }
1436
}
1437

    
1438
static int net_socket_listen_init(VLANState *vlan,
1439
                                  const char *model,
1440
                                  const char *name,
1441
                                  const char *host_str)
1442
{
1443
    NetSocketListenState *s;
1444
    int fd, val, ret;
1445
    struct sockaddr_in saddr;
1446

    
1447
    if (parse_host_port(&saddr, host_str) < 0)
1448
        return -1;
1449

    
1450
    s = qemu_mallocz(sizeof(NetSocketListenState));
1451

    
1452
    fd = socket(PF_INET, SOCK_STREAM, 0);
1453
    if (fd < 0) {
1454
        perror("socket");
1455
        return -1;
1456
    }
1457
    socket_set_nonblock(fd);
1458

    
1459
    /* allow fast reuse */
1460
    val = 1;
1461
    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
1462

    
1463
    ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1464
    if (ret < 0) {
1465
        perror("bind");
1466
        return -1;
1467
    }
1468
    ret = listen(fd, 0);
1469
    if (ret < 0) {
1470
        perror("listen");
1471
        return -1;
1472
    }
1473
    s->vlan = vlan;
1474
    s->model = strdup(model);
1475
    s->name = name ? strdup(name) : NULL;
1476
    s->fd = fd;
1477
    qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
1478
    return 0;
1479
}
1480

    
1481
static int net_socket_connect_init(VLANState *vlan,
1482
                                   const char *model,
1483
                                   const char *name,
1484
                                   const char *host_str)
1485
{
1486
    NetSocketState *s;
1487
    int fd, connected, ret, err;
1488
    struct sockaddr_in saddr;
1489

    
1490
    if (parse_host_port(&saddr, host_str) < 0)
1491
        return -1;
1492

    
1493
    fd = socket(PF_INET, SOCK_STREAM, 0);
1494
    if (fd < 0) {
1495
        perror("socket");
1496
        return -1;
1497
    }
1498
    socket_set_nonblock(fd);
1499

    
1500
    connected = 0;
1501
    for(;;) {
1502
        ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1503
        if (ret < 0) {
1504
            err = socket_error();
1505
            if (err == EINTR || err == EWOULDBLOCK) {
1506
            } else if (err == EINPROGRESS) {
1507
                break;
1508
#ifdef _WIN32
1509
            } else if (err == WSAEALREADY) {
1510
                break;
1511
#endif
1512
            } else {
1513
                perror("connect");
1514
                closesocket(fd);
1515
                return -1;
1516
            }
1517
        } else {
1518
            connected = 1;
1519
            break;
1520
        }
1521
    }
1522
    s = net_socket_fd_init(vlan, model, name, fd, connected);
1523
    if (!s)
1524
        return -1;
1525
    snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1526
             "socket: connect to %s:%d",
1527
             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1528
    return 0;
1529
}
1530

    
1531
static int net_socket_mcast_init(VLANState *vlan,
1532
                                 const char *model,
1533
                                 const char *name,
1534
                                 const char *host_str)
1535
{
1536
    NetSocketState *s;
1537
    int fd;
1538
    struct sockaddr_in saddr;
1539

    
1540
    if (parse_host_port(&saddr, host_str) < 0)
1541
        return -1;
1542

    
1543

    
1544
    fd = net_socket_mcast_create(&saddr);
1545
    if (fd < 0)
1546
        return -1;
1547

    
1548
    s = net_socket_fd_init(vlan, model, name, fd, 0);
1549
    if (!s)
1550
        return -1;
1551

    
1552
    s->dgram_dst = saddr;
1553

    
1554
    snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1555
             "socket: mcast=%s:%d",
1556
             inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1557
    return 0;
1558

    
1559
}
1560

    
1561
/* find or alloc a new VLAN */
1562
VLANState *qemu_find_vlan(int id)
1563
{
1564
    VLANState **pvlan, *vlan;
1565
    for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1566
        if (vlan->id == id)
1567
            return vlan;
1568
    }
1569
    vlan = qemu_mallocz(sizeof(VLANState));
1570
    vlan->id = id;
1571
    vlan->next = NULL;
1572
    pvlan = &first_vlan;
1573
    while (*pvlan != NULL)
1574
        pvlan = &(*pvlan)->next;
1575
    *pvlan = vlan;
1576
    return vlan;
1577
}
1578

    
1579
static int nic_get_free_idx(void)
1580
{
1581
    int index;
1582

    
1583
    for (index = 0; index < MAX_NICS; index++)
1584
        if (!nd_table[index].used)
1585
            return index;
1586
    return -1;
1587
}
1588

    
1589
void qemu_check_nic_model(NICInfo *nd, const char *model)
1590
{
1591
    const char *models[2];
1592

    
1593
    models[0] = model;
1594
    models[1] = NULL;
1595

    
1596
    qemu_check_nic_model_list(nd, models, model);
1597
}
1598

    
1599
void qemu_check_nic_model_list(NICInfo *nd, const char * const *models,
1600
                               const char *default_model)
1601
{
1602
    int i, exit_status = 0;
1603

    
1604
    if (!nd->model)
1605
        nd->model = strdup(default_model);
1606

    
1607
    if (strcmp(nd->model, "?") != 0) {
1608
        for (i = 0 ; models[i]; i++)
1609
            if (strcmp(nd->model, models[i]) == 0)
1610
                return;
1611

    
1612
        fprintf(stderr, "qemu: Unsupported NIC model: %s\n", nd->model);
1613
        exit_status = 1;
1614
    }
1615

    
1616
    fprintf(stderr, "qemu: Supported NIC models: ");
1617
    for (i = 0 ; models[i]; i++)
1618
        fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
1619

    
1620
    exit(exit_status);
1621
}
1622

    
1623
int net_client_init(const char *device, const char *p)
1624
{
1625
    static const char * const fd_params[] = {
1626
        "vlan", "name", "fd", NULL
1627
    };
1628
    char buf[1024];
1629
    int vlan_id, ret;
1630
    VLANState *vlan;
1631
    char *name = NULL;
1632

    
1633
    vlan_id = 0;
1634
    if (get_param_value(buf, sizeof(buf), "vlan", p)) {
1635
        vlan_id = strtol(buf, NULL, 0);
1636
    }
1637
    vlan = qemu_find_vlan(vlan_id);
1638

    
1639
    if (get_param_value(buf, sizeof(buf), "name", p)) {
1640
        name = strdup(buf);
1641
    }
1642
    if (!strcmp(device, "nic")) {
1643
        static const char * const nic_params[] = {
1644
            "vlan", "name", "macaddr", "model", NULL
1645
        };
1646
        NICInfo *nd;
1647
        uint8_t *macaddr;
1648
        int idx = nic_get_free_idx();
1649

    
1650
        if (check_params(buf, sizeof(buf), nic_params, p) < 0) {
1651
            fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
1652
                    buf, p);
1653
            return -1;
1654
        }
1655
        if (idx == -1 || nb_nics >= MAX_NICS) {
1656
            fprintf(stderr, "Too Many NICs\n");
1657
            ret = -1;
1658
            goto out;
1659
        }
1660
        nd = &nd_table[idx];
1661
        macaddr = nd->macaddr;
1662
        macaddr[0] = 0x52;
1663
        macaddr[1] = 0x54;
1664
        macaddr[2] = 0x00;
1665
        macaddr[3] = 0x12;
1666
        macaddr[4] = 0x34;
1667
        macaddr[5] = 0x56 + idx;
1668

    
1669
        if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
1670
            if (parse_macaddr(macaddr, buf) < 0) {
1671
                fprintf(stderr, "invalid syntax for ethernet address\n");
1672
                ret = -1;
1673
                goto out;
1674
            }
1675
        }
1676
        if (get_param_value(buf, sizeof(buf), "model", p)) {
1677
            nd->model = strdup(buf);
1678
        }
1679
        nd->vlan = vlan;
1680
        nd->name = name;
1681
        nd->used = 1;
1682
        name = NULL;
1683
        nb_nics++;
1684
        vlan->nb_guest_devs++;
1685
        ret = idx;
1686
    } else
1687
    if (!strcmp(device, "none")) {
1688
        if (*p != '\0') {
1689
            fprintf(stderr, "qemu: 'none' takes no parameters\n");
1690
            return -1;
1691
        }
1692
        /* does nothing. It is needed to signal that no network cards
1693
           are wanted */
1694
        ret = 0;
1695
    } else
1696
#ifdef CONFIG_SLIRP
1697
    if (!strcmp(device, "user")) {
1698
        static const char * const slirp_params[] = {
1699
            "vlan", "name", "hostname", "restrict", "ip", NULL
1700
        };
1701
        if (check_params(buf, sizeof(buf), slirp_params, p) < 0) {
1702
            fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
1703
                    buf, p);
1704
            return -1;
1705
        }
1706
        if (get_param_value(buf, sizeof(buf), "hostname", p)) {
1707
            pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
1708
        }
1709
        if (get_param_value(buf, sizeof(buf), "restrict", p)) {
1710
            slirp_restrict = (buf[0] == 'y') ? 1 : 0;
1711
        }
1712
        if (get_param_value(buf, sizeof(buf), "ip", p)) {
1713
            slirp_ip = strdup(buf);
1714
        }
1715
        vlan->nb_host_devs++;
1716
        ret = net_slirp_init(vlan, device, name);
1717
    } else if (!strcmp(device, "channel")) {
1718
        long port;
1719
        char name[20], *devname;
1720
        struct VMChannel *vmc;
1721

    
1722
        port = strtol(p, &devname, 10);
1723
        devname++;
1724
        if (port < 1 || port > 65535) {
1725
            fprintf(stderr, "vmchannel wrong port number\n");
1726
            ret = -1;
1727
            goto out;
1728
        }
1729
        vmc = malloc(sizeof(struct VMChannel));
1730
        snprintf(name, 20, "vmchannel%ld", port);
1731
        vmc->hd = qemu_chr_open(name, devname, NULL);
1732
        if (!vmc->hd) {
1733
            fprintf(stderr, "qemu: could not open vmchannel device"
1734
                    "'%s'\n", devname);
1735
            ret = -1;
1736
            goto out;
1737
        }
1738
        vmc->port = port;
1739
        slirp_add_exec(3, vmc->hd, 4, port);
1740
        qemu_chr_add_handlers(vmc->hd, vmchannel_can_read, vmchannel_read,
1741
                NULL, vmc);
1742
        ret = 0;
1743
    } else
1744
#endif
1745
#ifdef _WIN32
1746
    if (!strcmp(device, "tap")) {
1747
        static const char * const tap_params[] = {
1748
            "vlan", "name", "ifname", NULL
1749
        };
1750
        char ifname[64];
1751

    
1752
        if (check_params(buf, sizeof(buf), tap_params, p) < 0) {
1753
            fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
1754
                    buf, p);
1755
            return -1;
1756
        }
1757
        if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
1758
            fprintf(stderr, "tap: no interface name\n");
1759
            ret = -1;
1760
            goto out;
1761
        }
1762
        vlan->nb_host_devs++;
1763
        ret = tap_win32_init(vlan, device, name, ifname);
1764
    } else
1765
#elif defined (_AIX)
1766
#else
1767
    if (!strcmp(device, "tap")) {
1768
        char ifname[64];
1769
        char setup_script[1024], down_script[1024];
1770
        int fd;
1771
        vlan->nb_host_devs++;
1772
        if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
1773
            if (check_params(buf, sizeof(buf), fd_params, p) < 0) {
1774
                fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
1775
                        buf, p);
1776
                return -1;
1777
            }
1778
            fd = strtol(buf, NULL, 0);
1779
            fcntl(fd, F_SETFL, O_NONBLOCK);
1780
            net_tap_fd_init(vlan, device, name, fd);
1781
            ret = 0;
1782
        } else {
1783
            static const char * const tap_params[] = {
1784
                "vlan", "name", "ifname", "script", "downscript", NULL
1785
            };
1786
            if (check_params(buf, sizeof(buf), tap_params, p) < 0) {
1787
                fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
1788
                        buf, p);
1789
                return -1;
1790
            }
1791
            if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
1792
                ifname[0] = '\0';
1793
            }
1794
            if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
1795
                pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
1796
            }
1797
            if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
1798
                pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
1799
            }
1800
            ret = net_tap_init(vlan, device, name, ifname, setup_script, down_script);
1801
        }
1802
    } else
1803
#endif
1804
    if (!strcmp(device, "socket")) {
1805
        if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
1806
            int fd;
1807
            if (check_params(buf, sizeof(buf), fd_params, p) < 0) {
1808
                fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
1809
                        buf, p);
1810
                return -1;
1811
            }
1812
            fd = strtol(buf, NULL, 0);
1813
            ret = -1;
1814
            if (net_socket_fd_init(vlan, device, name, fd, 1))
1815
                ret = 0;
1816
        } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
1817
            static const char * const listen_params[] = {
1818
                "vlan", "name", "listen", NULL
1819
            };
1820
            if (check_params(buf, sizeof(buf), listen_params, p) < 0) {
1821
                fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
1822
                        buf, p);
1823
                return -1;
1824
            }
1825
            ret = net_socket_listen_init(vlan, device, name, buf);
1826
        } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
1827
            static const char * const connect_params[] = {
1828
                "vlan", "name", "connect", NULL
1829
            };
1830
            if (check_params(buf, sizeof(buf), connect_params, p) < 0) {
1831
                fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
1832
                        buf, p);
1833
                return -1;
1834
            }
1835
            ret = net_socket_connect_init(vlan, device, name, buf);
1836
        } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
1837
            static const char * const mcast_params[] = {
1838
                "vlan", "name", "mcast", NULL
1839
            };
1840
            if (check_params(buf, sizeof(buf), mcast_params, p) < 0) {
1841
                fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
1842
                        buf, p);
1843
                return -1;
1844
            }
1845
            ret = net_socket_mcast_init(vlan, device, name, buf);
1846
        } else {
1847
            fprintf(stderr, "Unknown socket options: %s\n", p);
1848
            ret = -1;
1849
            goto out;
1850
        }
1851
        vlan->nb_host_devs++;
1852
    } else
1853
#ifdef CONFIG_VDE
1854
    if (!strcmp(device, "vde")) {
1855
        static const char * const vde_params[] = {
1856
            "vlan", "name", "sock", "port", "group", "mode", NULL
1857
        };
1858
        char vde_sock[1024], vde_group[512];
1859
        int vde_port, vde_mode;
1860

    
1861
        if (check_params(buf, sizeof(buf), vde_params, p) < 0) {
1862
            fprintf(stderr, "qemu: invalid parameter '%s' in '%s'\n",
1863
                    buf, p);
1864
            return -1;
1865
        }
1866
        vlan->nb_host_devs++;
1867
        if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
1868
            vde_sock[0] = '\0';
1869
        }
1870
        if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
1871
            vde_port = strtol(buf, NULL, 10);
1872
        } else {
1873
            vde_port = 0;
1874
        }
1875
        if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
1876
            vde_group[0] = '\0';
1877
        }
1878
        if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
1879
            vde_mode = strtol(buf, NULL, 8);
1880
        } else {
1881
            vde_mode = 0700;
1882
        }
1883
        ret = net_vde_init(vlan, device, name, vde_sock, vde_port, vde_group, vde_mode);
1884
    } else
1885
#endif
1886
    {
1887
        fprintf(stderr, "Unknown network device: %s\n", device);
1888
        ret = -1;
1889
        goto out;
1890
    }
1891
    if (ret < 0) {
1892
        fprintf(stderr, "Could not initialize device '%s'\n", device);
1893
    }
1894
out:
1895
    if (name)
1896
        free(name);
1897
    return ret;
1898
}
1899

    
1900
void net_client_uninit(NICInfo *nd)
1901
{
1902
    nd->vlan->nb_guest_devs--;
1903
    nb_nics--;
1904
    nd->used = 0;
1905
    free((void *)nd->model);
1906
}
1907

    
1908
static int net_host_check_device(const char *device)
1909
{
1910
    int i;
1911
    const char *valid_param_list[] = { "tap", "socket"
1912
#ifdef CONFIG_SLIRP
1913
                                       ,"user"
1914
#endif
1915
#ifdef CONFIG_VDE
1916
                                       ,"vde"
1917
#endif
1918
    };
1919
    for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
1920
        if (!strncmp(valid_param_list[i], device,
1921
                     strlen(valid_param_list[i])))
1922
            return 1;
1923
    }
1924

    
1925
    return 0;
1926
}
1927

    
1928
void net_host_device_add(Monitor *mon, const char *device, const char *opts)
1929
{
1930
    if (!net_host_check_device(device)) {
1931
        monitor_printf(mon, "invalid host network device %s\n", device);
1932
        return;
1933
    }
1934
    net_client_init(device, opts);
1935
}
1936

    
1937
void net_host_device_remove(Monitor *mon, int vlan_id, const char *device)
1938
{
1939
    VLANState *vlan;
1940
    VLANClientState *vc;
1941

    
1942
    vlan = qemu_find_vlan(vlan_id);
1943

    
1944
    for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
1945
        if (!strcmp(vc->name, device)) {
1946
            break;
1947
        }
1948
    }
1949

    
1950
    if (!vc) {
1951
        monitor_printf(mon, "can't find device %s\n", device);
1952
        return;
1953
    }
1954
    if (!net_host_check_device(vc->model)) {
1955
        monitor_printf(mon, "invalid host network device %s\n", device);
1956
        return;
1957
    }
1958
    qemu_del_vlan_client(vc);
1959
}
1960

    
1961
int net_client_parse(const char *str)
1962
{
1963
    const char *p;
1964
    char *q;
1965
    char device[64];
1966

    
1967
    p = str;
1968
    q = device;
1969
    while (*p != '\0' && *p != ',') {
1970
        if ((q - device) < sizeof(device) - 1)
1971
            *q++ = *p;
1972
        p++;
1973
    }
1974
    *q = '\0';
1975
    if (*p == ',')
1976
        p++;
1977

    
1978
    return net_client_init(device, p);
1979
}
1980

    
1981
void do_info_network(Monitor *mon)
1982
{
1983
    VLANState *vlan;
1984
    VLANClientState *vc;
1985

    
1986
    for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
1987
        monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
1988
        for(vc = vlan->first_client; vc != NULL; vc = vc->next)
1989
            monitor_printf(mon, "  %s: %s\n", vc->name, vc->info_str);
1990
    }
1991
}
1992

    
1993
int do_set_link(Monitor *mon, const char *name, const char *up_or_down)
1994
{
1995
    VLANState *vlan;
1996
    VLANClientState *vc = NULL;
1997

    
1998
    for (vlan = first_vlan; vlan != NULL; vlan = vlan->next)
1999
        for (vc = vlan->first_client; vc != NULL; vc = vc->next)
2000
            if (strcmp(vc->name, name) == 0)
2001
                goto done;
2002
done:
2003

    
2004
    if (!vc) {
2005
        monitor_printf(mon, "could not find network device '%s'", name);
2006
        return 0;
2007
    }
2008

    
2009
    if (strcmp(up_or_down, "up") == 0)
2010
        vc->link_down = 0;
2011
    else if (strcmp(up_or_down, "down") == 0)
2012
        vc->link_down = 1;
2013
    else
2014
        monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' "
2015
                       "valid\n", up_or_down);
2016

    
2017
    if (vc->link_status_changed)
2018
        vc->link_status_changed(vc);
2019

    
2020
    return 1;
2021
}
2022

    
2023
void net_cleanup(void)
2024
{
2025
    VLANState *vlan;
2026

    
2027
    /* close network clients */
2028
    for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2029
        VLANClientState *vc = vlan->first_client;
2030

    
2031
        while (vc) {
2032
            VLANClientState *next = vc->next;
2033

    
2034
            qemu_del_vlan_client(vc);
2035

    
2036
            vc = next;
2037
        }
2038
    }
2039
}
2040

    
2041
void net_client_check(void)
2042
{
2043
    VLANState *vlan;
2044

    
2045
    for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2046
        if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
2047
            continue;
2048
        if (vlan->nb_guest_devs == 0)
2049
            fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
2050
        if (vlan->nb_host_devs == 0)
2051
            fprintf(stderr,
2052
                    "Warning: vlan %d is not connected to host network\n",
2053
                    vlan->id);
2054
    }
2055
}