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