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