]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * QEMU System Emulator | |
3 | * | |
4 | * Copyright (c) 2003-2008 Fabrice Bellard | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
25 | #include "qemu/osdep.h" | |
26 | #include "net/slirp.h" | |
27 | ||
28 | ||
29 | #ifndef _WIN32 | |
30 | #include <pwd.h> | |
31 | #include <sys/wait.h> | |
32 | #endif | |
33 | #include "net/net.h" | |
34 | #include "clients.h" | |
35 | #include "hub.h" | |
36 | #include "monitor/monitor.h" | |
37 | #include "qemu/error-report.h" | |
38 | #include "qemu/sockets.h" | |
39 | #include "slirp/libslirp.h" | |
40 | #include "slirp/ip6.h" | |
41 | #include "chardev/char-fe.h" | |
42 | #include "sysemu/sysemu.h" | |
43 | #include "qemu/cutils.h" | |
44 | #include "qapi/error.h" | |
45 | #include "qapi/qmp/qdict.h" | |
46 | ||
47 | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) | |
48 | { | |
49 | const char *p, *p1; | |
50 | int len; | |
51 | p = *pp; | |
52 | p1 = strchr(p, sep); | |
53 | if (!p1) | |
54 | return -1; | |
55 | len = p1 - p; | |
56 | p1++; | |
57 | if (buf_size > 0) { | |
58 | if (len > buf_size - 1) | |
59 | len = buf_size - 1; | |
60 | memcpy(buf, p, len); | |
61 | buf[len] = '\0'; | |
62 | } | |
63 | *pp = p1; | |
64 | return 0; | |
65 | } | |
66 | ||
67 | /* slirp network adapter */ | |
68 | ||
69 | #define SLIRP_CFG_HOSTFWD 1 | |
70 | ||
71 | struct slirp_config_str { | |
72 | struct slirp_config_str *next; | |
73 | int flags; | |
74 | char str[1024]; | |
75 | }; | |
76 | ||
77 | typedef struct SlirpState { | |
78 | NetClientState nc; | |
79 | QTAILQ_ENTRY(SlirpState) entry; | |
80 | Slirp *slirp; | |
81 | Notifier exit_notifier; | |
82 | #ifndef _WIN32 | |
83 | gchar *smb_dir; | |
84 | #endif | |
85 | } SlirpState; | |
86 | ||
87 | static struct slirp_config_str *slirp_configs; | |
88 | static QTAILQ_HEAD(slirp_stacks, SlirpState) slirp_stacks = | |
89 | QTAILQ_HEAD_INITIALIZER(slirp_stacks); | |
90 | ||
91 | static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp); | |
92 | static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp); | |
93 | ||
94 | #ifndef _WIN32 | |
95 | static int slirp_smb(SlirpState *s, const char *exported_dir, | |
96 | struct in_addr vserver_addr, Error **errp); | |
97 | static void slirp_smb_cleanup(SlirpState *s); | |
98 | #else | |
99 | static inline void slirp_smb_cleanup(SlirpState *s) { } | |
100 | #endif | |
101 | ||
102 | void slirp_output(void *opaque, const uint8_t *pkt, int pkt_len) | |
103 | { | |
104 | SlirpState *s = opaque; | |
105 | ||
106 | qemu_send_packet(&s->nc, pkt, pkt_len); | |
107 | } | |
108 | ||
109 | static ssize_t net_slirp_receive(NetClientState *nc, const uint8_t *buf, size_t size) | |
110 | { | |
111 | SlirpState *s = DO_UPCAST(SlirpState, nc, nc); | |
112 | ||
113 | slirp_input(s->slirp, buf, size); | |
114 | ||
115 | return size; | |
116 | } | |
117 | ||
118 | static void slirp_smb_exit(Notifier *n, void *data) | |
119 | { | |
120 | SlirpState *s = container_of(n, SlirpState, exit_notifier); | |
121 | slirp_smb_cleanup(s); | |
122 | } | |
123 | ||
124 | static void net_slirp_cleanup(NetClientState *nc) | |
125 | { | |
126 | SlirpState *s = DO_UPCAST(SlirpState, nc, nc); | |
127 | ||
128 | slirp_cleanup(s->slirp); | |
129 | if (s->exit_notifier.notify) { | |
130 | qemu_remove_exit_notifier(&s->exit_notifier); | |
131 | } | |
132 | slirp_smb_cleanup(s); | |
133 | QTAILQ_REMOVE(&slirp_stacks, s, entry); | |
134 | } | |
135 | ||
136 | static NetClientInfo net_slirp_info = { | |
137 | .type = NET_CLIENT_DRIVER_USER, | |
138 | .size = sizeof(SlirpState), | |
139 | .receive = net_slirp_receive, | |
140 | .cleanup = net_slirp_cleanup, | |
141 | }; | |
142 | ||
143 | static int net_slirp_init(NetClientState *peer, const char *model, | |
144 | const char *name, int restricted, | |
145 | bool ipv4, const char *vnetwork, const char *vhost, | |
146 | bool ipv6, const char *vprefix6, int vprefix6_len, | |
147 | const char *vhost6, | |
148 | const char *vhostname, const char *tftp_export, | |
149 | const char *bootfile, const char *vdhcp_start, | |
150 | const char *vnameserver, const char *vnameserver6, | |
151 | const char *smb_export, const char *vsmbserver, | |
152 | const char **dnssearch, const char *vdomainname, | |
153 | Error **errp) | |
154 | { | |
155 | /* default settings according to historic slirp */ | |
156 | struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */ | |
157 | struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */ | |
158 | struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */ | |
159 | struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */ | |
160 | struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */ | |
161 | struct in6_addr ip6_prefix; | |
162 | struct in6_addr ip6_host; | |
163 | struct in6_addr ip6_dns; | |
164 | #ifndef _WIN32 | |
165 | struct in_addr smbsrv = { .s_addr = 0 }; | |
166 | #endif | |
167 | NetClientState *nc; | |
168 | SlirpState *s; | |
169 | char buf[20]; | |
170 | uint32_t addr; | |
171 | int shift; | |
172 | char *end; | |
173 | struct slirp_config_str *config; | |
174 | ||
175 | if (!ipv4 && (vnetwork || vhost || vnameserver)) { | |
176 | error_setg(errp, "IPv4 disabled but netmask/host/dns provided"); | |
177 | return -1; | |
178 | } | |
179 | ||
180 | if (!ipv6 && (vprefix6 || vhost6 || vnameserver6)) { | |
181 | error_setg(errp, "IPv6 disabled but prefix/host6/dns6 provided"); | |
182 | return -1; | |
183 | } | |
184 | ||
185 | if (!ipv4 && !ipv6) { | |
186 | /* It doesn't make sense to disable both */ | |
187 | error_setg(errp, "IPv4 and IPv6 disabled"); | |
188 | return -1; | |
189 | } | |
190 | ||
191 | if (vnetwork) { | |
192 | if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) { | |
193 | if (!inet_aton(vnetwork, &net)) { | |
194 | error_setg(errp, "Failed to parse netmask"); | |
195 | return -1; | |
196 | } | |
197 | addr = ntohl(net.s_addr); | |
198 | if (!(addr & 0x80000000)) { | |
199 | mask.s_addr = htonl(0xff000000); /* class A */ | |
200 | } else if ((addr & 0xfff00000) == 0xac100000) { | |
201 | mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */ | |
202 | } else if ((addr & 0xc0000000) == 0x80000000) { | |
203 | mask.s_addr = htonl(0xffff0000); /* class B */ | |
204 | } else if ((addr & 0xffff0000) == 0xc0a80000) { | |
205 | mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */ | |
206 | } else if ((addr & 0xffff0000) == 0xc6120000) { | |
207 | mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */ | |
208 | } else if ((addr & 0xe0000000) == 0xe0000000) { | |
209 | mask.s_addr = htonl(0xffffff00); /* class C */ | |
210 | } else { | |
211 | mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */ | |
212 | } | |
213 | } else { | |
214 | if (!inet_aton(buf, &net)) { | |
215 | error_setg(errp, "Failed to parse netmask"); | |
216 | return -1; | |
217 | } | |
218 | shift = strtol(vnetwork, &end, 10); | |
219 | if (*end != '\0') { | |
220 | if (!inet_aton(vnetwork, &mask)) { | |
221 | error_setg(errp, | |
222 | "Failed to parse netmask (trailing chars)"); | |
223 | return -1; | |
224 | } | |
225 | } else if (shift < 4 || shift > 32) { | |
226 | error_setg(errp, | |
227 | "Invalid netmask provided (must be in range 4-32)"); | |
228 | return -1; | |
229 | } else { | |
230 | mask.s_addr = htonl(0xffffffff << (32 - shift)); | |
231 | } | |
232 | } | |
233 | net.s_addr &= mask.s_addr; | |
234 | host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr); | |
235 | dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr); | |
236 | dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr); | |
237 | } | |
238 | ||
239 | if (vhost && !inet_aton(vhost, &host)) { | |
240 | error_setg(errp, "Failed to parse host"); | |
241 | return -1; | |
242 | } | |
243 | if ((host.s_addr & mask.s_addr) != net.s_addr) { | |
244 | error_setg(errp, "Host doesn't belong to network"); | |
245 | return -1; | |
246 | } | |
247 | ||
248 | if (vnameserver && !inet_aton(vnameserver, &dns)) { | |
249 | error_setg(errp, "Failed to parse DNS"); | |
250 | return -1; | |
251 | } | |
252 | if ((dns.s_addr & mask.s_addr) != net.s_addr) { | |
253 | error_setg(errp, "DNS doesn't belong to network"); | |
254 | return -1; | |
255 | } | |
256 | if (dns.s_addr == host.s_addr) { | |
257 | error_setg(errp, "DNS must be different from host"); | |
258 | return -1; | |
259 | } | |
260 | ||
261 | if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) { | |
262 | error_setg(errp, "Failed to parse DHCP start address"); | |
263 | return -1; | |
264 | } | |
265 | if ((dhcp.s_addr & mask.s_addr) != net.s_addr) { | |
266 | error_setg(errp, "DHCP doesn't belong to network"); | |
267 | return -1; | |
268 | } | |
269 | if (dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) { | |
270 | error_setg(errp, "DNS must be different from host and DNS"); | |
271 | return -1; | |
272 | } | |
273 | ||
274 | #ifndef _WIN32 | |
275 | if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) { | |
276 | error_setg(errp, "Failed to parse SMB address"); | |
277 | return -1; | |
278 | } | |
279 | #endif | |
280 | ||
281 | #if defined(_WIN32) && (_WIN32_WINNT < 0x0600) | |
282 | /* No inet_pton helper before Vista... */ | |
283 | if (vprefix6) { | |
284 | /* Unsupported */ | |
285 | error_setg(errp, "IPv6 prefix not supported"); | |
286 | return -1; | |
287 | } | |
288 | memset(&ip6_prefix, 0, sizeof(ip6_prefix)); | |
289 | ip6_prefix.s6_addr[0] = 0xfe; | |
290 | ip6_prefix.s6_addr[1] = 0xc0; | |
291 | #else | |
292 | if (!vprefix6) { | |
293 | vprefix6 = "fec0::"; | |
294 | } | |
295 | if (!inet_pton(AF_INET6, vprefix6, &ip6_prefix)) { | |
296 | error_setg(errp, "Failed to parse IPv6 prefix"); | |
297 | return -1; | |
298 | } | |
299 | #endif | |
300 | ||
301 | if (!vprefix6_len) { | |
302 | vprefix6_len = 64; | |
303 | } | |
304 | if (vprefix6_len < 0 || vprefix6_len > 126) { | |
305 | error_setg(errp, | |
306 | "Invalid prefix provided (prefix len must be in range 0-126"); | |
307 | return -1; | |
308 | } | |
309 | ||
310 | if (vhost6) { | |
311 | #if defined(_WIN32) && (_WIN32_WINNT < 0x0600) | |
312 | error_setg(errp, "IPv6 host not supported"); | |
313 | return -1; | |
314 | #else | |
315 | if (!inet_pton(AF_INET6, vhost6, &ip6_host)) { | |
316 | error_setg(errp, "Failed to parse IPv6 host"); | |
317 | return -1; | |
318 | } | |
319 | if (!in6_equal_net(&ip6_prefix, &ip6_host, vprefix6_len)) { | |
320 | error_setg(errp, "IPv6 Host doesn't belong to network"); | |
321 | return -1; | |
322 | } | |
323 | #endif | |
324 | } else { | |
325 | ip6_host = ip6_prefix; | |
326 | ip6_host.s6_addr[15] |= 2; | |
327 | } | |
328 | ||
329 | if (vnameserver6) { | |
330 | #if defined(_WIN32) && (_WIN32_WINNT < 0x0600) | |
331 | error_setg(errp, "IPv6 DNS not supported"); | |
332 | return -1; | |
333 | #else | |
334 | if (!inet_pton(AF_INET6, vnameserver6, &ip6_dns)) { | |
335 | error_setg(errp, "Failed to parse IPv6 DNS"); | |
336 | return -1; | |
337 | } | |
338 | if (!in6_equal_net(&ip6_prefix, &ip6_dns, vprefix6_len)) { | |
339 | error_setg(errp, "IPv6 DNS doesn't belong to network"); | |
340 | return -1; | |
341 | } | |
342 | #endif | |
343 | } else { | |
344 | ip6_dns = ip6_prefix; | |
345 | ip6_dns.s6_addr[15] |= 3; | |
346 | } | |
347 | ||
348 | if (vdomainname && !*vdomainname) { | |
349 | error_setg(errp, "'domainname' parameter cannot be empty"); | |
350 | return -1; | |
351 | } | |
352 | ||
353 | if (vdomainname && strlen(vdomainname) > 255) { | |
354 | error_setg(errp, "'domainname' parameter cannot exceed 255 bytes"); | |
355 | return -1; | |
356 | } | |
357 | ||
358 | if (vhostname && strlen(vhostname) > 255) { | |
359 | error_setg(errp, "'vhostname' parameter cannot exceed 255 bytes"); | |
360 | return -1; | |
361 | } | |
362 | ||
363 | nc = qemu_new_net_client(&net_slirp_info, peer, model, name); | |
364 | ||
365 | snprintf(nc->info_str, sizeof(nc->info_str), | |
366 | "net=%s,restrict=%s", inet_ntoa(net), | |
367 | restricted ? "on" : "off"); | |
368 | ||
369 | s = DO_UPCAST(SlirpState, nc, nc); | |
370 | ||
371 | s->slirp = slirp_init(restricted, ipv4, net, mask, host, | |
372 | ipv6, ip6_prefix, vprefix6_len, ip6_host, | |
373 | vhostname, tftp_export, bootfile, dhcp, | |
374 | dns, ip6_dns, dnssearch, vdomainname, s); | |
375 | QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry); | |
376 | ||
377 | for (config = slirp_configs; config; config = config->next) { | |
378 | if (config->flags & SLIRP_CFG_HOSTFWD) { | |
379 | if (slirp_hostfwd(s, config->str, errp) < 0) { | |
380 | goto error; | |
381 | } | |
382 | } else { | |
383 | if (slirp_guestfwd(s, config->str, errp) < 0) { | |
384 | goto error; | |
385 | } | |
386 | } | |
387 | } | |
388 | #ifndef _WIN32 | |
389 | if (smb_export) { | |
390 | if (slirp_smb(s, smb_export, smbsrv, errp) < 0) { | |
391 | goto error; | |
392 | } | |
393 | } | |
394 | #endif | |
395 | ||
396 | s->exit_notifier.notify = slirp_smb_exit; | |
397 | qemu_add_exit_notifier(&s->exit_notifier); | |
398 | return 0; | |
399 | ||
400 | error: | |
401 | qemu_del_net_client(nc); | |
402 | return -1; | |
403 | } | |
404 | ||
405 | static SlirpState *slirp_lookup(Monitor *mon, const char *hub_id, | |
406 | const char *name) | |
407 | { | |
408 | if (name) { | |
409 | NetClientState *nc; | |
410 | if (hub_id) { | |
411 | nc = net_hub_find_client_by_name(strtol(hub_id, NULL, 0), name); | |
412 | if (!nc) { | |
413 | monitor_printf(mon, "unrecognized (hub-id, stackname) pair\n"); | |
414 | return NULL; | |
415 | } | |
416 | warn_report("Using 'hub-id' is deprecated, specify the netdev id " | |
417 | "directly instead"); | |
418 | } else { | |
419 | nc = qemu_find_netdev(name); | |
420 | if (!nc) { | |
421 | monitor_printf(mon, "unrecognized netdev id '%s'\n", name); | |
422 | return NULL; | |
423 | } | |
424 | } | |
425 | if (strcmp(nc->model, "user")) { | |
426 | monitor_printf(mon, "invalid device specified\n"); | |
427 | return NULL; | |
428 | } | |
429 | return DO_UPCAST(SlirpState, nc, nc); | |
430 | } else { | |
431 | if (QTAILQ_EMPTY(&slirp_stacks)) { | |
432 | monitor_printf(mon, "user mode network stack not in use\n"); | |
433 | return NULL; | |
434 | } | |
435 | return QTAILQ_FIRST(&slirp_stacks); | |
436 | } | |
437 | } | |
438 | ||
439 | void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict) | |
440 | { | |
441 | struct in_addr host_addr = { .s_addr = INADDR_ANY }; | |
442 | int host_port; | |
443 | char buf[256]; | |
444 | const char *src_str, *p; | |
445 | SlirpState *s; | |
446 | int is_udp = 0; | |
447 | int err; | |
448 | const char *arg1 = qdict_get_str(qdict, "arg1"); | |
449 | const char *arg2 = qdict_get_try_str(qdict, "arg2"); | |
450 | const char *arg3 = qdict_get_try_str(qdict, "arg3"); | |
451 | ||
452 | if (arg3) { | |
453 | s = slirp_lookup(mon, arg1, arg2); | |
454 | src_str = arg3; | |
455 | } else if (arg2) { | |
456 | s = slirp_lookup(mon, NULL, arg1); | |
457 | src_str = arg2; | |
458 | } else { | |
459 | s = slirp_lookup(mon, NULL, NULL); | |
460 | src_str = arg1; | |
461 | } | |
462 | if (!s) { | |
463 | return; | |
464 | } | |
465 | ||
466 | p = src_str; | |
467 | if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
468 | goto fail_syntax; | |
469 | } | |
470 | ||
471 | if (!strcmp(buf, "tcp") || buf[0] == '\0') { | |
472 | is_udp = 0; | |
473 | } else if (!strcmp(buf, "udp")) { | |
474 | is_udp = 1; | |
475 | } else { | |
476 | goto fail_syntax; | |
477 | } | |
478 | ||
479 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
480 | goto fail_syntax; | |
481 | } | |
482 | if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) { | |
483 | goto fail_syntax; | |
484 | } | |
485 | ||
486 | if (qemu_strtoi(p, NULL, 10, &host_port)) { | |
487 | goto fail_syntax; | |
488 | } | |
489 | ||
490 | err = slirp_remove_hostfwd(s->slirp, is_udp, host_addr, host_port); | |
491 | ||
492 | monitor_printf(mon, "host forwarding rule for %s %s\n", src_str, | |
493 | err ? "not found" : "removed"); | |
494 | return; | |
495 | ||
496 | fail_syntax: | |
497 | monitor_printf(mon, "invalid format\n"); | |
498 | } | |
499 | ||
500 | static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp) | |
501 | { | |
502 | struct in_addr host_addr = { .s_addr = INADDR_ANY }; | |
503 | struct in_addr guest_addr = { .s_addr = 0 }; | |
504 | int host_port, guest_port; | |
505 | const char *p; | |
506 | char buf[256]; | |
507 | int is_udp; | |
508 | char *end; | |
509 | const char *fail_reason = "Unknown reason"; | |
510 | ||
511 | p = redir_str; | |
512 | if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
513 | fail_reason = "No : separators"; | |
514 | goto fail_syntax; | |
515 | } | |
516 | if (!strcmp(buf, "tcp") || buf[0] == '\0') { | |
517 | is_udp = 0; | |
518 | } else if (!strcmp(buf, "udp")) { | |
519 | is_udp = 1; | |
520 | } else { | |
521 | fail_reason = "Bad protocol name"; | |
522 | goto fail_syntax; | |
523 | } | |
524 | ||
525 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
526 | fail_reason = "Missing : separator"; | |
527 | goto fail_syntax; | |
528 | } | |
529 | if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) { | |
530 | fail_reason = "Bad host address"; | |
531 | goto fail_syntax; | |
532 | } | |
533 | ||
534 | if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) { | |
535 | fail_reason = "Bad host port separator"; | |
536 | goto fail_syntax; | |
537 | } | |
538 | host_port = strtol(buf, &end, 0); | |
539 | if (*end != '\0' || host_port < 0 || host_port > 65535) { | |
540 | fail_reason = "Bad host port"; | |
541 | goto fail_syntax; | |
542 | } | |
543 | ||
544 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
545 | fail_reason = "Missing guest address"; | |
546 | goto fail_syntax; | |
547 | } | |
548 | if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) { | |
549 | fail_reason = "Bad guest address"; | |
550 | goto fail_syntax; | |
551 | } | |
552 | ||
553 | guest_port = strtol(p, &end, 0); | |
554 | if (*end != '\0' || guest_port < 1 || guest_port > 65535) { | |
555 | fail_reason = "Bad guest port"; | |
556 | goto fail_syntax; | |
557 | } | |
558 | ||
559 | if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr, | |
560 | guest_port) < 0) { | |
561 | error_setg(errp, "Could not set up host forwarding rule '%s'", | |
562 | redir_str); | |
563 | return -1; | |
564 | } | |
565 | return 0; | |
566 | ||
567 | fail_syntax: | |
568 | error_setg(errp, "Invalid host forwarding rule '%s' (%s)", redir_str, | |
569 | fail_reason); | |
570 | return -1; | |
571 | } | |
572 | ||
573 | void hmp_hostfwd_add(Monitor *mon, const QDict *qdict) | |
574 | { | |
575 | const char *redir_str; | |
576 | SlirpState *s; | |
577 | const char *arg1 = qdict_get_str(qdict, "arg1"); | |
578 | const char *arg2 = qdict_get_try_str(qdict, "arg2"); | |
579 | const char *arg3 = qdict_get_try_str(qdict, "arg3"); | |
580 | ||
581 | if (arg3) { | |
582 | s = slirp_lookup(mon, arg1, arg2); | |
583 | redir_str = arg3; | |
584 | } else if (arg2) { | |
585 | s = slirp_lookup(mon, NULL, arg1); | |
586 | redir_str = arg2; | |
587 | } else { | |
588 | s = slirp_lookup(mon, NULL, NULL); | |
589 | redir_str = arg1; | |
590 | } | |
591 | if (s) { | |
592 | Error *err = NULL; | |
593 | if (slirp_hostfwd(s, redir_str, &err) < 0) { | |
594 | error_report_err(err); | |
595 | } | |
596 | } | |
597 | ||
598 | } | |
599 | ||
600 | #ifndef _WIN32 | |
601 | ||
602 | /* automatic user mode samba server configuration */ | |
603 | static void slirp_smb_cleanup(SlirpState *s) | |
604 | { | |
605 | int ret; | |
606 | ||
607 | if (s->smb_dir) { | |
608 | gchar *cmd = g_strdup_printf("rm -rf %s", s->smb_dir); | |
609 | ret = system(cmd); | |
610 | if (ret == -1 || !WIFEXITED(ret)) { | |
611 | error_report("'%s' failed.", cmd); | |
612 | } else if (WEXITSTATUS(ret)) { | |
613 | error_report("'%s' failed. Error code: %d", | |
614 | cmd, WEXITSTATUS(ret)); | |
615 | } | |
616 | g_free(cmd); | |
617 | g_free(s->smb_dir); | |
618 | s->smb_dir = NULL; | |
619 | } | |
620 | } | |
621 | ||
622 | static int slirp_smb(SlirpState* s, const char *exported_dir, | |
623 | struct in_addr vserver_addr, Error **errp) | |
624 | { | |
625 | char *smb_conf; | |
626 | char *smb_cmdline; | |
627 | struct passwd *passwd; | |
628 | FILE *f; | |
629 | ||
630 | passwd = getpwuid(geteuid()); | |
631 | if (!passwd) { | |
632 | error_setg(errp, "Failed to retrieve user name"); | |
633 | return -1; | |
634 | } | |
635 | ||
636 | if (access(CONFIG_SMBD_COMMAND, F_OK)) { | |
637 | error_setg(errp, "Could not find '%s', please install it", | |
638 | CONFIG_SMBD_COMMAND); | |
639 | return -1; | |
640 | } | |
641 | ||
642 | if (access(exported_dir, R_OK | X_OK)) { | |
643 | error_setg(errp, "Error accessing shared directory '%s': %s", | |
644 | exported_dir, strerror(errno)); | |
645 | return -1; | |
646 | } | |
647 | ||
648 | s->smb_dir = g_dir_make_tmp("qemu-smb.XXXXXX", NULL); | |
649 | if (!s->smb_dir) { | |
650 | error_setg(errp, "Could not create samba server dir"); | |
651 | return -1; | |
652 | } | |
653 | smb_conf = g_strdup_printf("%s/%s", s->smb_dir, "smb.conf"); | |
654 | ||
655 | f = fopen(smb_conf, "w"); | |
656 | if (!f) { | |
657 | slirp_smb_cleanup(s); | |
658 | error_setg(errp, | |
659 | "Could not create samba server configuration file '%s'", | |
660 | smb_conf); | |
661 | g_free(smb_conf); | |
662 | return -1; | |
663 | } | |
664 | fprintf(f, | |
665 | "[global]\n" | |
666 | "private dir=%s\n" | |
667 | "interfaces=127.0.0.1\n" | |
668 | "bind interfaces only=yes\n" | |
669 | "pid directory=%s\n" | |
670 | "lock directory=%s\n" | |
671 | "state directory=%s\n" | |
672 | "cache directory=%s\n" | |
673 | "ncalrpc dir=%s/ncalrpc\n" | |
674 | "log file=%s/log.smbd\n" | |
675 | "smb passwd file=%s/smbpasswd\n" | |
676 | "security = user\n" | |
677 | "map to guest = Bad User\n" | |
678 | "load printers = no\n" | |
679 | "printing = bsd\n" | |
680 | "disable spoolss = yes\n" | |
681 | "usershare max shares = 0\n" | |
682 | "[qemu]\n" | |
683 | "path=%s\n" | |
684 | "read only=no\n" | |
685 | "guest ok=yes\n" | |
686 | "force user=%s\n", | |
687 | s->smb_dir, | |
688 | s->smb_dir, | |
689 | s->smb_dir, | |
690 | s->smb_dir, | |
691 | s->smb_dir, | |
692 | s->smb_dir, | |
693 | s->smb_dir, | |
694 | s->smb_dir, | |
695 | exported_dir, | |
696 | passwd->pw_name | |
697 | ); | |
698 | fclose(f); | |
699 | ||
700 | smb_cmdline = g_strdup_printf("%s -l %s -s %s", | |
701 | CONFIG_SMBD_COMMAND, s->smb_dir, smb_conf); | |
702 | g_free(smb_conf); | |
703 | ||
704 | if (slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 139) < 0 || | |
705 | slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 445) < 0) { | |
706 | slirp_smb_cleanup(s); | |
707 | g_free(smb_cmdline); | |
708 | error_setg(errp, "Conflicting/invalid smbserver address"); | |
709 | return -1; | |
710 | } | |
711 | g_free(smb_cmdline); | |
712 | return 0; | |
713 | } | |
714 | ||
715 | #endif /* !defined(_WIN32) */ | |
716 | ||
717 | struct GuestFwd { | |
718 | CharBackend hd; | |
719 | struct in_addr server; | |
720 | int port; | |
721 | Slirp *slirp; | |
722 | }; | |
723 | ||
724 | static int guestfwd_can_read(void *opaque) | |
725 | { | |
726 | struct GuestFwd *fwd = opaque; | |
727 | return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port); | |
728 | } | |
729 | ||
730 | static void guestfwd_read(void *opaque, const uint8_t *buf, int size) | |
731 | { | |
732 | struct GuestFwd *fwd = opaque; | |
733 | slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size); | |
734 | } | |
735 | ||
736 | static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp) | |
737 | { | |
738 | struct in_addr server = { .s_addr = 0 }; | |
739 | struct GuestFwd *fwd; | |
740 | const char *p; | |
741 | char buf[128]; | |
742 | char *end; | |
743 | int port; | |
744 | ||
745 | p = config_str; | |
746 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
747 | goto fail_syntax; | |
748 | } | |
749 | if (strcmp(buf, "tcp") && buf[0] != '\0') { | |
750 | goto fail_syntax; | |
751 | } | |
752 | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) { | |
753 | goto fail_syntax; | |
754 | } | |
755 | if (buf[0] != '\0' && !inet_aton(buf, &server)) { | |
756 | goto fail_syntax; | |
757 | } | |
758 | if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) { | |
759 | goto fail_syntax; | |
760 | } | |
761 | port = strtol(buf, &end, 10); | |
762 | if (*end != '\0' || port < 1 || port > 65535) { | |
763 | goto fail_syntax; | |
764 | } | |
765 | ||
766 | snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port); | |
767 | ||
768 | if ((strlen(p) > 4) && !strncmp(p, "cmd:", 4)) { | |
769 | if (slirp_add_exec(s->slirp, 0, &p[4], &server, port) < 0) { | |
770 | error_setg(errp, "Conflicting/invalid host:port in guest " | |
771 | "forwarding rule '%s'", config_str); | |
772 | return -1; | |
773 | } | |
774 | } else { | |
775 | Error *err = NULL; | |
776 | /* | |
777 | * FIXME: sure we want to support implicit | |
778 | * muxed monitors here? | |
779 | */ | |
780 | Chardev *chr = qemu_chr_new_mux_mon(buf, p); | |
781 | ||
782 | if (!chr) { | |
783 | error_setg(errp, "Could not open guest forwarding device '%s'", | |
784 | buf); | |
785 | return -1; | |
786 | } | |
787 | ||
788 | fwd = g_new(struct GuestFwd, 1); | |
789 | qemu_chr_fe_init(&fwd->hd, chr, &err); | |
790 | if (err) { | |
791 | error_propagate(errp, err); | |
792 | g_free(fwd); | |
793 | return -1; | |
794 | } | |
795 | ||
796 | if (slirp_add_exec(s->slirp, 3, &fwd->hd, &server, port) < 0) { | |
797 | error_setg(errp, "Conflicting/invalid host:port in guest " | |
798 | "forwarding rule '%s'", config_str); | |
799 | g_free(fwd); | |
800 | return -1; | |
801 | } | |
802 | fwd->server = server; | |
803 | fwd->port = port; | |
804 | fwd->slirp = s->slirp; | |
805 | ||
806 | qemu_chr_fe_set_handlers(&fwd->hd, guestfwd_can_read, guestfwd_read, | |
807 | NULL, NULL, fwd, NULL, true); | |
808 | } | |
809 | return 0; | |
810 | ||
811 | fail_syntax: | |
812 | error_setg(errp, "Invalid guest forwarding rule '%s'", config_str); | |
813 | return -1; | |
814 | } | |
815 | ||
816 | void hmp_info_usernet(Monitor *mon, const QDict *qdict) | |
817 | { | |
818 | SlirpState *s; | |
819 | ||
820 | QTAILQ_FOREACH(s, &slirp_stacks, entry) { | |
821 | int id; | |
822 | bool got_hub_id = net_hub_id_for_client(&s->nc, &id) == 0; | |
823 | monitor_printf(mon, "Hub %d (%s):\n", | |
824 | got_hub_id ? id : -1, | |
825 | s->nc.name); | |
826 | slirp_connection_info(s->slirp, mon); | |
827 | } | |
828 | } | |
829 | ||
830 | static void | |
831 | net_init_slirp_configs(const StringList *fwd, int flags) | |
832 | { | |
833 | while (fwd) { | |
834 | struct slirp_config_str *config; | |
835 | ||
836 | config = g_malloc0(sizeof(*config)); | |
837 | pstrcpy(config->str, sizeof(config->str), fwd->value->str); | |
838 | config->flags = flags; | |
839 | config->next = slirp_configs; | |
840 | slirp_configs = config; | |
841 | ||
842 | fwd = fwd->next; | |
843 | } | |
844 | } | |
845 | ||
846 | static const char **slirp_dnssearch(const StringList *dnsname) | |
847 | { | |
848 | const StringList *c = dnsname; | |
849 | size_t i = 0, num_opts = 0; | |
850 | const char **ret; | |
851 | ||
852 | while (c) { | |
853 | num_opts++; | |
854 | c = c->next; | |
855 | } | |
856 | ||
857 | if (num_opts == 0) { | |
858 | return NULL; | |
859 | } | |
860 | ||
861 | ret = g_malloc((num_opts + 1) * sizeof(*ret)); | |
862 | c = dnsname; | |
863 | while (c) { | |
864 | ret[i++] = c->value->str; | |
865 | c = c->next; | |
866 | } | |
867 | ret[i] = NULL; | |
868 | return ret; | |
869 | } | |
870 | ||
871 | int net_init_slirp(const Netdev *netdev, const char *name, | |
872 | NetClientState *peer, Error **errp) | |
873 | { | |
874 | struct slirp_config_str *config; | |
875 | char *vnet; | |
876 | int ret; | |
877 | const NetdevUserOptions *user; | |
878 | const char **dnssearch; | |
879 | bool ipv4 = true, ipv6 = true; | |
880 | ||
881 | assert(netdev->type == NET_CLIENT_DRIVER_USER); | |
882 | user = &netdev->u.user; | |
883 | ||
884 | if ((user->has_ipv6 && user->ipv6 && !user->has_ipv4) || | |
885 | (user->has_ipv4 && !user->ipv4)) { | |
886 | ipv4 = 0; | |
887 | } | |
888 | if ((user->has_ipv4 && user->ipv4 && !user->has_ipv6) || | |
889 | (user->has_ipv6 && !user->ipv6)) { | |
890 | ipv6 = 0; | |
891 | } | |
892 | ||
893 | vnet = user->has_net ? g_strdup(user->net) : | |
894 | user->has_ip ? g_strdup_printf("%s/24", user->ip) : | |
895 | NULL; | |
896 | ||
897 | dnssearch = slirp_dnssearch(user->dnssearch); | |
898 | ||
899 | /* all optional fields are initialized to "all bits zero" */ | |
900 | ||
901 | net_init_slirp_configs(user->hostfwd, SLIRP_CFG_HOSTFWD); | |
902 | net_init_slirp_configs(user->guestfwd, 0); | |
903 | ||
904 | ret = net_slirp_init(peer, "user", name, user->q_restrict, | |
905 | ipv4, vnet, user->host, | |
906 | ipv6, user->ipv6_prefix, user->ipv6_prefixlen, | |
907 | user->ipv6_host, user->hostname, user->tftp, | |
908 | user->bootfile, user->dhcpstart, | |
909 | user->dns, user->ipv6_dns, user->smb, | |
910 | user->smbserver, dnssearch, user->domainname, errp); | |
911 | ||
912 | while (slirp_configs) { | |
913 | config = slirp_configs; | |
914 | slirp_configs = config->next; | |
915 | g_free(config); | |
916 | } | |
917 | ||
918 | g_free(vnet); | |
919 | g_free(dnssearch); | |
920 | ||
921 | return ret; | |
922 | } |