blob: c4334ee876c7b72dec9f29fe949658110c8d2406 [file] [log] [blame]
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001/*
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 */
Markus Armbruster452fcdb2018-02-01 12:18:39 +010024
Peter Maydell2744d922016-01-29 17:50:00 +000025#include "qemu/osdep.h"
Marc-André Lureau2addc8f2018-11-14 16:36:33 +040026#include "qemu/log.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000027#include "net/slirp.h"
28
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000029
Blue Swirl28b150b2010-01-27 17:47:33 +000030#ifndef _WIN32
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +020031#include <pwd.h>
Blue Swirl28b150b2010-01-27 17:47:33 +000032#include <sys/wait.h>
33#endif
Paolo Bonzini1422e322012-10-24 08:43:34 +020034#include "net/net.h"
Paolo Bonzinia245fc12012-09-17 18:43:51 +020035#include "clients.h"
36#include "hub.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010037#include "monitor/monitor.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010038#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010039#include "qemu/sockets.h"
Marc-André Lureau675b9b52019-02-12 17:25:23 +010040#include <libslirp.h>
Marc-André Lureau4d43a602017-01-26 18:26:44 +040041#include "chardev/char-fe.h"
Paolo Bonzinif6c2e662016-07-12 09:57:12 +020042#include "sysemu/sysemu.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020043#include "qemu/cutils.h"
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +030044#include "qapi/error.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010045#include "qapi/qmp/qdict.h"
Marc-André Lureaue05ae1d2018-11-14 16:36:40 +040046#include "util.h"
Marc-André Lureaud8903442019-02-12 17:25:19 +010047#include "migration/register.h"
48#include "migration/qemu-file-types.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000049
50static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
51{
52 const char *p, *p1;
53 int len;
54 p = *pp;
55 p1 = strchr(p, sep);
56 if (!p1)
57 return -1;
58 len = p1 - p;
59 p1++;
60 if (buf_size > 0) {
61 if (len > buf_size - 1)
62 len = buf_size - 1;
63 memcpy(buf, p, len);
64 buf[len] = '\0';
65 }
66 *pp = p1;
67 return 0;
68}
69
70/* slirp network adapter */
71
72#define SLIRP_CFG_HOSTFWD 1
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000073
74struct slirp_config_str {
75 struct slirp_config_str *next;
76 int flags;
77 char str[1024];
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000078};
79
Marc-André Lureau8d45a3b2019-01-17 15:43:35 +040080struct GuestFwd {
81 CharBackend hd;
82 struct in_addr server;
83 int port;
84 Slirp *slirp;
85};
86
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000087typedef struct SlirpState {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010088 NetClientState nc;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000089 QTAILQ_ENTRY(SlirpState) entry;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000090 Slirp *slirp;
Marc-André Lureau1ab67b92019-01-17 15:43:55 +040091 Notifier poll_notifier;
Paolo Bonzinif6c2e662016-07-12 09:57:12 +020092 Notifier exit_notifier;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000093#ifndef _WIN32
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +010094 gchar *smb_dir;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000095#endif
Marc-André Lureau8d45a3b2019-01-17 15:43:35 +040096 GSList *fwd;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000097} SlirpState;
98
99static struct slirp_config_str *slirp_configs;
Paolo Bonzinib58deb32018-12-06 11:58:10 +0100100static QTAILQ_HEAD(, SlirpState) slirp_stacks =
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000101 QTAILQ_HEAD_INITIALIZER(slirp_stacks);
102
Thomas Huthd18572d2018-08-22 15:43:30 +0200103static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp);
104static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000105
106#ifndef _WIN32
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000107static int slirp_smb(SlirpState *s, const char *exported_dir,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200108 struct in_addr vserver_addr, Error **errp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000109static void slirp_smb_cleanup(SlirpState *s);
110#else
111static inline void slirp_smb_cleanup(SlirpState *s) { }
112#endif
113
Marc-André Lureau625a5262019-01-17 15:43:54 +0400114static ssize_t net_slirp_send_packet(const void *pkt, size_t pkt_len,
115 void *opaque)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000116{
117 SlirpState *s = opaque;
118
Marc-André Lureau625a5262019-01-17 15:43:54 +0400119 return qemu_send_packet(&s->nc, pkt, pkt_len);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000120}
121
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100122static ssize_t net_slirp_receive(NetClientState *nc, const uint8_t *buf, size_t size)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000123{
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000124 SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000125
126 slirp_input(s->slirp, buf, size);
127
128 return size;
129}
130
Paolo Bonzinif6c2e662016-07-12 09:57:12 +0200131static void slirp_smb_exit(Notifier *n, void *data)
132{
133 SlirpState *s = container_of(n, SlirpState, exit_notifier);
134 slirp_smb_cleanup(s);
135}
136
Marc-André Lureau8d45a3b2019-01-17 15:43:35 +0400137static void slirp_free_fwd(gpointer data)
138{
139 struct GuestFwd *fwd = data;
140
141 qemu_chr_fe_deinit(&fwd->hd, true);
142 g_free(data);
143}
144
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100145static void net_slirp_cleanup(NetClientState *nc)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000146{
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000147 SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000148
Marc-André Lureau8d45a3b2019-01-17 15:43:35 +0400149 g_slist_free_full(s->fwd, slirp_free_fwd);
Marc-André Lureau1ab67b92019-01-17 15:43:55 +0400150 main_loop_poll_remove_notifier(&s->poll_notifier);
Marc-André Lureaud8903442019-02-12 17:25:19 +0100151 unregister_savevm(NULL, "slirp", s->slirp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000152 slirp_cleanup(s->slirp);
Marc-André Lureau67f32802016-08-18 17:44:05 +0400153 if (s->exit_notifier.notify) {
154 qemu_remove_exit_notifier(&s->exit_notifier);
155 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000156 slirp_smb_cleanup(s);
157 QTAILQ_REMOVE(&slirp_stacks, s, entry);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000158}
159
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000160static NetClientInfo net_slirp_info = {
Eric Blakef394b2e2016-07-13 21:50:23 -0600161 .type = NET_CLIENT_DRIVER_USER,
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000162 .size = sizeof(SlirpState),
163 .receive = net_slirp_receive,
164 .cleanup = net_slirp_cleanup,
165};
166
Marc-André Lureau3e0fad32019-01-17 15:43:58 +0400167static void net_slirp_guest_error(const char *msg, void *opaque)
Marc-André Lureau2addc8f2018-11-14 16:36:33 +0400168{
169 qemu_log_mask(LOG_GUEST_ERROR, "%s", msg);
170}
171
Marc-André Lureau3e0fad32019-01-17 15:43:58 +0400172static int64_t net_slirp_clock_get_ns(void *opaque)
Marc-André Lureaue6dbff32018-11-22 02:06:28 +0400173{
174 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
175}
176
Marc-André Lureau3e0fad32019-01-17 15:43:58 +0400177static void *net_slirp_timer_new(SlirpTimerCb cb,
178 void *cb_opaque, void *opaque)
Marc-André Lureau07abf6d2019-01-17 15:43:37 +0400179{
180 return timer_new_full(NULL, QEMU_CLOCK_VIRTUAL,
181 SCALE_MS, QEMU_TIMER_ATTR_EXTERNAL,
Marc-André Lureau3e0fad32019-01-17 15:43:58 +0400182 cb, cb_opaque);
Marc-André Lureau07abf6d2019-01-17 15:43:37 +0400183}
184
Marc-André Lureau3e0fad32019-01-17 15:43:58 +0400185static void net_slirp_timer_free(void *timer, void *opaque)
Marc-André Lureau07abf6d2019-01-17 15:43:37 +0400186{
187 timer_del(timer);
188 timer_free(timer);
189}
190
Marc-André Lureau3e0fad32019-01-17 15:43:58 +0400191static void net_slirp_timer_mod(void *timer, int64_t expire_timer,
192 void *opaque)
Marc-André Lureau07abf6d2019-01-17 15:43:37 +0400193{
194 timer_mod(timer, expire_timer);
195}
196
Marc-André Lureau3e0fad32019-01-17 15:43:58 +0400197static void net_slirp_register_poll_fd(int fd, void *opaque)
Marc-André Lureau848c7092019-01-17 15:43:41 +0400198{
199 qemu_fd_register(fd);
200}
201
Marc-André Lureau3e0fad32019-01-17 15:43:58 +0400202static void net_slirp_unregister_poll_fd(int fd, void *opaque)
Marc-André Lureauf6e5aa32019-01-17 15:43:42 +0400203{
204 /* no qemu_fd_unregister */
205}
206
Marc-André Lureau3e0fad32019-01-17 15:43:58 +0400207static void net_slirp_notify(void *opaque)
208{
209 qemu_notify_event();
210}
211
Marc-André Lureaud846b922018-11-14 16:36:07 +0400212static const SlirpCb slirp_cb = {
Marc-André Lureau625a5262019-01-17 15:43:54 +0400213 .send_packet = net_slirp_send_packet,
Marc-André Lureau2addc8f2018-11-14 16:36:33 +0400214 .guest_error = net_slirp_guest_error,
Marc-André Lureaue6dbff32018-11-22 02:06:28 +0400215 .clock_get_ns = net_slirp_clock_get_ns,
Marc-André Lureau07abf6d2019-01-17 15:43:37 +0400216 .timer_new = net_slirp_timer_new,
217 .timer_free = net_slirp_timer_free,
218 .timer_mod = net_slirp_timer_mod,
Marc-André Lureau848c7092019-01-17 15:43:41 +0400219 .register_poll_fd = net_slirp_register_poll_fd,
Marc-André Lureauf6e5aa32019-01-17 15:43:42 +0400220 .unregister_poll_fd = net_slirp_unregister_poll_fd,
Marc-André Lureau3e0fad32019-01-17 15:43:58 +0400221 .notify = net_slirp_notify,
Marc-André Lureaud846b922018-11-14 16:36:07 +0400222};
223
Marc-André Lureaudeaeb3f2019-01-17 15:43:57 +0400224static int slirp_poll_to_gio(int events)
225{
226 int ret = 0;
227
228 if (events & SLIRP_POLL_IN) {
229 ret |= G_IO_IN;
230 }
231 if (events & SLIRP_POLL_OUT) {
232 ret |= G_IO_OUT;
233 }
234 if (events & SLIRP_POLL_PRI) {
235 ret |= G_IO_PRI;
236 }
237 if (events & SLIRP_POLL_ERR) {
238 ret |= G_IO_ERR;
239 }
240 if (events & SLIRP_POLL_HUP) {
241 ret |= G_IO_HUP;
242 }
243
244 return ret;
245}
246
247static int net_slirp_add_poll(int fd, int events, void *opaque)
248{
249 GArray *pollfds = opaque;
250 GPollFD pfd = {
251 .fd = fd,
252 .events = slirp_poll_to_gio(events),
253 };
254 int idx = pollfds->len;
255 g_array_append_val(pollfds, pfd);
256 return idx;
257}
258
259static int slirp_gio_to_poll(int events)
260{
261 int ret = 0;
262
263 if (events & G_IO_IN) {
264 ret |= SLIRP_POLL_IN;
265 }
266 if (events & G_IO_OUT) {
267 ret |= SLIRP_POLL_OUT;
268 }
269 if (events & G_IO_PRI) {
270 ret |= SLIRP_POLL_PRI;
271 }
272 if (events & G_IO_ERR) {
273 ret |= SLIRP_POLL_ERR;
274 }
275 if (events & G_IO_HUP) {
276 ret |= SLIRP_POLL_HUP;
277 }
278
279 return ret;
280}
281
282static int net_slirp_get_revents(int idx, void *opaque)
283{
284 GArray *pollfds = opaque;
285
286 return slirp_gio_to_poll(g_array_index(pollfds, GPollFD, idx).revents);
287}
288
Marc-André Lureau1ab67b92019-01-17 15:43:55 +0400289static void net_slirp_poll_notify(Notifier *notifier, void *data)
290{
291 MainLoopPoll *poll = data;
292 SlirpState *s = container_of(notifier, SlirpState, poll_notifier);
293
294 switch (poll->state) {
295 case MAIN_LOOP_POLL_FILL:
Marc-André Lureaudeaeb3f2019-01-17 15:43:57 +0400296 slirp_pollfds_fill(s->slirp, &poll->timeout,
297 net_slirp_add_poll, poll->pollfds);
Marc-André Lureau1ab67b92019-01-17 15:43:55 +0400298 break;
299 case MAIN_LOOP_POLL_OK:
300 case MAIN_LOOP_POLL_ERR:
Marc-André Lureaudeaeb3f2019-01-17 15:43:57 +0400301 slirp_pollfds_poll(s->slirp, poll->state == MAIN_LOOP_POLL_ERR,
302 net_slirp_get_revents, poll->pollfds);
Marc-André Lureau1ab67b92019-01-17 15:43:55 +0400303 break;
304 default:
305 g_assert_not_reached();
306 }
307}
308
Marc-André Lureaud8903442019-02-12 17:25:19 +0100309static ssize_t
310net_slirp_stream_read(void *buf, size_t size, void *opaque)
311{
312 QEMUFile *f = opaque;
313
314 return qemu_get_buffer(f, buf, size);
315}
316
317static ssize_t
318net_slirp_stream_write(const void *buf, size_t size, void *opaque)
319{
320 QEMUFile *f = opaque;
321
322 qemu_put_buffer(f, buf, size);
323 if (qemu_file_get_error(f)) {
324 return -1;
325 }
326
327 return size;
328}
329
330static int net_slirp_state_load(QEMUFile *f, void *opaque, int version_id)
331{
332 Slirp *slirp = opaque;
333
334 return slirp_state_load(slirp, version_id, net_slirp_stream_read, f);
335}
336
337static void net_slirp_state_save(QEMUFile *f, void *opaque)
338{
339 Slirp *slirp = opaque;
340
341 slirp_state_save(slirp, net_slirp_stream_write, f);
342}
343
344static SaveVMHandlers savevm_slirp_state = {
345 .save_state = net_slirp_state_save,
346 .load_state = net_slirp_state_load,
347};
348
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100349static int net_slirp_init(NetClientState *peer, const char *model,
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000350 const char *name, int restricted,
Samuel Thibault0b11c032016-03-20 12:29:54 +0100351 bool ipv4, const char *vnetwork, const char *vhost,
352 bool ipv6, const char *vprefix6, int vprefix6_len,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100353 const char *vhost6,
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000354 const char *vhostname, const char *tftp_export,
355 const char *bootfile, const char *vdhcp_start,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100356 const char *vnameserver, const char *vnameserver6,
357 const char *smb_export, const char *vsmbserver,
Benjamin Drungf18d1372018-02-27 17:06:01 +0100358 const char **dnssearch, const char *vdomainname,
Fam Zheng0fca92b2018-09-14 15:26:16 +0800359 const char *tftp_server_name,
Benjamin Drungf18d1372018-02-27 17:06:01 +0100360 Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000361{
362 /* default settings according to historic slirp */
363 struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */
364 struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */
365 struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */
366 struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */
367 struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */
Yann Bordenave7aac5312016-03-15 10:31:22 +0100368 struct in6_addr ip6_prefix;
369 struct in6_addr ip6_host;
370 struct in6_addr ip6_dns;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000371#ifndef _WIN32
372 struct in_addr smbsrv = { .s_addr = 0 };
373#endif
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100374 NetClientState *nc;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000375 SlirpState *s;
376 char buf[20];
377 uint32_t addr;
378 int shift;
379 char *end;
380 struct slirp_config_str *config;
381
Samuel Thibault0b11c032016-03-20 12:29:54 +0100382 if (!ipv4 && (vnetwork || vhost || vnameserver)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200383 error_setg(errp, "IPv4 disabled but netmask/host/dns provided");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100384 return -1;
385 }
386
387 if (!ipv6 && (vprefix6 || vhost6 || vnameserver6)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200388 error_setg(errp, "IPv6 disabled but prefix/host6/dns6 provided");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100389 return -1;
390 }
391
392 if (!ipv4 && !ipv6) {
393 /* It doesn't make sense to disable both */
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200394 error_setg(errp, "IPv4 and IPv6 disabled");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100395 return -1;
396 }
397
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000398 if (vnetwork) {
399 if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) {
400 if (!inet_aton(vnetwork, &net)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200401 error_setg(errp, "Failed to parse netmask");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000402 return -1;
403 }
404 addr = ntohl(net.s_addr);
405 if (!(addr & 0x80000000)) {
406 mask.s_addr = htonl(0xff000000); /* class A */
407 } else if ((addr & 0xfff00000) == 0xac100000) {
408 mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */
409 } else if ((addr & 0xc0000000) == 0x80000000) {
410 mask.s_addr = htonl(0xffff0000); /* class B */
411 } else if ((addr & 0xffff0000) == 0xc0a80000) {
412 mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */
413 } else if ((addr & 0xffff0000) == 0xc6120000) {
414 mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */
415 } else if ((addr & 0xe0000000) == 0xe0000000) {
416 mask.s_addr = htonl(0xffffff00); /* class C */
417 } else {
418 mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */
419 }
420 } else {
421 if (!inet_aton(buf, &net)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200422 error_setg(errp, "Failed to parse netmask");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000423 return -1;
424 }
425 shift = strtol(vnetwork, &end, 10);
426 if (*end != '\0') {
427 if (!inet_aton(vnetwork, &mask)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200428 error_setg(errp,
429 "Failed to parse netmask (trailing chars)");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000430 return -1;
431 }
432 } else if (shift < 4 || shift > 32) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200433 error_setg(errp,
434 "Invalid netmask provided (must be in range 4-32)");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000435 return -1;
436 } else {
437 mask.s_addr = htonl(0xffffffff << (32 - shift));
438 }
439 }
440 net.s_addr &= mask.s_addr;
441 host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr);
442 dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr);
443 dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr);
444 }
445
446 if (vhost && !inet_aton(vhost, &host)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200447 error_setg(errp, "Failed to parse host");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000448 return -1;
449 }
450 if ((host.s_addr & mask.s_addr) != net.s_addr) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200451 error_setg(errp, "Host doesn't belong to network");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000452 return -1;
453 }
454
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000455 if (vnameserver && !inet_aton(vnameserver, &dns)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200456 error_setg(errp, "Failed to parse DNS");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000457 return -1;
458 }
Samuel Thibault120b7212019-09-29 20:08:20 +0200459 if (restricted && (dns.s_addr & mask.s_addr) != net.s_addr) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200460 error_setg(errp, "DNS doesn't belong to network");
461 return -1;
462 }
463 if (dns.s_addr == host.s_addr) {
464 error_setg(errp, "DNS must be different from host");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000465 return -1;
466 }
467
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200468 if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200469 error_setg(errp, "Failed to parse DHCP start address");
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200470 return -1;
471 }
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200472 if ((dhcp.s_addr & mask.s_addr) != net.s_addr) {
473 error_setg(errp, "DHCP doesn't belong to network");
474 return -1;
475 }
476 if (dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) {
477 error_setg(errp, "DNS must be different from host and DNS");
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200478 return -1;
479 }
480
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000481#ifndef _WIN32
482 if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200483 error_setg(errp, "Failed to parse SMB address");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000484 return -1;
485 }
486#endif
487
Yann Bordenave7aac5312016-03-15 10:31:22 +0100488 if (!vprefix6) {
489 vprefix6 = "fec0::";
490 }
491 if (!inet_pton(AF_INET6, vprefix6, &ip6_prefix)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200492 error_setg(errp, "Failed to parse IPv6 prefix");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100493 return -1;
494 }
Yann Bordenave7aac5312016-03-15 10:31:22 +0100495
496 if (!vprefix6_len) {
497 vprefix6_len = 64;
498 }
499 if (vprefix6_len < 0 || vprefix6_len > 126) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200500 error_setg(errp,
Stefano Garzarella178a0a52019-05-15 11:08:05 +0200501 "Invalid IPv6 prefix provided "
502 "(IPv6 prefix length must be between 0 and 126)");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100503 return -1;
504 }
505
506 if (vhost6) {
Yann Bordenave7aac5312016-03-15 10:31:22 +0100507 if (!inet_pton(AF_INET6, vhost6, &ip6_host)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200508 error_setg(errp, "Failed to parse IPv6 host");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100509 return -1;
510 }
511 if (!in6_equal_net(&ip6_prefix, &ip6_host, vprefix6_len)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200512 error_setg(errp, "IPv6 Host doesn't belong to network");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100513 return -1;
514 }
Yann Bordenave7aac5312016-03-15 10:31:22 +0100515 } else {
516 ip6_host = ip6_prefix;
517 ip6_host.s6_addr[15] |= 2;
518 }
519
520 if (vnameserver6) {
Yann Bordenave7aac5312016-03-15 10:31:22 +0100521 if (!inet_pton(AF_INET6, vnameserver6, &ip6_dns)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200522 error_setg(errp, "Failed to parse IPv6 DNS");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100523 return -1;
524 }
Samuel Thibault120b7212019-09-29 20:08:20 +0200525 if (restricted && !in6_equal_net(&ip6_prefix, &ip6_dns, vprefix6_len)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200526 error_setg(errp, "IPv6 DNS doesn't belong to network");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100527 return -1;
528 }
Yann Bordenave7aac5312016-03-15 10:31:22 +0100529 } else {
530 ip6_dns = ip6_prefix;
531 ip6_dns.s6_addr[15] |= 3;
532 }
533
Benjamin Drungf18d1372018-02-27 17:06:01 +0100534 if (vdomainname && !*vdomainname) {
535 error_setg(errp, "'domainname' parameter cannot be empty");
536 return -1;
537 }
538
Fam Zheng6e157a02018-09-14 15:26:15 +0800539 if (vdomainname && strlen(vdomainname) > 255) {
540 error_setg(errp, "'domainname' parameter cannot exceed 255 bytes");
541 return -1;
542 }
543
544 if (vhostname && strlen(vhostname) > 255) {
545 error_setg(errp, "'vhostname' parameter cannot exceed 255 bytes");
546 return -1;
547 }
Yann Bordenave7aac5312016-03-15 10:31:22 +0100548
Fam Zheng0fca92b2018-09-14 15:26:16 +0800549 if (tftp_server_name && strlen(tftp_server_name) > 255) {
550 error_setg(errp, "'tftp-server-name' parameter cannot exceed 255 bytes");
551 return -1;
552 }
553
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100554 nc = qemu_new_net_client(&net_slirp_info, peer, model, name);
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000555
556 snprintf(nc->info_str, sizeof(nc->info_str),
Jan Kiszkac54ed5b2011-07-20 12:20:14 +0200557 "net=%s,restrict=%s", inet_ntoa(net),
558 restricted ? "on" : "off");
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000559
560 s = DO_UPCAST(SlirpState, nc, nc);
561
Samuel Thibault0b11c032016-03-20 12:29:54 +0100562 s->slirp = slirp_init(restricted, ipv4, net, mask, host,
563 ipv6, ip6_prefix, vprefix6_len, ip6_host,
Fam Zheng0fca92b2018-09-14 15:26:16 +0800564 vhostname, tftp_server_name,
565 tftp_export, bootfile, dhcp,
Marc-André Lureau62c1d2c2018-11-10 17:45:36 +0400566 dns, ip6_dns, dnssearch, vdomainname,
Marc-André Lureaud846b922018-11-14 16:36:07 +0400567 &slirp_cb, s);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000568 QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry);
569
Marc-André Lureaud8903442019-02-12 17:25:19 +0100570 /*
571 * Make sure the current bitstream version of slirp is 4, to avoid
572 * QEMU migration incompatibilities, if upstream slirp bumped the
573 * version.
574 *
575 * FIXME: use bitfields of features? teach libslirp to save with
576 * specific version?
577 */
578 g_assert(slirp_state_version() == 4);
Dr. David Alan Gilbertce62df52019-08-22 12:54:33 +0100579 register_savevm_live("slirp", 0, slirp_state_version(),
Marc-André Lureaud8903442019-02-12 17:25:19 +0100580 &savevm_slirp_state, s->slirp);
581
Marc-André Lureau1ab67b92019-01-17 15:43:55 +0400582 s->poll_notifier.notify = net_slirp_poll_notify;
583 main_loop_poll_add_notifier(&s->poll_notifier);
584
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000585 for (config = slirp_configs; config; config = config->next) {
586 if (config->flags & SLIRP_CFG_HOSTFWD) {
Thomas Huthd18572d2018-08-22 15:43:30 +0200587 if (slirp_hostfwd(s, config->str, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000588 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200589 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000590 } else {
Thomas Huthd18572d2018-08-22 15:43:30 +0200591 if (slirp_guestfwd(s, config->str, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000592 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200593 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000594 }
595 }
596#ifndef _WIN32
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000597 if (smb_export) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200598 if (slirp_smb(s, smb_export, smbsrv, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000599 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200600 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000601 }
602#endif
603
Paolo Bonzinif6c2e662016-07-12 09:57:12 +0200604 s->exit_notifier.notify = slirp_smb_exit;
605 qemu_add_exit_notifier(&s->exit_notifier);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000606 return 0;
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000607
608error:
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100609 qemu_del_net_client(nc);
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000610 return -1;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000611}
612
Thomas Huth93653062018-01-11 21:02:40 +0100613static SlirpState *slirp_lookup(Monitor *mon, const char *hub_id,
614 const char *name)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000615{
Thomas Huth93653062018-01-11 21:02:40 +0100616 if (name) {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100617 NetClientState *nc;
Thomas Huth93653062018-01-11 21:02:40 +0100618 if (hub_id) {
619 nc = net_hub_find_client_by_name(strtol(hub_id, NULL, 0), name);
620 if (!nc) {
Thomas Huth442da402018-04-30 20:02:24 +0200621 monitor_printf(mon, "unrecognized (hub-id, stackname) pair\n");
Thomas Huth93653062018-01-11 21:02:40 +0100622 return NULL;
623 }
Thomas Huth68cb29e2018-09-20 10:22:27 +0200624 warn_report("Using 'hub-id' is deprecated, specify the netdev id "
625 "directly instead");
Thomas Huth93653062018-01-11 21:02:40 +0100626 } else {
627 nc = qemu_find_netdev(name);
628 if (!nc) {
629 monitor_printf(mon, "unrecognized netdev id '%s'\n", name);
630 return NULL;
631 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000632 }
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000633 if (strcmp(nc->model, "user")) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000634 monitor_printf(mon, "invalid device specified\n");
635 return NULL;
636 }
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000637 return DO_UPCAST(SlirpState, nc, nc);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000638 } else {
639 if (QTAILQ_EMPTY(&slirp_stacks)) {
640 monitor_printf(mon, "user mode network stack not in use\n");
641 return NULL;
642 }
643 return QTAILQ_FIRST(&slirp_stacks);
644 }
645}
646
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100647void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000648{
649 struct in_addr host_addr = { .s_addr = INADDR_ANY };
650 int host_port;
Markus Armbrustere30e5eb2011-11-16 15:45:59 +0100651 char buf[256];
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000652 const char *src_str, *p;
653 SlirpState *s;
654 int is_udp = 0;
655 int err;
656 const char *arg1 = qdict_get_str(qdict, "arg1");
657 const char *arg2 = qdict_get_try_str(qdict, "arg2");
658 const char *arg3 = qdict_get_try_str(qdict, "arg3");
659
Thomas Huth93653062018-01-11 21:02:40 +0100660 if (arg3) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000661 s = slirp_lookup(mon, arg1, arg2);
662 src_str = arg3;
Thomas Huth93653062018-01-11 21:02:40 +0100663 } else if (arg2) {
664 s = slirp_lookup(mon, NULL, arg1);
665 src_str = arg2;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000666 } else {
667 s = slirp_lookup(mon, NULL, NULL);
668 src_str = arg1;
669 }
670 if (!s) {
671 return;
672 }
673
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000674 p = src_str;
Markus Armbrustere30e5eb2011-11-16 15:45:59 +0100675 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
676 goto fail_syntax;
677 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000678
679 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
680 is_udp = 0;
681 } else if (!strcmp(buf, "udp")) {
682 is_udp = 1;
683 } else {
684 goto fail_syntax;
685 }
686
687 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
688 goto fail_syntax;
689 }
690 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
691 goto fail_syntax;
692 }
693
Nia Alarie1fb3f7f2018-03-16 14:39:21 +0000694 if (qemu_strtoi(p, NULL, 10, &host_port)) {
695 goto fail_syntax;
696 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000697
Peter Maydell70381662014-06-16 16:47:49 +0100698 err = slirp_remove_hostfwd(s->slirp, is_udp, host_addr, host_port);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000699
700 monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
Geoffrey Thomasb15ba6c2011-12-17 04:23:59 -0500701 err ? "not found" : "removed");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000702 return;
703
704 fail_syntax:
705 monitor_printf(mon, "invalid format\n");
706}
707
Thomas Huthd18572d2018-08-22 15:43:30 +0200708static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000709{
710 struct in_addr host_addr = { .s_addr = INADDR_ANY };
711 struct in_addr guest_addr = { .s_addr = 0 };
712 int host_port, guest_port;
713 const char *p;
714 char buf[256];
715 int is_udp;
716 char *end;
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100717 const char *fail_reason = "Unknown reason";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000718
719 p = redir_str;
720 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100721 fail_reason = "No : separators";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000722 goto fail_syntax;
723 }
724 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
725 is_udp = 0;
726 } else if (!strcmp(buf, "udp")) {
727 is_udp = 1;
728 } else {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100729 fail_reason = "Bad protocol name";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000730 goto fail_syntax;
731 }
732
Thomas Huthd18572d2018-08-22 15:43:30 +0200733 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
734 fail_reason = "Missing : separator";
735 goto fail_syntax;
736 }
737 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
738 fail_reason = "Bad host address";
739 goto fail_syntax;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000740 }
741
Thomas Huthd18572d2018-08-22 15:43:30 +0200742 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100743 fail_reason = "Bad host port separator";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000744 goto fail_syntax;
745 }
746 host_port = strtol(buf, &end, 0);
Vincent Bernat0bed71e2017-02-25 22:31:58 +0100747 if (*end != '\0' || host_port < 0 || host_port > 65535) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100748 fail_reason = "Bad host port";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000749 goto fail_syntax;
750 }
751
752 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100753 fail_reason = "Missing guest address";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000754 goto fail_syntax;
755 }
756 if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100757 fail_reason = "Bad guest address";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000758 goto fail_syntax;
759 }
760
761 guest_port = strtol(p, &end, 0);
762 if (*end != '\0' || guest_port < 1 || guest_port > 65535) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100763 fail_reason = "Bad guest port";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000764 goto fail_syntax;
765 }
766
767 if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr,
768 guest_port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200769 error_setg(errp, "Could not set up host forwarding rule '%s'",
770 redir_str);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000771 return -1;
772 }
773 return 0;
774
775 fail_syntax:
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100776 error_setg(errp, "Invalid host forwarding rule '%s' (%s)", redir_str,
777 fail_reason);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000778 return -1;
779}
780
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100781void hmp_hostfwd_add(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000782{
783 const char *redir_str;
784 SlirpState *s;
785 const char *arg1 = qdict_get_str(qdict, "arg1");
786 const char *arg2 = qdict_get_try_str(qdict, "arg2");
787 const char *arg3 = qdict_get_try_str(qdict, "arg3");
788
Thomas Huth93653062018-01-11 21:02:40 +0100789 if (arg3) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000790 s = slirp_lookup(mon, arg1, arg2);
791 redir_str = arg3;
Thomas Huth93653062018-01-11 21:02:40 +0100792 } else if (arg2) {
793 s = slirp_lookup(mon, NULL, arg1);
794 redir_str = arg2;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000795 } else {
796 s = slirp_lookup(mon, NULL, NULL);
797 redir_str = arg1;
798 }
799 if (s) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200800 Error *err = NULL;
Thomas Huthd18572d2018-08-22 15:43:30 +0200801 if (slirp_hostfwd(s, redir_str, &err) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200802 error_report_err(err);
803 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000804 }
805
806}
807
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000808#ifndef _WIN32
809
810/* automatic user mode samba server configuration */
811static void slirp_smb_cleanup(SlirpState *s)
812{
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100813 int ret;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000814
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100815 if (s->smb_dir) {
816 gchar *cmd = g_strdup_printf("rm -rf %s", s->smb_dir);
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100817 ret = system(cmd);
Juan Quintela24ac07d2010-03-04 10:00:31 +0100818 if (ret == -1 || !WIFEXITED(ret)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100819 error_report("'%s' failed.", cmd);
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100820 } else if (WEXITSTATUS(ret)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100821 error_report("'%s' failed. Error code: %d",
822 cmd, WEXITSTATUS(ret));
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100823 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100824 g_free(cmd);
825 g_free(s->smb_dir);
826 s->smb_dir = NULL;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000827 }
828}
829
830static int slirp_smb(SlirpState* s, const char *exported_dir,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200831 struct in_addr vserver_addr, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000832{
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100833 char *smb_conf;
834 char *smb_cmdline;
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200835 struct passwd *passwd;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000836 FILE *f;
837
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200838 passwd = getpwuid(geteuid());
839 if (!passwd) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200840 error_setg(errp, "Failed to retrieve user name");
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200841 return -1;
842 }
843
Dunrong Huang927d8112012-07-06 14:04:43 +0800844 if (access(CONFIG_SMBD_COMMAND, F_OK)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200845 error_setg(errp, "Could not find '%s', please install it",
846 CONFIG_SMBD_COMMAND);
Dunrong Huang927d8112012-07-06 14:04:43 +0800847 return -1;
848 }
849
850 if (access(exported_dir, R_OK | X_OK)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200851 error_setg(errp, "Error accessing shared directory '%s': %s",
852 exported_dir, strerror(errno));
Dunrong Huang927d8112012-07-06 14:04:43 +0800853 return -1;
854 }
855
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100856 s->smb_dir = g_dir_make_tmp("qemu-smb.XXXXXX", NULL);
857 if (!s->smb_dir) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200858 error_setg(errp, "Could not create samba server dir");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000859 return -1;
860 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100861 smb_conf = g_strdup_printf("%s/%s", s->smb_dir, "smb.conf");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000862
863 f = fopen(smb_conf, "w");
864 if (!f) {
865 slirp_smb_cleanup(s);
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200866 error_setg(errp,
867 "Could not create samba server configuration file '%s'",
868 smb_conf);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100869 g_free(smb_conf);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000870 return -1;
871 }
872 fprintf(f,
873 "[global]\n"
874 "private dir=%s\n"
Peter Wu7912d042014-11-03 11:52:10 +0100875 "interfaces=127.0.0.1\n"
876 "bind interfaces only=yes\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000877 "pid directory=%s\n"
878 "lock directory=%s\n"
Nikolaus Rath276eda52012-04-25 09:57:19 -0400879 "state directory=%s\n"
Peter Wu7912d042014-11-03 11:52:10 +0100880 "cache directory=%s\n"
Michael Bueschb87b8a82014-04-27 14:54:12 +0400881 "ncalrpc dir=%s/ncalrpc\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000882 "log file=%s/log.smbd\n"
883 "smb passwd file=%s/smbpasswd\n"
Michael Bueschc2804ee2013-11-01 12:23:49 +0100884 "security = user\n"
885 "map to guest = Bad User\n"
Peter Wu7912d042014-11-03 11:52:10 +0100886 "load printers = no\n"
887 "printing = bsd\n"
888 "disable spoolss = yes\n"
889 "usershare max shares = 0\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000890 "[qemu]\n"
891 "path=%s\n"
892 "read only=no\n"
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200893 "guest ok=yes\n"
894 "force user=%s\n",
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000895 s->smb_dir,
896 s->smb_dir,
897 s->smb_dir,
898 s->smb_dir,
899 s->smb_dir,
Nikolaus Rath276eda52012-04-25 09:57:19 -0400900 s->smb_dir,
Michael Bueschb87b8a82014-04-27 14:54:12 +0400901 s->smb_dir,
Peter Wu7912d042014-11-03 11:52:10 +0100902 s->smb_dir,
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200903 exported_dir,
904 passwd->pw_name
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000905 );
906 fclose(f);
907
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100908 smb_cmdline = g_strdup_printf("%s -l %s -s %s",
Michael Tokarev44d8d2b2014-10-25 00:29:50 +0400909 CONFIG_SMBD_COMMAND, s->smb_dir, smb_conf);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100910 g_free(smb_conf);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000911
Marc-André Lureau44b4ff22019-01-17 15:43:33 +0400912 if (slirp_add_exec(s->slirp, smb_cmdline, &vserver_addr, 139) < 0 ||
913 slirp_add_exec(s->slirp, smb_cmdline, &vserver_addr, 445) < 0) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000914 slirp_smb_cleanup(s);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100915 g_free(smb_cmdline);
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200916 error_setg(errp, "Conflicting/invalid smbserver address");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000917 return -1;
918 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100919 g_free(smb_cmdline);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000920 return 0;
921}
922
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000923#endif /* !defined(_WIN32) */
924
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000925static int guestfwd_can_read(void *opaque)
926{
927 struct GuestFwd *fwd = opaque;
928 return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port);
929}
930
931static void guestfwd_read(void *opaque, const uint8_t *buf, int size)
932{
933 struct GuestFwd *fwd = opaque;
934 slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size);
935}
936
Marc-André Lureau625a5262019-01-17 15:43:54 +0400937static ssize_t guestfwd_write(const void *buf, size_t len, void *chr)
Marc-André Lureau44b4ff22019-01-17 15:43:33 +0400938{
939 return qemu_chr_fe_write_all(chr, buf, len);
940}
941
Thomas Huthd18572d2018-08-22 15:43:30 +0200942static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000943{
Samuel Thibaultffe02f52016-04-01 00:46:35 +0200944 /* TODO: IPv6 */
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000945 struct in_addr server = { .s_addr = 0 };
946 struct GuestFwd *fwd;
947 const char *p;
948 char buf[128];
949 char *end;
950 int port;
951
952 p = config_str;
Thomas Huthd18572d2018-08-22 15:43:30 +0200953 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
954 goto fail_syntax;
955 }
956 if (strcmp(buf, "tcp") && buf[0] != '\0') {
957 goto fail_syntax;
958 }
959 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
960 goto fail_syntax;
961 }
962 if (buf[0] != '\0' && !inet_aton(buf, &server)) {
963 goto fail_syntax;
964 }
965 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
966 goto fail_syntax;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000967 }
968 port = strtol(buf, &end, 10);
969 if (*end != '\0' || port < 1 || port > 65535) {
970 goto fail_syntax;
971 }
972
Alexander Grafa9899992011-06-04 07:25:59 +0200973 snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000974
Marc-André Lureau36247302019-01-17 15:43:34 +0400975 if (g_str_has_prefix(p, "cmd:")) {
Marc-André Lureau44b4ff22019-01-17 15:43:33 +0400976 if (slirp_add_exec(s->slirp, &p[4], &server, port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200977 error_setg(errp, "Conflicting/invalid host:port in guest "
978 "forwarding rule '%s'", config_str);
Alexander Grafb412eb62012-06-03 09:45:01 +0200979 return -1;
980 }
981 } else {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300982 Error *err = NULL;
Marc-André Lureau95e30b22018-08-22 19:19:42 +0200983 /*
984 * FIXME: sure we want to support implicit
985 * muxed monitors here?
986 */
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +0100987 Chardev *chr = qemu_chr_new_mux_mon(buf, p, NULL);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300988
989 if (!chr) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200990 error_setg(errp, "Could not open guest forwarding device '%s'",
991 buf);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300992 return -1;
993 }
994
995 fwd = g_new(struct GuestFwd, 1);
996 qemu_chr_fe_init(&fwd->hd, chr, &err);
997 if (err) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200998 error_propagate(errp, err);
Marc-André Lureau8e207c32019-01-17 15:43:36 +0400999 object_unparent(OBJECT(chr));
Alexander Grafb412eb62012-06-03 09:45:01 +02001000 g_free(fwd);
1001 return -1;
1002 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001003
Marc-André Lureau44b4ff22019-01-17 15:43:33 +04001004 if (slirp_add_guestfwd(s->slirp, guestfwd_write, &fwd->hd,
1005 &server, port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +02001006 error_setg(errp, "Conflicting/invalid host:port in guest "
1007 "forwarding rule '%s'", config_str);
Marc-André Lureau8e207c32019-01-17 15:43:36 +04001008 qemu_chr_fe_deinit(&fwd->hd, true);
Alexander Grafb412eb62012-06-03 09:45:01 +02001009 g_free(fwd);
1010 return -1;
1011 }
1012 fwd->server = server;
1013 fwd->port = port;
1014 fwd->slirp = s->slirp;
1015
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001016 qemu_chr_fe_set_handlers(&fwd->hd, guestfwd_can_read, guestfwd_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +03001017 NULL, NULL, fwd, NULL, true);
Marc-André Lureau8d45a3b2019-01-17 15:43:35 +04001018 s->fwd = g_slist_append(s->fwd, fwd);
Alexander Grafb412eb62012-06-03 09:45:01 +02001019 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001020 return 0;
1021
1022 fail_syntax:
Hervé Poussineau5c843af2017-07-15 18:43:50 +02001023 error_setg(errp, "Invalid guest forwarding rule '%s'", config_str);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001024 return -1;
1025}
1026
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001027void hmp_info_usernet(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001028{
1029 SlirpState *s;
1030
1031 QTAILQ_FOREACH(s, &slirp_stacks, entry) {
Stefan Hajnoczi90d87a32012-07-24 16:35:06 +01001032 int id;
Thomas Huth442da402018-04-30 20:02:24 +02001033 bool got_hub_id = net_hub_id_for_client(&s->nc, &id) == 0;
Marc-André Lureaub7f43bf2018-11-10 17:45:43 +04001034 char *info = slirp_connection_info(s->slirp);
1035 monitor_printf(mon, "Hub %d (%s):\n%s",
Thomas Huth442da402018-04-30 20:02:24 +02001036 got_hub_id ? id : -1,
Marc-André Lureaub7f43bf2018-11-10 17:45:43 +04001037 s->nc.name, info);
1038 g_free(info);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001039 }
1040}
1041
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001042static void
1043net_init_slirp_configs(const StringList *fwd, int flags)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001044{
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001045 while (fwd) {
1046 struct slirp_config_str *config;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001047
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001048 config = g_malloc0(sizeof(*config));
1049 pstrcpy(config->str, sizeof(config->str), fwd->value->str);
1050 config->flags = flags;
1051 config->next = slirp_configs;
1052 slirp_configs = config;
1053
1054 fwd = fwd->next;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001055 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001056}
1057
Klaus Stengel63d29602012-10-27 19:53:39 +02001058static const char **slirp_dnssearch(const StringList *dnsname)
1059{
1060 const StringList *c = dnsname;
1061 size_t i = 0, num_opts = 0;
1062 const char **ret;
1063
1064 while (c) {
1065 num_opts++;
1066 c = c->next;
1067 }
1068
1069 if (num_opts == 0) {
1070 return NULL;
1071 }
1072
1073 ret = g_malloc((num_opts + 1) * sizeof(*ret));
1074 c = dnsname;
1075 while (c) {
1076 ret[i++] = c->value->str;
1077 c = c->next;
1078 }
1079 ret[i] = NULL;
1080 return ret;
1081}
1082
Kővágó, Zoltáncebea512016-07-13 21:50:12 -06001083int net_init_slirp(const Netdev *netdev, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +02001084 NetClientState *peer, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001085{
1086 struct slirp_config_str *config;
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001087 char *vnet;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001088 int ret;
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001089 const NetdevUserOptions *user;
Klaus Stengel63d29602012-10-27 19:53:39 +02001090 const char **dnssearch;
Samuel Thibault0b11c032016-03-20 12:29:54 +01001091 bool ipv4 = true, ipv6 = true;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001092
Eric Blakef394b2e2016-07-13 21:50:23 -06001093 assert(netdev->type == NET_CLIENT_DRIVER_USER);
1094 user = &netdev->u.user;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001095
Samuel Thibault0b11c032016-03-20 12:29:54 +01001096 if ((user->has_ipv6 && user->ipv6 && !user->has_ipv4) ||
1097 (user->has_ipv4 && !user->ipv4)) {
1098 ipv4 = 0;
1099 }
1100 if ((user->has_ipv4 && user->ipv4 && !user->has_ipv6) ||
1101 (user->has_ipv6 && !user->ipv6)) {
1102 ipv6 = 0;
1103 }
1104
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001105 vnet = user->has_net ? g_strdup(user->net) :
1106 user->has_ip ? g_strdup_printf("%s/24", user->ip) :
1107 NULL;
Jan Kiszkac54ed5b2011-07-20 12:20:14 +02001108
Klaus Stengel63d29602012-10-27 19:53:39 +02001109 dnssearch = slirp_dnssearch(user->dnssearch);
1110
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001111 /* all optional fields are initialized to "all bits zero" */
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001112
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001113 net_init_slirp_configs(user->hostfwd, SLIRP_CFG_HOSTFWD);
1114 net_init_slirp_configs(user->guestfwd, 0);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001115
Samuel Thibault0b11c032016-03-20 12:29:54 +01001116 ret = net_slirp_init(peer, "user", name, user->q_restrict,
1117 ipv4, vnet, user->host,
1118 ipv6, user->ipv6_prefix, user->ipv6_prefixlen,
Samuel Thibaultd8eb3862016-03-25 00:02:58 +01001119 user->ipv6_host, user->hostname, user->tftp,
Yann Bordenave7aac5312016-03-15 10:31:22 +01001120 user->bootfile, user->dhcpstart,
Samuel Thibaultd8eb3862016-03-25 00:02:58 +01001121 user->dns, user->ipv6_dns, user->smb,
Fam Zheng0fca92b2018-09-14 15:26:16 +08001122 user->smbserver, dnssearch, user->domainname,
1123 user->tftp_server_name, errp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001124
1125 while (slirp_configs) {
1126 config = slirp_configs;
1127 slirp_configs = config->next;
Anthony Liguori7267c092011-08-20 22:09:37 -05001128 g_free(config);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001129 }
1130
Anthony Liguori7267c092011-08-20 22:09:37 -05001131 g_free(vnet);
Klaus Stengel63d29602012-10-27 19:53:39 +02001132 g_free(dnssearch);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001133
1134 return ret;
1135}