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