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