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