blob: a7c35778a6f93117d38e92889b47ea68900d7bcf [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{
Marc-André Lureaufaa4ec12023-02-21 16:47:56 +0400251#ifdef WIN32
252 AioContext *ctxt = qemu_get_aio_context();
253
254 qemu_socket_select(fd, event_notifier_get_handle(&ctxt->notifier),
255 FD_READ | FD_ACCEPT | FD_CLOSE |
256 FD_CONNECT | FD_WRITE | FD_OOB, NULL);
257#endif
Marc-André Lureau848c7092019-01-17 15:43:41 +0400258}
259
Marc-André Lureau3e0fad32019-01-17 15:43:58 +0400260static void net_slirp_unregister_poll_fd(int fd, void *opaque)
Marc-André Lureauf6e5aa32019-01-17 15:43:42 +0400261{
Marc-André Lureau21ac7282023-02-21 16:47:57 +0400262#ifdef WIN32
263 qemu_socket_unselect(fd, NULL);
264#endif
Marc-André Lureauf6e5aa32019-01-17 15:43:42 +0400265}
266
Marc-André Lureau3e0fad32019-01-17 15:43:58 +0400267static void net_slirp_notify(void *opaque)
268{
269 qemu_notify_event();
270}
271
Marc-André Lureaud846b922018-11-14 16:36:07 +0400272static const SlirpCb slirp_cb = {
Marc-André Lureau625a5262019-01-17 15:43:54 +0400273 .send_packet = net_slirp_send_packet,
Marc-André Lureau2addc8f2018-11-14 16:36:33 +0400274 .guest_error = net_slirp_guest_error,
Marc-André Lureaue6dbff32018-11-22 02:06:28 +0400275 .clock_get_ns = net_slirp_clock_get_ns,
Paolo Bonzini6222e552022-04-11 09:39:16 +0200276#if SLIRP_CHECK_VERSION(4,7,0)
277 .init_completed = net_slirp_init_completed,
278 .timer_new_opaque = net_slirp_timer_new_opaque,
279#else
Marc-André Lureau07abf6d2019-01-17 15:43:37 +0400280 .timer_new = net_slirp_timer_new,
Paolo Bonzini6222e552022-04-11 09:39:16 +0200281#endif
Marc-André Lureau07abf6d2019-01-17 15:43:37 +0400282 .timer_free = net_slirp_timer_free,
283 .timer_mod = net_slirp_timer_mod,
Marc-André Lureau848c7092019-01-17 15:43:41 +0400284 .register_poll_fd = net_slirp_register_poll_fd,
Marc-André Lureauf6e5aa32019-01-17 15:43:42 +0400285 .unregister_poll_fd = net_slirp_unregister_poll_fd,
Marc-André Lureau3e0fad32019-01-17 15:43:58 +0400286 .notify = net_slirp_notify,
Marc-André Lureaud846b922018-11-14 16:36:07 +0400287};
288
Marc-André Lureaudeaeb3f2019-01-17 15:43:57 +0400289static int slirp_poll_to_gio(int events)
290{
291 int ret = 0;
292
293 if (events & SLIRP_POLL_IN) {
294 ret |= G_IO_IN;
295 }
296 if (events & SLIRP_POLL_OUT) {
297 ret |= G_IO_OUT;
298 }
299 if (events & SLIRP_POLL_PRI) {
300 ret |= G_IO_PRI;
301 }
302 if (events & SLIRP_POLL_ERR) {
303 ret |= G_IO_ERR;
304 }
305 if (events & SLIRP_POLL_HUP) {
306 ret |= G_IO_HUP;
307 }
308
309 return ret;
310}
311
312static int net_slirp_add_poll(int fd, int events, void *opaque)
313{
314 GArray *pollfds = opaque;
315 GPollFD pfd = {
316 .fd = fd,
317 .events = slirp_poll_to_gio(events),
318 };
319 int idx = pollfds->len;
320 g_array_append_val(pollfds, pfd);
321 return idx;
322}
323
324static int slirp_gio_to_poll(int events)
325{
326 int ret = 0;
327
328 if (events & G_IO_IN) {
329 ret |= SLIRP_POLL_IN;
330 }
331 if (events & G_IO_OUT) {
332 ret |= SLIRP_POLL_OUT;
333 }
334 if (events & G_IO_PRI) {
335 ret |= SLIRP_POLL_PRI;
336 }
337 if (events & G_IO_ERR) {
338 ret |= SLIRP_POLL_ERR;
339 }
340 if (events & G_IO_HUP) {
341 ret |= SLIRP_POLL_HUP;
342 }
343
344 return ret;
345}
346
347static int net_slirp_get_revents(int idx, void *opaque)
348{
349 GArray *pollfds = opaque;
350
351 return slirp_gio_to_poll(g_array_index(pollfds, GPollFD, idx).revents);
352}
353
Marc-André Lureau1ab67b92019-01-17 15:43:55 +0400354static void net_slirp_poll_notify(Notifier *notifier, void *data)
355{
356 MainLoopPoll *poll = data;
357 SlirpState *s = container_of(notifier, SlirpState, poll_notifier);
358
359 switch (poll->state) {
360 case MAIN_LOOP_POLL_FILL:
Marc-André Lureaudeaeb3f2019-01-17 15:43:57 +0400361 slirp_pollfds_fill(s->slirp, &poll->timeout,
362 net_slirp_add_poll, poll->pollfds);
Marc-André Lureau1ab67b92019-01-17 15:43:55 +0400363 break;
364 case MAIN_LOOP_POLL_OK:
365 case MAIN_LOOP_POLL_ERR:
Marc-André Lureaudeaeb3f2019-01-17 15:43:57 +0400366 slirp_pollfds_poll(s->slirp, poll->state == MAIN_LOOP_POLL_ERR,
367 net_slirp_get_revents, poll->pollfds);
Marc-André Lureau1ab67b92019-01-17 15:43:55 +0400368 break;
369 default:
370 g_assert_not_reached();
371 }
372}
373
Marc-André Lureaud8903442019-02-12 17:25:19 +0100374static ssize_t
375net_slirp_stream_read(void *buf, size_t size, void *opaque)
376{
377 QEMUFile *f = opaque;
378
379 return qemu_get_buffer(f, buf, size);
380}
381
382static ssize_t
383net_slirp_stream_write(const void *buf, size_t size, void *opaque)
384{
385 QEMUFile *f = opaque;
386
387 qemu_put_buffer(f, buf, size);
388 if (qemu_file_get_error(f)) {
389 return -1;
390 }
391
392 return size;
393}
394
395static int net_slirp_state_load(QEMUFile *f, void *opaque, int version_id)
396{
397 Slirp *slirp = opaque;
398
399 return slirp_state_load(slirp, version_id, net_slirp_stream_read, f);
400}
401
402static void net_slirp_state_save(QEMUFile *f, void *opaque)
403{
404 Slirp *slirp = opaque;
405
406 slirp_state_save(slirp, net_slirp_stream_write, f);
407}
408
409static SaveVMHandlers savevm_slirp_state = {
410 .save_state = net_slirp_state_save,
411 .load_state = net_slirp_state_load,
412};
413
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100414static int net_slirp_init(NetClientState *peer, const char *model,
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000415 const char *name, int restricted,
Samuel Thibault0b11c032016-03-20 12:29:54 +0100416 bool ipv4, const char *vnetwork, const char *vhost,
417 bool ipv6, const char *vprefix6, int vprefix6_len,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100418 const char *vhost6,
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000419 const char *vhostname, const char *tftp_export,
420 const char *bootfile, const char *vdhcp_start,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100421 const char *vnameserver, const char *vnameserver6,
422 const char *smb_export, const char *vsmbserver,
Benjamin Drungf18d1372018-02-27 17:06:01 +0100423 const char **dnssearch, const char *vdomainname,
Fam Zheng0fca92b2018-09-14 15:26:16 +0800424 const char *tftp_server_name,
Benjamin Drungf18d1372018-02-27 17:06:01 +0100425 Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000426{
427 /* default settings according to historic slirp */
428 struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */
429 struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */
430 struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */
431 struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */
432 struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */
Yann Bordenave7aac5312016-03-15 10:31:22 +0100433 struct in6_addr ip6_prefix;
434 struct in6_addr ip6_host;
435 struct in6_addr ip6_dns;
Paolo Bonzini35acbb32021-10-13 13:43:36 +0200436#if defined(CONFIG_SMBD_COMMAND)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000437 struct in_addr smbsrv = { .s_addr = 0 };
438#endif
Paolo Bonzinibce63de2022-04-11 10:16:36 +0200439 SlirpConfig cfg = { 0 };
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100440 NetClientState *nc;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000441 SlirpState *s;
442 char buf[20];
443 uint32_t addr;
444 int shift;
445 char *end;
446 struct slirp_config_str *config;
447
Samuel Thibault0b11c032016-03-20 12:29:54 +0100448 if (!ipv4 && (vnetwork || vhost || vnameserver)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200449 error_setg(errp, "IPv4 disabled but netmask/host/dns provided");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100450 return -1;
451 }
452
453 if (!ipv6 && (vprefix6 || vhost6 || vnameserver6)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200454 error_setg(errp, "IPv6 disabled but prefix/host6/dns6 provided");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100455 return -1;
456 }
457
458 if (!ipv4 && !ipv6) {
459 /* It doesn't make sense to disable both */
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200460 error_setg(errp, "IPv4 and IPv6 disabled");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100461 return -1;
462 }
463
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000464 if (vnetwork) {
465 if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) {
466 if (!inet_aton(vnetwork, &net)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200467 error_setg(errp, "Failed to parse netmask");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000468 return -1;
469 }
470 addr = ntohl(net.s_addr);
471 if (!(addr & 0x80000000)) {
472 mask.s_addr = htonl(0xff000000); /* class A */
473 } else if ((addr & 0xfff00000) == 0xac100000) {
474 mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */
475 } else if ((addr & 0xc0000000) == 0x80000000) {
476 mask.s_addr = htonl(0xffff0000); /* class B */
477 } else if ((addr & 0xffff0000) == 0xc0a80000) {
478 mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */
479 } else if ((addr & 0xffff0000) == 0xc6120000) {
480 mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */
481 } else if ((addr & 0xe0000000) == 0xe0000000) {
482 mask.s_addr = htonl(0xffffff00); /* class C */
483 } else {
484 mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */
485 }
486 } else {
487 if (!inet_aton(buf, &net)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200488 error_setg(errp, "Failed to parse netmask");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000489 return -1;
490 }
491 shift = strtol(vnetwork, &end, 10);
492 if (*end != '\0') {
493 if (!inet_aton(vnetwork, &mask)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200494 error_setg(errp,
495 "Failed to parse netmask (trailing chars)");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000496 return -1;
497 }
498 } else if (shift < 4 || shift > 32) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200499 error_setg(errp,
500 "Invalid netmask provided (must be in range 4-32)");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000501 return -1;
502 } else {
503 mask.s_addr = htonl(0xffffffff << (32 - shift));
504 }
505 }
506 net.s_addr &= mask.s_addr;
507 host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr);
508 dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr);
509 dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr);
510 }
511
512 if (vhost && !inet_aton(vhost, &host)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200513 error_setg(errp, "Failed to parse host");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000514 return -1;
515 }
516 if ((host.s_addr & mask.s_addr) != net.s_addr) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200517 error_setg(errp, "Host doesn't belong to network");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000518 return -1;
519 }
520
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000521 if (vnameserver && !inet_aton(vnameserver, &dns)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200522 error_setg(errp, "Failed to parse DNS");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000523 return -1;
524 }
Samuel Thibault120b7212019-09-29 20:08:20 +0200525 if (restricted && (dns.s_addr & mask.s_addr) != net.s_addr) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200526 error_setg(errp, "DNS doesn't belong to network");
527 return -1;
528 }
529 if (dns.s_addr == host.s_addr) {
530 error_setg(errp, "DNS must be different from host");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000531 return -1;
532 }
533
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200534 if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200535 error_setg(errp, "Failed to parse DHCP start address");
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200536 return -1;
537 }
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200538 if ((dhcp.s_addr & mask.s_addr) != net.s_addr) {
539 error_setg(errp, "DHCP doesn't belong to network");
540 return -1;
541 }
542 if (dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) {
Doug Evans0c373c02021-01-21 16:42:51 -0800543 error_setg(errp, "DHCP must be different from host and DNS");
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200544 return -1;
545 }
546
Paolo Bonzini35acbb32021-10-13 13:43:36 +0200547#if defined(CONFIG_SMBD_COMMAND)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000548 if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200549 error_setg(errp, "Failed to parse SMB address");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000550 return -1;
551 }
552#endif
553
Yann Bordenave7aac5312016-03-15 10:31:22 +0100554 if (!vprefix6) {
555 vprefix6 = "fec0::";
556 }
557 if (!inet_pton(AF_INET6, vprefix6, &ip6_prefix)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200558 error_setg(errp, "Failed to parse IPv6 prefix");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100559 return -1;
560 }
Yann Bordenave7aac5312016-03-15 10:31:22 +0100561
562 if (!vprefix6_len) {
563 vprefix6_len = 64;
564 }
565 if (vprefix6_len < 0 || vprefix6_len > 126) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200566 error_setg(errp,
Stefano Garzarella178a0a52019-05-15 11:08:05 +0200567 "Invalid IPv6 prefix provided "
568 "(IPv6 prefix length must be between 0 and 126)");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100569 return -1;
570 }
571
572 if (vhost6) {
Yann Bordenave7aac5312016-03-15 10:31:22 +0100573 if (!inet_pton(AF_INET6, vhost6, &ip6_host)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200574 error_setg(errp, "Failed to parse IPv6 host");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100575 return -1;
576 }
577 if (!in6_equal_net(&ip6_prefix, &ip6_host, vprefix6_len)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200578 error_setg(errp, "IPv6 Host doesn't belong to network");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100579 return -1;
580 }
Yann Bordenave7aac5312016-03-15 10:31:22 +0100581 } else {
582 ip6_host = ip6_prefix;
583 ip6_host.s6_addr[15] |= 2;
584 }
585
586 if (vnameserver6) {
Yann Bordenave7aac5312016-03-15 10:31:22 +0100587 if (!inet_pton(AF_INET6, vnameserver6, &ip6_dns)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200588 error_setg(errp, "Failed to parse IPv6 DNS");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100589 return -1;
590 }
Samuel Thibault120b7212019-09-29 20:08:20 +0200591 if (restricted && !in6_equal_net(&ip6_prefix, &ip6_dns, vprefix6_len)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200592 error_setg(errp, "IPv6 DNS doesn't belong to network");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100593 return -1;
594 }
Yann Bordenave7aac5312016-03-15 10:31:22 +0100595 } else {
596 ip6_dns = ip6_prefix;
597 ip6_dns.s6_addr[15] |= 3;
598 }
599
Benjamin Drungf18d1372018-02-27 17:06:01 +0100600 if (vdomainname && !*vdomainname) {
601 error_setg(errp, "'domainname' parameter cannot be empty");
602 return -1;
603 }
604
Fam Zheng6e157a02018-09-14 15:26:15 +0800605 if (vdomainname && strlen(vdomainname) > 255) {
606 error_setg(errp, "'domainname' parameter cannot exceed 255 bytes");
607 return -1;
608 }
609
610 if (vhostname && strlen(vhostname) > 255) {
611 error_setg(errp, "'vhostname' parameter cannot exceed 255 bytes");
612 return -1;
613 }
Yann Bordenave7aac5312016-03-15 10:31:22 +0100614
Fam Zheng0fca92b2018-09-14 15:26:16 +0800615 if (tftp_server_name && strlen(tftp_server_name) > 255) {
616 error_setg(errp, "'tftp-server-name' parameter cannot exceed 255 bytes");
617 return -1;
618 }
619
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100620 nc = qemu_new_net_client(&net_slirp_info, peer, model, name);
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000621
Laurent Vivier53b85d92022-10-21 11:09:10 +0200622 qemu_set_info_str(nc, "net=%s,restrict=%s", inet_ntoa(net),
623 restricted ? "on" : "off");
Jason Wangd89b4f82021-04-02 11:03:12 +0800624
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000625 s = DO_UPCAST(SlirpState, nc, nc);
626
Paolo Bonzini6222e552022-04-11 09:39:16 +0200627 cfg.version = SLIRP_CHECK_VERSION(4,7,0) ? 4 : 1;
Paolo Bonzinibce63de2022-04-11 10:16:36 +0200628 cfg.restricted = restricted;
629 cfg.in_enabled = ipv4;
630 cfg.vnetwork = net;
631 cfg.vnetmask = mask;
632 cfg.vhost = host;
633 cfg.in6_enabled = ipv6;
634 cfg.vprefix_addr6 = ip6_prefix;
635 cfg.vprefix_len = vprefix6_len;
636 cfg.vhost6 = ip6_host;
637 cfg.vhostname = vhostname;
638 cfg.tftp_server_name = tftp_server_name;
639 cfg.tftp_path = tftp_export;
640 cfg.bootfile = bootfile;
641 cfg.vdhcp_start = dhcp;
642 cfg.vnameserver = dns;
643 cfg.vnameserver6 = ip6_dns;
644 cfg.vdnssearch = dnssearch;
645 cfg.vdomainname = vdomainname;
646 s->slirp = slirp_new(&cfg, &slirp_cb, s);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000647 QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry);
648
Marc-André Lureaud8903442019-02-12 17:25:19 +0100649 /*
650 * Make sure the current bitstream version of slirp is 4, to avoid
651 * QEMU migration incompatibilities, if upstream slirp bumped the
652 * version.
653 *
654 * FIXME: use bitfields of features? teach libslirp to save with
655 * specific version?
656 */
657 g_assert(slirp_state_version() == 4);
Dr. David Alan Gilbertce62df52019-08-22 12:54:33 +0100658 register_savevm_live("slirp", 0, slirp_state_version(),
Marc-André Lureaud8903442019-02-12 17:25:19 +0100659 &savevm_slirp_state, s->slirp);
660
Marc-André Lureau1ab67b92019-01-17 15:43:55 +0400661 s->poll_notifier.notify = net_slirp_poll_notify;
662 main_loop_poll_add_notifier(&s->poll_notifier);
663
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000664 for (config = slirp_configs; config; config = config->next) {
665 if (config->flags & SLIRP_CFG_HOSTFWD) {
Thomas Huthd18572d2018-08-22 15:43:30 +0200666 if (slirp_hostfwd(s, config->str, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000667 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200668 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000669 } else {
Thomas Huthd18572d2018-08-22 15:43:30 +0200670 if (slirp_guestfwd(s, config->str, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000671 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200672 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000673 }
674 }
Paolo Bonzini35acbb32021-10-13 13:43:36 +0200675#if defined(CONFIG_SMBD_COMMAND)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000676 if (smb_export) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200677 if (slirp_smb(s, smb_export, smbsrv, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000678 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200679 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000680 }
681#endif
682
Paolo Bonzinif6c2e662016-07-12 09:57:12 +0200683 s->exit_notifier.notify = slirp_smb_exit;
684 qemu_add_exit_notifier(&s->exit_notifier);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000685 return 0;
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000686
687error:
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100688 qemu_del_net_client(nc);
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000689 return -1;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000690}
691
Thomas Huthb4983c52019-12-05 11:41:09 +0100692static SlirpState *slirp_lookup(Monitor *mon, const char *id)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000693{
Thomas Huthb4983c52019-12-05 11:41:09 +0100694 if (id) {
695 NetClientState *nc = qemu_find_netdev(id);
696 if (!nc) {
697 monitor_printf(mon, "unrecognized netdev id '%s'\n", id);
698 return NULL;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000699 }
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000700 if (strcmp(nc->model, "user")) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000701 monitor_printf(mon, "invalid device specified\n");
702 return NULL;
703 }
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000704 return DO_UPCAST(SlirpState, nc, nc);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000705 } else {
706 if (QTAILQ_EMPTY(&slirp_stacks)) {
707 monitor_printf(mon, "user mode network stack not in use\n");
708 return NULL;
709 }
710 return QTAILQ_FIRST(&slirp_stacks);
711 }
712}
713
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100714void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000715{
716 struct in_addr host_addr = { .s_addr = INADDR_ANY };
717 int host_port;
Markus Armbrustere30e5eb2011-11-16 15:45:59 +0100718 char buf[256];
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000719 const char *src_str, *p;
720 SlirpState *s;
721 int is_udp = 0;
722 int err;
723 const char *arg1 = qdict_get_str(qdict, "arg1");
724 const char *arg2 = qdict_get_try_str(qdict, "arg2");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000725
Thomas Huthb4983c52019-12-05 11:41:09 +0100726 if (arg2) {
727 s = slirp_lookup(mon, arg1);
Thomas Huth93653062018-01-11 21:02:40 +0100728 src_str = arg2;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000729 } else {
Thomas Huthb4983c52019-12-05 11:41:09 +0100730 s = slirp_lookup(mon, NULL);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000731 src_str = arg1;
732 }
733 if (!s) {
734 return;
735 }
736
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000737 p = src_str;
Markus Armbrustere30e5eb2011-11-16 15:45:59 +0100738 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
739 goto fail_syntax;
740 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000741
742 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
743 is_udp = 0;
744 } else if (!strcmp(buf, "udp")) {
745 is_udp = 1;
746 } else {
747 goto fail_syntax;
748 }
749
750 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
751 goto fail_syntax;
752 }
753 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
754 goto fail_syntax;
755 }
756
Nia Alarie1fb3f7f2018-03-16 14:39:21 +0000757 if (qemu_strtoi(p, NULL, 10, &host_port)) {
758 goto fail_syntax;
759 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000760
Peter Maydell70381662014-06-16 16:47:49 +0100761 err = slirp_remove_hostfwd(s->slirp, is_udp, host_addr, host_port);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000762
763 monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
Geoffrey Thomasb15ba6c2011-12-17 04:23:59 -0500764 err ? "not found" : "removed");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000765 return;
766
767 fail_syntax:
768 monitor_printf(mon, "invalid format\n");
769}
770
Thomas Huthd18572d2018-08-22 15:43:30 +0200771static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000772{
773 struct in_addr host_addr = { .s_addr = INADDR_ANY };
774 struct in_addr guest_addr = { .s_addr = 0 };
775 int host_port, guest_port;
776 const char *p;
777 char buf[256];
778 int is_udp;
779 char *end;
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100780 const char *fail_reason = "Unknown reason";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000781
782 p = redir_str;
783 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100784 fail_reason = "No : separators";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000785 goto fail_syntax;
786 }
787 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
788 is_udp = 0;
789 } else if (!strcmp(buf, "udp")) {
790 is_udp = 1;
791 } else {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100792 fail_reason = "Bad protocol name";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000793 goto fail_syntax;
794 }
795
Thomas Huthd18572d2018-08-22 15:43:30 +0200796 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
797 fail_reason = "Missing : separator";
798 goto fail_syntax;
799 }
800 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
801 fail_reason = "Bad host address";
802 goto fail_syntax;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000803 }
804
Thomas Huthd18572d2018-08-22 15:43:30 +0200805 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100806 fail_reason = "Bad host port separator";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000807 goto fail_syntax;
808 }
809 host_port = strtol(buf, &end, 0);
Vincent Bernat0bed71e2017-02-25 22:31:58 +0100810 if (*end != '\0' || host_port < 0 || host_port > 65535) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100811 fail_reason = "Bad host port";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000812 goto fail_syntax;
813 }
814
815 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100816 fail_reason = "Missing guest address";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000817 goto fail_syntax;
818 }
819 if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100820 fail_reason = "Bad guest address";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000821 goto fail_syntax;
822 }
823
824 guest_port = strtol(p, &end, 0);
825 if (*end != '\0' || guest_port < 1 || guest_port > 65535) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100826 fail_reason = "Bad guest port";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000827 goto fail_syntax;
828 }
829
830 if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr,
831 guest_port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200832 error_setg(errp, "Could not set up host forwarding rule '%s'",
833 redir_str);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000834 return -1;
835 }
836 return 0;
837
838 fail_syntax:
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100839 error_setg(errp, "Invalid host forwarding rule '%s' (%s)", redir_str,
840 fail_reason);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000841 return -1;
842}
843
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100844void hmp_hostfwd_add(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000845{
846 const char *redir_str;
847 SlirpState *s;
848 const char *arg1 = qdict_get_str(qdict, "arg1");
849 const char *arg2 = qdict_get_try_str(qdict, "arg2");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000850
Thomas Huthb4983c52019-12-05 11:41:09 +0100851 if (arg2) {
852 s = slirp_lookup(mon, arg1);
Thomas Huth93653062018-01-11 21:02:40 +0100853 redir_str = arg2;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000854 } else {
Thomas Huthb4983c52019-12-05 11:41:09 +0100855 s = slirp_lookup(mon, NULL);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000856 redir_str = arg1;
857 }
858 if (s) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200859 Error *err = NULL;
Thomas Huthd18572d2018-08-22 15:43:30 +0200860 if (slirp_hostfwd(s, redir_str, &err) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200861 error_report_err(err);
862 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000863 }
864
865}
866
Paolo Bonzini35acbb32021-10-13 13:43:36 +0200867#if defined(CONFIG_SMBD_COMMAND)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000868
869/* automatic user mode samba server configuration */
870static void slirp_smb_cleanup(SlirpState *s)
871{
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100872 int ret;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000873
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100874 if (s->smb_dir) {
875 gchar *cmd = g_strdup_printf("rm -rf %s", s->smb_dir);
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100876 ret = system(cmd);
Juan Quintela24ac07d2010-03-04 10:00:31 +0100877 if (ret == -1 || !WIFEXITED(ret)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100878 error_report("'%s' failed.", cmd);
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100879 } else if (WEXITSTATUS(ret)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100880 error_report("'%s' failed. Error code: %d",
881 cmd, WEXITSTATUS(ret));
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100882 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100883 g_free(cmd);
884 g_free(s->smb_dir);
885 s->smb_dir = NULL;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000886 }
887}
888
889static int slirp_smb(SlirpState* s, const char *exported_dir,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200890 struct in_addr vserver_addr, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000891{
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100892 char *smb_conf;
893 char *smb_cmdline;
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200894 struct passwd *passwd;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000895 FILE *f;
896
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200897 passwd = getpwuid(geteuid());
898 if (!passwd) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200899 error_setg(errp, "Failed to retrieve user name");
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200900 return -1;
901 }
902
Dunrong Huang927d8112012-07-06 14:04:43 +0800903 if (access(CONFIG_SMBD_COMMAND, F_OK)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200904 error_setg(errp, "Could not find '%s', please install it",
905 CONFIG_SMBD_COMMAND);
Dunrong Huang927d8112012-07-06 14:04:43 +0800906 return -1;
907 }
908
909 if (access(exported_dir, R_OK | X_OK)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200910 error_setg(errp, "Error accessing shared directory '%s': %s",
911 exported_dir, strerror(errno));
Dunrong Huang927d8112012-07-06 14:04:43 +0800912 return -1;
913 }
914
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100915 s->smb_dir = g_dir_make_tmp("qemu-smb.XXXXXX", NULL);
916 if (!s->smb_dir) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200917 error_setg(errp, "Could not create samba server dir");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000918 return -1;
919 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100920 smb_conf = g_strdup_printf("%s/%s", s->smb_dir, "smb.conf");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000921
922 f = fopen(smb_conf, "w");
923 if (!f) {
924 slirp_smb_cleanup(s);
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200925 error_setg(errp,
926 "Could not create samba server configuration file '%s'",
927 smb_conf);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100928 g_free(smb_conf);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000929 return -1;
930 }
931 fprintf(f,
932 "[global]\n"
933 "private dir=%s\n"
Peter Wu7912d042014-11-03 11:52:10 +0100934 "interfaces=127.0.0.1\n"
935 "bind interfaces only=yes\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000936 "pid directory=%s\n"
937 "lock directory=%s\n"
Nikolaus Rath276eda52012-04-25 09:57:19 -0400938 "state directory=%s\n"
Peter Wu7912d042014-11-03 11:52:10 +0100939 "cache directory=%s\n"
Michael Bueschb87b8a82014-04-27 14:54:12 +0400940 "ncalrpc dir=%s/ncalrpc\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000941 "log file=%s/log.smbd\n"
942 "smb passwd file=%s/smbpasswd\n"
Michael Bueschc2804ee2013-11-01 12:23:49 +0100943 "security = user\n"
944 "map to guest = Bad User\n"
Peter Wu7912d042014-11-03 11:52:10 +0100945 "load printers = no\n"
946 "printing = bsd\n"
947 "disable spoolss = yes\n"
948 "usershare max shares = 0\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000949 "[qemu]\n"
950 "path=%s\n"
951 "read only=no\n"
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200952 "guest ok=yes\n"
953 "force user=%s\n",
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000954 s->smb_dir,
955 s->smb_dir,
956 s->smb_dir,
957 s->smb_dir,
958 s->smb_dir,
Nikolaus Rath276eda52012-04-25 09:57:19 -0400959 s->smb_dir,
Michael Bueschb87b8a82014-04-27 14:54:12 +0400960 s->smb_dir,
Peter Wu7912d042014-11-03 11:52:10 +0100961 s->smb_dir,
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200962 exported_dir,
963 passwd->pw_name
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000964 );
965 fclose(f);
966
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100967 smb_cmdline = g_strdup_printf("%s -l %s -s %s",
Michael Tokarev44d8d2b2014-10-25 00:29:50 +0400968 CONFIG_SMBD_COMMAND, s->smb_dir, smb_conf);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100969 g_free(smb_conf);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000970
Marc-André Lureau44b4ff22019-01-17 15:43:33 +0400971 if (slirp_add_exec(s->slirp, smb_cmdline, &vserver_addr, 139) < 0 ||
972 slirp_add_exec(s->slirp, smb_cmdline, &vserver_addr, 445) < 0) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000973 slirp_smb_cleanup(s);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100974 g_free(smb_cmdline);
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200975 error_setg(errp, "Conflicting/invalid smbserver address");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000976 return -1;
977 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100978 g_free(smb_cmdline);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000979 return 0;
980}
981
Paolo Bonzini35acbb32021-10-13 13:43:36 +0200982#endif /* defined(CONFIG_SMBD_COMMAND) */
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000983
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000984static int guestfwd_can_read(void *opaque)
985{
986 struct GuestFwd *fwd = opaque;
987 return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port);
988}
989
990static void guestfwd_read(void *opaque, const uint8_t *buf, int size)
991{
992 struct GuestFwd *fwd = opaque;
993 slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size);
994}
995
Marc-André Lureau625a5262019-01-17 15:43:54 +0400996static ssize_t guestfwd_write(const void *buf, size_t len, void *chr)
Marc-André Lureau44b4ff22019-01-17 15:43:33 +0400997{
998 return qemu_chr_fe_write_all(chr, buf, len);
999}
1000
Thomas Huthd18572d2018-08-22 15:43:30 +02001001static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001002{
Samuel Thibaultffe02f52016-04-01 00:46:35 +02001003 /* TODO: IPv6 */
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001004 struct in_addr server = { .s_addr = 0 };
1005 struct GuestFwd *fwd;
1006 const char *p;
1007 char buf[128];
1008 char *end;
1009 int port;
1010
1011 p = config_str;
Thomas Huthd18572d2018-08-22 15:43:30 +02001012 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
1013 goto fail_syntax;
1014 }
1015 if (strcmp(buf, "tcp") && buf[0] != '\0') {
1016 goto fail_syntax;
1017 }
1018 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
1019 goto fail_syntax;
1020 }
1021 if (buf[0] != '\0' && !inet_aton(buf, &server)) {
1022 goto fail_syntax;
1023 }
1024 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
1025 goto fail_syntax;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001026 }
1027 port = strtol(buf, &end, 10);
1028 if (*end != '\0' || port < 1 || port > 65535) {
1029 goto fail_syntax;
1030 }
1031
Alexander Grafa9899992011-06-04 07:25:59 +02001032 snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001033
Marc-André Lureau36247302019-01-17 15:43:34 +04001034 if (g_str_has_prefix(p, "cmd:")) {
Marc-André Lureau44b4ff22019-01-17 15:43:33 +04001035 if (slirp_add_exec(s->slirp, &p[4], &server, port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +02001036 error_setg(errp, "Conflicting/invalid host:port in guest "
1037 "forwarding rule '%s'", config_str);
Alexander Grafb412eb62012-06-03 09:45:01 +02001038 return -1;
1039 }
1040 } else {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03001041 Error *err = NULL;
Marc-André Lureau95e30b22018-08-22 19:19:42 +02001042 /*
1043 * FIXME: sure we want to support implicit
1044 * muxed monitors here?
1045 */
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01001046 Chardev *chr = qemu_chr_new_mux_mon(buf, p, NULL);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03001047
1048 if (!chr) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +02001049 error_setg(errp, "Could not open guest forwarding device '%s'",
1050 buf);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03001051 return -1;
1052 }
1053
1054 fwd = g_new(struct GuestFwd, 1);
1055 qemu_chr_fe_init(&fwd->hd, chr, &err);
1056 if (err) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +02001057 error_propagate(errp, err);
Marc-André Lureau8e207c32019-01-17 15:43:36 +04001058 object_unparent(OBJECT(chr));
Alexander Grafb412eb62012-06-03 09:45:01 +02001059 g_free(fwd);
1060 return -1;
1061 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001062
Marc-André Lureau44b4ff22019-01-17 15:43:33 +04001063 if (slirp_add_guestfwd(s->slirp, guestfwd_write, &fwd->hd,
1064 &server, port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +02001065 error_setg(errp, "Conflicting/invalid host:port in guest "
1066 "forwarding rule '%s'", config_str);
Marc-André Lureau8e207c32019-01-17 15:43:36 +04001067 qemu_chr_fe_deinit(&fwd->hd, true);
Alexander Grafb412eb62012-06-03 09:45:01 +02001068 g_free(fwd);
1069 return -1;
1070 }
1071 fwd->server = server;
1072 fwd->port = port;
1073 fwd->slirp = s->slirp;
1074
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001075 qemu_chr_fe_set_handlers(&fwd->hd, guestfwd_can_read, guestfwd_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +03001076 NULL, NULL, fwd, NULL, true);
Marc-André Lureau8d45a3b2019-01-17 15:43:35 +04001077 s->fwd = g_slist_append(s->fwd, fwd);
Alexander Grafb412eb62012-06-03 09:45:01 +02001078 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001079 return 0;
1080
1081 fail_syntax:
Hervé Poussineau5c843af2017-07-15 18:43:50 +02001082 error_setg(errp, "Invalid guest forwarding rule '%s'", config_str);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001083 return -1;
1084}
1085
Markus Armbruster1ce6be22015-02-06 14:18:24 +01001086void hmp_info_usernet(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001087{
1088 SlirpState *s;
1089
1090 QTAILQ_FOREACH(s, &slirp_stacks, entry) {
Stefan Hajnoczi90d87a32012-07-24 16:35:06 +01001091 int id;
Thomas Huth442da402018-04-30 20:02:24 +02001092 bool got_hub_id = net_hub_id_for_client(&s->nc, &id) == 0;
Marc-André Lureaub7f43bf2018-11-10 17:45:43 +04001093 char *info = slirp_connection_info(s->slirp);
1094 monitor_printf(mon, "Hub %d (%s):\n%s",
Thomas Huth442da402018-04-30 20:02:24 +02001095 got_hub_id ? id : -1,
Marc-André Lureaub7f43bf2018-11-10 17:45:43 +04001096 s->nc.name, info);
1097 g_free(info);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001098 }
1099}
1100
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001101static void
1102net_init_slirp_configs(const StringList *fwd, int flags)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001103{
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001104 while (fwd) {
1105 struct slirp_config_str *config;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001106
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001107 config = g_malloc0(sizeof(*config));
1108 pstrcpy(config->str, sizeof(config->str), fwd->value->str);
1109 config->flags = flags;
1110 config->next = slirp_configs;
1111 slirp_configs = config;
1112
1113 fwd = fwd->next;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001114 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001115}
1116
Klaus Stengel63d29602012-10-27 19:53:39 +02001117static const char **slirp_dnssearch(const StringList *dnsname)
1118{
1119 const StringList *c = dnsname;
1120 size_t i = 0, num_opts = 0;
1121 const char **ret;
1122
1123 while (c) {
1124 num_opts++;
1125 c = c->next;
1126 }
1127
1128 if (num_opts == 0) {
1129 return NULL;
1130 }
1131
1132 ret = g_malloc((num_opts + 1) * sizeof(*ret));
1133 c = dnsname;
1134 while (c) {
1135 ret[i++] = c->value->str;
1136 c = c->next;
1137 }
1138 ret[i] = NULL;
1139 return ret;
1140}
1141
Kővágó, Zoltáncebea512016-07-13 21:50:12 -06001142int net_init_slirp(const Netdev *netdev, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +02001143 NetClientState *peer, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001144{
1145 struct slirp_config_str *config;
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001146 char *vnet;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001147 int ret;
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001148 const NetdevUserOptions *user;
Klaus Stengel63d29602012-10-27 19:53:39 +02001149 const char **dnssearch;
Samuel Thibault0b11c032016-03-20 12:29:54 +01001150 bool ipv4 = true, ipv6 = true;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001151
Eric Blakef394b2e2016-07-13 21:50:23 -06001152 assert(netdev->type == NET_CLIENT_DRIVER_USER);
1153 user = &netdev->u.user;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001154
Samuel Thibault0b11c032016-03-20 12:29:54 +01001155 if ((user->has_ipv6 && user->ipv6 && !user->has_ipv4) ||
1156 (user->has_ipv4 && !user->ipv4)) {
1157 ipv4 = 0;
1158 }
1159 if ((user->has_ipv4 && user->ipv4 && !user->has_ipv6) ||
1160 (user->has_ipv6 && !user->ipv6)) {
1161 ipv6 = 0;
1162 }
1163
Markus Armbruster74808742022-11-04 17:07:00 +01001164 vnet = user->net ? g_strdup(user->net) :
1165 user->ip ? g_strdup_printf("%s/24", user->ip) :
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001166 NULL;
Jan Kiszkac54ed5b2011-07-20 12:20:14 +02001167
Klaus Stengel63d29602012-10-27 19:53:39 +02001168 dnssearch = slirp_dnssearch(user->dnssearch);
1169
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001170 /* all optional fields are initialized to "all bits zero" */
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001171
Laszlo Ersek094f15c2012-07-17 16:17:16 +02001172 net_init_slirp_configs(user->hostfwd, SLIRP_CFG_HOSTFWD);
1173 net_init_slirp_configs(user->guestfwd, 0);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001174
Samuel Thibault0b11c032016-03-20 12:29:54 +01001175 ret = net_slirp_init(peer, "user", name, user->q_restrict,
1176 ipv4, vnet, user->host,
1177 ipv6, user->ipv6_prefix, user->ipv6_prefixlen,
Samuel Thibaultd8eb3862016-03-25 00:02:58 +01001178 user->ipv6_host, user->hostname, user->tftp,
Yann Bordenave7aac5312016-03-15 10:31:22 +01001179 user->bootfile, user->dhcpstart,
Samuel Thibaultd8eb3862016-03-25 00:02:58 +01001180 user->dns, user->ipv6_dns, user->smb,
Fam Zheng0fca92b2018-09-14 15:26:16 +08001181 user->smbserver, dnssearch, user->domainname,
1182 user->tftp_server_name, errp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001183
1184 while (slirp_configs) {
1185 config = slirp_configs;
1186 slirp_configs = config->next;
Anthony Liguori7267c092011-08-20 22:09:37 -05001187 g_free(config);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001188 }
1189
Anthony Liguori7267c092011-08-20 22:09:37 -05001190 g_free(vnet);
Klaus Stengel63d29602012-10-27 19:53:39 +02001191 g_free(dnssearch);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001192
1193 return ret;
1194}