net: fix vnet_hdr bustage with slirp
[qemu-kvm/fedora.git] / net.c
blob2428f631d072b43a87f6c91c6476680d1a678eca
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
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>
32 /* Needed early for HOST_BSD etc. */
33 #include "config-host.h"
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>
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"
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
96 #if defined(__OpenBSD__)
97 #include <util.h>
98 #endif
100 #if defined(CONFIG_VDE)
101 #include <libvdeplug.h>
102 #endif
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
113 // FIXME: #include "qemu-kvm.h"
114 #include "qemu-common.h"
115 #include "net.h"
116 #include "monitor.h"
117 #include "sysemu.h"
118 #include "qemu-timer.h"
119 #include "qemu-char.h"
120 #include "audio/audio.h"
121 #include "qemu_socket.h"
122 #include "qemu-log.h"
124 #include "slirp/libslirp.h"
127 static VLANState *first_vlan;
129 /***********************************************************/
130 /* network device redirectors */
132 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
133 static void hex_dump(FILE *f, const uint8_t *buf, int size)
135 int len, i, j, c;
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, " ");
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);
155 fprintf(f, "\n");
158 #endif
160 static int parse_macaddr(uint8_t *macaddr, const char *p)
162 int i;
163 char *last_char;
164 long int offset;
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++;
186 return 0;
189 return -1;
192 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
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';
208 *pp = p1;
209 return 0;
212 int parse_host_src_port(struct sockaddr_in *haddr,
213 struct sockaddr_in *saddr,
214 const char *input_str)
216 char *str = strdup(input_str);
217 char *host_str = str;
218 char *src_str;
219 const char *src_str2;
220 char *ptr;
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".
227 if ((ptr = strchr(str,',')))
228 *ptr = '\0';
230 if ((src_str = strchr(input_str,'@'))) {
231 *src_str = '\0';
232 src_str++;
235 if (parse_host_port(haddr, host_str) < 0)
236 goto fail;
238 src_str2 = src_str;
239 if (!src_str || *src_str == '\0')
240 src_str2 = ":0";
242 if (parse_host_port(saddr, src_str2) < 0)
243 goto fail;
245 free(str);
246 return(0);
248 fail:
249 free(str);
250 return -1;
253 int parse_host_port(struct sockaddr_in *saddr, const char *str)
255 char buf[512];
256 struct hostent *he;
257 const char *p, *r;
258 int port;
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;
276 port = strtol(p, (char **)&r, 0);
277 if (r == p)
278 return -1;
279 saddr->sin_port = htons(port);
280 return 0;
283 #if !defined(_WIN32) && 0
284 static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
286 const char *p;
287 int len;
289 len = MIN(108, strlen(str));
290 p = strchr(str, ',');
291 if (p)
292 len = MIN(len, p - str);
294 memset(uaddr, 0, sizeof(*uaddr));
296 uaddr->sun_family = AF_UNIX;
297 memcpy(uaddr->sun_path, str, len);
299 return 0;
301 #endif
303 void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
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]);
312 static char *assign_name(VLANClientState *vc1, const char *model)
314 VLANState *vlan;
315 char buf[256];
316 int id = 0;
318 for (vlan = first_vlan; vlan; vlan = vlan->next) {
319 VLANClientState *vc;
321 for (vc = vlan->first_client; vc; vc = vc->next)
322 if (vc != vc1 && strcmp(vc->model, model) == 0)
323 id++;
326 snprintf(buf, sizeof(buf), "%s.%d", model, id);
328 return strdup(buf);
331 VLANClientState *qemu_new_vlan_client(VLANState *vlan,
332 const char *model,
333 const char *name,
334 NetCanReceive *can_receive,
335 NetReceive *receive,
336 NetReceiveIOV *receive_iov,
337 NetCleanup *cleanup,
338 void *opaque)
340 VLANClientState *vc, **pvc;
341 vc = qemu_mallocz(sizeof(VLANClientState));
342 vc->model = strdup(model);
343 if (name)
344 vc->name = strdup(name);
345 else
346 vc->name = assign_name(vc, model);
347 vc->can_receive = can_receive;
348 vc->receive = receive;
349 vc->receive_iov = receive_iov;
350 vc->cleanup = cleanup;
351 vc->opaque = opaque;
352 vc->vlan = vlan;
354 vc->next = NULL;
355 pvc = &vlan->first_client;
356 while (*pvc != NULL)
357 pvc = &(*pvc)->next;
358 *pvc = vc;
359 return vc;
362 void qemu_del_vlan_client(VLANClientState *vc)
364 VLANClientState **pvc = &vc->vlan->first_client;
366 while (*pvc != NULL)
367 if (*pvc == vc) {
368 *pvc = vc->next;
369 if (vc->cleanup) {
370 vc->cleanup(vc);
372 free(vc->name);
373 free(vc->model);
374 qemu_free(vc);
375 break;
376 } else
377 pvc = &(*pvc)->next;
380 VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque)
382 VLANClientState **pvc = &vlan->first_client;
384 while (*pvc != NULL)
385 if ((*pvc)->opaque == opaque)
386 return *pvc;
387 else
388 pvc = &(*pvc)->next;
390 return NULL;
393 static VLANClientState *
394 qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
395 const char *client_str)
397 VLANState *vlan;
398 VLANClientState *vc;
400 vlan = qemu_find_vlan(vlan_id, 0);
401 if (!vlan) {
402 monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
403 return NULL;
406 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
407 if (!strcmp(vc->name, client_str)) {
408 break;
411 if (!vc) {
412 monitor_printf(mon, "can't find device %s on VLAN %d\n",
413 client_str, vlan_id);
416 return vc;
419 int qemu_can_send_packet(VLANClientState *sender)
421 VLANState *vlan = sender->vlan;
422 VLANClientState *vc;
424 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
425 if (vc == sender) {
426 continue;
429 /* no can_receive() handler, they can always receive */
430 if (!vc->can_receive || vc->can_receive(vc)) {
431 return 1;
434 return 0;
437 static int
438 qemu_deliver_packet(VLANClientState *sender, const uint8_t *buf, int size, int raw)
440 VLANClientState *vc;
441 int ret = -1;
443 sender->vlan->delivering = 1;
445 for (vc = sender->vlan->first_client; vc != NULL; vc = vc->next) {
446 ssize_t len;
448 if (vc == sender) {
449 continue;
452 if (vc->link_down) {
453 ret = size;
454 continue;
457 if (raw && vc->receive_raw) {
458 len = vc->receive_raw(vc, buf, size);
459 } else {
460 len = vc->receive(vc, buf, size);
463 ret = (ret >= 0) ? ret : len;
466 sender->vlan->delivering = 0;
468 return ret;
471 void qemu_purge_queued_packets(VLANClientState *vc)
473 VLANPacket **pp = &vc->vlan->send_queue;
475 while (*pp != NULL) {
476 VLANPacket *packet = *pp;
478 if (packet->sender == vc) {
479 *pp = packet->next;
480 qemu_free(packet);
481 } else {
482 pp = &packet->next;
487 void qemu_flush_queued_packets(VLANClientState *vc)
489 VLANPacket *packet;
491 while ((packet = vc->vlan->send_queue) != NULL) {
492 int ret;
494 vc->vlan->send_queue = packet->next;
496 ret = qemu_deliver_packet(packet->sender, packet->data,
497 packet->size, packet->raw);
498 if (ret == 0 && packet->sent_cb != NULL) {
499 packet->next = vc->vlan->send_queue;
500 vc->vlan->send_queue = packet;
501 break;
504 if (packet->sent_cb)
505 packet->sent_cb(packet->sender, ret);
507 qemu_free(packet);
511 static void qemu_enqueue_packet(VLANClientState *sender,
512 const uint8_t *buf, int size, int raw,
513 NetPacketSent *sent_cb)
515 VLANPacket *packet;
517 packet = qemu_malloc(sizeof(VLANPacket) + size);
518 packet->next = sender->vlan->send_queue;
519 packet->sender = sender;
520 packet->size = size;
521 packet->raw = raw;
522 packet->sent_cb = sent_cb;
523 memcpy(packet->data, buf, size);
524 sender->vlan->send_queue = packet;
527 static ssize_t qemu_send_packet_async2(VLANClientState *sender,
528 const uint8_t *buf, int size, int raw,
529 NetPacketSent *sent_cb)
531 int ret;
533 if (sender->link_down) {
534 return size;
537 #ifdef DEBUG_NET
538 printf("vlan %d send:\n", sender->vlan->id);
539 hex_dump(stdout, buf, size);
540 #endif
542 if (sender->vlan->delivering) {
543 qemu_enqueue_packet(sender, buf, size, raw, NULL);
544 return size;
547 ret = qemu_deliver_packet(sender, buf, size, raw);
548 if (ret == 0 && sent_cb != NULL) {
549 qemu_enqueue_packet(sender, buf, size, raw, sent_cb);
550 return 0;
553 qemu_flush_queued_packets(sender);
555 return ret;
558 ssize_t qemu_send_packet_async(VLANClientState *sender,
559 const uint8_t *buf, int size,
560 NetPacketSent *sent_cb)
562 return qemu_send_packet_async2(sender, buf, size, 0, sent_cb);
565 ssize_t qemu_send_packet(VLANClientState *sender, const uint8_t *buf, int size)
567 return qemu_send_packet_async2(sender, buf, size, 0, NULL);
570 ssize_t qemu_send_packet_raw(VLANClientState *sender, const uint8_t *buf, int size)
572 return qemu_send_packet_async2(sender, buf, size, 1, NULL);
575 static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
576 int iovcnt)
578 uint8_t buffer[4096];
579 size_t offset = 0;
580 int i;
582 for (i = 0; i < iovcnt; i++) {
583 size_t len;
585 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
586 memcpy(buffer + offset, iov[i].iov_base, len);
587 offset += len;
590 return vc->receive(vc, buffer, offset);
593 static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
595 size_t offset = 0;
596 int i;
598 for (i = 0; i < iovcnt; i++)
599 offset += iov[i].iov_len;
600 return offset;
603 static int qemu_deliver_packet_iov(VLANClientState *sender,
604 const struct iovec *iov, int iovcnt)
606 VLANClientState *vc;
607 int ret = -1;
609 sender->vlan->delivering = 1;
611 for (vc = sender->vlan->first_client; vc != NULL; vc = vc->next) {
612 ssize_t len;
614 if (vc == sender) {
615 continue;
618 if (vc->link_down) {
619 ret = calc_iov_length(iov, iovcnt);
620 continue;
623 if (vc->receive_iov) {
624 len = vc->receive_iov(vc, iov, iovcnt);
625 } else {
626 len = vc_sendv_compat(vc, iov, iovcnt);
629 ret = (ret >= 0) ? ret : len;
632 sender->vlan->delivering = 0;
634 return ret;
637 static ssize_t qemu_enqueue_packet_iov(VLANClientState *sender,
638 const struct iovec *iov, int iovcnt,
639 NetPacketSent *sent_cb)
641 VLANPacket *packet;
642 size_t max_len = 0;
643 int i;
645 max_len = calc_iov_length(iov, iovcnt);
647 packet = qemu_malloc(sizeof(VLANPacket) + max_len);
648 packet->next = sender->vlan->send_queue;
649 packet->sender = sender;
650 packet->sent_cb = sent_cb;
651 packet->size = 0;
652 packet->raw = 0;
654 for (i = 0; i < iovcnt; i++) {
655 size_t len = iov[i].iov_len;
657 memcpy(packet->data + packet->size, iov[i].iov_base, len);
658 packet->size += len;
661 sender->vlan->send_queue = packet;
663 return packet->size;
666 ssize_t qemu_sendv_packet_async(VLANClientState *sender,
667 const struct iovec *iov, int iovcnt,
668 NetPacketSent *sent_cb)
670 int ret;
672 if (sender->link_down) {
673 return calc_iov_length(iov, iovcnt);
676 if (sender->vlan->delivering) {
677 return qemu_enqueue_packet_iov(sender, iov, iovcnt, NULL);
680 ret = qemu_deliver_packet_iov(sender, iov, iovcnt);
681 if (ret == 0 && sent_cb != NULL) {
682 qemu_enqueue_packet_iov(sender, iov, iovcnt, sent_cb);
683 return 0;
686 qemu_flush_queued_packets(sender);
688 return ret;
691 ssize_t
692 qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
694 return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
697 static void config_error(Monitor *mon, const char *fmt, ...)
699 va_list ap;
701 va_start(ap, fmt);
702 if (mon) {
703 monitor_vprintf(mon, fmt, ap);
704 } else {
705 fprintf(stderr, "qemu: ");
706 vfprintf(stderr, fmt, ap);
707 exit(1);
709 va_end(ap);
712 #if defined(CONFIG_SLIRP)
714 /* slirp network adapter */
716 #define SLIRP_CFG_HOSTFWD 1
717 #define SLIRP_CFG_LEGACY 2
719 struct slirp_config_str {
720 struct slirp_config_str *next;
721 int flags;
722 char str[1024];
723 int legacy_format;
726 typedef struct SlirpState {
727 TAILQ_ENTRY(SlirpState) entry;
728 VLANClientState *vc;
729 Slirp *slirp;
730 #ifndef _WIN32
731 char smb_dir[128];
732 #endif
733 } SlirpState;
735 static struct slirp_config_str *slirp_configs;
736 const char *legacy_tftp_prefix;
737 const char *legacy_bootp_filename;
738 static TAILQ_HEAD(slirp_stacks, SlirpState) slirp_stacks =
739 TAILQ_HEAD_INITIALIZER(slirp_stacks);
741 static void slirp_hostfwd(SlirpState *s, Monitor *mon, const char *redir_str,
742 int legacy_format);
743 static void slirp_guestfwd(SlirpState *s, Monitor *mon, const char *config_str,
744 int legacy_format);
746 #ifndef _WIN32
747 static const char *legacy_smb_export;
749 static void slirp_smb(SlirpState *s, Monitor *mon, const char *exported_dir,
750 struct in_addr vserver_addr);
751 static void slirp_smb_cleanup(SlirpState *s);
752 #else
753 static inline void slirp_smb_cleanup(SlirpState *s) { }
754 #endif
756 int slirp_can_output(void *opaque)
758 SlirpState *s = opaque;
760 return qemu_can_send_packet(s->vc);
763 void slirp_output(void *opaque, const uint8_t *pkt, int pkt_len)
765 SlirpState *s = opaque;
767 #ifdef DEBUG_SLIRP
768 printf("slirp output:\n");
769 hex_dump(stdout, pkt, pkt_len);
770 #endif
771 qemu_send_packet(s->vc, pkt, pkt_len);
774 static ssize_t slirp_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
776 SlirpState *s = vc->opaque;
778 #ifdef DEBUG_SLIRP
779 printf("slirp input:\n");
780 hex_dump(stdout, buf, size);
781 #endif
782 slirp_input(s->slirp, buf, size);
783 return size;
786 static void net_slirp_cleanup(VLANClientState *vc)
788 SlirpState *s = vc->opaque;
790 slirp_cleanup(s->slirp);
791 slirp_smb_cleanup(s);
792 TAILQ_REMOVE(&slirp_stacks, s, entry);
793 qemu_free(s);
796 static int net_slirp_init(Monitor *mon, VLANState *vlan, const char *model,
797 const char *name, int restricted,
798 const char *vnetwork, const char *vhost,
799 const char *vhostname, const char *tftp_export,
800 const char *bootfile, const char *vdhcp_start,
801 const char *vnameserver, const char *smb_export,
802 const char *vsmbserver)
804 /* default settings according to historic slirp */
805 struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */
806 struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */
807 struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */
808 struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */
809 struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */
810 #ifndef _WIN32
811 struct in_addr smbsrv = { .s_addr = 0 };
812 #endif
813 SlirpState *s;
814 char buf[20];
815 uint32_t addr;
816 int shift;
817 char *end;
819 if (!tftp_export) {
820 tftp_export = legacy_tftp_prefix;
822 if (!bootfile) {
823 bootfile = legacy_bootp_filename;
826 if (vnetwork) {
827 if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) {
828 if (!inet_aton(vnetwork, &net)) {
829 return -1;
831 addr = ntohl(net.s_addr);
832 if (!(addr & 0x80000000)) {
833 mask.s_addr = htonl(0xff000000); /* class A */
834 } else if ((addr & 0xfff00000) == 0xac100000) {
835 mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */
836 } else if ((addr & 0xc0000000) == 0x80000000) {
837 mask.s_addr = htonl(0xffff0000); /* class B */
838 } else if ((addr & 0xffff0000) == 0xc0a80000) {
839 mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */
840 } else if ((addr & 0xffff0000) == 0xc6120000) {
841 mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */
842 } else if ((addr & 0xe0000000) == 0xe0000000) {
843 mask.s_addr = htonl(0xffffff00); /* class C */
844 } else {
845 mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */
847 } else {
848 if (!inet_aton(buf, &net)) {
849 return -1;
851 shift = strtol(vnetwork, &end, 10);
852 if (*end != '\0') {
853 if (!inet_aton(vnetwork, &mask)) {
854 return -1;
856 } else if (shift < 4 || shift > 32) {
857 return -1;
858 } else {
859 mask.s_addr = htonl(0xffffffff << (32 - shift));
862 net.s_addr &= mask.s_addr;
863 host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr);
864 dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr);
865 dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr);
868 if (vhost && !inet_aton(vhost, &host)) {
869 return -1;
871 if ((host.s_addr & mask.s_addr) != net.s_addr) {
872 return -1;
875 if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) {
876 return -1;
878 if ((dhcp.s_addr & mask.s_addr) != net.s_addr ||
879 dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) {
880 return -1;
883 if (vnameserver && !inet_aton(vnameserver, &dns)) {
884 return -1;
886 if ((dns.s_addr & mask.s_addr) != net.s_addr ||
887 dns.s_addr == host.s_addr) {
888 return -1;
891 #ifndef _WIN32
892 if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
893 return -1;
895 #endif
897 s = qemu_mallocz(sizeof(SlirpState));
898 s->slirp = slirp_init(restricted, net, mask, host, vhostname,
899 tftp_export, bootfile, dhcp, dns, s);
900 TAILQ_INSERT_TAIL(&slirp_stacks, s, entry);
902 while (slirp_configs) {
903 struct slirp_config_str *config = slirp_configs;
905 if (config->flags & SLIRP_CFG_HOSTFWD) {
906 slirp_hostfwd(s, mon, config->str,
907 config->flags & SLIRP_CFG_LEGACY);
908 } else {
909 slirp_guestfwd(s, mon, config->str,
910 config->flags & SLIRP_CFG_LEGACY);
912 slirp_configs = config->next;
913 qemu_free(config);
915 #ifndef _WIN32
916 if (!smb_export) {
917 smb_export = legacy_smb_export;
919 if (smb_export) {
920 slirp_smb(s, mon, smb_export, smbsrv);
922 #endif
924 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, slirp_receive, NULL,
925 net_slirp_cleanup, s);
926 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
927 "net=%s, restricted=%c", inet_ntoa(net), restricted ? 'y' : 'n');
928 return 0;
931 static SlirpState *slirp_lookup(Monitor *mon, const char *vlan,
932 const char *stack)
934 VLANClientState *vc;
936 if (vlan) {
937 vc = qemu_find_vlan_client_by_name(mon, strtol(vlan, NULL, 0), stack);
938 if (!vc) {
939 return NULL;
941 if (strcmp(vc->model, "user")) {
942 monitor_printf(mon, "invalid device specified\n");
943 return NULL;
945 return vc->opaque;
946 } else {
947 if (TAILQ_EMPTY(&slirp_stacks)) {
948 monitor_printf(mon, "user mode network stack not in use\n");
949 return NULL;
951 return TAILQ_FIRST(&slirp_stacks);
955 void net_slirp_hostfwd_remove(Monitor *mon, const char *arg1,
956 const char *arg2, const char *arg3)
958 struct in_addr host_addr = { .s_addr = INADDR_ANY };
959 int host_port;
960 char buf[256] = "";
961 const char *src_str, *p;
962 SlirpState *s;
963 int is_udp = 0;
964 int err;
966 if (arg2) {
967 s = slirp_lookup(mon, arg1, arg2);
968 src_str = arg3;
969 } else {
970 s = slirp_lookup(mon, NULL, NULL);
971 src_str = arg1;
973 if (!s) {
974 return;
977 if (!src_str || !src_str[0])
978 goto fail_syntax;
980 p = src_str;
981 get_str_sep(buf, sizeof(buf), &p, ':');
983 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
984 is_udp = 0;
985 } else if (!strcmp(buf, "udp")) {
986 is_udp = 1;
987 } else {
988 goto fail_syntax;
991 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
992 goto fail_syntax;
994 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
995 goto fail_syntax;
998 host_port = atoi(p);
1000 err = slirp_remove_hostfwd(TAILQ_FIRST(&slirp_stacks)->slirp, is_udp,
1001 host_addr, host_port);
1003 monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
1004 err ? "removed" : "not found");
1005 return;
1007 fail_syntax:
1008 monitor_printf(mon, "invalid format\n");
1011 static void slirp_hostfwd(SlirpState *s, Monitor *mon, const char *redir_str,
1012 int legacy_format)
1014 struct in_addr host_addr = { .s_addr = INADDR_ANY };
1015 struct in_addr guest_addr = { .s_addr = 0 };
1016 int host_port, guest_port;
1017 const char *p;
1018 char buf[256];
1019 int is_udp;
1020 char *end;
1022 p = redir_str;
1023 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
1024 goto fail_syntax;
1026 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
1027 is_udp = 0;
1028 } else if (!strcmp(buf, "udp")) {
1029 is_udp = 1;
1030 } else {
1031 goto fail_syntax;
1034 if (!legacy_format) {
1035 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
1036 goto fail_syntax;
1038 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
1039 goto fail_syntax;
1043 if (get_str_sep(buf, sizeof(buf), &p, legacy_format ? ':' : '-') < 0) {
1044 goto fail_syntax;
1046 host_port = strtol(buf, &end, 0);
1047 if (*end != '\0' || host_port < 1 || host_port > 65535) {
1048 goto fail_syntax;
1051 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
1052 goto fail_syntax;
1054 if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) {
1055 goto fail_syntax;
1058 guest_port = strtol(p, &end, 0);
1059 if (*end != '\0' || guest_port < 1 || guest_port > 65535) {
1060 goto fail_syntax;
1063 if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr,
1064 guest_port) < 0) {
1065 config_error(mon, "could not set up host forwarding rule '%s'\n",
1066 redir_str);
1068 return;
1070 fail_syntax:
1071 config_error(mon, "invalid host forwarding rule '%s'\n", redir_str);
1074 void net_slirp_hostfwd_add(Monitor *mon, const char *arg1,
1075 const char *arg2, const char *arg3)
1077 const char *redir_str;
1078 SlirpState *s;
1080 if (arg2) {
1081 s = slirp_lookup(mon, arg1, arg2);
1082 redir_str = arg3;
1083 } else {
1084 s = slirp_lookup(mon, NULL, NULL);
1085 redir_str = arg1;
1087 if (s) {
1088 slirp_hostfwd(s, mon, redir_str, 0);
1093 void net_slirp_redir(const char *redir_str)
1095 struct slirp_config_str *config;
1097 if (TAILQ_EMPTY(&slirp_stacks)) {
1098 config = qemu_malloc(sizeof(*config));
1099 pstrcpy(config->str, sizeof(config->str), redir_str);
1100 config->flags = SLIRP_CFG_HOSTFWD | SLIRP_CFG_LEGACY;
1101 config->next = slirp_configs;
1102 slirp_configs = config;
1103 return;
1106 slirp_hostfwd(TAILQ_FIRST(&slirp_stacks), NULL, redir_str, 1);
1109 #ifndef _WIN32
1111 /* automatic user mode samba server configuration */
1112 static void slirp_smb_cleanup(SlirpState *s)
1114 char cmd[128];
1116 if (s->smb_dir[0] != '\0') {
1117 snprintf(cmd, sizeof(cmd), "rm -rf %s", s->smb_dir);
1118 system(cmd);
1119 s->smb_dir[0] = '\0';
1123 static void slirp_smb(SlirpState* s, Monitor *mon, const char *exported_dir,
1124 struct in_addr vserver_addr)
1126 static int instance;
1127 char smb_conf[128];
1128 char smb_cmdline[128];
1129 FILE *f;
1131 snprintf(s->smb_dir, sizeof(s->smb_dir), "/tmp/qemu-smb.%ld-%d",
1132 (long)getpid(), instance++);
1133 if (mkdir(s->smb_dir, 0700) < 0) {
1134 config_error(mon, "could not create samba server dir '%s'\n",
1135 s->smb_dir);
1136 return;
1138 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", s->smb_dir, "smb.conf");
1140 f = fopen(smb_conf, "w");
1141 if (!f) {
1142 slirp_smb_cleanup(s);
1143 config_error(mon, "could not create samba server "
1144 "configuration file '%s'\n", smb_conf);
1145 return;
1147 fprintf(f,
1148 "[global]\n"
1149 "private dir=%s\n"
1150 "smb ports=0\n"
1151 "socket address=127.0.0.1\n"
1152 "pid directory=%s\n"
1153 "lock directory=%s\n"
1154 "log file=%s/log.smbd\n"
1155 "smb passwd file=%s/smbpasswd\n"
1156 "security = share\n"
1157 "[qemu]\n"
1158 "path=%s\n"
1159 "read only=no\n"
1160 "guest ok=yes\n",
1161 s->smb_dir,
1162 s->smb_dir,
1163 s->smb_dir,
1164 s->smb_dir,
1165 s->smb_dir,
1166 exported_dir
1168 fclose(f);
1170 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
1171 SMBD_COMMAND, smb_conf);
1173 if (slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 139) < 0) {
1174 slirp_smb_cleanup(s);
1175 config_error(mon, "conflicting/invalid smbserver address\n");
1179 /* automatic user mode samba server configuration (legacy interface) */
1180 void net_slirp_smb(const char *exported_dir)
1182 struct in_addr vserver_addr = { .s_addr = 0 };
1184 if (legacy_smb_export) {
1185 fprintf(stderr, "-smb given twice\n");
1186 exit(1);
1188 legacy_smb_export = exported_dir;
1189 if (!TAILQ_EMPTY(&slirp_stacks)) {
1190 slirp_smb(TAILQ_FIRST(&slirp_stacks), NULL, exported_dir,
1191 vserver_addr);
1195 #endif /* !defined(_WIN32) */
1197 struct GuestFwd {
1198 CharDriverState *hd;
1199 struct in_addr server;
1200 int port;
1201 Slirp *slirp;
1204 static int guestfwd_can_read(void *opaque)
1206 struct GuestFwd *fwd = opaque;
1207 return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port);
1210 static void guestfwd_read(void *opaque, const uint8_t *buf, int size)
1212 struct GuestFwd *fwd = opaque;
1213 slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size);
1216 static void slirp_guestfwd(SlirpState *s, Monitor *mon, const char *config_str,
1217 int legacy_format)
1219 struct in_addr server = { .s_addr = 0 };
1220 struct GuestFwd *fwd;
1221 const char *p;
1222 char buf[128];
1223 char *end;
1224 int port;
1226 p = config_str;
1227 if (legacy_format) {
1228 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
1229 goto fail_syntax;
1231 } else {
1232 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
1233 goto fail_syntax;
1235 if (strcmp(buf, "tcp") && buf[0] != '\0') {
1236 goto fail_syntax;
1238 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
1239 goto fail_syntax;
1241 if (buf[0] != '\0' && !inet_aton(buf, &server)) {
1242 goto fail_syntax;
1244 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
1245 goto fail_syntax;
1248 port = strtol(buf, &end, 10);
1249 if (*end != '\0' || port < 1 || port > 65535) {
1250 goto fail_syntax;
1253 fwd = qemu_malloc(sizeof(struct GuestFwd));
1254 snprintf(buf, sizeof(buf), "guestfwd.tcp:%d", port);
1255 fwd->hd = qemu_chr_open(buf, p, NULL);
1256 if (!fwd->hd) {
1257 config_error(mon, "could not open guest forwarding device '%s'\n",
1258 buf);
1259 qemu_free(fwd);
1260 return;
1263 if (slirp_add_exec(s->slirp, 3, fwd->hd, &server, port) < 0) {
1264 config_error(mon, "conflicting/invalid host:port in guest forwarding "
1265 "rule '%s'\n", config_str);
1266 qemu_free(fwd);
1267 return;
1269 fwd->server = server;
1270 fwd->port = port;
1271 fwd->slirp = s->slirp;
1273 qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read,
1274 NULL, fwd);
1275 return;
1277 fail_syntax:
1278 config_error(mon, "invalid guest forwarding rule '%s'\n", config_str);
1281 void do_info_usernet(Monitor *mon)
1283 SlirpState *s;
1285 TAILQ_FOREACH(s, &slirp_stacks, entry) {
1286 monitor_printf(mon, "VLAN %d (%s):\n", s->vc->vlan->id, s->vc->name);
1287 slirp_connection_info(s->slirp, mon);
1291 #endif /* CONFIG_SLIRP */
1293 #ifdef _WIN32
1295 int tap_has_vnet_hdr(void *opaque)
1297 return 0;
1300 void tap_using_vnet_hdr(void *opaque, int using_vnet_hdr)
1304 #else /* !defined(_WIN32) */
1306 /* Maximum GSO packet size (64k) plus plenty of room for
1307 * the ethernet and virtio_net headers
1309 #define TAP_BUFSIZE (4096 + 65536)
1311 #ifdef IFF_VNET_HDR
1312 #include <linux/virtio_net.h>
1313 #endif
1315 typedef struct TAPState {
1316 VLANClientState *vc;
1317 int fd;
1318 char down_script[1024];
1319 char down_script_arg[128];
1320 uint8_t buf[TAP_BUFSIZE];
1321 unsigned int read_poll : 1;
1322 unsigned int write_poll : 1;
1323 unsigned int has_vnet_hdr : 1;
1324 unsigned int using_vnet_hdr : 1;
1325 } TAPState;
1327 static int launch_script(const char *setup_script, const char *ifname, int fd);
1329 static int tap_can_send(void *opaque);
1330 static void tap_send(void *opaque);
1331 static void tap_writable(void *opaque);
1333 static void tap_update_fd_handler(TAPState *s)
1335 qemu_set_fd_handler2(s->fd,
1336 s->read_poll ? tap_can_send : NULL,
1337 s->read_poll ? tap_send : NULL,
1338 s->write_poll ? tap_writable : NULL,
1342 static void tap_read_poll(TAPState *s, int enable)
1344 s->read_poll = !!enable;
1345 tap_update_fd_handler(s);
1348 static void tap_write_poll(TAPState *s, int enable)
1350 s->write_poll = !!enable;
1351 tap_update_fd_handler(s);
1354 static void tap_writable(void *opaque)
1356 TAPState *s = opaque;
1358 tap_write_poll(s, 0);
1360 qemu_flush_queued_packets(s->vc);
1363 static ssize_t tap_receive_iov(VLANClientState *vc, const struct iovec *iov,
1364 int iovcnt)
1366 TAPState *s = vc->opaque;
1367 ssize_t len;
1369 do {
1370 len = writev(s->fd, iov, iovcnt);
1371 } while (len == -1 && errno == EINTR);
1373 if (len == -1 && errno == EAGAIN) {
1374 tap_write_poll(s, 1);
1375 return 0;
1378 return len;
1381 static ssize_t tap_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
1383 struct iovec iov[2];
1384 int i = 0;
1386 #ifdef IFF_VNET_HDR
1387 TAPState *s = vc->opaque;
1388 struct virtio_net_hdr hdr = { 0, };
1390 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
1391 iov[i].iov_base = &hdr;
1392 iov[i].iov_len = sizeof(hdr);
1393 i++;
1395 #endif
1397 iov[i].iov_base = (char *) buf;
1398 iov[i].iov_len = size;
1399 i++;
1401 return tap_receive_iov(vc, iov, i);
1404 static ssize_t tap_receive_raw(VLANClientState *vc, const uint8_t *buf, size_t size)
1406 struct iovec iov[2];
1407 int i = 0;
1409 #ifdef IFF_VNET_HDR
1410 TAPState *s = vc->opaque;
1411 struct virtio_net_hdr hdr = { 0, };
1413 if (s->has_vnet_hdr && s->using_vnet_hdr) {
1414 iov[i].iov_base = &hdr;
1415 iov[i].iov_len = sizeof(hdr);
1416 i++;
1418 #endif
1420 iov[i].iov_base = (char *) buf;
1421 iov[i].iov_len = size;
1422 i++;
1424 return tap_receive_iov(vc, iov, i);
1427 static int tap_can_send(void *opaque)
1429 TAPState *s = opaque;
1431 return qemu_can_send_packet(s->vc);
1434 #ifdef __sun__
1435 static ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
1437 struct strbuf sbuf;
1438 int f = 0;
1440 sbuf.maxlen = maxlen;
1441 sbuf.buf = (char *)buf;
1443 return getmsg(tapfd, NULL, &sbuf, &f) >= 0 ? sbuf.len : -1;
1445 #else
1446 static ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
1448 return read(tapfd, buf, maxlen);
1450 #endif
1452 static void tap_send_completed(VLANClientState *vc, ssize_t len)
1454 TAPState *s = vc->opaque;
1455 tap_read_poll(s, 1);
1458 static void tap_send(void *opaque)
1460 TAPState *s = opaque;
1461 int size;
1463 do {
1464 uint8_t *buf = s->buf;
1466 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
1467 if (size <= 0) {
1468 break;
1471 #ifdef IFF_VNET_HDR
1472 if (s->has_vnet_hdr && !s->using_vnet_hdr) {
1473 buf += sizeof(struct virtio_net_hdr);
1474 size -= sizeof(struct virtio_net_hdr);
1476 #endif
1478 size = qemu_send_packet_async(s->vc, buf, size, tap_send_completed);
1479 if (size == 0) {
1480 tap_read_poll(s, 0);
1482 } while (size > 0);
1485 #ifdef TUNSETSNDBUF
1486 /* sndbuf should be set to a value lower than the tx queue
1487 * capacity of any destination network interface.
1488 * Ethernet NICs generally have txqueuelen=1000, so 1Mb is
1489 * a good default, given a 1500 byte MTU.
1491 #define TAP_DEFAULT_SNDBUF 1024*1024
1493 static void tap_set_sndbuf(TAPState *s, const char *sndbuf_str, Monitor *mon)
1495 int sndbuf = TAP_DEFAULT_SNDBUF;
1497 if (sndbuf_str) {
1498 sndbuf = atoi(sndbuf_str);
1501 if (!sndbuf) {
1502 sndbuf = INT_MAX;
1505 if (ioctl(s->fd, TUNSETSNDBUF, &sndbuf) == -1 && sndbuf_str) {
1506 config_error(mon, "TUNSETSNDBUF ioctl failed: %s\n",
1507 strerror(errno));
1510 #else
1511 static void tap_set_sndbuf(TAPState *s, const char *sndbuf_str, Monitor *mon)
1513 if (sndbuf_str) {
1514 config_error(mon, "No '-net tap,sndbuf=<nbytes>' support available\n");
1517 #endif /* TUNSETSNDBUF */
1519 int tap_has_vnet_hdr(void *opaque)
1521 VLANClientState *vc = opaque;
1522 TAPState *s = vc->opaque;
1524 if (vc->receive != tap_receive)
1525 return 0;
1527 return s ? s->has_vnet_hdr : 0;
1530 void tap_using_vnet_hdr(void *opaque, int using_vnet_hdr)
1532 VLANClientState *vc = opaque;
1533 TAPState *s = vc->opaque;
1535 if (vc->receive != tap_receive)
1536 return;
1538 if (!s || !s->has_vnet_hdr)
1539 return;
1541 s->using_vnet_hdr = using_vnet_hdr != 0;
1544 static int tap_probe_vnet_hdr(int fd)
1546 #if defined(TUNGETIFF) && defined(IFF_VNET_HDR)
1547 struct ifreq ifr;
1549 if (ioctl(fd, TUNGETIFF, &ifr) != 0) {
1550 fprintf(stderr, "TUNGETIFF ioctl() failed: %s\n", strerror(errno));
1551 return 0;
1554 return ifr.ifr_flags & IFF_VNET_HDR;
1555 #else
1556 return 0;
1557 #endif
1560 #ifdef TUNSETOFFLOAD
1561 static void tap_set_offload(VLANClientState *vc, int csum, int tso4, int tso6,
1562 int ecn)
1564 TAPState *s = vc->opaque;
1565 unsigned int offload = 0;
1567 if (csum) {
1568 offload |= TUN_F_CSUM;
1569 if (tso4)
1570 offload |= TUN_F_TSO4;
1571 if (tso6)
1572 offload |= TUN_F_TSO6;
1573 if ((tso4 || tso6) && ecn)
1574 offload |= TUN_F_TSO_ECN;
1577 if (ioctl(s->fd, TUNSETOFFLOAD, offload) != 0)
1578 fprintf(stderr, "TUNSETOFFLOAD ioctl() failed: %s\n",
1579 strerror(errno));
1581 #endif /* TUNSETOFFLOAD */
1583 static void tap_cleanup(VLANClientState *vc)
1585 TAPState *s = vc->opaque;
1587 qemu_purge_queued_packets(vc);
1589 if (s->down_script[0])
1590 launch_script(s->down_script, s->down_script_arg, s->fd);
1592 tap_read_poll(s, 0);
1593 tap_write_poll(s, 0);
1594 close(s->fd);
1595 qemu_free(s);
1598 /* fd support */
1600 static TAPState *net_tap_fd_init(VLANState *vlan,
1601 const char *model,
1602 const char *name,
1603 int fd,
1604 int vnet_hdr)
1606 TAPState *s;
1608 s = qemu_mallocz(sizeof(TAPState));
1609 s->fd = fd;
1610 s->has_vnet_hdr = vnet_hdr != 0;
1611 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, tap_receive,
1612 tap_receive_iov, tap_cleanup, s);
1613 s->vc->receive_raw = tap_receive_raw;
1614 #ifdef TUNSETOFFLOAD
1615 s->vc->set_offload = tap_set_offload;
1616 tap_set_offload(s->vc, 0, 0, 0, 0);
1617 #endif
1618 tap_read_poll(s, 1);
1619 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd);
1620 return s;
1623 #if defined (HOST_BSD) || defined (__FreeBSD_kernel__)
1624 static int tap_open(char *ifname, int ifname_size)
1626 int fd;
1627 char *dev;
1628 struct stat s;
1630 TFR(fd = open("/dev/tap", O_RDWR));
1631 if (fd < 0) {
1632 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
1633 return -1;
1636 fstat(fd, &s);
1637 dev = devname(s.st_rdev, S_IFCHR);
1638 pstrcpy(ifname, ifname_size, dev);
1640 fcntl(fd, F_SETFL, O_NONBLOCK);
1641 return fd;
1643 #elif defined(__sun__)
1644 #define TUNNEWPPA (('T'<<16) | 0x0001)
1646 * Allocate TAP device, returns opened fd.
1647 * Stores dev name in the first arg(must be large enough).
1649 static int tap_alloc(char *dev, size_t dev_size)
1651 int tap_fd, if_fd, ppa = -1;
1652 static int ip_fd = 0;
1653 char *ptr;
1655 static int arp_fd = 0;
1656 int ip_muxid, arp_muxid;
1657 struct strioctl strioc_if, strioc_ppa;
1658 int link_type = I_PLINK;;
1659 struct lifreq ifr;
1660 char actual_name[32] = "";
1662 memset(&ifr, 0x0, sizeof(ifr));
1664 if( *dev ){
1665 ptr = dev;
1666 while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
1667 ppa = atoi(ptr);
1670 /* Check if IP device was opened */
1671 if( ip_fd )
1672 close(ip_fd);
1674 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
1675 if (ip_fd < 0) {
1676 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
1677 return -1;
1680 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
1681 if (tap_fd < 0) {
1682 syslog(LOG_ERR, "Can't open /dev/tap");
1683 return -1;
1686 /* Assign a new PPA and get its unit number. */
1687 strioc_ppa.ic_cmd = TUNNEWPPA;
1688 strioc_ppa.ic_timout = 0;
1689 strioc_ppa.ic_len = sizeof(ppa);
1690 strioc_ppa.ic_dp = (char *)&ppa;
1691 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
1692 syslog (LOG_ERR, "Can't assign new interface");
1694 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
1695 if (if_fd < 0) {
1696 syslog(LOG_ERR, "Can't open /dev/tap (2)");
1697 return -1;
1699 if(ioctl(if_fd, I_PUSH, "ip") < 0){
1700 syslog(LOG_ERR, "Can't push IP module");
1701 return -1;
1704 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
1705 syslog(LOG_ERR, "Can't get flags\n");
1707 snprintf (actual_name, 32, "tap%d", ppa);
1708 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1710 ifr.lifr_ppa = ppa;
1711 /* Assign ppa according to the unit number returned by tun device */
1713 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
1714 syslog (LOG_ERR, "Can't set PPA %d", ppa);
1715 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
1716 syslog (LOG_ERR, "Can't get flags\n");
1717 /* Push arp module to if_fd */
1718 if (ioctl (if_fd, I_PUSH, "arp") < 0)
1719 syslog (LOG_ERR, "Can't push ARP module (2)");
1721 /* Push arp module to ip_fd */
1722 if (ioctl (ip_fd, I_POP, NULL) < 0)
1723 syslog (LOG_ERR, "I_POP failed\n");
1724 if (ioctl (ip_fd, I_PUSH, "arp") < 0)
1725 syslog (LOG_ERR, "Can't push ARP module (3)\n");
1726 /* Open arp_fd */
1727 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
1728 if (arp_fd < 0)
1729 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
1731 /* Set ifname to arp */
1732 strioc_if.ic_cmd = SIOCSLIFNAME;
1733 strioc_if.ic_timout = 0;
1734 strioc_if.ic_len = sizeof(ifr);
1735 strioc_if.ic_dp = (char *)&ifr;
1736 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
1737 syslog (LOG_ERR, "Can't set ifname to arp\n");
1740 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
1741 syslog(LOG_ERR, "Can't link TAP device to IP");
1742 return -1;
1745 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
1746 syslog (LOG_ERR, "Can't link TAP device to ARP");
1748 close (if_fd);
1750 memset(&ifr, 0x0, sizeof(ifr));
1751 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1752 ifr.lifr_ip_muxid = ip_muxid;
1753 ifr.lifr_arp_muxid = arp_muxid;
1755 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
1757 ioctl (ip_fd, I_PUNLINK , arp_muxid);
1758 ioctl (ip_fd, I_PUNLINK, ip_muxid);
1759 syslog (LOG_ERR, "Can't set multiplexor id");
1762 snprintf(dev, dev_size, "tap%d", ppa);
1763 return tap_fd;
1766 static int tap_open(char *ifname, int ifname_size, int *vnet_hdr)
1768 char dev[10]="";
1769 int fd;
1770 if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
1771 fprintf(stderr, "Cannot allocate TAP device\n");
1772 return -1;
1774 pstrcpy(ifname, ifname_size, dev);
1775 fcntl(fd, F_SETFL, O_NONBLOCK);
1776 return fd;
1778 #elif defined (_AIX)
1779 static int tap_open(char *ifname, int ifname_size)
1781 fprintf (stderr, "no tap on AIX\n");
1782 return -1;
1784 #else
1785 static int tap_open(char *ifname, int ifname_size, int *vnet_hdr)
1787 struct ifreq ifr;
1788 int fd, ret;
1790 TFR(fd = open("/dev/net/tun", O_RDWR));
1791 if (fd < 0) {
1792 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1793 return -1;
1795 memset(&ifr, 0, sizeof(ifr));
1796 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1798 #if defined(TUNGETFEATURES) && defined(IFF_VNET_HDR)
1800 unsigned int features;
1802 if (ioctl(fd, TUNGETFEATURES, &features) == 0 &&
1803 features & IFF_VNET_HDR) {
1804 *vnet_hdr = 1;
1805 ifr.ifr_flags |= IFF_VNET_HDR;
1808 #endif
1810 if (ifname[0] != '\0')
1811 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
1812 else
1813 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
1814 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1815 if (ret != 0) {
1816 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1817 close(fd);
1818 return -1;
1820 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1821 fcntl(fd, F_SETFL, O_NONBLOCK);
1822 return fd;
1824 #endif
1826 static int launch_script(const char *setup_script, const char *ifname, int fd)
1828 sigset_t oldmask, mask;
1829 int pid, status;
1830 char *args[3];
1831 char **parg;
1833 sigemptyset(&mask);
1834 sigaddset(&mask, SIGCHLD);
1835 sigprocmask(SIG_BLOCK, &mask, &oldmask);
1837 /* try to launch network script */
1838 pid = fork();
1839 if (pid == 0) {
1840 int open_max = sysconf(_SC_OPEN_MAX), i;
1842 for (i = 0; i < open_max; i++) {
1843 if (i != STDIN_FILENO &&
1844 i != STDOUT_FILENO &&
1845 i != STDERR_FILENO &&
1846 i != fd) {
1847 close(i);
1850 parg = args;
1851 *parg++ = (char *)setup_script;
1852 *parg++ = (char *)ifname;
1853 *parg++ = NULL;
1854 execv(setup_script, args);
1855 _exit(1);
1856 } else if (pid > 0) {
1857 while (waitpid(pid, &status, 0) != pid) {
1858 /* loop */
1860 sigprocmask(SIG_SETMASK, &oldmask, NULL);
1862 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1863 return 0;
1866 fprintf(stderr, "%s: could not launch network script\n", setup_script);
1867 return -1;
1870 static TAPState *net_tap_init(VLANState *vlan, const char *model,
1871 const char *name, const char *ifname1,
1872 const char *setup_script, const char *down_script)
1874 TAPState *s;
1875 int fd;
1876 int vnet_hdr;
1877 char ifname[128];
1879 if (ifname1 != NULL)
1880 pstrcpy(ifname, sizeof(ifname), ifname1);
1881 else
1882 ifname[0] = '\0';
1883 vnet_hdr = 0;
1884 TFR(fd = tap_open(ifname, sizeof(ifname), &vnet_hdr));
1885 if (fd < 0)
1886 return NULL;
1888 if (!setup_script || !strcmp(setup_script, "no"))
1889 setup_script = "";
1890 if (setup_script[0] != '\0' &&
1891 launch_script(setup_script, ifname, fd)) {
1892 return NULL;
1894 s = net_tap_fd_init(vlan, model, name, fd, vnet_hdr);
1895 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1896 "ifname=%s,script=%s,downscript=%s",
1897 ifname, setup_script, down_script);
1898 if (down_script && strcmp(down_script, "no")) {
1899 snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
1900 snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
1902 return s;
1905 #endif /* !_WIN32 */
1907 #if defined(CONFIG_VDE)
1908 typedef struct VDEState {
1909 VLANClientState *vc;
1910 VDECONN *vde;
1911 } VDEState;
1913 static void vde_to_qemu(void *opaque)
1915 VDEState *s = opaque;
1916 uint8_t buf[4096];
1917 int size;
1919 size = vde_recv(s->vde, (char *)buf, sizeof(buf), 0);
1920 if (size > 0) {
1921 qemu_send_packet(s->vc, buf, size);
1925 static ssize_t vde_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
1927 VDEState *s = vc->opaque;
1928 ssize_t ret;
1930 do {
1931 ret = vde_send(s->vde, (const char *)buf, size, 0);
1932 } while (ret < 0 && errno == EINTR);
1934 return ret;
1937 static void vde_cleanup(VLANClientState *vc)
1939 VDEState *s = vc->opaque;
1940 qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL);
1941 vde_close(s->vde);
1942 qemu_free(s);
1945 static int net_vde_init(VLANState *vlan, const char *model,
1946 const char *name, const char *sock,
1947 int port, const char *group, int mode)
1949 VDEState *s;
1950 char *init_group = strlen(group) ? (char *)group : NULL;
1951 char *init_sock = strlen(sock) ? (char *)sock : NULL;
1953 struct vde_open_args args = {
1954 .port = port,
1955 .group = init_group,
1956 .mode = mode,
1959 s = qemu_mallocz(sizeof(VDEState));
1960 s->vde = vde_open(init_sock, (char *)"QEMU", &args);
1961 if (!s->vde){
1962 free(s);
1963 return -1;
1965 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, vde_receive,
1966 NULL, vde_cleanup, s);
1967 qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
1968 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d",
1969 sock, vde_datafd(s->vde));
1970 return 0;
1972 #endif
1974 /* network connection */
1975 typedef struct NetSocketState {
1976 VLANClientState *vc;
1977 int fd;
1978 int state; /* 0 = getting length, 1 = getting data */
1979 unsigned int index;
1980 unsigned int packet_len;
1981 uint8_t buf[4096];
1982 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1983 } NetSocketState;
1985 typedef struct NetSocketListenState {
1986 VLANState *vlan;
1987 char *model;
1988 char *name;
1989 int fd;
1990 } NetSocketListenState;
1992 /* XXX: we consider we can send the whole packet without blocking */
1993 static ssize_t net_socket_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
1995 NetSocketState *s = vc->opaque;
1996 uint32_t len;
1997 len = htonl(size);
1999 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
2000 return send_all(s->fd, buf, size);
2003 static ssize_t net_socket_receive_dgram(VLANClientState *vc, const uint8_t *buf, size_t size)
2005 NetSocketState *s = vc->opaque;
2007 return sendto(s->fd, (const void *)buf, size, 0,
2008 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
2011 static void net_socket_send(void *opaque)
2013 NetSocketState *s = opaque;
2014 int size, err;
2015 unsigned l;
2016 uint8_t buf1[4096];
2017 const uint8_t *buf;
2019 size = recv(s->fd, (void *)buf1, sizeof(buf1), 0);
2020 if (size < 0) {
2021 err = socket_error();
2022 if (err != EWOULDBLOCK)
2023 goto eoc;
2024 } else if (size == 0) {
2025 /* end of connection */
2026 eoc:
2027 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2028 closesocket(s->fd);
2029 return;
2031 buf = buf1;
2032 while (size > 0) {
2033 /* reassemble a packet from the network */
2034 switch(s->state) {
2035 case 0:
2036 l = 4 - s->index;
2037 if (l > size)
2038 l = size;
2039 memcpy(s->buf + s->index, buf, l);
2040 buf += l;
2041 size -= l;
2042 s->index += l;
2043 if (s->index == 4) {
2044 /* got length */
2045 s->packet_len = ntohl(*(uint32_t *)s->buf);
2046 s->index = 0;
2047 s->state = 1;
2049 break;
2050 case 1:
2051 l = s->packet_len - s->index;
2052 if (l > size)
2053 l = size;
2054 if (s->index + l <= sizeof(s->buf)) {
2055 memcpy(s->buf + s->index, buf, l);
2056 } else {
2057 fprintf(stderr, "serious error: oversized packet received,"
2058 "connection terminated.\n");
2059 s->state = 0;
2060 goto eoc;
2063 s->index += l;
2064 buf += l;
2065 size -= l;
2066 if (s->index >= s->packet_len) {
2067 qemu_send_packet(s->vc, s->buf, s->packet_len);
2068 s->index = 0;
2069 s->state = 0;
2071 break;
2076 static void net_socket_send_dgram(void *opaque)
2078 NetSocketState *s = opaque;
2079 int size;
2081 size = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0);
2082 if (size < 0)
2083 return;
2084 if (size == 0) {
2085 /* end of connection */
2086 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2087 return;
2089 qemu_send_packet(s->vc, s->buf, size);
2092 static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
2094 struct ip_mreq imr;
2095 int fd;
2096 int val, ret;
2097 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
2098 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
2099 inet_ntoa(mcastaddr->sin_addr),
2100 (int)ntohl(mcastaddr->sin_addr.s_addr));
2101 return -1;
2104 fd = socket(PF_INET, SOCK_DGRAM, 0);
2105 if (fd < 0) {
2106 perror("socket(PF_INET, SOCK_DGRAM)");
2107 return -1;
2110 val = 1;
2111 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
2112 (const char *)&val, sizeof(val));
2113 if (ret < 0) {
2114 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
2115 goto fail;
2118 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
2119 if (ret < 0) {
2120 perror("bind");
2121 goto fail;
2124 /* Add host to multicast group */
2125 imr.imr_multiaddr = mcastaddr->sin_addr;
2126 imr.imr_interface.s_addr = htonl(INADDR_ANY);
2128 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
2129 (const char *)&imr, sizeof(struct ip_mreq));
2130 if (ret < 0) {
2131 perror("setsockopt(IP_ADD_MEMBERSHIP)");
2132 goto fail;
2135 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
2136 val = 1;
2137 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
2138 (const char *)&val, sizeof(val));
2139 if (ret < 0) {
2140 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
2141 goto fail;
2144 socket_set_nonblock(fd);
2145 return fd;
2146 fail:
2147 if (fd >= 0)
2148 closesocket(fd);
2149 return -1;
2152 static void net_socket_cleanup(VLANClientState *vc)
2154 NetSocketState *s = vc->opaque;
2155 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
2156 close(s->fd);
2157 qemu_free(s);
2160 static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
2161 const char *model,
2162 const char *name,
2163 int fd, int is_connected)
2165 struct sockaddr_in saddr;
2166 int newfd;
2167 socklen_t saddr_len;
2168 NetSocketState *s;
2170 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
2171 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
2172 * by ONLY ONE process: we must "clone" this dgram socket --jjo
2175 if (is_connected) {
2176 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
2177 /* must be bound */
2178 if (saddr.sin_addr.s_addr==0) {
2179 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
2180 fd);
2181 return NULL;
2183 /* clone dgram socket */
2184 newfd = net_socket_mcast_create(&saddr);
2185 if (newfd < 0) {
2186 /* error already reported by net_socket_mcast_create() */
2187 close(fd);
2188 return NULL;
2190 /* clone newfd to fd, close newfd */
2191 dup2(newfd, fd);
2192 close(newfd);
2194 } else {
2195 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
2196 fd, strerror(errno));
2197 return NULL;
2201 s = qemu_mallocz(sizeof(NetSocketState));
2202 s->fd = fd;
2204 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, net_socket_receive_dgram,
2205 NULL, net_socket_cleanup, s);
2206 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
2208 /* mcast: save bound address as dst */
2209 if (is_connected) s->dgram_dst=saddr;
2211 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
2212 "socket: fd=%d (%s mcast=%s:%d)",
2213 fd, is_connected? "cloned" : "",
2214 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
2215 return s;
2218 static void net_socket_connect(void *opaque)
2220 NetSocketState *s = opaque;
2221 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
2224 static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
2225 const char *model,
2226 const char *name,
2227 int fd, int is_connected)
2229 NetSocketState *s;
2230 s = qemu_mallocz(sizeof(NetSocketState));
2231 s->fd = fd;
2232 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, net_socket_receive,
2233 NULL, net_socket_cleanup, s);
2234 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
2235 "socket: fd=%d", fd);
2236 if (is_connected) {
2237 net_socket_connect(s);
2238 } else {
2239 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
2241 return s;
2244 static NetSocketState *net_socket_fd_init(VLANState *vlan,
2245 const char *model, const char *name,
2246 int fd, int is_connected)
2248 int so_type=-1, optlen=sizeof(so_type);
2250 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
2251 (socklen_t *)&optlen)< 0) {
2252 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
2253 return NULL;
2255 switch(so_type) {
2256 case SOCK_DGRAM:
2257 return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected);
2258 case SOCK_STREAM:
2259 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
2260 default:
2261 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
2262 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
2263 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
2265 return NULL;
2268 static void net_socket_accept(void *opaque)
2270 NetSocketListenState *s = opaque;
2271 NetSocketState *s1;
2272 struct sockaddr_in saddr;
2273 socklen_t len;
2274 int fd;
2276 for(;;) {
2277 len = sizeof(saddr);
2278 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
2279 if (fd < 0 && errno != EINTR) {
2280 return;
2281 } else if (fd >= 0) {
2282 break;
2285 s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1);
2286 if (!s1) {
2287 closesocket(fd);
2288 } else {
2289 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
2290 "socket: connection from %s:%d",
2291 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
2295 static int net_socket_listen_init(VLANState *vlan,
2296 const char *model,
2297 const char *name,
2298 const char *host_str)
2300 NetSocketListenState *s;
2301 int fd, val, ret;
2302 struct sockaddr_in saddr;
2304 if (parse_host_port(&saddr, host_str) < 0)
2305 return -1;
2307 s = qemu_mallocz(sizeof(NetSocketListenState));
2309 fd = socket(PF_INET, SOCK_STREAM, 0);
2310 if (fd < 0) {
2311 perror("socket");
2312 return -1;
2314 socket_set_nonblock(fd);
2316 /* allow fast reuse */
2317 val = 1;
2318 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
2320 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
2321 if (ret < 0) {
2322 perror("bind");
2323 return -1;
2325 ret = listen(fd, 0);
2326 if (ret < 0) {
2327 perror("listen");
2328 return -1;
2330 s->vlan = vlan;
2331 s->model = strdup(model);
2332 s->name = name ? strdup(name) : NULL;
2333 s->fd = fd;
2334 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
2335 return 0;
2338 static int net_socket_connect_init(VLANState *vlan,
2339 const char *model,
2340 const char *name,
2341 const char *host_str)
2343 NetSocketState *s;
2344 int fd, connected, ret, err;
2345 struct sockaddr_in saddr;
2347 if (parse_host_port(&saddr, host_str) < 0)
2348 return -1;
2350 fd = socket(PF_INET, SOCK_STREAM, 0);
2351 if (fd < 0) {
2352 perror("socket");
2353 return -1;
2355 socket_set_nonblock(fd);
2357 connected = 0;
2358 for(;;) {
2359 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
2360 if (ret < 0) {
2361 err = socket_error();
2362 if (err == EINTR || err == EWOULDBLOCK) {
2363 } else if (err == EINPROGRESS) {
2364 break;
2365 #ifdef _WIN32
2366 } else if (err == WSAEALREADY) {
2367 break;
2368 #endif
2369 } else {
2370 perror("connect");
2371 closesocket(fd);
2372 return -1;
2374 } else {
2375 connected = 1;
2376 break;
2379 s = net_socket_fd_init(vlan, model, name, fd, connected);
2380 if (!s)
2381 return -1;
2382 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
2383 "socket: connect to %s:%d",
2384 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
2385 return 0;
2388 static int net_socket_mcast_init(VLANState *vlan,
2389 const char *model,
2390 const char *name,
2391 const char *host_str)
2393 NetSocketState *s;
2394 int fd;
2395 struct sockaddr_in saddr;
2397 if (parse_host_port(&saddr, host_str) < 0)
2398 return -1;
2401 fd = net_socket_mcast_create(&saddr);
2402 if (fd < 0)
2403 return -1;
2405 s = net_socket_fd_init(vlan, model, name, fd, 0);
2406 if (!s)
2407 return -1;
2409 s->dgram_dst = saddr;
2411 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
2412 "socket: mcast=%s:%d",
2413 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
2414 return 0;
2418 typedef struct DumpState {
2419 VLANClientState *pcap_vc;
2420 int fd;
2421 int pcap_caplen;
2422 } DumpState;
2424 #define PCAP_MAGIC 0xa1b2c3d4
2426 struct pcap_file_hdr {
2427 uint32_t magic;
2428 uint16_t version_major;
2429 uint16_t version_minor;
2430 int32_t thiszone;
2431 uint32_t sigfigs;
2432 uint32_t snaplen;
2433 uint32_t linktype;
2436 struct pcap_sf_pkthdr {
2437 struct {
2438 int32_t tv_sec;
2439 int32_t tv_usec;
2440 } ts;
2441 uint32_t caplen;
2442 uint32_t len;
2445 static ssize_t dump_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
2447 DumpState *s = vc->opaque;
2448 struct pcap_sf_pkthdr hdr;
2449 int64_t ts;
2450 int caplen;
2452 /* Early return in case of previous error. */
2453 if (s->fd < 0) {
2454 return size;
2457 ts = muldiv64(qemu_get_clock(vm_clock), 1000000, ticks_per_sec);
2458 caplen = size > s->pcap_caplen ? s->pcap_caplen : size;
2460 hdr.ts.tv_sec = ts / 1000000;
2461 hdr.ts.tv_usec = ts % 1000000;
2462 hdr.caplen = caplen;
2463 hdr.len = size;
2464 if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) ||
2465 write(s->fd, buf, caplen) != caplen) {
2466 qemu_log("-net dump write error - stop dump\n");
2467 close(s->fd);
2468 s->fd = -1;
2471 return size;
2474 static void net_dump_cleanup(VLANClientState *vc)
2476 DumpState *s = vc->opaque;
2478 close(s->fd);
2479 qemu_free(s);
2482 static int net_dump_init(Monitor *mon, VLANState *vlan, const char *device,
2483 const char *name, const char *filename, int len)
2485 struct pcap_file_hdr hdr;
2486 DumpState *s;
2488 s = qemu_malloc(sizeof(DumpState));
2490 s->fd = open(filename, O_CREAT | O_WRONLY | O_BINARY, 0644);
2491 if (s->fd < 0) {
2492 config_error(mon, "-net dump: can't open %s\n", filename);
2493 return -1;
2496 s->pcap_caplen = len;
2498 hdr.magic = PCAP_MAGIC;
2499 hdr.version_major = 2;
2500 hdr.version_minor = 4;
2501 hdr.thiszone = 0;
2502 hdr.sigfigs = 0;
2503 hdr.snaplen = s->pcap_caplen;
2504 hdr.linktype = 1;
2506 if (write(s->fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
2507 config_error(mon, "-net dump write error: %s\n", strerror(errno));
2508 close(s->fd);
2509 qemu_free(s);
2510 return -1;
2513 s->pcap_vc = qemu_new_vlan_client(vlan, device, name, NULL, dump_receive, NULL,
2514 net_dump_cleanup, s);
2515 snprintf(s->pcap_vc->info_str, sizeof(s->pcap_vc->info_str),
2516 "dump to %s (len=%d)", filename, len);
2517 return 0;
2520 /* find or alloc a new VLAN */
2521 VLANState *qemu_find_vlan(int id, int allocate)
2523 VLANState **pvlan, *vlan;
2524 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2525 if (vlan->id == id)
2526 return vlan;
2528 if (!allocate) {
2529 return NULL;
2531 vlan = qemu_mallocz(sizeof(VLANState));
2532 vlan->id = id;
2533 vlan->next = NULL;
2534 pvlan = &first_vlan;
2535 while (*pvlan != NULL)
2536 pvlan = &(*pvlan)->next;
2537 *pvlan = vlan;
2538 return vlan;
2541 static int nic_get_free_idx(void)
2543 int index;
2545 for (index = 0; index < MAX_NICS; index++)
2546 if (!nd_table[index].used)
2547 return index;
2548 return -1;
2551 void qemu_check_nic_model(NICInfo *nd, const char *model)
2553 const char *models[2];
2555 models[0] = model;
2556 models[1] = NULL;
2558 qemu_check_nic_model_list(nd, models, model);
2561 void qemu_check_nic_model_list(NICInfo *nd, const char * const *models,
2562 const char *default_model)
2564 int i, exit_status = 0;
2566 if (!nd->model)
2567 nd->model = strdup(default_model);
2569 if (strcmp(nd->model, "?") != 0) {
2570 for (i = 0 ; models[i]; i++)
2571 if (strcmp(nd->model, models[i]) == 0)
2572 return;
2574 fprintf(stderr, "qemu: Unsupported NIC model: %s\n", nd->model);
2575 exit_status = 1;
2578 fprintf(stderr, "qemu: Supported NIC models: ");
2579 for (i = 0 ; models[i]; i++)
2580 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
2582 exit(exit_status);
2585 static int net_handle_fd_param(Monitor *mon, const char *param)
2587 if (!qemu_isdigit(param[0])) {
2588 int fd;
2590 fd = monitor_get_fd(mon, param);
2591 if (fd == -1) {
2592 config_error(mon, "No file descriptor named %s found", param);
2593 return -1;
2596 return fd;
2597 } else {
2598 return strtol(param, NULL, 0);
2602 int net_client_init(Monitor *mon, const char *device, const char *p)
2604 char buf[1024];
2605 int vlan_id, ret;
2606 VLANState *vlan;
2607 char *name = NULL;
2609 vlan_id = 0;
2610 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
2611 vlan_id = strtol(buf, NULL, 0);
2613 vlan = qemu_find_vlan(vlan_id, 1);
2615 if (get_param_value(buf, sizeof(buf), "name", p)) {
2616 name = qemu_strdup(buf);
2618 if (!strcmp(device, "nic")) {
2619 static const char * const nic_params[] = {
2620 "vlan", "name", "macaddr", "model", "addr", "id", "vectors", NULL
2622 NICInfo *nd;
2623 uint8_t *macaddr;
2624 int idx = nic_get_free_idx();
2626 if (check_params(buf, sizeof(buf), nic_params, p) < 0) {
2627 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
2628 ret = -1;
2629 goto out;
2631 if (idx == -1 || nb_nics >= MAX_NICS) {
2632 config_error(mon, "Too Many NICs\n");
2633 ret = -1;
2634 goto out;
2636 nd = &nd_table[idx];
2637 macaddr = nd->macaddr;
2638 macaddr[0] = 0x52;
2639 macaddr[1] = 0x54;
2640 macaddr[2] = 0x00;
2641 macaddr[3] = 0x12;
2642 macaddr[4] = 0x34;
2643 macaddr[5] = 0x56 + idx;
2645 if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
2646 if (parse_macaddr(macaddr, buf) < 0) {
2647 config_error(mon, "invalid syntax for ethernet address\n");
2648 ret = -1;
2649 goto out;
2652 if (get_param_value(buf, sizeof(buf), "model", p)) {
2653 nd->model = strdup(buf);
2655 if (get_param_value(buf, sizeof(buf), "addr", p)) {
2656 nd->devaddr = strdup(buf);
2658 if (get_param_value(buf, sizeof(buf), "id", p)) {
2659 nd->id = strdup(buf);
2661 nd->nvectors = NIC_NVECTORS_UNSPECIFIED;
2662 if (get_param_value(buf, sizeof(buf), "vectors", p)) {
2663 char *endptr;
2664 long vectors = strtol(buf, &endptr, 0);
2665 if (*endptr) {
2666 config_error(mon, "invalid syntax for # of vectors\n");
2667 ret = -1;
2668 goto out;
2670 if (vectors < 0 || vectors > 0x7ffffff) {
2671 config_error(mon, "invalid # of vectors\n");
2672 ret = -1;
2673 goto out;
2675 nd->nvectors = vectors;
2677 nd->vlan = vlan;
2678 nd->name = name;
2679 nd->used = 1;
2680 name = NULL;
2681 nb_nics++;
2682 vlan->nb_guest_devs++;
2683 ret = idx;
2684 } else
2685 if (!strcmp(device, "none")) {
2686 if (*p != '\0') {
2687 config_error(mon, "'none' takes no parameters\n");
2688 ret = -1;
2689 goto out;
2691 /* does nothing. It is needed to signal that no network cards
2692 are wanted */
2693 ret = 0;
2694 } else
2695 #ifdef CONFIG_SLIRP
2696 if (!strcmp(device, "user")) {
2697 static const char * const slirp_params[] = {
2698 "vlan", "name", "hostname", "restrict", "ip", "net", "host",
2699 "tftp", "bootfile", "dhcpstart", "dns", "smb", "smbserver",
2700 "hostfwd", "guestfwd", NULL
2702 struct slirp_config_str *config;
2703 int restricted = 0;
2704 char *vnet = NULL;
2705 char *vhost = NULL;
2706 char *vhostname = NULL;
2707 char *tftp_export = NULL;
2708 char *bootfile = NULL;
2709 char *vdhcp_start = NULL;
2710 char *vnamesrv = NULL;
2711 char *smb_export = NULL;
2712 char *vsmbsrv = NULL;
2713 const char *q;
2715 if (check_params(buf, sizeof(buf), slirp_params, p) < 0) {
2716 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
2717 ret = -1;
2718 goto out;
2720 if (get_param_value(buf, sizeof(buf), "ip", p)) {
2721 int vnet_buflen = strlen(buf) + strlen("/24") + 1;
2722 /* emulate legacy parameter */
2723 vnet = qemu_malloc(vnet_buflen);
2724 pstrcpy(vnet, vnet_buflen, buf);
2725 pstrcat(vnet, vnet_buflen, "/24");
2727 if (get_param_value(buf, sizeof(buf), "net", p)) {
2728 vnet = qemu_strdup(buf);
2730 if (get_param_value(buf, sizeof(buf), "host", p)) {
2731 vhost = qemu_strdup(buf);
2733 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
2734 vhostname = qemu_strdup(buf);
2736 if (get_param_value(buf, sizeof(buf), "restrict", p)) {
2737 restricted = (buf[0] == 'y') ? 1 : 0;
2739 if (get_param_value(buf, sizeof(buf), "dhcpstart", p)) {
2740 vdhcp_start = qemu_strdup(buf);
2742 if (get_param_value(buf, sizeof(buf), "dns", p)) {
2743 vnamesrv = qemu_strdup(buf);
2745 if (get_param_value(buf, sizeof(buf), "tftp", p)) {
2746 tftp_export = qemu_strdup(buf);
2748 if (get_param_value(buf, sizeof(buf), "bootfile", p)) {
2749 bootfile = qemu_strdup(buf);
2751 if (get_param_value(buf, sizeof(buf), "smb", p)) {
2752 smb_export = qemu_strdup(buf);
2753 if (get_param_value(buf, sizeof(buf), "smbserver", p)) {
2754 vsmbsrv = qemu_strdup(buf);
2757 q = p;
2758 while (1) {
2759 config = qemu_malloc(sizeof(*config));
2760 if (!get_next_param_value(config->str, sizeof(config->str),
2761 "hostfwd", &q)) {
2762 break;
2764 config->flags = SLIRP_CFG_HOSTFWD;
2765 config->next = slirp_configs;
2766 slirp_configs = config;
2767 config = NULL;
2769 q = p;
2770 while (1) {
2771 config = qemu_malloc(sizeof(*config));
2772 if (!get_next_param_value(config->str, sizeof(config->str),
2773 "guestfwd", &q)) {
2774 break;
2776 config->flags = 0;
2777 config->next = slirp_configs;
2778 slirp_configs = config;
2779 config = NULL;
2781 qemu_free(config);
2782 vlan->nb_host_devs++;
2783 ret = net_slirp_init(mon, vlan, device, name, restricted, vnet, vhost,
2784 vhostname, tftp_export, bootfile, vdhcp_start,
2785 vnamesrv, smb_export, vsmbsrv);
2786 qemu_free(vnet);
2787 qemu_free(vhost);
2788 qemu_free(vhostname);
2789 qemu_free(tftp_export);
2790 qemu_free(bootfile);
2791 qemu_free(vdhcp_start);
2792 qemu_free(vnamesrv);
2793 qemu_free(smb_export);
2794 qemu_free(vsmbsrv);
2795 } else if (!strcmp(device, "channel")) {
2796 if (TAILQ_EMPTY(&slirp_stacks)) {
2797 struct slirp_config_str *config;
2799 config = qemu_malloc(sizeof(*config));
2800 pstrcpy(config->str, sizeof(config->str), p);
2801 config->flags = SLIRP_CFG_LEGACY;
2802 config->next = slirp_configs;
2803 slirp_configs = config;
2804 } else {
2805 slirp_guestfwd(TAILQ_FIRST(&slirp_stacks), mon, p, 1);
2807 ret = 0;
2808 } else
2809 #endif
2810 #ifdef _WIN32
2811 if (!strcmp(device, "tap")) {
2812 static const char * const tap_params[] = {
2813 "vlan", "name", "ifname", NULL
2815 char ifname[64];
2817 if (check_params(buf, sizeof(buf), tap_params, p) < 0) {
2818 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
2819 ret = -1;
2820 goto out;
2822 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
2823 config_error(mon, "tap: no interface name\n");
2824 ret = -1;
2825 goto out;
2827 vlan->nb_host_devs++;
2828 ret = tap_win32_init(vlan, device, name, ifname);
2829 } else
2830 #elif defined (_AIX)
2831 #else
2832 if (!strcmp(device, "tap")) {
2833 char ifname[64], chkbuf[64];
2834 char setup_script[1024], down_script[1024];
2835 TAPState *s;
2836 int fd;
2837 vlan->nb_host_devs++;
2838 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
2839 static const char * const fd_params[] = {
2840 "vlan", "name", "fd", "sndbuf", NULL
2842 ret = -1;
2843 if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
2844 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2845 goto out;
2847 fd = net_handle_fd_param(mon, buf);
2848 if (fd == -1) {
2849 goto out;
2851 fcntl(fd, F_SETFL, O_NONBLOCK);
2852 s = net_tap_fd_init(vlan, device, name, fd, tap_probe_vnet_hdr(fd));
2853 if (!s) {
2854 close(fd);
2856 } else {
2857 static const char * const tap_params[] = {
2858 "vlan", "name", "ifname", "script", "downscript", "sndbuf", NULL
2860 if (check_params(chkbuf, sizeof(chkbuf), tap_params, p) < 0) {
2861 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2862 ret = -1;
2863 goto out;
2865 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
2866 ifname[0] = '\0';
2868 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
2869 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
2871 if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
2872 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
2874 s = net_tap_init(vlan, device, name, ifname, setup_script, down_script);
2876 if (s != NULL) {
2877 const char *sndbuf_str = NULL;
2878 if (get_param_value(buf, sizeof(buf), "sndbuf", p)) {
2879 sndbuf_str = buf;
2881 tap_set_sndbuf(s, sndbuf_str, mon);
2882 ret = 0;
2883 } else {
2884 ret = -1;
2886 } else
2887 #endif
2888 if (!strcmp(device, "socket")) {
2889 char chkbuf[64];
2890 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
2891 static const char * const fd_params[] = {
2892 "vlan", "name", "fd", NULL
2894 int fd;
2895 ret = -1;
2896 if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
2897 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2898 goto out;
2900 fd = net_handle_fd_param(mon, buf);
2901 if (fd == -1) {
2902 goto out;
2904 if (!net_socket_fd_init(vlan, device, name, fd, 1)) {
2905 close(fd);
2906 goto out;
2908 ret = 0;
2909 } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
2910 static const char * const listen_params[] = {
2911 "vlan", "name", "listen", NULL
2913 if (check_params(chkbuf, sizeof(chkbuf), listen_params, p) < 0) {
2914 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2915 ret = -1;
2916 goto out;
2918 ret = net_socket_listen_init(vlan, device, name, buf);
2919 } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
2920 static const char * const connect_params[] = {
2921 "vlan", "name", "connect", NULL
2923 if (check_params(chkbuf, sizeof(chkbuf), connect_params, p) < 0) {
2924 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2925 ret = -1;
2926 goto out;
2928 ret = net_socket_connect_init(vlan, device, name, buf);
2929 } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
2930 static const char * const mcast_params[] = {
2931 "vlan", "name", "mcast", NULL
2933 if (check_params(chkbuf, sizeof(chkbuf), mcast_params, p) < 0) {
2934 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2935 ret = -1;
2936 goto out;
2938 ret = net_socket_mcast_init(vlan, device, name, buf);
2939 } else {
2940 config_error(mon, "Unknown socket options: %s\n", p);
2941 ret = -1;
2942 goto out;
2944 vlan->nb_host_devs++;
2945 } else
2946 #ifdef CONFIG_VDE
2947 if (!strcmp(device, "vde")) {
2948 static const char * const vde_params[] = {
2949 "vlan", "name", "sock", "port", "group", "mode", NULL
2951 char vde_sock[1024], vde_group[512];
2952 int vde_port, vde_mode;
2954 if (check_params(buf, sizeof(buf), vde_params, p) < 0) {
2955 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
2956 ret = -1;
2957 goto out;
2959 vlan->nb_host_devs++;
2960 if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
2961 vde_sock[0] = '\0';
2963 if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
2964 vde_port = strtol(buf, NULL, 10);
2965 } else {
2966 vde_port = 0;
2968 if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
2969 vde_group[0] = '\0';
2971 if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
2972 vde_mode = strtol(buf, NULL, 8);
2973 } else {
2974 vde_mode = 0700;
2976 ret = net_vde_init(vlan, device, name, vde_sock, vde_port, vde_group, vde_mode);
2977 } else
2978 #endif
2979 if (!strcmp(device, "dump")) {
2980 int len = 65536;
2982 if (get_param_value(buf, sizeof(buf), "len", p) > 0) {
2983 len = strtol(buf, NULL, 0);
2985 if (!get_param_value(buf, sizeof(buf), "file", p)) {
2986 snprintf(buf, sizeof(buf), "qemu-vlan%d.pcap", vlan_id);
2988 ret = net_dump_init(mon, vlan, device, name, buf, len);
2989 } else {
2990 config_error(mon, "Unknown network device: %s\n", device);
2991 ret = -1;
2992 goto out;
2994 if (ret < 0) {
2995 config_error(mon, "Could not initialize device '%s'\n", device);
2997 out:
2998 qemu_free(name);
2999 return ret;
3002 void net_client_uninit(NICInfo *nd)
3004 nd->vlan->nb_guest_devs--;
3005 nb_nics--;
3006 nd->used = 0;
3007 free((void *)nd->model);
3010 static int net_host_check_device(const char *device)
3012 int i;
3013 const char *valid_param_list[] = { "tap", "socket", "dump"
3014 #ifdef CONFIG_SLIRP
3015 ,"user"
3016 #endif
3017 #ifdef CONFIG_VDE
3018 ,"vde"
3019 #endif
3021 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
3022 if (!strncmp(valid_param_list[i], device,
3023 strlen(valid_param_list[i])))
3024 return 1;
3027 return 0;
3030 void net_host_device_add(Monitor *mon, const char *device, const char *opts)
3032 if (!net_host_check_device(device)) {
3033 monitor_printf(mon, "invalid host network device %s\n", device);
3034 return;
3036 if (net_client_init(mon, device, opts ? opts : "") < 0) {
3037 monitor_printf(mon, "adding host network device %s failed\n", device);
3041 void net_host_device_remove(Monitor *mon, int vlan_id, const char *device)
3043 VLANClientState *vc;
3045 vc = qemu_find_vlan_client_by_name(mon, vlan_id, device);
3046 if (!vc) {
3047 return;
3049 if (!net_host_check_device(vc->model)) {
3050 monitor_printf(mon, "invalid host network device %s\n", device);
3051 return;
3053 qemu_del_vlan_client(vc);
3056 int net_client_parse(const char *str)
3058 const char *p;
3059 char *q;
3060 char device[64];
3062 p = str;
3063 q = device;
3064 while (*p != '\0' && *p != ',') {
3065 if ((q - device) < sizeof(device) - 1)
3066 *q++ = *p;
3067 p++;
3069 *q = '\0';
3070 if (*p == ',')
3071 p++;
3073 return net_client_init(NULL, device, p);
3076 void net_set_boot_mask(int net_boot_mask)
3078 int i;
3080 /* Only the first four NICs may be bootable */
3081 net_boot_mask = net_boot_mask & 0xF;
3083 for (i = 0; i < nb_nics; i++) {
3084 if (net_boot_mask & (1 << i)) {
3085 nd_table[i].bootable = 1;
3086 net_boot_mask &= ~(1 << i);
3090 if (net_boot_mask) {
3091 fprintf(stderr, "Cannot boot from non-existent NIC\n");
3092 exit(1);
3096 void do_info_network(Monitor *mon)
3098 VLANState *vlan;
3099 VLANClientState *vc;
3101 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
3102 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
3103 for(vc = vlan->first_client; vc != NULL; vc = vc->next)
3104 monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
3108 int do_set_link(Monitor *mon, const char *name, const char *up_or_down)
3110 VLANState *vlan;
3111 VLANClientState *vc = NULL;
3113 for (vlan = first_vlan; vlan != NULL; vlan = vlan->next)
3114 for (vc = vlan->first_client; vc != NULL; vc = vc->next)
3115 if (strcmp(vc->name, name) == 0)
3116 goto done;
3117 done:
3119 if (!vc) {
3120 monitor_printf(mon, "could not find network device '%s'", name);
3121 return 0;
3124 if (strcmp(up_or_down, "up") == 0)
3125 vc->link_down = 0;
3126 else if (strcmp(up_or_down, "down") == 0)
3127 vc->link_down = 1;
3128 else
3129 monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' "
3130 "valid\n", up_or_down);
3132 if (vc->link_status_changed)
3133 vc->link_status_changed(vc);
3135 return 1;
3138 void net_cleanup(void)
3140 VLANState *vlan;
3142 /* close network clients */
3143 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
3144 VLANClientState *vc = vlan->first_client;
3146 while (vc) {
3147 VLANClientState *next = vc->next;
3149 qemu_del_vlan_client(vc);
3151 vc = next;
3156 void net_client_check(void)
3158 VLANState *vlan;
3160 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
3161 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
3162 continue;
3163 if (vlan->nb_guest_devs == 0)
3164 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
3165 if (vlan->nb_host_devs == 0)
3166 fprintf(stderr,
3167 "Warning: vlan %d is not connected to host network\n",
3168 vlan->id);