slirp: Move smb, redir, tftp and bootp parameters and -net channel
[qemu-kvm/fedora.git] / net.c
blob18c51190bf5ff20625ecaf3de45bd4aab4403044
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 #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 #include "qemu-log.h"
123 #if defined(CONFIG_SLIRP)
124 #include "libslirp.h"
125 #endif
128 static VLANState *first_vlan;
130 /***********************************************************/
131 /* network device redirectors */
133 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
134 static void hex_dump(FILE *f, const uint8_t *buf, int size)
136 int len, i, j, c;
138 for(i=0;i<size;i+=16) {
139 len = size - i;
140 if (len > 16)
141 len = 16;
142 fprintf(f, "%08x ", i);
143 for(j=0;j<16;j++) {
144 if (j < len)
145 fprintf(f, " %02x", buf[i+j]);
146 else
147 fprintf(f, " ");
149 fprintf(f, " ");
150 for(j=0;j<len;j++) {
151 c = buf[i+j];
152 if (c < ' ' || c > '~')
153 c = '.';
154 fprintf(f, "%c", c);
156 fprintf(f, "\n");
159 #endif
161 static int parse_macaddr(uint8_t *macaddr, const char *p)
163 int i;
164 char *last_char;
165 long int offset;
167 errno = 0;
168 offset = strtol(p, &last_char, 0);
169 if (0 == errno && '\0' == *last_char &&
170 offset >= 0 && offset <= 0xFFFFFF) {
171 macaddr[3] = (offset & 0xFF0000) >> 16;
172 macaddr[4] = (offset & 0xFF00) >> 8;
173 macaddr[5] = offset & 0xFF;
174 return 0;
175 } else {
176 for(i = 0; i < 6; i++) {
177 macaddr[i] = strtol(p, (char **)&p, 16);
178 if (i == 5) {
179 if (*p != '\0')
180 return -1;
181 } else {
182 if (*p != ':' && *p != '-')
183 return -1;
184 p++;
187 return 0;
190 return -1;
193 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
195 const char *p, *p1;
196 int len;
197 p = *pp;
198 p1 = strchr(p, sep);
199 if (!p1)
200 return -1;
201 len = p1 - p;
202 p1++;
203 if (buf_size > 0) {
204 if (len > buf_size - 1)
205 len = buf_size - 1;
206 memcpy(buf, p, len);
207 buf[len] = '\0';
209 *pp = p1;
210 return 0;
213 int parse_host_src_port(struct sockaddr_in *haddr,
214 struct sockaddr_in *saddr,
215 const char *input_str)
217 char *str = strdup(input_str);
218 char *host_str = str;
219 char *src_str;
220 const char *src_str2;
221 char *ptr;
224 * Chop off any extra arguments at the end of the string which
225 * would start with a comma, then fill in the src port information
226 * if it was provided else use the "any address" and "any port".
228 if ((ptr = strchr(str,',')))
229 *ptr = '\0';
231 if ((src_str = strchr(input_str,'@'))) {
232 *src_str = '\0';
233 src_str++;
236 if (parse_host_port(haddr, host_str) < 0)
237 goto fail;
239 src_str2 = src_str;
240 if (!src_str || *src_str == '\0')
241 src_str2 = ":0";
243 if (parse_host_port(saddr, src_str2) < 0)
244 goto fail;
246 free(str);
247 return(0);
249 fail:
250 free(str);
251 return -1;
254 int parse_host_port(struct sockaddr_in *saddr, const char *str)
256 char buf[512];
257 struct hostent *he;
258 const char *p, *r;
259 int port;
261 p = str;
262 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
263 return -1;
264 saddr->sin_family = AF_INET;
265 if (buf[0] == '\0') {
266 saddr->sin_addr.s_addr = 0;
267 } else {
268 if (qemu_isdigit(buf[0])) {
269 if (!inet_aton(buf, &saddr->sin_addr))
270 return -1;
271 } else {
272 if ((he = gethostbyname(buf)) == NULL)
273 return - 1;
274 saddr->sin_addr = *(struct in_addr *)he->h_addr;
277 port = strtol(p, (char **)&r, 0);
278 if (r == p)
279 return -1;
280 saddr->sin_port = htons(port);
281 return 0;
284 #if !defined(_WIN32) && 0
285 static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
287 const char *p;
288 int len;
290 len = MIN(108, strlen(str));
291 p = strchr(str, ',');
292 if (p)
293 len = MIN(len, p - str);
295 memset(uaddr, 0, sizeof(*uaddr));
297 uaddr->sun_family = AF_UNIX;
298 memcpy(uaddr->sun_path, str, len);
300 return 0;
302 #endif
304 void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
306 snprintf(vc->info_str, sizeof(vc->info_str),
307 "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
308 vc->model,
309 macaddr[0], macaddr[1], macaddr[2],
310 macaddr[3], macaddr[4], macaddr[5]);
313 static char *assign_name(VLANClientState *vc1, const char *model)
315 VLANState *vlan;
316 char buf[256];
317 int id = 0;
319 for (vlan = first_vlan; vlan; vlan = vlan->next) {
320 VLANClientState *vc;
322 for (vc = vlan->first_client; vc; vc = vc->next)
323 if (vc != vc1 && strcmp(vc->model, model) == 0)
324 id++;
327 snprintf(buf, sizeof(buf), "%s.%d", model, id);
329 return strdup(buf);
332 VLANClientState *qemu_new_vlan_client(VLANState *vlan,
333 const char *model,
334 const char *name,
335 NetCanReceive *can_receive,
336 NetReceive *receive,
337 NetReceiveIOV *receive_iov,
338 NetCleanup *cleanup,
339 void *opaque)
341 VLANClientState *vc, **pvc;
342 vc = qemu_mallocz(sizeof(VLANClientState));
343 vc->model = strdup(model);
344 if (name)
345 vc->name = strdup(name);
346 else
347 vc->name = assign_name(vc, model);
348 vc->can_receive = can_receive;
349 vc->receive = receive;
350 vc->receive_iov = receive_iov;
351 vc->cleanup = cleanup;
352 vc->opaque = opaque;
353 vc->vlan = vlan;
355 vc->next = NULL;
356 pvc = &vlan->first_client;
357 while (*pvc != NULL)
358 pvc = &(*pvc)->next;
359 *pvc = vc;
360 return vc;
363 void qemu_del_vlan_client(VLANClientState *vc)
365 VLANClientState **pvc = &vc->vlan->first_client;
367 while (*pvc != NULL)
368 if (*pvc == vc) {
369 *pvc = vc->next;
370 if (vc->cleanup) {
371 vc->cleanup(vc);
373 free(vc->name);
374 free(vc->model);
375 qemu_free(vc);
376 break;
377 } else
378 pvc = &(*pvc)->next;
381 VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque)
383 VLANClientState **pvc = &vlan->first_client;
385 while (*pvc != NULL)
386 if ((*pvc)->opaque == opaque)
387 return *pvc;
388 else
389 pvc = &(*pvc)->next;
391 return NULL;
394 int qemu_can_send_packet(VLANClientState *sender)
396 VLANState *vlan = sender->vlan;
397 VLANClientState *vc;
399 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
400 if (vc == sender) {
401 continue;
404 /* no can_receive() handler, they can always receive */
405 if (!vc->can_receive || vc->can_receive(vc)) {
406 return 1;
409 return 0;
412 static int
413 qemu_deliver_packet(VLANClientState *sender, const uint8_t *buf, int size)
415 VLANClientState *vc;
416 int ret = -1;
418 sender->vlan->delivering = 1;
420 for (vc = sender->vlan->first_client; vc != NULL; vc = vc->next) {
421 ssize_t len;
423 if (vc == sender) {
424 continue;
427 if (vc->link_down) {
428 ret = size;
429 continue;
432 len = vc->receive(vc, buf, size);
434 ret = (ret >= 0) ? ret : len;
437 sender->vlan->delivering = 0;
439 return ret;
442 void qemu_purge_queued_packets(VLANClientState *vc)
444 VLANPacket **pp = &vc->vlan->send_queue;
446 while (*pp != NULL) {
447 VLANPacket *packet = *pp;
449 if (packet->sender == vc) {
450 *pp = packet->next;
451 qemu_free(packet);
452 } else {
453 pp = &packet->next;
458 void qemu_flush_queued_packets(VLANClientState *vc)
460 VLANPacket *packet;
462 while ((packet = vc->vlan->send_queue) != NULL) {
463 int ret;
465 vc->vlan->send_queue = packet->next;
467 ret = qemu_deliver_packet(packet->sender, packet->data, packet->size);
468 if (ret == 0 && packet->sent_cb != NULL) {
469 packet->next = vc->vlan->send_queue;
470 vc->vlan->send_queue = packet;
471 break;
474 if (packet->sent_cb)
475 packet->sent_cb(packet->sender, ret);
477 qemu_free(packet);
481 static void qemu_enqueue_packet(VLANClientState *sender,
482 const uint8_t *buf, int size,
483 NetPacketSent *sent_cb)
485 VLANPacket *packet;
487 packet = qemu_malloc(sizeof(VLANPacket) + size);
488 packet->next = sender->vlan->send_queue;
489 packet->sender = sender;
490 packet->size = size;
491 packet->sent_cb = sent_cb;
492 memcpy(packet->data, buf, size);
493 sender->vlan->send_queue = packet;
496 ssize_t qemu_send_packet_async(VLANClientState *sender,
497 const uint8_t *buf, int size,
498 NetPacketSent *sent_cb)
500 int ret;
502 if (sender->link_down) {
503 return size;
506 #ifdef DEBUG_NET
507 printf("vlan %d send:\n", sender->vlan->id);
508 hex_dump(stdout, buf, size);
509 #endif
511 if (sender->vlan->delivering) {
512 qemu_enqueue_packet(sender, buf, size, NULL);
513 return size;
516 ret = qemu_deliver_packet(sender, buf, size);
517 if (ret == 0 && sent_cb != NULL) {
518 qemu_enqueue_packet(sender, buf, size, sent_cb);
519 return 0;
522 qemu_flush_queued_packets(sender);
524 return ret;
527 void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
529 qemu_send_packet_async(vc, buf, size, NULL);
532 static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
533 int iovcnt)
535 uint8_t buffer[4096];
536 size_t offset = 0;
537 int i;
539 for (i = 0; i < iovcnt; i++) {
540 size_t len;
542 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
543 memcpy(buffer + offset, iov[i].iov_base, len);
544 offset += len;
547 return vc->receive(vc, buffer, offset);
550 static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
552 size_t offset = 0;
553 int i;
555 for (i = 0; i < iovcnt; i++)
556 offset += iov[i].iov_len;
557 return offset;
560 static int qemu_deliver_packet_iov(VLANClientState *sender,
561 const struct iovec *iov, int iovcnt)
563 VLANClientState *vc;
564 int ret = -1;
566 sender->vlan->delivering = 1;
568 for (vc = sender->vlan->first_client; vc != NULL; vc = vc->next) {
569 ssize_t len;
571 if (vc == sender) {
572 continue;
575 if (vc->link_down) {
576 ret = calc_iov_length(iov, iovcnt);
577 continue;
580 if (vc->receive_iov) {
581 len = vc->receive_iov(vc, iov, iovcnt);
582 } else {
583 len = vc_sendv_compat(vc, iov, iovcnt);
586 ret = (ret >= 0) ? ret : len;
589 sender->vlan->delivering = 0;
591 return ret;
594 static ssize_t qemu_enqueue_packet_iov(VLANClientState *sender,
595 const struct iovec *iov, int iovcnt,
596 NetPacketSent *sent_cb)
598 VLANPacket *packet;
599 size_t max_len = 0;
600 int i;
602 max_len = calc_iov_length(iov, iovcnt);
604 packet = qemu_malloc(sizeof(VLANPacket) + max_len);
605 packet->next = sender->vlan->send_queue;
606 packet->sender = sender;
607 packet->sent_cb = sent_cb;
608 packet->size = 0;
610 for (i = 0; i < iovcnt; i++) {
611 size_t len = iov[i].iov_len;
613 memcpy(packet->data + packet->size, iov[i].iov_base, len);
614 packet->size += len;
617 sender->vlan->send_queue = packet;
619 return packet->size;
622 ssize_t qemu_sendv_packet_async(VLANClientState *sender,
623 const struct iovec *iov, int iovcnt,
624 NetPacketSent *sent_cb)
626 int ret;
628 if (sender->link_down) {
629 return calc_iov_length(iov, iovcnt);
632 if (sender->vlan->delivering) {
633 return qemu_enqueue_packet_iov(sender, iov, iovcnt, NULL);
636 ret = qemu_deliver_packet_iov(sender, iov, iovcnt);
637 if (ret == 0 && sent_cb != NULL) {
638 qemu_enqueue_packet_iov(sender, iov, iovcnt, sent_cb);
639 return 0;
642 qemu_flush_queued_packets(sender);
644 return ret;
647 ssize_t
648 qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
650 return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
653 static void config_error(Monitor *mon, const char *fmt, ...)
655 va_list ap;
657 va_start(ap, fmt);
658 if (mon) {
659 monitor_vprintf(mon, fmt, ap);
660 } else {
661 fprintf(stderr, "qemu: ");
662 vfprintf(stderr, fmt, ap);
663 exit(1);
665 va_end(ap);
668 #if defined(CONFIG_SLIRP)
670 /* slirp network adapter */
672 #define SLIRP_CFG_REDIR 1
674 struct slirp_config_str {
675 struct slirp_config_str *next;
676 int flags;
677 char str[1024];
680 static int slirp_inited;
681 static struct slirp_config_str *slirp_configs;
682 const char *legacy_tftp_prefix;
683 const char *legacy_bootp_filename;
684 static VLANClientState *slirp_vc;
686 static void slirp_redirection(Monitor *mon, const char *redir_str);
687 static void vmchannel_init(Monitor *mon, const char *config_str);
689 #ifndef _WIN32
690 static const char *legacy_smb_export;
692 static void slirp_smb(const char *exported_dir);
693 #endif
695 int slirp_can_output(void)
697 return !slirp_vc || qemu_can_send_packet(slirp_vc);
700 void slirp_output(const uint8_t *pkt, int pkt_len)
702 #ifdef DEBUG_SLIRP
703 printf("slirp output:\n");
704 hex_dump(stdout, pkt, pkt_len);
705 #endif
706 if (!slirp_vc)
707 return;
708 qemu_send_packet(slirp_vc, pkt, pkt_len);
711 int slirp_is_inited(void)
713 return slirp_inited;
716 static ssize_t slirp_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
718 #ifdef DEBUG_SLIRP
719 printf("slirp input:\n");
720 hex_dump(stdout, buf, size);
721 #endif
722 slirp_input(buf, size);
723 return size;
726 static int slirp_in_use;
728 static void net_slirp_cleanup(VLANClientState *vc)
730 slirp_in_use = 0;
733 static int net_slirp_init(Monitor *mon, VLANState *vlan, const char *model,
734 const char *name, int restricted, const char *ip,
735 const char *tftp_export, const char *bootfile,
736 const char *smb_export)
738 if (slirp_in_use) {
739 /* slirp only supports a single instance so far */
740 return -1;
742 if (!slirp_inited) {
743 if (!tftp_export) {
744 tftp_export = legacy_tftp_prefix;
746 if (!bootfile) {
747 bootfile = legacy_bootp_filename;
749 slirp_inited = 1;
750 slirp_init(restricted, ip, tftp_export, bootfile);
752 while (slirp_configs) {
753 struct slirp_config_str *config = slirp_configs;
755 if (config->flags & SLIRP_CFG_REDIR) {
756 slirp_redirection(mon, config->str);
757 } else {
758 vmchannel_init(mon, config->str);
760 slirp_configs = config->next;
761 qemu_free(config);
763 #ifndef _WIN32
764 if (!smb_export) {
765 smb_export = legacy_smb_export;
767 if (smb_export) {
768 slirp_smb(smb_export);
770 #endif
773 slirp_vc = qemu_new_vlan_client(vlan, model, name, NULL, slirp_receive,
774 NULL, net_slirp_cleanup, NULL);
775 slirp_vc->info_str[0] = '\0';
776 slirp_in_use = 1;
777 return 0;
780 static void net_slirp_redir_rm(Monitor *mon, const char *port_str)
782 int host_port;
783 char buf[256] = "";
784 const char *p = port_str;
785 int is_udp = 0;
786 int n;
788 if (!mon)
789 return;
791 if (!port_str || !port_str[0])
792 goto fail_syntax;
794 get_str_sep(buf, sizeof(buf), &p, ':');
796 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
797 is_udp = 0;
798 } else if (!strcmp(buf, "udp")) {
799 is_udp = 1;
800 } else {
801 goto fail_syntax;
804 host_port = atoi(p);
806 n = slirp_redir_rm(is_udp, host_port);
808 monitor_printf(mon, "removed %d redirections to %s port %d\n", n,
809 is_udp ? "udp" : "tcp", host_port);
810 return;
812 fail_syntax:
813 monitor_printf(mon, "invalid format\n");
816 static void slirp_redirection(Monitor *mon, const char *redir_str)
818 struct in_addr guest_addr;
819 int host_port, guest_port;
820 const char *p;
821 char buf[256], *r;
822 int is_udp;
824 p = redir_str;
825 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
826 goto fail_syntax;
828 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
829 is_udp = 0;
830 } else if (!strcmp(buf, "udp")) {
831 is_udp = 1;
832 } else {
833 goto fail_syntax;
836 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
837 goto fail_syntax;
839 host_port = strtol(buf, &r, 0);
840 if (r == buf) {
841 goto fail_syntax;
844 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
845 goto fail_syntax;
847 if (buf[0] == '\0') {
848 pstrcpy(buf, sizeof(buf), "10.0.2.15");
850 if (!inet_aton(buf, &guest_addr)) {
851 goto fail_syntax;
854 guest_port = strtol(p, &r, 0);
855 if (r == p) {
856 goto fail_syntax;
859 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) {
860 config_error(mon, "could not set up redirection '%s'\n", redir_str);
862 return;
864 fail_syntax:
865 config_error(mon, "invalid redirection format '%s'\n", redir_str);
868 void net_slirp_redir(Monitor *mon, const char *redir_str, const char *redir_opt2)
870 struct slirp_config_str *config;
872 if (!slirp_inited) {
873 if (mon) {
874 monitor_printf(mon, "user mode network stack not in use\n");
875 } else {
876 config = qemu_malloc(sizeof(*config));
877 pstrcpy(config->str, sizeof(config->str), redir_str);
878 config->flags = SLIRP_CFG_REDIR;
879 config->next = slirp_configs;
880 slirp_configs = config;
882 return;
885 if (!strcmp(redir_str, "remove")) {
886 net_slirp_redir_rm(mon, redir_opt2);
887 return;
890 slirp_redirection(mon, redir_str);
893 #ifndef _WIN32
895 static char smb_dir[1024];
897 static void erase_dir(char *dir_name)
899 DIR *d;
900 struct dirent *de;
901 char filename[1024];
903 /* erase all the files in the directory */
904 if ((d = opendir(dir_name)) != NULL) {
905 for(;;) {
906 de = readdir(d);
907 if (!de)
908 break;
909 if (strcmp(de->d_name, ".") != 0 &&
910 strcmp(de->d_name, "..") != 0) {
911 snprintf(filename, sizeof(filename), "%s/%s",
912 smb_dir, de->d_name);
913 if (unlink(filename) != 0) /* is it a directory? */
914 erase_dir(filename);
917 closedir(d);
918 rmdir(dir_name);
922 /* automatic user mode samba server configuration */
923 static void smb_exit(void)
925 erase_dir(smb_dir);
928 static void slirp_smb(const char *exported_dir)
930 char smb_conf[1024];
931 char smb_cmdline[1024];
932 FILE *f;
934 /* XXX: better tmp dir construction */
935 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%ld", (long)getpid());
936 if (mkdir(smb_dir, 0700) < 0) {
937 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir);
938 exit(1);
940 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
942 f = fopen(smb_conf, "w");
943 if (!f) {
944 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf);
945 exit(1);
947 fprintf(f,
948 "[global]\n"
949 "private dir=%s\n"
950 "smb ports=0\n"
951 "socket address=127.0.0.1\n"
952 "pid directory=%s\n"
953 "lock directory=%s\n"
954 "log file=%s/log.smbd\n"
955 "smb passwd file=%s/smbpasswd\n"
956 "security = share\n"
957 "[qemu]\n"
958 "path=%s\n"
959 "read only=no\n"
960 "guest ok=yes\n",
961 smb_dir,
962 smb_dir,
963 smb_dir,
964 smb_dir,
965 smb_dir,
966 exported_dir
968 fclose(f);
969 atexit(smb_exit);
971 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
972 SMBD_COMMAND, smb_conf);
974 slirp_add_exec(0, smb_cmdline, 4, 139);
977 /* automatic user mode samba server configuration (legacy interface) */
978 void net_slirp_smb(const char *exported_dir)
980 if (legacy_smb_export) {
981 fprintf(stderr, "-smb given twice\n");
982 exit(1);
984 legacy_smb_export = exported_dir;
985 if (slirp_inited) {
986 slirp_smb(exported_dir);
990 #endif /* !defined(_WIN32) */
992 void do_info_slirp(Monitor *mon)
994 slirp_stats();
997 struct VMChannel {
998 CharDriverState *hd;
999 int port;
1002 static int vmchannel_can_read(void *opaque)
1004 struct VMChannel *vmc = (struct VMChannel*)opaque;
1005 return slirp_socket_can_recv(4, vmc->port);
1008 static void vmchannel_read(void *opaque, const uint8_t *buf, int size)
1010 struct VMChannel *vmc = (struct VMChannel*)opaque;
1011 slirp_socket_recv(4, vmc->port, buf, size);
1014 static void vmchannel_init(Monitor *mon, const char *config_str)
1016 struct VMChannel *vmc;
1017 char *devname;
1018 char name[20];
1019 int port;
1021 port = strtol(config_str, &devname, 10);
1022 if (port < 1 || port > 65535 || *devname != ':') {
1023 config_error(mon, "invalid vmchannel port number\n");
1024 return;
1026 devname++;
1028 vmc = qemu_malloc(sizeof(struct VMChannel));
1029 snprintf(name, sizeof(name), "vmchannel%d", port);
1030 vmc->hd = qemu_chr_open(name, devname, NULL);
1031 if (!vmc->hd) {
1032 config_error(mon, "could not open vmchannel device '%s'\n", devname);
1033 qemu_free(vmc);
1034 return;
1036 vmc->port = port;
1038 slirp_add_exec(3, vmc->hd, 4, port);
1039 qemu_chr_add_handlers(vmc->hd, vmchannel_can_read, vmchannel_read,
1040 NULL, vmc);
1041 return;
1044 #endif /* CONFIG_SLIRP */
1046 #if !defined(_WIN32)
1048 typedef struct TAPState {
1049 VLANClientState *vc;
1050 int fd;
1051 char down_script[1024];
1052 char down_script_arg[128];
1053 uint8_t buf[4096];
1054 unsigned int read_poll : 1;
1055 unsigned int write_poll : 1;
1056 } TAPState;
1058 static int launch_script(const char *setup_script, const char *ifname, int fd);
1060 static int tap_can_send(void *opaque);
1061 static void tap_send(void *opaque);
1062 static void tap_writable(void *opaque);
1064 static void tap_update_fd_handler(TAPState *s)
1066 qemu_set_fd_handler2(s->fd,
1067 s->read_poll ? tap_can_send : NULL,
1068 s->read_poll ? tap_send : NULL,
1069 s->write_poll ? tap_writable : NULL,
1073 static void tap_read_poll(TAPState *s, int enable)
1075 s->read_poll = !!enable;
1076 tap_update_fd_handler(s);
1079 static void tap_write_poll(TAPState *s, int enable)
1081 s->write_poll = !!enable;
1082 tap_update_fd_handler(s);
1085 static void tap_writable(void *opaque)
1087 TAPState *s = opaque;
1089 tap_write_poll(s, 0);
1091 qemu_flush_queued_packets(s->vc);
1094 static ssize_t tap_receive_iov(VLANClientState *vc, const struct iovec *iov,
1095 int iovcnt)
1097 TAPState *s = vc->opaque;
1098 ssize_t len;
1100 do {
1101 len = writev(s->fd, iov, iovcnt);
1102 } while (len == -1 && errno == EINTR);
1104 if (len == -1 && errno == EAGAIN) {
1105 tap_write_poll(s, 1);
1106 return 0;
1109 return len;
1112 static ssize_t tap_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
1114 TAPState *s = vc->opaque;
1115 ssize_t len;
1117 do {
1118 len = write(s->fd, buf, size);
1119 } while (len == -1 && (errno == EINTR || errno == EAGAIN));
1121 return len;
1124 static int tap_can_send(void *opaque)
1126 TAPState *s = opaque;
1128 return qemu_can_send_packet(s->vc);
1131 #ifdef __sun__
1132 static ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
1134 struct strbuf sbuf;
1135 int f = 0;
1137 sbuf.maxlen = maxlen;
1138 sbuf.buf = (char *)buf;
1140 return getmsg(tapfd, NULL, &sbuf, &f) >= 0 ? sbuf.len : -1;
1142 #else
1143 static ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
1145 return read(tapfd, buf, maxlen);
1147 #endif
1149 static void tap_send_completed(VLANClientState *vc, ssize_t len)
1151 TAPState *s = vc->opaque;
1152 tap_read_poll(s, 1);
1155 static void tap_send(void *opaque)
1157 TAPState *s = opaque;
1158 int size;
1160 do {
1161 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
1162 if (size <= 0) {
1163 break;
1166 size = qemu_send_packet_async(s->vc, s->buf, size, tap_send_completed);
1167 if (size == 0) {
1168 tap_read_poll(s, 0);
1170 } while (size > 0);
1173 static void tap_set_sndbuf(TAPState *s, int sndbuf, Monitor *mon)
1175 #ifdef TUNSETSNDBUF
1176 if (ioctl(s->fd, TUNSETSNDBUF, &sndbuf) == -1) {
1177 config_error(mon, "TUNSETSNDBUF ioctl failed: %s\n",
1178 strerror(errno));
1180 #else
1181 config_error(mon, "No '-net tap,sndbuf=<nbytes>' support available\n");
1182 #endif
1185 static void tap_cleanup(VLANClientState *vc)
1187 TAPState *s = vc->opaque;
1189 qemu_purge_queued_packets(vc);
1191 if (s->down_script[0])
1192 launch_script(s->down_script, s->down_script_arg, s->fd);
1194 tap_read_poll(s, 0);
1195 tap_write_poll(s, 0);
1196 close(s->fd);
1197 qemu_free(s);
1200 /* fd support */
1202 static TAPState *net_tap_fd_init(VLANState *vlan,
1203 const char *model,
1204 const char *name,
1205 int fd)
1207 TAPState *s;
1209 s = qemu_mallocz(sizeof(TAPState));
1210 s->fd = fd;
1211 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, tap_receive,
1212 tap_receive_iov, tap_cleanup, s);
1213 tap_read_poll(s, 1);
1214 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd);
1215 return s;
1218 #if defined (HOST_BSD) || defined (__FreeBSD_kernel__)
1219 static int tap_open(char *ifname, int ifname_size)
1221 int fd;
1222 char *dev;
1223 struct stat s;
1225 TFR(fd = open("/dev/tap", O_RDWR));
1226 if (fd < 0) {
1227 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n");
1228 return -1;
1231 fstat(fd, &s);
1232 dev = devname(s.st_rdev, S_IFCHR);
1233 pstrcpy(ifname, ifname_size, dev);
1235 fcntl(fd, F_SETFL, O_NONBLOCK);
1236 return fd;
1238 #elif defined(__sun__)
1239 #define TUNNEWPPA (('T'<<16) | 0x0001)
1241 * Allocate TAP device, returns opened fd.
1242 * Stores dev name in the first arg(must be large enough).
1244 static int tap_alloc(char *dev, size_t dev_size)
1246 int tap_fd, if_fd, ppa = -1;
1247 static int ip_fd = 0;
1248 char *ptr;
1250 static int arp_fd = 0;
1251 int ip_muxid, arp_muxid;
1252 struct strioctl strioc_if, strioc_ppa;
1253 int link_type = I_PLINK;;
1254 struct lifreq ifr;
1255 char actual_name[32] = "";
1257 memset(&ifr, 0x0, sizeof(ifr));
1259 if( *dev ){
1260 ptr = dev;
1261 while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
1262 ppa = atoi(ptr);
1265 /* Check if IP device was opened */
1266 if( ip_fd )
1267 close(ip_fd);
1269 TFR(ip_fd = open("/dev/udp", O_RDWR, 0));
1270 if (ip_fd < 0) {
1271 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)");
1272 return -1;
1275 TFR(tap_fd = open("/dev/tap", O_RDWR, 0));
1276 if (tap_fd < 0) {
1277 syslog(LOG_ERR, "Can't open /dev/tap");
1278 return -1;
1281 /* Assign a new PPA and get its unit number. */
1282 strioc_ppa.ic_cmd = TUNNEWPPA;
1283 strioc_ppa.ic_timout = 0;
1284 strioc_ppa.ic_len = sizeof(ppa);
1285 strioc_ppa.ic_dp = (char *)&ppa;
1286 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0)
1287 syslog (LOG_ERR, "Can't assign new interface");
1289 TFR(if_fd = open("/dev/tap", O_RDWR, 0));
1290 if (if_fd < 0) {
1291 syslog(LOG_ERR, "Can't open /dev/tap (2)");
1292 return -1;
1294 if(ioctl(if_fd, I_PUSH, "ip") < 0){
1295 syslog(LOG_ERR, "Can't push IP module");
1296 return -1;
1299 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
1300 syslog(LOG_ERR, "Can't get flags\n");
1302 snprintf (actual_name, 32, "tap%d", ppa);
1303 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1305 ifr.lifr_ppa = ppa;
1306 /* Assign ppa according to the unit number returned by tun device */
1308 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0)
1309 syslog (LOG_ERR, "Can't set PPA %d", ppa);
1310 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0)
1311 syslog (LOG_ERR, "Can't get flags\n");
1312 /* Push arp module to if_fd */
1313 if (ioctl (if_fd, I_PUSH, "arp") < 0)
1314 syslog (LOG_ERR, "Can't push ARP module (2)");
1316 /* Push arp module to ip_fd */
1317 if (ioctl (ip_fd, I_POP, NULL) < 0)
1318 syslog (LOG_ERR, "I_POP failed\n");
1319 if (ioctl (ip_fd, I_PUSH, "arp") < 0)
1320 syslog (LOG_ERR, "Can't push ARP module (3)\n");
1321 /* Open arp_fd */
1322 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0));
1323 if (arp_fd < 0)
1324 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap");
1326 /* Set ifname to arp */
1327 strioc_if.ic_cmd = SIOCSLIFNAME;
1328 strioc_if.ic_timout = 0;
1329 strioc_if.ic_len = sizeof(ifr);
1330 strioc_if.ic_dp = (char *)&ifr;
1331 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){
1332 syslog (LOG_ERR, "Can't set ifname to arp\n");
1335 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){
1336 syslog(LOG_ERR, "Can't link TAP device to IP");
1337 return -1;
1340 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
1341 syslog (LOG_ERR, "Can't link TAP device to ARP");
1343 close (if_fd);
1345 memset(&ifr, 0x0, sizeof(ifr));
1346 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1347 ifr.lifr_ip_muxid = ip_muxid;
1348 ifr.lifr_arp_muxid = arp_muxid;
1350 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
1352 ioctl (ip_fd, I_PUNLINK , arp_muxid);
1353 ioctl (ip_fd, I_PUNLINK, ip_muxid);
1354 syslog (LOG_ERR, "Can't set multiplexor id");
1357 snprintf(dev, dev_size, "tap%d", ppa);
1358 return tap_fd;
1361 static int tap_open(char *ifname, int ifname_size)
1363 char dev[10]="";
1364 int fd;
1365 if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){
1366 fprintf(stderr, "Cannot allocate TAP device\n");
1367 return -1;
1369 pstrcpy(ifname, ifname_size, dev);
1370 fcntl(fd, F_SETFL, O_NONBLOCK);
1371 return fd;
1373 #elif defined (_AIX)
1374 static int tap_open(char *ifname, int ifname_size)
1376 fprintf (stderr, "no tap on AIX\n");
1377 return -1;
1379 #else
1380 static int tap_open(char *ifname, int ifname_size)
1382 struct ifreq ifr;
1383 int fd, ret;
1385 TFR(fd = open("/dev/net/tun", O_RDWR));
1386 if (fd < 0) {
1387 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n");
1388 return -1;
1390 memset(&ifr, 0, sizeof(ifr));
1391 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
1392 if (ifname[0] != '\0')
1393 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname);
1394 else
1395 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d");
1396 ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
1397 if (ret != 0) {
1398 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n");
1399 close(fd);
1400 return -1;
1402 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1403 fcntl(fd, F_SETFL, O_NONBLOCK);
1404 return fd;
1406 #endif
1408 static int launch_script(const char *setup_script, const char *ifname, int fd)
1410 sigset_t oldmask, mask;
1411 int pid, status;
1412 char *args[3];
1413 char **parg;
1415 sigemptyset(&mask);
1416 sigaddset(&mask, SIGCHLD);
1417 sigprocmask(SIG_BLOCK, &mask, &oldmask);
1419 /* try to launch network script */
1420 pid = fork();
1421 if (pid == 0) {
1422 int open_max = sysconf(_SC_OPEN_MAX), i;
1424 for (i = 0; i < open_max; i++) {
1425 if (i != STDIN_FILENO &&
1426 i != STDOUT_FILENO &&
1427 i != STDERR_FILENO &&
1428 i != fd) {
1429 close(i);
1432 parg = args;
1433 *parg++ = (char *)setup_script;
1434 *parg++ = (char *)ifname;
1435 *parg++ = NULL;
1436 execv(setup_script, args);
1437 _exit(1);
1438 } else if (pid > 0) {
1439 while (waitpid(pid, &status, 0) != pid) {
1440 /* loop */
1442 sigprocmask(SIG_SETMASK, &oldmask, NULL);
1444 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1445 return 0;
1448 fprintf(stderr, "%s: could not launch network script\n", setup_script);
1449 return -1;
1452 static TAPState *net_tap_init(VLANState *vlan, const char *model,
1453 const char *name, const char *ifname1,
1454 const char *setup_script, const char *down_script)
1456 TAPState *s;
1457 int fd;
1458 char ifname[128];
1460 if (ifname1 != NULL)
1461 pstrcpy(ifname, sizeof(ifname), ifname1);
1462 else
1463 ifname[0] = '\0';
1464 TFR(fd = tap_open(ifname, sizeof(ifname)));
1465 if (fd < 0)
1466 return NULL;
1468 if (!setup_script || !strcmp(setup_script, "no"))
1469 setup_script = "";
1470 if (setup_script[0] != '\0' &&
1471 launch_script(setup_script, ifname, fd)) {
1472 return NULL;
1474 s = net_tap_fd_init(vlan, model, name, fd);
1475 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1476 "ifname=%s,script=%s,downscript=%s",
1477 ifname, setup_script, down_script);
1478 if (down_script && strcmp(down_script, "no")) {
1479 snprintf(s->down_script, sizeof(s->down_script), "%s", down_script);
1480 snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname);
1482 return s;
1485 #endif /* !_WIN32 */
1487 #if defined(CONFIG_VDE)
1488 typedef struct VDEState {
1489 VLANClientState *vc;
1490 VDECONN *vde;
1491 } VDEState;
1493 static void vde_to_qemu(void *opaque)
1495 VDEState *s = opaque;
1496 uint8_t buf[4096];
1497 int size;
1499 size = vde_recv(s->vde, (char *)buf, sizeof(buf), 0);
1500 if (size > 0) {
1501 qemu_send_packet(s->vc, buf, size);
1505 static ssize_t vde_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
1507 VDEState *s = vc->opaque;
1508 ssize_t ret;
1510 do {
1511 ret = vde_send(s->vde, (const char *)buf, size, 0);
1512 } while (ret < 0 && errno == EINTR);
1514 return ret;
1517 static void vde_cleanup(VLANClientState *vc)
1519 VDEState *s = vc->opaque;
1520 qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL);
1521 vde_close(s->vde);
1522 qemu_free(s);
1525 static int net_vde_init(VLANState *vlan, const char *model,
1526 const char *name, const char *sock,
1527 int port, const char *group, int mode)
1529 VDEState *s;
1530 char *init_group = strlen(group) ? (char *)group : NULL;
1531 char *init_sock = strlen(sock) ? (char *)sock : NULL;
1533 struct vde_open_args args = {
1534 .port = port,
1535 .group = init_group,
1536 .mode = mode,
1539 s = qemu_mallocz(sizeof(VDEState));
1540 s->vde = vde_open(init_sock, (char *)"QEMU", &args);
1541 if (!s->vde){
1542 free(s);
1543 return -1;
1545 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, vde_receive,
1546 NULL, vde_cleanup, s);
1547 qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s);
1548 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d",
1549 sock, vde_datafd(s->vde));
1550 return 0;
1552 #endif
1554 /* network connection */
1555 typedef struct NetSocketState {
1556 VLANClientState *vc;
1557 int fd;
1558 int state; /* 0 = getting length, 1 = getting data */
1559 unsigned int index;
1560 unsigned int packet_len;
1561 uint8_t buf[4096];
1562 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
1563 } NetSocketState;
1565 typedef struct NetSocketListenState {
1566 VLANState *vlan;
1567 char *model;
1568 char *name;
1569 int fd;
1570 } NetSocketListenState;
1572 /* XXX: we consider we can send the whole packet without blocking */
1573 static ssize_t net_socket_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
1575 NetSocketState *s = vc->opaque;
1576 uint32_t len;
1577 len = htonl(size);
1579 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
1580 return send_all(s->fd, buf, size);
1583 static ssize_t net_socket_receive_dgram(VLANClientState *vc, const uint8_t *buf, size_t size)
1585 NetSocketState *s = vc->opaque;
1587 return sendto(s->fd, (const void *)buf, size, 0,
1588 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
1591 static void net_socket_send(void *opaque)
1593 NetSocketState *s = opaque;
1594 int size, err;
1595 unsigned l;
1596 uint8_t buf1[4096];
1597 const uint8_t *buf;
1599 size = recv(s->fd, (void *)buf1, sizeof(buf1), 0);
1600 if (size < 0) {
1601 err = socket_error();
1602 if (err != EWOULDBLOCK)
1603 goto eoc;
1604 } else if (size == 0) {
1605 /* end of connection */
1606 eoc:
1607 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1608 closesocket(s->fd);
1609 return;
1611 buf = buf1;
1612 while (size > 0) {
1613 /* reassemble a packet from the network */
1614 switch(s->state) {
1615 case 0:
1616 l = 4 - s->index;
1617 if (l > size)
1618 l = size;
1619 memcpy(s->buf + s->index, buf, l);
1620 buf += l;
1621 size -= l;
1622 s->index += l;
1623 if (s->index == 4) {
1624 /* got length */
1625 s->packet_len = ntohl(*(uint32_t *)s->buf);
1626 s->index = 0;
1627 s->state = 1;
1629 break;
1630 case 1:
1631 l = s->packet_len - s->index;
1632 if (l > size)
1633 l = size;
1634 if (s->index + l <= sizeof(s->buf)) {
1635 memcpy(s->buf + s->index, buf, l);
1636 } else {
1637 fprintf(stderr, "serious error: oversized packet received,"
1638 "connection terminated.\n");
1639 s->state = 0;
1640 goto eoc;
1643 s->index += l;
1644 buf += l;
1645 size -= l;
1646 if (s->index >= s->packet_len) {
1647 qemu_send_packet(s->vc, s->buf, s->packet_len);
1648 s->index = 0;
1649 s->state = 0;
1651 break;
1656 static void net_socket_send_dgram(void *opaque)
1658 NetSocketState *s = opaque;
1659 int size;
1661 size = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0);
1662 if (size < 0)
1663 return;
1664 if (size == 0) {
1665 /* end of connection */
1666 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1667 return;
1669 qemu_send_packet(s->vc, s->buf, size);
1672 static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
1674 struct ip_mreq imr;
1675 int fd;
1676 int val, ret;
1677 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) {
1678 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n",
1679 inet_ntoa(mcastaddr->sin_addr),
1680 (int)ntohl(mcastaddr->sin_addr.s_addr));
1681 return -1;
1684 fd = socket(PF_INET, SOCK_DGRAM, 0);
1685 if (fd < 0) {
1686 perror("socket(PF_INET, SOCK_DGRAM)");
1687 return -1;
1690 val = 1;
1691 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
1692 (const char *)&val, sizeof(val));
1693 if (ret < 0) {
1694 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)");
1695 goto fail;
1698 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
1699 if (ret < 0) {
1700 perror("bind");
1701 goto fail;
1704 /* Add host to multicast group */
1705 imr.imr_multiaddr = mcastaddr->sin_addr;
1706 imr.imr_interface.s_addr = htonl(INADDR_ANY);
1708 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1709 (const char *)&imr, sizeof(struct ip_mreq));
1710 if (ret < 0) {
1711 perror("setsockopt(IP_ADD_MEMBERSHIP)");
1712 goto fail;
1715 /* Force mcast msgs to loopback (eg. several QEMUs in same host */
1716 val = 1;
1717 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1718 (const char *)&val, sizeof(val));
1719 if (ret < 0) {
1720 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)");
1721 goto fail;
1724 socket_set_nonblock(fd);
1725 return fd;
1726 fail:
1727 if (fd >= 0)
1728 closesocket(fd);
1729 return -1;
1732 static void net_socket_cleanup(VLANClientState *vc)
1734 NetSocketState *s = vc->opaque;
1735 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1736 close(s->fd);
1737 qemu_free(s);
1740 static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
1741 const char *model,
1742 const char *name,
1743 int fd, int is_connected)
1745 struct sockaddr_in saddr;
1746 int newfd;
1747 socklen_t saddr_len;
1748 NetSocketState *s;
1750 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it
1751 * Because this may be "shared" socket from a "master" process, datagrams would be recv()
1752 * by ONLY ONE process: we must "clone" this dgram socket --jjo
1755 if (is_connected) {
1756 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) {
1757 /* must be bound */
1758 if (saddr.sin_addr.s_addr==0) {
1759 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n",
1760 fd);
1761 return NULL;
1763 /* clone dgram socket */
1764 newfd = net_socket_mcast_create(&saddr);
1765 if (newfd < 0) {
1766 /* error already reported by net_socket_mcast_create() */
1767 close(fd);
1768 return NULL;
1770 /* clone newfd to fd, close newfd */
1771 dup2(newfd, fd);
1772 close(newfd);
1774 } else {
1775 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1776 fd, strerror(errno));
1777 return NULL;
1781 s = qemu_mallocz(sizeof(NetSocketState));
1782 s->fd = fd;
1784 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, net_socket_receive_dgram,
1785 NULL, net_socket_cleanup, s);
1786 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s);
1788 /* mcast: save bound address as dst */
1789 if (is_connected) s->dgram_dst=saddr;
1791 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1792 "socket: fd=%d (%s mcast=%s:%d)",
1793 fd, is_connected? "cloned" : "",
1794 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1795 return s;
1798 static void net_socket_connect(void *opaque)
1800 NetSocketState *s = opaque;
1801 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
1804 static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
1805 const char *model,
1806 const char *name,
1807 int fd, int is_connected)
1809 NetSocketState *s;
1810 s = qemu_mallocz(sizeof(NetSocketState));
1811 s->fd = fd;
1812 s->vc = qemu_new_vlan_client(vlan, model, name, NULL, net_socket_receive,
1813 NULL, net_socket_cleanup, s);
1814 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1815 "socket: fd=%d", fd);
1816 if (is_connected) {
1817 net_socket_connect(s);
1818 } else {
1819 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s);
1821 return s;
1824 static NetSocketState *net_socket_fd_init(VLANState *vlan,
1825 const char *model, const char *name,
1826 int fd, int is_connected)
1828 int so_type=-1, optlen=sizeof(so_type);
1830 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type,
1831 (socklen_t *)&optlen)< 0) {
1832 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd);
1833 return NULL;
1835 switch(so_type) {
1836 case SOCK_DGRAM:
1837 return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected);
1838 case SOCK_STREAM:
1839 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
1840 default:
1841 /* who knows ... this could be a eg. a pty, do warn and continue as stream */
1842 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd);
1843 return net_socket_fd_init_stream(vlan, model, name, fd, is_connected);
1845 return NULL;
1848 static void net_socket_accept(void *opaque)
1850 NetSocketListenState *s = opaque;
1851 NetSocketState *s1;
1852 struct sockaddr_in saddr;
1853 socklen_t len;
1854 int fd;
1856 for(;;) {
1857 len = sizeof(saddr);
1858 fd = accept(s->fd, (struct sockaddr *)&saddr, &len);
1859 if (fd < 0 && errno != EINTR) {
1860 return;
1861 } else if (fd >= 0) {
1862 break;
1865 s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1);
1866 if (!s1) {
1867 closesocket(fd);
1868 } else {
1869 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str),
1870 "socket: connection from %s:%d",
1871 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1875 static int net_socket_listen_init(VLANState *vlan,
1876 const char *model,
1877 const char *name,
1878 const char *host_str)
1880 NetSocketListenState *s;
1881 int fd, val, ret;
1882 struct sockaddr_in saddr;
1884 if (parse_host_port(&saddr, host_str) < 0)
1885 return -1;
1887 s = qemu_mallocz(sizeof(NetSocketListenState));
1889 fd = socket(PF_INET, SOCK_STREAM, 0);
1890 if (fd < 0) {
1891 perror("socket");
1892 return -1;
1894 socket_set_nonblock(fd);
1896 /* allow fast reuse */
1897 val = 1;
1898 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
1900 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1901 if (ret < 0) {
1902 perror("bind");
1903 return -1;
1905 ret = listen(fd, 0);
1906 if (ret < 0) {
1907 perror("listen");
1908 return -1;
1910 s->vlan = vlan;
1911 s->model = strdup(model);
1912 s->name = name ? strdup(name) : NULL;
1913 s->fd = fd;
1914 qemu_set_fd_handler(fd, net_socket_accept, NULL, s);
1915 return 0;
1918 static int net_socket_connect_init(VLANState *vlan,
1919 const char *model,
1920 const char *name,
1921 const char *host_str)
1923 NetSocketState *s;
1924 int fd, connected, ret, err;
1925 struct sockaddr_in saddr;
1927 if (parse_host_port(&saddr, host_str) < 0)
1928 return -1;
1930 fd = socket(PF_INET, SOCK_STREAM, 0);
1931 if (fd < 0) {
1932 perror("socket");
1933 return -1;
1935 socket_set_nonblock(fd);
1937 connected = 0;
1938 for(;;) {
1939 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1940 if (ret < 0) {
1941 err = socket_error();
1942 if (err == EINTR || err == EWOULDBLOCK) {
1943 } else if (err == EINPROGRESS) {
1944 break;
1945 #ifdef _WIN32
1946 } else if (err == WSAEALREADY) {
1947 break;
1948 #endif
1949 } else {
1950 perror("connect");
1951 closesocket(fd);
1952 return -1;
1954 } else {
1955 connected = 1;
1956 break;
1959 s = net_socket_fd_init(vlan, model, name, fd, connected);
1960 if (!s)
1961 return -1;
1962 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1963 "socket: connect to %s:%d",
1964 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1965 return 0;
1968 static int net_socket_mcast_init(VLANState *vlan,
1969 const char *model,
1970 const char *name,
1971 const char *host_str)
1973 NetSocketState *s;
1974 int fd;
1975 struct sockaddr_in saddr;
1977 if (parse_host_port(&saddr, host_str) < 0)
1978 return -1;
1981 fd = net_socket_mcast_create(&saddr);
1982 if (fd < 0)
1983 return -1;
1985 s = net_socket_fd_init(vlan, model, name, fd, 0);
1986 if (!s)
1987 return -1;
1989 s->dgram_dst = saddr;
1991 snprintf(s->vc->info_str, sizeof(s->vc->info_str),
1992 "socket: mcast=%s:%d",
1993 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
1994 return 0;
1998 typedef struct DumpState {
1999 VLANClientState *pcap_vc;
2000 int fd;
2001 int pcap_caplen;
2002 } DumpState;
2004 #define PCAP_MAGIC 0xa1b2c3d4
2006 struct pcap_file_hdr {
2007 uint32_t magic;
2008 uint16_t version_major;
2009 uint16_t version_minor;
2010 int32_t thiszone;
2011 uint32_t sigfigs;
2012 uint32_t snaplen;
2013 uint32_t linktype;
2016 struct pcap_sf_pkthdr {
2017 struct {
2018 int32_t tv_sec;
2019 int32_t tv_usec;
2020 } ts;
2021 uint32_t caplen;
2022 uint32_t len;
2025 static ssize_t dump_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
2027 DumpState *s = vc->opaque;
2028 struct pcap_sf_pkthdr hdr;
2029 int64_t ts;
2030 int caplen;
2032 /* Early return in case of previous error. */
2033 if (s->fd < 0) {
2034 return size;
2037 ts = muldiv64(qemu_get_clock(vm_clock), 1000000, ticks_per_sec);
2038 caplen = size > s->pcap_caplen ? s->pcap_caplen : size;
2040 hdr.ts.tv_sec = ts / 1000000;
2041 hdr.ts.tv_usec = ts % 1000000;
2042 hdr.caplen = caplen;
2043 hdr.len = size;
2044 if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) ||
2045 write(s->fd, buf, caplen) != caplen) {
2046 qemu_log("-net dump write error - stop dump\n");
2047 close(s->fd);
2048 s->fd = -1;
2051 return size;
2054 static void net_dump_cleanup(VLANClientState *vc)
2056 DumpState *s = vc->opaque;
2058 close(s->fd);
2059 qemu_free(s);
2062 static int net_dump_init(Monitor *mon, VLANState *vlan, const char *device,
2063 const char *name, const char *filename, int len)
2065 struct pcap_file_hdr hdr;
2066 DumpState *s;
2068 s = qemu_malloc(sizeof(DumpState));
2070 s->fd = open(filename, O_CREAT | O_WRONLY | O_BINARY, 0644);
2071 if (s->fd < 0) {
2072 config_error(mon, "-net dump: can't open %s\n", filename);
2073 return -1;
2076 s->pcap_caplen = len;
2078 hdr.magic = PCAP_MAGIC;
2079 hdr.version_major = 2;
2080 hdr.version_minor = 4;
2081 hdr.thiszone = 0;
2082 hdr.sigfigs = 0;
2083 hdr.snaplen = s->pcap_caplen;
2084 hdr.linktype = 1;
2086 if (write(s->fd, &hdr, sizeof(hdr)) < sizeof(hdr)) {
2087 config_error(mon, "-net dump write error: %s\n", strerror(errno));
2088 close(s->fd);
2089 qemu_free(s);
2090 return -1;
2093 s->pcap_vc = qemu_new_vlan_client(vlan, device, name, NULL, dump_receive, NULL,
2094 net_dump_cleanup, s);
2095 snprintf(s->pcap_vc->info_str, sizeof(s->pcap_vc->info_str),
2096 "dump to %s (len=%d)", filename, len);
2097 return 0;
2100 /* find or alloc a new VLAN */
2101 VLANState *qemu_find_vlan(int id)
2103 VLANState **pvlan, *vlan;
2104 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2105 if (vlan->id == id)
2106 return vlan;
2108 vlan = qemu_mallocz(sizeof(VLANState));
2109 vlan->id = id;
2110 vlan->next = NULL;
2111 pvlan = &first_vlan;
2112 while (*pvlan != NULL)
2113 pvlan = &(*pvlan)->next;
2114 *pvlan = vlan;
2115 return vlan;
2118 static int nic_get_free_idx(void)
2120 int index;
2122 for (index = 0; index < MAX_NICS; index++)
2123 if (!nd_table[index].used)
2124 return index;
2125 return -1;
2128 void qemu_check_nic_model(NICInfo *nd, const char *model)
2130 const char *models[2];
2132 models[0] = model;
2133 models[1] = NULL;
2135 qemu_check_nic_model_list(nd, models, model);
2138 void qemu_check_nic_model_list(NICInfo *nd, const char * const *models,
2139 const char *default_model)
2141 int i, exit_status = 0;
2143 if (!nd->model)
2144 nd->model = strdup(default_model);
2146 if (strcmp(nd->model, "?") != 0) {
2147 for (i = 0 ; models[i]; i++)
2148 if (strcmp(nd->model, models[i]) == 0)
2149 return;
2151 fprintf(stderr, "qemu: Unsupported NIC model: %s\n", nd->model);
2152 exit_status = 1;
2155 fprintf(stderr, "qemu: Supported NIC models: ");
2156 for (i = 0 ; models[i]; i++)
2157 fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
2159 exit(exit_status);
2162 int net_client_init(Monitor *mon, const char *device, const char *p)
2164 char buf[1024];
2165 int vlan_id, ret;
2166 VLANState *vlan;
2167 char *name = NULL;
2169 vlan_id = 0;
2170 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
2171 vlan_id = strtol(buf, NULL, 0);
2173 vlan = qemu_find_vlan(vlan_id);
2175 if (get_param_value(buf, sizeof(buf), "name", p)) {
2176 name = qemu_strdup(buf);
2178 if (!strcmp(device, "nic")) {
2179 static const char * const nic_params[] = {
2180 "vlan", "name", "macaddr", "model", "addr", "vectors", NULL
2182 NICInfo *nd;
2183 uint8_t *macaddr;
2184 int idx = nic_get_free_idx();
2186 if (check_params(buf, sizeof(buf), nic_params, p) < 0) {
2187 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
2188 ret = -1;
2189 goto out;
2191 if (idx == -1 || nb_nics >= MAX_NICS) {
2192 config_error(mon, "Too Many NICs\n");
2193 ret = -1;
2194 goto out;
2196 nd = &nd_table[idx];
2197 macaddr = nd->macaddr;
2198 macaddr[0] = 0x52;
2199 macaddr[1] = 0x54;
2200 macaddr[2] = 0x00;
2201 macaddr[3] = 0x12;
2202 macaddr[4] = 0x34;
2203 macaddr[5] = 0x56 + idx;
2205 if (get_param_value(buf, sizeof(buf), "macaddr", p)) {
2206 if (parse_macaddr(macaddr, buf) < 0) {
2207 config_error(mon, "invalid syntax for ethernet address\n");
2208 ret = -1;
2209 goto out;
2212 if (get_param_value(buf, sizeof(buf), "model", p)) {
2213 nd->model = strdup(buf);
2215 if (get_param_value(buf, sizeof(buf), "addr", p)) {
2216 nd->devaddr = strdup(buf);
2218 nd->nvectors = NIC_NVECTORS_UNSPECIFIED;
2219 if (get_param_value(buf, sizeof(buf), "vectors", p)) {
2220 char *endptr;
2221 long vectors = strtol(buf, &endptr, 0);
2222 if (*endptr) {
2223 config_error(mon, "invalid syntax for # of vectors\n");
2224 ret = -1;
2225 goto out;
2227 if (vectors < 0 || vectors > 0x7ffffff) {
2228 config_error(mon, "invalid # of vectors\n");
2229 ret = -1;
2230 goto out;
2232 nd->nvectors = vectors;
2234 nd->vlan = vlan;
2235 nd->name = name;
2236 nd->used = 1;
2237 name = NULL;
2238 nb_nics++;
2239 vlan->nb_guest_devs++;
2240 ret = idx;
2241 } else
2242 if (!strcmp(device, "none")) {
2243 if (*p != '\0') {
2244 config_error(mon, "'none' takes no parameters\n");
2245 ret = -1;
2246 goto out;
2248 /* does nothing. It is needed to signal that no network cards
2249 are wanted */
2250 ret = 0;
2251 } else
2252 #ifdef CONFIG_SLIRP
2253 if (!strcmp(device, "user")) {
2254 static const char * const slirp_params[] = {
2255 "vlan", "name", "hostname", "restrict", "ip", "tftp", "bootfile",
2256 "smb", "redir", "channel", NULL
2258 struct slirp_config_str *config;
2259 char *tftp_export = NULL;
2260 char *bootfile = NULL;
2261 char *smb_export = NULL;
2262 int restricted = 0;
2263 char *ip = NULL;
2264 const char *q;
2266 if (check_params(buf, sizeof(buf), slirp_params, p) < 0) {
2267 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
2268 ret = -1;
2269 goto out;
2271 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
2272 pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
2274 if (get_param_value(buf, sizeof(buf), "restrict", p)) {
2275 restricted = (buf[0] == 'y') ? 1 : 0;
2277 if (get_param_value(buf, sizeof(buf), "ip", p)) {
2278 ip = qemu_strdup(buf);
2280 if (get_param_value(buf, sizeof(buf), "tftp", p)) {
2281 tftp_export = qemu_strdup(buf);
2283 if (get_param_value(buf, sizeof(buf), "bootfile", p)) {
2284 bootfile = qemu_strdup(buf);
2286 if (get_param_value(buf, sizeof(buf), "smb", p)) {
2287 smb_export = qemu_strdup(buf);
2289 q = p;
2290 while (1) {
2291 config = qemu_malloc(sizeof(*config));
2292 if (!get_next_param_value(config->str, sizeof(config->str),
2293 "redir", &q)) {
2294 break;
2296 config->flags = SLIRP_CFG_REDIR;
2297 config->next = slirp_configs;
2298 slirp_configs = config;
2299 config = NULL;
2301 q = p;
2302 while (1) {
2303 config = qemu_malloc(sizeof(*config));
2304 if (!get_next_param_value(config->str, sizeof(config->str),
2305 "channel", &q)) {
2306 break;
2308 config->flags = 0;
2309 config->next = slirp_configs;
2310 slirp_configs = config;
2311 config = NULL;
2313 qemu_free(config);
2314 vlan->nb_host_devs++;
2315 ret = net_slirp_init(mon, vlan, device, name, restricted, ip,
2316 tftp_export, bootfile, smb_export);
2317 qemu_free(ip);
2318 qemu_free(tftp_export);
2319 qemu_free(bootfile);
2320 qemu_free(smb_export);
2321 } else if (!strcmp(device, "channel")) {
2322 if (!slirp_inited) {
2323 struct slirp_config_str *config;
2325 config = qemu_malloc(sizeof(*config));
2326 pstrcpy(config->str, sizeof(config->str), p);
2327 config->flags = 0;
2328 config->next = slirp_configs;
2329 slirp_configs = config;
2330 } else {
2331 vmchannel_init(mon, p);
2333 ret = 0;
2334 } else
2335 #endif
2336 #ifdef _WIN32
2337 if (!strcmp(device, "tap")) {
2338 static const char * const tap_params[] = {
2339 "vlan", "name", "ifname", NULL
2341 char ifname[64];
2343 if (check_params(buf, sizeof(buf), tap_params, p) < 0) {
2344 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
2345 ret = -1;
2346 goto out;
2348 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
2349 config_error(mon, "tap: no interface name\n");
2350 ret = -1;
2351 goto out;
2353 vlan->nb_host_devs++;
2354 ret = tap_win32_init(vlan, device, name, ifname);
2355 } else
2356 #elif defined (_AIX)
2357 #else
2358 if (!strcmp(device, "tap")) {
2359 char ifname[64], chkbuf[64];
2360 char setup_script[1024], down_script[1024];
2361 TAPState *s;
2362 int fd;
2363 vlan->nb_host_devs++;
2364 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
2365 static const char * const fd_params[] = {
2366 "vlan", "name", "fd", "sndbuf", NULL
2368 if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
2369 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2370 ret = -1;
2371 goto out;
2373 fd = strtol(buf, NULL, 0);
2374 fcntl(fd, F_SETFL, O_NONBLOCK);
2375 s = net_tap_fd_init(vlan, device, name, fd);
2376 } else {
2377 static const char * const tap_params[] = {
2378 "vlan", "name", "ifname", "script", "downscript", "sndbuf", NULL
2380 if (check_params(chkbuf, sizeof(chkbuf), tap_params, p) < 0) {
2381 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2382 ret = -1;
2383 goto out;
2385 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
2386 ifname[0] = '\0';
2388 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
2389 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
2391 if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
2392 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
2394 s = net_tap_init(vlan, device, name, ifname, setup_script, down_script);
2396 if (s != NULL) {
2397 if (get_param_value(buf, sizeof(buf), "sndbuf", p)) {
2398 tap_set_sndbuf(s, atoi(buf), mon);
2400 ret = 0;
2401 } else {
2402 ret = -1;
2404 } else
2405 #endif
2406 if (!strcmp(device, "socket")) {
2407 char chkbuf[64];
2408 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) {
2409 static const char * const fd_params[] = {
2410 "vlan", "name", "fd", NULL
2412 int fd;
2413 if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) {
2414 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2415 ret = -1;
2416 goto out;
2418 fd = strtol(buf, NULL, 0);
2419 ret = -1;
2420 if (net_socket_fd_init(vlan, device, name, fd, 1))
2421 ret = 0;
2422 } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) {
2423 static const char * const listen_params[] = {
2424 "vlan", "name", "listen", NULL
2426 if (check_params(chkbuf, sizeof(chkbuf), listen_params, p) < 0) {
2427 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2428 ret = -1;
2429 goto out;
2431 ret = net_socket_listen_init(vlan, device, name, buf);
2432 } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) {
2433 static const char * const connect_params[] = {
2434 "vlan", "name", "connect", NULL
2436 if (check_params(chkbuf, sizeof(chkbuf), connect_params, p) < 0) {
2437 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2438 ret = -1;
2439 goto out;
2441 ret = net_socket_connect_init(vlan, device, name, buf);
2442 } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) {
2443 static const char * const mcast_params[] = {
2444 "vlan", "name", "mcast", NULL
2446 if (check_params(chkbuf, sizeof(chkbuf), mcast_params, p) < 0) {
2447 config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p);
2448 ret = -1;
2449 goto out;
2451 ret = net_socket_mcast_init(vlan, device, name, buf);
2452 } else {
2453 config_error(mon, "Unknown socket options: %s\n", p);
2454 ret = -1;
2455 goto out;
2457 vlan->nb_host_devs++;
2458 } else
2459 #ifdef CONFIG_VDE
2460 if (!strcmp(device, "vde")) {
2461 static const char * const vde_params[] = {
2462 "vlan", "name", "sock", "port", "group", "mode", NULL
2464 char vde_sock[1024], vde_group[512];
2465 int vde_port, vde_mode;
2467 if (check_params(buf, sizeof(buf), vde_params, p) < 0) {
2468 config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p);
2469 ret = -1;
2470 goto out;
2472 vlan->nb_host_devs++;
2473 if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
2474 vde_sock[0] = '\0';
2476 if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
2477 vde_port = strtol(buf, NULL, 10);
2478 } else {
2479 vde_port = 0;
2481 if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
2482 vde_group[0] = '\0';
2484 if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
2485 vde_mode = strtol(buf, NULL, 8);
2486 } else {
2487 vde_mode = 0700;
2489 ret = net_vde_init(vlan, device, name, vde_sock, vde_port, vde_group, vde_mode);
2490 } else
2491 #endif
2492 if (!strcmp(device, "dump")) {
2493 int len = 65536;
2495 if (get_param_value(buf, sizeof(buf), "len", p) > 0) {
2496 len = strtol(buf, NULL, 0);
2498 if (!get_param_value(buf, sizeof(buf), "file", p)) {
2499 snprintf(buf, sizeof(buf), "qemu-vlan%d.pcap", vlan_id);
2501 ret = net_dump_init(mon, vlan, device, name, buf, len);
2502 } else {
2503 config_error(mon, "Unknown network device: %s\n", device);
2504 ret = -1;
2505 goto out;
2507 if (ret < 0) {
2508 config_error(mon, "Could not initialize device '%s'\n", device);
2510 out:
2511 qemu_free(name);
2512 return ret;
2515 void net_client_uninit(NICInfo *nd)
2517 nd->vlan->nb_guest_devs--;
2518 nb_nics--;
2519 nd->used = 0;
2520 free((void *)nd->model);
2523 static int net_host_check_device(const char *device)
2525 int i;
2526 const char *valid_param_list[] = { "tap", "socket", "dump"
2527 #ifdef CONFIG_SLIRP
2528 ,"user"
2529 #endif
2530 #ifdef CONFIG_VDE
2531 ,"vde"
2532 #endif
2534 for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
2535 if (!strncmp(valid_param_list[i], device,
2536 strlen(valid_param_list[i])))
2537 return 1;
2540 return 0;
2543 void net_host_device_add(Monitor *mon, const char *device, const char *opts)
2545 if (!net_host_check_device(device)) {
2546 monitor_printf(mon, "invalid host network device %s\n", device);
2547 return;
2549 if (net_client_init(mon, device, opts ? opts : "") < 0) {
2550 monitor_printf(mon, "adding host network device %s failed\n", device);
2554 void net_host_device_remove(Monitor *mon, int vlan_id, const char *device)
2556 VLANState *vlan;
2557 VLANClientState *vc;
2559 vlan = qemu_find_vlan(vlan_id);
2561 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
2562 if (!strcmp(vc->name, device)) {
2563 break;
2567 if (!vc) {
2568 monitor_printf(mon, "can't find device %s\n", device);
2569 return;
2571 if (!net_host_check_device(vc->model)) {
2572 monitor_printf(mon, "invalid host network device %s\n", device);
2573 return;
2575 qemu_del_vlan_client(vc);
2578 int net_client_parse(const char *str)
2580 const char *p;
2581 char *q;
2582 char device[64];
2584 p = str;
2585 q = device;
2586 while (*p != '\0' && *p != ',') {
2587 if ((q - device) < sizeof(device) - 1)
2588 *q++ = *p;
2589 p++;
2591 *q = '\0';
2592 if (*p == ',')
2593 p++;
2595 return net_client_init(NULL, device, p);
2598 void net_set_boot_mask(int net_boot_mask)
2600 int i;
2602 /* Only the first four NICs may be bootable */
2603 net_boot_mask = net_boot_mask & 0xF;
2605 for (i = 0; i < nb_nics; i++) {
2606 if (net_boot_mask & (1 << i)) {
2607 nd_table[i].bootable = 1;
2608 net_boot_mask &= ~(1 << i);
2612 if (net_boot_mask) {
2613 fprintf(stderr, "Cannot boot from non-existent NIC\n");
2614 exit(1);
2618 void do_info_network(Monitor *mon)
2620 VLANState *vlan;
2621 VLANClientState *vc;
2623 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2624 monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
2625 for(vc = vlan->first_client; vc != NULL; vc = vc->next)
2626 monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
2630 int do_set_link(Monitor *mon, const char *name, const char *up_or_down)
2632 VLANState *vlan;
2633 VLANClientState *vc = NULL;
2635 for (vlan = first_vlan; vlan != NULL; vlan = vlan->next)
2636 for (vc = vlan->first_client; vc != NULL; vc = vc->next)
2637 if (strcmp(vc->name, name) == 0)
2638 goto done;
2639 done:
2641 if (!vc) {
2642 monitor_printf(mon, "could not find network device '%s'", name);
2643 return 0;
2646 if (strcmp(up_or_down, "up") == 0)
2647 vc->link_down = 0;
2648 else if (strcmp(up_or_down, "down") == 0)
2649 vc->link_down = 1;
2650 else
2651 monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' "
2652 "valid\n", up_or_down);
2654 if (vc->link_status_changed)
2655 vc->link_status_changed(vc);
2657 return 1;
2660 void net_cleanup(void)
2662 VLANState *vlan;
2664 /* close network clients */
2665 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2666 VLANClientState *vc = vlan->first_client;
2668 while (vc) {
2669 VLANClientState *next = vc->next;
2671 qemu_del_vlan_client(vc);
2673 vc = next;
2678 void net_client_check(void)
2680 VLANState *vlan;
2682 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2683 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0)
2684 continue;
2685 if (vlan->nb_guest_devs == 0)
2686 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
2687 if (vlan->nb_host_devs == 0)
2688 fprintf(stderr,
2689 "Warning: vlan %d is not connected to host network\n",
2690 vlan->id);