]>
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" | |
bb9ea79e | 121 | #include "qemu-log.h" |
511d2b14 | 122 | |
d918f23e | 123 | #include "slirp/libslirp.h" |
511d2b14 BS |
124 | |
125 | ||
63a01ef8 AL |
126 | static VLANState *first_vlan; |
127 | ||
128 | /***********************************************************/ | |
129 | /* network device redirectors */ | |
130 | ||
131 | #if defined(DEBUG_NET) || defined(DEBUG_SLIRP) | |
132 | static void hex_dump(FILE *f, const uint8_t *buf, int size) | |
133 | { | |
134 | int len, i, j, c; | |
135 | ||
136 | for(i=0;i<size;i+=16) { | |
137 | len = size - i; | |
138 | if (len > 16) | |
139 | len = 16; | |
140 | fprintf(f, "%08x ", i); | |
141 | for(j=0;j<16;j++) { | |
142 | if (j < len) | |
143 | fprintf(f, " %02x", buf[i+j]); | |
144 | else | |
145 | fprintf(f, " "); | |
146 | } | |
147 | fprintf(f, " "); | |
148 | for(j=0;j<len;j++) { | |
149 | c = buf[i+j]; | |
150 | if (c < ' ' || c > '~') | |
151 | c = '.'; | |
152 | fprintf(f, "%c", c); | |
153 | } | |
154 | fprintf(f, "\n"); | |
155 | } | |
156 | } | |
157 | #endif | |
158 | ||
159 | static int parse_macaddr(uint8_t *macaddr, const char *p) | |
160 | { | |
161 | int i; | |
162 | char *last_char; | |
163 | long int offset; | |
164 | ||
165 | errno = 0; | |
166 | offset = strtol(p, &last_char, 0); | |
167 | if (0 == errno && '\0' == *last_char && | |
168 | offset >= 0 && offset <= 0xFFFFFF) { | |
169 | macaddr[3] = (offset & 0xFF0000) >> 16; | |
170 | macaddr[4] = (offset & 0xFF00) >> 8; | |
171 | macaddr[5] = offset & 0xFF; | |
172 | return 0; | |
173 | } else { | |
174 | for(i = 0; i < 6; i++) { | |
175 | macaddr[i] = strtol(p, (char **)&p, 16); | |
176 | if (i == 5) { | |
177 | if (*p != '\0') | |
178 | return -1; | |
179 | } else { | |
180 | if (*p != ':' && *p != '-') | |
181 | return -1; | |
182 | p++; | |
183 | } | |
184 | } | |
185 | return 0; | |
186 | } | |
187 | ||
188 | return -1; | |
189 | } | |
190 | ||
191 | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) | |
192 | { | |
193 | const char *p, *p1; | |
194 | int len; | |
195 | p = *pp; | |
196 | p1 = strchr(p, sep); | |
197 | if (!p1) | |
198 | return -1; | |
199 | len = p1 - p; | |
200 | p1++; | |
201 | if (buf_size > 0) { | |
202 | if (len > buf_size - 1) | |
203 | len = buf_size - 1; | |
204 | memcpy(buf, p, len); | |
205 | buf[len] = '\0'; | |
206 | } | |
207 | *pp = p1; | |
208 | return 0; | |
209 | } | |
210 | ||
211 | int parse_host_src_port(struct sockaddr_in *haddr, | |
212 | struct sockaddr_in *saddr, | |
213 | const char *input_str) | |
214 | { | |
215 | char *str = strdup(input_str); | |
216 | char *host_str = str; | |
217 | char *src_str; | |
218 | const char *src_str2; | |
219 | char *ptr; | |
220 | ||
221 | /* | |
222 | * Chop off any extra arguments at the end of the string which | |
223 | * would start with a comma, then fill in the src port information | |
224 | * if it was provided else use the "any address" and "any port". | |
225 | */ | |
226 | if ((ptr = strchr(str,','))) | |
227 | *ptr = '\0'; | |
228 | ||
229 | if ((src_str = strchr(input_str,'@'))) { | |
230 | *src_str = '\0'; | |
231 | src_str++; | |
232 | } | |
233 | ||
234 | if (parse_host_port(haddr, host_str) < 0) | |
235 | goto fail; | |
236 | ||
237 | src_str2 = src_str; | |
238 | if (!src_str || *src_str == '\0') | |
239 | src_str2 = ":0"; | |
240 | ||
241 | if (parse_host_port(saddr, src_str2) < 0) | |
242 | goto fail; | |
243 | ||
244 | free(str); | |
245 | return(0); | |
246 | ||
247 | fail: | |
248 | free(str); | |
249 | return -1; | |
250 | } | |
251 | ||
252 | int parse_host_port(struct sockaddr_in *saddr, const char *str) | |
253 | { | |
254 | char buf[512]; | |
255 | struct hostent *he; | |
256 | const char *p, *r; | |
257 | int port; | |
258 | ||
259 | p = str; | |
260 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) | |
261 | return -1; | |
262 | saddr->sin_family = AF_INET; | |
263 | if (buf[0] == '\0') { | |
264 | saddr->sin_addr.s_addr = 0; | |
265 | } else { | |
cd390083 | 266 | if (qemu_isdigit(buf[0])) { |
63a01ef8 AL |
267 | if (!inet_aton(buf, &saddr->sin_addr)) |
268 | return -1; | |
269 | } else { | |
270 | if ((he = gethostbyname(buf)) == NULL) | |
271 | return - 1; | |
272 | saddr->sin_addr = *(struct in_addr *)he->h_addr; | |
273 | } | |
274 | } | |
275 | port = strtol(p, (char **)&r, 0); | |
276 | if (r == p) | |
277 | return -1; | |
278 | saddr->sin_port = htons(port); | |
279 | return 0; | |
280 | } | |
281 | ||
69d6451c BS |
282 | #if !defined(_WIN32) && 0 |
283 | static int parse_unix_path(struct sockaddr_un *uaddr, const char *str) | |
63a01ef8 AL |
284 | { |
285 | const char *p; | |
286 | int len; | |
287 | ||
288 | len = MIN(108, strlen(str)); | |
289 | p = strchr(str, ','); | |
290 | if (p) | |
291 | len = MIN(len, p - str); | |
292 | ||
293 | memset(uaddr, 0, sizeof(*uaddr)); | |
294 | ||
295 | uaddr->sun_family = AF_UNIX; | |
296 | memcpy(uaddr->sun_path, str, len); | |
297 | ||
298 | return 0; | |
299 | } | |
300 | #endif | |
301 | ||
7cb7434b AL |
302 | void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6]) |
303 | { | |
304 | snprintf(vc->info_str, sizeof(vc->info_str), | |
4dda4063 AL |
305 | "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x", |
306 | vc->model, | |
7cb7434b AL |
307 | macaddr[0], macaddr[1], macaddr[2], |
308 | macaddr[3], macaddr[4], macaddr[5]); | |
309 | } | |
310 | ||
676cff29 AL |
311 | static char *assign_name(VLANClientState *vc1, const char *model) |
312 | { | |
313 | VLANState *vlan; | |
314 | char buf[256]; | |
315 | int id = 0; | |
316 | ||
317 | for (vlan = first_vlan; vlan; vlan = vlan->next) { | |
318 | VLANClientState *vc; | |
319 | ||
320 | for (vc = vlan->first_client; vc; vc = vc->next) | |
321 | if (vc != vc1 && strcmp(vc->model, model) == 0) | |
322 | id++; | |
323 | } | |
324 | ||
325 | snprintf(buf, sizeof(buf), "%s.%d", model, id); | |
326 | ||
327 | return strdup(buf); | |
328 | } | |
329 | ||
63a01ef8 | 330 | VLANClientState *qemu_new_vlan_client(VLANState *vlan, |
bf38c1a0 | 331 | const char *model, |
7a9f6e4a | 332 | const char *name, |
cda9046b MM |
333 | NetCanReceive *can_receive, |
334 | NetReceive *receive, | |
335 | NetReceiveIOV *receive_iov, | |
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); | |
cda9046b MM |
346 | vc->can_receive = can_receive; |
347 | vc->receive = receive; | |
348 | vc->receive_iov = receive_iov; | |
b946a153 | 349 | vc->cleanup = cleanup; |
63a01ef8 AL |
350 | vc->opaque = opaque; |
351 | vc->vlan = vlan; | |
352 | ||
353 | vc->next = NULL; | |
354 | pvc = &vlan->first_client; | |
355 | while (*pvc != NULL) | |
356 | pvc = &(*pvc)->next; | |
357 | *pvc = vc; | |
358 | return vc; | |
359 | } | |
360 | ||
361 | void qemu_del_vlan_client(VLANClientState *vc) | |
362 | { | |
363 | VLANClientState **pvc = &vc->vlan->first_client; | |
364 | ||
365 | while (*pvc != NULL) | |
366 | if (*pvc == vc) { | |
367 | *pvc = vc->next; | |
b946a153 AL |
368 | if (vc->cleanup) { |
369 | vc->cleanup(vc); | |
370 | } | |
676cff29 | 371 | free(vc->name); |
bf38c1a0 | 372 | free(vc->model); |
dad35419 | 373 | qemu_free(vc); |
63a01ef8 AL |
374 | break; |
375 | } else | |
376 | pvc = &(*pvc)->next; | |
377 | } | |
378 | ||
8b13c4a7 AL |
379 | VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque) |
380 | { | |
381 | VLANClientState **pvc = &vlan->first_client; | |
382 | ||
383 | while (*pvc != NULL) | |
384 | if ((*pvc)->opaque == opaque) | |
385 | return *pvc; | |
386 | else | |
387 | pvc = &(*pvc)->next; | |
388 | ||
389 | return NULL; | |
390 | } | |
391 | ||
2e1e0641 | 392 | int qemu_can_send_packet(VLANClientState *sender) |
63a01ef8 | 393 | { |
2e1e0641 | 394 | VLANState *vlan = sender->vlan; |
63a01ef8 AL |
395 | VLANClientState *vc; |
396 | ||
2e1e0641 MM |
397 | for (vc = vlan->first_client; vc != NULL; vc = vc->next) { |
398 | if (vc == sender) { | |
399 | continue; | |
400 | } | |
401 | ||
cda9046b | 402 | /* no can_receive() handler, they can always receive */ |
e3f5ec2b | 403 | if (!vc->can_receive || vc->can_receive(vc)) { |
2e1e0641 | 404 | return 1; |
63a01ef8 AL |
405 | } |
406 | } | |
407 | return 0; | |
408 | } | |
409 | ||
3e021d40 | 410 | static int |
764a4d1d | 411 | qemu_deliver_packet(VLANClientState *sender, const uint8_t *buf, int size) |
63a01ef8 | 412 | { |
63a01ef8 | 413 | VLANClientState *vc; |
3e021d40 | 414 | int ret = -1; |
63a01ef8 | 415 | |
e94667b9 MM |
416 | sender->vlan->delivering = 1; |
417 | ||
764a4d1d | 418 | for (vc = sender->vlan->first_client; vc != NULL; vc = vc->next) { |
3e021d40 MM |
419 | ssize_t len; |
420 | ||
421 | if (vc == sender) { | |
422 | continue; | |
764a4d1d | 423 | } |
3e021d40 MM |
424 | |
425 | if (vc->link_down) { | |
426 | ret = size; | |
427 | continue; | |
428 | } | |
429 | ||
430 | len = vc->receive(vc, buf, size); | |
431 | ||
432 | ret = (ret >= 0) ? ret : len; | |
764a4d1d | 433 | } |
3e021d40 | 434 | |
e94667b9 MM |
435 | sender->vlan->delivering = 0; |
436 | ||
3e021d40 | 437 | return ret; |
764a4d1d AL |
438 | } |
439 | ||
8cad5516 MM |
440 | void qemu_purge_queued_packets(VLANClientState *vc) |
441 | { | |
442 | VLANPacket **pp = &vc->vlan->send_queue; | |
443 | ||
444 | while (*pp != NULL) { | |
445 | VLANPacket *packet = *pp; | |
446 | ||
447 | if (packet->sender == vc) { | |
448 | *pp = packet->next; | |
449 | qemu_free(packet); | |
450 | } else { | |
451 | pp = &packet->next; | |
452 | } | |
453 | } | |
454 | } | |
455 | ||
f3b6c7fc | 456 | void qemu_flush_queued_packets(VLANClientState *vc) |
e94667b9 MM |
457 | { |
458 | VLANPacket *packet; | |
459 | ||
460 | while ((packet = vc->vlan->send_queue) != NULL) { | |
f3b6c7fc MM |
461 | int ret; |
462 | ||
e94667b9 | 463 | vc->vlan->send_queue = packet->next; |
f3b6c7fc MM |
464 | |
465 | ret = qemu_deliver_packet(packet->sender, packet->data, packet->size); | |
466 | if (ret == 0 && packet->sent_cb != NULL) { | |
467 | packet->next = vc->vlan->send_queue; | |
468 | vc->vlan->send_queue = packet; | |
469 | break; | |
470 | } | |
471 | ||
472 | if (packet->sent_cb) | |
783527a9 | 473 | packet->sent_cb(packet->sender, ret); |
f3b6c7fc | 474 | |
e94667b9 MM |
475 | qemu_free(packet); |
476 | } | |
477 | } | |
478 | ||
f3b6c7fc MM |
479 | static void qemu_enqueue_packet(VLANClientState *sender, |
480 | const uint8_t *buf, int size, | |
481 | NetPacketSent *sent_cb) | |
e94667b9 MM |
482 | { |
483 | VLANPacket *packet; | |
484 | ||
485 | packet = qemu_malloc(sizeof(VLANPacket) + size); | |
486 | packet->next = sender->vlan->send_queue; | |
487 | packet->sender = sender; | |
488 | packet->size = size; | |
f3b6c7fc | 489 | packet->sent_cb = sent_cb; |
e94667b9 MM |
490 | memcpy(packet->data, buf, size); |
491 | sender->vlan->send_queue = packet; | |
492 | } | |
493 | ||
f3b6c7fc MM |
494 | ssize_t qemu_send_packet_async(VLANClientState *sender, |
495 | const uint8_t *buf, int size, | |
496 | NetPacketSent *sent_cb) | |
764a4d1d | 497 | { |
f3b6c7fc | 498 | int ret; |
764a4d1d | 499 | |
f3b6c7fc MM |
500 | if (sender->link_down) { |
501 | return size; | |
502 | } | |
436e5e53 | 503 | |
63a01ef8 | 504 | #ifdef DEBUG_NET |
f3b6c7fc | 505 | printf("vlan %d send:\n", sender->vlan->id); |
63a01ef8 AL |
506 | hex_dump(stdout, buf, size); |
507 | #endif | |
f3b6c7fc MM |
508 | |
509 | if (sender->vlan->delivering) { | |
510 | qemu_enqueue_packet(sender, buf, size, NULL); | |
511 | return size; | |
512 | } | |
513 | ||
514 | ret = qemu_deliver_packet(sender, buf, size); | |
515 | if (ret == 0 && sent_cb != NULL) { | |
516 | qemu_enqueue_packet(sender, buf, size, sent_cb); | |
517 | return 0; | |
63a01ef8 | 518 | } |
e94667b9 | 519 | |
f3b6c7fc MM |
520 | qemu_flush_queued_packets(sender); |
521 | ||
522 | return ret; | |
523 | } | |
524 | ||
525 | void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size) | |
526 | { | |
527 | qemu_send_packet_async(vc, buf, size, NULL); | |
63a01ef8 AL |
528 | } |
529 | ||
fbe78f4f AL |
530 | static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov, |
531 | int iovcnt) | |
532 | { | |
533 | uint8_t buffer[4096]; | |
534 | size_t offset = 0; | |
535 | int i; | |
536 | ||
537 | for (i = 0; i < iovcnt; i++) { | |
538 | size_t len; | |
539 | ||
540 | len = MIN(sizeof(buffer) - offset, iov[i].iov_len); | |
541 | memcpy(buffer + offset, iov[i].iov_base, len); | |
542 | offset += len; | |
543 | } | |
544 | ||
f3b6c7fc | 545 | return vc->receive(vc, buffer, offset); |
fbe78f4f AL |
546 | } |
547 | ||
e0e7877a AL |
548 | static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt) |
549 | { | |
550 | size_t offset = 0; | |
551 | int i; | |
552 | ||
553 | for (i = 0; i < iovcnt; i++) | |
554 | offset += iov[i].iov_len; | |
555 | return offset; | |
556 | } | |
557 | ||
e94667b9 MM |
558 | static int qemu_deliver_packet_iov(VLANClientState *sender, |
559 | const struct iovec *iov, int iovcnt) | |
fbe78f4f | 560 | { |
fbe78f4f | 561 | VLANClientState *vc; |
e94667b9 MM |
562 | int ret = -1; |
563 | ||
564 | sender->vlan->delivering = 1; | |
565 | ||
566 | for (vc = sender->vlan->first_client; vc != NULL; vc = vc->next) { | |
567 | ssize_t len; | |
568 | ||
569 | if (vc == sender) { | |
570 | continue; | |
571 | } | |
572 | ||
573 | if (vc->link_down) { | |
574 | ret = calc_iov_length(iov, iovcnt); | |
575 | continue; | |
576 | } | |
577 | ||
578 | if (vc->receive_iov) { | |
579 | len = vc->receive_iov(vc, iov, iovcnt); | |
580 | } else { | |
581 | len = vc_sendv_compat(vc, iov, iovcnt); | |
582 | } | |
583 | ||
584 | ret = (ret >= 0) ? ret : len; | |
585 | } | |
586 | ||
587 | sender->vlan->delivering = 0; | |
588 | ||
589 | return ret; | |
590 | } | |
591 | ||
592 | static ssize_t qemu_enqueue_packet_iov(VLANClientState *sender, | |
f3b6c7fc MM |
593 | const struct iovec *iov, int iovcnt, |
594 | NetPacketSent *sent_cb) | |
e94667b9 | 595 | { |
c27ff608 | 596 | VLANPacket *packet; |
e94667b9 | 597 | size_t max_len = 0; |
c27ff608 | 598 | int i; |
fbe78f4f | 599 | |
e94667b9 | 600 | max_len = calc_iov_length(iov, iovcnt); |
e0e7877a | 601 | |
e94667b9 MM |
602 | packet = qemu_malloc(sizeof(VLANPacket) + max_len); |
603 | packet->next = sender->vlan->send_queue; | |
604 | packet->sender = sender; | |
f3b6c7fc | 605 | packet->sent_cb = sent_cb; |
e94667b9 | 606 | packet->size = 0; |
c27ff608 | 607 | |
e94667b9 MM |
608 | for (i = 0; i < iovcnt; i++) { |
609 | size_t len = iov[i].iov_len; | |
c27ff608 | 610 | |
e94667b9 MM |
611 | memcpy(packet->data + packet->size, iov[i].iov_base, len); |
612 | packet->size += len; | |
613 | } | |
fbe78f4f | 614 | |
e94667b9 | 615 | sender->vlan->send_queue = packet; |
fbe78f4f | 616 | |
e94667b9 MM |
617 | return packet->size; |
618 | } | |
fbe78f4f | 619 | |
f3b6c7fc MM |
620 | ssize_t qemu_sendv_packet_async(VLANClientState *sender, |
621 | const struct iovec *iov, int iovcnt, | |
622 | NetPacketSent *sent_cb) | |
e94667b9 MM |
623 | { |
624 | int ret; | |
625 | ||
626 | if (sender->link_down) { | |
627 | return calc_iov_length(iov, iovcnt); | |
628 | } | |
629 | ||
630 | if (sender->vlan->delivering) { | |
f3b6c7fc | 631 | return qemu_enqueue_packet_iov(sender, iov, iovcnt, NULL); |
fbe78f4f AL |
632 | } |
633 | ||
e94667b9 | 634 | ret = qemu_deliver_packet_iov(sender, iov, iovcnt); |
f3b6c7fc MM |
635 | if (ret == 0 && sent_cb != NULL) { |
636 | qemu_enqueue_packet_iov(sender, iov, iovcnt, sent_cb); | |
637 | return 0; | |
638 | } | |
e94667b9 MM |
639 | |
640 | qemu_flush_queued_packets(sender); | |
641 | ||
642 | return ret; | |
fbe78f4f AL |
643 | } |
644 | ||
f3b6c7fc MM |
645 | ssize_t |
646 | qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt) | |
647 | { | |
648 | return qemu_sendv_packet_async(vc, iov, iovcnt, NULL); | |
649 | } | |
650 | ||
10ae5a7a JK |
651 | static void config_error(Monitor *mon, const char *fmt, ...) |
652 | { | |
653 | va_list ap; | |
654 | ||
655 | va_start(ap, fmt); | |
656 | if (mon) { | |
657 | monitor_vprintf(mon, fmt, ap); | |
658 | } else { | |
659 | fprintf(stderr, "qemu: "); | |
660 | vfprintf(stderr, fmt, ap); | |
661 | exit(1); | |
662 | } | |
663 | va_end(ap); | |
664 | } | |
665 | ||
63a01ef8 AL |
666 | #if defined(CONFIG_SLIRP) |
667 | ||
668 | /* slirp network adapter */ | |
669 | ||
c92ef6a2 JK |
670 | #define SLIRP_CFG_HOSTFWD 1 |
671 | #define SLIRP_CFG_LEGACY 2 | |
ad196a9d | 672 | |
b8e8af38 JK |
673 | struct slirp_config_str { |
674 | struct slirp_config_str *next; | |
ad196a9d JK |
675 | int flags; |
676 | char str[1024]; | |
c92ef6a2 | 677 | int legacy_format; |
b8e8af38 JK |
678 | }; |
679 | ||
9f8bd042 | 680 | typedef struct SlirpState { |
b1c99fcd | 681 | TAILQ_ENTRY(SlirpState) entry; |
9f8bd042 JK |
682 | VLANClientState *vc; |
683 | Slirp *slirp; | |
684 | } SlirpState; | |
685 | ||
ad196a9d JK |
686 | static struct slirp_config_str *slirp_configs; |
687 | const char *legacy_tftp_prefix; | |
688 | const char *legacy_bootp_filename; | |
b1c99fcd JK |
689 | static TAILQ_HEAD(slirp_stacks, SlirpState) slirp_stacks = |
690 | TAILQ_HEAD_INITIALIZER(slirp_stacks); | |
63a01ef8 | 691 | |
9f8bd042 | 692 | static void slirp_hostfwd(SlirpState *s, Monitor *mon, const char *redir_str, |
3c6a0580 | 693 | int legacy_format); |
9f8bd042 | 694 | static void slirp_guestfwd(SlirpState *s, Monitor *mon, const char *config_str, |
c92ef6a2 | 695 | int legacy_format); |
ad196a9d | 696 | |
c5b76b38 | 697 | #ifndef _WIN32 |
ad196a9d JK |
698 | static const char *legacy_smb_export; |
699 | ||
9f8bd042 JK |
700 | static void slirp_smb(SlirpState *s, const char *exported_dir, |
701 | struct in_addr vserver_addr); | |
c5b76b38 | 702 | #endif |
b8e8af38 | 703 | |
9f8bd042 | 704 | int slirp_can_output(void *opaque) |
63a01ef8 | 705 | { |
9f8bd042 JK |
706 | SlirpState *s = opaque; |
707 | ||
708 | return qemu_can_send_packet(s->vc); | |
63a01ef8 AL |
709 | } |
710 | ||
9f8bd042 | 711 | void slirp_output(void *opaque, const uint8_t *pkt, int pkt_len) |
63a01ef8 | 712 | { |
9f8bd042 JK |
713 | SlirpState *s = opaque; |
714 | ||
63a01ef8 AL |
715 | #ifdef DEBUG_SLIRP |
716 | printf("slirp output:\n"); | |
717 | hex_dump(stdout, pkt, pkt_len); | |
718 | #endif | |
9f8bd042 | 719 | qemu_send_packet(s->vc, pkt, pkt_len); |
63a01ef8 AL |
720 | } |
721 | ||
4f1c942b | 722 | static ssize_t slirp_receive(VLANClientState *vc, const uint8_t *buf, size_t size) |
63a01ef8 | 723 | { |
9f8bd042 JK |
724 | SlirpState *s = vc->opaque; |
725 | ||
63a01ef8 AL |
726 | #ifdef DEBUG_SLIRP |
727 | printf("slirp input:\n"); | |
728 | hex_dump(stdout, buf, size); | |
729 | #endif | |
9f8bd042 | 730 | slirp_input(s->slirp, buf, size); |
4f1c942b | 731 | return size; |
63a01ef8 AL |
732 | } |
733 | ||
8d6249a7 AL |
734 | static void net_slirp_cleanup(VLANClientState *vc) |
735 | { | |
ad0d8c4c JK |
736 | SlirpState *s = vc->opaque; |
737 | ||
738 | slirp_cleanup(s->slirp); | |
b1c99fcd | 739 | TAILQ_REMOVE(&slirp_stacks, s, entry); |
ad0d8c4c | 740 | qemu_free(s); |
8d6249a7 AL |
741 | } |
742 | ||
ad196a9d | 743 | static int net_slirp_init(Monitor *mon, VLANState *vlan, const char *model, |
c92ef6a2 JK |
744 | const char *name, int restricted, |
745 | const char *vnetwork, const char *vhost, | |
746 | const char *vhostname, const char *tftp_export, | |
747 | const char *bootfile, const char *vdhcp_start, | |
748 | const char *vnameserver, const char *smb_export, | |
749 | const char *vsmbserver) | |
63a01ef8 | 750 | { |
ad0d8c4c JK |
751 | /* default settings according to historic slirp */ |
752 | struct in_addr net = { .s_addr = htonl(0x0a000000) }; /* 10.0.0.0 */ | |
753 | struct in_addr mask = { .s_addr = htonl(0xff000000) }; /* 255.0.0.0 */ | |
754 | struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */ | |
755 | struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */ | |
756 | struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */ | |
c92ef6a2 | 757 | #ifndef _WIN32 |
ad0d8c4c | 758 | struct in_addr smbsrv = { .s_addr = 0 }; |
c92ef6a2 | 759 | #endif |
ad0d8c4c JK |
760 | SlirpState *s; |
761 | char buf[20]; | |
762 | uint32_t addr; | |
763 | int shift; | |
764 | char *end; | |
765 | ||
766 | if (!tftp_export) { | |
767 | tftp_export = legacy_tftp_prefix; | |
768 | } | |
769 | if (!bootfile) { | |
770 | bootfile = legacy_bootp_filename; | |
771 | } | |
c92ef6a2 | 772 | |
ad0d8c4c JK |
773 | if (vnetwork) { |
774 | if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) { | |
775 | if (!inet_aton(vnetwork, &net)) { | |
776 | return -1; | |
777 | } | |
778 | addr = ntohl(net.s_addr); | |
779 | if (!(addr & 0x80000000)) { | |
780 | mask.s_addr = htonl(0xff000000); /* class A */ | |
781 | } else if ((addr & 0xfff00000) == 0xac100000) { | |
782 | mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */ | |
783 | } else if ((addr & 0xc0000000) == 0x80000000) { | |
784 | mask.s_addr = htonl(0xffff0000); /* class B */ | |
785 | } else if ((addr & 0xffff0000) == 0xc0a80000) { | |
786 | mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */ | |
787 | } else if ((addr & 0xffff0000) == 0xc6120000) { | |
788 | mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */ | |
789 | } else if ((addr & 0xe0000000) == 0xe0000000) { | |
790 | mask.s_addr = htonl(0xffffff00); /* class C */ | |
c92ef6a2 | 791 | } else { |
ad0d8c4c JK |
792 | mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */ |
793 | } | |
794 | } else { | |
795 | if (!inet_aton(buf, &net)) { | |
796 | return -1; | |
797 | } | |
798 | shift = strtol(vnetwork, &end, 10); | |
799 | if (*end != '\0') { | |
800 | if (!inet_aton(vnetwork, &mask)) { | |
c92ef6a2 | 801 | return -1; |
c92ef6a2 | 802 | } |
ad0d8c4c JK |
803 | } else if (shift < 4 || shift > 32) { |
804 | return -1; | |
805 | } else { | |
806 | mask.s_addr = htonl(0xffffffff << (32 - shift)); | |
c92ef6a2 | 807 | } |
c92ef6a2 | 808 | } |
ad0d8c4c JK |
809 | net.s_addr &= mask.s_addr; |
810 | host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr); | |
811 | dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr); | |
812 | dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr); | |
813 | } | |
c92ef6a2 | 814 | |
ad0d8c4c JK |
815 | if (vhost && !inet_aton(vhost, &host)) { |
816 | return -1; | |
817 | } | |
818 | if ((host.s_addr & mask.s_addr) != net.s_addr) { | |
819 | return -1; | |
820 | } | |
c92ef6a2 | 821 | |
ad0d8c4c JK |
822 | if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) { |
823 | return -1; | |
824 | } | |
825 | if ((dhcp.s_addr & mask.s_addr) != net.s_addr || | |
826 | dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) { | |
827 | return -1; | |
828 | } | |
c92ef6a2 | 829 | |
ad0d8c4c JK |
830 | if (vnameserver && !inet_aton(vnameserver, &dns)) { |
831 | return -1; | |
832 | } | |
833 | if ((dns.s_addr & mask.s_addr) != net.s_addr || | |
834 | dns.s_addr == host.s_addr) { | |
835 | return -1; | |
836 | } | |
c92ef6a2 JK |
837 | |
838 | #ifndef _WIN32 | |
ad0d8c4c JK |
839 | if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) { |
840 | return -1; | |
841 | } | |
c92ef6a2 JK |
842 | #endif |
843 | ||
ad0d8c4c JK |
844 | s = qemu_mallocz(sizeof(SlirpState)); |
845 | s->slirp = slirp_init(restricted, net, mask, host, vhostname, | |
846 | tftp_export, bootfile, dhcp, dns, s); | |
b1c99fcd | 847 | TAILQ_INSERT_TAIL(&slirp_stacks, s, entry); |
b8e8af38 | 848 | |
ad0d8c4c JK |
849 | while (slirp_configs) { |
850 | struct slirp_config_str *config = slirp_configs; | |
b8e8af38 | 851 | |
ad0d8c4c JK |
852 | if (config->flags & SLIRP_CFG_HOSTFWD) { |
853 | slirp_hostfwd(s, mon, config->str, | |
854 | config->flags & SLIRP_CFG_LEGACY); | |
855 | } else { | |
856 | slirp_guestfwd(s, mon, config->str, | |
857 | config->flags & SLIRP_CFG_LEGACY); | |
b8e8af38 | 858 | } |
ad0d8c4c JK |
859 | slirp_configs = config->next; |
860 | qemu_free(config); | |
861 | } | |
b8e8af38 | 862 | #ifndef _WIN32 |
ad0d8c4c JK |
863 | if (!smb_export) { |
864 | smb_export = legacy_smb_export; | |
865 | } | |
866 | if (smb_export) { | |
867 | slirp_smb(s, smb_export, smbsrv); | |
63a01ef8 | 868 | } |
ad0d8c4c | 869 | #endif |
b8e8af38 | 870 | |
9f8bd042 JK |
871 | s->vc = qemu_new_vlan_client(vlan, model, name, NULL, slirp_receive, NULL, |
872 | net_slirp_cleanup, s); | |
873 | s->vc->info_str[0] = '\0'; | |
63a01ef8 AL |
874 | return 0; |
875 | } | |
876 | ||
3c6a0580 | 877 | void net_slirp_hostfwd_remove(Monitor *mon, const char *src_str) |
c1261d8d | 878 | { |
3c6a0580 | 879 | struct in_addr host_addr = { .s_addr = INADDR_ANY }; |
c1261d8d AG |
880 | int host_port; |
881 | char buf[256] = ""; | |
3c6a0580 | 882 | const char *p = src_str; |
c1261d8d | 883 | int is_udp = 0; |
9c12a6f2 | 884 | int err; |
c1261d8d | 885 | |
b1c99fcd | 886 | if (TAILQ_EMPTY(&slirp_stacks)) { |
f3546deb | 887 | monitor_printf(mon, "user mode network stack not in use\n"); |
c1261d8d | 888 | return; |
f3546deb | 889 | } |
c1261d8d | 890 | |
3c6a0580 | 891 | if (!src_str || !src_str[0]) |
c1261d8d AG |
892 | goto fail_syntax; |
893 | ||
894 | get_str_sep(buf, sizeof(buf), &p, ':'); | |
895 | ||
896 | if (!strcmp(buf, "tcp") || buf[0] == '\0') { | |
897 | is_udp = 0; | |
898 | } else if (!strcmp(buf, "udp")) { | |
899 | is_udp = 1; | |
900 | } else { | |
901 | goto fail_syntax; | |
902 | } | |
903 | ||
3c6a0580 JK |
904 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
905 | goto fail_syntax; | |
906 | } | |
907 | if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) { | |
908 | goto fail_syntax; | |
909 | } | |
910 | ||
c1261d8d AG |
911 | host_port = atoi(p); |
912 | ||
b1c99fcd | 913 | err = slirp_remove_hostfwd(TAILQ_FIRST(&slirp_stacks)->slirp, is_udp, |
9f8bd042 | 914 | host_addr, host_port); |
c1261d8d | 915 | |
9c12a6f2 JK |
916 | monitor_printf(mon, "host forwarding rule for %s %s\n", src_str, |
917 | err ? "removed" : "not found"); | |
c1261d8d AG |
918 | return; |
919 | ||
920 | fail_syntax: | |
921 | monitor_printf(mon, "invalid format\n"); | |
922 | } | |
923 | ||
9f8bd042 | 924 | static void slirp_hostfwd(SlirpState *s, Monitor *mon, const char *redir_str, |
3c6a0580 | 925 | int legacy_format) |
63a01ef8 | 926 | { |
3c6a0580 | 927 | struct in_addr host_addr = { .s_addr = INADDR_ANY }; |
c92ef6a2 | 928 | struct in_addr guest_addr = { .s_addr = 0 }; |
63a01ef8 | 929 | int host_port, guest_port; |
b8e8af38 | 930 | const char *p; |
c92ef6a2 | 931 | char buf[256]; |
b8e8af38 | 932 | int is_udp; |
c92ef6a2 | 933 | char *end; |
1c6ed9f3 | 934 | |
63a01ef8 | 935 | p = redir_str; |
b8e8af38 | 936 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
d4ebe193 | 937 | goto fail_syntax; |
b8e8af38 | 938 | } |
d4ebe193 | 939 | if (!strcmp(buf, "tcp") || buf[0] == '\0') { |
63a01ef8 AL |
940 | is_udp = 0; |
941 | } else if (!strcmp(buf, "udp")) { | |
942 | is_udp = 1; | |
943 | } else { | |
d4ebe193 | 944 | goto fail_syntax; |
63a01ef8 AL |
945 | } |
946 | ||
3c6a0580 JK |
947 | if (!legacy_format) { |
948 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
949 | goto fail_syntax; | |
950 | } | |
951 | if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) { | |
952 | goto fail_syntax; | |
953 | } | |
954 | } | |
955 | ||
956 | if (get_str_sep(buf, sizeof(buf), &p, legacy_format ? ':' : '-') < 0) { | |
d4ebe193 | 957 | goto fail_syntax; |
b8e8af38 | 958 | } |
c92ef6a2 JK |
959 | host_port = strtol(buf, &end, 0); |
960 | if (*end != '\0' || host_port < 1 || host_port > 65535) { | |
d4ebe193 | 961 | goto fail_syntax; |
b8e8af38 | 962 | } |
63a01ef8 | 963 | |
b8e8af38 | 964 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { |
d4ebe193 | 965 | goto fail_syntax; |
b8e8af38 | 966 | } |
c92ef6a2 | 967 | if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) { |
d4ebe193 | 968 | goto fail_syntax; |
b8e8af38 | 969 | } |
63a01ef8 | 970 | |
c92ef6a2 JK |
971 | guest_port = strtol(p, &end, 0); |
972 | if (*end != '\0' || guest_port < 1 || guest_port > 65535) { | |
d4ebe193 | 973 | goto fail_syntax; |
b8e8af38 | 974 | } |
63a01ef8 | 975 | |
9f8bd042 JK |
976 | if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr, |
977 | guest_port) < 0) { | |
c92ef6a2 JK |
978 | config_error(mon, "could not set up host forwarding rule '%s'\n", |
979 | redir_str); | |
63a01ef8 AL |
980 | } |
981 | return; | |
d4ebe193 AL |
982 | |
983 | fail_syntax: | |
c92ef6a2 | 984 | config_error(mon, "invalid host forwarding rule '%s'\n", redir_str); |
63a01ef8 AL |
985 | } |
986 | ||
f3546deb | 987 | void net_slirp_hostfwd_add(Monitor *mon, const char *redir_str) |
b8e8af38 | 988 | { |
b1c99fcd | 989 | if (TAILQ_EMPTY(&slirp_stacks)) { |
f3546deb | 990 | monitor_printf(mon, "user mode network stack not in use\n"); |
b8e8af38 JK |
991 | return; |
992 | } | |
993 | ||
b1c99fcd | 994 | slirp_hostfwd(TAILQ_FIRST(&slirp_stacks), mon, redir_str, 0); |
f3546deb JK |
995 | } |
996 | ||
997 | void net_slirp_redir(const char *redir_str) | |
998 | { | |
999 | struct slirp_config_str *config; | |
1000 | ||
b1c99fcd | 1001 | if (TAILQ_EMPTY(&slirp_stacks)) { |
f3546deb JK |
1002 | config = qemu_malloc(sizeof(*config)); |
1003 | pstrcpy(config->str, sizeof(config->str), redir_str); | |
3c6a0580 | 1004 | config->flags = SLIRP_CFG_HOSTFWD | SLIRP_CFG_LEGACY; |
f3546deb JK |
1005 | config->next = slirp_configs; |
1006 | slirp_configs = config; | |
b8e8af38 JK |
1007 | return; |
1008 | } | |
1009 | ||
b1c99fcd | 1010 | slirp_hostfwd(TAILQ_FIRST(&slirp_stacks), NULL, redir_str, 1); |
b8e8af38 JK |
1011 | } |
1012 | ||
63a01ef8 AL |
1013 | #ifndef _WIN32 |
1014 | ||
1015 | static char smb_dir[1024]; | |
1016 | ||
63a01ef8 AL |
1017 | /* automatic user mode samba server configuration */ |
1018 | static void smb_exit(void) | |
1019 | { | |
09c18925 JK |
1020 | char cmd[1024]; |
1021 | ||
1022 | snprintf(cmd, sizeof(cmd), "rm -rf %s", smb_dir); | |
1023 | system(cmd); | |
63a01ef8 AL |
1024 | } |
1025 | ||
9f8bd042 JK |
1026 | static void slirp_smb(SlirpState* s, const char *exported_dir, |
1027 | struct in_addr vserver_addr) | |
63a01ef8 AL |
1028 | { |
1029 | char smb_conf[1024]; | |
1030 | char smb_cmdline[1024]; | |
1031 | FILE *f; | |
1032 | ||
63a01ef8 | 1033 | /* XXX: better tmp dir construction */ |
3f4cb3d3 | 1034 | snprintf(smb_dir, sizeof(smb_dir), "/tmp/qemu-smb.%ld", (long)getpid()); |
63a01ef8 AL |
1035 | if (mkdir(smb_dir, 0700) < 0) { |
1036 | fprintf(stderr, "qemu: could not create samba server dir '%s'\n", smb_dir); | |
1037 | exit(1); | |
1038 | } | |
1039 | snprintf(smb_conf, sizeof(smb_conf), "%s/%s", smb_dir, "smb.conf"); | |
1040 | ||
1041 | f = fopen(smb_conf, "w"); | |
1042 | if (!f) { | |
1043 | fprintf(stderr, "qemu: could not create samba server configuration file '%s'\n", smb_conf); | |
1044 | exit(1); | |
1045 | } | |
1046 | fprintf(f, | |
1047 | "[global]\n" | |
1048 | "private dir=%s\n" | |
1049 | "smb ports=0\n" | |
1050 | "socket address=127.0.0.1\n" | |
1051 | "pid directory=%s\n" | |
1052 | "lock directory=%s\n" | |
1053 | "log file=%s/log.smbd\n" | |
1054 | "smb passwd file=%s/smbpasswd\n" | |
1055 | "security = share\n" | |
1056 | "[qemu]\n" | |
1057 | "path=%s\n" | |
1058 | "read only=no\n" | |
1059 | "guest ok=yes\n", | |
1060 | smb_dir, | |
1061 | smb_dir, | |
1062 | smb_dir, | |
1063 | smb_dir, | |
1064 | smb_dir, | |
1065 | exported_dir | |
1066 | ); | |
1067 | fclose(f); | |
1068 | atexit(smb_exit); | |
1069 | ||
1070 | snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s", | |
1071 | SMBD_COMMAND, smb_conf); | |
1072 | ||
9f8bd042 | 1073 | if (slirp_add_exec(s->slirp, 0, smb_cmdline, vserver_addr, 139) < 0) { |
c92ef6a2 JK |
1074 | fprintf(stderr, "conflicting/invalid smbserver address\n"); |
1075 | exit(1); | |
1076 | } | |
63a01ef8 AL |
1077 | } |
1078 | ||
ad196a9d | 1079 | /* automatic user mode samba server configuration (legacy interface) */ |
b8e8af38 JK |
1080 | void net_slirp_smb(const char *exported_dir) |
1081 | { | |
c92ef6a2 JK |
1082 | struct in_addr vserver_addr = { .s_addr = 0 }; |
1083 | ||
ad196a9d | 1084 | if (legacy_smb_export) { |
b8e8af38 JK |
1085 | fprintf(stderr, "-smb given twice\n"); |
1086 | exit(1); | |
1087 | } | |
ad196a9d | 1088 | legacy_smb_export = exported_dir; |
b1c99fcd JK |
1089 | if (!TAILQ_EMPTY(&slirp_stacks)) { |
1090 | slirp_smb(TAILQ_FIRST(&slirp_stacks), exported_dir, vserver_addr); | |
b8e8af38 JK |
1091 | } |
1092 | } | |
1093 | ||
63a01ef8 | 1094 | #endif /* !defined(_WIN32) */ |
b8e8af38 | 1095 | |
c92ef6a2 | 1096 | struct GuestFwd { |
8ca9217d | 1097 | CharDriverState *hd; |
c92ef6a2 | 1098 | struct in_addr server; |
8ca9217d | 1099 | int port; |
9f8bd042 | 1100 | Slirp *slirp; |
511d2b14 | 1101 | }; |
8ca9217d | 1102 | |
c92ef6a2 | 1103 | static int guestfwd_can_read(void *opaque) |
8ca9217d | 1104 | { |
c92ef6a2 | 1105 | struct GuestFwd *fwd = opaque; |
9f8bd042 | 1106 | return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port); |
8ca9217d AL |
1107 | } |
1108 | ||
c92ef6a2 | 1109 | static void guestfwd_read(void *opaque, const uint8_t *buf, int size) |
8ca9217d | 1110 | { |
c92ef6a2 | 1111 | struct GuestFwd *fwd = opaque; |
9f8bd042 | 1112 | slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size); |
8ca9217d AL |
1113 | } |
1114 | ||
9f8bd042 | 1115 | static void slirp_guestfwd(SlirpState *s, Monitor *mon, const char *config_str, |
c92ef6a2 | 1116 | int legacy_format) |
ad196a9d | 1117 | { |
c92ef6a2 JK |
1118 | struct in_addr server = { .s_addr = 0 }; |
1119 | struct GuestFwd *fwd; | |
1120 | const char *p; | |
1121 | char buf[128]; | |
1122 | char *end; | |
ad196a9d JK |
1123 | int port; |
1124 | ||
c92ef6a2 JK |
1125 | p = config_str; |
1126 | if (legacy_format) { | |
1127 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
1128 | goto fail_syntax; | |
1129 | } | |
1130 | } else { | |
1131 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
1132 | goto fail_syntax; | |
1133 | } | |
1134 | if (strcmp(buf, "tcp") && buf[0] != '\0') { | |
1135 | goto fail_syntax; | |
1136 | } | |
1137 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
1138 | goto fail_syntax; | |
1139 | } | |
1140 | if (buf[0] != '\0' && !inet_aton(buf, &server)) { | |
1141 | goto fail_syntax; | |
1142 | } | |
1143 | if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) { | |
1144 | goto fail_syntax; | |
1145 | } | |
1146 | } | |
1147 | port = strtol(buf, &end, 10); | |
1148 | if (*end != '\0' || port < 1 || port > 65535) { | |
1149 | goto fail_syntax; | |
ad196a9d | 1150 | } |
ad196a9d | 1151 | |
c92ef6a2 JK |
1152 | fwd = qemu_malloc(sizeof(struct GuestFwd)); |
1153 | snprintf(buf, sizeof(buf), "guestfwd.tcp:%d", port); | |
1154 | fwd->hd = qemu_chr_open(buf, p, NULL); | |
1155 | if (!fwd->hd) { | |
1156 | config_error(mon, "could not open guest forwarding device '%s'\n", | |
1157 | buf); | |
1158 | qemu_free(fwd); | |
ad196a9d JK |
1159 | return; |
1160 | } | |
c92ef6a2 JK |
1161 | fwd->server = server; |
1162 | fwd->port = port; | |
9f8bd042 | 1163 | fwd->slirp = s->slirp; |
ad196a9d | 1164 | |
9f8bd042 | 1165 | if (slirp_add_exec(s->slirp, 3, fwd->hd, server, port) < 0) { |
c92ef6a2 JK |
1166 | config_error(mon, "conflicting/invalid host:port in guest forwarding " |
1167 | "rule '%s'\n", config_str); | |
1168 | qemu_free(fwd); | |
1169 | return; | |
1170 | } | |
1171 | qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read, | |
1172 | NULL, fwd); | |
ad196a9d | 1173 | return; |
c92ef6a2 JK |
1174 | |
1175 | fail_syntax: | |
1176 | config_error(mon, "invalid guest forwarding rule '%s'\n", config_str); | |
ad196a9d JK |
1177 | } |
1178 | ||
6dbe553f JK |
1179 | void do_info_usernet(Monitor *mon) |
1180 | { | |
b1c99fcd | 1181 | SlirpState *s; |
9f8bd042 | 1182 | |
b1c99fcd JK |
1183 | TAILQ_FOREACH(s, &slirp_stacks, entry) { |
1184 | monitor_printf(mon, "VLAN %d (%s):\n", s->vc->vlan->id, s->vc->name); | |
1185 | slirp_connection_info(s->slirp, mon); | |
9f8bd042 | 1186 | } |
6dbe553f JK |
1187 | } |
1188 | ||
63a01ef8 AL |
1189 | #endif /* CONFIG_SLIRP */ |
1190 | ||
1191 | #if !defined(_WIN32) | |
1192 | ||
1193 | typedef struct TAPState { | |
1194 | VLANClientState *vc; | |
1195 | int fd; | |
1196 | char down_script[1024]; | |
973cbd37 | 1197 | char down_script_arg[128]; |
5b01e886 | 1198 | uint8_t buf[4096]; |
b664e367 | 1199 | unsigned int read_poll : 1; |
1f7babf6 | 1200 | unsigned int write_poll : 1; |
63a01ef8 AL |
1201 | } TAPState; |
1202 | ||
b946a153 AL |
1203 | static int launch_script(const char *setup_script, const char *ifname, int fd); |
1204 | ||
b664e367 MM |
1205 | static int tap_can_send(void *opaque); |
1206 | static void tap_send(void *opaque); | |
1f7babf6 | 1207 | static void tap_writable(void *opaque); |
b664e367 MM |
1208 | |
1209 | static void tap_update_fd_handler(TAPState *s) | |
1210 | { | |
1211 | qemu_set_fd_handler2(s->fd, | |
1212 | s->read_poll ? tap_can_send : NULL, | |
1213 | s->read_poll ? tap_send : NULL, | |
1f7babf6 | 1214 | s->write_poll ? tap_writable : NULL, |
b664e367 MM |
1215 | s); |
1216 | } | |
1217 | ||
1218 | static void tap_read_poll(TAPState *s, int enable) | |
1219 | { | |
1220 | s->read_poll = !!enable; | |
1221 | tap_update_fd_handler(s); | |
1222 | } | |
1223 | ||
1f7babf6 MM |
1224 | static void tap_write_poll(TAPState *s, int enable) |
1225 | { | |
1226 | s->write_poll = !!enable; | |
1227 | tap_update_fd_handler(s); | |
1228 | } | |
1229 | ||
1230 | static void tap_writable(void *opaque) | |
1231 | { | |
1232 | TAPState *s = opaque; | |
1233 | ||
1234 | tap_write_poll(s, 0); | |
1235 | ||
1236 | qemu_flush_queued_packets(s->vc); | |
1237 | } | |
1238 | ||
e3f5ec2b | 1239 | static ssize_t tap_receive_iov(VLANClientState *vc, const struct iovec *iov, |
b535b7b2 AL |
1240 | int iovcnt) |
1241 | { | |
e3f5ec2b | 1242 | TAPState *s = vc->opaque; |
b535b7b2 AL |
1243 | ssize_t len; |
1244 | ||
1245 | do { | |
1246 | len = writev(s->fd, iov, iovcnt); | |
1f7babf6 MM |
1247 | } while (len == -1 && errno == EINTR); |
1248 | ||
1249 | if (len == -1 && errno == EAGAIN) { | |
1250 | tap_write_poll(s, 1); | |
1251 | return 0; | |
1252 | } | |
b535b7b2 AL |
1253 | |
1254 | return len; | |
1255 | } | |
b535b7b2 | 1256 | |
4f1c942b | 1257 | static ssize_t tap_receive(VLANClientState *vc, const uint8_t *buf, size_t size) |
63a01ef8 | 1258 | { |
e3f5ec2b | 1259 | TAPState *s = vc->opaque; |
4f1c942b MM |
1260 | ssize_t len; |
1261 | ||
1262 | do { | |
1263 | len = write(s->fd, buf, size); | |
1264 | } while (len == -1 && (errno == EINTR || errno == EAGAIN)); | |
1265 | ||
1266 | return len; | |
63a01ef8 AL |
1267 | } |
1268 | ||
3471b757 MM |
1269 | static int tap_can_send(void *opaque) |
1270 | { | |
1271 | TAPState *s = opaque; | |
1272 | ||
1273 | return qemu_can_send_packet(s->vc); | |
1274 | } | |
1275 | ||
63a01ef8 | 1276 | #ifdef __sun__ |
5a6d8815 MM |
1277 | static ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen) |
1278 | { | |
63a01ef8 AL |
1279 | struct strbuf sbuf; |
1280 | int f = 0; | |
5a6d8815 MM |
1281 | |
1282 | sbuf.maxlen = maxlen; | |
3f4cb3d3 | 1283 | sbuf.buf = (char *)buf; |
5a6d8815 MM |
1284 | |
1285 | return getmsg(tapfd, NULL, &sbuf, &f) >= 0 ? sbuf.len : -1; | |
1286 | } | |
63a01ef8 | 1287 | #else |
5a6d8815 MM |
1288 | static ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen) |
1289 | { | |
1290 | return read(tapfd, buf, maxlen); | |
1291 | } | |
63a01ef8 | 1292 | #endif |
5a6d8815 | 1293 | |
783527a9 | 1294 | static void tap_send_completed(VLANClientState *vc, ssize_t len) |
e19eb224 MM |
1295 | { |
1296 | TAPState *s = vc->opaque; | |
b664e367 | 1297 | tap_read_poll(s, 1); |
e19eb224 MM |
1298 | } |
1299 | ||
5a6d8815 MM |
1300 | static void tap_send(void *opaque) |
1301 | { | |
1302 | TAPState *s = opaque; | |
5a6d8815 MM |
1303 | int size; |
1304 | ||
e19eb224 MM |
1305 | do { |
1306 | size = tap_read_packet(s->fd, s->buf, sizeof(s->buf)); | |
1307 | if (size <= 0) { | |
1308 | break; | |
1309 | } | |
1310 | ||
1311 | size = qemu_send_packet_async(s->vc, s->buf, size, tap_send_completed); | |
1312 | if (size == 0) { | |
b664e367 | 1313 | tap_read_poll(s, 0); |
e19eb224 MM |
1314 | } |
1315 | } while (size > 0); | |
63a01ef8 AL |
1316 | } |
1317 | ||
0df0ff6d MM |
1318 | static void tap_set_sndbuf(TAPState *s, int sndbuf, Monitor *mon) |
1319 | { | |
1320 | #ifdef TUNSETSNDBUF | |
1321 | if (ioctl(s->fd, TUNSETSNDBUF, &sndbuf) == -1) { | |
1322 | config_error(mon, "TUNSETSNDBUF ioctl failed: %s\n", | |
1323 | strerror(errno)); | |
1324 | } | |
1325 | #else | |
1326 | config_error(mon, "No '-net tap,sndbuf=<nbytes>' support available\n"); | |
1327 | #endif | |
1328 | } | |
1329 | ||
b946a153 AL |
1330 | static void tap_cleanup(VLANClientState *vc) |
1331 | { | |
1332 | TAPState *s = vc->opaque; | |
1333 | ||
b9adce2c MM |
1334 | qemu_purge_queued_packets(vc); |
1335 | ||
b946a153 AL |
1336 | if (s->down_script[0]) |
1337 | launch_script(s->down_script, s->down_script_arg, s->fd); | |
1338 | ||
b664e367 | 1339 | tap_read_poll(s, 0); |
1f7babf6 | 1340 | tap_write_poll(s, 0); |
b946a153 AL |
1341 | close(s->fd); |
1342 | qemu_free(s); | |
1343 | } | |
1344 | ||
63a01ef8 AL |
1345 | /* fd support */ |
1346 | ||
7a9f6e4a AL |
1347 | static TAPState *net_tap_fd_init(VLANState *vlan, |
1348 | const char *model, | |
1349 | const char *name, | |
1350 | int fd) | |
63a01ef8 AL |
1351 | { |
1352 | TAPState *s; | |
1353 | ||
1354 | s = qemu_mallocz(sizeof(TAPState)); | |
63a01ef8 | 1355 | s->fd = fd; |
463af534 MM |
1356 | s->vc = qemu_new_vlan_client(vlan, model, name, NULL, tap_receive, |
1357 | tap_receive_iov, tap_cleanup, s); | |
b664e367 | 1358 | tap_read_poll(s, 1); |
7cb7434b | 1359 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd); |
63a01ef8 AL |
1360 | return s; |
1361 | } | |
1362 | ||
179a2c19 | 1363 | #if defined (HOST_BSD) || defined (__FreeBSD_kernel__) |
63a01ef8 AL |
1364 | static int tap_open(char *ifname, int ifname_size) |
1365 | { | |
1366 | int fd; | |
1367 | char *dev; | |
1368 | struct stat s; | |
1369 | ||
1370 | TFR(fd = open("/dev/tap", O_RDWR)); | |
1371 | if (fd < 0) { | |
1372 | fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n"); | |
1373 | return -1; | |
1374 | } | |
1375 | ||
1376 | fstat(fd, &s); | |
1377 | dev = devname(s.st_rdev, S_IFCHR); | |
1378 | pstrcpy(ifname, ifname_size, dev); | |
1379 | ||
1380 | fcntl(fd, F_SETFL, O_NONBLOCK); | |
1381 | return fd; | |
1382 | } | |
1383 | #elif defined(__sun__) | |
1384 | #define TUNNEWPPA (('T'<<16) | 0x0001) | |
1385 | /* | |
1386 | * Allocate TAP device, returns opened fd. | |
1387 | * Stores dev name in the first arg(must be large enough). | |
1388 | */ | |
3f4cb3d3 | 1389 | static int tap_alloc(char *dev, size_t dev_size) |
63a01ef8 AL |
1390 | { |
1391 | int tap_fd, if_fd, ppa = -1; | |
1392 | static int ip_fd = 0; | |
1393 | char *ptr; | |
1394 | ||
1395 | static int arp_fd = 0; | |
1396 | int ip_muxid, arp_muxid; | |
1397 | struct strioctl strioc_if, strioc_ppa; | |
1398 | int link_type = I_PLINK;; | |
1399 | struct lifreq ifr; | |
1400 | char actual_name[32] = ""; | |
1401 | ||
1402 | memset(&ifr, 0x0, sizeof(ifr)); | |
1403 | ||
1404 | if( *dev ){ | |
1405 | ptr = dev; | |
47398b9c | 1406 | while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++; |
63a01ef8 AL |
1407 | ppa = atoi(ptr); |
1408 | } | |
1409 | ||
1410 | /* Check if IP device was opened */ | |
1411 | if( ip_fd ) | |
1412 | close(ip_fd); | |
1413 | ||
1414 | TFR(ip_fd = open("/dev/udp", O_RDWR, 0)); | |
1415 | if (ip_fd < 0) { | |
1416 | syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)"); | |
1417 | return -1; | |
1418 | } | |
1419 | ||
1420 | TFR(tap_fd = open("/dev/tap", O_RDWR, 0)); | |
1421 | if (tap_fd < 0) { | |
1422 | syslog(LOG_ERR, "Can't open /dev/tap"); | |
1423 | return -1; | |
1424 | } | |
1425 | ||
1426 | /* Assign a new PPA and get its unit number. */ | |
1427 | strioc_ppa.ic_cmd = TUNNEWPPA; | |
1428 | strioc_ppa.ic_timout = 0; | |
1429 | strioc_ppa.ic_len = sizeof(ppa); | |
1430 | strioc_ppa.ic_dp = (char *)&ppa; | |
1431 | if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0) | |
1432 | syslog (LOG_ERR, "Can't assign new interface"); | |
1433 | ||
1434 | TFR(if_fd = open("/dev/tap", O_RDWR, 0)); | |
1435 | if (if_fd < 0) { | |
1436 | syslog(LOG_ERR, "Can't open /dev/tap (2)"); | |
1437 | return -1; | |
1438 | } | |
1439 | if(ioctl(if_fd, I_PUSH, "ip") < 0){ | |
1440 | syslog(LOG_ERR, "Can't push IP module"); | |
1441 | return -1; | |
1442 | } | |
1443 | ||
1444 | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) | |
1445 | syslog(LOG_ERR, "Can't get flags\n"); | |
1446 | ||
1447 | snprintf (actual_name, 32, "tap%d", ppa); | |
1448 | pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name); | |
1449 | ||
1450 | ifr.lifr_ppa = ppa; | |
1451 | /* Assign ppa according to the unit number returned by tun device */ | |
1452 | ||
1453 | if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0) | |
1454 | syslog (LOG_ERR, "Can't set PPA %d", ppa); | |
1455 | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0) | |
1456 | syslog (LOG_ERR, "Can't get flags\n"); | |
1457 | /* Push arp module to if_fd */ | |
1458 | if (ioctl (if_fd, I_PUSH, "arp") < 0) | |
1459 | syslog (LOG_ERR, "Can't push ARP module (2)"); | |
1460 | ||
1461 | /* Push arp module to ip_fd */ | |
1462 | if (ioctl (ip_fd, I_POP, NULL) < 0) | |
1463 | syslog (LOG_ERR, "I_POP failed\n"); | |
1464 | if (ioctl (ip_fd, I_PUSH, "arp") < 0) | |
1465 | syslog (LOG_ERR, "Can't push ARP module (3)\n"); | |
1466 | /* Open arp_fd */ | |
1467 | TFR(arp_fd = open ("/dev/tap", O_RDWR, 0)); | |
1468 | if (arp_fd < 0) | |
1469 | syslog (LOG_ERR, "Can't open %s\n", "/dev/tap"); | |
1470 | ||
1471 | /* Set ifname to arp */ | |
1472 | strioc_if.ic_cmd = SIOCSLIFNAME; | |
1473 | strioc_if.ic_timout = 0; | |
1474 | strioc_if.ic_len = sizeof(ifr); | |
1475 | strioc_if.ic_dp = (char *)𝔦 | |
1476 | if (ioctl(arp_fd, I_STR, &strioc_if) < 0){ | |
1477 | syslog (LOG_ERR, "Can't set ifname to arp\n"); | |
1478 | } | |
1479 | ||
1480 | if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){ | |
1481 | syslog(LOG_ERR, "Can't link TAP device to IP"); | |
1482 | return -1; | |
1483 | } | |
1484 | ||
1485 | if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0) | |
1486 | syslog (LOG_ERR, "Can't link TAP device to ARP"); | |
1487 | ||
1488 | close (if_fd); | |
1489 | ||
1490 | memset(&ifr, 0x0, sizeof(ifr)); | |
1491 | pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name); | |
1492 | ifr.lifr_ip_muxid = ip_muxid; | |
1493 | ifr.lifr_arp_muxid = arp_muxid; | |
1494 | ||
1495 | if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0) | |
1496 | { | |
1497 | ioctl (ip_fd, I_PUNLINK , arp_muxid); | |
1498 | ioctl (ip_fd, I_PUNLINK, ip_muxid); | |
1499 | syslog (LOG_ERR, "Can't set multiplexor id"); | |
1500 | } | |
1501 | ||
1502 | snprintf(dev, dev_size, "tap%d", ppa); | |
1503 | return tap_fd; | |
1504 | } | |
1505 | ||
1506 | static int tap_open(char *ifname, int ifname_size) | |
1507 | { | |
1508 | char dev[10]=""; | |
1509 | int fd; | |
1510 | if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){ | |
1511 | fprintf(stderr, "Cannot allocate TAP device\n"); | |
1512 | return -1; | |
1513 | } | |
1514 | pstrcpy(ifname, ifname_size, dev); | |
1515 | fcntl(fd, F_SETFL, O_NONBLOCK); | |
1516 | return fd; | |
1517 | } | |
b29fe3ed | 1518 | #elif defined (_AIX) |
1519 | static int tap_open(char *ifname, int ifname_size) | |
1520 | { | |
1521 | fprintf (stderr, "no tap on AIX\n"); | |
1522 | return -1; | |
1523 | } | |
63a01ef8 AL |
1524 | #else |
1525 | static int tap_open(char *ifname, int ifname_size) | |
1526 | { | |
1527 | struct ifreq ifr; | |
1528 | int fd, ret; | |
1529 | ||
1530 | TFR(fd = open("/dev/net/tun", O_RDWR)); | |
1531 | if (fd < 0) { | |
1532 | fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n"); | |
1533 | return -1; | |
1534 | } | |
1535 | memset(&ifr, 0, sizeof(ifr)); | |
1536 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; | |
1537 | if (ifname[0] != '\0') | |
1538 | pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname); | |
1539 | else | |
1540 | pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d"); | |
1541 | ret = ioctl(fd, TUNSETIFF, (void *) &ifr); | |
1542 | if (ret != 0) { | |
1543 | fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n"); | |
1544 | close(fd); | |
1545 | return -1; | |
1546 | } | |
1547 | pstrcpy(ifname, ifname_size, ifr.ifr_name); | |
1548 | fcntl(fd, F_SETFL, O_NONBLOCK); | |
1549 | return fd; | |
1550 | } | |
1551 | #endif | |
1552 | ||
1553 | static int launch_script(const char *setup_script, const char *ifname, int fd) | |
1554 | { | |
7c3370d4 | 1555 | sigset_t oldmask, mask; |
63a01ef8 AL |
1556 | int pid, status; |
1557 | char *args[3]; | |
1558 | char **parg; | |
1559 | ||
7c3370d4 JK |
1560 | sigemptyset(&mask); |
1561 | sigaddset(&mask, SIGCHLD); | |
1562 | sigprocmask(SIG_BLOCK, &mask, &oldmask); | |
1563 | ||
1564 | /* try to launch network script */ | |
1565 | pid = fork(); | |
1566 | if (pid == 0) { | |
1567 | int open_max = sysconf(_SC_OPEN_MAX), i; | |
1568 | ||
1569 | for (i = 0; i < open_max; i++) { | |
1570 | if (i != STDIN_FILENO && | |
1571 | i != STDOUT_FILENO && | |
1572 | i != STDERR_FILENO && | |
1573 | i != fd) { | |
1574 | close(i); | |
63a01ef8 AL |
1575 | } |
1576 | } | |
7c3370d4 JK |
1577 | parg = args; |
1578 | *parg++ = (char *)setup_script; | |
1579 | *parg++ = (char *)ifname; | |
1580 | *parg++ = NULL; | |
1581 | execv(setup_script, args); | |
1582 | _exit(1); | |
1583 | } else if (pid > 0) { | |
1584 | while (waitpid(pid, &status, 0) != pid) { | |
1585 | /* loop */ | |
1586 | } | |
1587 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | |
1588 | ||
1589 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { | |
1590 | return 0; | |
1591 | } | |
1592 | } | |
1593 | fprintf(stderr, "%s: could not launch network script\n", setup_script); | |
1594 | return -1; | |
63a01ef8 AL |
1595 | } |
1596 | ||
4a77b25e MM |
1597 | static TAPState *net_tap_init(VLANState *vlan, const char *model, |
1598 | const char *name, const char *ifname1, | |
1599 | const char *setup_script, const char *down_script) | |
63a01ef8 AL |
1600 | { |
1601 | TAPState *s; | |
1602 | int fd; | |
1603 | char ifname[128]; | |
1604 | ||
1605 | if (ifname1 != NULL) | |
1606 | pstrcpy(ifname, sizeof(ifname), ifname1); | |
1607 | else | |
1608 | ifname[0] = '\0'; | |
1609 | TFR(fd = tap_open(ifname, sizeof(ifname))); | |
1610 | if (fd < 0) | |
4a77b25e | 1611 | return NULL; |
63a01ef8 AL |
1612 | |
1613 | if (!setup_script || !strcmp(setup_script, "no")) | |
1614 | setup_script = ""; | |
4a77b25e MM |
1615 | if (setup_script[0] != '\0' && |
1616 | launch_script(setup_script, ifname, fd)) { | |
1617 | return NULL; | |
63a01ef8 | 1618 | } |
7a9f6e4a | 1619 | s = net_tap_fd_init(vlan, model, name, fd); |
63a01ef8 | 1620 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
7cb7434b AL |
1621 | "ifname=%s,script=%s,downscript=%s", |
1622 | ifname, setup_script, down_script); | |
973cbd37 | 1623 | if (down_script && strcmp(down_script, "no")) { |
63a01ef8 | 1624 | snprintf(s->down_script, sizeof(s->down_script), "%s", down_script); |
973cbd37 AL |
1625 | snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname); |
1626 | } | |
4a77b25e | 1627 | return s; |
63a01ef8 AL |
1628 | } |
1629 | ||
1630 | #endif /* !_WIN32 */ | |
1631 | ||
1632 | #if defined(CONFIG_VDE) | |
1633 | typedef struct VDEState { | |
1634 | VLANClientState *vc; | |
1635 | VDECONN *vde; | |
1636 | } VDEState; | |
1637 | ||
1638 | static void vde_to_qemu(void *opaque) | |
1639 | { | |
1640 | VDEState *s = opaque; | |
1641 | uint8_t buf[4096]; | |
1642 | int size; | |
1643 | ||
cc63bb0f | 1644 | size = vde_recv(s->vde, (char *)buf, sizeof(buf), 0); |
63a01ef8 AL |
1645 | if (size > 0) { |
1646 | qemu_send_packet(s->vc, buf, size); | |
1647 | } | |
1648 | } | |
1649 | ||
4f1c942b | 1650 | static ssize_t vde_receive(VLANClientState *vc, const uint8_t *buf, size_t size) |
63a01ef8 | 1651 | { |
e3f5ec2b | 1652 | VDEState *s = vc->opaque; |
068daedd | 1653 | ssize_t ret; |
4f1c942b MM |
1654 | |
1655 | do { | |
1656 | ret = vde_send(s->vde, (const char *)buf, size, 0); | |
1657 | } while (ret < 0 && errno == EINTR); | |
1658 | ||
1659 | return ret; | |
63a01ef8 AL |
1660 | } |
1661 | ||
b946a153 AL |
1662 | static void vde_cleanup(VLANClientState *vc) |
1663 | { | |
1664 | VDEState *s = vc->opaque; | |
1665 | qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL); | |
1666 | vde_close(s->vde); | |
1667 | qemu_free(s); | |
1668 | } | |
1669 | ||
7a9f6e4a AL |
1670 | static int net_vde_init(VLANState *vlan, const char *model, |
1671 | const char *name, const char *sock, | |
bf38c1a0 | 1672 | int port, const char *group, int mode) |
63a01ef8 AL |
1673 | { |
1674 | VDEState *s; | |
1675 | char *init_group = strlen(group) ? (char *)group : NULL; | |
1676 | char *init_sock = strlen(sock) ? (char *)sock : NULL; | |
1677 | ||
1678 | struct vde_open_args args = { | |
1679 | .port = port, | |
1680 | .group = init_group, | |
1681 | .mode = mode, | |
1682 | }; | |
1683 | ||
1684 | s = qemu_mallocz(sizeof(VDEState)); | |
cc63bb0f | 1685 | s->vde = vde_open(init_sock, (char *)"QEMU", &args); |
63a01ef8 AL |
1686 | if (!s->vde){ |
1687 | free(s); | |
1688 | return -1; | |
1689 | } | |
e3f5ec2b | 1690 | s->vc = qemu_new_vlan_client(vlan, model, name, NULL, vde_receive, |
b946a153 | 1691 | NULL, vde_cleanup, s); |
63a01ef8 | 1692 | qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s); |
7cb7434b | 1693 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d", |
63a01ef8 AL |
1694 | sock, vde_datafd(s->vde)); |
1695 | return 0; | |
1696 | } | |
1697 | #endif | |
1698 | ||
1699 | /* network connection */ | |
1700 | typedef struct NetSocketState { | |
1701 | VLANClientState *vc; | |
1702 | int fd; | |
1703 | int state; /* 0 = getting length, 1 = getting data */ | |
abcd2baa AL |
1704 | unsigned int index; |
1705 | unsigned int packet_len; | |
63a01ef8 AL |
1706 | uint8_t buf[4096]; |
1707 | struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ | |
1708 | } NetSocketState; | |
1709 | ||
1710 | typedef struct NetSocketListenState { | |
1711 | VLANState *vlan; | |
bf38c1a0 | 1712 | char *model; |
7a9f6e4a | 1713 | char *name; |
63a01ef8 AL |
1714 | int fd; |
1715 | } NetSocketListenState; | |
1716 | ||
1717 | /* XXX: we consider we can send the whole packet without blocking */ | |
4f1c942b | 1718 | static ssize_t net_socket_receive(VLANClientState *vc, const uint8_t *buf, size_t size) |
63a01ef8 | 1719 | { |
e3f5ec2b | 1720 | NetSocketState *s = vc->opaque; |
63a01ef8 AL |
1721 | uint32_t len; |
1722 | len = htonl(size); | |
1723 | ||
1724 | send_all(s->fd, (const uint8_t *)&len, sizeof(len)); | |
4f1c942b | 1725 | return send_all(s->fd, buf, size); |
63a01ef8 AL |
1726 | } |
1727 | ||
4f1c942b | 1728 | static ssize_t net_socket_receive_dgram(VLANClientState *vc, const uint8_t *buf, size_t size) |
63a01ef8 | 1729 | { |
e3f5ec2b | 1730 | NetSocketState *s = vc->opaque; |
4f1c942b | 1731 | |
70503264 | 1732 | return sendto(s->fd, (const void *)buf, size, 0, |
4f1c942b | 1733 | (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst)); |
63a01ef8 AL |
1734 | } |
1735 | ||
1736 | static void net_socket_send(void *opaque) | |
1737 | { | |
1738 | NetSocketState *s = opaque; | |
abcd2baa AL |
1739 | int size, err; |
1740 | unsigned l; | |
63a01ef8 AL |
1741 | uint8_t buf1[4096]; |
1742 | const uint8_t *buf; | |
1743 | ||
c5b76b38 | 1744 | size = recv(s->fd, (void *)buf1, sizeof(buf1), 0); |
63a01ef8 AL |
1745 | if (size < 0) { |
1746 | err = socket_error(); | |
1747 | if (err != EWOULDBLOCK) | |
1748 | goto eoc; | |
1749 | } else if (size == 0) { | |
1750 | /* end of connection */ | |
1751 | eoc: | |
1752 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); | |
1753 | closesocket(s->fd); | |
1754 | return; | |
1755 | } | |
1756 | buf = buf1; | |
1757 | while (size > 0) { | |
1758 | /* reassemble a packet from the network */ | |
1759 | switch(s->state) { | |
1760 | case 0: | |
1761 | l = 4 - s->index; | |
1762 | if (l > size) | |
1763 | l = size; | |
1764 | memcpy(s->buf + s->index, buf, l); | |
1765 | buf += l; | |
1766 | size -= l; | |
1767 | s->index += l; | |
1768 | if (s->index == 4) { | |
1769 | /* got length */ | |
1770 | s->packet_len = ntohl(*(uint32_t *)s->buf); | |
1771 | s->index = 0; | |
1772 | s->state = 1; | |
1773 | } | |
1774 | break; | |
1775 | case 1: | |
1776 | l = s->packet_len - s->index; | |
1777 | if (l > size) | |
1778 | l = size; | |
abcd2baa AL |
1779 | if (s->index + l <= sizeof(s->buf)) { |
1780 | memcpy(s->buf + s->index, buf, l); | |
1781 | } else { | |
1782 | fprintf(stderr, "serious error: oversized packet received," | |
1783 | "connection terminated.\n"); | |
1784 | s->state = 0; | |
1785 | goto eoc; | |
1786 | } | |
1787 | ||
63a01ef8 AL |
1788 | s->index += l; |
1789 | buf += l; | |
1790 | size -= l; | |
1791 | if (s->index >= s->packet_len) { | |
1792 | qemu_send_packet(s->vc, s->buf, s->packet_len); | |
1793 | s->index = 0; | |
1794 | s->state = 0; | |
1795 | } | |
1796 | break; | |
1797 | } | |
1798 | } | |
1799 | } | |
1800 | ||
1801 | static void net_socket_send_dgram(void *opaque) | |
1802 | { | |
1803 | NetSocketState *s = opaque; | |
1804 | int size; | |
1805 | ||
c5b76b38 | 1806 | size = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0); |
63a01ef8 AL |
1807 | if (size < 0) |
1808 | return; | |
1809 | if (size == 0) { | |
1810 | /* end of connection */ | |
1811 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); | |
1812 | return; | |
1813 | } | |
1814 | qemu_send_packet(s->vc, s->buf, size); | |
1815 | } | |
1816 | ||
1817 | static int net_socket_mcast_create(struct sockaddr_in *mcastaddr) | |
1818 | { | |
1819 | struct ip_mreq imr; | |
1820 | int fd; | |
1821 | int val, ret; | |
1822 | if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) { | |
1823 | fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n", | |
1824 | inet_ntoa(mcastaddr->sin_addr), | |
1825 | (int)ntohl(mcastaddr->sin_addr.s_addr)); | |
1826 | return -1; | |
1827 | ||
1828 | } | |
1829 | fd = socket(PF_INET, SOCK_DGRAM, 0); | |
1830 | if (fd < 0) { | |
1831 | perror("socket(PF_INET, SOCK_DGRAM)"); | |
1832 | return -1; | |
1833 | } | |
1834 | ||
1835 | val = 1; | |
1836 | ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, | |
1837 | (const char *)&val, sizeof(val)); | |
1838 | if (ret < 0) { | |
1839 | perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); | |
1840 | goto fail; | |
1841 | } | |
1842 | ||
1843 | ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); | |
1844 | if (ret < 0) { | |
1845 | perror("bind"); | |
1846 | goto fail; | |
1847 | } | |
1848 | ||
1849 | /* Add host to multicast group */ | |
1850 | imr.imr_multiaddr = mcastaddr->sin_addr; | |
1851 | imr.imr_interface.s_addr = htonl(INADDR_ANY); | |
1852 | ||
1853 | ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, | |
1854 | (const char *)&imr, sizeof(struct ip_mreq)); | |
1855 | if (ret < 0) { | |
1856 | perror("setsockopt(IP_ADD_MEMBERSHIP)"); | |
1857 | goto fail; | |
1858 | } | |
1859 | ||
1860 | /* Force mcast msgs to loopback (eg. several QEMUs in same host */ | |
1861 | val = 1; | |
1862 | ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, | |
1863 | (const char *)&val, sizeof(val)); | |
1864 | if (ret < 0) { | |
1865 | perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)"); | |
1866 | goto fail; | |
1867 | } | |
1868 | ||
1869 | socket_set_nonblock(fd); | |
1870 | return fd; | |
1871 | fail: | |
1872 | if (fd >= 0) | |
1873 | closesocket(fd); | |
1874 | return -1; | |
1875 | } | |
1876 | ||
b946a153 AL |
1877 | static void net_socket_cleanup(VLANClientState *vc) |
1878 | { | |
1879 | NetSocketState *s = vc->opaque; | |
1880 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); | |
1881 | close(s->fd); | |
1882 | qemu_free(s); | |
1883 | } | |
1884 | ||
7a9f6e4a AL |
1885 | static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, |
1886 | const char *model, | |
1887 | const char *name, | |
bf38c1a0 | 1888 | int fd, int is_connected) |
63a01ef8 AL |
1889 | { |
1890 | struct sockaddr_in saddr; | |
1891 | int newfd; | |
1892 | socklen_t saddr_len; | |
1893 | NetSocketState *s; | |
1894 | ||
1895 | /* fd passed: multicast: "learn" dgram_dst address from bound address and save it | |
1896 | * Because this may be "shared" socket from a "master" process, datagrams would be recv() | |
1897 | * by ONLY ONE process: we must "clone" this dgram socket --jjo | |
1898 | */ | |
1899 | ||
1900 | if (is_connected) { | |
1901 | if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { | |
1902 | /* must be bound */ | |
1903 | if (saddr.sin_addr.s_addr==0) { | |
1904 | fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n", | |
1905 | fd); | |
1906 | return NULL; | |
1907 | } | |
1908 | /* clone dgram socket */ | |
1909 | newfd = net_socket_mcast_create(&saddr); | |
1910 | if (newfd < 0) { | |
1911 | /* error already reported by net_socket_mcast_create() */ | |
1912 | close(fd); | |
1913 | return NULL; | |
1914 | } | |
1915 | /* clone newfd to fd, close newfd */ | |
1916 | dup2(newfd, fd); | |
1917 | close(newfd); | |
1918 | ||
1919 | } else { | |
1920 | fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n", | |
1921 | fd, strerror(errno)); | |
1922 | return NULL; | |
1923 | } | |
1924 | } | |
1925 | ||
1926 | s = qemu_mallocz(sizeof(NetSocketState)); | |
63a01ef8 AL |
1927 | s->fd = fd; |
1928 | ||
463af534 | 1929 | s->vc = qemu_new_vlan_client(vlan, model, name, NULL, net_socket_receive_dgram, |
b946a153 | 1930 | NULL, net_socket_cleanup, s); |
63a01ef8 AL |
1931 | qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s); |
1932 | ||
1933 | /* mcast: save bound address as dst */ | |
1934 | if (is_connected) s->dgram_dst=saddr; | |
1935 | ||
1936 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), | |
1937 | "socket: fd=%d (%s mcast=%s:%d)", | |
1938 | fd, is_connected? "cloned" : "", | |
1939 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); | |
1940 | return s; | |
1941 | } | |
1942 | ||
1943 | static void net_socket_connect(void *opaque) | |
1944 | { | |
1945 | NetSocketState *s = opaque; | |
1946 | qemu_set_fd_handler(s->fd, net_socket_send, NULL, s); | |
1947 | } | |
1948 | ||
7a9f6e4a AL |
1949 | static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, |
1950 | const char *model, | |
1951 | const char *name, | |
bf38c1a0 | 1952 | int fd, int is_connected) |
63a01ef8 AL |
1953 | { |
1954 | NetSocketState *s; | |
1955 | s = qemu_mallocz(sizeof(NetSocketState)); | |
63a01ef8 | 1956 | s->fd = fd; |
463af534 | 1957 | s->vc = qemu_new_vlan_client(vlan, model, name, NULL, net_socket_receive, |
b946a153 | 1958 | NULL, net_socket_cleanup, s); |
63a01ef8 AL |
1959 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), |
1960 | "socket: fd=%d", fd); | |
1961 | if (is_connected) { | |
1962 | net_socket_connect(s); | |
1963 | } else { | |
1964 | qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s); | |
1965 | } | |
1966 | return s; | |
1967 | } | |
1968 | ||
7a9f6e4a AL |
1969 | static NetSocketState *net_socket_fd_init(VLANState *vlan, |
1970 | const char *model, const char *name, | |
bf38c1a0 | 1971 | int fd, int is_connected) |
63a01ef8 AL |
1972 | { |
1973 | int so_type=-1, optlen=sizeof(so_type); | |
1974 | ||
1975 | if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, | |
1976 | (socklen_t *)&optlen)< 0) { | |
1977 | fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd); | |
1978 | return NULL; | |
1979 | } | |
1980 | switch(so_type) { | |
1981 | case SOCK_DGRAM: | |
7a9f6e4a | 1982 | return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected); |
63a01ef8 | 1983 | case SOCK_STREAM: |
7a9f6e4a | 1984 | return net_socket_fd_init_stream(vlan, model, name, fd, is_connected); |
63a01ef8 AL |
1985 | default: |
1986 | /* who knows ... this could be a eg. a pty, do warn and continue as stream */ | |
1987 | fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd); | |
7a9f6e4a | 1988 | return net_socket_fd_init_stream(vlan, model, name, fd, is_connected); |
63a01ef8 AL |
1989 | } |
1990 | return NULL; | |
1991 | } | |
1992 | ||
1993 | static void net_socket_accept(void *opaque) | |
1994 | { | |
1995 | NetSocketListenState *s = opaque; | |
1996 | NetSocketState *s1; | |
1997 | struct sockaddr_in saddr; | |
1998 | socklen_t len; | |
1999 | int fd; | |
2000 | ||
2001 | for(;;) { | |
2002 | len = sizeof(saddr); | |
2003 | fd = accept(s->fd, (struct sockaddr *)&saddr, &len); | |
2004 | if (fd < 0 && errno != EINTR) { | |
2005 | return; | |
2006 | } else if (fd >= 0) { | |
2007 | break; | |
2008 | } | |
2009 | } | |
7a9f6e4a | 2010 | s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1); |
63a01ef8 AL |
2011 | if (!s1) { |
2012 | closesocket(fd); | |
2013 | } else { | |
2014 | snprintf(s1->vc->info_str, sizeof(s1->vc->info_str), | |
2015 | "socket: connection from %s:%d", | |
2016 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); | |
2017 | } | |
2018 | } | |
2019 | ||
7a9f6e4a AL |
2020 | static int net_socket_listen_init(VLANState *vlan, |
2021 | const char *model, | |
2022 | const char *name, | |
bf38c1a0 | 2023 | const char *host_str) |
63a01ef8 AL |
2024 | { |
2025 | NetSocketListenState *s; | |
2026 | int fd, val, ret; | |
2027 | struct sockaddr_in saddr; | |
2028 | ||
2029 | if (parse_host_port(&saddr, host_str) < 0) | |
2030 | return -1; | |
2031 | ||
2032 | s = qemu_mallocz(sizeof(NetSocketListenState)); | |
63a01ef8 AL |
2033 | |
2034 | fd = socket(PF_INET, SOCK_STREAM, 0); | |
2035 | if (fd < 0) { | |
2036 | perror("socket"); | |
2037 | return -1; | |
2038 | } | |
2039 | socket_set_nonblock(fd); | |
2040 | ||
2041 | /* allow fast reuse */ | |
2042 | val = 1; | |
2043 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); | |
2044 | ||
2045 | ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); | |
2046 | if (ret < 0) { | |
2047 | perror("bind"); | |
2048 | return -1; | |
2049 | } | |
2050 | ret = listen(fd, 0); | |
2051 | if (ret < 0) { | |
2052 | perror("listen"); | |
2053 | return -1; | |
2054 | } | |
2055 | s->vlan = vlan; | |
bf38c1a0 | 2056 | s->model = strdup(model); |
ea053add | 2057 | s->name = name ? strdup(name) : NULL; |
63a01ef8 AL |
2058 | s->fd = fd; |
2059 | qemu_set_fd_handler(fd, net_socket_accept, NULL, s); | |
2060 | return 0; | |
2061 | } | |
2062 | ||
7a9f6e4a AL |
2063 | static int net_socket_connect_init(VLANState *vlan, |
2064 | const char *model, | |
2065 | const char *name, | |
bf38c1a0 | 2066 | const char *host_str) |
63a01ef8 AL |
2067 | { |
2068 | NetSocketState *s; | |
2069 | int fd, connected, ret, err; | |
2070 | struct sockaddr_in saddr; | |
2071 | ||
2072 | if (parse_host_port(&saddr, host_str) < 0) | |
2073 | return -1; | |
2074 | ||
2075 | fd = socket(PF_INET, SOCK_STREAM, 0); | |
2076 | if (fd < 0) { | |
2077 | perror("socket"); | |
2078 | return -1; | |
2079 | } | |
2080 | socket_set_nonblock(fd); | |
2081 | ||
2082 | connected = 0; | |
2083 | for(;;) { | |
2084 | ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); | |
2085 | if (ret < 0) { | |
2086 | err = socket_error(); | |
2087 | if (err == EINTR || err == EWOULDBLOCK) { | |
2088 | } else if (err == EINPROGRESS) { | |
2089 | break; | |
2090 | #ifdef _WIN32 | |
2091 | } else if (err == WSAEALREADY) { | |
2092 | break; | |
2093 | #endif | |
2094 | } else { | |
2095 | perror("connect"); | |
2096 | closesocket(fd); | |
2097 | return -1; | |
2098 | } | |
2099 | } else { | |
2100 | connected = 1; | |
2101 | break; | |
2102 | } | |
2103 | } | |
7a9f6e4a | 2104 | s = net_socket_fd_init(vlan, model, name, fd, connected); |
63a01ef8 AL |
2105 | if (!s) |
2106 | return -1; | |
2107 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), | |
2108 | "socket: connect to %s:%d", | |
2109 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); | |
2110 | return 0; | |
2111 | } | |
2112 | ||
7a9f6e4a AL |
2113 | static int net_socket_mcast_init(VLANState *vlan, |
2114 | const char *model, | |
2115 | const char *name, | |
bf38c1a0 | 2116 | const char *host_str) |
63a01ef8 AL |
2117 | { |
2118 | NetSocketState *s; | |
2119 | int fd; | |
2120 | struct sockaddr_in saddr; | |
2121 | ||
2122 | if (parse_host_port(&saddr, host_str) < 0) | |
2123 | return -1; | |
2124 | ||
2125 | ||
2126 | fd = net_socket_mcast_create(&saddr); | |
2127 | if (fd < 0) | |
2128 | return -1; | |
2129 | ||
7a9f6e4a | 2130 | s = net_socket_fd_init(vlan, model, name, fd, 0); |
63a01ef8 AL |
2131 | if (!s) |
2132 | return -1; | |
2133 | ||
2134 | s->dgram_dst = saddr; | |
2135 | ||
2136 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), | |
2137 | "socket: mcast=%s:%d", | |
2138 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); | |
2139 | return 0; | |
2140 | ||
2141 | } | |
2142 | ||
bb9ea79e AL |
2143 | typedef struct DumpState { |
2144 | VLANClientState *pcap_vc; | |
2145 | int fd; | |
2146 | int pcap_caplen; | |
2147 | } DumpState; | |
2148 | ||
2149 | #define PCAP_MAGIC 0xa1b2c3d4 | |
2150 | ||
2151 | struct pcap_file_hdr { | |
2152 | uint32_t magic; | |
2153 | uint16_t version_major; | |
2154 | uint16_t version_minor; | |
2155 | int32_t thiszone; | |
2156 | uint32_t sigfigs; | |
2157 | uint32_t snaplen; | |
2158 | uint32_t linktype; | |
2159 | }; | |
2160 | ||
2161 | struct pcap_sf_pkthdr { | |
2162 | struct { | |
2163 | int32_t tv_sec; | |
2164 | int32_t tv_usec; | |
2165 | } ts; | |
2166 | uint32_t caplen; | |
2167 | uint32_t len; | |
2168 | }; | |
2169 | ||
4f1c942b | 2170 | static ssize_t dump_receive(VLANClientState *vc, const uint8_t *buf, size_t size) |
bb9ea79e | 2171 | { |
e3f5ec2b | 2172 | DumpState *s = vc->opaque; |
bb9ea79e AL |
2173 | struct pcap_sf_pkthdr hdr; |
2174 | int64_t ts; | |
2175 | int caplen; | |
2176 | ||
2177 | /* Early return in case of previous error. */ | |
2178 | if (s->fd < 0) { | |
4f1c942b | 2179 | return size; |
bb9ea79e AL |
2180 | } |
2181 | ||
37cb6fc3 | 2182 | ts = muldiv64(qemu_get_clock(vm_clock), 1000000, ticks_per_sec); |
bb9ea79e AL |
2183 | caplen = size > s->pcap_caplen ? s->pcap_caplen : size; |
2184 | ||
37cb6fc3 | 2185 | hdr.ts.tv_sec = ts / 1000000; |
bb9ea79e AL |
2186 | hdr.ts.tv_usec = ts % 1000000; |
2187 | hdr.caplen = caplen; | |
2188 | hdr.len = size; | |
2189 | if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) || | |
2190 | write(s->fd, buf, caplen) != caplen) { | |
2191 | qemu_log("-net dump write error - stop dump\n"); | |
2192 | close(s->fd); | |
2193 | s->fd = -1; | |
2194 | } | |
4f1c942b MM |
2195 | |
2196 | return size; | |
bb9ea79e AL |
2197 | } |
2198 | ||
2199 | static void net_dump_cleanup(VLANClientState *vc) | |
2200 | { | |
2201 | DumpState *s = vc->opaque; | |
2202 | ||
2203 | close(s->fd); | |
2204 | qemu_free(s); | |
2205 | } | |
2206 | ||
10ae5a7a | 2207 | static int net_dump_init(Monitor *mon, VLANState *vlan, const char *device, |
bb9ea79e AL |
2208 | const char *name, const char *filename, int len) |
2209 | { | |
2210 | struct pcap_file_hdr hdr; | |
2211 | DumpState *s; | |
2212 | ||
2213 | s = qemu_malloc(sizeof(DumpState)); | |
2214 | ||
024431b3 | 2215 | s->fd = open(filename, O_CREAT | O_WRONLY | O_BINARY, 0644); |
bb9ea79e | 2216 | if (s->fd < 0) { |
10ae5a7a | 2217 | config_error(mon, "-net dump: can't open %s\n", filename); |
bb9ea79e AL |
2218 | return -1; |
2219 | } | |
2220 | ||
2221 | s->pcap_caplen = len; | |
2222 | ||
2223 | hdr.magic = PCAP_MAGIC; | |
2224 | hdr.version_major = 2; | |
2225 | hdr.version_minor = 4; | |
2226 | hdr.thiszone = 0; | |
2227 | hdr.sigfigs = 0; | |
2228 | hdr.snaplen = s->pcap_caplen; | |
2229 | hdr.linktype = 1; | |
2230 | ||
2231 | if (write(s->fd, &hdr, sizeof(hdr)) < sizeof(hdr)) { | |
10ae5a7a | 2232 | config_error(mon, "-net dump write error: %s\n", strerror(errno)); |
bb9ea79e AL |
2233 | close(s->fd); |
2234 | qemu_free(s); | |
2235 | return -1; | |
2236 | } | |
2237 | ||
463af534 | 2238 | s->pcap_vc = qemu_new_vlan_client(vlan, device, name, NULL, dump_receive, NULL, |
bb9ea79e AL |
2239 | net_dump_cleanup, s); |
2240 | snprintf(s->pcap_vc->info_str, sizeof(s->pcap_vc->info_str), | |
2241 | "dump to %s (len=%d)", filename, len); | |
2242 | return 0; | |
2243 | } | |
2244 | ||
63a01ef8 AL |
2245 | /* find or alloc a new VLAN */ |
2246 | VLANState *qemu_find_vlan(int id) | |
2247 | { | |
2248 | VLANState **pvlan, *vlan; | |
2249 | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { | |
2250 | if (vlan->id == id) | |
2251 | return vlan; | |
2252 | } | |
2253 | vlan = qemu_mallocz(sizeof(VLANState)); | |
63a01ef8 AL |
2254 | vlan->id = id; |
2255 | vlan->next = NULL; | |
2256 | pvlan = &first_vlan; | |
2257 | while (*pvlan != NULL) | |
2258 | pvlan = &(*pvlan)->next; | |
2259 | *pvlan = vlan; | |
2260 | return vlan; | |
2261 | } | |
2262 | ||
7697079b AL |
2263 | static int nic_get_free_idx(void) |
2264 | { | |
2265 | int index; | |
2266 | ||
2267 | for (index = 0; index < MAX_NICS; index++) | |
2268 | if (!nd_table[index].used) | |
2269 | return index; | |
2270 | return -1; | |
2271 | } | |
2272 | ||
d07f22c5 AL |
2273 | void qemu_check_nic_model(NICInfo *nd, const char *model) |
2274 | { | |
2275 | const char *models[2]; | |
2276 | ||
2277 | models[0] = model; | |
2278 | models[1] = NULL; | |
2279 | ||
2280 | qemu_check_nic_model_list(nd, models, model); | |
2281 | } | |
2282 | ||
2283 | void qemu_check_nic_model_list(NICInfo *nd, const char * const *models, | |
2284 | const char *default_model) | |
2285 | { | |
2286 | int i, exit_status = 0; | |
2287 | ||
2288 | if (!nd->model) | |
2289 | nd->model = strdup(default_model); | |
2290 | ||
2291 | if (strcmp(nd->model, "?") != 0) { | |
2292 | for (i = 0 ; models[i]; i++) | |
2293 | if (strcmp(nd->model, models[i]) == 0) | |
2294 | return; | |
2295 | ||
2296 | fprintf(stderr, "qemu: Unsupported NIC model: %s\n", nd->model); | |
2297 | exit_status = 1; | |
2298 | } | |
2299 | ||
2300 | fprintf(stderr, "qemu: Supported NIC models: "); | |
2301 | for (i = 0 ; models[i]; i++) | |
2302 | fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n'); | |
2303 | ||
2304 | exit(exit_status); | |
2305 | } | |
2306 | ||
10ae5a7a | 2307 | int net_client_init(Monitor *mon, const char *device, const char *p) |
63a01ef8 AL |
2308 | { |
2309 | char buf[1024]; | |
2310 | int vlan_id, ret; | |
2311 | VLANState *vlan; | |
7a9f6e4a | 2312 | char *name = NULL; |
63a01ef8 AL |
2313 | |
2314 | vlan_id = 0; | |
2315 | if (get_param_value(buf, sizeof(buf), "vlan", p)) { | |
2316 | vlan_id = strtol(buf, NULL, 0); | |
2317 | } | |
2318 | vlan = qemu_find_vlan(vlan_id); | |
9036de1a | 2319 | |
7a9f6e4a | 2320 | if (get_param_value(buf, sizeof(buf), "name", p)) { |
10ae5a7a | 2321 | name = qemu_strdup(buf); |
7a9f6e4a | 2322 | } |
63a01ef8 | 2323 | if (!strcmp(device, "nic")) { |
8e4416af | 2324 | static const char * const nic_params[] = { |
ffe6370c | 2325 | "vlan", "name", "macaddr", "model", "addr", "vectors", NULL |
8e4416af | 2326 | }; |
63a01ef8 AL |
2327 | NICInfo *nd; |
2328 | uint8_t *macaddr; | |
7697079b | 2329 | int idx = nic_get_free_idx(); |
63a01ef8 | 2330 | |
0aa7a205 | 2331 | if (check_params(buf, sizeof(buf), nic_params, p) < 0) { |
10ae5a7a JK |
2332 | config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p); |
2333 | ret = -1; | |
2334 | goto out; | |
8e4416af | 2335 | } |
7697079b | 2336 | if (idx == -1 || nb_nics >= MAX_NICS) { |
10ae5a7a | 2337 | config_error(mon, "Too Many NICs\n"); |
771f1339 AL |
2338 | ret = -1; |
2339 | goto out; | |
63a01ef8 | 2340 | } |
7697079b | 2341 | nd = &nd_table[idx]; |
63a01ef8 AL |
2342 | macaddr = nd->macaddr; |
2343 | macaddr[0] = 0x52; | |
2344 | macaddr[1] = 0x54; | |
2345 | macaddr[2] = 0x00; | |
2346 | macaddr[3] = 0x12; | |
2347 | macaddr[4] = 0x34; | |
7697079b | 2348 | macaddr[5] = 0x56 + idx; |
63a01ef8 AL |
2349 | |
2350 | if (get_param_value(buf, sizeof(buf), "macaddr", p)) { | |
2351 | if (parse_macaddr(macaddr, buf) < 0) { | |
10ae5a7a | 2352 | config_error(mon, "invalid syntax for ethernet address\n"); |
771f1339 AL |
2353 | ret = -1; |
2354 | goto out; | |
63a01ef8 AL |
2355 | } |
2356 | } | |
2357 | if (get_param_value(buf, sizeof(buf), "model", p)) { | |
2358 | nd->model = strdup(buf); | |
2359 | } | |
5607c388 MA |
2360 | if (get_param_value(buf, sizeof(buf), "addr", p)) { |
2361 | nd->devaddr = strdup(buf); | |
2362 | } | |
ffe6370c MT |
2363 | nd->nvectors = NIC_NVECTORS_UNSPECIFIED; |
2364 | if (get_param_value(buf, sizeof(buf), "vectors", p)) { | |
2365 | char *endptr; | |
2366 | long vectors = strtol(buf, &endptr, 0); | |
2367 | if (*endptr) { | |
2368 | config_error(mon, "invalid syntax for # of vectors\n"); | |
2369 | ret = -1; | |
2370 | goto out; | |
2371 | } | |
2372 | if (vectors < 0 || vectors > 0x7ffffff) { | |
2373 | config_error(mon, "invalid # of vectors\n"); | |
2374 | ret = -1; | |
2375 | goto out; | |
2376 | } | |
2377 | nd->nvectors = vectors; | |
2378 | } | |
63a01ef8 | 2379 | nd->vlan = vlan; |
7a9f6e4a | 2380 | nd->name = name; |
7697079b | 2381 | nd->used = 1; |
7a9f6e4a | 2382 | name = NULL; |
63a01ef8 AL |
2383 | nb_nics++; |
2384 | vlan->nb_guest_devs++; | |
4d73cd3b | 2385 | ret = idx; |
63a01ef8 AL |
2386 | } else |
2387 | if (!strcmp(device, "none")) { | |
8e4416af | 2388 | if (*p != '\0') { |
10ae5a7a JK |
2389 | config_error(mon, "'none' takes no parameters\n"); |
2390 | ret = -1; | |
2391 | goto out; | |
8e4416af | 2392 | } |
63a01ef8 AL |
2393 | /* does nothing. It is needed to signal that no network cards |
2394 | are wanted */ | |
2395 | ret = 0; | |
2396 | } else | |
2397 | #ifdef CONFIG_SLIRP | |
2398 | if (!strcmp(device, "user")) { | |
8e4416af | 2399 | static const char * const slirp_params[] = { |
c92ef6a2 JK |
2400 | "vlan", "name", "hostname", "restrict", "ip", "net", "host", |
2401 | "tftp", "bootfile", "dhcpstart", "dns", "smb", "smbserver", | |
2402 | "hostfwd", "guestfwd", NULL | |
8e4416af | 2403 | }; |
ad196a9d | 2404 | struct slirp_config_str *config; |
c92ef6a2 JK |
2405 | int restricted = 0; |
2406 | char *vnet = NULL; | |
2407 | char *vhost = NULL; | |
2408 | char *vhostname = NULL; | |
ad196a9d JK |
2409 | char *tftp_export = NULL; |
2410 | char *bootfile = NULL; | |
c92ef6a2 JK |
2411 | char *vdhcp_start = NULL; |
2412 | char *vnamesrv = NULL; | |
ad196a9d | 2413 | char *smb_export = NULL; |
c92ef6a2 | 2414 | char *vsmbsrv = NULL; |
ad196a9d | 2415 | const char *q; |
b8e8af38 | 2416 | |
0aa7a205 | 2417 | if (check_params(buf, sizeof(buf), slirp_params, p) < 0) { |
10ae5a7a JK |
2418 | config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p); |
2419 | ret = -1; | |
2420 | goto out; | |
8e4416af | 2421 | } |
c92ef6a2 JK |
2422 | if (get_param_value(buf, sizeof(buf), "ip", p)) { |
2423 | /* emulate legacy parameter */ | |
2424 | vnet = qemu_malloc(strlen(buf) + strlen("/24") + 1); | |
2425 | strcpy(vnet, buf); | |
2426 | strcat(vnet, "/24"); | |
2427 | } | |
2428 | if (get_param_value(buf, sizeof(buf), "net", p)) { | |
2429 | vnet = qemu_strdup(buf); | |
2430 | } | |
2431 | if (get_param_value(buf, sizeof(buf), "host", p)) { | |
2432 | vhost = qemu_strdup(buf); | |
2433 | } | |
63a01ef8 | 2434 | if (get_param_value(buf, sizeof(buf), "hostname", p)) { |
c92ef6a2 | 2435 | vhostname = qemu_strdup(buf); |
63a01ef8 | 2436 | } |
49ec9b40 | 2437 | if (get_param_value(buf, sizeof(buf), "restrict", p)) { |
b8e8af38 | 2438 | restricted = (buf[0] == 'y') ? 1 : 0; |
49ec9b40 | 2439 | } |
c92ef6a2 JK |
2440 | if (get_param_value(buf, sizeof(buf), "dhcpstart", p)) { |
2441 | vdhcp_start = qemu_strdup(buf); | |
2442 | } | |
2443 | if (get_param_value(buf, sizeof(buf), "dns", p)) { | |
2444 | vnamesrv = qemu_strdup(buf); | |
49ec9b40 | 2445 | } |
ad196a9d JK |
2446 | if (get_param_value(buf, sizeof(buf), "tftp", p)) { |
2447 | tftp_export = qemu_strdup(buf); | |
2448 | } | |
2449 | if (get_param_value(buf, sizeof(buf), "bootfile", p)) { | |
2450 | bootfile = qemu_strdup(buf); | |
2451 | } | |
2452 | if (get_param_value(buf, sizeof(buf), "smb", p)) { | |
2453 | smb_export = qemu_strdup(buf); | |
c92ef6a2 JK |
2454 | if (get_param_value(buf, sizeof(buf), "smbserver", p)) { |
2455 | vsmbsrv = qemu_strdup(buf); | |
2456 | } | |
ad196a9d JK |
2457 | } |
2458 | q = p; | |
2459 | while (1) { | |
2460 | config = qemu_malloc(sizeof(*config)); | |
2461 | if (!get_next_param_value(config->str, sizeof(config->str), | |
c92ef6a2 | 2462 | "hostfwd", &q)) { |
ad196a9d JK |
2463 | break; |
2464 | } | |
c92ef6a2 | 2465 | config->flags = SLIRP_CFG_HOSTFWD; |
ad196a9d JK |
2466 | config->next = slirp_configs; |
2467 | slirp_configs = config; | |
2468 | config = NULL; | |
2469 | } | |
2470 | q = p; | |
2471 | while (1) { | |
2472 | config = qemu_malloc(sizeof(*config)); | |
2473 | if (!get_next_param_value(config->str, sizeof(config->str), | |
c92ef6a2 | 2474 | "guestfwd", &q)) { |
ad196a9d JK |
2475 | break; |
2476 | } | |
2477 | config->flags = 0; | |
2478 | config->next = slirp_configs; | |
2479 | slirp_configs = config; | |
2480 | config = NULL; | |
2481 | } | |
2482 | qemu_free(config); | |
63a01ef8 | 2483 | vlan->nb_host_devs++; |
c92ef6a2 JK |
2484 | ret = net_slirp_init(mon, vlan, device, name, restricted, vnet, vhost, |
2485 | vhostname, tftp_export, bootfile, vdhcp_start, | |
2486 | vnamesrv, smb_export, vsmbsrv); | |
2487 | qemu_free(vnet); | |
2488 | qemu_free(vhost); | |
2489 | qemu_free(vhostname); | |
ad196a9d JK |
2490 | qemu_free(tftp_export); |
2491 | qemu_free(bootfile); | |
c92ef6a2 JK |
2492 | qemu_free(vdhcp_start); |
2493 | qemu_free(vnamesrv); | |
ad196a9d | 2494 | qemu_free(smb_export); |
c92ef6a2 | 2495 | qemu_free(vsmbsrv); |
8ca9217d | 2496 | } else if (!strcmp(device, "channel")) { |
b1c99fcd | 2497 | if (TAILQ_EMPTY(&slirp_stacks)) { |
ad196a9d JK |
2498 | struct slirp_config_str *config; |
2499 | ||
2500 | config = qemu_malloc(sizeof(*config)); | |
2501 | pstrcpy(config->str, sizeof(config->str), p); | |
c92ef6a2 | 2502 | config->flags = SLIRP_CFG_LEGACY; |
ad196a9d JK |
2503 | config->next = slirp_configs; |
2504 | slirp_configs = config; | |
2505 | } else { | |
b1c99fcd | 2506 | slirp_guestfwd(TAILQ_FIRST(&slirp_stacks), mon, p, 1); |
8ca9217d | 2507 | } |
8ca9217d | 2508 | ret = 0; |
63a01ef8 AL |
2509 | } else |
2510 | #endif | |
2511 | #ifdef _WIN32 | |
2512 | if (!strcmp(device, "tap")) { | |
8e4416af AL |
2513 | static const char * const tap_params[] = { |
2514 | "vlan", "name", "ifname", NULL | |
2515 | }; | |
63a01ef8 | 2516 | char ifname[64]; |
8e4416af | 2517 | |
0aa7a205 | 2518 | if (check_params(buf, sizeof(buf), tap_params, p) < 0) { |
10ae5a7a JK |
2519 | config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p); |
2520 | ret = -1; | |
2521 | goto out; | |
8e4416af | 2522 | } |
63a01ef8 | 2523 | if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
10ae5a7a | 2524 | config_error(mon, "tap: no interface name\n"); |
771f1339 AL |
2525 | ret = -1; |
2526 | goto out; | |
63a01ef8 AL |
2527 | } |
2528 | vlan->nb_host_devs++; | |
7a9f6e4a | 2529 | ret = tap_win32_init(vlan, device, name, ifname); |
63a01ef8 | 2530 | } else |
b29fe3ed | 2531 | #elif defined (_AIX) |
63a01ef8 AL |
2532 | #else |
2533 | if (!strcmp(device, "tap")) { | |
0aa7a205 | 2534 | char ifname[64], chkbuf[64]; |
63a01ef8 | 2535 | char setup_script[1024], down_script[1024]; |
4a77b25e | 2536 | TAPState *s; |
63a01ef8 AL |
2537 | int fd; |
2538 | vlan->nb_host_devs++; | |
2539 | if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { | |
0df0ff6d MM |
2540 | static const char * const fd_params[] = { |
2541 | "vlan", "name", "fd", "sndbuf", NULL | |
2542 | }; | |
0aa7a205 | 2543 | if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) { |
10ae5a7a JK |
2544 | config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p); |
2545 | ret = -1; | |
2546 | goto out; | |
8e4416af | 2547 | } |
63a01ef8 AL |
2548 | fd = strtol(buf, NULL, 0); |
2549 | fcntl(fd, F_SETFL, O_NONBLOCK); | |
4a77b25e | 2550 | s = net_tap_fd_init(vlan, device, name, fd); |
63a01ef8 | 2551 | } else { |
8e4416af | 2552 | static const char * const tap_params[] = { |
0df0ff6d | 2553 | "vlan", "name", "ifname", "script", "downscript", "sndbuf", NULL |
8e4416af | 2554 | }; |
0aa7a205 | 2555 | if (check_params(chkbuf, sizeof(chkbuf), tap_params, p) < 0) { |
10ae5a7a JK |
2556 | config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p); |
2557 | ret = -1; | |
2558 | goto out; | |
8e4416af | 2559 | } |
63a01ef8 AL |
2560 | if (get_param_value(ifname, sizeof(ifname), "ifname", p) <= 0) { |
2561 | ifname[0] = '\0'; | |
2562 | } | |
2563 | if (get_param_value(setup_script, sizeof(setup_script), "script", p) == 0) { | |
2564 | pstrcpy(setup_script, sizeof(setup_script), DEFAULT_NETWORK_SCRIPT); | |
2565 | } | |
2566 | if (get_param_value(down_script, sizeof(down_script), "downscript", p) == 0) { | |
2567 | pstrcpy(down_script, sizeof(down_script), DEFAULT_NETWORK_DOWN_SCRIPT); | |
2568 | } | |
4a77b25e MM |
2569 | s = net_tap_init(vlan, device, name, ifname, setup_script, down_script); |
2570 | } | |
2571 | if (s != NULL) { | |
0df0ff6d MM |
2572 | if (get_param_value(buf, sizeof(buf), "sndbuf", p)) { |
2573 | tap_set_sndbuf(s, atoi(buf), mon); | |
2574 | } | |
4a77b25e MM |
2575 | ret = 0; |
2576 | } else { | |
2577 | ret = -1; | |
63a01ef8 AL |
2578 | } |
2579 | } else | |
2580 | #endif | |
2581 | if (!strcmp(device, "socket")) { | |
0aa7a205 | 2582 | char chkbuf[64]; |
63a01ef8 | 2583 | if (get_param_value(buf, sizeof(buf), "fd", p) > 0) { |
0df0ff6d MM |
2584 | static const char * const fd_params[] = { |
2585 | "vlan", "name", "fd", NULL | |
2586 | }; | |
63a01ef8 | 2587 | int fd; |
0aa7a205 | 2588 | if (check_params(chkbuf, sizeof(chkbuf), fd_params, p) < 0) { |
10ae5a7a JK |
2589 | config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p); |
2590 | ret = -1; | |
2591 | goto out; | |
8e4416af | 2592 | } |
63a01ef8 AL |
2593 | fd = strtol(buf, NULL, 0); |
2594 | ret = -1; | |
7a9f6e4a | 2595 | if (net_socket_fd_init(vlan, device, name, fd, 1)) |
63a01ef8 AL |
2596 | ret = 0; |
2597 | } else if (get_param_value(buf, sizeof(buf), "listen", p) > 0) { | |
8e4416af AL |
2598 | static const char * const listen_params[] = { |
2599 | "vlan", "name", "listen", NULL | |
2600 | }; | |
0aa7a205 | 2601 | if (check_params(chkbuf, sizeof(chkbuf), listen_params, p) < 0) { |
10ae5a7a JK |
2602 | config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p); |
2603 | ret = -1; | |
2604 | goto out; | |
8e4416af | 2605 | } |
7a9f6e4a | 2606 | ret = net_socket_listen_init(vlan, device, name, buf); |
63a01ef8 | 2607 | } else if (get_param_value(buf, sizeof(buf), "connect", p) > 0) { |
8e4416af AL |
2608 | static const char * const connect_params[] = { |
2609 | "vlan", "name", "connect", NULL | |
2610 | }; | |
0aa7a205 | 2611 | if (check_params(chkbuf, sizeof(chkbuf), connect_params, p) < 0) { |
10ae5a7a JK |
2612 | config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p); |
2613 | ret = -1; | |
2614 | goto out; | |
8e4416af | 2615 | } |
7a9f6e4a | 2616 | ret = net_socket_connect_init(vlan, device, name, buf); |
63a01ef8 | 2617 | } else if (get_param_value(buf, sizeof(buf), "mcast", p) > 0) { |
8e4416af AL |
2618 | static const char * const mcast_params[] = { |
2619 | "vlan", "name", "mcast", NULL | |
2620 | }; | |
0aa7a205 | 2621 | if (check_params(chkbuf, sizeof(chkbuf), mcast_params, p) < 0) { |
10ae5a7a JK |
2622 | config_error(mon, "invalid parameter '%s' in '%s'\n", chkbuf, p); |
2623 | ret = -1; | |
2624 | goto out; | |
8e4416af | 2625 | } |
7a9f6e4a | 2626 | ret = net_socket_mcast_init(vlan, device, name, buf); |
63a01ef8 | 2627 | } else { |
10ae5a7a | 2628 | config_error(mon, "Unknown socket options: %s\n", p); |
771f1339 AL |
2629 | ret = -1; |
2630 | goto out; | |
63a01ef8 AL |
2631 | } |
2632 | vlan->nb_host_devs++; | |
2633 | } else | |
2634 | #ifdef CONFIG_VDE | |
2635 | if (!strcmp(device, "vde")) { | |
8e4416af AL |
2636 | static const char * const vde_params[] = { |
2637 | "vlan", "name", "sock", "port", "group", "mode", NULL | |
2638 | }; | |
63a01ef8 AL |
2639 | char vde_sock[1024], vde_group[512]; |
2640 | int vde_port, vde_mode; | |
8e4416af | 2641 | |
0aa7a205 | 2642 | if (check_params(buf, sizeof(buf), vde_params, p) < 0) { |
10ae5a7a JK |
2643 | config_error(mon, "invalid parameter '%s' in '%s'\n", buf, p); |
2644 | ret = -1; | |
2645 | goto out; | |
8e4416af | 2646 | } |
63a01ef8 AL |
2647 | vlan->nb_host_devs++; |
2648 | if (get_param_value(vde_sock, sizeof(vde_sock), "sock", p) <= 0) { | |
2649 | vde_sock[0] = '\0'; | |
2650 | } | |
2651 | if (get_param_value(buf, sizeof(buf), "port", p) > 0) { | |
2652 | vde_port = strtol(buf, NULL, 10); | |
2653 | } else { | |
2654 | vde_port = 0; | |
2655 | } | |
2656 | if (get_param_value(vde_group, sizeof(vde_group), "group", p) <= 0) { | |
2657 | vde_group[0] = '\0'; | |
2658 | } | |
2659 | if (get_param_value(buf, sizeof(buf), "mode", p) > 0) { | |
2660 | vde_mode = strtol(buf, NULL, 8); | |
2661 | } else { | |
2662 | vde_mode = 0700; | |
2663 | } | |
7a9f6e4a | 2664 | ret = net_vde_init(vlan, device, name, vde_sock, vde_port, vde_group, vde_mode); |
63a01ef8 AL |
2665 | } else |
2666 | #endif | |
bb9ea79e AL |
2667 | if (!strcmp(device, "dump")) { |
2668 | int len = 65536; | |
2669 | ||
2670 | if (get_param_value(buf, sizeof(buf), "len", p) > 0) { | |
2671 | len = strtol(buf, NULL, 0); | |
2672 | } | |
2673 | if (!get_param_value(buf, sizeof(buf), "file", p)) { | |
2674 | snprintf(buf, sizeof(buf), "qemu-vlan%d.pcap", vlan_id); | |
2675 | } | |
10ae5a7a | 2676 | ret = net_dump_init(mon, vlan, device, name, buf, len); |
bb9ea79e | 2677 | } else { |
10ae5a7a | 2678 | config_error(mon, "Unknown network device: %s\n", device); |
771f1339 AL |
2679 | ret = -1; |
2680 | goto out; | |
63a01ef8 AL |
2681 | } |
2682 | if (ret < 0) { | |
10ae5a7a | 2683 | config_error(mon, "Could not initialize device '%s'\n", device); |
63a01ef8 | 2684 | } |
771f1339 | 2685 | out: |
10ae5a7a | 2686 | qemu_free(name); |
63a01ef8 AL |
2687 | return ret; |
2688 | } | |
2689 | ||
8b13c4a7 AL |
2690 | void net_client_uninit(NICInfo *nd) |
2691 | { | |
2692 | nd->vlan->nb_guest_devs--; | |
2693 | nb_nics--; | |
2694 | nd->used = 0; | |
2695 | free((void *)nd->model); | |
2696 | } | |
2697 | ||
6f338c34 AL |
2698 | static int net_host_check_device(const char *device) |
2699 | { | |
2700 | int i; | |
bb9ea79e | 2701 | const char *valid_param_list[] = { "tap", "socket", "dump" |
6f338c34 AL |
2702 | #ifdef CONFIG_SLIRP |
2703 | ,"user" | |
2704 | #endif | |
2705 | #ifdef CONFIG_VDE | |
2706 | ,"vde" | |
2707 | #endif | |
2708 | }; | |
2709 | for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) { | |
2710 | if (!strncmp(valid_param_list[i], device, | |
2711 | strlen(valid_param_list[i]))) | |
2712 | return 1; | |
2713 | } | |
2714 | ||
2715 | return 0; | |
2716 | } | |
2717 | ||
376253ec | 2718 | void net_host_device_add(Monitor *mon, const char *device, const char *opts) |
6f338c34 AL |
2719 | { |
2720 | if (!net_host_check_device(device)) { | |
376253ec | 2721 | monitor_printf(mon, "invalid host network device %s\n", device); |
6f338c34 AL |
2722 | return; |
2723 | } | |
10ae5a7a | 2724 | if (net_client_init(mon, device, opts ? opts : "") < 0) { |
5c8be678 AL |
2725 | monitor_printf(mon, "adding host network device %s failed\n", device); |
2726 | } | |
6f338c34 AL |
2727 | } |
2728 | ||
376253ec | 2729 | void net_host_device_remove(Monitor *mon, int vlan_id, const char *device) |
6f338c34 AL |
2730 | { |
2731 | VLANState *vlan; | |
2732 | VLANClientState *vc; | |
2733 | ||
6f338c34 | 2734 | vlan = qemu_find_vlan(vlan_id); |
6f338c34 | 2735 | |
e8f1f9db AL |
2736 | for (vc = vlan->first_client; vc != NULL; vc = vc->next) { |
2737 | if (!strcmp(vc->name, device)) { | |
6f338c34 | 2738 | break; |
e8f1f9db AL |
2739 | } |
2740 | } | |
6f338c34 AL |
2741 | |
2742 | if (!vc) { | |
376253ec | 2743 | monitor_printf(mon, "can't find device %s\n", device); |
6f338c34 AL |
2744 | return; |
2745 | } | |
e8f1f9db AL |
2746 | if (!net_host_check_device(vc->model)) { |
2747 | monitor_printf(mon, "invalid host network device %s\n", device); | |
2748 | return; | |
2749 | } | |
6f338c34 AL |
2750 | qemu_del_vlan_client(vc); |
2751 | } | |
2752 | ||
63a01ef8 AL |
2753 | int net_client_parse(const char *str) |
2754 | { | |
2755 | const char *p; | |
2756 | char *q; | |
2757 | char device[64]; | |
2758 | ||
2759 | p = str; | |
2760 | q = device; | |
2761 | while (*p != '\0' && *p != ',') { | |
2762 | if ((q - device) < sizeof(device) - 1) | |
2763 | *q++ = *p; | |
2764 | p++; | |
2765 | } | |
2766 | *q = '\0'; | |
2767 | if (*p == ',') | |
2768 | p++; | |
2769 | ||
10ae5a7a | 2770 | return net_client_init(NULL, device, p); |
63a01ef8 AL |
2771 | } |
2772 | ||
406c8df3 GC |
2773 | void net_set_boot_mask(int net_boot_mask) |
2774 | { | |
2775 | int i; | |
2776 | ||
2777 | /* Only the first four NICs may be bootable */ | |
2778 | net_boot_mask = net_boot_mask & 0xF; | |
2779 | ||
2780 | for (i = 0; i < nb_nics; i++) { | |
2781 | if (net_boot_mask & (1 << i)) { | |
2782 | nd_table[i].bootable = 1; | |
2783 | net_boot_mask &= ~(1 << i); | |
2784 | } | |
2785 | } | |
2786 | ||
2787 | if (net_boot_mask) { | |
2788 | fprintf(stderr, "Cannot boot from non-existent NIC\n"); | |
2789 | exit(1); | |
2790 | } | |
2791 | } | |
2792 | ||
376253ec | 2793 | void do_info_network(Monitor *mon) |
63a01ef8 AL |
2794 | { |
2795 | VLANState *vlan; | |
2796 | VLANClientState *vc; | |
2797 | ||
2798 | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { | |
376253ec | 2799 | monitor_printf(mon, "VLAN %d devices:\n", vlan->id); |
63a01ef8 | 2800 | for(vc = vlan->first_client; vc != NULL; vc = vc->next) |
376253ec | 2801 | monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str); |
63a01ef8 AL |
2802 | } |
2803 | } | |
2804 | ||
376253ec | 2805 | int do_set_link(Monitor *mon, const char *name, const char *up_or_down) |
436e5e53 AL |
2806 | { |
2807 | VLANState *vlan; | |
2808 | VLANClientState *vc = NULL; | |
2809 | ||
2810 | for (vlan = first_vlan; vlan != NULL; vlan = vlan->next) | |
2811 | for (vc = vlan->first_client; vc != NULL; vc = vc->next) | |
2812 | if (strcmp(vc->name, name) == 0) | |
dd5de373 EI |
2813 | goto done; |
2814 | done: | |
436e5e53 AL |
2815 | |
2816 | if (!vc) { | |
376253ec | 2817 | monitor_printf(mon, "could not find network device '%s'", name); |
436e5e53 AL |
2818 | return 0; |
2819 | } | |
2820 | ||
2821 | if (strcmp(up_or_down, "up") == 0) | |
2822 | vc->link_down = 0; | |
2823 | else if (strcmp(up_or_down, "down") == 0) | |
2824 | vc->link_down = 1; | |
2825 | else | |
376253ec AL |
2826 | monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' " |
2827 | "valid\n", up_or_down); | |
436e5e53 | 2828 | |
34b25ca7 AL |
2829 | if (vc->link_status_changed) |
2830 | vc->link_status_changed(vc); | |
2831 | ||
436e5e53 AL |
2832 | return 1; |
2833 | } | |
2834 | ||
63a01ef8 AL |
2835 | void net_cleanup(void) |
2836 | { | |
2837 | VLANState *vlan; | |
2838 | ||
63a01ef8 AL |
2839 | /* close network clients */ |
2840 | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { | |
b946a153 | 2841 | VLANClientState *vc = vlan->first_client; |
63a01ef8 | 2842 | |
b946a153 AL |
2843 | while (vc) { |
2844 | VLANClientState *next = vc->next; | |
63a01ef8 | 2845 | |
b946a153 AL |
2846 | qemu_del_vlan_client(vc); |
2847 | ||
2848 | vc = next; | |
63a01ef8 AL |
2849 | } |
2850 | } | |
63a01ef8 AL |
2851 | } |
2852 | ||
2853 | void net_client_check(void) | |
2854 | { | |
2855 | VLANState *vlan; | |
2856 | ||
2857 | for(vlan = first_vlan; vlan != NULL; vlan = vlan->next) { | |
2858 | if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0) | |
2859 | continue; | |
2860 | if (vlan->nb_guest_devs == 0) | |
2861 | fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id); | |
2862 | if (vlan->nb_host_devs == 0) | |
2863 | fprintf(stderr, | |
2864 | "Warning: vlan %d is not connected to host network\n", | |
2865 | vlan->id); | |
2866 | } | |
2867 | } |