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