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