]>
Commit | Line | Data |
---|---|---|
68ac40d2 MM |
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 "net/slirp.h" | |
25 | ||
26 | #include "config-host.h" | |
27 | ||
28 | #include "net.h" | |
29 | #include "monitor.h" | |
30 | #include "sysemu.h" | |
31 | #include "qemu_socket.h" | |
32 | #include "slirp/libslirp.h" | |
33 | ||
34 | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) | |
35 | { | |
36 | const char *p, *p1; | |
37 | int len; | |
38 | p = *pp; | |
39 | p1 = strchr(p, sep); | |
40 | if (!p1) | |
41 | return -1; | |
42 | len = p1 - p; | |
43 | p1++; | |
44 | if (buf_size > 0) { | |
45 | if (len > buf_size - 1) | |
46 | len = buf_size - 1; | |
47 | memcpy(buf, p, len); | |
48 | buf[len] = '\0'; | |
49 | } | |
50 | *pp = p1; | |
51 | return 0; | |
52 | } | |
53 | ||
54 | /* slirp network adapter */ | |
55 | ||
56 | #define SLIRP_CFG_HOSTFWD 1 | |
57 | #define SLIRP_CFG_LEGACY 2 | |
58 | ||
59 | struct slirp_config_str { | |
60 | struct slirp_config_str *next; | |
61 | int flags; | |
62 | char str[1024]; | |
63 | int legacy_format; | |
64 | }; | |
65 | ||
66 | typedef struct SlirpState { | |
67 | QTAILQ_ENTRY(SlirpState) entry; | |
68 | VLANClientState *vc; | |
69 | Slirp *slirp; | |
70 | #ifndef _WIN32 | |
71 | char smb_dir[128]; | |
72 | #endif | |
73 | } SlirpState; | |
74 | ||
75 | static struct slirp_config_str *slirp_configs; | |
76 | const char *legacy_tftp_prefix; | |
77 | const char *legacy_bootp_filename; | |
78 | static QTAILQ_HEAD(slirp_stacks, SlirpState) slirp_stacks = | |
79 | QTAILQ_HEAD_INITIALIZER(slirp_stacks); | |
80 | ||
81 | static int slirp_hostfwd(SlirpState *s, const char *redir_str, | |
82 | int legacy_format); | |
83 | static int slirp_guestfwd(SlirpState *s, const char *config_str, | |
84 | int legacy_format); | |
85 | ||
86 | #ifndef _WIN32 | |
87 | static const char *legacy_smb_export; | |
88 | ||
89 | static int slirp_smb(SlirpState *s, const char *exported_dir, | |
90 | struct in_addr vserver_addr); | |
91 | static void slirp_smb_cleanup(SlirpState *s); | |
92 | #else | |
93 | static inline void slirp_smb_cleanup(SlirpState *s) { } | |
94 | #endif | |
95 | ||
96 | int slirp_can_output(void *opaque) | |
97 | { | |
98 | SlirpState *s = opaque; | |
99 | ||
100 | return qemu_can_send_packet(s->vc); | |
101 | } | |
102 | ||
103 | void slirp_output(void *opaque, const uint8_t *pkt, int pkt_len) | |
104 | { | |
105 | SlirpState *s = opaque; | |
106 | ||
107 | qemu_send_packet(s->vc, pkt, pkt_len); | |
108 | } | |
109 | ||
110 | static ssize_t slirp_receive(VLANClientState *vc, const uint8_t *buf, size_t size) | |
111 | { | |
112 | SlirpState *s = vc->opaque; | |
113 | ||
114 | slirp_input(s->slirp, buf, size); | |
115 | ||
116 | return size; | |
117 | } | |
118 | ||
119 | static void net_slirp_cleanup(VLANClientState *vc) | |
120 | { | |
121 | SlirpState *s = vc->opaque; | |
122 | ||
123 | slirp_cleanup(s->slirp); | |
124 | slirp_smb_cleanup(s); | |
125 | QTAILQ_REMOVE(&slirp_stacks, s, entry); | |
126 | qemu_free(s); | |
127 | } | |
128 | ||
129 | static int net_slirp_init(VLANState *vlan, const char *model, | |
130 | const char *name, int restricted, | |
131 | const char *vnetwork, const char *vhost, | |
132 | const char *vhostname, const char *tftp_export, | |
133 | const char *bootfile, const char *vdhcp_start, | |
134 | const char *vnameserver, const char *smb_export, | |
135 | const char *vsmbserver) | |
136 | { | |
137 | /* default settings according to historic slirp */ | |
138 | struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */ | |
139 | struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */ | |
140 | struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */ | |
141 | struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */ | |
142 | struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */ | |
143 | #ifndef _WIN32 | |
144 | struct in_addr smbsrv = { .s_addr = 0 }; | |
145 | #endif | |
146 | SlirpState *s; | |
147 | char buf[20]; | |
148 | uint32_t addr; | |
149 | int shift; | |
150 | char *end; | |
151 | struct slirp_config_str *config; | |
152 | ||
153 | if (!tftp_export) { | |
154 | tftp_export = legacy_tftp_prefix; | |
155 | } | |
156 | if (!bootfile) { | |
157 | bootfile = legacy_bootp_filename; | |
158 | } | |
159 | ||
160 | if (vnetwork) { | |
161 | if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) { | |
162 | if (!inet_aton(vnetwork, &net)) { | |
163 | return -1; | |
164 | } | |
165 | addr = ntohl(net.s_addr); | |
166 | if (!(addr & 0x80000000)) { | |
167 | mask.s_addr = htonl(0xff000000); /* class A */ | |
168 | } else if ((addr & 0xfff00000) == 0xac100000) { | |
169 | mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */ | |
170 | } else if ((addr & 0xc0000000) == 0x80000000) { | |
171 | mask.s_addr = htonl(0xffff0000); /* class B */ | |
172 | } else if ((addr & 0xffff0000) == 0xc0a80000) { | |
173 | mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */ | |
174 | } else if ((addr & 0xffff0000) == 0xc6120000) { | |
175 | mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */ | |
176 | } else if ((addr & 0xe0000000) == 0xe0000000) { | |
177 | mask.s_addr = htonl(0xffffff00); /* class C */ | |
178 | } else { | |
179 | mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */ | |
180 | } | |
181 | } else { | |
182 | if (!inet_aton(buf, &net)) { | |
183 | return -1; | |
184 | } | |
185 | shift = strtol(vnetwork, &end, 10); | |
186 | if (*end != '\0') { | |
187 | if (!inet_aton(vnetwork, &mask)) { | |
188 | return -1; | |
189 | } | |
190 | } else if (shift < 4 || shift > 32) { | |
191 | return -1; | |
192 | } else { | |
193 | mask.s_addr = htonl(0xffffffff << (32 - shift)); | |
194 | } | |
195 | } | |
196 | net.s_addr &= mask.s_addr; | |
197 | host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr); | |
198 | dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr); | |
199 | dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr); | |
200 | } | |
201 | ||
202 | if (vhost && !inet_aton(vhost, &host)) { | |
203 | return -1; | |
204 | } | |
205 | if ((host.s_addr & mask.s_addr) != net.s_addr) { | |
206 | return -1; | |
207 | } | |
208 | ||
209 | if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) { | |
210 | return -1; | |
211 | } | |
212 | if ((dhcp.s_addr & mask.s_addr) != net.s_addr || | |
213 | dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) { | |
214 | return -1; | |
215 | } | |
216 | ||
217 | if (vnameserver && !inet_aton(vnameserver, &dns)) { | |
218 | return -1; | |
219 | } | |
220 | if ((dns.s_addr & mask.s_addr) != net.s_addr || | |
221 | dns.s_addr == host.s_addr) { | |
222 | return -1; | |
223 | } | |
224 | ||
225 | #ifndef _WIN32 | |
226 | if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) { | |
227 | return -1; | |
228 | } | |
229 | #endif | |
230 | ||
231 | s = qemu_mallocz(sizeof(SlirpState)); | |
232 | s->slirp = slirp_init(restricted, net, mask, host, vhostname, | |
233 | tftp_export, bootfile, dhcp, dns, s); | |
234 | QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry); | |
235 | ||
236 | for (config = slirp_configs; config; config = config->next) { | |
237 | if (config->flags & SLIRP_CFG_HOSTFWD) { | |
238 | if (slirp_hostfwd(s, config->str, | |
239 | config->flags & SLIRP_CFG_LEGACY) < 0) | |
240 | return -1; | |
241 | } else { | |
242 | if (slirp_guestfwd(s, config->str, | |
243 | config->flags & SLIRP_CFG_LEGACY) < 0) | |
244 | return -1; | |
245 | } | |
246 | } | |
247 | #ifndef _WIN32 | |
248 | if (!smb_export) { | |
249 | smb_export = legacy_smb_export; | |
250 | } | |
251 | if (smb_export) { | |
252 | if (slirp_smb(s, smb_export, smbsrv) < 0) | |
253 | return -1; | |
254 | } | |
255 | #endif | |
256 | ||
257 | s->vc = qemu_new_vlan_client(NET_CLIENT_TYPE_SLIRP, | |
258 | vlan, NULL, model, name, NULL, | |
259 | slirp_receive, NULL, NULL, | |
260 | net_slirp_cleanup, s); | |
261 | snprintf(s->vc->info_str, sizeof(s->vc->info_str), | |
262 | "net=%s, restricted=%c", inet_ntoa(net), restricted ? 'y' : 'n'); | |
263 | return 0; | |
264 | } | |
265 | ||
266 | static SlirpState *slirp_lookup(Monitor *mon, const char *vlan, | |
267 | const char *stack) | |
268 | { | |
269 | VLANClientState *vc; | |
270 | ||
271 | if (vlan) { | |
272 | vc = qemu_find_vlan_client_by_name(mon, strtol(vlan, NULL, 0), stack); | |
273 | if (!vc) { | |
274 | return NULL; | |
275 | } | |
276 | if (strcmp(vc->model, "user")) { | |
277 | monitor_printf(mon, "invalid device specified\n"); | |
278 | return NULL; | |
279 | } | |
280 | return vc->opaque; | |
281 | } else { | |
282 | if (QTAILQ_EMPTY(&slirp_stacks)) { | |
283 | monitor_printf(mon, "user mode network stack not in use\n"); | |
284 | return NULL; | |
285 | } | |
286 | return QTAILQ_FIRST(&slirp_stacks); | |
287 | } | |
288 | } | |
289 | ||
290 | void net_slirp_hostfwd_remove(Monitor *mon, const QDict *qdict) | |
291 | { | |
292 | struct in_addr host_addr = { .s_addr = INADDR_ANY }; | |
293 | int host_port; | |
294 | char buf[256] = ""; | |
295 | const char *src_str, *p; | |
296 | SlirpState *s; | |
297 | int is_udp = 0; | |
298 | int err; | |
299 | const char *arg1 = qdict_get_str(qdict, "arg1"); | |
300 | const char *arg2 = qdict_get_try_str(qdict, "arg2"); | |
301 | const char *arg3 = qdict_get_try_str(qdict, "arg3"); | |
302 | ||
303 | if (arg2) { | |
304 | s = slirp_lookup(mon, arg1, arg2); | |
305 | src_str = arg3; | |
306 | } else { | |
307 | s = slirp_lookup(mon, NULL, NULL); | |
308 | src_str = arg1; | |
309 | } | |
310 | if (!s) { | |
311 | return; | |
312 | } | |
313 | ||
314 | if (!src_str || !src_str[0]) | |
315 | goto fail_syntax; | |
316 | ||
317 | p = src_str; | |
318 | get_str_sep(buf, sizeof(buf), &p, ':'); | |
319 | ||
320 | if (!strcmp(buf, "tcp") || buf[0] == '\0') { | |
321 | is_udp = 0; | |
322 | } else if (!strcmp(buf, "udp")) { | |
323 | is_udp = 1; | |
324 | } else { | |
325 | goto fail_syntax; | |
326 | } | |
327 | ||
328 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
329 | goto fail_syntax; | |
330 | } | |
331 | if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) { | |
332 | goto fail_syntax; | |
333 | } | |
334 | ||
335 | host_port = atoi(p); | |
336 | ||
337 | err = slirp_remove_hostfwd(QTAILQ_FIRST(&slirp_stacks)->slirp, is_udp, | |
338 | host_addr, host_port); | |
339 | ||
340 | monitor_printf(mon, "host forwarding rule for %s %s\n", src_str, | |
341 | err ? "removed" : "not found"); | |
342 | return; | |
343 | ||
344 | fail_syntax: | |
345 | monitor_printf(mon, "invalid format\n"); | |
346 | } | |
347 | ||
348 | static int slirp_hostfwd(SlirpState *s, const char *redir_str, | |
349 | int legacy_format) | |
350 | { | |
351 | struct in_addr host_addr = { .s_addr = INADDR_ANY }; | |
352 | struct in_addr guest_addr = { .s_addr = 0 }; | |
353 | int host_port, guest_port; | |
354 | const char *p; | |
355 | char buf[256]; | |
356 | int is_udp; | |
357 | char *end; | |
358 | ||
359 | p = redir_str; | |
360 | if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
361 | goto fail_syntax; | |
362 | } | |
363 | if (!strcmp(buf, "tcp") || buf[0] == '\0') { | |
364 | is_udp = 0; | |
365 | } else if (!strcmp(buf, "udp")) { | |
366 | is_udp = 1; | |
367 | } else { | |
368 | goto fail_syntax; | |
369 | } | |
370 | ||
371 | if (!legacy_format) { | |
372 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
373 | goto fail_syntax; | |
374 | } | |
375 | if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) { | |
376 | goto fail_syntax; | |
377 | } | |
378 | } | |
379 | ||
380 | if (get_str_sep(buf, sizeof(buf), &p, legacy_format ? ':' : '-') < 0) { | |
381 | goto fail_syntax; | |
382 | } | |
383 | host_port = strtol(buf, &end, 0); | |
384 | if (*end != '\0' || host_port < 1 || host_port > 65535) { | |
385 | goto fail_syntax; | |
386 | } | |
387 | ||
388 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
389 | goto fail_syntax; | |
390 | } | |
391 | if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) { | |
392 | goto fail_syntax; | |
393 | } | |
394 | ||
395 | guest_port = strtol(p, &end, 0); | |
396 | if (*end != '\0' || guest_port < 1 || guest_port > 65535) { | |
397 | goto fail_syntax; | |
398 | } | |
399 | ||
400 | if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr, | |
401 | guest_port) < 0) { | |
402 | qemu_error("could not set up host forwarding rule '%s'\n", | |
403 | redir_str); | |
404 | return -1; | |
405 | } | |
406 | return 0; | |
407 | ||
408 | fail_syntax: | |
409 | qemu_error("invalid host forwarding rule '%s'\n", redir_str); | |
410 | return -1; | |
411 | } | |
412 | ||
413 | void net_slirp_hostfwd_add(Monitor *mon, const QDict *qdict) | |
414 | { | |
415 | const char *redir_str; | |
416 | SlirpState *s; | |
417 | const char *arg1 = qdict_get_str(qdict, "arg1"); | |
418 | const char *arg2 = qdict_get_try_str(qdict, "arg2"); | |
419 | const char *arg3 = qdict_get_try_str(qdict, "arg3"); | |
420 | ||
421 | if (arg2) { | |
422 | s = slirp_lookup(mon, arg1, arg2); | |
423 | redir_str = arg3; | |
424 | } else { | |
425 | s = slirp_lookup(mon, NULL, NULL); | |
426 | redir_str = arg1; | |
427 | } | |
428 | if (s) { | |
429 | slirp_hostfwd(s, redir_str, 0); | |
430 | } | |
431 | ||
432 | } | |
433 | ||
434 | int net_slirp_redir(const char *redir_str) | |
435 | { | |
436 | struct slirp_config_str *config; | |
437 | ||
438 | if (QTAILQ_EMPTY(&slirp_stacks)) { | |
439 | config = qemu_malloc(sizeof(*config)); | |
440 | pstrcpy(config->str, sizeof(config->str), redir_str); | |
441 | config->flags = SLIRP_CFG_HOSTFWD | SLIRP_CFG_LEGACY; | |
442 | config->next = slirp_configs; | |
443 | slirp_configs = config; | |
444 | return 0; | |
445 | } | |
446 | ||
447 | return slirp_hostfwd(QTAILQ_FIRST(&slirp_stacks), redir_str, 1); | |
448 | } | |
449 | ||
450 | #ifndef _WIN32 | |
451 | ||
452 | /* automatic user mode samba server configuration */ | |
453 | static void slirp_smb_cleanup(SlirpState *s) | |
454 | { | |
455 | char cmd[128]; | |
456 | ||
457 | if (s->smb_dir[0] != '\0') { | |
458 | snprintf(cmd, sizeof(cmd), "rm -rf %s", s->smb_dir); | |
459 | system(cmd); | |
460 | s->smb_dir[0] = '\0'; | |
461 | } | |
462 | } | |
463 | ||
464 | static int slirp_smb(SlirpState* s, const char *exported_dir, | |
465 | struct in_addr vserver_addr) | |
466 | { | |
467 | static int instance; | |
468 | char smb_conf[128]; | |
469 | char smb_cmdline[128]; | |
470 | FILE *f; | |
471 | ||
472 | snprintf(s->smb_dir, sizeof(s->smb_dir), "/tmp/qemu-smb.%ld-%d", | |
473 | (long)getpid(), instance++); | |
474 | if (mkdir(s->smb_dir, 0700) < 0) { | |
475 | qemu_error("could not create samba server dir '%s'\n", s->smb_dir); | |
476 | return -1; | |
477 | } | |
478 | snprintf(smb_conf, sizeof(smb_conf), "%s/%s", s->smb_dir, "smb.conf"); | |
479 | ||
480 | f = fopen(smb_conf, "w"); | |
481 | if (!f) { | |
482 | slirp_smb_cleanup(s); | |
483 | qemu_error("could not create samba server configuration file '%s'\n", | |
484 | smb_conf); | |
485 | return -1; | |
486 | } | |
487 | fprintf(f, | |
488 | "[global]\n" | |
489 | "private dir=%s\n" | |
490 | "smb ports=0\n" | |
491 | "socket address=127.0.0.1\n" | |
492 | "pid directory=%s\n" | |
493 | "lock directory=%s\n" | |
494 | "log file=%s/log.smbd\n" | |
495 | "smb passwd file=%s/smbpasswd\n" | |
496 | "security = share\n" | |
497 | "[qemu]\n" | |
498 | "path=%s\n" | |
499 | "read only=no\n" | |
500 | "guest ok=yes\n", | |
501 | s->smb_dir, | |
502 | s->smb_dir, | |
503 | s->smb_dir, | |
504 | s->smb_dir, | |
505 | s->smb_dir, | |
506 | exported_dir | |
507 | ); | |
508 | fclose(f); | |
509 | ||
510 | snprintf(smb_cmdline, sizeof(smb_cmdline), "%s -s %s", | |
511 | SMBD_COMMAND, smb_conf); | |
512 | ||
513 | if (slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 139) < 0) { | |
514 | slirp_smb_cleanup(s); | |
515 | qemu_error("conflicting/invalid smbserver address\n"); | |
516 | return -1; | |
517 | } | |
518 | return 0; | |
519 | } | |
520 | ||
521 | /* automatic user mode samba server configuration (legacy interface) */ | |
522 | int net_slirp_smb(const char *exported_dir) | |
523 | { | |
524 | struct in_addr vserver_addr = { .s_addr = 0 }; | |
525 | ||
526 | if (legacy_smb_export) { | |
527 | fprintf(stderr, "-smb given twice\n"); | |
528 | return -1; | |
529 | } | |
530 | legacy_smb_export = exported_dir; | |
531 | if (!QTAILQ_EMPTY(&slirp_stacks)) { | |
532 | return slirp_smb(QTAILQ_FIRST(&slirp_stacks), exported_dir, | |
533 | vserver_addr); | |
534 | } | |
535 | return 0; | |
536 | } | |
537 | ||
538 | #endif /* !defined(_WIN32) */ | |
539 | ||
540 | struct GuestFwd { | |
541 | CharDriverState *hd; | |
542 | struct in_addr server; | |
543 | int port; | |
544 | Slirp *slirp; | |
545 | }; | |
546 | ||
547 | static int guestfwd_can_read(void *opaque) | |
548 | { | |
549 | struct GuestFwd *fwd = opaque; | |
550 | return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port); | |
551 | } | |
552 | ||
553 | static void guestfwd_read(void *opaque, const uint8_t *buf, int size) | |
554 | { | |
555 | struct GuestFwd *fwd = opaque; | |
556 | slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size); | |
557 | } | |
558 | ||
559 | static int slirp_guestfwd(SlirpState *s, const char *config_str, | |
560 | int legacy_format) | |
561 | { | |
562 | struct in_addr server = { .s_addr = 0 }; | |
563 | struct GuestFwd *fwd; | |
564 | const char *p; | |
565 | char buf[128]; | |
566 | char *end; | |
567 | int port; | |
568 | ||
569 | p = config_str; | |
570 | if (legacy_format) { | |
571 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
572 | goto fail_syntax; | |
573 | } | |
574 | } else { | |
575 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
576 | goto fail_syntax; | |
577 | } | |
578 | if (strcmp(buf, "tcp") && buf[0] != '\0') { | |
579 | goto fail_syntax; | |
580 | } | |
581 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
582 | goto fail_syntax; | |
583 | } | |
584 | if (buf[0] != '\0' && !inet_aton(buf, &server)) { | |
585 | goto fail_syntax; | |
586 | } | |
587 | if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) { | |
588 | goto fail_syntax; | |
589 | } | |
590 | } | |
591 | port = strtol(buf, &end, 10); | |
592 | if (*end != '\0' || port < 1 || port > 65535) { | |
593 | goto fail_syntax; | |
594 | } | |
595 | ||
596 | fwd = qemu_malloc(sizeof(struct GuestFwd)); | |
597 | snprintf(buf, sizeof(buf), "guestfwd.tcp:%d", port); | |
598 | fwd->hd = qemu_chr_open(buf, p, NULL); | |
599 | if (!fwd->hd) { | |
600 | qemu_error("could not open guest forwarding device '%s'\n", buf); | |
601 | qemu_free(fwd); | |
602 | return -1; | |
603 | } | |
604 | ||
605 | if (slirp_add_exec(s->slirp, 3, fwd->hd, &server, port) < 0) { | |
606 | qemu_error("conflicting/invalid host:port in guest forwarding " | |
607 | "rule '%s'\n", config_str); | |
608 | qemu_free(fwd); | |
609 | return -1; | |
610 | } | |
611 | fwd->server = server; | |
612 | fwd->port = port; | |
613 | fwd->slirp = s->slirp; | |
614 | ||
615 | qemu_chr_add_handlers(fwd->hd, guestfwd_can_read, guestfwd_read, | |
616 | NULL, fwd); | |
617 | return 0; | |
618 | ||
619 | fail_syntax: | |
620 | qemu_error("invalid guest forwarding rule '%s'\n", config_str); | |
621 | return -1; | |
622 | } | |
623 | ||
624 | void do_info_usernet(Monitor *mon) | |
625 | { | |
626 | SlirpState *s; | |
627 | ||
628 | QTAILQ_FOREACH(s, &slirp_stacks, entry) { | |
629 | monitor_printf(mon, "VLAN %d (%s):\n", s->vc->vlan->id, s->vc->name); | |
630 | slirp_connection_info(s->slirp, mon); | |
631 | } | |
632 | } | |
633 | ||
634 | static int net_init_slirp_configs(const char *name, const char *value, void *opaque) | |
635 | { | |
636 | struct slirp_config_str *config; | |
637 | ||
638 | if (strcmp(name, "hostfwd") != 0 && strcmp(name, "guestfwd") != 0) { | |
639 | return 0; | |
640 | } | |
641 | ||
642 | config = qemu_mallocz(sizeof(*config)); | |
643 | ||
644 | pstrcpy(config->str, sizeof(config->str), value); | |
645 | ||
646 | if (!strcmp(name, "hostfwd")) { | |
647 | config->flags = SLIRP_CFG_HOSTFWD; | |
648 | } | |
649 | ||
650 | config->next = slirp_configs; | |
651 | slirp_configs = config; | |
652 | ||
653 | return 0; | |
654 | } | |
655 | ||
656 | int net_init_slirp(QemuOpts *opts, | |
657 | Monitor *mon, | |
658 | const char *name, | |
659 | VLANState *vlan) | |
660 | { | |
661 | struct slirp_config_str *config; | |
662 | const char *vhost; | |
663 | const char *vhostname; | |
664 | const char *vdhcp_start; | |
665 | const char *vnamesrv; | |
666 | const char *tftp_export; | |
667 | const char *bootfile; | |
668 | const char *smb_export; | |
669 | const char *vsmbsrv; | |
670 | char *vnet = NULL; | |
671 | int restricted = 0; | |
672 | int ret; | |
673 | ||
674 | vhost = qemu_opt_get(opts, "host"); | |
675 | vhostname = qemu_opt_get(opts, "hostname"); | |
676 | vdhcp_start = qemu_opt_get(opts, "dhcpstart"); | |
677 | vnamesrv = qemu_opt_get(opts, "dns"); | |
678 | tftp_export = qemu_opt_get(opts, "tftp"); | |
679 | bootfile = qemu_opt_get(opts, "bootfile"); | |
680 | smb_export = qemu_opt_get(opts, "smb"); | |
681 | vsmbsrv = qemu_opt_get(opts, "smbserver"); | |
682 | ||
683 | if (qemu_opt_get(opts, "ip")) { | |
684 | const char *ip = qemu_opt_get(opts, "ip"); | |
685 | int l = strlen(ip) + strlen("/24") + 1; | |
686 | ||
687 | vnet = qemu_malloc(l); | |
688 | ||
689 | /* emulate legacy ip= parameter */ | |
690 | pstrcpy(vnet, l, ip); | |
691 | pstrcat(vnet, l, "/24"); | |
692 | } | |
693 | ||
694 | if (qemu_opt_get(opts, "net")) { | |
695 | if (vnet) { | |
696 | qemu_free(vnet); | |
697 | } | |
698 | vnet = qemu_strdup(qemu_opt_get(opts, "net")); | |
699 | } | |
700 | ||
701 | if (qemu_opt_get(opts, "restrict") && | |
702 | qemu_opt_get(opts, "restrict")[0] == 'y') { | |
703 | restricted = 1; | |
704 | } | |
705 | ||
706 | qemu_opt_foreach(opts, net_init_slirp_configs, NULL, 0); | |
707 | ||
708 | ret = net_slirp_init(vlan, "user", name, restricted, vnet, vhost, | |
709 | vhostname, tftp_export, bootfile, vdhcp_start, | |
710 | vnamesrv, smb_export, vsmbsrv); | |
711 | ||
712 | while (slirp_configs) { | |
713 | config = slirp_configs; | |
714 | slirp_configs = config->next; | |
715 | qemu_free(config); | |
716 | } | |
717 | ||
718 | if (ret != -1 && vlan) { | |
719 | vlan->nb_host_devs++; | |
720 | } | |
721 | ||
722 | qemu_free(vnet); | |
723 | ||
724 | return ret; | |
725 | } | |
726 | ||
727 | int net_slirp_parse_legacy(QemuOptsList *opts_list, const char *optarg, int *ret) | |
728 | { | |
729 | if (strcmp(opts_list->name, "net") != 0 || | |
730 | strncmp(optarg, "channel,", strlen("channel,")) != 0) { | |
731 | return 0; | |
732 | } | |
733 | ||
734 | /* handle legacy -net channel,port:chr */ | |
735 | optarg += strlen("channel,"); | |
736 | ||
737 | if (QTAILQ_EMPTY(&slirp_stacks)) { | |
738 | struct slirp_config_str *config; | |
739 | ||
740 | config = qemu_malloc(sizeof(*config)); | |
741 | pstrcpy(config->str, sizeof(config->str), optarg); | |
742 | config->flags = SLIRP_CFG_LEGACY; | |
743 | config->next = slirp_configs; | |
744 | slirp_configs = config; | |
745 | *ret = 0; | |
746 | } else { | |
747 | *ret = slirp_guestfwd(QTAILQ_FIRST(&slirp_stacks), optarg, 1); | |
748 | } | |
749 | ||
750 | return 1; | |
751 | } | |
752 |