|
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 "qemu-common.h" |
|
25 #include "net.h" |
|
26 #include "console.h" |
|
27 #include "sysemu.h" |
|
28 #include "qemu-timer.h" |
|
29 #include "qemu-char.h" |
|
30 #include "audio/audio.h" |
|
31 |
|
32 #include <unistd.h> |
|
33 #include <fcntl.h> |
|
34 #include <signal.h> |
|
35 #include <time.h> |
|
36 #include <errno.h> |
|
37 #include <sys/time.h> |
|
38 #include <zlib.h> |
|
39 |
|
40 #ifndef _WIN32 |
|
41 #include <sys/times.h> |
|
42 #include <sys/wait.h> |
|
43 #include <termios.h> |
|
44 #include <sys/mman.h> |
|
45 #include <sys/ioctl.h> |
|
46 #include <sys/resource.h> |
|
47 #include <sys/socket.h> |
|
48 #include <netinet/in.h> |
|
49 #include <net/if.h> |
|
50 #ifdef __NetBSD__ |
|
51 #include <net/if_tap.h> |
|
52 #endif |
|
53 #ifdef __linux__ |
|
54 #include <linux/if_tun.h> |
|
55 #endif |
|
56 #include <arpa/inet.h> |
|
57 #include <dirent.h> |
|
58 #include <netdb.h> |
|
59 #include <sys/select.h> |
|
60 #ifdef _BSD |
|
61 #include <sys/stat.h> |
|
62 #ifdef __FreeBSD__ |
|
63 #include <libutil.h> |
|
64 #else |
|
65 #include <util.h> |
|
66 #endif |
|
67 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__) |
|
68 #include <freebsd/stdlib.h> |
|
69 #else |
|
70 #ifdef __linux__ |
|
71 #include <pty.h> |
|
72 #include <malloc.h> |
|
73 #include <linux/rtc.h> |
|
74 |
|
75 /* For the benefit of older linux systems which don't supply it, |
|
76 we use a local copy of hpet.h. */ |
|
77 /* #include <linux/hpet.h> */ |
|
78 #include "hpet.h" |
|
79 |
|
80 #include <linux/ppdev.h> |
|
81 #include <linux/parport.h> |
|
82 #endif |
|
83 #ifdef __sun__ |
|
84 #include <sys/stat.h> |
|
85 #include <sys/ethernet.h> |
|
86 #include <sys/sockio.h> |
|
87 #include <netinet/arp.h> |
|
88 #include <netinet/in.h> |
|
89 #include <netinet/in_systm.h> |
|
90 #include <netinet/ip.h> |
|
91 #include <netinet/ip_icmp.h> // must come after ip.h |
|
92 #include <netinet/udp.h> |
|
93 #include <netinet/tcp.h> |
|
94 #include <net/if.h> |
|
95 #include <syslog.h> |
|
96 #include <stropts.h> |
|
97 #endif |
|
98 #endif |
|
99 #endif |
|
100 |
|
101 #include "qemu_socket.h" |
|
102 |
|
103 #if defined(CONFIG_SLIRP) |
|
104 #include "libslirp.h" |
|
105 #endif |
|
106 |
|
107 #if defined(__OpenBSD__) |
|
108 #include <util.h> |
|
109 #endif |
|
110 |
|
111 #if defined(CONFIG_VDE) |
|
112 #include <libvdeplug.h> |
|
113 #endif |
|
114 |
|
115 #ifdef _WIN32 |
|
116 #include <malloc.h> |
|
117 #include <sys/timeb.h> |
|
118 #include <mmsystem.h> |
|
119 #define getopt_long_only getopt_long |
|
120 #define memalign(align, size) malloc(size) |
|
121 #endif |
|
122 |
|
123 static VLANState *first_vlan; |
|
124 |
|
125 /***********************************************************/ |
|
126 /* network device redirectors */ |
|
127 |
|
128 #if defined(DEBUG_NET) || defined(DEBUG_SLIRP) |
|
129 static void hex_dump(FILE *f, const uint8_t *buf, int size) |
|
130 { |
|
131 int len, i, j, c; |
|
132 |
|
133 for(i=0;i<size;i+=16) { |
|
134 len = size - i; |
|
135 if (len > 16) |
|
136 len = 16; |
|
137 fprintf(f, "%08x ", i); |
|
138 for(j=0;j<16;j++) { |
|
139 if (j < len) |
|
140 fprintf(f, " %02x", buf[i+j]); |
|
141 else |
|
142 fprintf(f, " "); |
|
143 } |
|
144 fprintf(f, " "); |
|
145 for(j=0;j<len;j++) { |
|
146 c = buf[i+j]; |
|
147 if (c < ' ' || c > '~') |
|
148 c = '.'; |
|
149 fprintf(f, "%c", c); |
|
150 } |
|
151 fprintf(f, "\n"); |
|
152 } |
|
153 } |
|
154 #endif |
|
155 |
|
156 static int parse_macaddr(uint8_t *macaddr, const char *p) |
|
157 { |
|
158 int i; |
|
159 char *last_char; |
|
160 long int offset; |
|
161 |
|
162 errno = 0; |
|
163 offset = strtol(p, &last_char, 0); |
|
164 if (0 == errno && '\0' == *last_char && |
|
165 offset >= 0 && offset <= 0xFFFFFF) { |
|
166 macaddr[3] = (offset & 0xFF0000) >> 16; |
|
167 macaddr[4] = (offset & 0xFF00) >> 8; |
|
168 macaddr[5] = offset & 0xFF; |
|
169 return 0; |
|
170 } else { |
|
171 for(i = 0; i < 6; i++) { |
|
172 macaddr[i] = strtol(p, (char **)&p, 16); |
|
173 if (i == 5) { |
|
174 if (*p != '\0') |
|
175 return -1; |
|
176 } else { |
|
177 if (*p != ':' && *p != '-') |
|
178 return -1; |
|
179 p++; |
|
180 } |
|
181 } |
|
182 return 0; |
|
183 } |
|
184 |
|
185 return -1; |
|
186 } |
|
187 |
|
188 static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
|
189 { |
|
190 const char *p, *p1; |
|
191 int len; |
|
192 p = *pp; |
|
193 p1 = strchr(p, sep); |
|
194 if (!p1) |
|
195 return -1; |
|
196 len = p1 - p; |
|
197 p1++; |
|
198 if (buf_size > 0) { |
|
199 if (len > buf_size - 1) |
|
200 len = buf_size - 1; |
|
201 memcpy(buf, p, len); |
|
202 buf[len] = '\0'; |
|
203 } |
|
204 *pp = p1; |
|
205 return 0; |
|
206 } |
|
207 |
|
208 int parse_host_src_port(struct sockaddr_in *haddr, |
|
209 struct sockaddr_in *saddr, |
|
210 const char *input_str) |
|
211 { |
|
212 char *str = strdup(input_str); |
|
213 char *host_str = str; |
|
214 char *src_str; |
|
215 const char *src_str2; |
|
216 char *ptr; |
|
217 |
|
218 /* |
|
219 * Chop off any extra arguments at the end of the string which |
|
220 * would start with a comma, then fill in the src port information |
|
221 * if it was provided else use the "any address" and "any port". |
|
222 */ |
|
223 if ((ptr = strchr(str,','))) |
|
224 *ptr = '\0'; |
|
225 |
|
226 if ((src_str = strchr(input_str,'@'))) { |
|
227 *src_str = '\0'; |
|
228 src_str++; |
|
229 } |
|
230 |
|
231 if (parse_host_port(haddr, host_str) < 0) |
|
232 goto fail; |
|
233 |
|
234 src_str2 = src_str; |
|
235 if (!src_str || *src_str == '\0') |
|
236 src_str2 = ":0"; |
|
237 |
|
238 if (parse_host_port(saddr, src_str2) < 0) |
|
239 goto fail; |
|
240 |
|
241 free(str); |
|
242 return(0); |
|
243 |
|
244 fail: |
|
245 free(str); |
|
246 return -1; |
|
247 } |
|
248 |
|
249 int parse_host_port(struct sockaddr_in *saddr, const char *str) |
|
250 { |
|
251 char buf[512]; |
|
252 struct hostent *he; |
|
253 const char *p, *r; |
|
254 int port; |
|
255 |
|
256 p = str; |
|
257 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
|
258 return -1; |
|
259 saddr->sin_family = AF_INET; |
|
260 if (buf[0] == '\0') { |
|
261 saddr->sin_addr.s_addr = 0; |
|
262 } else { |
|
263 if (qemu_isdigit(buf[0])) { |
|
264 if (!inet_aton(buf, &saddr->sin_addr)) |
|
265 return -1; |
|
266 } else { |
|
267 if ((he = gethostbyname(buf)) == NULL) |
|
268 return - 1; |
|
269 saddr->sin_addr = *(struct in_addr *)he->h_addr; |
|
270 } |
|
271 } |
|
272 port = strtol(p, (char **)&r, 0); |
|
273 if (r == p) |
|
274 return -1; |
|
275 saddr->sin_port = htons(port); |
|
276 return 0; |
|
277 } |
|
278 |
|
279 #if !defined(_WIN32) && 0 |
|
280 static int parse_unix_path(struct sockaddr_un *uaddr, const char *str) |
|
281 { |
|
282 const char *p; |
|
283 int len; |
|
284 |
|
285 len = MIN(108, strlen(str)); |
|
286 p = strchr(str, ','); |
|
287 if (p) |
|
288 len = MIN(len, p - str); |
|
289 |
|
290 memset(uaddr, 0, sizeof(*uaddr)); |
|
291 |
|
292 uaddr->sun_family = AF_UNIX; |
|
293 memcpy(uaddr->sun_path, str, len); |
|
294 |
|
295 return 0; |
|
296 } |
|
297 #endif |
|
298 |
|
299 VLANClientState *qemu_new_vlan_client(VLANState *vlan, |
|
300 IOReadHandler *fd_read, |
|
301 IOCanRWHandler *fd_can_read, |
|
302 void *opaque) |
|
303 { |
|
304 VLANClientState *vc, **pvc; |
|
305 vc = qemu_mallocz(sizeof(VLANClientState)); |
|
306 if (!vc) |
|
307 return NULL; |
|
308 vc->fd_read = fd_read; |
|
309 vc->fd_can_read = fd_can_read; |
|
310 vc->opaque = opaque; |
|
311 vc->vlan = vlan; |
|
312 |
|
313 vc->next = NULL; |
|
314 pvc = &vlan->first_client; |
|
315 while (*pvc != NULL) |
|
316 pvc = &(*pvc)->next; |
|
317 *pvc = vc; |
|
318 return vc; |
|
319 } |
|
320 |
|
321 void qemu_del_vlan_client(VLANClientState *vc) |
|
322 { |
|
323 VLANClientState **pvc = &vc->vlan->first_client; |
|
324 |
|
325 while (*pvc != NULL) |
|
326 if (*pvc == vc) { |
|
327 *pvc = vc->next; |
|
328 free(vc); |
|
329 break; |
|
330 } else |
|
331 pvc = &(*pvc)->next; |
|
332 } |
|
333 |
|
334 int qemu_can_send_packet(VLANClientState *vc1) |
|
335 { |
|
336 VLANState *vlan = vc1->vlan; |
|
337 VLANClientState *vc; |
|
338 |
|
339 for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
|
340 if (vc != vc1) { |
|
341 if (vc->fd_can_read && vc->fd_can_read(vc->opaque)) |
|
342 return 1; |
|
343 } |
|
344 } |
|
345 return 0; |
|
346 } |
|
347 |
|
348 void qemu_send_packet(VLANClientState *vc1, const uint8_t *buf, int size) |
|
349 { |
|
350 VLANState *vlan = vc1->vlan; |
|
351 VLANClientState *vc; |
|
352 |
|
353 #ifdef DEBUG_NET |
|
354 printf("vlan %d send:\n", vlan->id); |
|
355 hex_dump(stdout, buf, size); |
|
356 #endif |
|
357 for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
|
358 if (vc != vc1) { |
|
359 vc->fd_read(vc->opaque, buf, size); |
|
360 } |
|
361 } |
|
362 } |
|
363 |
|
364 static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov, |
|
365 int iovcnt) |
|
366 { |
|
367 uint8_t buffer[4096]; |
|
368 size_t offset = 0; |
|
369 int i; |
|
370 |
|
371 for (i = 0; i < iovcnt; i++) { |
|
372 size_t len; |
|
373 |
|
374 len = MIN(sizeof(buffer) - offset, iov[i].iov_len); |
|
375 memcpy(buffer + offset, iov[i].iov_base, len); |
|
376 offset += len; |
|
377 } |
|
378 |
|
379 vc->fd_read(vc->opaque, buffer, offset); |
|
380 |
|
381 return offset; |
|
382 } |
|
383 |
|
384 ssize_t qemu_sendv_packet(VLANClientState *vc1, const struct iovec *iov, |
|
385 int iovcnt) |
|
386 { |
|
387 VLANState *vlan = vc1->vlan; |
|
388 VLANClientState *vc; |
|
389 ssize_t max_len = 0; |
|
390 |
|
391 for (vc = vlan->first_client; vc != NULL; vc = vc->next) { |
|
392 ssize_t len = 0; |
|
393 |
|
394 if (vc == vc1) |
|
395 continue; |
|
396 |
|
397 if (vc->fd_readv) |
|
398 len = vc->fd_readv(vc->opaque, iov, iovcnt); |
|
399 else if (vc->fd_read) |
|
400 len = vc_sendv_compat(vc, iov, iovcnt); |
|
401 |
|
402 max_len = MAX(max_len, len); |
|
403 } |
|
404 |
|
405 return max_len; |
|
406 } |
|
407 |
|
408 #if defined(CONFIG_SLIRP) |
|
409 |
|
410 /* slirp network adapter */ |
|
411 |
|
412 static int slirp_inited; |
|
413 static VLANClientState *slirp_vc; |
|
414 |
|
415 int slirp_can_output(void) |
|
416 { |
|
417 return !slirp_vc || qemu_can_send_packet(slirp_vc); |
|
418 } |
|
419 |
|
420 void slirp_output(const uint8_t *pkt, int pkt_len) |
|
421 { |
|
422 #ifdef DEBUG_SLIRP |
|
423 printf("slirp output:\n"); |
|
424 hex_dump(stdout, pkt, pkt_len); |
|
425 #endif |
|
426 if (!slirp_vc) |
|
427 return; |
|
428 qemu_send_packet(slirp_vc, pkt, pkt_len); |
|
429 } |
|
430 |
|
431 int slirp_is_inited(void) |
|
432 { |
|
433 return slirp_inited; |
|
434 } |
|
435 |
|
436 static void slirp_receive(void *opaque, const uint8_t *buf, int size) |
|
437 { |
|
438 #ifdef DEBUG_SLIRP |
|
439 printf("slirp input:\n"); |
|
440 hex_dump(stdout, buf, size); |
|
441 #endif |
|
442 slirp_input(buf, size); |
|
443 } |
|
444 |
|
445 static int net_slirp_init(VLANState *vlan) |
|
446 { |
|
447 if (!slirp_inited) { |
|
448 slirp_inited = 1; |
|
449 slirp_init(); |
|
450 } |
|
451 slirp_vc = qemu_new_vlan_client(vlan, |
|
452 slirp_receive, NULL, NULL); |
|
453 snprintf(slirp_vc->info_str, sizeof(slirp_vc->info_str), "user redirector"); |
|
454 return 0; |
|
455 } |
|
456 |
|
457 void net_slirp_redir(const char *redir_str) |
|
458 { |
|
459 int is_udp; |
|
460 char buf[256], *r; |
|
461 const char *p; |
|
462 struct in_addr guest_addr; |
|
463 int host_port, guest_port; |
|
464 |
|
465 if (!slirp_inited) { |
|
466 slirp_inited = 1; |
|
467 slirp_init(); |
|
468 } |
|
469 |
|
470 p = redir_str; |
|
471 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
|
472 goto fail; |
|
473 if (!strcmp(buf, "tcp")) { |
|
474 is_udp = 0; |
|
475 } else if (!strcmp(buf, "udp")) { |
|
476 is_udp = 1; |
|
477 } else { |
|
478 goto fail; |
|
479 } |
|
480 |
|
481 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
|
482 goto fail; |
|
483 host_port = strtol(buf, &r, 0); |
|
484 if (r == buf) |
|
485 goto fail; |
|
486 |
|
487 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
|
488 goto fail; |
|
489 if (buf[0] == '\0') { |
|
490 pstrcpy(buf, sizeof(buf), "10.0.2.15"); |
|
491 } |
|
492 if (!inet_aton(buf, &guest_addr)) |
|
493 goto fail; |
|
494 |
|
495 guest_port = strtol(p, &r, 0); |
|
496 if (r == p) |
|
497 goto fail; |
|
498 |
|
499 if (slirp_redir(is_udp, host_port, guest_addr, guest_port) < 0) { |
|
500 fprintf(stderr, "qemu: could not set up redirection\n"); |
|
501 exit(1); |
|
502 } |
|
503 return; |
|
504 fail: |
|
505 fprintf(stderr, "qemu: syntax: -redir [tcp|udp]:host-port:[guest-host]:guest-port\n"); |
|
506 exit(1); |
|
507 } |
|
508 |
|
509 #ifndef _WIN32 |
|
510 |
|
511 static char smb_dir[1024]; |
|
512 |
|
513 static void erase_dir(char *dir_name) |
|
514 { |
|
515 DIR *d; |
|
516 struct dirent *de; |
|
517 char filename[1024]; |
|
518 |
|
519 /* erase all the files in the directory */ |
|
520 if ((d = opendir(dir_name)) != 0) { |
|
521 for(;;) { |
|
522 de = readdir(d); |
|
523 if (!de) |
|
524 break; |
|
525 if (strcmp(de->d_name, ".") != 0 && |
|
526 strcmp(de->d_name, "..") != 0) { |
|
527 snprintf(filename, sizeof(filename), "%s/%s", |
|
528 smb_dir, de->d_name); |
|
529 if (unlink(filename) != 0) /* is it a directory? */ |
|
530 erase_dir(filename); |
|
531 } |
|
532 } |
|
533 closedir(d); |
|
534 rmdir(dir_name); |
|
535 } |
|
536 } |
|
537 |
|
538 /* automatic user mode samba server configuration */ |
|
539 static void smb_exit(void) |
|
540 { |
|
541 erase_dir(smb_dir); |
|
542 } |
|
543 |
|
544 /* automatic user mode samba server configuration */ |
|
545 void net_slirp_smb(const char *exported_dir) |
|
546 { |
|
547 char smb_conf[1024]; |
|
548 char smb_cmdline[1024]; |
|
549 FILE *f; |
|
550 |
|
551 if (!slirp_inited) { |
|
552 slirp_inited = 1; |
|
553 slirp_init(); |
|
554 } |
|
555 |
|
556 /* XXX: better tmp dir construction */ |
|
557 snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%d", getpid()); |
|
558 if (mkdir(smb_dir, 0700) < 0) { |
|
559 fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir); |
|
560 exit(1); |
|
561 } |
|
562 snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf"); |
|
563 |
|
564 f = fopen(smb_conf, "w"); |
|
565 if (!f) { |
|
566 fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf); |
|
567 exit(1); |
|
568 } |
|
569 fprintf(f, |
|
570 "[global]\n" |
|
571 "private dir=%s\n" |
|
572 "smb ports=0\n" |
|
573 "socket address=127.0.0.1\n" |
|
574 "pid directory=%s\n" |
|
575 "lock directory=%s\n" |
|
576 "log file=%s/log.smbd\n" |
|
577 "smb passwd file=%s/smbpasswd\n" |
|
578 "security = share\n" |
|
579 "[qemu]\n" |
|
580 "path=%s\n" |
|
581 "read only=no\n" |
|
582 "guest ok=yes\n", |
|
583 smb_dir, |
|
584 smb_dir, |
|
585 smb_dir, |
|
586 smb_dir, |
|
587 smb_dir, |
|
588 exported_dir |
|
589 ); |
|
590 fclose(f); |
|
591 atexit(smb_exit); |
|
592 |
|
593 snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s", |
|
594 SMBD_COMMAND, smb_conf); |
|
595 |
|
596 slirp_add_exec(0, smb_cmdline, 4, 139); |
|
597 } |
|
598 |
|
599 #endif /* !defined(_WIN32) */ |
|
600 void do_info_slirp(void) |
|
601 { |
|
602 slirp_stats(); |
|
603 } |
|
604 |
|
605 #endif /* CONFIG_SLIRP */ |
|
606 |
|
607 #if !defined(_WIN32) |
|
608 |
|
609 typedef struct TAPState { |
|
610 VLANClientState *vc; |
|
611 int fd; |
|
612 char down_script[1024]; |
|
613 } TAPState; |
|
614 |
|
615 #ifdef HAVE_IOVEC |
|
616 static ssize_t tap_receive_iov(void *opaque, const struct iovec *iov, |
|
617 int iovcnt) |
|
618 { |
|
619 TAPState *s = opaque; |
|
620 ssize_t len; |
|
621 |
|
622 do { |
|
623 len = writev(s->fd, iov, iovcnt); |
|
624 } while (len == -1 && (errno == EINTR || errno == EAGAIN)); |
|
625 |
|
626 return len; |
|
627 } |
|
628 #endif |
|
629 |
|
630 static void tap_receive(void *opaque, const uint8_t *buf, int size) |
|
631 { |
|
632 TAPState *s = opaque; |
|
633 int ret; |
|
634 for(;;) { |
|
635 ret = write(s->fd, buf, size); |
|
636 if (ret < 0 && (errno == EINTR || errno == EAGAIN)) { |
|
637 } else { |
|
638 break; |
|
639 } |
|
640 } |
|
641 } |
|
642 |
|
643 static void tap_send(void *opaque) |
|
644 { |
|
645 TAPState *s = opaque; |
|
646 uint8_t buf[4096]; |
|
647 int size; |
|
648 |
|
649 #ifdef __sun__ |
|
650 struct strbuf sbuf; |
|
651 int f = 0; |
|
652 sbuf.maxlen = sizeof(buf); |
|
653 sbuf.buf = buf; |
|
654 size = getmsg(s->fd, NULL, &sbuf, &f) >=0 ? sbuf.len : -1; |
|
655 #else |
|
656 size = read(s->fd, buf, sizeof(buf)); |
|
657 #endif |
|
658 if (size > 0) { |
|
659 qemu_send_packet(s->vc, buf, size); |
|
660 } |
|
661 } |
|
662 |
|
663 /* fd support */ |
|
664 |
|
665 static TAPState *net_tap_fd_init(VLANState *vlan, int fd) |
|
666 { |
|
667 TAPState *s; |
|
668 |
|
669 s = qemu_mallocz(sizeof(TAPState)); |
|
670 if (!s) |
|
671 return NULL; |
|
672 s->fd = fd; |
|
673 s->vc = qemu_new_vlan_client(vlan, tap_receive, NULL, s); |
|
674 #ifdef HAVE_IOVEC |
|
675 s->vc->fd_readv = tap_receive_iov; |
|
676 #endif |
|
677 qemu_set_fd_handler(s->fd, tap_send, NULL, s); |
|
678 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "tap: fd=%d", fd); |
|
679 return s; |
|
680 } |
|
681 |
|
682 #if defined (_BSD) || defined (__FreeBSD_kernel__) |
|
683 static int tap_open(char *ifname, int ifname_size) |
|
684 { |
|
685 int fd; |
|
686 char *dev; |
|
687 struct stat s; |
|
688 |
|
689 TFR(fd = open("/dev/tap", O_RDWR)); |
|
690 if (fd < 0) { |
|
691 fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n"); |
|
692 return -1; |
|
693 } |
|
694 |
|
695 fstat(fd, &s); |
|
696 dev = devname(s.st_rdev, S_IFCHR); |
|
697 pstrcpy(ifname, ifname_size, dev); |
|
698 |
|
699 fcntl(fd, F_SETFL, O_NONBLOCK); |
|
700 return fd; |
|
701 } |
|
702 #elif defined(__sun__) |
|
703 #define TUNNEWPPA (('T'<<16) | 0x0001) |
|
704 /* |
|
705 * Allocate TAP device, returns opened fd. |
|
706 * Stores dev name in the first arg(must be large enough). |
|
707 */ |
|
708 int tap_alloc(char *dev, size_t dev_size) |
|
709 { |
|
710 int tap_fd, if_fd, ppa = -1; |
|
711 static int ip_fd = 0; |
|
712 char *ptr; |
|
713 |
|
714 static int arp_fd = 0; |
|
715 int ip_muxid, arp_muxid; |
|
716 struct strioctl strioc_if, strioc_ppa; |
|
717 int link_type = I_PLINK;; |
|
718 struct lifreq ifr; |
|
719 char actual_name[32] = ""; |
|
720 |
|
721 memset(&ifr, 0x0, sizeof(ifr)); |
|
722 |
|
723 if( *dev ){ |
|
724 ptr = dev; |
|
725 while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++; |
|
726 ppa = atoi(ptr); |
|
727 } |
|
728 |
|
729 /* Check if IP device was opened */ |
|
730 if( ip_fd ) |
|
731 close(ip_fd); |
|
732 |
|
733 TFR(ip_fd = open("/dev/udp", O_RDWR, 0)); |
|
734 if (ip_fd < 0) { |
|
735 syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)"); |
|
736 return -1; |
|
737 } |
|
738 |
|
739 TFR(tap_fd = open("/dev/tap", O_RDWR, 0)); |
|
740 if (tap_fd < 0) { |
|
741 syslog(LOG_ERR, "Can't open /dev/tap"); |
|
742 return -1; |
|
743 } |
|
744 |
|
745 /* Assign a new PPA and get its unit number. */ |
|
746 strioc_ppa.ic_cmd = TUNNEWPPA; |
|
747 strioc_ppa.ic_timout = 0; |
|
748 strioc_ppa.ic_len = sizeof(ppa); |
|
749 strioc_ppa.ic_dp = (char *)&ppa; |
|
750 if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0) |
|
751 syslog (LOG_ERR, "Can't assign new interface"); |
|
752 |
|
753 TFR(if_fd = open("/dev/tap", O_RDWR, 0)); |
|
754 if (if_fd < 0) { |
|
755 syslog(LOG_ERR, "Can't open /dev/tap (2)"); |
|
756 return -1; |
|
757 } |
|
758 if(ioctl(if_fd, I_PUSH, "ip") < 0){ |
|
759 syslog(LOG_ERR, "Can't push IP module"); |
|
760 return -1; |
|
761 } |
|
762 |
|
763 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) |
|
764 syslog(LOG_ERR, "Can't get flags\n"); |
|
765 |
|
766 snprintf (actual_name, 32, "tap%d", ppa); |
|
767 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name); |
|
768 |
|
769 ifr.lifr_ppa = ppa; |
|
770 /* Assign ppa according to the unit number returned by tun device */ |
|
771 |
|
772 if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0) |
|
773 syslog (LOG_ERR, "Can't set PPA %d", ppa); |
|
774 if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0) |
|
775 syslog (LOG_ERR, "Can't get flags\n"); |
|
776 /* Push arp module to if_fd */ |
|
777 if (ioctl (if_fd, I_PUSH, "arp") < 0) |
|
778 syslog (LOG_ERR, "Can't push ARP module (2)"); |
|
779 |
|
780 /* Push arp module to ip_fd */ |
|
781 if (ioctl (ip_fd, I_POP, NULL) < 0) |
|
782 syslog (LOG_ERR, "I_POP failed\n"); |
|
783 if (ioctl (ip_fd, I_PUSH, "arp") < 0) |
|
784 syslog (LOG_ERR, "Can't push ARP module (3)\n"); |
|
785 /* Open arp_fd */ |
|
786 TFR(arp_fd = open ("/dev/tap", O_RDWR, 0)); |
|
787 if (arp_fd < 0) |
|
788 syslog (LOG_ERR, "Can't open %s\n", "/dev/tap"); |
|
789 |
|
790 /* Set ifname to arp */ |
|
791 strioc_if.ic_cmd = SIOCSLIFNAME; |
|
792 strioc_if.ic_timout = 0; |
|
793 strioc_if.ic_len = sizeof(ifr); |
|
794 strioc_if.ic_dp = (char *)𝔦 |
|
795 if (ioctl(arp_fd, I_STR, &strioc_if) < 0){ |
|
796 syslog (LOG_ERR, "Can't set ifname to arp\n"); |
|
797 } |
|
798 |
|
799 if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){ |
|
800 syslog(LOG_ERR, "Can't link TAP device to IP"); |
|
801 return -1; |
|
802 } |
|
803 |
|
804 if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0) |
|
805 syslog (LOG_ERR, "Can't link TAP device to ARP"); |
|
806 |
|
807 close (if_fd); |
|
808 |
|
809 memset(&ifr, 0x0, sizeof(ifr)); |
|
810 pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name); |
|
811 ifr.lifr_ip_muxid = ip_muxid; |
|
812 ifr.lifr_arp_muxid = arp_muxid; |
|
813 |
|
814 if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0) |
|
815 { |
|
816 ioctl (ip_fd, I_PUNLINK , arp_muxid); |
|
817 ioctl (ip_fd, I_PUNLINK, ip_muxid); |
|
818 syslog (LOG_ERR, "Can't set multiplexor id"); |
|
819 } |
|
820 |
|
821 snprintf(dev, dev_size, "tap%d", ppa); |
|
822 return tap_fd; |
|
823 } |
|
824 |
|
825 static int tap_open(char *ifname, int ifname_size) |
|
826 { |
|
827 char dev[10]=""; |
|
828 int fd; |
|
829 if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){ |
|
830 fprintf(stderr, "Cannot allocate TAP device\n"); |
|
831 return -1; |
|
832 } |
|
833 pstrcpy(ifname, ifname_size, dev); |
|
834 fcntl(fd, F_SETFL, O_NONBLOCK); |
|
835 return fd; |
|
836 } |
|
837 #elif defined (_AIX) |
|
838 static int tap_open(char *ifname, int ifname_size) |
|
839 { |
|
840 fprintf (stderr, "no tap on AIX\n"); |
|
841 return -1; |
|
842 } |
|
843 #else |
|
844 static int tap_open(char *ifname, int ifname_size) |
|
845 { |
|
846 struct ifreq ifr; |
|
847 int fd, ret; |
|
848 |
|
849 TFR(fd = open("/dev/net/tun", O_RDWR)); |
|
850 if (fd < 0) { |
|
851 fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n"); |
|
852 return -1; |
|
853 } |
|
854 memset(&ifr, 0, sizeof(ifr)); |
|
855 ifr.ifr_flags = IFF_TAP | IFF_NO_PI; |
|
856 if (ifname[0] != '\0') |
|
857 pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname); |
|
858 else |
|
859 pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d"); |
|
860 ret = ioctl(fd, TUNSETIFF, (void *) &ifr); |
|
861 if (ret != 0) { |
|
862 fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n"); |
|
863 close(fd); |
|
864 return -1; |
|
865 } |
|
866 pstrcpy(ifname, ifname_size, ifr.ifr_name); |
|
867 fcntl(fd, F_SETFL, O_NONBLOCK); |
|
868 return fd; |
|
869 } |
|
870 #endif |
|
871 |
|
872 static int launch_script(const char *setup_script, const char *ifname, int fd) |
|
873 { |
|
874 int pid, status; |
|
875 char *args[3]; |
|
876 char **parg; |
|
877 |
|
878 /* try to launch network script */ |
|
879 pid = fork(); |
|
880 if (pid >= 0) { |
|
881 if (pid == 0) { |
|
882 int open_max = sysconf (_SC_OPEN_MAX), i; |
|
883 for (i = 0; i < open_max; i++) |
|
884 if (i != STDIN_FILENO && |
|
885 i != STDOUT_FILENO && |
|
886 i != STDERR_FILENO && |
|
887 i != fd) |
|
888 close(i); |
|
889 |
|
890 parg = args; |
|
891 *parg++ = (char *)setup_script; |
|
892 *parg++ = (char *)ifname; |
|
893 *parg++ = NULL; |
|
894 execv(setup_script, args); |
|
895 _exit(1); |
|
896 } |
|
897 while (waitpid(pid, &status, 0) != pid); |
|
898 if (!WIFEXITED(status) || |
|
899 WEXITSTATUS(status) != 0) { |
|
900 fprintf(stderr, "%s: could not launch network script\n", |
|
901 setup_script); |
|
902 return -1; |
|
903 } |
|
904 } |
|
905 return 0; |
|
906 } |
|
907 |
|
908 static int net_tap_init(VLANState *vlan, const char *ifname1, |
|
909 const char *setup_script, const char *down_script) |
|
910 { |
|
911 TAPState *s; |
|
912 int fd; |
|
913 char ifname[128]; |
|
914 |
|
915 if (ifname1 != NULL) |
|
916 pstrcpy(ifname, sizeof(ifname), ifname1); |
|
917 else |
|
918 ifname[0] = '\0'; |
|
919 TFR(fd = tap_open(ifname, sizeof(ifname))); |
|
920 if (fd < 0) |
|
921 return -1; |
|
922 |
|
923 if (!setup_script || !strcmp(setup_script, "no")) |
|
924 setup_script = ""; |
|
925 if (setup_script[0] != '\0') { |
|
926 if (launch_script(setup_script, ifname, fd)) |
|
927 return -1; |
|
928 } |
|
929 s = net_tap_fd_init(vlan, fd); |
|
930 if (!s) |
|
931 return -1; |
|
932 snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
|
933 "tap: ifname=%s setup_script=%s", ifname, setup_script); |
|
934 if (down_script && strcmp(down_script, "no")) |
|
935 snprintf(s->down_script, sizeof(s->down_script), "%s", down_script); |
|
936 return 0; |
|
937 } |
|
938 |
|
939 #endif /* !_WIN32 */ |
|
940 |
|
941 #if defined(CONFIG_VDE) |
|
942 typedef struct VDEState { |
|
943 VLANClientState *vc; |
|
944 VDECONN *vde; |
|
945 } VDEState; |
|
946 |
|
947 static void vde_to_qemu(void *opaque) |
|
948 { |
|
949 VDEState *s = opaque; |
|
950 uint8_t buf[4096]; |
|
951 int size; |
|
952 |
|
953 size = vde_recv(s->vde, buf, sizeof(buf), 0); |
|
954 if (size > 0) { |
|
955 qemu_send_packet(s->vc, buf, size); |
|
956 } |
|
957 } |
|
958 |
|
959 static void vde_from_qemu(void *opaque, const uint8_t *buf, int size) |
|
960 { |
|
961 VDEState *s = opaque; |
|
962 int ret; |
|
963 for(;;) { |
|
964 ret = vde_send(s->vde, buf, size, 0); |
|
965 if (ret < 0 && errno == EINTR) { |
|
966 } else { |
|
967 break; |
|
968 } |
|
969 } |
|
970 } |
|
971 |
|
972 static int net_vde_init(VLANState *vlan, const char *sock, int port, |
|
973 const char *group, int mode) |
|
974 { |
|
975 VDEState *s; |
|
976 char *init_group = strlen(group) ? (char *)group : NULL; |
|
977 char *init_sock = strlen(sock) ? (char *)sock : NULL; |
|
978 |
|
979 struct vde_open_args args = { |
|
980 .port = port, |
|
981 .group = init_group, |
|
982 .mode = mode, |
|
983 }; |
|
984 |
|
985 s = qemu_mallocz(sizeof(VDEState)); |
|
986 if (!s) |
|
987 return -1; |
|
988 s->vde = vde_open(init_sock, "QEMU", &args); |
|
989 if (!s->vde){ |
|
990 free(s); |
|
991 return -1; |
|
992 } |
|
993 s->vc = qemu_new_vlan_client(vlan, vde_from_qemu, NULL, s); |
|
994 qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s); |
|
995 snprintf(s->vc->info_str, sizeof(s->vc->info_str), "vde: sock=%s fd=%d", |
|
996 sock, vde_datafd(s->vde)); |
|
997 return 0; |
|
998 } |
|
999 #endif |
|
1000 |
|
1001 /* network connection */ |
|
1002 typedef struct NetSocketState { |
|
1003 VLANClientState *vc; |
|
1004 int fd; |
|
1005 int state; /* 0 = getting length, 1 = getting data */ |
|
1006 int index; |
|
1007 int packet_len; |
|
1008 uint8_t buf[4096]; |
|
1009 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ |
|
1010 } NetSocketState; |
|
1011 |
|
1012 typedef struct NetSocketListenState { |
|
1013 VLANState *vlan; |
|
1014 int fd; |
|
1015 } NetSocketListenState; |
|
1016 |
|
1017 /* XXX: we consider we can send the whole packet without blocking */ |
|
1018 static void net_socket_receive(void *opaque, const uint8_t *buf, int size) |
|
1019 { |
|
1020 NetSocketState *s = opaque; |
|
1021 uint32_t len; |
|
1022 len = htonl(size); |
|
1023 |
|
1024 send_all(s->fd, (const uint8_t *)&len, sizeof(len)); |
|
1025 send_all(s->fd, buf, size); |
|
1026 } |
|
1027 |
|
1028 static void net_socket_receive_dgram(void *opaque, const uint8_t *buf, int size) |
|
1029 { |
|
1030 NetSocketState *s = opaque; |
|
1031 sendto(s->fd, buf, size, 0, |
|
1032 (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst)); |
|
1033 } |
|
1034 |
|
1035 static void net_socket_send(void *opaque) |
|
1036 { |
|
1037 NetSocketState *s = opaque; |
|
1038 int l, size, err; |
|
1039 uint8_t buf1[4096]; |
|
1040 const uint8_t *buf; |
|
1041 |
|
1042 size = recv(s->fd, buf1, sizeof(buf1), 0); |
|
1043 if (size < 0) { |
|
1044 err = socket_error(); |
|
1045 if (err != EWOULDBLOCK) |
|
1046 goto eoc; |
|
1047 } else if (size == 0) { |
|
1048 /* end of connection */ |
|
1049 eoc: |
|
1050 qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
|
1051 closesocket(s->fd); |
|
1052 return; |
|
1053 } |
|
1054 buf = buf1; |
|
1055 while (size > 0) { |
|
1056 /* reassemble a packet from the network */ |
|
1057 switch(s->state) { |
|
1058 case 0: |
|
1059 l = 4 - s->index; |
|
1060 if (l > size) |
|
1061 l = size; |
|
1062 memcpy(s->buf + s->index, buf, l); |
|
1063 buf += l; |
|
1064 size -= l; |
|
1065 s->index += l; |
|
1066 if (s->index == 4) { |
|
1067 /* got length */ |
|
1068 s->packet_len = ntohl(*(uint32_t *)s->buf); |
|
1069 s->index = 0; |
|
1070 s->state = 1; |
|
1071 } |
|
1072 break; |
|
1073 case 1: |
|
1074 l = s->packet_len - s->index; |
|
1075 if (l > size) |
|
1076 l = size; |
|
1077 memcpy(s->buf + s->index, buf, l); |
|
1078 s->index += l; |
|
1079 buf += l; |
|
1080 size -= l; |
|
1081 if (s->index >= s->packet_len) { |
|
1082 qemu_send_packet(s->vc, s->buf, s->packet_len); |
|
1083 s->index = 0; |
|
1084 s->state = 0; |
|
1085 } |
|
1086 break; |
|
1087 } |
|
1088 } |
|
1089 } |
|
1090 |
|
1091 static void net_socket_send_dgram(void *opaque) |
|
1092 { |
|
1093 NetSocketState *s = opaque; |
|
1094 int size; |
|
1095 |
|
1096 size = recv(s->fd, s->buf, sizeof(s->buf), 0); |
|
1097 if (size < 0) |
|
1098 return; |
|
1099 if (size == 0) { |
|
1100 /* end of connection */ |
|
1101 qemu_set_fd_handler(s->fd, NULL, NULL, NULL); |
|
1102 return; |
|
1103 } |
|
1104 qemu_send_packet(s->vc, s->buf, size); |
|
1105 } |
|
1106 |
|
1107 static int net_socket_mcast_create(struct sockaddr_in *mcastaddr) |
|
1108 { |
|
1109 struct ip_mreq imr; |
|
1110 int fd; |
|
1111 int val, ret; |
|
1112 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) { |
|
1113 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n", |
|
1114 inet_ntoa(mcastaddr->sin_addr), |
|
1115 (int)ntohl(mcastaddr->sin_addr.s_addr)); |
|
1116 return -1; |
|
1117 |
|
1118 } |
|
1119 fd = socket(PF_INET, SOCK_DGRAM, 0); |
|
1120 if (fd < 0) { |
|
1121 perror("socket(PF_INET, SOCK_DGRAM)"); |
|
1122 return -1; |
|
1123 } |
|
1124 |
|
1125 val = 1; |
|
1126 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, |
|
1127 (const char *)&val, sizeof(val)); |
|
1128 if (ret < 0) { |
|
1129 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); |
|
1130 goto fail; |
|
1131 } |
|
1132 |
|
1133 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); |
|
1134 if (ret < 0) { |
|
1135 perror("bind"); |
|
1136 goto fail; |
|
1137 } |
|
1138 |
|
1139 /* Add host to multicast group */ |
|
1140 imr.imr_multiaddr = mcastaddr->sin_addr; |
|
1141 imr.imr_interface.s_addr = htonl(INADDR_ANY); |
|
1142 |
|
1143 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, |
|
1144 (const char *)&imr, sizeof(struct ip_mreq)); |
|
1145 if (ret < 0) { |
|
1146 perror("setsockopt(IP_ADD_MEMBERSHIP)"); |
|
1147 goto fail; |
|
1148 } |
|
1149 |
|
1150 /* Force mcast msgs to loopback (eg. several QEMUs in same host */ |
|
1151 val = 1; |
|
1152 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, |
|
1153 (const char *)&val, sizeof(val)); |
|
1154 if (ret < 0) { |
|
1155 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)"); |
|
1156 goto fail; |
|
1157 } |
|
1158 |
|
1159 socket_set_nonblock(fd); |
|
1160 return fd; |
|
1161 fail: |
|
1162 if (fd >= 0) |
|
1163 closesocket(fd); |
|
1164 return -1; |
|
1165 } |
|
1166 |
|
1167 static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, int fd, |
|
1168 int is_connected) |
|
1169 { |
|
1170 struct sockaddr_in saddr; |
|
1171 int newfd; |
|
1172 socklen_t saddr_len; |
|
1173 NetSocketState *s; |
|
1174 |
|
1175 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it |
|
1176 * Because this may be "shared" socket from a "master" process, datagrams would be recv() |
|
1177 * by ONLY ONE process: we must "clone" this dgram socket --jjo |
|
1178 */ |
|
1179 |
|
1180 if (is_connected) { |
|
1181 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { |
|
1182 /* must be bound */ |
|
1183 if (saddr.sin_addr.s_addr==0) { |
|
1184 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n", |
|
1185 fd); |
|
1186 return NULL; |
|
1187 } |
|
1188 /* clone dgram socket */ |
|
1189 newfd = net_socket_mcast_create(&saddr); |
|
1190 if (newfd < 0) { |
|
1191 /* error already reported by net_socket_mcast_create() */ |
|
1192 close(fd); |
|
1193 return NULL; |
|
1194 } |
|
1195 /* clone newfd to fd, close newfd */ |
|
1196 dup2(newfd, fd); |
|
1197 close(newfd); |
|
1198 |
|
1199 } else { |
|
1200 fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n", |
|
1201 fd, strerror(errno)); |
|
1202 return NULL; |
|
1203 } |
|
1204 } |
|
1205 |
|
1206 s = qemu_mallocz(sizeof(NetSocketState)); |
|
1207 if (!s) |
|
1208 return NULL; |
|
1209 s->fd = fd; |
|
1210 |
|
1211 s->vc = qemu_new_vlan_client(vlan, net_socket_receive_dgram, NULL, s); |
|
1212 qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s); |
|
1213 |
|
1214 /* mcast: save bound address as dst */ |
|
1215 if (is_connected) s->dgram_dst=saddr; |
|
1216 |
|
1217 snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
|
1218 "socket: fd=%d (%s mcast=%s:%d)", |
|
1219 fd, is_connected? "cloned" : "", |
|
1220 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
|
1221 return s; |
|
1222 } |
|
1223 |
|
1224 static void net_socket_connect(void *opaque) |
|
1225 { |
|
1226 NetSocketState *s = opaque; |
|
1227 qemu_set_fd_handler(s->fd, net_socket_send, NULL, s); |
|
1228 } |
|
1229 |
|
1230 static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, int fd, |
|
1231 int is_connected) |
|
1232 { |
|
1233 NetSocketState *s; |
|
1234 s = qemu_mallocz(sizeof(NetSocketState)); |
|
1235 if (!s) |
|
1236 return NULL; |
|
1237 s->fd = fd; |
|
1238 s->vc = qemu_new_vlan_client(vlan, |
|
1239 net_socket_receive, NULL, s); |
|
1240 snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
|
1241 "socket: fd=%d", fd); |
|
1242 if (is_connected) { |
|
1243 net_socket_connect(s); |
|
1244 } else { |
|
1245 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s); |
|
1246 } |
|
1247 return s; |
|
1248 } |
|
1249 |
|
1250 static NetSocketState *net_socket_fd_init(VLANState *vlan, int fd, |
|
1251 int is_connected) |
|
1252 { |
|
1253 int so_type=-1, optlen=sizeof(so_type); |
|
1254 |
|
1255 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, |
|
1256 (socklen_t *)&optlen)< 0) { |
|
1257 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd); |
|
1258 return NULL; |
|
1259 } |
|
1260 switch(so_type) { |
|
1261 case SOCK_DGRAM: |
|
1262 return net_socket_fd_init_dgram(vlan, fd, is_connected); |
|
1263 case SOCK_STREAM: |
|
1264 return net_socket_fd_init_stream(vlan, fd, is_connected); |
|
1265 default: |
|
1266 /* who knows ... this could be a eg. a pty, do warn and continue as stream */ |
|
1267 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd); |
|
1268 return net_socket_fd_init_stream(vlan, fd, is_connected); |
|
1269 } |
|
1270 return NULL; |
|
1271 } |
|
1272 |
|
1273 static void net_socket_accept(void *opaque) |
|
1274 { |
|
1275 NetSocketListenState *s = opaque; |
|
1276 NetSocketState *s1; |
|
1277 struct sockaddr_in saddr; |
|
1278 socklen_t len; |
|
1279 int fd; |
|
1280 |
|
1281 for(;;) { |
|
1282 len = sizeof(saddr); |
|
1283 fd = accept(s->fd, (struct sockaddr *)&saddr, &len); |
|
1284 if (fd < 0 && errno != EINTR) { |
|
1285 return; |
|
1286 } else if (fd >= 0) { |
|
1287 break; |
|
1288 } |
|
1289 } |
|
1290 s1 = net_socket_fd_init(s->vlan, fd, 1); |
|
1291 if (!s1) { |
|
1292 closesocket(fd); |
|
1293 } else { |
|
1294 snprintf(s1->vc->info_str, sizeof(s1->vc->info_str), |
|
1295 "socket: connection from %s:%d", |
|
1296 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
|
1297 } |
|
1298 } |
|
1299 |
|
1300 static int net_socket_listen_init(VLANState *vlan, const char *host_str) |
|
1301 { |
|
1302 NetSocketListenState *s; |
|
1303 int fd, val, ret; |
|
1304 struct sockaddr_in saddr; |
|
1305 |
|
1306 if (parse_host_port(&saddr, host_str) < 0) |
|
1307 return -1; |
|
1308 |
|
1309 s = qemu_mallocz(sizeof(NetSocketListenState)); |
|
1310 if (!s) |
|
1311 return -1; |
|
1312 |
|
1313 fd = socket(PF_INET, SOCK_STREAM, 0); |
|
1314 if (fd < 0) { |
|
1315 perror("socket"); |
|
1316 return -1; |
|
1317 } |
|
1318 socket_set_nonblock(fd); |
|
1319 |
|
1320 /* allow fast reuse */ |
|
1321 val = 1; |
|
1322 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); |
|
1323 |
|
1324 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
|
1325 if (ret < 0) { |
|
1326 perror("bind"); |
|
1327 return -1; |
|
1328 } |
|
1329 ret = listen(fd, 0); |
|
1330 if (ret < 0) { |
|
1331 perror("listen"); |
|
1332 return -1; |
|
1333 } |
|
1334 s->vlan = vlan; |
|
1335 s->fd = fd; |
|
1336 qemu_set_fd_handler(fd, net_socket_accept, NULL, s); |
|
1337 return 0; |
|
1338 } |
|
1339 |
|
1340 static int net_socket_connect_init(VLANState *vlan, const char *host_str) |
|
1341 { |
|
1342 NetSocketState *s; |
|
1343 int fd, connected, ret, err; |
|
1344 struct sockaddr_in saddr; |
|
1345 |
|
1346 if (parse_host_port(&saddr, host_str) < 0) |
|
1347 return -1; |
|
1348 |
|
1349 fd = socket(PF_INET, SOCK_STREAM, 0); |
|
1350 if (fd < 0) { |
|
1351 perror("socket"); |
|
1352 return -1; |
|
1353 } |
|
1354 socket_set_nonblock(fd); |
|
1355 |
|
1356 connected = 0; |
|
1357 for(;;) { |
|
1358 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); |
|
1359 if (ret < 0) { |
|
1360 err = socket_error(); |
|
1361 if (err == EINTR || err == EWOULDBLOCK) { |
|
1362 } else if (err == EINPROGRESS) { |
|
1363 break; |
|
1364 #ifdef _WIN32 |
|
1365 } else if (err == WSAEALREADY) { |
|
1366 break; |
|
1367 #endif |
|
1368 } else { |
|
1369 perror("connect"); |
|
1370 closesocket(fd); |
|
1371 return -1; |
|
1372 } |
|
1373 } else { |
|
1374 connected = 1; |
|
1375 break; |
|
1376 } |
|
1377 } |
|
1378 s = net_socket_fd_init(vlan, fd, connected); |
|
1379 if (!s) |
|
1380 return -1; |
|
1381 snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
|
1382 "socket: connect to %s:%d", |
|
1383 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
|
1384 return 0; |
|
1385 } |
|
1386 |
|
1387 static int net_socket_mcast_init(VLANState *vlan, const char *host_str) |
|
1388 { |
|
1389 NetSocketState *s; |
|
1390 int fd; |
|
1391 struct sockaddr_in saddr; |
|
1392 |
|
1393 if (parse_host_port(&saddr, host_str) < 0) |
|
1394 return -1; |
|
1395 |
|
1396 |
|
1397 fd = net_socket_mcast_create(&saddr); |
|
1398 if (fd < 0) |
|
1399 return -1; |
|
1400 |
|
1401 s = net_socket_fd_init(vlan, fd, 0); |
|
1402 if (!s) |
|
1403 return -1; |
|
1404 |
|
1405 s->dgram_dst = saddr; |
|
1406 |
|
1407 snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
|
1408 "socket: mcast=%s:%d", |
|
1409 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); |
|
1410 return 0; |
|
1411 |
|
1412 } |
|
1413 |
|
1414 /* find or alloc a new VLAN */ |
|
1415 VLANState *qemu_find_vlan(int id) |
|
1416 { |
|
1417 VLANState **pvlan, *vlan; |
|
1418 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
|
1419 if (vlan->id == id) |
|
1420 return vlan; |
|
1421 } |
|
1422 vlan = qemu_mallocz(sizeof(VLANState)); |
|
1423 if (!vlan) |
|
1424 return NULL; |
|
1425 vlan->id = id; |
|
1426 vlan->next = NULL; |
|
1427 pvlan = &first_vlan; |
|
1428 while (*pvlan != NULL) |
|
1429 pvlan = &(*pvlan)->next; |
|
1430 *pvlan = vlan; |
|
1431 return vlan; |
|
1432 } |
|
1433 |
|
1434 int net_client_init(const char *device, const char *p) |
|
1435 { |
|
1436 char buf[1024]; |
|
1437 int vlan_id, ret; |
|
1438 VLANState *vlan; |
|
1439 |
|
1440 vlan_id = 0; |
|
1441 if (get_param_value(buf, sizeof(buf), "vlan", p)) { |
|
1442 vlan_id = strtol(buf, NULL, 0); |
|
1443 } |
|
1444 vlan = qemu_find_vlan(vlan_id); |
|
1445 if (!vlan) { |
|
1446 fprintf(stderr, "Could not create vlan %d\n", vlan_id); |
|
1447 return -1; |
|
1448 } |
|
1449 if (!strcmp(device, "nic")) { |
|
1450 NICInfo *nd; |
|
1451 uint8_t *macaddr; |
|
1452 |
|
1453 if (nb_nics >= MAX_NICS) { |
|
1454 fprintf(stderr, "Too Many NICs\n"); |
|
1455 return -1; |
|
1456 } |
|
1457 nd = &nd_table[nb_nics]; |
|
1458 macaddr = nd->macaddr; |
|
1459 macaddr[0] = 0x52; |
|
1460 macaddr[1] = 0x54; |
|
1461 macaddr[2] = 0x00; |
|
1462 macaddr[3] = 0x12; |
|
1463 macaddr[4] = 0x34; |
|
1464 macaddr[5] = 0x56 + nb_nics; |
|
1465 |
|
1466 if (get_param_value(buf, sizeof(buf), "macaddr", p)) { |
|
1467 if (parse_macaddr(macaddr, buf) < 0) { |
|
1468 fprintf(stderr, "invalid syntax for ethernet address\n"); |
|
1469 return -1; |
|
1470 } |
|
1471 } |
|
1472 if (get_param_value(buf, sizeof(buf), "model", p)) { |
|
1473 nd->model = strdup(buf); |
|
1474 } |
|
1475 nd->vlan = vlan; |
|
1476 nb_nics++; |
|
1477 vlan->nb_guest_devs++; |
|
1478 ret = 0; |
|
1479 } else |
|
1480 if (!strcmp(device, "none")) { |
|
1481 /* does nothing. It is needed to signal that no network cards |
|
1482 are wanted */ |
|
1483 ret = 0; |
|
1484 } else |
|
1485 #ifdef CONFIG_SLIRP |
|
1486 if (!strcmp(device, "user")) { |
|
1487 if (get_param_value(buf, sizeof(buf), "hostname", p)) { |
|
1488 pstrcpy(slirp_hostname, sizeof(slirp_hostname), buf); |
|
1489 } |
|
1490 vlan->nb_host_devs++; |
|
1491 ret = net_slirp_init(vlan); |
|
1492 } else |
|
1493 #endif |
|
1494 #ifdef _WIN32 |
|
1495 if (!strcmp(device, "tap")) { |
|
1496 char ifname[64]; |
|
1497 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
|
1498 fprintf(stderr, "tap: no interface name\n"); |
|
1499 return -1; |
|
1500 } |
|
1501 vlan->nb_host_devs++; |
|
1502 ret = tap_win32_init(vlan, ifname); |
|
1503 } else |
|
1504 #elif defined (_AIX) |
|
1505 #else |
|
1506 if (!strcmp(device, "tap")) { |
|
1507 char ifname[64]; |
|
1508 char setup_script[1024], down_script[1024]; |
|
1509 int fd; |
|
1510 vlan->nb_host_devs++; |
|
1511 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
|
1512 fd = strtol(buf, NULL, 0); |
|
1513 fcntl(fd, F_SETFL, O_NONBLOCK); |
|
1514 ret = -1; |
|
1515 if (net_tap_fd_init(vlan, fd)) |
|
1516 ret = 0; |
|
1517 } else { |
|
1518 if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
|
1519 ifname[0] = '\0'; |
|
1520 } |
|
1521 if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) { |
|
1522 pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT); |
|
1523 } |
|
1524 if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) { |
|
1525 pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT); |
|
1526 } |
|
1527 ret = net_tap_init(vlan, ifname, setup_script, down_script); |
|
1528 } |
|
1529 } else |
|
1530 #endif |
|
1531 if (!strcmp(device, "socket")) { |
|
1532 if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
|
1533 int fd; |
|
1534 fd = strtol(buf, NULL, 0); |
|
1535 ret = -1; |
|
1536 if (net_socket_fd_init(vlan, fd, 1)) |
|
1537 ret = 0; |
|
1538 } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) { |
|
1539 ret = net_socket_listen_init(vlan, buf); |
|
1540 } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) { |
|
1541 ret = net_socket_connect_init(vlan, buf); |
|
1542 } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) { |
|
1543 ret = net_socket_mcast_init(vlan, buf); |
|
1544 } else { |
|
1545 fprintf(stderr, "Unknown socket options: %s\n", p); |
|
1546 return -1; |
|
1547 } |
|
1548 vlan->nb_host_devs++; |
|
1549 } else |
|
1550 #ifdef CONFIG_VDE |
|
1551 if (!strcmp(device, "vde")) { |
|
1552 char vde_sock[1024], vde_group[512]; |
|
1553 int vde_port, vde_mode; |
|
1554 vlan->nb_host_devs++; |
|
1555 if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) { |
|
1556 vde_sock[0] = '\0'; |
|
1557 } |
|
1558 if (get_param_value(buf, sizeof(buf), "port", p) > 0) { |
|
1559 vde_port = strtol(buf, NULL, 10); |
|
1560 } else { |
|
1561 vde_port = 0; |
|
1562 } |
|
1563 if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) { |
|
1564 vde_group[0] = '\0'; |
|
1565 } |
|
1566 if (get_param_value(buf, sizeof(buf), "mode", p) > 0) { |
|
1567 vde_mode = strtol(buf, NULL, 8); |
|
1568 } else { |
|
1569 vde_mode = 0700; |
|
1570 } |
|
1571 ret = net_vde_init(vlan, vde_sock, vde_port, vde_group, vde_mode); |
|
1572 } else |
|
1573 #endif |
|
1574 { |
|
1575 fprintf(stderr, "Unknown network device: %s\n", device); |
|
1576 return -1; |
|
1577 } |
|
1578 if (ret < 0) { |
|
1579 fprintf(stderr, "Could not initialize device '%s'\n", device); |
|
1580 } |
|
1581 |
|
1582 return ret; |
|
1583 } |
|
1584 |
|
1585 int net_client_parse(const char *str) |
|
1586 { |
|
1587 const char *p; |
|
1588 char *q; |
|
1589 char device[64]; |
|
1590 |
|
1591 p = str; |
|
1592 q = device; |
|
1593 while (*p != '\0' && *p != ',') { |
|
1594 if ((q - device) < sizeof(device) - 1) |
|
1595 *q++ = *p; |
|
1596 p++; |
|
1597 } |
|
1598 *q = '\0'; |
|
1599 if (*p == ',') |
|
1600 p++; |
|
1601 |
|
1602 return net_client_init(device, p); |
|
1603 } |
|
1604 |
|
1605 void do_info_network(void) |
|
1606 { |
|
1607 VLANState *vlan; |
|
1608 VLANClientState *vc; |
|
1609 |
|
1610 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
|
1611 term_printf("VLAN %d devices:\n", vlan->id); |
|
1612 for(vc = vlan->first_client; vc != NULL; vc = vc->next) |
|
1613 term_printf(" %s\n", vc->info_str); |
|
1614 } |
|
1615 } |
|
1616 |
|
1617 void net_cleanup(void) |
|
1618 { |
|
1619 VLANState *vlan; |
|
1620 |
|
1621 #if !defined(_WIN32) |
|
1622 /* close network clients */ |
|
1623 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
|
1624 VLANClientState *vc; |
|
1625 |
|
1626 for(vc = vlan->first_client; vc != NULL; vc = vc->next) { |
|
1627 if (vc->fd_read == tap_receive) { |
|
1628 char ifname[64]; |
|
1629 TAPState *s = vc->opaque; |
|
1630 |
|
1631 if (sscanf(vc->info_str, "tap: ifname=%63s ", ifname) == 1 && |
|
1632 s->down_script[0]) |
|
1633 launch_script(s->down_script, ifname, s->fd); |
|
1634 } |
|
1635 #if defined(CONFIG_VDE) |
|
1636 if (vc->fd_read == vde_from_qemu) { |
|
1637 VDEState *s = vc->opaque; |
|
1638 vde_close(s->vde); |
|
1639 } |
|
1640 #endif |
|
1641 } |
|
1642 } |
|
1643 #endif |
|
1644 } |
|
1645 |
|
1646 void net_client_check(void) |
|
1647 { |
|
1648 VLANState *vlan; |
|
1649 |
|
1650 for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { |
|
1651 if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0) |
|
1652 continue; |
|
1653 if (vlan->nb_guest_devs == 0) |
|
1654 fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id); |
|
1655 if (vlan->nb_host_devs == 0) |
|
1656 fprintf(stderr, |
|
1657 "Warning: vlan %d is not connected to host network\n", |
|
1658 vlan->id); |
|
1659 } |
|
1660 } |