blob: 2ee3f1a0d7ca484d4dff36c73e74af31154c5f05 [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
Paolo Bonzini35acbb32021-10-13 13:43:36 +020030#if defined(CONFIG_SMBD_COMMAND)
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
Bin Meng969e50b2021-03-17 14:26:29 +080034#include "net/eth.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020035#include "net/net.h"
Paolo Bonzinia245fc12012-09-17 18:43:51 +020036#include "clients.h"
37#include "hub.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010038#include "monitor/monitor.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010039#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010040#include "qemu/sockets.h"
Marc-André Lureau675b9b52019-02-12 17:25:23 +010041#include <libslirp.h>
Marc-André Lureau4d43a602017-01-26 18:26:44 +040042#include "chardev/char-fe.h"
Paolo Bonzinif6c2e662016-07-12 09:57:12 +020043#include "sysemu/sysemu.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020044#include "qemu/cutils.h"
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +030045#include "qapi/error.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010046#include "qapi/qmp/qdict.h"
Marc-André Lureaue05ae1d2018-11-14 16:36:40 +040047#include "util.h"
Marc-André Lureaud8903442019-02-12 17:25:19 +010048#include "migration/register.h"
49#include "migration/qemu-file-types.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000050
51static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
52{
53 const char *p, *p1;
54 int len;
55 p = *pp;
56 p1 = strchr(p, sep);
57 if (!p1)
58 return -1;
59 len = p1 - p;
60 p1++;
61 if (buf_size > 0) {
62 if (len > buf_size - 1)
63 len = buf_size - 1;
64 memcpy(buf, p, len);
65 buf[len] = '\0';
66 }
67 *pp = p1;
68 return 0;
69}
70
71/* slirp network adapter */
72
73#define SLIRP_CFG_HOSTFWD 1
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000074
75struct slirp_config_str {
76 struct slirp_config_str *next;
77 int flags;
78 char str[1024];
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000079};
80
Marc-André Lureau8d45a3b2019-01-17 15:43:35 +040081struct GuestFwd {
82 CharBackend hd;
83 struct in_addr server;
84 int port;
85 Slirp *slirp;
86};
87
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000088typedef struct SlirpState {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010089 NetClientState nc;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000090 QTAILQ_ENTRY(SlirpState) entry;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000091 Slirp *slirp;
Marc-André Lureau1ab67b92019-01-17 15:43:55 +040092 Notifier poll_notifier;
Paolo Bonzinif6c2e662016-07-12 09:57:12 +020093 Notifier exit_notifier;
Paolo Bonzini35acbb32021-10-13 13:43:36 +020094#if defined(CONFIG_SMBD_COMMAND)
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +010095 gchar *smb_dir;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000096#endif
Marc-André Lureau8d45a3b2019-01-17 15:43:35 +040097 GSList *fwd;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000098} SlirpState;
99
100static struct slirp_config_str *slirp_configs;
Paolo Bonzinib58deb32018-12-06 11:58:10 +0100101static QTAILQ_HEAD(, SlirpState) slirp_stacks =
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000102 QTAILQ_HEAD_INITIALIZER(slirp_stacks);
103
Thomas Huthd18572d2018-08-22 15:43:30 +0200104static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp);
105static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000106
Paolo Bonzini35acbb32021-10-13 13:43:36 +0200107#if defined(CONFIG_SMBD_COMMAND)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000108static int slirp_smb(SlirpState *s, const char *exported_dir,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200109 struct in_addr vserver_addr, Error **errp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000110static void slirp_smb_cleanup(SlirpState *s);
111#else
112static inline void slirp_smb_cleanup(SlirpState *s) { }
113#endif
114
Marc-André Lureau625a5262019-01-17 15:43:54 +0400115static ssize_t net_slirp_send_packet(const void *pkt, size_t pkt_len,
116 void *opaque)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000117{
118 SlirpState *s = opaque;
Bin Meng969e50b2021-03-17 14:26:29 +0800119 uint8_t min_pkt[ETH_ZLEN];
120 size_t min_pktsz = sizeof(min_pkt);
121
Jason Wangbc38e312021-04-23 11:18:03 +0800122 if (net_peer_needs_padding(&s->nc)) {
Bin Meng969e50b2021-03-17 14:26:29 +0800123 if (eth_pad_short_frame(min_pkt, &min_pktsz, pkt, pkt_len)) {
124 pkt = min_pkt;
125 pkt_len = min_pktsz;
126 }
127 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000128
Marc-André Lureau625a5262019-01-17 15:43:54 +0400129 return qemu_send_packet(&s->nc, pkt, pkt_len);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000130}
131
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100132static ssize_t net_slirp_receive(NetClientState *nc, const uint8_t *buf, size_t size)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000133{
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000134 SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000135
136 slirp_input(s->slirp, buf, size);
137
138 return size;
139}
140
Paolo Bonzinif6c2e662016-07-12 09:57:12 +0200141static void slirp_smb_exit(Notifier *n, void *data)
142{
143 SlirpState *s = container_of(n, SlirpState, exit_notifier);
144 slirp_smb_cleanup(s);
145}
146
Marc-André Lureau8d45a3b2019-01-17 15:43:35 +0400147static void slirp_free_fwd(gpointer data)
148{
149 struct GuestFwd *fwd = data;
150
151 qemu_chr_fe_deinit(&fwd->hd, true);
152 g_free(data);
153}
154
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100155static void net_slirp_cleanup(NetClientState *nc)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000156{
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000157 SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000158
Marc-André Lureau8d45a3b2019-01-17 15:43:35 +0400159 g_slist_free_full(s->fwd, slirp_free_fwd);
Marc-André Lureau1ab67b92019-01-17 15:43:55 +0400160 main_loop_poll_remove_notifier(&s->poll_notifier);
Marc-André Lureaud8903442019-02-12 17:25:19 +0100161 unregister_savevm(NULL, "slirp", s->slirp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000162 slirp_cleanup(s->slirp);
Marc-André Lureau67f32802016-08-18 17:44:05 +0400163 if (s->exit_notifier.notify) {
164 qemu_remove_exit_notifier(&s->exit_notifier);
165 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000166 slirp_smb_cleanup(s);
167 QTAILQ_REMOVE(&slirp_stacks, s, entry);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000168}
169
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000170static NetClientInfo net_slirp_info = {
Eric Blakef394b2e2016-07-13 21:50:23 -0600171 .type = NET_CLIENT_DRIVER_USER,
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000172 .size = sizeof(SlirpState),
173 .receive = net_slirp_receive,
174 .cleanup = net_slirp_cleanup,
175};
176
Marc-André Lureau3e0fad32019-01-17 15:43:58 +0400177static void net_slirp_guest_error(const char *msg, void *opaque)
Marc-André Lureau2addc8f2018-11-14 16:36:33 +0400178{
179 qemu_log_mask(LOG_GUEST_ERROR, "%s", msg);
180}
181
Marc-André Lureau3e0fad32019-01-17 15:43:58 +0400182static int64_t net_slirp_clock_get_ns(void *opaque)
Marc-André Lureaue6dbff32018-11-22 02:06:28 +0400183{
184 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
185}
186
Paolo Bonzini6222e552022-04-11 09:39:16 +0200187typedef struct SlirpTimer SlirpTimer;
Paolo Bonziniad2e5b82022-04-11 09:26:06 +0200188struct SlirpTimer {
189 QEMUTimer timer;
Paolo Bonzini6222e552022-04-11 09:39:16 +0200190#if SLIRP_CHECK_VERSION(4,7,0)
191 Slirp *slirp;
192 SlirpTimerId id;
193 void *cb_opaque;
194#endif
195};
196
197#if SLIRP_CHECK_VERSION(4,7,0)
198static void net_slirp_init_completed(Slirp *slirp, void *opaque)
199{
200 SlirpState *s = opaque;
201 s->slirp = slirp;
Paolo Bonziniad2e5b82022-04-11 09:26:06 +0200202}
203
Paolo Bonzini6222e552022-04-11 09:39:16 +0200204static void net_slirp_timer_cb(void *opaque)
205{
206 SlirpTimer *t = opaque;
207 slirp_handle_timer(t->slirp, t->id, t->cb_opaque);
208}
209
210static void *net_slirp_timer_new_opaque(SlirpTimerId id,
211 void *cb_opaque, void *opaque)
212{
213 SlirpState *s = opaque;
214 SlirpTimer *t = g_new(SlirpTimer, 1);
215 t->slirp = s->slirp;
216 t->id = id;
217 t->cb_opaque = cb_opaque;
218 timer_init_full(&t->timer, NULL, QEMU_CLOCK_VIRTUAL,
219 SCALE_MS, QEMU_TIMER_ATTR_EXTERNAL,
220 net_slirp_timer_cb, t);
221 return t;
222}
223#else
Marc-André Lureau3e0fad32019-01-17 15:43:58 +0400224static void *net_slirp_timer_new(SlirpTimerCb cb,
225 void *cb_opaque, void *opaque)
Marc-André Lureau07abf6d2019-01-17 15:43:37 +0400226{
Paolo Bonziniad2e5b82022-04-11 09:26:06 +0200227 SlirpTimer *t = g_new(SlirpTimer, 1);
228 timer_init_full(&t->timer, NULL, QEMU_CLOCK_VIRTUAL,
229 SCALE_MS, QEMU_TIMER_ATTR_EXTERNAL,
230 cb, cb_opaque);
231 return t;
Marc-André Lureau07abf6d2019-01-17 15:43:37 +0400232}
Paolo Bonzini6222e552022-04-11 09:39:16 +0200233#endif
Marc-André Lureau07abf6d2019-01-17 15:43:37 +0400234
Marc-André Lureau3e0fad32019-01-17 15:43:58 +0400235static void net_slirp_timer_free(void *timer, void *opaque)
Marc-André Lureau07abf6d2019-01-17 15:43:37 +0400236{
Paolo Bonziniad2e5b82022-04-11 09:26:06 +0200237 SlirpTimer *t = timer;
238 timer_del(&t->timer);
239 g_free(t);
Marc-André Lureau07abf6d2019-01-17 15:43:37 +0400240}
241
Marc-André Lureau3e0fad32019-01-17 15:43:58 +0400242static void net_slirp_timer_mod(void *timer, int64_t expire_timer,
243 void *opaque)
Marc-André Lureau07abf6d2019-01-17 15:43:37 +0400244{
Paolo Bonziniad2e5b82022-04-11 09:26:06 +0200245 SlirpTimer *t = timer;
246 timer_mod(&t->timer, expire_timer);
Marc-André Lureau07abf6d2019-01-17 15:43:37 +0400247}
248
Marc-André Lureau3e0fad32019-01-17 15:43:58 +0400249static void net_slirp_register_poll_fd(int fd, void *opaque)
Marc-André Lureau848c7092019-01-17 15:43:41 +0400250{
251 qemu_fd_register(fd);
252}
253
Marc-André Lureau3e0fad32019-01-17 15:43:58 +0400254static void net_slirp_unregister_poll_fd(int fd, void *opaque)
Marc-André Lureauf6e5aa32019-01-17 15:43:42 +0400255{
256 /* no qemu_fd_unregister */
257}
258
Marc-André Lureau3e0fad32019-01-17 15:43:58 +0400259static void net_slirp_notify(void *opaque)
260{
261 qemu_notify_event();
262}
263
Marc-André Lureaud846b922018-11-14 16:36:07 +0400264static const SlirpCb slirp_cb = {
Marc-André Lureau625a5262019-01-17 15:43:54 +0400265 .send_packet = net_slirp_send_packet,
Marc-André Lureau2addc8f2018-11-14 16:36:33 +0400266 .guest_error = net_slirp_guest_error,
Marc-André Lureaue6dbff32018-11-22 02:06:28 +0400267 .clock_get_ns = net_slirp_clock_get_ns,
Paolo Bonzini6222e552022-04-11 09:39:16 +0200268#if SLIRP_CHECK_VERSION(4,7,0)
269 .init_completed = net_slirp_init_completed,
270 .timer_new_opaque = net_slirp_timer_new_opaque,
271#else
Marc-André Lureau07abf6d2019-01-17 15:43:37 +0400272 .timer_new = net_slirp_timer_new,
Paolo Bonzini6222e552022-04-11 09:39:16 +0200273#endif
Marc-André Lureau07abf6d2019-01-17 15:43:37 +0400274 .timer_free = net_slirp_timer_free,
275 .timer_mod = net_slirp_timer_mod,
Marc-André Lureau848c7092019-01-17 15:43:41 +0400276 .register_poll_fd = net_slirp_register_poll_fd,
Marc-André Lureauf6e5aa32019-01-17 15:43:42 +0400277 .unregister_poll_fd = net_slirp_unregister_poll_fd,
Marc-André Lureau3e0fad32019-01-17 15:43:58 +0400278 .notify = net_slirp_notify,
Marc-André Lureaud846b922018-11-14 16:36:07 +0400279};
280
Marc-André Lureaudeaeb3f2019-01-17 15:43:57 +0400281static int slirp_poll_to_gio(int events)
282{
283 int ret = 0;
284
285 if (events & SLIRP_POLL_IN) {
286 ret |= G_IO_IN;
287 }
288 if (events & SLIRP_POLL_OUT) {
289 ret |= G_IO_OUT;
290 }
291 if (events & SLIRP_POLL_PRI) {
292 ret |= G_IO_PRI;
293 }
294 if (events & SLIRP_POLL_ERR) {
295 ret |= G_IO_ERR;
296 }
297 if (events & SLIRP_POLL_HUP) {
298 ret |= G_IO_HUP;
299 }
300
301 return ret;
302}
303
304static int net_slirp_add_poll(int fd, int events, void *opaque)
305{
306 GArray *pollfds = opaque;
307 GPollFD pfd = {
308 .fd = fd,
309 .events = slirp_poll_to_gio(events),
310 };
311 int idx = pollfds->len;
312 g_array_append_val(pollfds, pfd);
313 return idx;
314}
315
316static int slirp_gio_to_poll(int events)
317{
318 int ret = 0;
319
320 if (events & G_IO_IN) {
321 ret |= SLIRP_POLL_IN;
322 }
323 if (events & G_IO_OUT) {
324 ret |= SLIRP_POLL_OUT;
325 }
326 if (events & G_IO_PRI) {
327 ret |= SLIRP_POLL_PRI;
328 }
329 if (events & G_IO_ERR) {
330 ret |= SLIRP_POLL_ERR;
331 }
332 if (events & G_IO_HUP) {
333 ret |= SLIRP_POLL_HUP;
334 }
335
336 return ret;
337}
338
339static int net_slirp_get_revents(int idx, void *opaque)
340{
341 GArray *pollfds = opaque;
342
343 return slirp_gio_to_poll(g_array_index(pollfds, GPollFD, idx).revents);
344}
345
Marc-André Lureau1ab67b92019-01-17 15:43:55 +0400346static void net_slirp_poll_notify(Notifier *notifier, void *data)
347{
348 MainLoopPoll *poll = data;
349 SlirpState *s = container_of(notifier, SlirpState, poll_notifier);
350
351 switch (poll->state) {
352 case MAIN_LOOP_POLL_FILL:
Marc-André Lureaudeaeb3f2019-01-17 15:43:57 +0400353 slirp_pollfds_fill(s->slirp, &poll->timeout,
354 net_slirp_add_poll, poll->pollfds);
Marc-André Lureau1ab67b92019-01-17 15:43:55 +0400355 break;
356 case MAIN_LOOP_POLL_OK:
357 case MAIN_LOOP_POLL_ERR:
Marc-André Lureaudeaeb3f2019-01-17 15:43:57 +0400358 slirp_pollfds_poll(s->slirp, poll->state == MAIN_LOOP_POLL_ERR,
359 net_slirp_get_revents, poll->pollfds);
Marc-André Lureau1ab67b92019-01-17 15:43:55 +0400360 break;
361 default:
362 g_assert_not_reached();
363 }
364}
365
Marc-André Lureaud8903442019-02-12 17:25:19 +0100366static ssize_t
367net_slirp_stream_read(void *buf, size_t size, void *opaque)
368{
369 QEMUFile *f = opaque;
370
371 return qemu_get_buffer(f, buf, size);
372}
373
374static ssize_t
375net_slirp_stream_write(const void *buf, size_t size, void *opaque)
376{
377 QEMUFile *f = opaque;
378
379 qemu_put_buffer(f, buf, size);
380 if (qemu_file_get_error(f)) {
381 return -1;
382 }
383
384 return size;
385}
386
387static int net_slirp_state_load(QEMUFile *f, void *opaque, int version_id)
388{
389 Slirp *slirp = opaque;
390
391 return slirp_state_load(slirp, version_id, net_slirp_stream_read, f);
392}
393
394static void net_slirp_state_save(QEMUFile *f, void *opaque)
395{
396 Slirp *slirp = opaque;
397
398 slirp_state_save(slirp, net_slirp_stream_write, f);
399}
400
401static SaveVMHandlers savevm_slirp_state = {
402 .save_state = net_slirp_state_save,
403 .load_state = net_slirp_state_load,
404};
405
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100406static int net_slirp_init(NetClientState *peer, const char *model,
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000407 const char *name, int restricted,
Samuel Thibault0b11c032016-03-20 12:29:54 +0100408 bool ipv4, const char *vnetwork, const char *vhost,
409 bool ipv6, const char *vprefix6, int vprefix6_len,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100410 const char *vhost6,
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000411 const char *vhostname, const char *tftp_export,
412 const char *bootfile, const char *vdhcp_start,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100413 const char *vnameserver, const char *vnameserver6,
414 const char *smb_export, const char *vsmbserver,
Benjamin Drungf18d1372018-02-27 17:06:01 +0100415 const char **dnssearch, const char *vdomainname,
Fam Zheng0fca92b2018-09-14 15:26:16 +0800416 const char *tftp_server_name,
Benjamin Drungf18d1372018-02-27 17:06:01 +0100417 Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000418{
419 /* default settings according to historic slirp */
420 struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */
421 struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */
422 struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */
423 struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */
424 struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */
Yann Bordenave7aac5312016-03-15 10:31:22 +0100425 struct in6_addr ip6_prefix;
426 struct in6_addr ip6_host;
427 struct in6_addr ip6_dns;
Paolo Bonzini35acbb32021-10-13 13:43:36 +0200428#if defined(CONFIG_SMBD_COMMAND)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000429 struct in_addr smbsrv = { .s_addr = 0 };
430#endif
Paolo Bonzinibce63de2022-04-11 10:16:36 +0200431 SlirpConfig cfg = { 0 };
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100432 NetClientState *nc;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000433 SlirpState *s;
434 char buf[20];
435 uint32_t addr;
436 int shift;
437 char *end;
438 struct slirp_config_str *config;
439
Samuel Thibault0b11c032016-03-20 12:29:54 +0100440 if (!ipv4 && (vnetwork || vhost || vnameserver)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200441 error_setg(errp, "IPv4 disabled but netmask/host/dns provided");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100442 return -1;
443 }
444
445 if (!ipv6 && (vprefix6 || vhost6 || vnameserver6)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200446 error_setg(errp, "IPv6 disabled but prefix/host6/dns6 provided");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100447 return -1;
448 }
449
450 if (!ipv4 && !ipv6) {
451 /* It doesn't make sense to disable both */
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200452 error_setg(errp, "IPv4 and IPv6 disabled");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100453 return -1;
454 }
455
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000456 if (vnetwork) {
457 if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) {
458 if (!inet_aton(vnetwork, &net)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200459 error_setg(errp, "Failed to parse netmask");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000460 return -1;
461 }
462 addr = ntohl(net.s_addr);
463 if (!(addr & 0x80000000)) {
464 mask.s_addr = htonl(0xff000000); /* class A */
465 } else if ((addr & 0xfff00000) == 0xac100000) {
466 mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */
467 } else if ((addr & 0xc0000000) == 0x80000000) {
468 mask.s_addr = htonl(0xffff0000); /* class B */
469 } else if ((addr & 0xffff0000) == 0xc0a80000) {
470 mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */
471 } else if ((addr & 0xffff0000) == 0xc6120000) {
472 mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */
473 } else if ((addr & 0xe0000000) == 0xe0000000) {
474 mask.s_addr = htonl(0xffffff00); /* class C */
475 } else {
476 mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */
477 }
478 } else {
479 if (!inet_aton(buf, &net)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200480 error_setg(errp, "Failed to parse netmask");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000481 return -1;
482 }
483 shift = strtol(vnetwork, &end, 10);
484 if (*end != '\0') {
485 if (!inet_aton(vnetwork, &mask)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200486 error_setg(errp,
487 "Failed to parse netmask (trailing chars)");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000488 return -1;
489 }
490 } else if (shift < 4 || shift > 32) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200491 error_setg(errp,
492 "Invalid netmask provided (must be in range 4-32)");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000493 return -1;
494 } else {
495 mask.s_addr = htonl(0xffffffff << (32 - shift));
496 }
497 }
498 net.s_addr &= mask.s_addr;
499 host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr);
500 dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr);
501 dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr);
502 }
503
504 if (vhost && !inet_aton(vhost, &host)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200505 error_setg(errp, "Failed to parse host");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000506 return -1;
507 }
508 if ((host.s_addr & mask.s_addr) != net.s_addr) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200509 error_setg(errp, "Host doesn't belong to network");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000510 return -1;
511 }
512
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000513 if (vnameserver && !inet_aton(vnameserver, &dns)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200514 error_setg(errp, "Failed to parse DNS");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000515 return -1;
516 }
Samuel Thibault120b7212019-09-29 20:08:20 +0200517 if (restricted && (dns.s_addr & mask.s_addr) != net.s_addr) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200518 error_setg(errp, "DNS doesn't belong to network");
519 return -1;
520 }
521 if (dns.s_addr == host.s_addr) {
522 error_setg(errp, "DNS must be different from host");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000523 return -1;
524 }
525
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200526 if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200527 error_setg(errp, "Failed to parse DHCP start address");
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200528 return -1;
529 }
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200530 if ((dhcp.s_addr & mask.s_addr) != net.s_addr) {
531 error_setg(errp, "DHCP doesn't belong to network");
532 return -1;
533 }
534 if (dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) {
Doug Evans0c373c02021-01-21 16:42:51 -0800535 error_setg(errp, "DHCP must be different from host and DNS");
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200536 return -1;
537 }
538
Paolo Bonzini35acbb32021-10-13 13:43:36 +0200539#if defined(CONFIG_SMBD_COMMAND)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000540 if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200541 error_setg(errp, "Failed to parse SMB address");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000542 return -1;
543 }
544#endif
545
Yann Bordenave7aac5312016-03-15 10:31:22 +0100546 if (!vprefix6) {
547 vprefix6 = "fec0::";
548 }
549 if (!inet_pton(AF_INET6, vprefix6, &ip6_prefix)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200550 error_setg(errp, "Failed to parse IPv6 prefix");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100551 return -1;
552 }
Yann Bordenave7aac5312016-03-15 10:31:22 +0100553
554 if (!vprefix6_len) {
555 vprefix6_len = 64;
556 }
557 if (vprefix6_len < 0 || vprefix6_len > 126) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200558 error_setg(errp,
Stefano Garzarella178a0a52019-05-15 11:08:05 +0200559 "Invalid IPv6 prefix provided "
560 "(IPv6 prefix length must be between 0 and 126)");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100561 return -1;
562 }
563
564 if (vhost6) {
Yann Bordenave7aac5312016-03-15 10:31:22 +0100565 if (!inet_pton(AF_INET6, vhost6, &ip6_host)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200566 error_setg(errp, "Failed to parse IPv6 host");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100567 return -1;
568 }
569 if (!in6_equal_net(&ip6_prefix, &ip6_host, vprefix6_len)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200570 error_setg(errp, "IPv6 Host doesn't belong to network");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100571 return -1;
572 }
Yann Bordenave7aac5312016-03-15 10:31:22 +0100573 } else {
574 ip6_host = ip6_prefix;
575 ip6_host.s6_addr[15] |= 2;
576 }
577
578 if (vnameserver6) {
Yann Bordenave7aac5312016-03-15 10:31:22 +0100579 if (!inet_pton(AF_INET6, vnameserver6, &ip6_dns)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200580 error_setg(errp, "Failed to parse IPv6 DNS");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100581 return -1;
582 }
Samuel Thibault120b7212019-09-29 20:08:20 +0200583 if (restricted && !in6_equal_net(&ip6_prefix, &ip6_dns, vprefix6_len)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200584 error_setg(errp, "IPv6 DNS doesn't belong to network");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100585 return -1;
586 }
Yann Bordenave7aac5312016-03-15 10:31:22 +0100587 } else {
588 ip6_dns = ip6_prefix;
589 ip6_dns.s6_addr[15] |= 3;
590 }
591
Benjamin Drungf18d1372018-02-27 17:06:01 +0100592 if (vdomainname && !*vdomainname) {
593 error_setg(errp, "'domainname' parameter cannot be empty");
594 return -1;
595 }
596
Fam Zheng6e157a02018-09-14 15:26:15 +0800597 if (vdomainname && strlen(vdomainname) > 255) {
598 error_setg(errp, "'domainname' parameter cannot exceed 255 bytes");
599 return -1;
600 }
601
602 if (vhostname && strlen(vhostname) > 255) {
603 error_setg(errp, "'vhostname' parameter cannot exceed 255 bytes");
604 return -1;
605 }
Yann Bordenave7aac5312016-03-15 10:31:22 +0100606
Fam Zheng0fca92b2018-09-14 15:26:16 +0800607 if (tftp_server_name && strlen(tftp_server_name) > 255) {
608 error_setg(errp, "'tftp-server-name' parameter cannot exceed 255 bytes");
609 return -1;
610 }
611
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100612 nc = qemu_new_net_client(&net_slirp_info, peer, model, name);
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000613
Laurent Vivier53b85d92022-10-21 11:09:10 +0200614 qemu_set_info_str(nc, "net=%s,restrict=%s", inet_ntoa(net),
615 restricted ? "on" : "off");
Jason Wangd89b4f82021-04-02 11:03:12 +0800616
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000617 s = DO_UPCAST(SlirpState, nc, nc);
618
Paolo Bonzini6222e552022-04-11 09:39:16 +0200619 cfg.version = SLIRP_CHECK_VERSION(4,7,0) ? 4 : 1;
Paolo Bonzinibce63de2022-04-11 10:16:36 +0200620 cfg.restricted = restricted;
621 cfg.in_enabled = ipv4;
622 cfg.vnetwork = net;
623 cfg.vnetmask = mask;
624 cfg.vhost = host;
625 cfg.in6_enabled = ipv6;
626 cfg.vprefix_addr6 = ip6_prefix;
627 cfg.vprefix_len = vprefix6_len;
628 cfg.vhost6 = ip6_host;
629 cfg.vhostname = vhostname;
630 cfg.tftp_server_name = tftp_server_name;
631 cfg.tftp_path = tftp_export;
632 cfg.bootfile = bootfile;
633 cfg.vdhcp_start = dhcp;
634 cfg.vnameserver = dns;
635 cfg.vnameserver6 = ip6_dns;
636 cfg.vdnssearch = dnssearch;
637 cfg.vdomainname = vdomainname;
638 s->slirp = slirp_new(&cfg, &slirp_cb, s);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000639 QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry);
640
Marc-André Lureaud8903442019-02-12 17:25:19 +0100641 /*
642 * Make sure the current bitstream version of slirp is 4, to avoid
643 * QEMU migration incompatibilities, if upstream slirp bumped the
644 * version.
645 *
646 * FIXME: use bitfields of features? teach libslirp to save with
647 * specific version?
648 */
649 g_assert(slirp_state_version() == 4);
Dr. David Alan Gilbertce62df52019-08-22 12:54:33 +0100650 register_savevm_live("slirp", 0, slirp_state_version(),
Marc-André Lureaud8903442019-02-12 17:25:19 +0100651 &savevm_slirp_state, s->slirp);
652
Marc-André Lureau1ab67b92019-01-17 15:43:55 +0400653 s->poll_notifier.notify = net_slirp_poll_notify;
654 main_loop_poll_add_notifier(&s->poll_notifier);
655
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000656 for (config = slirp_configs; config; config = config->next) {
657 if (config->flags & SLIRP_CFG_HOSTFWD) {
Thomas Huthd18572d2018-08-22 15:43:30 +0200658 if (slirp_hostfwd(s, config->str, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000659 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200660 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000661 } else {
Thomas Huthd18572d2018-08-22 15:43:30 +0200662 if (slirp_guestfwd(s, config->str, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000663 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200664 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000665 }
666 }
Paolo Bonzini35acbb32021-10-13 13:43:36 +0200667#if defined(CONFIG_SMBD_COMMAND)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000668 if (smb_export) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200669 if (slirp_smb(s, smb_export, smbsrv, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000670 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200671 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000672 }
673#endif
674
Paolo Bonzinif6c2e662016-07-12 09:57:12 +0200675 s->exit_notifier.notify = slirp_smb_exit;
676 qemu_add_exit_notifier(&s->exit_notifier);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000677 return 0;
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000678
679error:
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100680 qemu_del_net_client(nc);
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000681 return -1;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000682}
683
Thomas Huthb4983c52019-12-05 11:41:09 +0100684static SlirpState *slirp_lookup(Monitor *mon, const char *id)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000685{
Thomas Huthb4983c52019-12-05 11:41:09 +0100686 if (id) {
687 NetClientState *nc = qemu_find_netdev(id);
688 if (!nc) {
689 monitor_printf(mon, "unrecognized netdev id '%s'\n", id);
690 return NULL;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000691 }
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000692 if (strcmp(nc->model, "user")) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000693 monitor_printf(mon, "invalid device specified\n");
694 return NULL;
695 }
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000696 return DO_UPCAST(SlirpState, nc, nc);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000697 } else {
698 if (QTAILQ_EMPTY(&slirp_stacks)) {
699 monitor_printf(mon, "user mode network stack not in use\n");
700 return NULL;
701 }
702 return QTAILQ_FIRST(&slirp_stacks);
703 }
704}
705
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100706void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000707{
708 struct in_addr host_addr = { .s_addr = INADDR_ANY };
709 int host_port;
Markus Armbrustere30e5eb2011-11-16 15:45:59 +0100710 char buf[256];
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000711 const char *src_str, *p;
712 SlirpState *s;
713 int is_udp = 0;
714 int err;
715 const char *arg1 = qdict_get_str(qdict, "arg1");
716 const char *arg2 = qdict_get_try_str(qdict, "arg2");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000717
Thomas Huthb4983c52019-12-05 11:41:09 +0100718 if (arg2) {
719 s = slirp_lookup(mon, arg1);
Thomas Huth93653062018-01-11 21:02:40 +0100720 src_str = arg2;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000721 } else {
Thomas Huthb4983c52019-12-05 11:41:09 +0100722 s = slirp_lookup(mon, NULL);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000723 src_str = arg1;
724 }
725 if (!s) {
726 return;
727 }
728
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000729 p = src_str;
Markus Armbrustere30e5eb2011-11-16 15:45:59 +0100730 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
731 goto fail_syntax;
732 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000733
734 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
735 is_udp = 0;
736 } else if (!strcmp(buf, "udp")) {
737 is_udp = 1;
738 } else {
739 goto fail_syntax;
740 }
741
742 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
743 goto fail_syntax;
744 }
745 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
746 goto fail_syntax;
747 }
748
Nia Alarie1fb3f7f2018-03-16 14:39:21 +0000749 if (qemu_strtoi(p, NULL, 10, &host_port)) {
750 goto fail_syntax;
751 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000752
Peter Maydell70381662014-06-16 16:47:49 +0100753 err = slirp_remove_hostfwd(s->slirp, is_udp, host_addr, host_port);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000754
755 monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
Geoffrey Thomasb15ba6c2011-12-17 04:23:59 -0500756 err ? "not found" : "removed");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000757 return;
758
759 fail_syntax:
760 monitor_printf(mon, "invalid format\n");
761}
762
Thomas Huthd18572d2018-08-22 15:43:30 +0200763static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000764{
765 struct in_addr host_addr = { .s_addr = INADDR_ANY };
766 struct in_addr guest_addr = { .s_addr = 0 };
767 int host_port, guest_port;
768 const char *p;
769 char buf[256];
770 int is_udp;
771 char *end;
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100772 const char *fail_reason = "Unknown reason";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000773
774 p = redir_str;
775 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100776 fail_reason = "No : separators";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000777 goto fail_syntax;
778 }
779 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
780 is_udp = 0;
781 } else if (!strcmp(buf, "udp")) {
782 is_udp = 1;
783 } else {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100784 fail_reason = "Bad protocol name";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000785 goto fail_syntax;
786 }
787
Thomas Huthd18572d2018-08-22 15:43:30 +0200788 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
789 fail_reason = "Missing : separator";
790 goto fail_syntax;
791 }
792 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
793 fail_reason = "Bad host address";
794 goto fail_syntax;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000795 }
796
Thomas Huthd18572d2018-08-22 15:43:30 +0200797 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100798 fail_reason = "Bad host port separator";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000799 goto fail_syntax;
800 }
801 host_port = strtol(buf, &end, 0);
Vincent Bernat0bed71e2017-02-25 22:31:58 +0100802 if (*end != '\0' || host_port < 0 || host_port > 65535) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100803 fail_reason = "Bad host port";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000804 goto fail_syntax;
805 }
806
807 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100808 fail_reason = "Missing guest address";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000809 goto fail_syntax;
810 }
811 if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100812 fail_reason = "Bad guest address";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000813 goto fail_syntax;
814 }
815
816 guest_port = strtol(p, &end, 0);
817 if (*end != '\0' || guest_port < 1 || guest_port > 65535) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100818 fail_reason = "Bad guest port";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000819 goto fail_syntax;
820 }
821
822 if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr,
823 guest_port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200824 error_setg(errp, "Could not set up host forwarding rule '%s'",
825 redir_str);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000826 return -1;
827 }
828 return 0;
829
830 fail_syntax:
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100831 error_setg(errp, "Invalid host forwarding rule '%s' (%s)", redir_str,
832 fail_reason);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000833 return -1;
834}
835
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100836void hmp_hostfwd_add(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000837{
838 const char *redir_str;
839 SlirpState *s;
840 const char *arg1 = qdict_get_str(qdict, "arg1");
841 const char *arg2 = qdict_get_try_str(qdict, "arg2");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000842
Thomas Huthb4983c52019-12-05 11:41:09 +0100843 if (arg2) {
844 s = slirp_lookup(mon, arg1);
Thomas Huth93653062018-01-11 21:02:40 +0100845 redir_str = arg2;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000846 } else {
Thomas Huthb4983c52019-12-05 11:41:09 +0100847 s = slirp_lookup(mon, NULL);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000848 redir_str = arg1;
849 }
850 if (s) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200851 Error *err = NULL;
Thomas Huthd18572d2018-08-22 15:43:30 +0200852 if (slirp_hostfwd(s, redir_str, &err) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200853 error_report_err(err);
854 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000855 }
856
857}
858
Paolo Bonzini35acbb32021-10-13 13:43:36 +0200859#if defined(CONFIG_SMBD_COMMAND)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000860
861/* automatic user mode samba server configuration */
862static void slirp_smb_cleanup(SlirpState *s)
863{
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100864 int ret;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000865
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100866 if (s->smb_dir) {
867 gchar *cmd = g_strdup_printf("rm -rf %s", s->smb_dir);
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100868 ret = system(cmd);
Juan Quintela24ac07d2010-03-04 10:00:31 +0100869 if (ret == -1 || !WIFEXITED(ret)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100870 error_report("'%s' failed.", cmd);
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100871 } else if (WEXITSTATUS(ret)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100872 error_report("'%s' failed. Error code: %d",
873 cmd, WEXITSTATUS(ret));
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100874 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100875 g_free(cmd);
876 g_free(s->smb_dir);
877 s->smb_dir = NULL;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000878 }
879}
880
881static int slirp_smb(SlirpState* s, const char *exported_dir,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200882 struct in_addr vserver_addr, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000883{
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100884 char *smb_conf;
885 char *smb_cmdline;
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200886 struct passwd *passwd;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000887 FILE *f;
888
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200889 passwd = getpwuid(geteuid());
890 if (!passwd) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200891 error_setg(errp, "Failed to retrieve user name");
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200892 return -1;
893 }
894
Dunrong Huang927d8112012-07-06 14:04:43 +0800895 if (access(CONFIG_SMBD_COMMAND, F_OK)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200896 error_setg(errp, "Could not find '%s', please install it",
897 CONFIG_SMBD_COMMAND);
Dunrong Huang927d8112012-07-06 14:04:43 +0800898 return -1;
899 }
900
901 if (access(exported_dir, R_OK | X_OK)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200902 error_setg(errp, "Error accessing shared directory '%s': %s",
903 exported_dir, strerror(errno));
Dunrong Huang927d8112012-07-06 14:04:43 +0800904 return -1;
905 }
906
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100907 s->smb_dir = g_dir_make_tmp("qemu-smb.XXXXXX", NULL);
908 if (!s->smb_dir) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200909 error_setg(errp, "Could not create samba server dir");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000910 return -1;
911 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100912 smb_conf = g_strdup_printf("%s/%s", s->smb_dir, "smb.conf");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000913
914 f = fopen(smb_conf, "w");
915 if (!f) {
916 slirp_smb_cleanup(s);
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200917 error_setg(errp,
918 "Could not create samba server configuration file '%s'",
919 smb_conf);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100920 g_free(smb_conf);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000921 return -1;
922 }
923 fprintf(f,
924 "[global]\n"
925 "private dir=%s\n"
Peter Wu7912d042014-11-03 11:52:10 +0100926 "interfaces=127.0.0.1\n"
927 "bind interfaces only=yes\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000928 "pid directory=%s\n"
929 "lock directory=%s\n"
Nikolaus Rath276eda52012-04-25 09:57:19 -0400930 "state directory=%s\n"
Peter Wu7912d042014-11-03 11:52:10 +0100931 "cache directory=%s\n"
Michael Bueschb87b8a82014-04-27 14:54:12 +0400932 "ncalrpc dir=%s/ncalrpc\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000933 "log file=%s/log.smbd\n"
934 "smb passwd file=%s/smbpasswd\n"
Michael Bueschc2804ee2013-11-01 12:23:49 +0100935 "security = user\n"
936 "map to guest = Bad User\n"
Peter Wu7912d042014-11-03 11:52:10 +0100937 "load printers = no\n"
938 "printing = bsd\n"
939 "disable spoolss = yes\n"
940 "usershare max shares = 0\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000941 "[qemu]\n"
942 "path=%s\n"
943 "read only=no\n"
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200944 "guest ok=yes\n"
945 "force user=%s\n",
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000946 s->smb_dir,
947 s->smb_dir,
948 s->smb_dir,
949 s->smb_dir,
950 s->smb_dir,
Nikolaus Rath276eda52012-04-25 09:57:19 -0400951 s->smb_dir,
Michael Bueschb87b8a82014-04-27 14:54:12 +0400952 s->smb_dir,
Peter Wu7912d042014-11-03 11:52:10 +0100953 s->smb_dir,
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200954 exported_dir,
955 passwd->pw_name
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000956 );
957 fclose(f);
958
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100959 smb_cmdline = g_strdup_printf("%s -l %s -s %s",
Michael Tokarev44d8d2b2014-10-25 00:29:50 +0400960 CONFIG_SMBD_COMMAND, s->smb_dir, smb_conf);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100961 g_free(smb_conf);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000962
Marc-André Lureau44b4ff22019-01-17 15:43:33 +0400963 if (slirp_add_exec(s->slirp, smb_cmdline, &vserver_addr, 139) < 0 ||
964 slirp_add_exec(s->slirp, smb_cmdline, &vserver_addr, 445) < 0) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000965 slirp_smb_cleanup(s);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100966 g_free(smb_cmdline);
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200967 error_setg(errp, "Conflicting/invalid smbserver address");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000968 return -1;
969 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100970 g_free(smb_cmdline);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000971 return 0;
972}
973
Paolo Bonzini35acbb32021-10-13 13:43:36 +0200974#endif /* defined(CONFIG_SMBD_COMMAND) */
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000975
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000976static int guestfwd_can_read(void *opaque)
977{
978 struct GuestFwd *fwd = opaque;
979 return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port);
980}
981
982static void guestfwd_read(void *opaque, const uint8_t *buf, int size)
983{
984 struct GuestFwd *fwd = opaque;
985 slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size);
986}
987
Marc-André Lureau625a5262019-01-17 15:43:54 +0400988static ssize_t guestfwd_write(const void *buf, size_t len, void *chr)
Marc-André Lureau44b4ff22019-01-17 15:43:33 +0400989{
990 return qemu_chr_fe_write_all(chr, buf, len);
991}
992
Thomas Huthd18572d2018-08-22 15:43:30 +0200993static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000994{
Samuel Thibaultffe02f52016-04-01 00:46:35 +0200995 /* TODO: IPv6 */
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000996 struct in_addr server = { .s_addr = 0 };
997 struct GuestFwd *fwd;
998 const char *p;
999 char buf[128];
1000 char *end;
1001 int port;
1002
1003 p = config_str;
Thomas Huthd18572d2018-08-22 15:43:30 +02001004 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
1005 goto fail_syntax;
1006 }
1007 if (strcmp(buf, "tcp") && buf[0] != '\0') {
1008 goto fail_syntax;
1009 }
1010 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
1011 goto fail_syntax;
1012 }
1013 if (buf[0] != '\0' && !inet_aton(buf, &server)) {
1014 goto fail_syntax;
1015 }
1016 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
1017 goto fail_syntax;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001018 }
1019 port = strtol(buf, &end, 10);
1020 if (*end != '\0' || port < 1 || port > 65535) {
1021 goto fail_syntax;
1022 }
1023
Alexander Grafa9899992011-06-04 07:25:59 +02001024 snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001025
Marc-André Lureau36247302019-01-17 15:43:34 +04001026 if (g_str_has_prefix(p, "cmd:")) {
Marc-André Lureau44b4ff22019-01-17 15:43:33 +04001027 if (slirp_add_exec(s->slirp, &p[4], &server, port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +02001028 error_setg(errp, "Conflicting/invalid host:port in guest "
1029 "forwarding rule '%s'", config_str);
Alexander Grafb412eb62012-06-03 09:45:01 +02001030 return -1;
1031 }
1032 } else {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03001033 Error *err = NULL;
Marc-André Lureau95e30b22018-08-22 19:19:42 +02001034 /*
1035 * FIXME: sure we want to support implicit
1036 * muxed monitors here?
1037 */
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01001038 Chardev *chr = qemu_chr_new_mux_mon(buf, p, NULL);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03001039
1040 if (!chr) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +02001041 error_setg(errp, "Could not open guest forwarding device '%s'",
1042 buf);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03001043 return -1;
1044 }
1045
1046 fwd = g_new(struct GuestFwd, 1);
1047 qemu_chr_fe_init(&fwd->hd, chr, &err);
1048 if (err) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +02001049 error_propagate(errp, err);
Marc-André Lureau8e207c32019-01-17 15:43:36 +04001050 object_unparent(OBJECT(chr));
Alexander Grafb412eb62012-06-03 09:45:01 +02001051 g_free(fwd);
1052 return -1;
1053 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001054
Marc-André Lureau44b4ff22019-01-17 15:43:33 +04001055 if (slirp_add_guestfwd(s->slirp, guestfwd_write, &fwd->hd,
1056 &server, port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +02001057 error_setg(errp, "Conflicting/invalid host:port in guest "
1058 "forwarding rule '%s'", config_str);
Marc-André Lureau8e207c32019-01-17 15:43:36 +04001059 qemu_chr_fe_deinit(&fwd->hd, true);
Alexander Grafb412eb62012-06-03 09:45:01 +02001060 g_free(fwd);
1061 return -1;
1062 }
1063 fwd->server = server;
1064 fwd->port = port;
1065 fwd->slirp = s->slirp;
1066
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001067 qemu_chr_fe_set_handlers(&fwd->hd, guestfwd_can_read, guestfwd_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +03001068 NULL, NULL, fwd, NULL, true);
Marc-André Lureau8d45a3b2019-01-17 15:43:35 +04001069 s->fwd = g_slist_append(s->fwd, fwd);
Alexander Grafb412eb62012-06-03 09:45:01 +02001070 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001071 return 0;
1072
1073 fail_syntax:
Hervé Poussineau5c843af2017-07-15 18:43:50 +02001074 error_setg(errp, "Invalid guest forwarding rule '%s'", config_str);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001075 return -1;
1076}
1077
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001078void hmp_info_usernet(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001079{
1080 SlirpState *s;
1081
1082 QTAILQ_FOREACH(s, &slirp_stacks, entry) {
Stefan Hajnoczi90d87a32012-07-24 16:35:06 +01001083 int id;
Thomas Huth442da402018-04-30 20:02:24 +02001084 bool got_hub_id = net_hub_id_for_client(&s->nc, &id) == 0;
Marc-André Lureaub7f43bf2018-11-10 17:45:43 +04001085 char *info = slirp_connection_info(s->slirp);
1086 monitor_printf(mon, "Hub %d (%s):\n%s",
Thomas Huth442da402018-04-30 20:02:24 +02001087 got_hub_id ? id : -1,
Marc-André Lureaub7f43bf2018-11-10 17:45:43 +04001088 s->nc.name, info);
1089 g_free(info);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001090 }
1091}
1092
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001093static void
1094net_init_slirp_configs(const StringList *fwd, int flags)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001095{
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001096 while (fwd) {
1097 struct slirp_config_str *config;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001098
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001099 config = g_malloc0(sizeof(*config));
1100 pstrcpy(config->str, sizeof(config->str), fwd->value->str);
1101 config->flags = flags;
1102 config->next = slirp_configs;
1103 slirp_configs = config;
1104
1105 fwd = fwd->next;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001106 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001107}
1108
Klaus Stengel63d29602012-10-27 19:53:39 +02001109static const char **slirp_dnssearch(const StringList *dnsname)
1110{
1111 const StringList *c = dnsname;
1112 size_t i = 0, num_opts = 0;
1113 const char **ret;
1114
1115 while (c) {
1116 num_opts++;
1117 c = c->next;
1118 }
1119
1120 if (num_opts == 0) {
1121 return NULL;
1122 }
1123
1124 ret = g_malloc((num_opts + 1) * sizeof(*ret));
1125 c = dnsname;
1126 while (c) {
1127 ret[i++] = c->value->str;
1128 c = c->next;
1129 }
1130 ret[i] = NULL;
1131 return ret;
1132}
1133
Kővágó, Zoltáncebea512016-07-13 21:50:12 -06001134int net_init_slirp(const Netdev *netdev, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +02001135 NetClientState *peer, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001136{
1137 struct slirp_config_str *config;
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001138 char *vnet;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001139 int ret;
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001140 const NetdevUserOptions *user;
Klaus Stengel63d29602012-10-27 19:53:39 +02001141 const char **dnssearch;
Samuel Thibault0b11c032016-03-20 12:29:54 +01001142 bool ipv4 = true, ipv6 = true;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001143
Eric Blakef394b2e2016-07-13 21:50:23 -06001144 assert(netdev->type == NET_CLIENT_DRIVER_USER);
1145 user = &netdev->u.user;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001146
Samuel Thibault0b11c032016-03-20 12:29:54 +01001147 if ((user->has_ipv6 && user->ipv6 && !user->has_ipv4) ||
1148 (user->has_ipv4 && !user->ipv4)) {
1149 ipv4 = 0;
1150 }
1151 if ((user->has_ipv4 && user->ipv4 && !user->has_ipv6) ||
1152 (user->has_ipv6 && !user->ipv6)) {
1153 ipv6 = 0;
1154 }
1155
Markus Armbruster74808742022-11-04 17:07:00 +01001156 vnet = user->net ? g_strdup(user->net) :
1157 user->ip ? g_strdup_printf("%s/24", user->ip) :
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001158 NULL;
Jan Kiszkac54ed5b2011-07-20 12:20:14 +02001159
Klaus Stengel63d29602012-10-27 19:53:39 +02001160 dnssearch = slirp_dnssearch(user->dnssearch);
1161
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001162 /* all optional fields are initialized to "all bits zero" */
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001163
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001164 net_init_slirp_configs(user->hostfwd, SLIRP_CFG_HOSTFWD);
1165 net_init_slirp_configs(user->guestfwd, 0);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001166
Samuel Thibault0b11c032016-03-20 12:29:54 +01001167 ret = net_slirp_init(peer, "user", name, user->q_restrict,
1168 ipv4, vnet, user->host,
1169 ipv6, user->ipv6_prefix, user->ipv6_prefixlen,
Samuel Thibaultd8eb3862016-03-25 00:02:58 +01001170 user->ipv6_host, user->hostname, user->tftp,
Yann Bordenave7aac5312016-03-15 10:31:22 +01001171 user->bootfile, user->dhcpstart,
Samuel Thibaultd8eb3862016-03-25 00:02:58 +01001172 user->dns, user->ipv6_dns, user->smb,
Fam Zheng0fca92b2018-09-14 15:26:16 +08001173 user->smbserver, dnssearch, user->domainname,
1174 user->tftp_server_name, errp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001175
1176 while (slirp_configs) {
1177 config = slirp_configs;
1178 slirp_configs = config->next;
Anthony Liguori7267c092011-08-20 22:09:37 -05001179 g_free(config);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001180 }
1181
Anthony Liguori7267c092011-08-20 22:09:37 -05001182 g_free(vnet);
Klaus Stengel63d29602012-10-27 19:53:39 +02001183 g_free(dnssearch);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001184
1185 return ret;
1186}