]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * QEMU System Emulator | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | #include <unistd.h> | |
25 | #include <fcntl.h> | |
26 | #include <signal.h> | |
27 | #include <time.h> | |
28 | #include <errno.h> | |
29 | #include <sys/time.h> | |
30 | #include <zlib.h> | |
31 | ||
32 | /* Needed early for CONFIG_BSD etc. */ | |
33 | #include "config-host.h" | |
34 | ||
35 | #ifndef _WIN32 | |
36 | #include <sys/times.h> | |
37 | #include <sys/wait.h> | |
38 | #include <termios.h> | |
39 | #include <sys/mman.h> | |
40 | #include <sys/ioctl.h> | |
41 | #include <sys/resource.h> | |
42 | #include <sys/socket.h> | |
43 | #include <netinet/in.h> | |
44 | #include <net/if.h> | |
45 | #ifdef __NetBSD__ | |
46 | #include <net/if_tap.h> | |
47 | #endif | |
48 | #ifdef __linux__ | |
49 | #include <linux/if_tun.h> | |
50 | #endif | |
51 | #include <arpa/inet.h> | |
52 | #include <dirent.h> | |
53 | #include <netdb.h> | |
54 | #include <sys/select.h> | |
55 | #ifdef CONFIG_BSD | |
56 | #include <sys/stat.h> | |
57 | #if defined(__FreeBSD__) || defined(__DragonFly__) | |
58 | #include <libutil.h> | |
59 | #else | |
60 | #include <util.h> | |
61 | #endif | |
62 | #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__) | |
63 | #include <freebsd/stdlib.h> | |
64 | #else | |
65 | #ifdef __linux__ | |
66 | #include <pty.h> | |
67 | #include <malloc.h> | |
68 | #include <linux/rtc.h> | |
69 | ||
70 | /* For the benefit of older linux systems which don't supply it, | |
71 | we use a local copy of hpet.h. */ | |
72 | /* #include <linux/hpet.h> */ | |
73 | #include "hpet.h" | |
74 | ||
75 | #include <linux/ppdev.h> | |
76 | #include <linux/parport.h> | |
77 | #endif | |
78 | #ifdef __sun__ | |
79 | #include <sys/stat.h> | |
80 | #include <sys/ethernet.h> | |
81 | #include <sys/sockio.h> | |
82 | #include <netinet/arp.h> | |
83 | #include <netinet/in.h> | |
84 | #include <netinet/in_systm.h> | |
85 | #include <netinet/ip.h> | |
86 | #include <netinet/ip_icmp.h> // must come after ip.h | |
87 | #include <netinet/udp.h> | |
88 | #include <netinet/tcp.h> | |
89 | #include <net/if.h> | |
90 | #include <syslog.h> | |
91 | #include <stropts.h> | |
92 | #endif | |
93 | #endif | |
94 | #endif | |
95 | ||
96 | #if defined(__OpenBSD__) | |
97 | #include <util.h> | |
98 | #endif | |
99 | ||
100 | #if defined(CONFIG_VDE) | |
101 | #include <libvdeplug.h> | |
102 | #endif | |
103 | ||
104 | #include "qemu-common.h" | |
105 | #include "net.h" | |
106 | #include "monitor.h" | |
107 | #include "sysemu.h" | |
108 | #include "qemu-timer.h" | |
109 | #include "qemu-char.h" | |
110 | #include "audio/audio.h" | |
111 | #include "qemu_socket.h" | |
112 | #include "qemu-log.h" | |
113 | #include "qemu-config.h" | |
114 | ||
115 | #include "slirp/libslirp.h" | |
116 | ||
117 | static QTAILQ_HEAD(, VLANState) vlans; | |
118 | static QTAILQ_HEAD(, VLANClientState) non_vlan_clients; | |
119 | ||
120 | /***********************************************************/ | |
121 | /* network device redirectors */ | |
122 | ||
123 | #if defined(DEBUG_NET) || defined(DEBUG_SLIRP) | |
124 | static void hex_dump(FILE *f, const uint8_t *buf, int size) | |
125 | { | |
126 | int len, i, j, c; | |
127 | ||
128 | for(i=0;i<size;i+=16) { | |
129 | len = size - i; | |
130 | if (len > 16) | |
131 | len = 16; | |
132 | fprintf(f, "%08x ", i); | |
133 | for(j=0;j<16;j++) { | |
134 | if (j < len) | |
135 | fprintf(f, " %02x", buf[i+j]); | |
136 | else | |
137 | fprintf(f, " "); | |
138 | } | |
139 | fprintf(f, " "); | |
140 | for(j=0;j<len;j++) { | |
141 | c = buf[i+j]; | |
142 | if (c < ' ' || c > '~') | |
143 | c = '.'; | |
144 | fprintf(f, "%c", c); | |
145 | } | |
146 | fprintf(f, "\n"); | |
147 | } | |
148 | } | |
149 | #endif | |
150 | ||
151 | static int parse_macaddr(uint8_t *macaddr, const char *p) | |
152 | { | |
153 | int i; | |
154 | char *last_char; | |
155 | long int offset; | |
156 | ||
157 | errno = 0; | |
158 | offset = strtol(p, &last_char, 0); | |
159 | if (0 == errno && '\0' == *last_char && | |
160 | offset >= 0 && offset <= 0xFFFFFF) { | |
161 | macaddr[3] = (offset & 0xFF0000) >> 16; | |
162 | macaddr[4] = (offset & 0xFF00) >> 8; | |
163 | macaddr[5] = offset & 0xFF; | |
164 | return 0; | |
165 | } else { | |
166 | for(i = 0; i < 6; i++) { | |
167 | macaddr[i] = strtol(p, (char **)&p, 16); | |
168 | if (i == 5) { | |
169 | if (*p != '\0') | |
170 | return -1; | |
171 | } else { | |
172 | if (*p != ':' && *p != '-') | |
173 | return -1; | |
174 | p++; | |
175 | } | |
176 | } | |
177 | return 0; | |
178 | } | |
179 | ||
180 | return -1; | |
181 | } | |
182 | ||
183 | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) | |
184 | { | |
185 | const char *p, *p1; | |
186 | int len; | |
187 | p = *pp; | |
188 | p1 = strchr(p, sep); | |
189 | if (!p1) | |
190 | return -1; | |
191 | len = p1 - p; | |
192 | p1++; | |
193 | if (buf_size > 0) { | |
194 | if (len > buf_size - 1) | |
195 | len = buf_size - 1; | |
196 | memcpy(buf, p, len); | |
197 | buf[len] = '\0'; | |
198 | } | |
199 | *pp = p1; | |
200 | return 0; | |
201 | } | |
202 | ||
203 | int parse_host_src_port(struct sockaddr_in *haddr, | |
204 | struct sockaddr_in *saddr, | |
205 | const char *input_str) | |
206 | { | |
207 | char *str = strdup(input_str); | |
208 | char *host_str = str; | |
209 | char *src_str; | |
210 | const char *src_str2; | |
211 | char *ptr; | |
212 | ||
213 | /* | |
214 | * Chop off any extra arguments at the end of the string which | |
215 | * would start with a comma, then fill in the src port information | |
216 | * if it was provided else use the "any address" and "any port". | |
217 | */ | |
218 | if ((ptr = strchr(str,','))) | |
219 | *ptr = '\0'; | |
220 | ||
221 | if ((src_str = strchr(input_str,'@'))) { | |
222 | *src_str = '\0'; | |
223 | src_str++; | |
224 | } | |
225 | ||
226 | if (parse_host_port(haddr, host_str) < 0) | |
227 | goto fail; | |
228 | ||
229 | src_str2 = src_str; | |
230 | if (!src_str || *src_str == '\0') | |
231 | src_str2 = ":0"; | |
232 | ||
233 | if (parse_host_port(saddr, src_str2) < 0) | |
234 | goto fail; | |
235 | ||
236 | free(str); | |
237 | return(0); | |
238 | ||
239 | fail: | |
240 | free(str); | |
241 | return -1; | |
242 | } | |
243 | ||
244 | int parse_host_port(struct sockaddr_in *saddr, const char *str) | |
245 | { | |
246 | char buf[512]; | |
247 | struct hostent *he; | |
248 | const char *p, *r; | |
249 | int port; | |
250 | ||
251 | p = str; | |
252 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) | |
253 | return -1; | |
254 | saddr->sin_family = AF_INET; | |
255 | if (buf[0] == '\0') { | |
256 | saddr->sin_addr.s_addr = 0; | |
257 | } else { | |
258 | if (qemu_isdigit(buf[0])) { | |
259 | if (!inet_aton(buf, &saddr->sin_addr)) | |
260 | return -1; | |
261 | } else { | |
262 | if ((he = gethostbyname(buf)) == NULL) | |
263 | return - 1; | |
264 | saddr->sin_addr = *(struct in_addr *)he->h_addr; | |
265 | } | |
266 | } | |
267 | port = strtol(p, (char **)&r, 0); | |
268 | if (r == p) | |
269 | return -1; | |
270 | saddr->sin_port = htons(port); | |
271 | return 0; | |
272 | } | |
273 | ||
274 | void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6]) | |
275 | { | |
276 | snprintf(vc->info_str, sizeof(vc->info_str), | |
277 | "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x", | |
278 | vc->model, | |
279 | macaddr[0], macaddr[1], macaddr[2], | |
280 | macaddr[3], macaddr[4], macaddr[5]); | |
281 | } | |
282 | ||
283 | static char *assign_name(VLANClientState *vc1, const char *model) | |
284 | { | |
285 | VLANState *vlan; | |
286 | char buf[256]; | |
287 | int id = 0; | |
288 | ||
289 | QTAILQ_FOREACH(vlan, &vlans, next) { | |
290 | VLANClientState *vc; | |
291 | ||
292 | QTAILQ_FOREACH(vc, &vlan->clients, next) { | |
293 | if (vc != vc1 && strcmp(vc->model, model) == 0) { | |
294 | id++; | |
295 | } | |
296 | } | |
297 | } | |
298 | ||
299 | snprintf(buf, sizeof(buf), "%s.%d", model, id); | |
300 | ||
301 | return qemu_strdup(buf); | |
302 | } | |
303 | ||
304 | VLANClientState *qemu_new_vlan_client(VLANState *vlan, | |
305 | VLANClientState *peer, | |
306 | const char *model, | |
307 | const char *name, | |
308 | NetCanReceive *can_receive, | |
309 | NetReceive *receive, | |
310 | NetReceiveIOV *receive_iov, | |
311 | NetCleanup *cleanup, | |
312 | void *opaque) | |
313 | { | |
314 | VLANClientState *vc; | |
315 | ||
316 | vc = qemu_mallocz(sizeof(VLANClientState)); | |
317 | ||
318 | vc->model = qemu_strdup(model); | |
319 | if (name) | |
320 | vc->name = qemu_strdup(name); | |
321 | else | |
322 | vc->name = assign_name(vc, model); | |
323 | vc->can_receive = can_receive; | |
324 | vc->receive = receive; | |
325 | vc->receive_iov = receive_iov; | |
326 | vc->cleanup = cleanup; | |
327 | vc->opaque = opaque; | |
328 | ||
329 | if (vlan) { | |
330 | assert(!peer); | |
331 | vc->vlan = vlan; | |
332 | QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next); | |
333 | } else { | |
334 | if (peer) { | |
335 | vc->peer = peer; | |
336 | peer->peer = vc; | |
337 | } | |
338 | QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next); | |
339 | } | |
340 | ||
341 | return vc; | |
342 | } | |
343 | ||
344 | void qemu_del_vlan_client(VLANClientState *vc) | |
345 | { | |
346 | if (vc->vlan) { | |
347 | QTAILQ_REMOVE(&vc->vlan->clients, vc, next); | |
348 | } else { | |
349 | QTAILQ_REMOVE(&non_vlan_clients, vc, next); | |
350 | if (vc->peer) { | |
351 | vc->peer->peer = NULL; | |
352 | } | |
353 | } | |
354 | ||
355 | if (vc->cleanup) { | |
356 | vc->cleanup(vc); | |
357 | } | |
358 | ||
359 | qemu_free(vc->name); | |
360 | qemu_free(vc->model); | |
361 | qemu_free(vc); | |
362 | } | |
363 | ||
364 | VLANClientState *qemu_find_vlan_client(VLANState *vlan, void *opaque) | |
365 | { | |
366 | VLANClientState *vc; | |
367 | ||
368 | QTAILQ_FOREACH(vc, &vlan->clients, next) { | |
369 | if (vc->opaque == opaque) { | |
370 | return vc; | |
371 | } | |
372 | } | |
373 | ||
374 | return NULL; | |
375 | } | |
376 | ||
377 | static VLANClientState * | |
378 | qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id, | |
379 | const char *client_str) | |
380 | { | |
381 | VLANState *vlan; | |
382 | VLANClientState *vc; | |
383 | ||
384 | vlan = qemu_find_vlan(vlan_id, 0); | |
385 | if (!vlan) { | |
386 | monitor_printf(mon, "unknown VLAN %d\n", vlan_id); | |
387 | return NULL; | |
388 | } | |
389 | ||
390 | QTAILQ_FOREACH(vc, &vlan->clients, next) { | |
391 | if (!strcmp(vc->name, client_str)) { | |
392 | break; | |
393 | } | |
394 | } | |
395 | if (!vc) { | |
396 | monitor_printf(mon, "can't find device %s on VLAN %d\n", | |
397 | client_str, vlan_id); | |
398 | } | |
399 | ||
400 | return vc; | |
401 | } | |
402 | ||
403 | int qemu_can_send_packet(VLANClientState *sender) | |
404 | { | |
405 | VLANState *vlan = sender->vlan; | |
406 | VLANClientState *vc; | |
407 | ||
408 | if (!sender->vlan) { | |
409 | return 1; | |
410 | } | |
411 | ||
412 | QTAILQ_FOREACH(vc, &vlan->clients, next) { | |
413 | if (vc == sender) { | |
414 | continue; | |
415 | } | |
416 | ||
417 | /* no can_receive() handler, they can always receive */ | |
418 | if (!vc->can_receive || vc->can_receive(vc)) { | |
419 | return 1; | |
420 | } | |
421 | } | |
422 | return 0; | |
423 | } | |
424 | ||
425 | static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender, | |
426 | const uint8_t *buf, | |
427 | size_t size, | |
428 | void *opaque) | |
429 | { | |
430 | VLANState *vlan = opaque; | |
431 | VLANClientState *vc; | |
432 | int ret = -1; | |
433 | ||
434 | QTAILQ_FOREACH(vc, &vlan->clients, next) { | |
435 | ssize_t len; | |
436 | ||
437 | if (vc == sender) { | |
438 | continue; | |
439 | } | |
440 | ||
441 | if (vc->link_down) { | |
442 | ret = size; | |
443 | continue; | |
444 | } | |
445 | ||
446 | len = vc->receive(vc, buf, size); | |
447 | ||
448 | ret = (ret >= 0) ? ret : len; | |
449 | } | |
450 | ||
451 | return ret; | |
452 | } | |
453 | ||
454 | void qemu_purge_queued_packets(VLANClientState *vc) | |
455 | { | |
456 | if (!vc->vlan) | |
457 | return; | |
458 | ||
459 | qemu_net_queue_purge(vc->vlan->send_queue, vc); | |
460 | } | |
461 | ||
462 | void qemu_flush_queued_packets(VLANClientState *vc) | |
463 | { | |
464 | if (!vc->vlan) | |
465 | return; | |
466 | ||
467 | qemu_net_queue_flush(vc->vlan->send_queue); | |
468 | } | |
469 | ||
470 | ssize_t qemu_send_packet_async(VLANClientState *sender, | |
471 | const uint8_t *buf, int size, | |
472 | NetPacketSent *sent_cb) | |
473 | { | |
474 | if (sender->link_down || !sender->vlan) { | |
475 | return size; | |
476 | } | |
477 | ||
478 | #ifdef DEBUG_NET | |
479 | printf("qemu_send_packet_async:\n"); | |
480 | hex_dump(stdout, buf, size); | |
481 | #endif | |
482 | ||
483 | return qemu_net_queue_send(sender->vlan->send_queue, | |
484 | sender, buf, size, sent_cb); | |
485 | } | |
486 | ||
487 | void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size) | |
488 | { | |
489 | qemu_send_packet_async(vc, buf, size, NULL); | |
490 | } | |
491 | ||
492 | static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov, | |
493 | int iovcnt) | |
494 | { | |
495 | uint8_t buffer[4096]; | |
496 | size_t offset = 0; | |
497 | int i; | |
498 | ||
499 | for (i = 0; i < iovcnt; i++) { | |
500 | size_t len; | |
501 | ||
502 | len = MIN(sizeof(buffer) - offset, iov[i].iov_len); | |
503 | memcpy(buffer + offset, iov[i].iov_base, len); | |
504 | offset += len; | |
505 | } | |
506 | ||
507 | return vc->receive(vc, buffer, offset); | |
508 | } | |
509 | ||
510 | static ssize_t calc_iov_length(const struct iovec *iov, int iovcnt) | |
511 | { | |
512 | size_t offset = 0; | |
513 | int i; | |
514 | ||
515 | for (i = 0; i < iovcnt; i++) | |
516 | offset += iov[i].iov_len; | |
517 | return offset; | |
518 | } | |
519 | ||
520 | static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender, | |
521 | const struct iovec *iov, | |
522 | int iovcnt, | |
523 | void *opaque) | |
524 | { | |
525 | VLANState *vlan = opaque; | |
526 | VLANClientState *vc; | |
527 | ssize_t ret = -1; | |
528 | ||
529 | QTAILQ_FOREACH(vc, &vlan->clients, next) { | |
530 | ssize_t len; | |
531 | ||
532 | if (vc == sender) { | |
533 | continue; | |
534 | } | |
535 | ||
536 | if (vc->link_down) { | |
537 | ret = calc_iov_length(iov, iovcnt); | |
538 | continue; | |
539 | } | |
540 | ||
541 | if (vc->receive_iov) { | |
542 | len = vc->receive_iov(vc, iov, iovcnt); | |
543 | } else { | |
544 | len = vc_sendv_compat(vc, iov, iovcnt); | |
545 | } | |
546 | ||
547 | ret = (ret >= 0) ? ret : len; | |
548 | } | |
549 | ||
550 | return ret; | |
551 | } | |
552 | ||
553 | ssize_t qemu_sendv_packet_async(VLANClientState *sender, | |
554 | const struct iovec *iov, int iovcnt, | |
555 | NetPacketSent *sent_cb) | |
556 | { | |
557 | if (sender->link_down || !sender->vlan) { | |
558 | return calc_iov_length(iov, iovcnt); | |
559 | } | |
560 | ||
561 | return qemu_net_queue_send_iov(sender->vlan->send_queue, | |
562 | sender, iov, iovcnt, sent_cb); | |
563 | } | |
564 | ||
565 | ssize_t | |
566 | qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt) | |
567 | { | |
568 | return qemu_sendv_packet_async(vc, iov, iovcnt, NULL); | |
569 | } | |
570 | ||
571 | #if defined(CONFIG_SLIRP) | |
572 | ||
573 | /* slirp network adapter */ | |
574 | ||
575 | #define SLIRP_CFG_HOSTFWD 1 | |
576 | #define SLIRP_CFG_LEGACY 2 | |
577 | ||
578 | struct slirp_config_str { | |
579 | struct slirp_config_str *next; | |
580 | int flags; | |
581 | char str[1024]; | |
582 | int legacy_format; | |
583 | }; | |
584 | ||
585 | typedef struct SlirpState { | |
586 | QTAILQ_ENTRY(SlirpState) entry; | |
587 | VLANClientState *vc; | |
588 | Slirp *slirp; | |
589 | #ifndef _WIN32 | |
590 | char smb_dir[128]; | |
591 | #endif | |
592 | } SlirpState; | |
593 | ||
594 | static struct slirp_config_str *slirp_configs; | |
595 | const char *legacy_tftp_prefix; | |
596 | const char *legacy_bootp_filename; | |
597 | static QTAILQ_HEAD(slirp_stacks, SlirpState) slirp_stacks = | |
598 | QTAILQ_HEAD_INITIALIZER(slirp_stacks); | |
599 | ||
600 | static int slirp_hostfwd(SlirpState *s, const char *redir_str, | |
601 | int legacy_format); | |
602 | static int slirp_guestfwd(SlirpState *s, const char *config_str, | |
603 | int legacy_format); | |
604 | ||
605 | #ifndef _WIN32 | |
606 | static const char *legacy_smb_export; | |
607 | ||
608 | static int slirp_smb(SlirpState *s, const char *exported_dir, | |
609 | struct in_addr vserver_addr); | |
610 | static void slirp_smb_cleanup(SlirpState *s); | |
611 | #else | |
612 | static inline void slirp_smb_cleanup(SlirpState *s) { } | |
613 | #endif | |
614 | ||
615 | int slirp_can_output(void *opaque) | |
616 | { | |
617 | SlirpState *s = opaque; | |
618 | ||
619 | return qemu_can_send_packet(s->vc); | |
620 | } | |
621 | ||
622 | void slirp_output(void *opaque, const uint8_t *pkt, int pkt_len) | |
623 | { | |
624 | SlirpState *s = opaque; | |
625 | ||
626 | #ifdef DEBUG_SLIRP | |
627 | printf("slirp output:\n"); | |
628 | hex_dump(stdout, pkt, pkt_len); | |
629 | #endif | |
630 | qemu_send_packet(s->vc, pkt, pkt_len); | |
631 | } | |
632 | ||
633 | static ssize_t slirp_receive(VLANClientState *vc, const uint8_t *buf, size_t size) | |
634 | { | |
635 | SlirpState *s = vc->opaque; | |
636 | ||
637 | #ifdef DEBUG_SLIRP | |
638 | printf("slirp input:\n"); | |
639 | hex_dump(stdout, buf, size); | |
640 | #endif | |
641 | slirp_input(s->slirp, buf, size); | |
642 | return size; | |
643 | } | |
644 | ||
645 | static void net_slirp_cleanup(VLANClientState *vc) | |
646 | { | |
647 | SlirpState *s = vc->opaque; | |
648 | ||
649 | slirp_cleanup(s->slirp); | |
650 | slirp_smb_cleanup(s); | |
651 | QTAILQ_REMOVE(&slirp_stacks, s, entry); | |
652 | qemu_free(s); | |
653 | } | |
654 | ||
655 | static int net_slirp_init(VLANState *vlan, const char *model, | |
656 | const char *name, int restricted, | |
657 | const char *vnetwork, const char *vhost, | |
658 | const char *vhostname, const char *tftp_export, | |
659 | const char *bootfile, const char *vdhcp_start, | |
660 | const char *vnameserver, const char *smb_export, | |
661 | const char *vsmbserver) | |
662 | { | |
663 | /* default settings according to historic slirp */ | |
664 | struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */ | |
665 | struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */ | |
666 | struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */ | |
667 | struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */ | |
668 | struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */ | |
669 | #ifndef _WIN32 | |
670 | struct in_addr smbsrv = { .s_addr = 0 }; | |
671 | #endif | |
672 | SlirpState *s; | |
673 | char buf[20]; | |
674 | uint32_t addr; | |
675 | int shift; | |
676 | char *end; | |
677 | struct slirp_config_str *config; | |
678 | ||
679 | if (!tftp_export) { | |
680 | tftp_export = legacy_tftp_prefix; | |
681 | } | |
682 | if (!bootfile) { | |
683 | bootfile = legacy_bootp_filename; | |
684 | } | |
685 | ||
686 | if (vnetwork) { | |
687 | if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) { | |
688 | if (!inet_aton(vnetwork, &net)) { | |
689 | return -1; | |
690 | } | |
691 | addr = ntohl(net.s_addr); | |
692 | if (!(addr & 0x80000000)) { | |
693 | mask.s_addr = htonl(0xff000000); /* class A */ | |
694 | } else if ((addr & 0xfff00000) == 0xac100000) { | |
695 | mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */ | |
696 | } else if ((addr & 0xc0000000) == 0x80000000) { | |
697 | mask.s_addr = htonl(0xffff0000); /* class B */ | |
698 | } else if ((addr & 0xffff0000) == 0xc0a80000) { | |
699 | mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */ | |
700 | } else if ((addr & 0xffff0000) == 0xc6120000) { | |
701 | mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */ | |
702 | } else if ((addr & 0xe0000000) == 0xe0000000) { | |
703 | mask.s_addr = htonl(0xffffff00); /* class C */ | |
704 | } else { | |
705 | mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */ | |
706 | } | |
707 | } else { | |
708 | if (!inet_aton(buf, &net)) { | |
709 | return -1; | |
710 | } | |
711 | shift = strtol(vnetwork, &end, 10); | |
712 | if (*end != '\0') { | |
713 | if (!inet_aton(vnetwork, &mask)) { | |
714 | return -1; | |
715 | } | |
716 | } else if (shift < 4 || shift > 32) { | |
717 | return -1; | |
718 | } else { | |
719 | mask.s_addr = htonl(0xffffffff << (32 - shift)); | |
720 | } | |
721 | } | |
722 | net.s_addr &= mask.s_addr; | |
723 | host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr); | |
724 | dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr); | |
725 | dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr); | |
726 | } | |
727 | ||
728 | if (vhost && !inet_aton(vhost, &host)) { | |
729 | return -1; | |
730 | } | |
731 | if ((host.s_addr & mask.s_addr) != net.s_addr) { | |
732 | return -1; | |
733 | } | |
734 | ||
735 | if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) { | |
736 | return -1; | |
737 | } | |
738 | if ((dhcp.s_addr & mask.s_addr) != net.s_addr || | |
739 | dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) { | |
740 | return -1; | |
741 | } | |
742 | ||
743 | if (vnameserver && !inet_aton(vnameserver, &dns)) { | |
744 | return -1; | |
745 | } | |
746 | if ((dns.s_addr & mask.s_addr) != net.s_addr || | |
747 | dns.s_addr == host.s_addr) { | |
748 | return -1; | |
749 | } | |
750 | ||
751 | #ifndef _WIN32 | |
752 | if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) { | |
753 | return -1; | |
754 | } | |
755 | #endif | |
756 | ||
757 | s = qemu_mallocz(sizeof(SlirpState)); | |
758 | s->slirp = slirp_init(restricted, net, mask, host, vhostname, | |
759 | tftp_export, bootfile, dhcp, dns, s); | |
760 | QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry); | |
761 | ||
762 | for (config = slirp_configs; config; config = config->next) { | |
763 | if (config->flags & SLIRP_CFG_HOSTFWD) { | |
764 | if (slirp_hostfwd(s, config->str, | |
765 | config->flags & SLIRP_CFG_LEGACY) < 0) | |
766 | return -1; | |
767 | } else { | |
768 | if (slirp_guestfwd(s, config->str, | |
769 | config->flags & SLIRP_CFG_LEGACY) < 0) | |
770 | return -1; | |
771 | } | |
772 | } | |
773 | #ifndef _WIN32 | |
774 | if (!smb_export) { | |
775 | smb_export = legacy_smb_export; | |
776 | } | |
777 | if (smb_export) { | |
778 | if (slirp_smb(s, smb_export, smbsrv) < 0) | |
779 | return -1; | |
780 | } | |
781 | #endif | |
782 | ||
783 | s->vc = qemu_new_vlan_client(vlan, NULL, model, name, NULL, | |
784 | slirp_receive, NULL, | |
785 | net_slirp_cleanup, s); | |
786 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), | |
787 | "net=%s, restricted=%c", inet_ntoa(net), restricted ? 'y' : 'n'); | |
788 | return 0; | |
789 | } | |
790 | ||
791 | static SlirpState *slirp_lookup(Monitor *mon, const char *vlan, | |
792 | const char *stack) | |
793 | { | |
794 | VLANClientState *vc; | |
795 | ||
796 | if (vlan) { | |
797 | vc = qemu_find_vlan_client_by_name(mon, strtol(vlan, NULL, 0), stack); | |
798 | if (!vc) { | |
799 | return NULL; | |
800 | } | |
801 | if (strcmp(vc->model, "user")) { | |
802 | monitor_printf(mon, "invalid device specified\n"); | |
803 | return NULL; | |
804 | } | |
805 | return vc->opaque; | |
806 | } else { | |
807 | if (QTAILQ_EMPTY(&slirp_stacks)) { | |
808 | monitor_printf(mon, "user mode network stack not in use\n"); | |
809 | return NULL; | |
810 | } | |
811 | return QTAILQ_FIRST(&slirp_stacks); | |
812 | } | |
813 | } | |
814 | ||
815 | void net_slirp_hostfwd_remove(Monitor *mon, const QDict *qdict) | |
816 | { | |
817 | struct in_addr host_addr = { .s_addr = INADDR_ANY }; | |
818 | int host_port; | |
819 | char buf[256] = ""; | |
820 | const char *src_str, *p; | |
821 | SlirpState *s; | |
822 | int is_udp = 0; | |
823 | int err; | |
824 | const char *arg1 = qdict_get_str(qdict, "arg1"); | |
825 | const char *arg2 = qdict_get_try_str(qdict, "arg2"); | |
826 | const char *arg3 = qdict_get_try_str(qdict, "arg3"); | |
827 | ||
828 | if (arg2) { | |
829 | s = slirp_lookup(mon, arg1, arg2); | |
830 | src_str = arg3; | |
831 | } else { | |
832 | s = slirp_lookup(mon, NULL, NULL); | |
833 | src_str = arg1; | |
834 | } | |
835 | if (!s) { | |
836 | return; | |
837 | } | |
838 | ||
839 | if (!src_str || !src_str[0]) | |
840 | goto fail_syntax; | |
841 | ||
842 | p = src_str; | |
843 | get_str_sep(buf, sizeof(buf), &p, ':'); | |
844 | ||
845 | if (!strcmp(buf, "tcp") || buf[0] == '\0') { | |
846 | is_udp = 0; | |
847 | } else if (!strcmp(buf, "udp")) { | |
848 | is_udp = 1; | |
849 | } else { | |
850 | goto fail_syntax; | |
851 | } | |
852 | ||
853 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
854 | goto fail_syntax; | |
855 | } | |
856 | if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) { | |
857 | goto fail_syntax; | |
858 | } | |
859 | ||
860 | host_port = atoi(p); | |
861 | ||
862 | err = slirp_remove_hostfwd(QTAILQ_FIRST(&slirp_stacks)->slirp, is_udp, | |
863 | host_addr, host_port); | |
864 | ||
865 | monitor_printf(mon, "host forwarding rule for %s %s\n", src_str, | |
866 | err ? "removed" : "not found"); | |
867 | return; | |
868 | ||
869 | fail_syntax: | |
870 | monitor_printf(mon, "invalid format\n"); | |
871 | } | |
872 | ||
873 | static int slirp_hostfwd(SlirpState *s, const char *redir_str, | |
874 | int legacy_format) | |
875 | { | |
876 | struct in_addr host_addr = { .s_addr = INADDR_ANY }; | |
877 | struct in_addr guest_addr = { .s_addr = 0 }; | |
878 | int host_port, guest_port; | |
879 | const char *p; | |
880 | char buf[256]; | |
881 | int is_udp; | |
882 | char *end; | |
883 | ||
884 | p = redir_str; | |
885 | if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
886 | goto fail_syntax; | |
887 | } | |
888 | if (!strcmp(buf, "tcp") || buf[0] == '\0') { | |
889 | is_udp = 0; | |
890 | } else if (!strcmp(buf, "udp")) { | |
891 | is_udp = 1; | |
892 | } else { | |
893 | goto fail_syntax; | |
894 | } | |
895 | ||
896 | if (!legacy_format) { | |
897 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
898 | goto fail_syntax; | |
899 | } | |
900 | if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) { | |
901 | goto fail_syntax; | |
902 | } | |
903 | } | |
904 | ||
905 | if (get_str_sep(buf, sizeof(buf), &p, legacy_format ? ':' : '-') < 0) { | |
906 | goto fail_syntax; | |
907 | } | |
908 | host_port = strtol(buf, &end, 0); | |
909 | if (*end != '\0' || host_port < 1 || host_port > 65535) { | |
910 | goto fail_syntax; | |
911 | } | |
912 | ||
913 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
914 | goto fail_syntax; | |
915 | } | |
916 | if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) { | |
917 | goto fail_syntax; | |
918 | } | |
919 | ||
920 | guest_port = strtol(p, &end, 0); | |
921 | if (*end != '\0' || guest_port < 1 || guest_port > 65535) { | |
922 | goto fail_syntax; | |
923 | } | |
924 | ||
925 | if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr, | |
926 | guest_port) < 0) { | |
927 | qemu_error("could not set up host forwarding rule '%s'\n", | |
928 | redir_str); | |
929 | return -1; | |
930 | } | |
931 | return 0; | |
932 | ||
933 | fail_syntax: | |
934 | qemu_error("invalid host forwarding rule '%s'\n", redir_str); | |
935 | return -1; | |
936 | } | |
937 | ||
938 | void net_slirp_hostfwd_add(Monitor *mon, const QDict *qdict) | |
939 | { | |
940 | const char *redir_str; | |
941 | SlirpState *s; | |
942 | const char *arg1 = qdict_get_str(qdict, "arg1"); | |
943 | const char *arg2 = qdict_get_try_str(qdict, "arg2"); | |
944 | const char *arg3 = qdict_get_try_str(qdict, "arg3"); | |
945 | ||
946 | if (arg2) { | |
947 | s = slirp_lookup(mon, arg1, arg2); | |
948 | redir_str = arg3; | |
949 | } else { | |
950 | s = slirp_lookup(mon, NULL, NULL); | |
951 | redir_str = arg1; | |
952 | } | |
953 | if (s) { | |
954 | slirp_hostfwd(s, redir_str, 0); | |
955 | } | |
956 | ||
957 | } | |
958 | ||
959 | int net_slirp_redir(const char *redir_str) | |
960 | { | |
961 | struct slirp_config_str *config; | |
962 | ||
963 | if (QTAILQ_EMPTY(&slirp_stacks)) { | |
964 | config = qemu_malloc(sizeof(*config)); | |
965 | pstrcpy(config->str, sizeof(config->str), redir_str); | |
966 | config->flags = SLIRP_CFG_HOSTFWD | SLIRP_CFG_LEGACY; | |
967 | config->next = slirp_configs; | |
968 | slirp_configs = config; | |
969 | return 0; | |
970 | } | |
971 | ||
972 | return slirp_hostfwd(QTAILQ_FIRST(&slirp_stacks), redir_str, 1); | |
973 | } | |
974 | ||
975 | #ifndef _WIN32 | |
976 | ||
977 | /* automatic user mode samba server configuration */ | |
978 | static void slirp_smb_cleanup(SlirpState *s) | |
979 | { | |
980 | char cmd[128]; | |
981 | ||
982 | if (s->smb_dir[0] != '\0') { | |
983 | snprintf(cmd, sizeof(cmd), "rm -rf %s", s->smb_dir); | |
984 | system(cmd); | |
985 | s->smb_dir[0] = '\0'; | |
986 | } | |
987 | } | |
988 | ||
989 | static int slirp_smb(SlirpState* s, const char *exported_dir, | |
990 | struct in_addr vserver_addr) | |
991 | { | |
992 | static int instance; | |
993 | char smb_conf[128]; | |
994 | char smb_cmdline[128]; | |
995 | FILE *f; | |
996 | ||
997 | snprintf(s->smb_dir, sizeof(s->smb_dir), "/tmp/qemu-smb.%ld-%d", | |
998 | (long)getpid(), instance++); | |
999 | if (mkdir(s->smb_dir, 0700) < 0) { | |
1000 | qemu_error("could not create samba server dir '%s'\n", s->smb_dir); | |
1001 | return -1; | |
1002 | } | |
1003 | snprintf(smb_conf, sizeof(smb_conf), "%s/%s", s->smb_dir, "smb.conf"); | |
1004 | ||
1005 | f = fopen(smb_conf, "w"); | |
1006 | if (!f) { | |
1007 | slirp_smb_cleanup(s); | |
1008 | qemu_error("could not create samba server configuration file '%s'\n", | |
1009 | smb_conf); | |
1010 | return -1; | |
1011 | } | |
1012 | fprintf(f, | |
1013 | "[global]\n" | |
1014 | "private dir=%s\n" | |
1015 | "smb ports=0\n" | |
1016 | "socket address=127.0.0.1\n" | |
1017 | "pid directory=%s\n" | |
1018 | "lock directory=%s\n" | |
1019 | "log file=%s/log.smbd\n" | |
1020 | "smb passwd file=%s/smbpasswd\n" | |
1021 | "security = share\n" | |
1022 | "[qemu]\n" | |
1023 | "path=%s\n" | |
1024 | "read only=no\n" | |
1025 | "guest ok=yes\n", | |
1026 | s->smb_dir, | |
1027 | s->smb_dir, | |
1028 | s->smb_dir, | |
1029 | s->smb_dir, | |
1030 | s->smb_dir, | |
1031 | exported_dir | |
1032 | ); | |
1033 | fclose(f); | |
1034 | ||
1035 | snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s", | |
1036 | SMBD_COMMAND, smb_conf); | |
1037 | ||
1038 | if (slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 139) < 0) { | |
1039 | slirp_smb_cleanup(s); | |
1040 | qemu_error("conflicting/invalid smbserver address\n"); | |
1041 | return -1; | |
1042 | } | |
1043 | return 0; | |
1044 | } | |
1045 | ||
1046 | /* automatic user mode samba server configuration (legacy interface) */ | |
1047 | int net_slirp_smb(const char *exported_dir) | |
1048 | { | |
1049 | struct in_addr vserver_addr = { .s_addr = 0 }; | |
1050 | ||
1051 | if (legacy_smb_export) { | |
1052 | fprintf(stderr, "-smb given twice\n"); | |
1053 | return -1; | |
1054 | } | |
1055 | legacy_smb_export = exported_dir; | |
1056 | if (!QTAILQ_EMPTY(&slirp_stacks)) { | |
1057 | return slirp_smb(QTAILQ_FIRST(&slirp_stacks), exported_dir, | |
1058 | vserver_addr); | |
1059 | } | |
1060 | return 0; | |
1061 | } | |
1062 | ||
1063 | #endif /* !defined(_WIN32) */ | |
1064 | ||
1065 | struct GuestFwd { | |
1066 | CharDriverState *hd; | |
1067 | struct in_addr server; | |
1068 | int port; | |
1069 | Slirp *slirp; | |
1070 | }; | |
1071 | ||
1072 | static int guestfwd_can_read(void *opaque) | |
1073 | { | |
1074 | struct GuestFwd *fwd = opaque; | |
1075 | return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port); | |
1076 | } | |
1077 | ||
1078 | static void guestfwd_read(void *opaque, const uint8_t *buf, int size) | |
1079 | { | |
1080 | struct GuestFwd *fwd = opaque; | |
1081 | slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size); | |
1082 | } | |
1083 | ||
1084 | static int slirp_guestfwd(SlirpState *s, const char *config_str, | |
1085 | int legacy_format) | |
1086 | { | |
1087 | struct in_addr server = { .s_addr = 0 }; | |
1088 | struct GuestFwd *fwd; | |
1089 | const char *p; | |
1090 | char buf[128]; | |
1091 | char *end; | |
1092 | int port; | |
1093 | ||
1094 | p = config_str; | |
1095 | if (legacy_format) { | |
1096 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
1097 | goto fail_syntax; | |
1098 | } | |
1099 | } else { | |
1100 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
1101 | goto fail_syntax; | |
1102 | } | |
1103 | if (strcmp(buf, "tcp") && buf[0] != '\0') { | |
1104 | goto fail_syntax; | |
1105 | } | |
1106 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
1107 | goto fail_syntax; | |
1108 | } | |
1109 | if (buf[0] != '\0' && !inet_aton(buf, &server)) { | |
1110 | goto fail_syntax; | |
1111 | } | |
1112 | if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) { | |
1113 | goto fail_syntax; | |
1114 | } | |
1115 | } | |
1116 | port = strtol(buf, &end, 10); | |
1117 | if (*end != '\0' || port < 1 || port > 65535) { | |
1118 | goto fail_syntax; | |
1119 | } | |
1120 | ||
1121 | fwd = qemu_malloc(sizeof(struct GuestFwd)); | |
1122 | snprintf(buf, sizeof(buf), "guestfwd.tcp:%d", port); | |
1123 | fwd->hd = qemu_chr_open(buf, p, NULL); | |
1124 | if (!fwd->hd) { | |
1125 | qemu_error("could not open guest forwarding device '%s'\n", buf); | |
1126 | qemu_free(fwd); | |
1127 | return -1; | |
1128 | } | |
1129 | ||
1130 | if (slirp_add_exec(s->slirp, 3, fwd->hd, &server, port) < 0) { | |
1131 | qemu_error("conflicting/invalid host:port in guest forwarding " | |
1132 | "rule '%s'\n", config_str); | |
1133 | qemu_free(fwd); | |
1134 | return -1; | |
1135 | } | |
1136 | fwd->server = server; | |
1137 | fwd->port = port; | |
1138 | fwd->slirp = s->slirp; | |
1139 | ||
1140 | qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read, | |
1141 | NULL, fwd); | |
1142 | return 0; | |
1143 | ||
1144 | fail_syntax: | |
1145 | qemu_error("invalid guest forwarding rule '%s'\n", config_str); | |
1146 | return -1; | |
1147 | } | |
1148 | ||
1149 | void do_info_usernet(Monitor *mon) | |
1150 | { | |
1151 | SlirpState *s; | |
1152 | ||
1153 | QTAILQ_FOREACH(s, &slirp_stacks, entry) { | |
1154 | monitor_printf(mon, "VLAN %d (%s):\n", s->vc->vlan->id, s->vc->name); | |
1155 | slirp_connection_info(s->slirp, mon); | |
1156 | } | |
1157 | } | |
1158 | ||
1159 | #endif /* CONFIG_SLIRP */ | |
1160 | ||
1161 | #if !defined(_WIN32) | |
1162 | ||
1163 | typedef struct TAPState { | |
1164 | VLANClientState *vc; | |
1165 | int fd; | |
1166 | char down_script[1024]; | |
1167 | char down_script_arg[128]; | |
1168 | uint8_t buf[4096]; | |
1169 | unsigned int read_poll : 1; | |
1170 | unsigned int write_poll : 1; | |
1171 | } TAPState; | |
1172 | ||
1173 | static int launch_script(const char *setup_script, const char *ifname, int fd); | |
1174 | ||
1175 | static int tap_can_send(void *opaque); | |
1176 | static void tap_send(void *opaque); | |
1177 | static void tap_writable(void *opaque); | |
1178 | ||
1179 | static void tap_update_fd_handler(TAPState *s) | |
1180 | { | |
1181 | qemu_set_fd_handler2(s->fd, | |
1182 | s->read_poll ? tap_can_send : NULL, | |
1183 | s->read_poll ? tap_send : NULL, | |
1184 | s->write_poll ? tap_writable : NULL, | |
1185 | s); | |
1186 | } | |
1187 | ||
1188 | static void tap_read_poll(TAPState *s, int enable) | |
1189 | { | |
1190 | s->read_poll = !!enable; | |
1191 | tap_update_fd_handler(s); | |
1192 | } | |
1193 | ||
1194 | static void tap_write_poll(TAPState *s, int enable) | |
1195 | { | |
1196 | s->write_poll = !!enable; | |
1197 | tap_update_fd_handler(s); | |
1198 | } | |
1199 | ||
1200 | static void tap_writable(void *opaque) | |
1201 | { | |
1202 | TAPState *s = opaque; | |
1203 | ||
1204 | tap_write_poll(s, 0); | |
1205 | ||
1206 | qemu_flush_queued_packets(s->vc); | |
1207 | } | |
1208 | ||
1209 | static ssize_t tap_receive_iov(VLANClientState *vc, const struct iovec *iov, | |
1210 | int iovcnt) | |
1211 | { | |
1212 | TAPState *s = vc->opaque; | |
1213 | ssize_t len; | |
1214 | ||
1215 | do { | |
1216 | len = writev(s->fd, iov, iovcnt); | |
1217 | } while (len == -1 && errno == EINTR); | |
1218 | ||
1219 | if (len == -1 && errno == EAGAIN) { | |
1220 | tap_write_poll(s, 1); | |
1221 | return 0; | |
1222 | } | |
1223 | ||
1224 | return len; | |
1225 | } | |
1226 | ||
1227 | static ssize_t tap_receive(VLANClientState *vc, const uint8_t *buf, size_t size) | |
1228 | { | |
1229 | TAPState *s = vc->opaque; | |
1230 | ssize_t len; | |
1231 | ||
1232 | do { | |
1233 | len = write(s->fd, buf, size); | |
1234 | } while (len == -1 && (errno == EINTR || errno == EAGAIN)); | |
1235 | ||
1236 | return len; | |
1237 | } | |
1238 | ||
1239 | static int tap_can_send(void *opaque) | |
1240 | { | |
1241 | TAPState *s = opaque; | |
1242 | ||
1243 | return qemu_can_send_packet(s->vc); | |
1244 | } | |
1245 | ||
1246 | #ifdef __sun__ | |
1247 | static ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen) | |
1248 | { | |
1249 | struct strbuf sbuf; | |
1250 | int f = 0; | |
1251 | ||
1252 | sbuf.maxlen = maxlen; | |
1253 | sbuf.buf = (char *)buf; | |
1254 | ||
1255 | return getmsg(tapfd, NULL, &sbuf, &f) >= 0 ? sbuf.len : -1; | |
1256 | } | |
1257 | #else | |
1258 | static ssize_t tap_read_packet(int tapfd, uint8_t *buf, int maxlen) | |
1259 | { | |
1260 | return read(tapfd, buf, maxlen); | |
1261 | } | |
1262 | #endif | |
1263 | ||
1264 | static void tap_send_completed(VLANClientState *vc, ssize_t len) | |
1265 | { | |
1266 | TAPState *s = vc->opaque; | |
1267 | tap_read_poll(s, 1); | |
1268 | } | |
1269 | ||
1270 | static void tap_send(void *opaque) | |
1271 | { | |
1272 | TAPState *s = opaque; | |
1273 | int size; | |
1274 | ||
1275 | do { | |
1276 | size = tap_read_packet(s->fd, s->buf, sizeof(s->buf)); | |
1277 | if (size <= 0) { | |
1278 | break; | |
1279 | } | |
1280 | ||
1281 | size = qemu_send_packet_async(s->vc, s->buf, size, tap_send_completed); | |
1282 | if (size == 0) { | |
1283 | tap_read_poll(s, 0); | |
1284 | } | |
1285 | } while (size > 0); | |
1286 | } | |
1287 | ||
1288 | #ifdef TUNSETSNDBUF | |
1289 | /* sndbuf should be set to a value lower than the tx queue | |
1290 | * capacity of any destination network interface. | |
1291 | * Ethernet NICs generally have txqueuelen=1000, so 1Mb is | |
1292 | * a good default, given a 1500 byte MTU. | |
1293 | */ | |
1294 | #define TAP_DEFAULT_SNDBUF 1024*1024 | |
1295 | ||
1296 | static int tap_set_sndbuf(TAPState *s, QemuOpts *opts) | |
1297 | { | |
1298 | int sndbuf; | |
1299 | ||
1300 | sndbuf = qemu_opt_get_size(opts, "sndbuf", TAP_DEFAULT_SNDBUF); | |
1301 | if (!sndbuf) { | |
1302 | sndbuf = INT_MAX; | |
1303 | } | |
1304 | ||
1305 | if (ioctl(s->fd, TUNSETSNDBUF, &sndbuf) == -1 && qemu_opt_get(opts, "sndbuf")) { | |
1306 | qemu_error("TUNSETSNDBUF ioctl failed: %s\n", strerror(errno)); | |
1307 | return -1; | |
1308 | } | |
1309 | return 0; | |
1310 | } | |
1311 | #else | |
1312 | static int tap_set_sndbuf(TAPState *s, QemuOpts *opts) | |
1313 | { | |
1314 | return 0; | |
1315 | } | |
1316 | #endif /* TUNSETSNDBUF */ | |
1317 | ||
1318 | static void tap_cleanup(VLANClientState *vc) | |
1319 | { | |
1320 | TAPState *s = vc->opaque; | |
1321 | ||
1322 | qemu_purge_queued_packets(vc); | |
1323 | ||
1324 | if (s->down_script[0]) | |
1325 | launch_script(s->down_script, s->down_script_arg, s->fd); | |
1326 | ||
1327 | tap_read_poll(s, 0); | |
1328 | tap_write_poll(s, 0); | |
1329 | close(s->fd); | |
1330 | qemu_free(s); | |
1331 | } | |
1332 | ||
1333 | /* fd support */ | |
1334 | ||
1335 | static TAPState *net_tap_fd_init(VLANState *vlan, | |
1336 | const char *model, | |
1337 | const char *name, | |
1338 | int fd) | |
1339 | { | |
1340 | TAPState *s; | |
1341 | ||
1342 | s = qemu_mallocz(sizeof(TAPState)); | |
1343 | s->fd = fd; | |
1344 | s->vc = qemu_new_vlan_client(vlan, NULL, model, name, NULL, | |
1345 | tap_receive, tap_receive_iov, | |
1346 | tap_cleanup, s); | |
1347 | tap_read_poll(s, 1); | |
1348 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), "fd=%d", fd); | |
1349 | return s; | |
1350 | } | |
1351 | ||
1352 | #if defined (CONFIG_BSD) || defined (__FreeBSD_kernel__) | |
1353 | static int tap_open(char *ifname, int ifname_size) | |
1354 | { | |
1355 | int fd; | |
1356 | char *dev; | |
1357 | struct stat s; | |
1358 | ||
1359 | TFR(fd = open("/dev/tap", O_RDWR)); | |
1360 | if (fd < 0) { | |
1361 | fprintf(stderr, "warning: could not open /dev/tap: no virtual network emulation\n"); | |
1362 | return -1; | |
1363 | } | |
1364 | ||
1365 | fstat(fd, &s); | |
1366 | dev = devname(s.st_rdev, S_IFCHR); | |
1367 | pstrcpy(ifname, ifname_size, dev); | |
1368 | ||
1369 | fcntl(fd, F_SETFL, O_NONBLOCK); | |
1370 | return fd; | |
1371 | } | |
1372 | #elif defined(__sun__) | |
1373 | #define TUNNEWPPA (('T'<<16) | 0x0001) | |
1374 | /* | |
1375 | * Allocate TAP device, returns opened fd. | |
1376 | * Stores dev name in the first arg(must be large enough). | |
1377 | */ | |
1378 | static int tap_alloc(char *dev, size_t dev_size) | |
1379 | { | |
1380 | int tap_fd, if_fd, ppa = -1; | |
1381 | static int ip_fd = 0; | |
1382 | char *ptr; | |
1383 | ||
1384 | static int arp_fd = 0; | |
1385 | int ip_muxid, arp_muxid; | |
1386 | struct strioctl strioc_if, strioc_ppa; | |
1387 | int link_type = I_PLINK;; | |
1388 | struct lifreq ifr; | |
1389 | char actual_name[32] = ""; | |
1390 | ||
1391 | memset(&ifr, 0x0, sizeof(ifr)); | |
1392 | ||
1393 | if( *dev ){ | |
1394 | ptr = dev; | |
1395 | while( *ptr && !qemu_isdigit((int)*ptr) ) ptr++; | |
1396 | ppa = atoi(ptr); | |
1397 | } | |
1398 | ||
1399 | /* Check if IP device was opened */ | |
1400 | if( ip_fd ) | |
1401 | close(ip_fd); | |
1402 | ||
1403 | TFR(ip_fd = open("/dev/udp", O_RDWR, 0)); | |
1404 | if (ip_fd < 0) { | |
1405 | syslog(LOG_ERR, "Can't open /dev/ip (actually /dev/udp)"); | |
1406 | return -1; | |
1407 | } | |
1408 | ||
1409 | TFR(tap_fd = open("/dev/tap", O_RDWR, 0)); | |
1410 | if (tap_fd < 0) { | |
1411 | syslog(LOG_ERR, "Can't open /dev/tap"); | |
1412 | return -1; | |
1413 | } | |
1414 | ||
1415 | /* Assign a new PPA and get its unit number. */ | |
1416 | strioc_ppa.ic_cmd = TUNNEWPPA; | |
1417 | strioc_ppa.ic_timout = 0; | |
1418 | strioc_ppa.ic_len = sizeof(ppa); | |
1419 | strioc_ppa.ic_dp = (char *)&ppa; | |
1420 | if ((ppa = ioctl (tap_fd, I_STR, &strioc_ppa)) < 0) | |
1421 | syslog (LOG_ERR, "Can't assign new interface"); | |
1422 | ||
1423 | TFR(if_fd = open("/dev/tap", O_RDWR, 0)); | |
1424 | if (if_fd < 0) { | |
1425 | syslog(LOG_ERR, "Can't open /dev/tap (2)"); | |
1426 | return -1; | |
1427 | } | |
1428 | if(ioctl(if_fd, I_PUSH, "ip") < 0){ | |
1429 | syslog(LOG_ERR, "Can't push IP module"); | |
1430 | return -1; | |
1431 | } | |
1432 | ||
1433 | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) < 0) | |
1434 | syslog(LOG_ERR, "Can't get flags\n"); | |
1435 | ||
1436 | snprintf (actual_name, 32, "tap%d", ppa); | |
1437 | pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name); | |
1438 | ||
1439 | ifr.lifr_ppa = ppa; | |
1440 | /* Assign ppa according to the unit number returned by tun device */ | |
1441 | ||
1442 | if (ioctl (if_fd, SIOCSLIFNAME, &ifr) < 0) | |
1443 | syslog (LOG_ERR, "Can't set PPA %d", ppa); | |
1444 | if (ioctl(if_fd, SIOCGLIFFLAGS, &ifr) <0) | |
1445 | syslog (LOG_ERR, "Can't get flags\n"); | |
1446 | /* Push arp module to if_fd */ | |
1447 | if (ioctl (if_fd, I_PUSH, "arp") < 0) | |
1448 | syslog (LOG_ERR, "Can't push ARP module (2)"); | |
1449 | ||
1450 | /* Push arp module to ip_fd */ | |
1451 | if (ioctl (ip_fd, I_POP, NULL) < 0) | |
1452 | syslog (LOG_ERR, "I_POP failed\n"); | |
1453 | if (ioctl (ip_fd, I_PUSH, "arp") < 0) | |
1454 | syslog (LOG_ERR, "Can't push ARP module (3)\n"); | |
1455 | /* Open arp_fd */ | |
1456 | TFR(arp_fd = open ("/dev/tap", O_RDWR, 0)); | |
1457 | if (arp_fd < 0) | |
1458 | syslog (LOG_ERR, "Can't open %s\n", "/dev/tap"); | |
1459 | ||
1460 | /* Set ifname to arp */ | |
1461 | strioc_if.ic_cmd = SIOCSLIFNAME; | |
1462 | strioc_if.ic_timout = 0; | |
1463 | strioc_if.ic_len = sizeof(ifr); | |
1464 | strioc_if.ic_dp = (char *)𝔦 | |
1465 | if (ioctl(arp_fd, I_STR, &strioc_if) < 0){ | |
1466 | syslog (LOG_ERR, "Can't set ifname to arp\n"); | |
1467 | } | |
1468 | ||
1469 | if((ip_muxid = ioctl(ip_fd, I_LINK, if_fd)) < 0){ | |
1470 | syslog(LOG_ERR, "Can't link TAP device to IP"); | |
1471 | return -1; | |
1472 | } | |
1473 | ||
1474 | if ((arp_muxid = ioctl (ip_fd, link_type, arp_fd)) < 0) | |
1475 | syslog (LOG_ERR, "Can't link TAP device to ARP"); | |
1476 | ||
1477 | close (if_fd); | |
1478 | ||
1479 | memset(&ifr, 0x0, sizeof(ifr)); | |
1480 | pstrcpy(ifr.lifr_name, sizeof(ifr.lifr_name), actual_name); | |
1481 | ifr.lifr_ip_muxid = ip_muxid; | |
1482 | ifr.lifr_arp_muxid = arp_muxid; | |
1483 | ||
1484 | if (ioctl (ip_fd, SIOCSLIFMUXID, &ifr) < 0) | |
1485 | { | |
1486 | ioctl (ip_fd, I_PUNLINK , arp_muxid); | |
1487 | ioctl (ip_fd, I_PUNLINK, ip_muxid); | |
1488 | syslog (LOG_ERR, "Can't set multiplexor id"); | |
1489 | } | |
1490 | ||
1491 | snprintf(dev, dev_size, "tap%d", ppa); | |
1492 | return tap_fd; | |
1493 | } | |
1494 | ||
1495 | static int tap_open(char *ifname, int ifname_size) | |
1496 | { | |
1497 | char dev[10]=""; | |
1498 | int fd; | |
1499 | if( (fd = tap_alloc(dev, sizeof(dev))) < 0 ){ | |
1500 | fprintf(stderr, "Cannot allocate TAP device\n"); | |
1501 | return -1; | |
1502 | } | |
1503 | pstrcpy(ifname, ifname_size, dev); | |
1504 | fcntl(fd, F_SETFL, O_NONBLOCK); | |
1505 | return fd; | |
1506 | } | |
1507 | #elif defined (_AIX) | |
1508 | static int tap_open(char *ifname, int ifname_size) | |
1509 | { | |
1510 | fprintf (stderr, "no tap on AIX\n"); | |
1511 | return -1; | |
1512 | } | |
1513 | #else | |
1514 | static int tap_open(char *ifname, int ifname_size) | |
1515 | { | |
1516 | struct ifreq ifr; | |
1517 | int fd, ret; | |
1518 | ||
1519 | TFR(fd = open("/dev/net/tun", O_RDWR)); | |
1520 | if (fd < 0) { | |
1521 | fprintf(stderr, "warning: could not open /dev/net/tun: no virtual network emulation\n"); | |
1522 | return -1; | |
1523 | } | |
1524 | memset(&ifr, 0, sizeof(ifr)); | |
1525 | ifr.ifr_flags = IFF_TAP | IFF_NO_PI; | |
1526 | if (ifname[0] != '\0') | |
1527 | pstrcpy(ifr.ifr_name, IFNAMSIZ, ifname); | |
1528 | else | |
1529 | pstrcpy(ifr.ifr_name, IFNAMSIZ, "tap%d"); | |
1530 | ret = ioctl(fd, TUNSETIFF, (void *) &ifr); | |
1531 | if (ret != 0) { | |
1532 | fprintf(stderr, "warning: could not configure /dev/net/tun: no virtual network emulation\n"); | |
1533 | close(fd); | |
1534 | return -1; | |
1535 | } | |
1536 | pstrcpy(ifname, ifname_size, ifr.ifr_name); | |
1537 | fcntl(fd, F_SETFL, O_NONBLOCK); | |
1538 | return fd; | |
1539 | } | |
1540 | #endif | |
1541 | ||
1542 | static int launch_script(const char *setup_script, const char *ifname, int fd) | |
1543 | { | |
1544 | sigset_t oldmask, mask; | |
1545 | int pid, status; | |
1546 | char *args[3]; | |
1547 | char **parg; | |
1548 | ||
1549 | sigemptyset(&mask); | |
1550 | sigaddset(&mask, SIGCHLD); | |
1551 | sigprocmask(SIG_BLOCK, &mask, &oldmask); | |
1552 | ||
1553 | /* try to launch network script */ | |
1554 | pid = fork(); | |
1555 | if (pid == 0) { | |
1556 | int open_max = sysconf(_SC_OPEN_MAX), i; | |
1557 | ||
1558 | for (i = 0; i < open_max; i++) { | |
1559 | if (i != STDIN_FILENO && | |
1560 | i != STDOUT_FILENO && | |
1561 | i != STDERR_FILENO && | |
1562 | i != fd) { | |
1563 | close(i); | |
1564 | } | |
1565 | } | |
1566 | parg = args; | |
1567 | *parg++ = (char *)setup_script; | |
1568 | *parg++ = (char *)ifname; | |
1569 | *parg++ = NULL; | |
1570 | execv(setup_script, args); | |
1571 | _exit(1); | |
1572 | } else if (pid > 0) { | |
1573 | while (waitpid(pid, &status, 0) != pid) { | |
1574 | /* loop */ | |
1575 | } | |
1576 | sigprocmask(SIG_SETMASK, &oldmask, NULL); | |
1577 | ||
1578 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { | |
1579 | return 0; | |
1580 | } | |
1581 | } | |
1582 | fprintf(stderr, "%s: could not launch network script\n", setup_script); | |
1583 | return -1; | |
1584 | } | |
1585 | ||
1586 | static TAPState *net_tap_init(VLANState *vlan, const char *model, | |
1587 | const char *name, const char *ifname1, | |
1588 | const char *setup_script, const char *down_script) | |
1589 | { | |
1590 | TAPState *s; | |
1591 | int fd; | |
1592 | char ifname[128]; | |
1593 | ||
1594 | if (ifname1 != NULL) | |
1595 | pstrcpy(ifname, sizeof(ifname), ifname1); | |
1596 | else | |
1597 | ifname[0] = '\0'; | |
1598 | TFR(fd = tap_open(ifname, sizeof(ifname))); | |
1599 | if (fd < 0) | |
1600 | return NULL; | |
1601 | ||
1602 | if (!setup_script || !strcmp(setup_script, "no")) | |
1603 | setup_script = ""; | |
1604 | if (setup_script[0] != '\0' && | |
1605 | launch_script(setup_script, ifname, fd)) { | |
1606 | return NULL; | |
1607 | } | |
1608 | s = net_tap_fd_init(vlan, model, name, fd); | |
1609 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), | |
1610 | "ifname=%s,script=%s,downscript=%s", | |
1611 | ifname, setup_script, down_script); | |
1612 | if (down_script && strcmp(down_script, "no")) { | |
1613 | snprintf(s->down_script, sizeof(s->down_script), "%s", down_script); | |
1614 | snprintf(s->down_script_arg, sizeof(s->down_script_arg), "%s", ifname); | |
1615 | } | |
1616 | return s; | |
1617 | } | |
1618 | ||
1619 | #endif /* !_WIN32 */ | |
1620 | ||
1621 | #if defined(CONFIG_VDE) | |
1622 | typedef struct VDEState { | |
1623 | VLANClientState *vc; | |
1624 | VDECONN *vde; | |
1625 | } VDEState; | |
1626 | ||
1627 | static void vde_to_qemu(void *opaque) | |
1628 | { | |
1629 | VDEState *s = opaque; | |
1630 | uint8_t buf[4096]; | |
1631 | int size; | |
1632 | ||
1633 | size = vde_recv(s->vde, (char *)buf, sizeof(buf), 0); | |
1634 | if (size > 0) { | |
1635 | qemu_send_packet(s->vc, buf, size); | |
1636 | } | |
1637 | } | |
1638 | ||
1639 | static ssize_t vde_receive(VLANClientState *vc, const uint8_t *buf, size_t size) | |
1640 | { | |
1641 | VDEState *s = vc->opaque; | |
1642 | ssize_t ret; | |
1643 | ||
1644 | do { | |
1645 | ret = vde_send(s->vde, (const char *)buf, size, 0); | |
1646 | } while (ret < 0 && errno == EINTR); | |
1647 | ||
1648 | return ret; | |
1649 | } | |
1650 | ||
1651 | static void vde_cleanup(VLANClientState *vc) | |
1652 | { | |
1653 | VDEState *s = vc->opaque; | |
1654 | qemu_set_fd_handler(vde_datafd(s->vde), NULL, NULL, NULL); | |
1655 | vde_close(s->vde); | |
1656 | qemu_free(s); | |
1657 | } | |
1658 | ||
1659 | static int net_vde_init(VLANState *vlan, const char *model, | |
1660 | const char *name, const char *sock, | |
1661 | int port, const char *group, int mode) | |
1662 | { | |
1663 | VDEState *s; | |
1664 | char *init_group = (char *)group; | |
1665 | char *init_sock = (char *)sock; | |
1666 | ||
1667 | struct vde_open_args args = { | |
1668 | .port = port, | |
1669 | .group = init_group, | |
1670 | .mode = mode, | |
1671 | }; | |
1672 | ||
1673 | s = qemu_mallocz(sizeof(VDEState)); | |
1674 | s->vde = vde_open(init_sock, (char *)"QEMU", &args); | |
1675 | if (!s->vde){ | |
1676 | free(s); | |
1677 | return -1; | |
1678 | } | |
1679 | s->vc = qemu_new_vlan_client(vlan, NULL, model, name, NULL, | |
1680 | vde_receive, NULL, | |
1681 | vde_cleanup, s); | |
1682 | qemu_set_fd_handler(vde_datafd(s->vde), vde_to_qemu, NULL, s); | |
1683 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), "sock=%s,fd=%d", | |
1684 | sock, vde_datafd(s->vde)); | |
1685 | return 0; | |
1686 | } | |
1687 | #endif | |
1688 | ||
1689 | /* network connection */ | |
1690 | typedef struct NetSocketState { | |
1691 | VLANClientState *vc; | |
1692 | int fd; | |
1693 | int state; /* 0 = getting length, 1 = getting data */ | |
1694 | unsigned int index; | |
1695 | unsigned int packet_len; | |
1696 | uint8_t buf[4096]; | |
1697 | struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ | |
1698 | } NetSocketState; | |
1699 | ||
1700 | typedef struct NetSocketListenState { | |
1701 | VLANState *vlan; | |
1702 | char *model; | |
1703 | char *name; | |
1704 | int fd; | |
1705 | } NetSocketListenState; | |
1706 | ||
1707 | /* XXX: we consider we can send the whole packet without blocking */ | |
1708 | static ssize_t net_socket_receive(VLANClientState *vc, const uint8_t *buf, size_t size) | |
1709 | { | |
1710 | NetSocketState *s = vc->opaque; | |
1711 | uint32_t len; | |
1712 | len = htonl(size); | |
1713 | ||
1714 | send_all(s->fd, (const uint8_t *)&len, sizeof(len)); | |
1715 | return send_all(s->fd, buf, size); | |
1716 | } | |
1717 | ||
1718 | static ssize_t net_socket_receive_dgram(VLANClientState *vc, const uint8_t *buf, size_t size) | |
1719 | { | |
1720 | NetSocketState *s = vc->opaque; | |
1721 | ||
1722 | return sendto(s->fd, (const void *)buf, size, 0, | |
1723 | (struct sockaddr *)&s->dgram_dst, sizeof(s->dgram_dst)); | |
1724 | } | |
1725 | ||
1726 | static void net_socket_send(void *opaque) | |
1727 | { | |
1728 | NetSocketState *s = opaque; | |
1729 | int size, err; | |
1730 | unsigned l; | |
1731 | uint8_t buf1[4096]; | |
1732 | const uint8_t *buf; | |
1733 | ||
1734 | size = recv(s->fd, (void *)buf1, sizeof(buf1), 0); | |
1735 | if (size < 0) { | |
1736 | err = socket_error(); | |
1737 | if (err != EWOULDBLOCK) | |
1738 | goto eoc; | |
1739 | } else if (size == 0) { | |
1740 | /* end of connection */ | |
1741 | eoc: | |
1742 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); | |
1743 | closesocket(s->fd); | |
1744 | return; | |
1745 | } | |
1746 | buf = buf1; | |
1747 | while (size > 0) { | |
1748 | /* reassemble a packet from the network */ | |
1749 | switch(s->state) { | |
1750 | case 0: | |
1751 | l = 4 - s->index; | |
1752 | if (l > size) | |
1753 | l = size; | |
1754 | memcpy(s->buf + s->index, buf, l); | |
1755 | buf += l; | |
1756 | size -= l; | |
1757 | s->index += l; | |
1758 | if (s->index == 4) { | |
1759 | /* got length */ | |
1760 | s->packet_len = ntohl(*(uint32_t *)s->buf); | |
1761 | s->index = 0; | |
1762 | s->state = 1; | |
1763 | } | |
1764 | break; | |
1765 | case 1: | |
1766 | l = s->packet_len - s->index; | |
1767 | if (l > size) | |
1768 | l = size; | |
1769 | if (s->index + l <= sizeof(s->buf)) { | |
1770 | memcpy(s->buf + s->index, buf, l); | |
1771 | } else { | |
1772 | fprintf(stderr, "serious error: oversized packet received," | |
1773 | "connection terminated.\n"); | |
1774 | s->state = 0; | |
1775 | goto eoc; | |
1776 | } | |
1777 | ||
1778 | s->index += l; | |
1779 | buf += l; | |
1780 | size -= l; | |
1781 | if (s->index >= s->packet_len) { | |
1782 | qemu_send_packet(s->vc, s->buf, s->packet_len); | |
1783 | s->index = 0; | |
1784 | s->state = 0; | |
1785 | } | |
1786 | break; | |
1787 | } | |
1788 | } | |
1789 | } | |
1790 | ||
1791 | static void net_socket_send_dgram(void *opaque) | |
1792 | { | |
1793 | NetSocketState *s = opaque; | |
1794 | int size; | |
1795 | ||
1796 | size = recv(s->fd, (void *)s->buf, sizeof(s->buf), 0); | |
1797 | if (size < 0) | |
1798 | return; | |
1799 | if (size == 0) { | |
1800 | /* end of connection */ | |
1801 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); | |
1802 | return; | |
1803 | } | |
1804 | qemu_send_packet(s->vc, s->buf, size); | |
1805 | } | |
1806 | ||
1807 | static int net_socket_mcast_create(struct sockaddr_in *mcastaddr) | |
1808 | { | |
1809 | struct ip_mreq imr; | |
1810 | int fd; | |
1811 | int val, ret; | |
1812 | if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) { | |
1813 | fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) does not contain a multicast address\n", | |
1814 | inet_ntoa(mcastaddr->sin_addr), | |
1815 | (int)ntohl(mcastaddr->sin_addr.s_addr)); | |
1816 | return -1; | |
1817 | ||
1818 | } | |
1819 | fd = socket(PF_INET, SOCK_DGRAM, 0); | |
1820 | if (fd < 0) { | |
1821 | perror("socket(PF_INET, SOCK_DGRAM)"); | |
1822 | return -1; | |
1823 | } | |
1824 | ||
1825 | val = 1; | |
1826 | ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, | |
1827 | (const char *)&val, sizeof(val)); | |
1828 | if (ret < 0) { | |
1829 | perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); | |
1830 | goto fail; | |
1831 | } | |
1832 | ||
1833 | ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); | |
1834 | if (ret < 0) { | |
1835 | perror("bind"); | |
1836 | goto fail; | |
1837 | } | |
1838 | ||
1839 | /* Add host to multicast group */ | |
1840 | imr.imr_multiaddr = mcastaddr->sin_addr; | |
1841 | imr.imr_interface.s_addr = htonl(INADDR_ANY); | |
1842 | ||
1843 | ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, | |
1844 | (const char *)&imr, sizeof(struct ip_mreq)); | |
1845 | if (ret < 0) { | |
1846 | perror("setsockopt(IP_ADD_MEMBERSHIP)"); | |
1847 | goto fail; | |
1848 | } | |
1849 | ||
1850 | /* Force mcast msgs to loopback (eg. several QEMUs in same host */ | |
1851 | val = 1; | |
1852 | ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, | |
1853 | (const char *)&val, sizeof(val)); | |
1854 | if (ret < 0) { | |
1855 | perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)"); | |
1856 | goto fail; | |
1857 | } | |
1858 | ||
1859 | socket_set_nonblock(fd); | |
1860 | return fd; | |
1861 | fail: | |
1862 | if (fd >= 0) | |
1863 | closesocket(fd); | |
1864 | return -1; | |
1865 | } | |
1866 | ||
1867 | static void net_socket_cleanup(VLANClientState *vc) | |
1868 | { | |
1869 | NetSocketState *s = vc->opaque; | |
1870 | qemu_set_fd_handler(s->fd, NULL, NULL, NULL); | |
1871 | close(s->fd); | |
1872 | qemu_free(s); | |
1873 | } | |
1874 | ||
1875 | static NetSocketState *net_socket_fd_init_dgram(VLANState *vlan, | |
1876 | const char *model, | |
1877 | const char *name, | |
1878 | int fd, int is_connected) | |
1879 | { | |
1880 | struct sockaddr_in saddr; | |
1881 | int newfd; | |
1882 | socklen_t saddr_len; | |
1883 | NetSocketState *s; | |
1884 | ||
1885 | /* fd passed: multicast: "learn" dgram_dst address from bound address and save it | |
1886 | * Because this may be "shared" socket from a "master" process, datagrams would be recv() | |
1887 | * by ONLY ONE process: we must "clone" this dgram socket --jjo | |
1888 | */ | |
1889 | ||
1890 | if (is_connected) { | |
1891 | if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { | |
1892 | /* must be bound */ | |
1893 | if (saddr.sin_addr.s_addr==0) { | |
1894 | fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, cannot setup multicast dst addr\n", | |
1895 | fd); | |
1896 | return NULL; | |
1897 | } | |
1898 | /* clone dgram socket */ | |
1899 | newfd = net_socket_mcast_create(&saddr); | |
1900 | if (newfd < 0) { | |
1901 | /* error already reported by net_socket_mcast_create() */ | |
1902 | close(fd); | |
1903 | return NULL; | |
1904 | } | |
1905 | /* clone newfd to fd, close newfd */ | |
1906 | dup2(newfd, fd); | |
1907 | close(newfd); | |
1908 | ||
1909 | } else { | |
1910 | fprintf(stderr, "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n", | |
1911 | fd, strerror(errno)); | |
1912 | return NULL; | |
1913 | } | |
1914 | } | |
1915 | ||
1916 | s = qemu_mallocz(sizeof(NetSocketState)); | |
1917 | s->fd = fd; | |
1918 | ||
1919 | s->vc = qemu_new_vlan_client(vlan, NULL, model, name, NULL, | |
1920 | net_socket_receive_dgram, NULL, | |
1921 | net_socket_cleanup, s); | |
1922 | qemu_set_fd_handler(s->fd, net_socket_send_dgram, NULL, s); | |
1923 | ||
1924 | /* mcast: save bound address as dst */ | |
1925 | if (is_connected) s->dgram_dst=saddr; | |
1926 | ||
1927 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), | |
1928 | "socket: fd=%d (%s mcast=%s:%d)", | |
1929 | fd, is_connected? "cloned" : "", | |
1930 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); | |
1931 | return s; | |
1932 | } | |
1933 | ||
1934 | static void net_socket_connect(void *opaque) | |
1935 | { | |
1936 | NetSocketState *s = opaque; | |
1937 | qemu_set_fd_handler(s->fd, net_socket_send, NULL, s); | |
1938 | } | |
1939 | ||
1940 | static NetSocketState *net_socket_fd_init_stream(VLANState *vlan, | |
1941 | const char *model, | |
1942 | const char *name, | |
1943 | int fd, int is_connected) | |
1944 | { | |
1945 | NetSocketState *s; | |
1946 | s = qemu_mallocz(sizeof(NetSocketState)); | |
1947 | s->fd = fd; | |
1948 | s->vc = qemu_new_vlan_client(vlan, NULL, model, name, NULL, | |
1949 | net_socket_receive, NULL, | |
1950 | net_socket_cleanup, s); | |
1951 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), | |
1952 | "socket: fd=%d", fd); | |
1953 | if (is_connected) { | |
1954 | net_socket_connect(s); | |
1955 | } else { | |
1956 | qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s); | |
1957 | } | |
1958 | return s; | |
1959 | } | |
1960 | ||
1961 | static NetSocketState *net_socket_fd_init(VLANState *vlan, | |
1962 | const char *model, const char *name, | |
1963 | int fd, int is_connected) | |
1964 | { | |
1965 | int so_type = -1, optlen=sizeof(so_type); | |
1966 | ||
1967 | if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, | |
1968 | (socklen_t *)&optlen)< 0) { | |
1969 | fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", fd); | |
1970 | return NULL; | |
1971 | } | |
1972 | switch(so_type) { | |
1973 | case SOCK_DGRAM: | |
1974 | return net_socket_fd_init_dgram(vlan, model, name, fd, is_connected); | |
1975 | case SOCK_STREAM: | |
1976 | return net_socket_fd_init_stream(vlan, model, name, fd, is_connected); | |
1977 | default: | |
1978 | /* who knows ... this could be a eg. a pty, do warn and continue as stream */ | |
1979 | fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd); | |
1980 | return net_socket_fd_init_stream(vlan, model, name, fd, is_connected); | |
1981 | } | |
1982 | return NULL; | |
1983 | } | |
1984 | ||
1985 | static void net_socket_accept(void *opaque) | |
1986 | { | |
1987 | NetSocketListenState *s = opaque; | |
1988 | NetSocketState *s1; | |
1989 | struct sockaddr_in saddr; | |
1990 | socklen_t len; | |
1991 | int fd; | |
1992 | ||
1993 | for(;;) { | |
1994 | len = sizeof(saddr); | |
1995 | fd = accept(s->fd, (struct sockaddr *)&saddr, &len); | |
1996 | if (fd < 0 && errno != EINTR) { | |
1997 | return; | |
1998 | } else if (fd >= 0) { | |
1999 | break; | |
2000 | } | |
2001 | } | |
2002 | s1 = net_socket_fd_init(s->vlan, s->model, s->name, fd, 1); | |
2003 | if (!s1) { | |
2004 | closesocket(fd); | |
2005 | } else { | |
2006 | snprintf(s1->vc->info_str, sizeof(s1->vc->info_str), | |
2007 | "socket: connection from %s:%d", | |
2008 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); | |
2009 | } | |
2010 | } | |
2011 | ||
2012 | static int net_socket_listen_init(VLANState *vlan, | |
2013 | const char *model, | |
2014 | const char *name, | |
2015 | const char *host_str) | |
2016 | { | |
2017 | NetSocketListenState *s; | |
2018 | int fd, val, ret; | |
2019 | struct sockaddr_in saddr; | |
2020 | ||
2021 | if (parse_host_port(&saddr, host_str) < 0) | |
2022 | return -1; | |
2023 | ||
2024 | s = qemu_mallocz(sizeof(NetSocketListenState)); | |
2025 | ||
2026 | fd = socket(PF_INET, SOCK_STREAM, 0); | |
2027 | if (fd < 0) { | |
2028 | perror("socket"); | |
2029 | return -1; | |
2030 | } | |
2031 | socket_set_nonblock(fd); | |
2032 | ||
2033 | /* allow fast reuse */ | |
2034 | val = 1; | |
2035 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); | |
2036 | ||
2037 | ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); | |
2038 | if (ret < 0) { | |
2039 | perror("bind"); | |
2040 | return -1; | |
2041 | } | |
2042 | ret = listen(fd, 0); | |
2043 | if (ret < 0) { | |
2044 | perror("listen"); | |
2045 | return -1; | |
2046 | } | |
2047 | s->vlan = vlan; | |
2048 | s->model = qemu_strdup(model); | |
2049 | s->name = name ? qemu_strdup(name) : NULL; | |
2050 | s->fd = fd; | |
2051 | qemu_set_fd_handler(fd, net_socket_accept, NULL, s); | |
2052 | return 0; | |
2053 | } | |
2054 | ||
2055 | static int net_socket_connect_init(VLANState *vlan, | |
2056 | const char *model, | |
2057 | const char *name, | |
2058 | const char *host_str) | |
2059 | { | |
2060 | NetSocketState *s; | |
2061 | int fd, connected, ret, err; | |
2062 | struct sockaddr_in saddr; | |
2063 | ||
2064 | if (parse_host_port(&saddr, host_str) < 0) | |
2065 | return -1; | |
2066 | ||
2067 | fd = socket(PF_INET, SOCK_STREAM, 0); | |
2068 | if (fd < 0) { | |
2069 | perror("socket"); | |
2070 | return -1; | |
2071 | } | |
2072 | socket_set_nonblock(fd); | |
2073 | ||
2074 | connected = 0; | |
2075 | for(;;) { | |
2076 | ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); | |
2077 | if (ret < 0) { | |
2078 | err = socket_error(); | |
2079 | if (err == EINTR || err == EWOULDBLOCK) { | |
2080 | } else if (err == EINPROGRESS) { | |
2081 | break; | |
2082 | #ifdef _WIN32 | |
2083 | } else if (err == WSAEALREADY) { | |
2084 | break; | |
2085 | #endif | |
2086 | } else { | |
2087 | perror("connect"); | |
2088 | closesocket(fd); | |
2089 | return -1; | |
2090 | } | |
2091 | } else { | |
2092 | connected = 1; | |
2093 | break; | |
2094 | } | |
2095 | } | |
2096 | s = net_socket_fd_init(vlan, model, name, fd, connected); | |
2097 | if (!s) | |
2098 | return -1; | |
2099 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), | |
2100 | "socket: connect to %s:%d", | |
2101 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); | |
2102 | return 0; | |
2103 | } | |
2104 | ||
2105 | static int net_socket_mcast_init(VLANState *vlan, | |
2106 | const char *model, | |
2107 | const char *name, | |
2108 | const char *host_str) | |
2109 | { | |
2110 | NetSocketState *s; | |
2111 | int fd; | |
2112 | struct sockaddr_in saddr; | |
2113 | ||
2114 | if (parse_host_port(&saddr, host_str) < 0) | |
2115 | return -1; | |
2116 | ||
2117 | ||
2118 | fd = net_socket_mcast_create(&saddr); | |
2119 | if (fd < 0) | |
2120 | return -1; | |
2121 | ||
2122 | s = net_socket_fd_init(vlan, model, name, fd, 0); | |
2123 | if (!s) | |
2124 | return -1; | |
2125 | ||
2126 | s->dgram_dst = saddr; | |
2127 | ||
2128 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), | |
2129 | "socket: mcast=%s:%d", | |
2130 | inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); | |
2131 | return 0; | |
2132 | ||
2133 | } | |
2134 | ||
2135 | typedef struct DumpState { | |
2136 | VLANClientState *pcap_vc; | |
2137 | int fd; | |
2138 | int pcap_caplen; | |
2139 | } DumpState; | |
2140 | ||
2141 | #define PCAP_MAGIC 0xa1b2c3d4 | |
2142 | ||
2143 | struct pcap_file_hdr { | |
2144 | uint32_t magic; | |
2145 | uint16_t version_major; | |
2146 | uint16_t version_minor; | |
2147 | int32_t thiszone; | |
2148 | uint32_t sigfigs; | |
2149 | uint32_t snaplen; | |
2150 | uint32_t linktype; | |
2151 | }; | |
2152 | ||
2153 | struct pcap_sf_pkthdr { | |
2154 | struct { | |
2155 | int32_t tv_sec; | |
2156 | int32_t tv_usec; | |
2157 | } ts; | |
2158 | uint32_t caplen; | |
2159 | uint32_t len; | |
2160 | }; | |
2161 | ||
2162 | static ssize_t dump_receive(VLANClientState *vc, const uint8_t *buf, size_t size) | |
2163 | { | |
2164 | DumpState *s = vc->opaque; | |
2165 | struct pcap_sf_pkthdr hdr; | |
2166 | int64_t ts; | |
2167 | int caplen; | |
2168 | ||
2169 | /* Early return in case of previous error. */ | |
2170 | if (s->fd < 0) { | |
2171 | return size; | |
2172 | } | |
2173 | ||
2174 | ts = muldiv64(qemu_get_clock(vm_clock), 1000000, get_ticks_per_sec()); | |
2175 | caplen = size > s->pcap_caplen ? s->pcap_caplen : size; | |
2176 | ||
2177 | hdr.ts.tv_sec = ts / 1000000; | |
2178 | hdr.ts.tv_usec = ts % 1000000; | |
2179 | hdr.caplen = caplen; | |
2180 | hdr.len = size; | |
2181 | if (write(s->fd, &hdr, sizeof(hdr)) != sizeof(hdr) || | |
2182 | write(s->fd, buf, caplen) != caplen) { | |
2183 | qemu_log("-net dump write error - stop dump\n"); | |
2184 | close(s->fd); | |
2185 | s->fd = -1; | |
2186 | } | |
2187 | ||
2188 | return size; | |
2189 | } | |
2190 | ||
2191 | static void net_dump_cleanup(VLANClientState *vc) | |
2192 | { | |
2193 | DumpState *s = vc->opaque; | |
2194 | ||
2195 | close(s->fd); | |
2196 | qemu_free(s); | |
2197 | } | |
2198 | ||
2199 | static int net_dump_init(VLANState *vlan, const char *device, | |
2200 | const char *name, const char *filename, int len) | |
2201 | { | |
2202 | struct pcap_file_hdr hdr; | |
2203 | DumpState *s; | |
2204 | ||
2205 | s = qemu_malloc(sizeof(DumpState)); | |
2206 | ||
2207 | s->fd = open(filename, O_CREAT | O_WRONLY | O_BINARY, 0644); | |
2208 | if (s->fd < 0) { | |
2209 | qemu_error("-net dump: can't open %s\n", filename); | |
2210 | return -1; | |
2211 | } | |
2212 | ||
2213 | s->pcap_caplen = len; | |
2214 | ||
2215 | hdr.magic = PCAP_MAGIC; | |
2216 | hdr.version_major = 2; | |
2217 | hdr.version_minor = 4; | |
2218 | hdr.thiszone = 0; | |
2219 | hdr.sigfigs = 0; | |
2220 | hdr.snaplen = s->pcap_caplen; | |
2221 | hdr.linktype = 1; | |
2222 | ||
2223 | if (write(s->fd, &hdr, sizeof(hdr)) < sizeof(hdr)) { | |
2224 | qemu_error("-net dump write error: %s\n", strerror(errno)); | |
2225 | close(s->fd); | |
2226 | qemu_free(s); | |
2227 | return -1; | |
2228 | } | |
2229 | ||
2230 | s->pcap_vc = qemu_new_vlan_client(vlan, NULL, device, name, NULL, | |
2231 | dump_receive, NULL, | |
2232 | net_dump_cleanup, s); | |
2233 | snprintf(s->pcap_vc->info_str, sizeof(s->pcap_vc->info_str), | |
2234 | "dump to %s (len=%d)", filename, len); | |
2235 | return 0; | |
2236 | } | |
2237 | ||
2238 | /* find or alloc a new VLAN */ | |
2239 | VLANState *qemu_find_vlan(int id, int allocate) | |
2240 | { | |
2241 | VLANState *vlan; | |
2242 | ||
2243 | QTAILQ_FOREACH(vlan, &vlans, next) { | |
2244 | if (vlan->id == id) { | |
2245 | return vlan; | |
2246 | } | |
2247 | } | |
2248 | ||
2249 | if (!allocate) { | |
2250 | return NULL; | |
2251 | } | |
2252 | ||
2253 | vlan = qemu_mallocz(sizeof(VLANState)); | |
2254 | vlan->id = id; | |
2255 | QTAILQ_INIT(&vlan->clients); | |
2256 | ||
2257 | vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet, | |
2258 | qemu_vlan_deliver_packet_iov, | |
2259 | vlan); | |
2260 | ||
2261 | QTAILQ_INSERT_TAIL(&vlans, vlan, next); | |
2262 | ||
2263 | return vlan; | |
2264 | } | |
2265 | ||
2266 | static VLANClientState *qemu_find_netdev(const char *id) | |
2267 | { | |
2268 | VLANClientState *vc; | |
2269 | ||
2270 | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { | |
2271 | if (!strcmp(vc->name, id)) { | |
2272 | return vc; | |
2273 | } | |
2274 | } | |
2275 | ||
2276 | return NULL; | |
2277 | } | |
2278 | ||
2279 | static int nic_get_free_idx(void) | |
2280 | { | |
2281 | int index; | |
2282 | ||
2283 | for (index = 0; index < MAX_NICS; index++) | |
2284 | if (!nd_table[index].used) | |
2285 | return index; | |
2286 | return -1; | |
2287 | } | |
2288 | ||
2289 | int qemu_show_nic_models(const char *arg, const char *const *models) | |
2290 | { | |
2291 | int i; | |
2292 | ||
2293 | if (!arg || strcmp(arg, "?")) | |
2294 | return 0; | |
2295 | ||
2296 | fprintf(stderr, "qemu: Supported NIC models: "); | |
2297 | for (i = 0 ; models[i]; i++) | |
2298 | fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n'); | |
2299 | return 1; | |
2300 | } | |
2301 | ||
2302 | void qemu_check_nic_model(NICInfo *nd, const char *model) | |
2303 | { | |
2304 | const char *models[2]; | |
2305 | ||
2306 | models[0] = model; | |
2307 | models[1] = NULL; | |
2308 | ||
2309 | if (qemu_show_nic_models(nd->model, models)) | |
2310 | exit(0); | |
2311 | if (qemu_find_nic_model(nd, models, model) < 0) | |
2312 | exit(1); | |
2313 | } | |
2314 | ||
2315 | int qemu_find_nic_model(NICInfo *nd, const char * const *models, | |
2316 | const char *default_model) | |
2317 | { | |
2318 | int i; | |
2319 | ||
2320 | if (!nd->model) | |
2321 | nd->model = qemu_strdup(default_model); | |
2322 | ||
2323 | for (i = 0 ; models[i]; i++) { | |
2324 | if (strcmp(nd->model, models[i]) == 0) | |
2325 | return i; | |
2326 | } | |
2327 | ||
2328 | qemu_error("qemu: Unsupported NIC model: %s\n", nd->model); | |
2329 | return -1; | |
2330 | } | |
2331 | ||
2332 | static int net_handle_fd_param(Monitor *mon, const char *param) | |
2333 | { | |
2334 | if (!qemu_isdigit(param[0])) { | |
2335 | int fd; | |
2336 | ||
2337 | fd = monitor_get_fd(mon, param); | |
2338 | if (fd == -1) { | |
2339 | qemu_error("No file descriptor named %s found", param); | |
2340 | return -1; | |
2341 | } | |
2342 | ||
2343 | return fd; | |
2344 | } else { | |
2345 | return strtol(param, NULL, 0); | |
2346 | } | |
2347 | } | |
2348 | ||
2349 | static int net_init_nic(QemuOpts *opts, | |
2350 | Monitor *mon, | |
2351 | const char *name, | |
2352 | VLANState *vlan) | |
2353 | { | |
2354 | int idx; | |
2355 | NICInfo *nd; | |
2356 | const char *netdev; | |
2357 | ||
2358 | idx = nic_get_free_idx(); | |
2359 | if (idx == -1 || nb_nics >= MAX_NICS) { | |
2360 | qemu_error("Too Many NICs\n"); | |
2361 | return -1; | |
2362 | } | |
2363 | ||
2364 | nd = &nd_table[idx]; | |
2365 | ||
2366 | memset(nd, 0, sizeof(*nd)); | |
2367 | ||
2368 | if ((netdev = qemu_opt_get(opts, "netdev"))) { | |
2369 | nd->netdev = qemu_find_netdev(netdev); | |
2370 | if (!nd->netdev) { | |
2371 | qemu_error("netdev '%s' not found\n", netdev); | |
2372 | return -1; | |
2373 | } | |
2374 | } else { | |
2375 | assert(vlan); | |
2376 | nd->vlan = vlan; | |
2377 | } | |
2378 | if (name) { | |
2379 | nd->name = qemu_strdup(name); | |
2380 | } | |
2381 | if (qemu_opt_get(opts, "model")) { | |
2382 | nd->model = qemu_strdup(qemu_opt_get(opts, "model")); | |
2383 | } | |
2384 | if (qemu_opt_get(opts, "addr")) { | |
2385 | nd->devaddr = qemu_strdup(qemu_opt_get(opts, "addr")); | |
2386 | } | |
2387 | ||
2388 | nd->macaddr[0] = 0x52; | |
2389 | nd->macaddr[1] = 0x54; | |
2390 | nd->macaddr[2] = 0x00; | |
2391 | nd->macaddr[3] = 0x12; | |
2392 | nd->macaddr[4] = 0x34; | |
2393 | nd->macaddr[5] = 0x56 + idx; | |
2394 | ||
2395 | if (qemu_opt_get(opts, "macaddr") && | |
2396 | parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) { | |
2397 | qemu_error("invalid syntax for ethernet address\n"); | |
2398 | return -1; | |
2399 | } | |
2400 | ||
2401 | nd->nvectors = qemu_opt_get_number(opts, "vectors", NIC_NVECTORS_UNSPECIFIED); | |
2402 | if (nd->nvectors != NIC_NVECTORS_UNSPECIFIED && | |
2403 | (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) { | |
2404 | qemu_error("invalid # of vectors: %d\n", nd->nvectors); | |
2405 | return -1; | |
2406 | } | |
2407 | ||
2408 | nd->used = 1; | |
2409 | if (vlan) { | |
2410 | nd->vlan->nb_guest_devs++; | |
2411 | } | |
2412 | nb_nics++; | |
2413 | ||
2414 | return idx; | |
2415 | } | |
2416 | ||
2417 | #if defined(CONFIG_SLIRP) | |
2418 | static int net_init_slirp_configs(const char *name, const char *value, void *opaque) | |
2419 | { | |
2420 | struct slirp_config_str *config; | |
2421 | ||
2422 | if (strcmp(name, "hostfwd") != 0 && strcmp(name, "guestfwd") != 0) { | |
2423 | return 0; | |
2424 | } | |
2425 | ||
2426 | config = qemu_mallocz(sizeof(*config)); | |
2427 | ||
2428 | pstrcpy(config->str, sizeof(config->str), value); | |
2429 | ||
2430 | if (!strcmp(name, "hostfwd")) { | |
2431 | config->flags = SLIRP_CFG_HOSTFWD; | |
2432 | } | |
2433 | ||
2434 | config->next = slirp_configs; | |
2435 | slirp_configs = config; | |
2436 | ||
2437 | return 0; | |
2438 | } | |
2439 | ||
2440 | static int net_init_slirp(QemuOpts *opts, | |
2441 | Monitor *mon, | |
2442 | const char *name, | |
2443 | VLANState *vlan) | |
2444 | { | |
2445 | struct slirp_config_str *config; | |
2446 | const char *vhost; | |
2447 | const char *vhostname; | |
2448 | const char *vdhcp_start; | |
2449 | const char *vnamesrv; | |
2450 | const char *tftp_export; | |
2451 | const char *bootfile; | |
2452 | const char *smb_export; | |
2453 | const char *vsmbsrv; | |
2454 | char *vnet = NULL; | |
2455 | int restricted = 0; | |
2456 | int ret; | |
2457 | ||
2458 | vhost = qemu_opt_get(opts, "host"); | |
2459 | vhostname = qemu_opt_get(opts, "hostname"); | |
2460 | vdhcp_start = qemu_opt_get(opts, "dhcpstart"); | |
2461 | vnamesrv = qemu_opt_get(opts, "dns"); | |
2462 | tftp_export = qemu_opt_get(opts, "tftp"); | |
2463 | bootfile = qemu_opt_get(opts, "bootfile"); | |
2464 | smb_export = qemu_opt_get(opts, "smb"); | |
2465 | vsmbsrv = qemu_opt_get(opts, "smbserver"); | |
2466 | ||
2467 | if (qemu_opt_get(opts, "ip")) { | |
2468 | const char *ip = qemu_opt_get(opts, "ip"); | |
2469 | int l = strlen(ip) + strlen("/24") + 1; | |
2470 | ||
2471 | vnet = qemu_malloc(l); | |
2472 | ||
2473 | /* emulate legacy ip= parameter */ | |
2474 | pstrcpy(vnet, l, ip); | |
2475 | pstrcat(vnet, l, "/24"); | |
2476 | } | |
2477 | ||
2478 | if (qemu_opt_get(opts, "net")) { | |
2479 | if (vnet) { | |
2480 | qemu_free(vnet); | |
2481 | } | |
2482 | vnet = qemu_strdup(qemu_opt_get(opts, "net")); | |
2483 | } | |
2484 | ||
2485 | if (qemu_opt_get(opts, "restrict") && | |
2486 | qemu_opt_get(opts, "restrict")[0] == 'y') { | |
2487 | restricted = 1; | |
2488 | } | |
2489 | ||
2490 | qemu_opt_foreach(opts, net_init_slirp_configs, NULL, 0); | |
2491 | ||
2492 | ret = net_slirp_init(vlan, "user", name, restricted, vnet, vhost, | |
2493 | vhostname, tftp_export, bootfile, vdhcp_start, | |
2494 | vnamesrv, smb_export, vsmbsrv); | |
2495 | ||
2496 | while (slirp_configs) { | |
2497 | config = slirp_configs; | |
2498 | slirp_configs = config->next; | |
2499 | qemu_free(config); | |
2500 | } | |
2501 | ||
2502 | if (ret != -1 && vlan) { | |
2503 | vlan->nb_host_devs++; | |
2504 | } | |
2505 | ||
2506 | qemu_free(vnet); | |
2507 | ||
2508 | return ret; | |
2509 | } | |
2510 | #endif /* CONFIG_SLIRP */ | |
2511 | ||
2512 | #ifdef _WIN32 | |
2513 | static int net_init_tap_win32(QemuOpts *opts, | |
2514 | Monitor *mon, | |
2515 | const char *name, | |
2516 | VLANState *vlan) | |
2517 | { | |
2518 | const char *ifname; | |
2519 | ||
2520 | ifname = qemu_opt_get(opts, "ifname"); | |
2521 | ||
2522 | if (!ifname) { | |
2523 | qemu_error("tap: no interface name\n"); | |
2524 | return -1; | |
2525 | } | |
2526 | ||
2527 | if (tap_win32_init(vlan, "tap", name, ifname) == -1) { | |
2528 | return -1; | |
2529 | } | |
2530 | ||
2531 | if (vlan) { | |
2532 | vlan->nb_host_devs++; | |
2533 | } | |
2534 | ||
2535 | return 0; | |
2536 | } | |
2537 | #elif !defined(_AIX) | |
2538 | static int net_init_tap(QemuOpts *opts, | |
2539 | Monitor *mon, | |
2540 | const char *name, | |
2541 | VLANState *vlan) | |
2542 | { | |
2543 | TAPState *s; | |
2544 | ||
2545 | if (qemu_opt_get(opts, "fd")) { | |
2546 | int fd; | |
2547 | ||
2548 | if (qemu_opt_get(opts, "ifname") || | |
2549 | qemu_opt_get(opts, "script") || | |
2550 | qemu_opt_get(opts, "downscript")) { | |
2551 | qemu_error("ifname=, script= and downscript= is invalid with fd=\n"); | |
2552 | return -1; | |
2553 | } | |
2554 | ||
2555 | fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd")); | |
2556 | if (fd == -1) { | |
2557 | return -1; | |
2558 | } | |
2559 | ||
2560 | fcntl(fd, F_SETFL, O_NONBLOCK); | |
2561 | ||
2562 | s = net_tap_fd_init(vlan, "tap", name, fd); | |
2563 | if (!s) { | |
2564 | close(fd); | |
2565 | } | |
2566 | } else { | |
2567 | const char *ifname, *script, *downscript; | |
2568 | ||
2569 | ifname = qemu_opt_get(opts, "ifname"); | |
2570 | script = qemu_opt_get(opts, "script"); | |
2571 | downscript = qemu_opt_get(opts, "downscript"); | |
2572 | ||
2573 | if (!script) { | |
2574 | script = DEFAULT_NETWORK_SCRIPT; | |
2575 | } | |
2576 | if (!downscript) { | |
2577 | downscript = DEFAULT_NETWORK_DOWN_SCRIPT; | |
2578 | } | |
2579 | ||
2580 | s = net_tap_init(vlan, "tap", name, ifname, script, downscript); | |
2581 | } | |
2582 | ||
2583 | if (!s) { | |
2584 | return -1; | |
2585 | } | |
2586 | ||
2587 | if (tap_set_sndbuf(s, opts) < 0) { | |
2588 | return -1; | |
2589 | } | |
2590 | ||
2591 | if (vlan) { | |
2592 | vlan->nb_host_devs++; | |
2593 | } | |
2594 | ||
2595 | return 0; | |
2596 | } | |
2597 | #endif | |
2598 | ||
2599 | static int net_init_socket(QemuOpts *opts, | |
2600 | Monitor *mon, | |
2601 | const char *name, | |
2602 | VLANState *vlan) | |
2603 | { | |
2604 | if (qemu_opt_get(opts, "fd")) { | |
2605 | int fd; | |
2606 | ||
2607 | if (qemu_opt_get(opts, "listen") || | |
2608 | qemu_opt_get(opts, "connect") || | |
2609 | qemu_opt_get(opts, "mcast")) { | |
2610 | qemu_error("listen=, connect= and mcast= is invalid with fd=\n"); | |
2611 | return -1; | |
2612 | } | |
2613 | ||
2614 | fd = net_handle_fd_param(mon, qemu_opt_get(opts, "fd")); | |
2615 | if (fd == -1) { | |
2616 | return -1; | |
2617 | } | |
2618 | ||
2619 | if (!net_socket_fd_init(vlan, "socket", name, fd, 1)) { | |
2620 | close(fd); | |
2621 | return -1; | |
2622 | } | |
2623 | } else if (qemu_opt_get(opts, "listen")) { | |
2624 | const char *listen; | |
2625 | ||
2626 | if (qemu_opt_get(opts, "fd") || | |
2627 | qemu_opt_get(opts, "connect") || | |
2628 | qemu_opt_get(opts, "mcast")) { | |
2629 | qemu_error("fd=, connect= and mcast= is invalid with listen=\n"); | |
2630 | return -1; | |
2631 | } | |
2632 | ||
2633 | listen = qemu_opt_get(opts, "listen"); | |
2634 | ||
2635 | if (net_socket_listen_init(vlan, "socket", name, listen) == -1) { | |
2636 | return -1; | |
2637 | } | |
2638 | } else if (qemu_opt_get(opts, "connect")) { | |
2639 | const char *connect; | |
2640 | ||
2641 | if (qemu_opt_get(opts, "fd") || | |
2642 | qemu_opt_get(opts, "listen") || | |
2643 | qemu_opt_get(opts, "mcast")) { | |
2644 | qemu_error("fd=, listen= and mcast= is invalid with connect=\n"); | |
2645 | return -1; | |
2646 | } | |
2647 | ||
2648 | connect = qemu_opt_get(opts, "connect"); | |
2649 | ||
2650 | if (net_socket_connect_init(vlan, "socket", name, connect) == -1) { | |
2651 | return -1; | |
2652 | } | |
2653 | } else if (qemu_opt_get(opts, "mcast")) { | |
2654 | const char *mcast; | |
2655 | ||
2656 | if (qemu_opt_get(opts, "fd") || | |
2657 | qemu_opt_get(opts, "connect") || | |
2658 | qemu_opt_get(opts, "listen")) { | |
2659 | qemu_error("fd=, connect= and listen= is invalid with mcast=\n"); | |
2660 | return -1; | |
2661 | } | |
2662 | ||
2663 | mcast = qemu_opt_get(opts, "mcast"); | |
2664 | ||
2665 | if (net_socket_mcast_init(vlan, "socket", name, mcast) == -1) { | |
2666 | return -1; | |
2667 | } | |
2668 | } else { | |
2669 | qemu_error("-socket requires fd=, listen=, connect= or mcast=\n"); | |
2670 | return -1; | |
2671 | } | |
2672 | ||
2673 | if (vlan) { | |
2674 | vlan->nb_host_devs++; | |
2675 | } | |
2676 | ||
2677 | return 0; | |
2678 | } | |
2679 | ||
2680 | #ifdef CONFIG_VDE | |
2681 | static int net_init_vde(QemuOpts *opts, | |
2682 | Monitor *mon, | |
2683 | const char *name, | |
2684 | VLANState *vlan) | |
2685 | { | |
2686 | const char *sock; | |
2687 | const char *group; | |
2688 | int port, mode; | |
2689 | ||
2690 | sock = qemu_opt_get(opts, "sock"); | |
2691 | group = qemu_opt_get(opts, "group"); | |
2692 | ||
2693 | port = qemu_opt_get_number(opts, "port", 0); | |
2694 | mode = qemu_opt_get_number(opts, "mode", 0700); | |
2695 | ||
2696 | if (net_vde_init(vlan, "vde", name, sock, port, group, mode) == -1) { | |
2697 | return -1; | |
2698 | } | |
2699 | ||
2700 | if (vlan) { | |
2701 | vlan->nb_host_devs++; | |
2702 | } | |
2703 | ||
2704 | return 0; | |
2705 | } | |
2706 | #endif | |
2707 | ||
2708 | static int net_init_dump(QemuOpts *opts, | |
2709 | Monitor *mon, | |
2710 | const char *name, | |
2711 | VLANState *vlan) | |
2712 | { | |
2713 | int len; | |
2714 | const char *file; | |
2715 | char def_file[128]; | |
2716 | ||
2717 | assert(vlan); | |
2718 | ||
2719 | file = qemu_opt_get(opts, "file"); | |
2720 | if (!file) { | |
2721 | snprintf(def_file, sizeof(def_file), "qemu-vlan%d.pcap", vlan->id); | |
2722 | file = def_file; | |
2723 | } | |
2724 | ||
2725 | len = qemu_opt_get_size(opts, "len", 65536); | |
2726 | ||
2727 | return net_dump_init(vlan, "dump", name, file, len); | |
2728 | } | |
2729 | ||
2730 | #define NET_COMMON_PARAMS_DESC \ | |
2731 | { \ | |
2732 | .name = "type", \ | |
2733 | .type = QEMU_OPT_STRING, \ | |
2734 | .help = "net client type (nic, tap etc.)", \ | |
2735 | }, { \ | |
2736 | .name = "vlan", \ | |
2737 | .type = QEMU_OPT_NUMBER, \ | |
2738 | .help = "vlan number", \ | |
2739 | }, { \ | |
2740 | .name = "name", \ | |
2741 | .type = QEMU_OPT_STRING, \ | |
2742 | .help = "identifier for monitor commands", \ | |
2743 | } | |
2744 | ||
2745 | typedef int (*net_client_init_func)(QemuOpts *opts, | |
2746 | Monitor *mon, | |
2747 | const char *name, | |
2748 | VLANState *vlan); | |
2749 | ||
2750 | /* magic number, but compiler will warn if too small */ | |
2751 | #define NET_MAX_DESC 20 | |
2752 | ||
2753 | static struct { | |
2754 | const char *type; | |
2755 | net_client_init_func init; | |
2756 | QemuOptDesc desc[NET_MAX_DESC]; | |
2757 | } net_client_types[] = { | |
2758 | { | |
2759 | .type = "none", | |
2760 | .desc = { | |
2761 | NET_COMMON_PARAMS_DESC, | |
2762 | { /* end of list */ } | |
2763 | }, | |
2764 | }, { | |
2765 | .type = "nic", | |
2766 | .init = net_init_nic, | |
2767 | .desc = { | |
2768 | NET_COMMON_PARAMS_DESC, | |
2769 | { | |
2770 | .name = "netdev", | |
2771 | .type = QEMU_OPT_STRING, | |
2772 | .help = "id of -netdev to connect to", | |
2773 | }, | |
2774 | { | |
2775 | .name = "macaddr", | |
2776 | .type = QEMU_OPT_STRING, | |
2777 | .help = "MAC address", | |
2778 | }, { | |
2779 | .name = "model", | |
2780 | .type = QEMU_OPT_STRING, | |
2781 | .help = "device model (e1000, rtl8139, virtio etc.)", | |
2782 | }, { | |
2783 | .name = "addr", | |
2784 | .type = QEMU_OPT_STRING, | |
2785 | .help = "PCI device address", | |
2786 | }, { | |
2787 | .name = "vectors", | |
2788 | .type = QEMU_OPT_NUMBER, | |
2789 | .help = "number of MSI-x vectors, 0 to disable MSI-X", | |
2790 | }, | |
2791 | { /* end of list */ } | |
2792 | }, | |
2793 | #ifdef CONFIG_SLIRP | |
2794 | }, { | |
2795 | .type = "user", | |
2796 | .init = net_init_slirp, | |
2797 | .desc = { | |
2798 | NET_COMMON_PARAMS_DESC, | |
2799 | { | |
2800 | .name = "hostname", | |
2801 | .type = QEMU_OPT_STRING, | |
2802 | .help = "client hostname reported by the builtin DHCP server", | |
2803 | }, { | |
2804 | .name = "restrict", | |
2805 | .type = QEMU_OPT_STRING, | |
2806 | .help = "isolate the guest from the host (y|yes|n|no)", | |
2807 | }, { | |
2808 | .name = "ip", | |
2809 | .type = QEMU_OPT_STRING, | |
2810 | .help = "legacy parameter, use net= instead", | |
2811 | }, { | |
2812 | .name = "net", | |
2813 | .type = QEMU_OPT_STRING, | |
2814 | .help = "IP address and optional netmask", | |
2815 | }, { | |
2816 | .name = "host", | |
2817 | .type = QEMU_OPT_STRING, | |
2818 | .help = "guest-visible address of the host", | |
2819 | }, { | |
2820 | .name = "tftp", | |
2821 | .type = QEMU_OPT_STRING, | |
2822 | .help = "root directory of the built-in TFTP server", | |
2823 | }, { | |
2824 | .name = "bootfile", | |
2825 | .type = QEMU_OPT_STRING, | |
2826 | .help = "BOOTP filename, for use with tftp=", | |
2827 | }, { | |
2828 | .name = "dhcpstart", | |
2829 | .type = QEMU_OPT_STRING, | |
2830 | .help = "the first of the 16 IPs the built-in DHCP server can assign", | |
2831 | }, { | |
2832 | .name = "dns", | |
2833 | .type = QEMU_OPT_STRING, | |
2834 | .help = "guest-visible address of the virtual nameserver", | |
2835 | }, { | |
2836 | .name = "smb", | |
2837 | .type = QEMU_OPT_STRING, | |
2838 | .help = "root directory of the built-in SMB server", | |
2839 | }, { | |
2840 | .name = "smbserver", | |
2841 | .type = QEMU_OPT_STRING, | |
2842 | .help = "IP address of the built-in SMB server", | |
2843 | }, { | |
2844 | .name = "hostfwd", | |
2845 | .type = QEMU_OPT_STRING, | |
2846 | .help = "guest port number to forward incoming TCP or UDP connections", | |
2847 | }, { | |
2848 | .name = "guestfwd", | |
2849 | .type = QEMU_OPT_STRING, | |
2850 | .help = "IP address and port to forward guest TCP connections", | |
2851 | }, | |
2852 | { /* end of list */ } | |
2853 | }, | |
2854 | #endif | |
2855 | #ifdef _WIN32 | |
2856 | }, { | |
2857 | .type = "tap", | |
2858 | .init = net_init_tap_win32, | |
2859 | .desc = { | |
2860 | NET_COMMON_PARAMS_DESC, | |
2861 | { | |
2862 | .name = "ifname", | |
2863 | .type = QEMU_OPT_STRING, | |
2864 | .help = "interface name", | |
2865 | }, | |
2866 | { /* end of list */ } | |
2867 | }, | |
2868 | #elif !defined(_AIX) | |
2869 | }, { | |
2870 | .type = "tap", | |
2871 | .init = net_init_tap, | |
2872 | .desc = { | |
2873 | NET_COMMON_PARAMS_DESC, | |
2874 | { | |
2875 | .name = "fd", | |
2876 | .type = QEMU_OPT_STRING, | |
2877 | .help = "file descriptor of an already opened tap", | |
2878 | }, { | |
2879 | .name = "ifname", | |
2880 | .type = QEMU_OPT_STRING, | |
2881 | .help = "interface name", | |
2882 | }, { | |
2883 | .name = "script", | |
2884 | .type = QEMU_OPT_STRING, | |
2885 | .help = "script to initialize the interface", | |
2886 | }, { | |
2887 | .name = "downscript", | |
2888 | .type = QEMU_OPT_STRING, | |
2889 | .help = "script to shut down the interface", | |
2890 | #ifdef TUNSETSNDBUF | |
2891 | }, { | |
2892 | .name = "sndbuf", | |
2893 | .type = QEMU_OPT_SIZE, | |
2894 | .help = "send buffer limit" | |
2895 | #endif | |
2896 | }, | |
2897 | { /* end of list */ } | |
2898 | }, | |
2899 | #endif | |
2900 | }, { | |
2901 | .type = "socket", | |
2902 | .init = net_init_socket, | |
2903 | .desc = { | |
2904 | NET_COMMON_PARAMS_DESC, | |
2905 | { | |
2906 | .name = "fd", | |
2907 | .type = QEMU_OPT_STRING, | |
2908 | .help = "file descriptor of an already opened socket", | |
2909 | }, { | |
2910 | .name = "listen", | |
2911 | .type = QEMU_OPT_STRING, | |
2912 | .help = "port number, and optional hostname, to listen on", | |
2913 | }, { | |
2914 | .name = "connect", | |
2915 | .type = QEMU_OPT_STRING, | |
2916 | .help = "port number, and optional hostname, to connect to", | |
2917 | }, { | |
2918 | .name = "mcast", | |
2919 | .type = QEMU_OPT_STRING, | |
2920 | .help = "UDP multicast address and port number", | |
2921 | }, | |
2922 | { /* end of list */ } | |
2923 | }, | |
2924 | #ifdef CONFIG_VDE | |
2925 | }, { | |
2926 | .type = "vde", | |
2927 | .init = net_init_vde, | |
2928 | .desc = { | |
2929 | NET_COMMON_PARAMS_DESC, | |
2930 | { | |
2931 | .name = "sock", | |
2932 | .type = QEMU_OPT_STRING, | |
2933 | .help = "socket path", | |
2934 | }, { | |
2935 | .name = "port", | |
2936 | .type = QEMU_OPT_NUMBER, | |
2937 | .help = "port number", | |
2938 | }, { | |
2939 | .name = "group", | |
2940 | .type = QEMU_OPT_STRING, | |
2941 | .help = "group owner of socket", | |
2942 | }, { | |
2943 | .name = "mode", | |
2944 | .type = QEMU_OPT_NUMBER, | |
2945 | .help = "permissions for socket", | |
2946 | }, | |
2947 | { /* end of list */ } | |
2948 | }, | |
2949 | #endif | |
2950 | }, { | |
2951 | .type = "dump", | |
2952 | .init = net_init_dump, | |
2953 | .desc = { | |
2954 | NET_COMMON_PARAMS_DESC, | |
2955 | { | |
2956 | .name = "len", | |
2957 | .type = QEMU_OPT_SIZE, | |
2958 | .help = "per-packet size limit (64k default)", | |
2959 | }, { | |
2960 | .name = "file", | |
2961 | .type = QEMU_OPT_STRING, | |
2962 | .help = "dump file path (default is qemu-vlan0.pcap)", | |
2963 | }, | |
2964 | { /* end of list */ } | |
2965 | }, | |
2966 | }, | |
2967 | { /* end of list */ } | |
2968 | }; | |
2969 | ||
2970 | int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev) | |
2971 | { | |
2972 | const char *name; | |
2973 | const char *type; | |
2974 | int i; | |
2975 | ||
2976 | type = qemu_opt_get(opts, "type"); | |
2977 | if (!type) { | |
2978 | qemu_error("No type specified for -net\n"); | |
2979 | return -1; | |
2980 | } | |
2981 | ||
2982 | if (is_netdev) { | |
2983 | if (strcmp(type, "tap") != 0 && | |
2984 | #ifdef CONFIG_SLIRP | |
2985 | strcmp(type, "user") != 0 && | |
2986 | #endif | |
2987 | #ifdef CONFIG_VDE | |
2988 | strcmp(type, "vde") != 0 && | |
2989 | #endif | |
2990 | strcmp(type, "socket") != 0) { | |
2991 | qemu_error("The '%s' network backend type is not valid with -netdev\n", | |
2992 | type); | |
2993 | return -1; | |
2994 | } | |
2995 | ||
2996 | if (qemu_opt_get(opts, "vlan")) { | |
2997 | qemu_error("The 'vlan' parameter is not valid with -netdev\n"); | |
2998 | return -1; | |
2999 | } | |
3000 | if (qemu_opt_get(opts, "name")) { | |
3001 | qemu_error("The 'name' parameter is not valid with -netdev\n"); | |
3002 | return -1; | |
3003 | } | |
3004 | if (!qemu_opts_id(opts)) { | |
3005 | qemu_error("The id= parameter is required with -netdev\n"); | |
3006 | return -1; | |
3007 | } | |
3008 | } | |
3009 | ||
3010 | name = qemu_opts_id(opts); | |
3011 | if (!name) { | |
3012 | name = qemu_opt_get(opts, "name"); | |
3013 | } | |
3014 | ||
3015 | for (i = 0; net_client_types[i].type != NULL; i++) { | |
3016 | if (!strcmp(net_client_types[i].type, type)) { | |
3017 | VLANState *vlan = NULL; | |
3018 | ||
3019 | if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) { | |
3020 | return -1; | |
3021 | } | |
3022 | ||
3023 | /* Do not add to a vlan if it's a -netdev or a nic with a | |
3024 | * netdev= parameter. */ | |
3025 | if (!(is_netdev || | |
3026 | (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) { | |
3027 | vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1); | |
3028 | } | |
3029 | ||
3030 | if (net_client_types[i].init) { | |
3031 | return net_client_types[i].init(opts, mon, name, vlan); | |
3032 | } else { | |
3033 | return 0; | |
3034 | } | |
3035 | } | |
3036 | } | |
3037 | ||
3038 | qemu_error("Invalid -net type '%s'\n", type); | |
3039 | return -1; | |
3040 | } | |
3041 | ||
3042 | void net_client_uninit(NICInfo *nd) | |
3043 | { | |
3044 | if (nd->vlan) { | |
3045 | nd->vlan->nb_guest_devs--; | |
3046 | } | |
3047 | nb_nics--; | |
3048 | ||
3049 | qemu_free(nd->model); | |
3050 | qemu_free(nd->name); | |
3051 | qemu_free(nd->devaddr); | |
3052 | ||
3053 | nd->used = 0; | |
3054 | } | |
3055 | ||
3056 | static int net_host_check_device(const char *device) | |
3057 | { | |
3058 | int i; | |
3059 | const char *valid_param_list[] = { "tap", "socket", "dump" | |
3060 | #ifdef CONFIG_SLIRP | |
3061 | ,"user" | |
3062 | #endif | |
3063 | #ifdef CONFIG_VDE | |
3064 | ,"vde" | |
3065 | #endif | |
3066 | }; | |
3067 | for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) { | |
3068 | if (!strncmp(valid_param_list[i], device, | |
3069 | strlen(valid_param_list[i]))) | |
3070 | return 1; | |
3071 | } | |
3072 | ||
3073 | return 0; | |
3074 | } | |
3075 | ||
3076 | void net_host_device_add(Monitor *mon, const QDict *qdict) | |
3077 | { | |
3078 | const char *device = qdict_get_str(qdict, "device"); | |
3079 | const char *opts_str = qdict_get_try_str(qdict, "opts"); | |
3080 | QemuOpts *opts; | |
3081 | ||
3082 | if (!net_host_check_device(device)) { | |
3083 | monitor_printf(mon, "invalid host network device %s\n", device); | |
3084 | return; | |
3085 | } | |
3086 | ||
3087 | opts = qemu_opts_parse(&qemu_net_opts, opts_str ? opts_str : "", NULL); | |
3088 | if (!opts) { | |
3089 | monitor_printf(mon, "parsing network options '%s' failed\n", | |
3090 | opts_str ? opts_str : ""); | |
3091 | return; | |
3092 | } | |
3093 | ||
3094 | qemu_opt_set(opts, "type", device); | |
3095 | ||
3096 | if (net_client_init(mon, opts, 0) < 0) { | |
3097 | monitor_printf(mon, "adding host network device %s failed\n", device); | |
3098 | } | |
3099 | } | |
3100 | ||
3101 | void net_host_device_remove(Monitor *mon, const QDict *qdict) | |
3102 | { | |
3103 | VLANClientState *vc; | |
3104 | int vlan_id = qdict_get_int(qdict, "vlan_id"); | |
3105 | const char *device = qdict_get_str(qdict, "device"); | |
3106 | ||
3107 | vc = qemu_find_vlan_client_by_name(mon, vlan_id, device); | |
3108 | if (!vc) { | |
3109 | return; | |
3110 | } | |
3111 | if (!net_host_check_device(vc->model)) { | |
3112 | monitor_printf(mon, "invalid host network device %s\n", device); | |
3113 | return; | |
3114 | } | |
3115 | qemu_del_vlan_client(vc); | |
3116 | } | |
3117 | ||
3118 | void net_set_boot_mask(int net_boot_mask) | |
3119 | { | |
3120 | int i; | |
3121 | ||
3122 | /* Only the first four NICs may be bootable */ | |
3123 | net_boot_mask = net_boot_mask & 0xF; | |
3124 | ||
3125 | for (i = 0; i < nb_nics; i++) { | |
3126 | if (net_boot_mask & (1 << i)) { | |
3127 | nd_table[i].bootable = 1; | |
3128 | net_boot_mask &= ~(1 << i); | |
3129 | } | |
3130 | } | |
3131 | ||
3132 | if (net_boot_mask) { | |
3133 | fprintf(stderr, "Cannot boot from non-existent NIC\n"); | |
3134 | exit(1); | |
3135 | } | |
3136 | } | |
3137 | ||
3138 | void do_info_network(Monitor *mon) | |
3139 | { | |
3140 | VLANState *vlan; | |
3141 | ||
3142 | QTAILQ_FOREACH(vlan, &vlans, next) { | |
3143 | VLANClientState *vc; | |
3144 | ||
3145 | monitor_printf(mon, "VLAN %d devices:\n", vlan->id); | |
3146 | ||
3147 | QTAILQ_FOREACH(vc, &vlan->clients, next) { | |
3148 | monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str); | |
3149 | } | |
3150 | } | |
3151 | } | |
3152 | ||
3153 | void do_set_link(Monitor *mon, const QDict *qdict) | |
3154 | { | |
3155 | VLANState *vlan; | |
3156 | VLANClientState *vc = NULL; | |
3157 | const char *name = qdict_get_str(qdict, "name"); | |
3158 | const char *up_or_down = qdict_get_str(qdict, "up_or_down"); | |
3159 | ||
3160 | QTAILQ_FOREACH(vlan, &vlans, next) { | |
3161 | QTAILQ_FOREACH(vc, &vlan->clients, next) { | |
3162 | if (strcmp(vc->name, name) == 0) { | |
3163 | goto done; | |
3164 | } | |
3165 | } | |
3166 | } | |
3167 | done: | |
3168 | ||
3169 | if (!vc) { | |
3170 | monitor_printf(mon, "could not find network device '%s'\n", name); | |
3171 | return; | |
3172 | } | |
3173 | ||
3174 | if (strcmp(up_or_down, "up") == 0) | |
3175 | vc->link_down = 0; | |
3176 | else if (strcmp(up_or_down, "down") == 0) | |
3177 | vc->link_down = 1; | |
3178 | else | |
3179 | monitor_printf(mon, "invalid link status '%s'; only 'up' or 'down' " | |
3180 | "valid\n", up_or_down); | |
3181 | ||
3182 | if (vc->link_status_changed) | |
3183 | vc->link_status_changed(vc); | |
3184 | } | |
3185 | ||
3186 | void net_cleanup(void) | |
3187 | { | |
3188 | VLANState *vlan; | |
3189 | VLANClientState *vc, *next_vc; | |
3190 | ||
3191 | QTAILQ_FOREACH(vlan, &vlans, next) { | |
3192 | QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) { | |
3193 | qemu_del_vlan_client(vc); | |
3194 | } | |
3195 | } | |
3196 | ||
3197 | QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) { | |
3198 | qemu_del_vlan_client(vc); | |
3199 | } | |
3200 | } | |
3201 | ||
3202 | static void net_check_clients(void) | |
3203 | { | |
3204 | VLANState *vlan; | |
3205 | ||
3206 | QTAILQ_FOREACH(vlan, &vlans, next) { | |
3207 | if (vlan->nb_guest_devs == 0 && vlan->nb_host_devs == 0) | |
3208 | continue; | |
3209 | if (vlan->nb_guest_devs == 0) | |
3210 | fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id); | |
3211 | if (vlan->nb_host_devs == 0) | |
3212 | fprintf(stderr, | |
3213 | "Warning: vlan %d is not connected to host network\n", | |
3214 | vlan->id); | |
3215 | } | |
3216 | } | |
3217 | ||
3218 | static int net_init_client(QemuOpts *opts, void *dummy) | |
3219 | { | |
3220 | return net_client_init(NULL, opts, 0); | |
3221 | } | |
3222 | ||
3223 | static int net_init_netdev(QemuOpts *opts, void *dummy) | |
3224 | { | |
3225 | return net_client_init(NULL, opts, 1); | |
3226 | } | |
3227 | ||
3228 | int net_init_clients(void) | |
3229 | { | |
3230 | if (QTAILQ_EMPTY(&qemu_net_opts.head)) { | |
3231 | /* if no clients, we use a default config */ | |
3232 | qemu_opts_set(&qemu_net_opts, NULL, "type", "nic"); | |
3233 | #ifdef CONFIG_SLIRP | |
3234 | qemu_opts_set(&qemu_net_opts, NULL, "type", "user"); | |
3235 | #endif | |
3236 | } | |
3237 | ||
3238 | QTAILQ_INIT(&vlans); | |
3239 | QTAILQ_INIT(&non_vlan_clients); | |
3240 | ||
3241 | if (qemu_opts_foreach(&qemu_netdev_opts, net_init_netdev, NULL, 1) == -1) | |
3242 | return -1; | |
3243 | ||
3244 | if (qemu_opts_foreach(&qemu_net_opts, net_init_client, NULL, 1) == -1) { | |
3245 | return -1; | |
3246 | } | |
3247 | ||
3248 | net_check_clients(); | |
3249 | ||
3250 | return 0; | |
3251 | } | |
3252 | ||
3253 | int net_client_parse(QemuOptsList *opts_list, const char *optarg) | |
3254 | { | |
3255 | #if defined(CONFIG_SLIRP) | |
3256 | /* handle legacy -net channel,port:chr */ | |
3257 | if (!strcmp(opts_list->name, "net") && | |
3258 | !strncmp(optarg, "channel,", strlen("channel,"))) { | |
3259 | int ret; | |
3260 | ||
3261 | optarg += strlen("channel,"); | |
3262 | ||
3263 | if (QTAILQ_EMPTY(&slirp_stacks)) { | |
3264 | struct slirp_config_str *config; | |
3265 | ||
3266 | config = qemu_malloc(sizeof(*config)); | |
3267 | pstrcpy(config->str, sizeof(config->str), optarg); | |
3268 | config->flags = SLIRP_CFG_LEGACY; | |
3269 | config->next = slirp_configs; | |
3270 | slirp_configs = config; | |
3271 | ret = 0; | |
3272 | } else { | |
3273 | ret = slirp_guestfwd(QTAILQ_FIRST(&slirp_stacks), optarg, 1); | |
3274 | } | |
3275 | ||
3276 | return ret; | |
3277 | } | |
3278 | #endif | |
3279 | if (!qemu_opts_parse(opts_list, optarg, "type")) { | |
3280 | return -1; | |
3281 | } | |
3282 | ||
3283 | return 0; | |
3284 | } |