]> git.proxmox.com Git - qemu.git/blob - net.c
slirp: Move smb, redir, tftp and bootp parameters and -net channel
[qemu.git] / net.c
1 /*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
30 #include <zlib.h>
31
32 /* Needed early for HOST_BSD etc. */
33 #include "config-host.h"
34
35 #ifndef _WIN32
36 #include <sys/times.h>
37 #include <sys/wait.h>
38 #include <termios.h>
39 #include <sys/mman.h>
40 #include <sys/ioctl.h>
41 #include <sys/resource.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <net/if.h>
45 #ifdef __NetBSD__
46 #include <net/if_tap.h>
47 #endif
48 #ifdef __linux__
49 #include <linux/if_tun.h>
50 #endif
51 #include <arpa/inet.h>
52 #include <dirent.h>
53 #include <netdb.h>
54 #include <sys/select.h>
55 #ifdef HOST_BSD
56 #include <sys/stat.h>
57 #if defined(__FreeBSD__) || defined(__DragonFly__)
58 #include <libutil.h>
59 #else
60 #include <util.h>
61 #endif
62 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
63 #include <freebsd/stdlib.h>
64 #else
65 #ifdef __linux__
66 #include <pty.h>
67 #include <malloc.h>
68 #include <linux/rtc.h>
69
70 /* For the benefit of older linux systems which don't supply it,
71 we use a local copy of hpet.h. */
72 /* #include <linux/hpet.h> */
73 #include "hpet.h"
74
75 #include <linux/ppdev.h>
76 #include <linux/parport.h>
77 #endif
78 #ifdef __sun__
79 #include <sys/stat.h>
80 #include <sys/ethernet.h>
81 #include <sys/sockio.h>
82 #include <netinet/arp.h>
83 #include <netinet/in.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/ip.h>
86 #include <netinet/ip_icmp.h> // must come after ip.h
87 #include <netinet/udp.h>
88 #include <netinet/tcp.h>
89 #include <net/if.h>
90 #include <syslog.h>
91 #include <stropts.h>
92 #endif
93 #endif
94 #endif
95
96 #if defined(__OpenBSD__)
97 #include <util.h>
98 #endif
99
100 #if defined(CONFIG_VDE)
101 #include <libvdeplug.h>
102 #endif
103
104 #ifdef _WIN32
105 #include <windows.h>
106 #include <malloc.h>
107 #include <sys/timeb.h>
108 #include <mmsystem.h>
109 #define getopt_long_only getopt_long
110 #define memalign(align, size) malloc(size)
111 #endif
112
113 #include "qemu-common.h"
114 #include "net.h"
115 #include "monitor.h"
116 #include "sysemu.h"
117 #include "qemu-timer.h"
118 #include "qemu-char.h"
119 #include "audio/audio.h"
120 #include "qemu_socket.h"
121 #include "qemu-log.h"
122
123 #if defined(CONFIG_SLIRP)
124 #include "libslirp.h"
125 #endif
126
127
128 static VLANState *first_vlan;
129
130 /***********************************************************/
131 /* network device redirectors */
132
133 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP)
134 static void hex_dump(FILE *f, const uint8_t *buf, int size)
135 {
136 int len, i, j, c;
137
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, " ");
148 }
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);
155 }
156 fprintf(f, "\n");
157 }
158 }
159 #endif
160
161 static int parse_macaddr(uint8_t *macaddr, const char *p)
162 {
163 int i;
164 char *last_char;
165 long int offset;
166
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++;
185 }
186 }
187 return 0;
188 }
189
190 return -1;
191 }
192
193 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
194 {
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';
208 }
209 *pp = p1;
210 return 0;
211 }
212
213 int parse_host_src_port(struct sockaddr_in *haddr,
214 struct sockaddr_in *saddr,
215 const char *input_str)
216 {
217 char *str = strdup(input_str);
218 char *host_str = str;
219 char *src_str;
220 const char *src_str2;
221 char *ptr;
222
223 /*
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".
227 */
228 if ((ptr = strchr(str,',')))
229 *ptr = '\0';
230
231 if ((src_str = strchr(input_str,'@'))) {
232 *src_str = '\0';
233 src_str++;
234 }
235
236 if (parse_host_port(haddr, host_str) < 0)
237 goto fail;
238
239 src_str2 = src_str;
240 if (!src_str || *src_str == '\0')
241 src_str2 = ":0";
242
243 if (parse_host_port(saddr, src_str2) < 0)
244 goto fail;
245
246 free(str);
247 return(0);
248
249 fail:
250 free(str);
251 return -1;
252 }
253
254 int parse_host_port(struct sockaddr_in *saddr, const char *str)
255 {
256 char buf[512];
257 struct hostent *he;
258 const char *p, *r;
259 int port;
260
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;
275 }
276 }
277 port = strtol(p, (char **)&r, 0);
278 if (r == p)
279 return -1;
280 saddr->sin_port = htons(port);
281 return 0;
282 }
283
284 #if !defined(_WIN32) && 0
285 static int parse_unix_path(struct sockaddr_un *uaddr, const char *str)
286 {
287 const char *p;
288 int len;
289
290 len = MIN(108, strlen(str));
291 p = strchr(str, ',');
292 if (p)
293 len = MIN(len, p - str);
294
295 memset(uaddr, 0, sizeof(*uaddr));
296
297 uaddr->sun_family = AF_UNIX;
298 memcpy(uaddr->sun_path, str, len);
299
300 return 0;
301 }
302 #endif
303
304 void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6])
305 {
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]);
311 }
312
313 static char *assign_name(VLANClientState *vc1, const char *model)
314 {
315 VLANState *vlan;
316 char buf[256];
317 int id = 0;
318
319 for (vlan = first_vlan; vlan; vlan = vlan->next) {
320 VLANClientState *vc;
321
322 for (vc = vlan->first_client; vc; vc = vc->next)
323 if (vc != vc1 && strcmp(vc->model, model) == 0)
324 id++;
325 }
326
327 snprintf(buf, sizeof(buf), "%s.%d", model, id);
328
329 return strdup(buf);
330 }
331
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)
340 {
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;
354
355 vc->next = NULL;
356 pvc = &vlan->first_client;
357 while (*pvc != NULL)
358 pvc = &(*pvc)->next;
359 *pvc = vc;
360 return vc;
361 }
362
363 void qemu_del_vlan_client(VLANClientState *vc)
364 {
365 VLANClientState **pvc = &vc->vlan->first_client;
366
367 while (*pvc != NULL)
368 if (*pvc == vc) {
369 *pvc = vc->next;
370 if (vc->cleanup) {
371 vc->cleanup(vc);
372 }
373 free(vc->name);
374 free(vc->model);
375 qemu_free(vc);
376 break;
377 } else
378 pvc = &(*pvc)->next;
379 }
380
381 VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque)
382 {
383 VLANClientState **pvc = &vlan->first_client;
384
385 while (*pvc != NULL)
386 if ((*pvc)->opaque == opaque)
387 return *pvc;
388 else
389 pvc = &(*pvc)->next;
390
391 return NULL;
392 }
393
394 int qemu_can_send_packet(VLANClientState *sender)
395 {
396 VLANState *vlan = sender->vlan;
397 VLANClientState *vc;
398
399 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
400 if (vc == sender) {
401 continue;
402 }
403
404 /* no can_receive() handler, they can always receive */
405 if (!vc->can_receive || vc->can_receive(vc)) {
406 return 1;
407 }
408 }
409 return 0;
410 }
411
412 static int
413 qemu_deliver_packet(VLANClientState *sender, const uint8_t *buf, int size)
414 {
415 VLANClientState *vc;
416 int ret = -1;
417
418 sender->vlan->delivering = 1;
419
420 for (vc = sender->vlan->first_client; vc != NULL; vc = vc->next) {
421 ssize_t len;
422
423 if (vc == sender) {
424 continue;
425 }
426
427 if (vc->link_down) {
428 ret = size;
429 continue;
430 }
431
432 len = vc->receive(vc, buf, size);
433
434 ret = (ret >= 0) ? ret : len;
435 }
436
437 sender->vlan->delivering = 0;
438
439 return ret;
440 }
441
442 void qemu_purge_queued_packets(VLANClientState *vc)
443 {
444 VLANPacket **pp = &vc->vlan->send_queue;
445
446 while (*pp != NULL) {
447 VLANPacket *packet = *pp;
448
449 if (packet->sender == vc) {
450 *pp = packet->next;
451 qemu_free(packet);
452 } else {
453 pp = &packet->next;
454 }
455 }
456 }
457
458 void qemu_flush_queued_packets(VLANClientState *vc)
459 {
460 VLANPacket *packet;
461
462 while ((packet = vc->vlan->send_queue) != NULL) {
463 int ret;
464
465 vc->vlan->send_queue = packet->next;
466
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;
472 }
473
474 if (packet->sent_cb)
475 packet->sent_cb(packet->sender, ret);
476
477 qemu_free(packet);
478 }
479 }
480
481 static void qemu_enqueue_packet(VLANClientState *sender,
482 const uint8_t *buf, int size,
483 NetPacketSent *sent_cb)
484 {
485 VLANPacket *packet;
486
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;
494 }
495
496 ssize_t qemu_send_packet_async(VLANClientState *sender,
497 const uint8_t *buf, int size,
498 NetPacketSent *sent_cb)
499 {
500 int ret;
501
502 if (sender->link_down) {
503 return size;
504 }
505
506 #ifdef DEBUG_NET
507 printf("vlan %d send:\n", sender->vlan->id);
508 hex_dump(stdout, buf, size);
509 #endif
510
511 if (sender->vlan->delivering) {
512 qemu_enqueue_packet(sender, buf, size, NULL);
513 return size;
514 }
515
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;
520 }
521
522 qemu_flush_queued_packets(sender);
523
524 return ret;
525 }
526
527 void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size)
528 {
529 qemu_send_packet_async(vc, buf, size, NULL);
530 }
531
532 static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov,
533 int iovcnt)
534 {
535 uint8_t buffer[4096];
536 size_t offset = 0;
537 int i;
538
539 for (i = 0; i < iovcnt; i++) {
540 size_t len;
541
542 len = MIN(sizeof(buffer) - offset, iov[i].iov_len);
543 memcpy(buffer + offset, iov[i].iov_base, len);
544 offset += len;
545 }
546
547 return vc->receive(vc, buffer, offset);
548 }
549
550 static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt)
551 {
552 size_t offset = 0;
553 int i;
554
555 for (i = 0; i < iovcnt; i++)
556 offset += iov[i].iov_len;
557 return offset;
558 }
559
560 static int qemu_deliver_packet_iov(VLANClientState *sender,
561 const struct iovec *iov, int iovcnt)
562 {
563 VLANClientState *vc;
564 int ret = -1;
565
566 sender->vlan->delivering = 1;
567
568 for (vc = sender->vlan->first_client; vc != NULL; vc = vc->next) {
569 ssize_t len;
570
571 if (vc == sender) {
572 continue;
573 }
574
575 if (vc->link_down) {
576 ret = calc_iov_length(iov, iovcnt);
577 continue;
578 }
579
580 if (vc->receive_iov) {
581 len = vc->receive_iov(vc, iov, iovcnt);
582 } else {
583 len = vc_sendv_compat(vc, iov, iovcnt);
584 }
585
586 ret = (ret >= 0) ? ret : len;
587 }
588
589 sender->vlan->delivering = 0;
590
591 return ret;
592 }
593
594 static ssize_t qemu_enqueue_packet_iov(VLANClientState *sender,
595 const struct iovec *iov, int iovcnt,
596 NetPacketSent *sent_cb)
597 {
598 VLANPacket *packet;
599 size_t max_len = 0;
600 int i;
601
602 max_len = calc_iov_length(iov, iovcnt);
603
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;
609
610 for (i = 0; i < iovcnt; i++) {
611 size_t len = iov[i].iov_len;
612
613 memcpy(packet->data + packet->size, iov[i].iov_base, len);
614 packet->size += len;
615 }
616
617 sender->vlan->send_queue = packet;
618
619 return packet->size;
620 }
621
622 ssize_t qemu_sendv_packet_async(VLANClientState *sender,
623 const struct iovec *iov, int iovcnt,
624 NetPacketSent *sent_cb)
625 {
626 int ret;
627
628 if (sender->link_down) {
629 return calc_iov_length(iov, iovcnt);
630 }
631
632 if (sender->vlan->delivering) {
633 return qemu_enqueue_packet_iov(sender, iov, iovcnt, NULL);
634 }
635
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;
640 }
641
642 qemu_flush_queued_packets(sender);
643
644 return ret;
645 }
646
647 ssize_t
648 qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt)
649 {
650 return qemu_sendv_packet_async(vc, iov, iovcnt, NULL);
651 }
652
653 static void config_error(Monitor *mon, const char *fmt, ...)
654 {
655 va_list ap;
656
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);
664 }
665 va_end(ap);
666 }
667
668 #if defined(CONFIG_SLIRP)
669
670 /* slirp network adapter */
671
672 #define SLIRP_CFG_REDIR 1
673
674 struct slirp_config_str {
675 struct slirp_config_str *next;
676 int flags;
677 char str[1024];
678 };
679
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;
685
686 static void slirp_redirection(Monitor *mon, const char *redir_str);
687 static void vmchannel_init(Monitor *mon, const char *config_str);
688
689 #ifndef _WIN32
690 static const char *legacy_smb_export;
691
692 static void slirp_smb(const char *exported_dir);
693 #endif
694
695 int slirp_can_output(void)
696 {
697 return !slirp_vc || qemu_can_send_packet(slirp_vc);
698 }
699
700 void slirp_output(const uint8_t *pkt, int pkt_len)
701 {
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);
709 }
710
711 int slirp_is_inited(void)
712 {
713 return slirp_inited;
714 }
715
716 static ssize_t slirp_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
717 {
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;
724 }
725
726 static int slirp_in_use;
727
728 static void net_slirp_cleanup(VLANClientState *vc)
729 {
730 slirp_in_use = 0;
731 }
732
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)
737 {
738 if (slirp_in_use) {
739 /* slirp only supports a single instance so far */
740 return -1;
741 }
742 if (!slirp_inited) {
743 if (!tftp_export) {
744 tftp_export = legacy_tftp_prefix;
745 }
746 if (!bootfile) {
747 bootfile = legacy_bootp_filename;
748 }
749 slirp_inited = 1;
750 slirp_init(restricted, ip, tftp_export, bootfile);
751
752 while (slirp_configs) {
753 struct slirp_config_str *config = slirp_configs;
754
755 if (config->flags & SLIRP_CFG_REDIR) {
756 slirp_redirection(mon, config->str);
757 } else {
758 vmchannel_init(mon, config->str);
759 }
760 slirp_configs = config->next;
761 qemu_free(config);
762 }
763 #ifndef _WIN32
764 if (!smb_export) {
765 smb_export = legacy_smb_export;
766 }
767 if (smb_export) {
768 slirp_smb(smb_export);
769 }
770 #endif
771 }
772
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;
778 }
779
780 static void net_slirp_redir_rm(Monitor *mon, const char *port_str)
781 {
782 int host_port;
783 char buf[256] = "";
784 const char *p = port_str;
785 int is_udp = 0;
786 int n;
787
788 if (!mon)
789 return;
790
791 if (!port_str || !port_str[0])
792 goto fail_syntax;
793
794 get_str_sep(buf, sizeof(buf), &p, ':');
795
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;
802 }
803
804 host_port = atoi(p);
805
806 n = slirp_redir_rm(is_udp, host_port);
807
808 monitor_printf(mon, "removed %d redirections to %s port %d\n", n,
809 is_udp ? "udp" : "tcp", host_port);
810 return;
811
812 fail_syntax:
813 monitor_printf(mon, "invalid format\n");
814 }
815
816 static void slirp_redirection(Monitor *mon, const char *redir_str)
817 {
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;
823
824 p = redir_str;
825 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
826 goto fail_syntax;
827 }
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;
834 }
835
836 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
837 goto fail_syntax;
838 }
839 host_port = strtol(buf, &r, 0);
840 if (r == buf) {
841 goto fail_syntax;
842 }
843
844 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
845 goto fail_syntax;
846 }
847 if (buf[0] == '\0') {
848 pstrcpy(buf, sizeof(buf), "10.0.2.15");
849 }
850 if (!inet_aton(buf, &guest_addr)) {
851 goto fail_syntax;
852 }
853
854 guest_port = strtol(p, &r, 0);
855 if (r == p) {
856 goto fail_syntax;
857 }
858
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);
861 }
862 return;
863
864 fail_syntax:
865 config_error(mon, "invalid redirection format '%s'\n", redir_str);
866 }
867
868 void net_slirp_redir(Monitor *mon, const char *redir_str, const char *redir_opt2)
869 {
870 struct slirp_config_str *config;
871
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;
881 }
882 return;
883 }
884
885 if (!strcmp(redir_str, "remove")) {
886 net_slirp_redir_rm(mon, redir_opt2);
887 return;
888 }
889
890 slirp_redirection(mon, redir_str);
891 }
892
893 #ifndef _WIN32
894
895 static char smb_dir[1024];
896
897 static void erase_dir(char *dir_name)
898 {
899 DIR *d;
900 struct dirent *de;
901 char filename[1024];
902
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);
915 }
916 }
917 closedir(d);
918 rmdir(dir_name);
919 }
920 }
921
922 /* automatic user mode samba server configuration */
923 static void smb_exit(void)
924 {
925 erase_dir(smb_dir);
926 }
927
928 static void slirp_smb(const char *exported_dir)
929 {
930 char smb_conf[1024];
931 char smb_cmdline[1024];
932 FILE *f;
933
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);
939 }
940 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf");
941
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);
946 }
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
967 );
968 fclose(f);
969 atexit(smb_exit);
970
971 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s",
972 SMBD_COMMAND, smb_conf);
973
974 slirp_add_exec(0, smb_cmdline, 4, 139);
975 }
976
977 /* automatic user mode samba server configuration (legacy interface) */
978 void net_slirp_smb(const char *exported_dir)
979 {
980 if (legacy_smb_export) {
981 fprintf(stderr, "-smb given twice\n");
982 exit(1);
983 }
984 legacy_smb_export = exported_dir;
985 if (slirp_inited) {
986 slirp_smb(exported_dir);
987 }
988 }
989
990 #endif /* !defined(_WIN32) */
991
992 void do_info_slirp(Monitor *mon)
993 {
994 slirp_stats();
995 }
996
997 struct VMChannel {
998 CharDriverState *hd;
999 int port;
1000 };
1001
1002 static int vmchannel_can_read(void *opaque)
1003 {
1004 struct VMChannel *vmc = (struct VMChannel*)opaque;
1005 return slirp_socket_can_recv(4, vmc->port);
1006 }
1007
1008 static void vmchannel_read(void *opaque, const uint8_t *buf, int size)
1009 {
1010 struct VMChannel *vmc = (struct VMChannel*)opaque;
1011 slirp_socket_recv(4, vmc->port, buf, size);
1012 }
1013
1014 static void vmchannel_init(Monitor *mon, const char *config_str)
1015 {
1016 struct VMChannel *vmc;
1017 char *devname;
1018 char name[20];
1019 int port;
1020
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;
1025 }
1026 devname++;
1027
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;
1035 }
1036 vmc->port = port;
1037
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;
1042 }
1043
1044 #endif /* CONFIG_SLIRP */
1045
1046 #if !defined(_WIN32)
1047
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;
1057
1058 static int launch_script(const char *setup_script, const char *ifname, int fd);
1059
1060 static int tap_can_send(void *opaque);
1061 static void tap_send(void *opaque);
1062 static void tap_writable(void *opaque);
1063
1064 static void tap_update_fd_handler(TAPState *s)
1065 {
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,
1070 s);
1071 }
1072
1073 static void tap_read_poll(TAPState *s, int enable)
1074 {
1075 s->read_poll = !!enable;
1076 tap_update_fd_handler(s);
1077 }
1078
1079 static void tap_write_poll(TAPState *s, int enable)
1080 {
1081 s->write_poll = !!enable;
1082 tap_update_fd_handler(s);
1083 }
1084
1085 static void tap_writable(void *opaque)
1086 {
1087 TAPState *s = opaque;
1088
1089 tap_write_poll(s, 0);
1090
1091 qemu_flush_queued_packets(s->vc);
1092 }
1093
1094 static ssize_t tap_receive_iov(VLANClientState *vc, const struct iovec *iov,
1095 int iovcnt)
1096 {
1097 TAPState *s = vc->opaque;
1098 ssize_t len;
1099
1100 do {
1101 len = writev(s->fd, iov, iovcnt);
1102 } while (len == -1 && errno == EINTR);
1103
1104 if (len == -1 && errno == EAGAIN) {
1105 tap_write_poll(s, 1);
1106 return 0;
1107 }
1108
1109 return len;
1110 }
1111
1112 static ssize_t tap_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
1113 {
1114 TAPState *s = vc->opaque;
1115 ssize_t len;
1116
1117 do {
1118 len = write(s->fd, buf, size);
1119 } while (len == -1 && (errno == EINTR || errno == EAGAIN));
1120
1121 return len;
1122 }
1123
1124 static int tap_can_send(void *opaque)
1125 {
1126 TAPState *s = opaque;
1127
1128 return qemu_can_send_packet(s->vc);
1129 }
1130
1131 #ifdef __sun__
1132 static ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
1133 {
1134 struct strbuf sbuf;
1135 int f = 0;
1136
1137 sbuf.maxlen = maxlen;
1138 sbuf.buf = (char *)buf;
1139
1140 return getmsg(tapfd, NULL, &sbuf, &f) >= 0 ? sbuf.len : -1;
1141 }
1142 #else
1143 static ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen)
1144 {
1145 return read(tapfd, buf, maxlen);
1146 }
1147 #endif
1148
1149 static void tap_send_completed(VLANClientState *vc, ssize_t len)
1150 {
1151 TAPState *s = vc->opaque;
1152 tap_read_poll(s, 1);
1153 }
1154
1155 static void tap_send(void *opaque)
1156 {
1157 TAPState *s = opaque;
1158 int size;
1159
1160 do {
1161 size = tap_read_packet(s->fd, s->buf, sizeof(s->buf));
1162 if (size <= 0) {
1163 break;
1164 }
1165
1166 size = qemu_send_packet_async(s->vc, s->buf, size, tap_send_completed);
1167 if (size == 0) {
1168 tap_read_poll(s, 0);
1169 }
1170 } while (size > 0);
1171 }
1172
1173 static void tap_set_sndbuf(TAPState *s, int sndbuf, Monitor *mon)
1174 {
1175 #ifdef TUNSETSNDBUF
1176 if (ioctl(s->fd, TUNSETSNDBUF, &sndbuf) == -1) {
1177 config_error(mon, "TUNSETSNDBUF ioctl failed: %s\n",
1178 strerror(errno));
1179 }
1180 #else
1181 config_error(mon, "No '-net tap,sndbuf=<nbytes>' support available\n");
1182 #endif
1183 }
1184
1185 static void tap_cleanup(VLANClientState *vc)
1186 {
1187 TAPState *s = vc->opaque;
1188
1189 qemu_purge_queued_packets(vc);
1190
1191 if (s->down_script[0])
1192 launch_script(s->down_script, s->down_script_arg, s->fd);
1193
1194 tap_read_poll(s, 0);
1195 tap_write_poll(s, 0);
1196 close(s->fd);
1197 qemu_free(s);
1198 }
1199
1200 /* fd support */
1201
1202 static TAPState *net_tap_fd_init(VLANState *vlan,
1203 const char *model,
1204 const char *name,
1205 int fd)
1206 {
1207 TAPState *s;
1208
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;
1216 }
1217
1218 #if defined (HOST_BSD) || defined (__FreeBSD_kernel__)
1219 static int tap_open(char *ifname, int ifname_size)
1220 {
1221 int fd;
1222 char *dev;
1223 struct stat s;
1224
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;
1229 }
1230
1231 fstat(fd, &s);
1232 dev = devname(s.st_rdev, S_IFCHR);
1233 pstrcpy(ifname, ifname_size, dev);
1234
1235 fcntl(fd, F_SETFL, O_NONBLOCK);
1236 return fd;
1237 }
1238 #elif defined(__sun__)
1239 #define TUNNEWPPA (('T'<<16) | 0x0001)
1240 /*
1241 * Allocate TAP device, returns opened fd.
1242 * Stores dev name in the first arg(must be large enough).
1243 */
1244 static int tap_alloc(char *dev, size_t dev_size)
1245 {
1246 int tap_fd, if_fd, ppa = -1;
1247 static int ip_fd = 0;
1248 char *ptr;
1249
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] = "";
1256
1257 memset(&ifr, 0x0, sizeof(ifr));
1258
1259 if( *dev ){
1260 ptr = dev;
1261 while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++;
1262 ppa = atoi(ptr);
1263 }
1264
1265 /* Check if IP device was opened */
1266 if( ip_fd )
1267 close(ip_fd);
1268
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;
1273 }
1274
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;
1279 }
1280
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");
1288
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;
1293 }
1294 if(ioctl(if_fd, I_PUSH, "ip") < 0){
1295 syslog(LOG_ERR, "Can't push IP module");
1296 return -1;
1297 }
1298
1299 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0)
1300 syslog(LOG_ERR, "Can't get flags\n");
1301
1302 snprintf (actual_name, 32, "tap%d", ppa);
1303 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name);
1304
1305 ifr.lifr_ppa = ppa;
1306 /* Assign ppa according to the unit number returned by tun device */
1307
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)");
1315
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");
1325
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");
1333 }
1334
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;
1338 }
1339
1340 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0)
1341 syslog (LOG_ERR, "Can't link TAP device to ARP");
1342
1343 close (if_fd);
1344
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;
1349
1350 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0)
1351 {
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");
1355 }
1356
1357 snprintf(dev, dev_size, "tap%d", ppa);
1358 return tap_fd;
1359 }
1360
1361 static int tap_open(char *ifname, int ifname_size)
1362 {
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;
1368 }
1369 pstrcpy(ifname, ifname_size, dev);
1370 fcntl(fd, F_SETFL, O_NONBLOCK);
1371 return fd;
1372 }
1373 #elif defined (_AIX)
1374 static int tap_open(char *ifname, int ifname_size)
1375 {
1376 fprintf (stderr, "no tap on AIX\n");
1377 return -1;
1378 }
1379 #else
1380 static int tap_open(char *ifname, int ifname_size)
1381 {
1382 struct ifreq ifr;
1383 int fd, ret;
1384
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;
1389 }
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;
1401 }
1402 pstrcpy(ifname, ifname_size, ifr.ifr_name);
1403 fcntl(fd, F_SETFL, O_NONBLOCK);
1404 return fd;
1405 }
1406 #endif
1407
1408 static int launch_script(const char *setup_script, const char *ifname, int fd)
1409 {
1410 sigset_t oldmask, mask;
1411 int pid, status;
1412 char *args[3];
1413 char **parg;
1414
1415 sigemptyset(&mask);
1416 sigaddset(&mask, SIGCHLD);
1417 sigprocmask(SIG_BLOCK, &mask, &oldmask);
1418
1419 /* try to launch network script */
1420 pid = fork();
1421 if (pid == 0) {
1422 int open_max = sysconf(_SC_OPEN_MAX), i;
1423
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);
1430 }
1431 }
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 */
1441 }
1442 sigprocmask(SIG_SETMASK, &oldmask, NULL);
1443
1444 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1445 return 0;
1446 }
1447 }
1448 fprintf(stderr, "%s: could not launch network script\n", setup_script);
1449 return -1;
1450 }
1451
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)
1455 {
1456 TAPState *s;
1457 int fd;
1458 char ifname[128];
1459
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;
1467
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;
1473 }
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);
1481 }
1482 return s;
1483 }
1484
1485 #endif /* !_WIN32 */
1486
1487 #if defined(CONFIG_VDE)
1488 typedef struct VDEState {
1489 VLANClientState *vc;
1490 VDECONN *vde;
1491 } VDEState;
1492
1493 static void vde_to_qemu(void *opaque)
1494 {
1495 VDEState *s = opaque;
1496 uint8_t buf[4096];
1497 int size;
1498
1499 size = vde_recv(s->vde, (char *)buf, sizeof(buf), 0);
1500 if (size > 0) {
1501 qemu_send_packet(s->vc, buf, size);
1502 }
1503 }
1504
1505 static ssize_t vde_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
1506 {
1507 VDEState *s = vc->opaque;
1508 ssize_t ret;
1509
1510 do {
1511 ret = vde_send(s->vde, (const char *)buf, size, 0);
1512 } while (ret < 0 && errno == EINTR);
1513
1514 return ret;
1515 }
1516
1517 static void vde_cleanup(VLANClientState *vc)
1518 {
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);
1523 }
1524
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)
1528 {
1529 VDEState *s;
1530 char *init_group = strlen(group) ? (char *)group : NULL;
1531 char *init_sock = strlen(sock) ? (char *)sock : NULL;
1532
1533 struct vde_open_args args = {
1534 .port = port,
1535 .group = init_group,
1536 .mode = mode,
1537 };
1538
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;
1544 }
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;
1551 }
1552 #endif
1553
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;
1564
1565 typedef struct NetSocketListenState {
1566 VLANState *vlan;
1567 char *model;
1568 char *name;
1569 int fd;
1570 } NetSocketListenState;
1571
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)
1574 {
1575 NetSocketState *s = vc->opaque;
1576 uint32_t len;
1577 len = htonl(size);
1578
1579 send_all(s->fd, (const uint8_t *)&len, sizeof(len));
1580 return send_all(s->fd, buf, size);
1581 }
1582
1583 static ssize_t net_socket_receive_dgram(VLANClientState *vc, const uint8_t *buf, size_t size)
1584 {
1585 NetSocketState *s = vc->opaque;
1586
1587 return sendto(s->fd, (const void *)buf, size, 0,
1588 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst));
1589 }
1590
1591 static void net_socket_send(void *opaque)
1592 {
1593 NetSocketState *s = opaque;
1594 int size, err;
1595 unsigned l;
1596 uint8_t buf1[4096];
1597 const uint8_t *buf;
1598
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;
1610 }
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;
1628 }
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;
1641 }
1642
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;
1650 }
1651 break;
1652 }
1653 }
1654 }
1655
1656 static void net_socket_send_dgram(void *opaque)
1657 {
1658 NetSocketState *s = opaque;
1659 int size;
1660
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;
1668 }
1669 qemu_send_packet(s->vc, s->buf, size);
1670 }
1671
1672 static int net_socket_mcast_create(struct sockaddr_in *mcastaddr)
1673 {
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;
1682
1683 }
1684 fd = socket(PF_INET, SOCK_DGRAM, 0);
1685 if (fd < 0) {
1686 perror("socket(PF_INET, SOCK_DGRAM)");
1687 return -1;
1688 }
1689
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;
1696 }
1697
1698 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr));
1699 if (ret < 0) {
1700 perror("bind");
1701 goto fail;
1702 }
1703
1704 /* Add host to multicast group */
1705 imr.imr_multiaddr = mcastaddr->sin_addr;
1706 imr.imr_interface.s_addr = htonl(INADDR_ANY);
1707
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;
1713 }
1714
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;
1722 }
1723
1724 socket_set_nonblock(fd);
1725 return fd;
1726 fail:
1727 if (fd >= 0)
1728 closesocket(fd);
1729 return -1;
1730 }
1731
1732 static void net_socket_cleanup(VLANClientState *vc)
1733 {
1734 NetSocketState *s = vc->opaque;
1735 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
1736 close(s->fd);
1737 qemu_free(s);
1738 }
1739
1740 static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan,
1741 const char *model,
1742 const char *name,
1743 int fd, int is_connected)
1744 {
1745 struct sockaddr_in saddr;
1746 int newfd;
1747 socklen_t saddr_len;
1748 NetSocketState *s;
1749
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
1753 */
1754
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;
1762 }
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;
1769 }
1770 /* clone newfd to fd, close newfd */
1771 dup2(newfd, fd);
1772 close(newfd);
1773
1774 } else {
1775 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n",
1776 fd, strerror(errno));
1777 return NULL;
1778 }
1779 }
1780
1781 s = qemu_mallocz(sizeof(NetSocketState));
1782 s->fd = fd;
1783
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);
1787
1788 /* mcast: save bound address as dst */
1789 if (is_connected) s->dgram_dst=saddr;
1790
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;
1796 }
1797
1798 static void net_socket_connect(void *opaque)
1799 {
1800 NetSocketState *s = opaque;
1801 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s);
1802 }
1803
1804 static NetSocketState *net_socket_fd_init_stream(VLANState *vlan,
1805 const char *model,
1806 const char *name,
1807 int fd, int is_connected)
1808 {
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);
1820 }
1821 return s;
1822 }
1823
1824 static NetSocketState *net_socket_fd_init(VLANState *vlan,
1825 const char *model, const char *name,
1826 int fd, int is_connected)
1827 {
1828 int so_type=-1, optlen=sizeof(so_type);
1829
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;
1834 }
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);
1844 }
1845 return NULL;
1846 }
1847
1848 static void net_socket_accept(void *opaque)
1849 {
1850 NetSocketListenState *s = opaque;
1851 NetSocketState *s1;
1852 struct sockaddr_in saddr;
1853 socklen_t len;
1854 int fd;
1855
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;
1863 }
1864 }
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));
1872 }
1873 }
1874
1875 static int net_socket_listen_init(VLANState *vlan,
1876 const char *model,
1877 const char *name,
1878 const char *host_str)
1879 {
1880 NetSocketListenState *s;
1881 int fd, val, ret;
1882 struct sockaddr_in saddr;
1883
1884 if (parse_host_port(&saddr, host_str) < 0)
1885 return -1;
1886
1887 s = qemu_mallocz(sizeof(NetSocketListenState));
1888
1889 fd = socket(PF_INET, SOCK_STREAM, 0);
1890 if (fd < 0) {
1891 perror("socket");
1892 return -1;
1893 }
1894 socket_set_nonblock(fd);
1895
1896 /* allow fast reuse */
1897 val = 1;
1898 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val));
1899
1900 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr));
1901 if (ret < 0) {
1902 perror("bind");
1903 return -1;
1904 }
1905 ret = listen(fd, 0);
1906 if (ret < 0) {
1907 perror("listen");
1908 return -1;
1909 }
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;
1916 }
1917
1918 static int net_socket_connect_init(VLANState *vlan,
1919 const char *model,
1920 const char *name,
1921 const char *host_str)
1922 {
1923 NetSocketState *s;
1924 int fd, connected, ret, err;
1925 struct sockaddr_in saddr;
1926
1927 if (parse_host_port(&saddr, host_str) < 0)
1928 return -1;
1929
1930 fd = socket(PF_INET, SOCK_STREAM, 0);
1931 if (fd < 0) {
1932 perror("socket");
1933 return -1;
1934 }
1935 socket_set_nonblock(fd);
1936
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;
1953 }
1954 } else {
1955 connected = 1;
1956 break;
1957 }
1958 }
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;
1966 }
1967
1968 static int net_socket_mcast_init(VLANState *vlan,
1969 const char *model,
1970 const char *name,
1971 const char *host_str)
1972 {
1973 NetSocketState *s;
1974 int fd;
1975 struct sockaddr_in saddr;
1976
1977 if (parse_host_port(&saddr, host_str) < 0)
1978 return -1;
1979
1980
1981 fd = net_socket_mcast_create(&saddr);
1982 if (fd < 0)
1983 return -1;
1984
1985 s = net_socket_fd_init(vlan, model, name, fd, 0);
1986 if (!s)
1987 return -1;
1988
1989 s->dgram_dst = saddr;
1990
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;
1995
1996 }
1997
1998 typedef struct DumpState {
1999 VLANClientState *pcap_vc;
2000 int fd;
2001 int pcap_caplen;
2002 } DumpState;
2003
2004 #define PCAP_MAGIC 0xa1b2c3d4
2005
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;
2014 };
2015
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;
2023 };
2024
2025 static ssize_t dump_receive(VLANClientState *vc, const uint8_t *buf, size_t size)
2026 {
2027 DumpState *s = vc->opaque;
2028 struct pcap_sf_pkthdr hdr;
2029 int64_t ts;
2030 int caplen;
2031
2032 /* Early return in case of previous error. */
2033 if (s->fd < 0) {
2034 return size;
2035 }
2036
2037 ts = muldiv64(qemu_get_clock(vm_clock), 1000000, ticks_per_sec);
2038 caplen = size > s->pcap_caplen ? s->pcap_caplen : size;
2039
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;
2049 }
2050
2051 return size;
2052 }
2053
2054 static void net_dump_cleanup(VLANClientState *vc)
2055 {
2056 DumpState *s = vc->opaque;
2057
2058 close(s->fd);
2059 qemu_free(s);
2060 }
2061
2062 static int net_dump_init(Monitor *mon, VLANState *vlan, const char *device,
2063 const char *name, const char *filename, int len)
2064 {
2065 struct pcap_file_hdr hdr;
2066 DumpState *s;
2067
2068 s = qemu_malloc(sizeof(DumpState));
2069
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;
2074 }
2075
2076 s->pcap_caplen = len;
2077
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;
2085
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;
2091 }
2092
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;
2098 }
2099
2100 /* find or alloc a new VLAN */
2101 VLANState *qemu_find_vlan(int id)
2102 {
2103 VLANState **pvlan, *vlan;
2104 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2105 if (vlan->id == id)
2106 return vlan;
2107 }
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;
2116 }
2117
2118 static int nic_get_free_idx(void)
2119 {
2120 int index;
2121
2122 for (index = 0; index < MAX_NICS; index++)
2123 if (!nd_table[index].used)
2124 return index;
2125 return -1;
2126 }
2127
2128 void qemu_check_nic_model(NICInfo *nd, const char *model)
2129 {
2130 const char *models[2];
2131
2132 models[0] = model;
2133 models[1] = NULL;
2134
2135 qemu_check_nic_model_list(nd, models, model);
2136 }
2137
2138 void qemu_check_nic_model_list(NICInfo *nd, const char * const *models,
2139 const char *default_model)
2140 {
2141 int i, exit_status = 0;
2142
2143 if (!nd->model)
2144 nd->model = strdup(default_model);
2145
2146 if (strcmp(nd->model, "?") != 0) {
2147 for (i = 0 ; models[i]; i++)
2148 if (strcmp(nd->model, models[i]) == 0)
2149 return;
2150
2151 fprintf(stderr, "qemu: Unsupported NIC model: %s\n", nd->model);
2152 exit_status = 1;
2153 }
2154
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');
2158
2159 exit(exit_status);
2160 }
2161
2162 int net_client_init(Monitor *mon, const char *device, const char *p)
2163 {
2164 char buf[1024];
2165 int vlan_id, ret;
2166 VLANState *vlan;
2167 char *name = NULL;
2168
2169 vlan_id = 0;
2170 if (get_param_value(buf, sizeof(buf), "vlan", p)) {
2171 vlan_id = strtol(buf, NULL, 0);
2172 }
2173 vlan = qemu_find_vlan(vlan_id);
2174
2175 if (get_param_value(buf, sizeof(buf), "name", p)) {
2176 name = qemu_strdup(buf);
2177 }
2178 if (!strcmp(device, "nic")) {
2179 static const char * const nic_params[] = {
2180 "vlan", "name", "macaddr", "model", "addr", "vectors", NULL
2181 };
2182 NICInfo *nd;
2183 uint8_t *macaddr;
2184 int idx = nic_get_free_idx();
2185
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;
2190 }
2191 if (idx == -1 || nb_nics >= MAX_NICS) {
2192 config_error(mon, "Too Many NICs\n");
2193 ret = -1;
2194 goto out;
2195 }
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;
2204
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;
2210 }
2211 }
2212 if (get_param_value(buf, sizeof(buf), "model", p)) {
2213 nd->model = strdup(buf);
2214 }
2215 if (get_param_value(buf, sizeof(buf), "addr", p)) {
2216 nd->devaddr = strdup(buf);
2217 }
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;
2226 }
2227 if (vectors < 0 || vectors > 0x7ffffff) {
2228 config_error(mon, "invalid # of vectors\n");
2229 ret = -1;
2230 goto out;
2231 }
2232 nd->nvectors = vectors;
2233 }
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;
2247 }
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
2257 };
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;
2265
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;
2270 }
2271 if (get_param_value(buf, sizeof(buf), "hostname", p)) {
2272 pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf);
2273 }
2274 if (get_param_value(buf, sizeof(buf), "restrict", p)) {
2275 restricted = (buf[0] == 'y') ? 1 : 0;
2276 }
2277 if (get_param_value(buf, sizeof(buf), "ip", p)) {
2278 ip = qemu_strdup(buf);
2279 }
2280 if (get_param_value(buf, sizeof(buf), "tftp", p)) {
2281 tftp_export = qemu_strdup(buf);
2282 }
2283 if (get_param_value(buf, sizeof(buf), "bootfile", p)) {
2284 bootfile = qemu_strdup(buf);
2285 }
2286 if (get_param_value(buf, sizeof(buf), "smb", p)) {
2287 smb_export = qemu_strdup(buf);
2288 }
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;
2295 }
2296 config->flags = SLIRP_CFG_REDIR;
2297 config->next = slirp_configs;
2298 slirp_configs = config;
2299 config = NULL;
2300 }
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;
2307 }
2308 config->flags = 0;
2309 config->next = slirp_configs;
2310 slirp_configs = config;
2311 config = NULL;
2312 }
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;
2324
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);
2332 }
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
2340 };
2341 char ifname[64];
2342
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;
2347 }
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;
2352 }
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
2367 };
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;
2372 }
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
2379 };
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;
2384 }
2385 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) {
2386 ifname[0] = '\0';
2387 }
2388 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) {
2389 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT);
2390 }
2391 if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) {
2392 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT);
2393 }
2394 s = net_tap_init(vlan, device, name, ifname, setup_script, down_script);
2395 }
2396 if (s != NULL) {
2397 if (get_param_value(buf, sizeof(buf), "sndbuf", p)) {
2398 tap_set_sndbuf(s, atoi(buf), mon);
2399 }
2400 ret = 0;
2401 } else {
2402 ret = -1;
2403 }
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
2411 };
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;
2417 }
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
2425 };
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;
2430 }
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
2435 };
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;
2440 }
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
2445 };
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;
2450 }
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;
2456 }
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
2463 };
2464 char vde_sock[1024], vde_group[512];
2465 int vde_port, vde_mode;
2466
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;
2471 }
2472 vlan->nb_host_devs++;
2473 if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) {
2474 vde_sock[0] = '\0';
2475 }
2476 if (get_param_value(buf, sizeof(buf), "port", p) > 0) {
2477 vde_port = strtol(buf, NULL, 10);
2478 } else {
2479 vde_port = 0;
2480 }
2481 if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) {
2482 vde_group[0] = '\0';
2483 }
2484 if (get_param_value(buf, sizeof(buf), "mode", p) > 0) {
2485 vde_mode = strtol(buf, NULL, 8);
2486 } else {
2487 vde_mode = 0700;
2488 }
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;
2494
2495 if (get_param_value(buf, sizeof(buf), "len", p) > 0) {
2496 len = strtol(buf, NULL, 0);
2497 }
2498 if (!get_param_value(buf, sizeof(buf), "file", p)) {
2499 snprintf(buf, sizeof(buf), "qemu-vlan%d.pcap", vlan_id);
2500 }
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;
2506 }
2507 if (ret < 0) {
2508 config_error(mon, "Could not initialize device '%s'\n", device);
2509 }
2510 out:
2511 qemu_free(name);
2512 return ret;
2513 }
2514
2515 void net_client_uninit(NICInfo *nd)
2516 {
2517 nd->vlan->nb_guest_devs--;
2518 nb_nics--;
2519 nd->used = 0;
2520 free((void *)nd->model);
2521 }
2522
2523 static int net_host_check_device(const char *device)
2524 {
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
2533 };
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;
2538 }
2539
2540 return 0;
2541 }
2542
2543 void net_host_device_add(Monitor *mon, const char *device, const char *opts)
2544 {
2545 if (!net_host_check_device(device)) {
2546 monitor_printf(mon, "invalid host network device %s\n", device);
2547 return;
2548 }
2549 if (net_client_init(mon, device, opts ? opts : "") < 0) {
2550 monitor_printf(mon, "adding host network device %s failed\n", device);
2551 }
2552 }
2553
2554 void net_host_device_remove(Monitor *mon, int vlan_id, const char *device)
2555 {
2556 VLANState *vlan;
2557 VLANClientState *vc;
2558
2559 vlan = qemu_find_vlan(vlan_id);
2560
2561 for (vc = vlan->first_client; vc != NULL; vc = vc->next) {
2562 if (!strcmp(vc->name, device)) {
2563 break;
2564 }
2565 }
2566
2567 if (!vc) {
2568 monitor_printf(mon, "can't find device %s\n", device);
2569 return;
2570 }
2571 if (!net_host_check_device(vc->model)) {
2572 monitor_printf(mon, "invalid host network device %s\n", device);
2573 return;
2574 }
2575 qemu_del_vlan_client(vc);
2576 }
2577
2578 int net_client_parse(const char *str)
2579 {
2580 const char *p;
2581 char *q;
2582 char device[64];
2583
2584 p = str;
2585 q = device;
2586 while (*p != '\0' && *p != ',') {
2587 if ((q - device) < sizeof(device) - 1)
2588 *q++ = *p;
2589 p++;
2590 }
2591 *q = '\0';
2592 if (*p == ',')
2593 p++;
2594
2595 return net_client_init(NULL, device, p);
2596 }
2597
2598 void net_set_boot_mask(int net_boot_mask)
2599 {
2600 int i;
2601
2602 /* Only the first four NICs may be bootable */
2603 net_boot_mask = net_boot_mask & 0xF;
2604
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);
2609 }
2610 }
2611
2612 if (net_boot_mask) {
2613 fprintf(stderr, "Cannot boot from non-existent NIC\n");
2614 exit(1);
2615 }
2616 }
2617
2618 void do_info_network(Monitor *mon)
2619 {
2620 VLANState *vlan;
2621 VLANClientState *vc;
2622
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);
2627 }
2628 }
2629
2630 int do_set_link(Monitor *mon, const char *name, const char *up_or_down)
2631 {
2632 VLANState *vlan;
2633 VLANClientState *vc = NULL;
2634
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:
2640
2641 if (!vc) {
2642 monitor_printf(mon, "could not find network device '%s'", name);
2643 return 0;
2644 }
2645
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);
2653
2654 if (vc->link_status_changed)
2655 vc->link_status_changed(vc);
2656
2657 return 1;
2658 }
2659
2660 void net_cleanup(void)
2661 {
2662 VLANState *vlan;
2663
2664 /* close network clients */
2665 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) {
2666 VLANClientState *vc = vlan->first_client;
2667
2668 while (vc) {
2669 VLANClientState *next = vc->next;
2670
2671 qemu_del_vlan_client(vc);
2672
2673 vc = next;
2674 }
2675 }
2676 }
2677
2678 void net_client_check(void)
2679 {
2680 VLANState *vlan;
2681
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);
2691 }
2692 }