blob: 78ba96b63f7228e427992625f160fc08cb96c49b [file] [log] [blame]
Mark McLoughlin68ac40d2009-11-25 18:48:54 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Markus Armbruster452fcdb2018-02-01 12:18:39 +010024
Peter Maydell2744d922016-01-29 17:50:00 +000025#include "qemu/osdep.h"
Marc-André Lureau2addc8f2018-11-14 16:36:33 +040026#include "qemu/log.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000027#include "net/slirp.h"
28
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000029
Blue Swirl28b150b2010-01-27 17:47:33 +000030#ifndef _WIN32
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +020031#include <pwd.h>
Blue Swirl28b150b2010-01-27 17:47:33 +000032#include <sys/wait.h>
33#endif
Paolo Bonzini1422e322012-10-24 08:43:34 +020034#include "net/net.h"
Paolo Bonzinia245fc12012-09-17 18:43:51 +020035#include "clients.h"
36#include "hub.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010037#include "monitor/monitor.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010038#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010039#include "qemu/sockets.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000040#include "slirp/libslirp.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040041#include "chardev/char-fe.h"
Paolo Bonzinif6c2e662016-07-12 09:57:12 +020042#include "sysemu/sysemu.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020043#include "qemu/cutils.h"
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +030044#include "qapi/error.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010045#include "qapi/qmp/qdict.h"
Marc-André Lureaue05ae1d2018-11-14 16:36:40 +040046#include "util.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000047
48static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
49{
50 const char *p, *p1;
51 int len;
52 p = *pp;
53 p1 = strchr(p, sep);
54 if (!p1)
55 return -1;
56 len = p1 - p;
57 p1++;
58 if (buf_size > 0) {
59 if (len > buf_size - 1)
60 len = buf_size - 1;
61 memcpy(buf, p, len);
62 buf[len] = '\0';
63 }
64 *pp = p1;
65 return 0;
66}
67
68/* slirp network adapter */
69
70#define SLIRP_CFG_HOSTFWD 1
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000071
72struct slirp_config_str {
73 struct slirp_config_str *next;
74 int flags;
75 char str[1024];
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000076};
77
Marc-André Lureau8d45a3b2019-01-17 15:43:35 +040078struct GuestFwd {
79 CharBackend hd;
80 struct in_addr server;
81 int port;
82 Slirp *slirp;
83};
84
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000085typedef struct SlirpState {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010086 NetClientState nc;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000087 QTAILQ_ENTRY(SlirpState) entry;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000088 Slirp *slirp;
Paolo Bonzinif6c2e662016-07-12 09:57:12 +020089 Notifier exit_notifier;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000090#ifndef _WIN32
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +010091 gchar *smb_dir;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000092#endif
Marc-André Lureau8d45a3b2019-01-17 15:43:35 +040093 GSList *fwd;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000094} SlirpState;
95
96static struct slirp_config_str *slirp_configs;
Paolo Bonzinib58deb32018-12-06 11:58:10 +010097static QTAILQ_HEAD(, SlirpState) slirp_stacks =
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000098 QTAILQ_HEAD_INITIALIZER(slirp_stacks);
99
Thomas Huthd18572d2018-08-22 15:43:30 +0200100static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp);
101static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000102
103#ifndef _WIN32
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000104static int slirp_smb(SlirpState *s, const char *exported_dir,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200105 struct in_addr vserver_addr, Error **errp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000106static void slirp_smb_cleanup(SlirpState *s);
107#else
108static inline void slirp_smb_cleanup(SlirpState *s) { }
109#endif
110
Marc-André Lureau62c1d2c2018-11-10 17:45:36 +0400111static void net_slirp_output(void *opaque, const uint8_t *pkt, int pkt_len)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000112{
113 SlirpState *s = opaque;
114
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000115 qemu_send_packet(&s->nc, pkt, pkt_len);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000116}
117
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100118static ssize_t net_slirp_receive(NetClientState *nc, const uint8_t *buf, size_t size)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000119{
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000120 SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000121
122 slirp_input(s->slirp, buf, size);
123
124 return size;
125}
126
Paolo Bonzinif6c2e662016-07-12 09:57:12 +0200127static void slirp_smb_exit(Notifier *n, void *data)
128{
129 SlirpState *s = container_of(n, SlirpState, exit_notifier);
130 slirp_smb_cleanup(s);
131}
132
Marc-André Lureau8d45a3b2019-01-17 15:43:35 +0400133static void slirp_free_fwd(gpointer data)
134{
135 struct GuestFwd *fwd = data;
136
137 qemu_chr_fe_deinit(&fwd->hd, true);
138 g_free(data);
139}
140
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100141static void net_slirp_cleanup(NetClientState *nc)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000142{
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000143 SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000144
Marc-André Lureau8d45a3b2019-01-17 15:43:35 +0400145 g_slist_free_full(s->fwd, slirp_free_fwd);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000146 slirp_cleanup(s->slirp);
Marc-André Lureau67f32802016-08-18 17:44:05 +0400147 if (s->exit_notifier.notify) {
148 qemu_remove_exit_notifier(&s->exit_notifier);
149 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000150 slirp_smb_cleanup(s);
151 QTAILQ_REMOVE(&slirp_stacks, s, entry);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000152}
153
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000154static NetClientInfo net_slirp_info = {
Eric Blakef394b2e2016-07-13 21:50:23 -0600155 .type = NET_CLIENT_DRIVER_USER,
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000156 .size = sizeof(SlirpState),
157 .receive = net_slirp_receive,
158 .cleanup = net_slirp_cleanup,
159};
160
Marc-André Lureau2addc8f2018-11-14 16:36:33 +0400161static void net_slirp_guest_error(const char *msg)
162{
163 qemu_log_mask(LOG_GUEST_ERROR, "%s", msg);
164}
165
Marc-André Lureaue6dbff32018-11-22 02:06:28 +0400166static int64_t net_slirp_clock_get_ns(void)
167{
168 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
169}
170
Marc-André Lureau07abf6d2019-01-17 15:43:37 +0400171static void *net_slirp_timer_new(SlirpTimerCb cb, void *opaque)
172{
173 return timer_new_full(NULL, QEMU_CLOCK_VIRTUAL,
174 SCALE_MS, QEMU_TIMER_ATTR_EXTERNAL,
175 cb, opaque);
176}
177
178static void net_slirp_timer_free(void *timer)
179{
180 timer_del(timer);
181 timer_free(timer);
182}
183
184static void net_slirp_timer_mod(void *timer, int64_t expire_timer)
185{
186 timer_mod(timer, expire_timer);
187}
188
Marc-André Lureau848c7092019-01-17 15:43:41 +0400189static void net_slirp_register_poll_fd(int fd)
190{
191 qemu_fd_register(fd);
192}
193
Marc-André Lureauf6e5aa32019-01-17 15:43:42 +0400194static void net_slirp_unregister_poll_fd(int fd)
195{
196 /* no qemu_fd_unregister */
197}
198
Marc-André Lureaud846b922018-11-14 16:36:07 +0400199static const SlirpCb slirp_cb = {
200 .output = net_slirp_output,
Marc-André Lureau2addc8f2018-11-14 16:36:33 +0400201 .guest_error = net_slirp_guest_error,
Marc-André Lureaue6dbff32018-11-22 02:06:28 +0400202 .clock_get_ns = net_slirp_clock_get_ns,
Marc-André Lureau07abf6d2019-01-17 15:43:37 +0400203 .timer_new = net_slirp_timer_new,
204 .timer_free = net_slirp_timer_free,
205 .timer_mod = net_slirp_timer_mod,
Marc-André Lureau848c7092019-01-17 15:43:41 +0400206 .register_poll_fd = net_slirp_register_poll_fd,
Marc-André Lureauf6e5aa32019-01-17 15:43:42 +0400207 .unregister_poll_fd = net_slirp_unregister_poll_fd,
Marc-André Lureaud846b922018-11-14 16:36:07 +0400208};
209
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100210static int net_slirp_init(NetClientState *peer, const char *model,
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000211 const char *name, int restricted,
Samuel Thibault0b11c032016-03-20 12:29:54 +0100212 bool ipv4, const char *vnetwork, const char *vhost,
213 bool ipv6, const char *vprefix6, int vprefix6_len,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100214 const char *vhost6,
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000215 const char *vhostname, const char *tftp_export,
216 const char *bootfile, const char *vdhcp_start,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100217 const char *vnameserver, const char *vnameserver6,
218 const char *smb_export, const char *vsmbserver,
Benjamin Drungf18d1372018-02-27 17:06:01 +0100219 const char **dnssearch, const char *vdomainname,
Fam Zheng0fca92b2018-09-14 15:26:16 +0800220 const char *tftp_server_name,
Benjamin Drungf18d1372018-02-27 17:06:01 +0100221 Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000222{
223 /* default settings according to historic slirp */
224 struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */
225 struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */
226 struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */
227 struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */
228 struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */
Yann Bordenave7aac5312016-03-15 10:31:22 +0100229 struct in6_addr ip6_prefix;
230 struct in6_addr ip6_host;
231 struct in6_addr ip6_dns;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000232#ifndef _WIN32
233 struct in_addr smbsrv = { .s_addr = 0 };
234#endif
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100235 NetClientState *nc;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000236 SlirpState *s;
237 char buf[20];
238 uint32_t addr;
239 int shift;
240 char *end;
241 struct slirp_config_str *config;
242
Samuel Thibault0b11c032016-03-20 12:29:54 +0100243 if (!ipv4 && (vnetwork || vhost || vnameserver)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200244 error_setg(errp, "IPv4 disabled but netmask/host/dns provided");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100245 return -1;
246 }
247
248 if (!ipv6 && (vprefix6 || vhost6 || vnameserver6)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200249 error_setg(errp, "IPv6 disabled but prefix/host6/dns6 provided");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100250 return -1;
251 }
252
253 if (!ipv4 && !ipv6) {
254 /* It doesn't make sense to disable both */
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200255 error_setg(errp, "IPv4 and IPv6 disabled");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100256 return -1;
257 }
258
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000259 if (vnetwork) {
260 if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) {
261 if (!inet_aton(vnetwork, &net)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200262 error_setg(errp, "Failed to parse netmask");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000263 return -1;
264 }
265 addr = ntohl(net.s_addr);
266 if (!(addr & 0x80000000)) {
267 mask.s_addr = htonl(0xff000000); /* class A */
268 } else if ((addr & 0xfff00000) == 0xac100000) {
269 mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */
270 } else if ((addr & 0xc0000000) == 0x80000000) {
271 mask.s_addr = htonl(0xffff0000); /* class B */
272 } else if ((addr & 0xffff0000) == 0xc0a80000) {
273 mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */
274 } else if ((addr & 0xffff0000) == 0xc6120000) {
275 mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */
276 } else if ((addr & 0xe0000000) == 0xe0000000) {
277 mask.s_addr = htonl(0xffffff00); /* class C */
278 } else {
279 mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */
280 }
281 } else {
282 if (!inet_aton(buf, &net)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200283 error_setg(errp, "Failed to parse netmask");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000284 return -1;
285 }
286 shift = strtol(vnetwork, &end, 10);
287 if (*end != '\0') {
288 if (!inet_aton(vnetwork, &mask)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200289 error_setg(errp,
290 "Failed to parse netmask (trailing chars)");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000291 return -1;
292 }
293 } else if (shift < 4 || shift > 32) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200294 error_setg(errp,
295 "Invalid netmask provided (must be in range 4-32)");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000296 return -1;
297 } else {
298 mask.s_addr = htonl(0xffffffff << (32 - shift));
299 }
300 }
301 net.s_addr &= mask.s_addr;
302 host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr);
303 dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr);
304 dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr);
305 }
306
307 if (vhost && !inet_aton(vhost, &host)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200308 error_setg(errp, "Failed to parse host");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000309 return -1;
310 }
311 if ((host.s_addr & mask.s_addr) != net.s_addr) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200312 error_setg(errp, "Host doesn't belong to network");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000313 return -1;
314 }
315
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000316 if (vnameserver && !inet_aton(vnameserver, &dns)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200317 error_setg(errp, "Failed to parse DNS");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000318 return -1;
319 }
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200320 if ((dns.s_addr & mask.s_addr) != net.s_addr) {
321 error_setg(errp, "DNS doesn't belong to network");
322 return -1;
323 }
324 if (dns.s_addr == host.s_addr) {
325 error_setg(errp, "DNS must be different from host");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000326 return -1;
327 }
328
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200329 if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200330 error_setg(errp, "Failed to parse DHCP start address");
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200331 return -1;
332 }
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200333 if ((dhcp.s_addr & mask.s_addr) != net.s_addr) {
334 error_setg(errp, "DHCP doesn't belong to network");
335 return -1;
336 }
337 if (dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) {
338 error_setg(errp, "DNS must be different from host and DNS");
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200339 return -1;
340 }
341
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000342#ifndef _WIN32
343 if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200344 error_setg(errp, "Failed to parse SMB address");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000345 return -1;
346 }
347#endif
348
Yann Bordenave7aac5312016-03-15 10:31:22 +0100349 if (!vprefix6) {
350 vprefix6 = "fec0::";
351 }
352 if (!inet_pton(AF_INET6, vprefix6, &ip6_prefix)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200353 error_setg(errp, "Failed to parse IPv6 prefix");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100354 return -1;
355 }
Yann Bordenave7aac5312016-03-15 10:31:22 +0100356
357 if (!vprefix6_len) {
358 vprefix6_len = 64;
359 }
360 if (vprefix6_len < 0 || vprefix6_len > 126) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200361 error_setg(errp,
362 "Invalid prefix provided (prefix len must be in range 0-126");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100363 return -1;
364 }
365
366 if (vhost6) {
Yann Bordenave7aac5312016-03-15 10:31:22 +0100367 if (!inet_pton(AF_INET6, vhost6, &ip6_host)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200368 error_setg(errp, "Failed to parse IPv6 host");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100369 return -1;
370 }
371 if (!in6_equal_net(&ip6_prefix, &ip6_host, vprefix6_len)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200372 error_setg(errp, "IPv6 Host doesn't belong to network");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100373 return -1;
374 }
Yann Bordenave7aac5312016-03-15 10:31:22 +0100375 } else {
376 ip6_host = ip6_prefix;
377 ip6_host.s6_addr[15] |= 2;
378 }
379
380 if (vnameserver6) {
Yann Bordenave7aac5312016-03-15 10:31:22 +0100381 if (!inet_pton(AF_INET6, vnameserver6, &ip6_dns)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200382 error_setg(errp, "Failed to parse IPv6 DNS");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100383 return -1;
384 }
385 if (!in6_equal_net(&ip6_prefix, &ip6_dns, vprefix6_len)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200386 error_setg(errp, "IPv6 DNS doesn't belong to network");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100387 return -1;
388 }
Yann Bordenave7aac5312016-03-15 10:31:22 +0100389 } else {
390 ip6_dns = ip6_prefix;
391 ip6_dns.s6_addr[15] |= 3;
392 }
393
Benjamin Drungf18d1372018-02-27 17:06:01 +0100394 if (vdomainname && !*vdomainname) {
395 error_setg(errp, "'domainname' parameter cannot be empty");
396 return -1;
397 }
398
Fam Zheng6e157a02018-09-14 15:26:15 +0800399 if (vdomainname && strlen(vdomainname) > 255) {
400 error_setg(errp, "'domainname' parameter cannot exceed 255 bytes");
401 return -1;
402 }
403
404 if (vhostname && strlen(vhostname) > 255) {
405 error_setg(errp, "'vhostname' parameter cannot exceed 255 bytes");
406 return -1;
407 }
Yann Bordenave7aac5312016-03-15 10:31:22 +0100408
Fam Zheng0fca92b2018-09-14 15:26:16 +0800409 if (tftp_server_name && strlen(tftp_server_name) > 255) {
410 error_setg(errp, "'tftp-server-name' parameter cannot exceed 255 bytes");
411 return -1;
412 }
413
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100414 nc = qemu_new_net_client(&net_slirp_info, peer, model, name);
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000415
416 snprintf(nc->info_str, sizeof(nc->info_str),
Jan Kiszkac54ed5b2011-07-20 12:20:14 +0200417 "net=%s,restrict=%s", inet_ntoa(net),
418 restricted ? "on" : "off");
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000419
420 s = DO_UPCAST(SlirpState, nc, nc);
421
Samuel Thibault0b11c032016-03-20 12:29:54 +0100422 s->slirp = slirp_init(restricted, ipv4, net, mask, host,
423 ipv6, ip6_prefix, vprefix6_len, ip6_host,
Fam Zheng0fca92b2018-09-14 15:26:16 +0800424 vhostname, tftp_server_name,
425 tftp_export, bootfile, dhcp,
Marc-André Lureau62c1d2c2018-11-10 17:45:36 +0400426 dns, ip6_dns, dnssearch, vdomainname,
Marc-André Lureaud846b922018-11-14 16:36:07 +0400427 &slirp_cb, s);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000428 QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry);
429
430 for (config = slirp_configs; config; config = config->next) {
431 if (config->flags & SLIRP_CFG_HOSTFWD) {
Thomas Huthd18572d2018-08-22 15:43:30 +0200432 if (slirp_hostfwd(s, config->str, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000433 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200434 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000435 } else {
Thomas Huthd18572d2018-08-22 15:43:30 +0200436 if (slirp_guestfwd(s, config->str, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000437 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200438 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000439 }
440 }
441#ifndef _WIN32
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000442 if (smb_export) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200443 if (slirp_smb(s, smb_export, smbsrv, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000444 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200445 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000446 }
447#endif
448
Paolo Bonzinif6c2e662016-07-12 09:57:12 +0200449 s->exit_notifier.notify = slirp_smb_exit;
450 qemu_add_exit_notifier(&s->exit_notifier);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000451 return 0;
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000452
453error:
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100454 qemu_del_net_client(nc);
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000455 return -1;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000456}
457
Thomas Huth93653062018-01-11 21:02:40 +0100458static SlirpState *slirp_lookup(Monitor *mon, const char *hub_id,
459 const char *name)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000460{
Thomas Huth93653062018-01-11 21:02:40 +0100461 if (name) {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100462 NetClientState *nc;
Thomas Huth93653062018-01-11 21:02:40 +0100463 if (hub_id) {
464 nc = net_hub_find_client_by_name(strtol(hub_id, NULL, 0), name);
465 if (!nc) {
Thomas Huth442da402018-04-30 20:02:24 +0200466 monitor_printf(mon, "unrecognized (hub-id, stackname) pair\n");
Thomas Huth93653062018-01-11 21:02:40 +0100467 return NULL;
468 }
Thomas Huth68cb29e2018-09-20 10:22:27 +0200469 warn_report("Using 'hub-id' is deprecated, specify the netdev id "
470 "directly instead");
Thomas Huth93653062018-01-11 21:02:40 +0100471 } else {
472 nc = qemu_find_netdev(name);
473 if (!nc) {
474 monitor_printf(mon, "unrecognized netdev id '%s'\n", name);
475 return NULL;
476 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000477 }
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000478 if (strcmp(nc->model, "user")) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000479 monitor_printf(mon, "invalid device specified\n");
480 return NULL;
481 }
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000482 return DO_UPCAST(SlirpState, nc, nc);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000483 } else {
484 if (QTAILQ_EMPTY(&slirp_stacks)) {
485 monitor_printf(mon, "user mode network stack not in use\n");
486 return NULL;
487 }
488 return QTAILQ_FIRST(&slirp_stacks);
489 }
490}
491
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100492void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000493{
494 struct in_addr host_addr = { .s_addr = INADDR_ANY };
495 int host_port;
Markus Armbrustere30e5eb2011-11-16 15:45:59 +0100496 char buf[256];
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000497 const char *src_str, *p;
498 SlirpState *s;
499 int is_udp = 0;
500 int err;
501 const char *arg1 = qdict_get_str(qdict, "arg1");
502 const char *arg2 = qdict_get_try_str(qdict, "arg2");
503 const char *arg3 = qdict_get_try_str(qdict, "arg3");
504
Thomas Huth93653062018-01-11 21:02:40 +0100505 if (arg3) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000506 s = slirp_lookup(mon, arg1, arg2);
507 src_str = arg3;
Thomas Huth93653062018-01-11 21:02:40 +0100508 } else if (arg2) {
509 s = slirp_lookup(mon, NULL, arg1);
510 src_str = arg2;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000511 } else {
512 s = slirp_lookup(mon, NULL, NULL);
513 src_str = arg1;
514 }
515 if (!s) {
516 return;
517 }
518
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000519 p = src_str;
Markus Armbrustere30e5eb2011-11-16 15:45:59 +0100520 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
521 goto fail_syntax;
522 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000523
524 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
525 is_udp = 0;
526 } else if (!strcmp(buf, "udp")) {
527 is_udp = 1;
528 } else {
529 goto fail_syntax;
530 }
531
532 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
533 goto fail_syntax;
534 }
535 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
536 goto fail_syntax;
537 }
538
Nia Alarie1fb3f7f2018-03-16 14:39:21 +0000539 if (qemu_strtoi(p, NULL, 10, &host_port)) {
540 goto fail_syntax;
541 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000542
Peter Maydell70381662014-06-16 16:47:49 +0100543 err = slirp_remove_hostfwd(s->slirp, is_udp, host_addr, host_port);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000544
545 monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
Geoffrey Thomasb15ba6c2011-12-17 04:23:59 -0500546 err ? "not found" : "removed");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000547 return;
548
549 fail_syntax:
550 monitor_printf(mon, "invalid format\n");
551}
552
Thomas Huthd18572d2018-08-22 15:43:30 +0200553static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000554{
555 struct in_addr host_addr = { .s_addr = INADDR_ANY };
556 struct in_addr guest_addr = { .s_addr = 0 };
557 int host_port, guest_port;
558 const char *p;
559 char buf[256];
560 int is_udp;
561 char *end;
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100562 const char *fail_reason = "Unknown reason";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000563
564 p = redir_str;
565 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100566 fail_reason = "No : separators";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000567 goto fail_syntax;
568 }
569 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
570 is_udp = 0;
571 } else if (!strcmp(buf, "udp")) {
572 is_udp = 1;
573 } else {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100574 fail_reason = "Bad protocol name";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000575 goto fail_syntax;
576 }
577
Thomas Huthd18572d2018-08-22 15:43:30 +0200578 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
579 fail_reason = "Missing : separator";
580 goto fail_syntax;
581 }
582 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
583 fail_reason = "Bad host address";
584 goto fail_syntax;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000585 }
586
Thomas Huthd18572d2018-08-22 15:43:30 +0200587 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100588 fail_reason = "Bad host port separator";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000589 goto fail_syntax;
590 }
591 host_port = strtol(buf, &end, 0);
Vincent Bernat0bed71e2017-02-25 22:31:58 +0100592 if (*end != '\0' || host_port < 0 || host_port > 65535) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100593 fail_reason = "Bad host port";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000594 goto fail_syntax;
595 }
596
597 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100598 fail_reason = "Missing guest address";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000599 goto fail_syntax;
600 }
601 if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100602 fail_reason = "Bad guest address";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000603 goto fail_syntax;
604 }
605
606 guest_port = strtol(p, &end, 0);
607 if (*end != '\0' || guest_port < 1 || guest_port > 65535) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100608 fail_reason = "Bad guest port";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000609 goto fail_syntax;
610 }
611
612 if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr,
613 guest_port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200614 error_setg(errp, "Could not set up host forwarding rule '%s'",
615 redir_str);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000616 return -1;
617 }
618 return 0;
619
620 fail_syntax:
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100621 error_setg(errp, "Invalid host forwarding rule '%s' (%s)", redir_str,
622 fail_reason);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000623 return -1;
624}
625
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100626void hmp_hostfwd_add(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000627{
628 const char *redir_str;
629 SlirpState *s;
630 const char *arg1 = qdict_get_str(qdict, "arg1");
631 const char *arg2 = qdict_get_try_str(qdict, "arg2");
632 const char *arg3 = qdict_get_try_str(qdict, "arg3");
633
Thomas Huth93653062018-01-11 21:02:40 +0100634 if (arg3) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000635 s = slirp_lookup(mon, arg1, arg2);
636 redir_str = arg3;
Thomas Huth93653062018-01-11 21:02:40 +0100637 } else if (arg2) {
638 s = slirp_lookup(mon, NULL, arg1);
639 redir_str = arg2;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000640 } else {
641 s = slirp_lookup(mon, NULL, NULL);
642 redir_str = arg1;
643 }
644 if (s) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200645 Error *err = NULL;
Thomas Huthd18572d2018-08-22 15:43:30 +0200646 if (slirp_hostfwd(s, redir_str, &err) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200647 error_report_err(err);
648 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000649 }
650
651}
652
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000653#ifndef _WIN32
654
655/* automatic user mode samba server configuration */
656static void slirp_smb_cleanup(SlirpState *s)
657{
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100658 int ret;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000659
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100660 if (s->smb_dir) {
661 gchar *cmd = g_strdup_printf("rm -rf %s", s->smb_dir);
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100662 ret = system(cmd);
Juan Quintela24ac07d2010-03-04 10:00:31 +0100663 if (ret == -1 || !WIFEXITED(ret)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100664 error_report("'%s' failed.", cmd);
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100665 } else if (WEXITSTATUS(ret)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100666 error_report("'%s' failed. Error code: %d",
667 cmd, WEXITSTATUS(ret));
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100668 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100669 g_free(cmd);
670 g_free(s->smb_dir);
671 s->smb_dir = NULL;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000672 }
673}
674
675static int slirp_smb(SlirpState* s, const char *exported_dir,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200676 struct in_addr vserver_addr, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000677{
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100678 char *smb_conf;
679 char *smb_cmdline;
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200680 struct passwd *passwd;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000681 FILE *f;
682
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200683 passwd = getpwuid(geteuid());
684 if (!passwd) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200685 error_setg(errp, "Failed to retrieve user name");
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200686 return -1;
687 }
688
Dunrong Huang927d8112012-07-06 14:04:43 +0800689 if (access(CONFIG_SMBD_COMMAND, F_OK)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200690 error_setg(errp, "Could not find '%s', please install it",
691 CONFIG_SMBD_COMMAND);
Dunrong Huang927d8112012-07-06 14:04:43 +0800692 return -1;
693 }
694
695 if (access(exported_dir, R_OK | X_OK)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200696 error_setg(errp, "Error accessing shared directory '%s': %s",
697 exported_dir, strerror(errno));
Dunrong Huang927d8112012-07-06 14:04:43 +0800698 return -1;
699 }
700
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100701 s->smb_dir = g_dir_make_tmp("qemu-smb.XXXXXX", NULL);
702 if (!s->smb_dir) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200703 error_setg(errp, "Could not create samba server dir");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000704 return -1;
705 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100706 smb_conf = g_strdup_printf("%s/%s", s->smb_dir, "smb.conf");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000707
708 f = fopen(smb_conf, "w");
709 if (!f) {
710 slirp_smb_cleanup(s);
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200711 error_setg(errp,
712 "Could not create samba server configuration file '%s'",
713 smb_conf);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100714 g_free(smb_conf);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000715 return -1;
716 }
717 fprintf(f,
718 "[global]\n"
719 "private dir=%s\n"
Peter Wu7912d042014-11-03 11:52:10 +0100720 "interfaces=127.0.0.1\n"
721 "bind interfaces only=yes\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000722 "pid directory=%s\n"
723 "lock directory=%s\n"
Nikolaus Rath276eda52012-04-25 09:57:19 -0400724 "state directory=%s\n"
Peter Wu7912d042014-11-03 11:52:10 +0100725 "cache directory=%s\n"
Michael Bueschb87b8a82014-04-27 14:54:12 +0400726 "ncalrpc dir=%s/ncalrpc\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000727 "log file=%s/log.smbd\n"
728 "smb passwd file=%s/smbpasswd\n"
Michael Bueschc2804ee2013-11-01 12:23:49 +0100729 "security = user\n"
730 "map to guest = Bad User\n"
Peter Wu7912d042014-11-03 11:52:10 +0100731 "load printers = no\n"
732 "printing = bsd\n"
733 "disable spoolss = yes\n"
734 "usershare max shares = 0\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000735 "[qemu]\n"
736 "path=%s\n"
737 "read only=no\n"
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200738 "guest ok=yes\n"
739 "force user=%s\n",
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000740 s->smb_dir,
741 s->smb_dir,
742 s->smb_dir,
743 s->smb_dir,
744 s->smb_dir,
Nikolaus Rath276eda52012-04-25 09:57:19 -0400745 s->smb_dir,
Michael Bueschb87b8a82014-04-27 14:54:12 +0400746 s->smb_dir,
Peter Wu7912d042014-11-03 11:52:10 +0100747 s->smb_dir,
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200748 exported_dir,
749 passwd->pw_name
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000750 );
751 fclose(f);
752
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100753 smb_cmdline = g_strdup_printf("%s -l %s -s %s",
Michael Tokarev44d8d2b2014-10-25 00:29:50 +0400754 CONFIG_SMBD_COMMAND, s->smb_dir, smb_conf);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100755 g_free(smb_conf);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000756
Marc-André Lureau44b4ff22019-01-17 15:43:33 +0400757 if (slirp_add_exec(s->slirp, smb_cmdline, &vserver_addr, 139) < 0 ||
758 slirp_add_exec(s->slirp, smb_cmdline, &vserver_addr, 445) < 0) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000759 slirp_smb_cleanup(s);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100760 g_free(smb_cmdline);
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200761 error_setg(errp, "Conflicting/invalid smbserver address");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000762 return -1;
763 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100764 g_free(smb_cmdline);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000765 return 0;
766}
767
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000768#endif /* !defined(_WIN32) */
769
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000770static int guestfwd_can_read(void *opaque)
771{
772 struct GuestFwd *fwd = opaque;
773 return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port);
774}
775
776static void guestfwd_read(void *opaque, const uint8_t *buf, int size)
777{
778 struct GuestFwd *fwd = opaque;
779 slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size);
780}
781
Marc-André Lureau44b4ff22019-01-17 15:43:33 +0400782static int guestfwd_write(const void *buf, size_t len, void *chr)
783{
784 return qemu_chr_fe_write_all(chr, buf, len);
785}
786
Thomas Huthd18572d2018-08-22 15:43:30 +0200787static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000788{
789 struct in_addr server = { .s_addr = 0 };
790 struct GuestFwd *fwd;
791 const char *p;
792 char buf[128];
793 char *end;
794 int port;
795
796 p = config_str;
Thomas Huthd18572d2018-08-22 15:43:30 +0200797 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
798 goto fail_syntax;
799 }
800 if (strcmp(buf, "tcp") && buf[0] != '\0') {
801 goto fail_syntax;
802 }
803 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
804 goto fail_syntax;
805 }
806 if (buf[0] != '\0' && !inet_aton(buf, &server)) {
807 goto fail_syntax;
808 }
809 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
810 goto fail_syntax;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000811 }
812 port = strtol(buf, &end, 10);
813 if (*end != '\0' || port < 1 || port > 65535) {
814 goto fail_syntax;
815 }
816
Alexander Grafa9899992011-06-04 07:25:59 +0200817 snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000818
Marc-André Lureau36247302019-01-17 15:43:34 +0400819 if (g_str_has_prefix(p, "cmd:")) {
Marc-André Lureau44b4ff22019-01-17 15:43:33 +0400820 if (slirp_add_exec(s->slirp, &p[4], &server, port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200821 error_setg(errp, "Conflicting/invalid host:port in guest "
822 "forwarding rule '%s'", config_str);
Alexander Grafb412eb62012-06-03 09:45:01 +0200823 return -1;
824 }
825 } else {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300826 Error *err = NULL;
Marc-André Lureau95e30b22018-08-22 19:19:42 +0200827 /*
828 * FIXME: sure we want to support implicit
829 * muxed monitors here?
830 */
831 Chardev *chr = qemu_chr_new_mux_mon(buf, p);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300832
833 if (!chr) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200834 error_setg(errp, "Could not open guest forwarding device '%s'",
835 buf);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300836 return -1;
837 }
838
839 fwd = g_new(struct GuestFwd, 1);
840 qemu_chr_fe_init(&fwd->hd, chr, &err);
841 if (err) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200842 error_propagate(errp, err);
Marc-André Lureau8e207c32019-01-17 15:43:36 +0400843 object_unparent(OBJECT(chr));
Alexander Grafb412eb62012-06-03 09:45:01 +0200844 g_free(fwd);
845 return -1;
846 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000847
Marc-André Lureau44b4ff22019-01-17 15:43:33 +0400848 if (slirp_add_guestfwd(s->slirp, guestfwd_write, &fwd->hd,
849 &server, port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200850 error_setg(errp, "Conflicting/invalid host:port in guest "
851 "forwarding rule '%s'", config_str);
Marc-André Lureau8e207c32019-01-17 15:43:36 +0400852 qemu_chr_fe_deinit(&fwd->hd, true);
Alexander Grafb412eb62012-06-03 09:45:01 +0200853 g_free(fwd);
854 return -1;
855 }
856 fwd->server = server;
857 fwd->port = port;
858 fwd->slirp = s->slirp;
859
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300860 qemu_chr_fe_set_handlers(&fwd->hd, guestfwd_can_read, guestfwd_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +0300861 NULL, NULL, fwd, NULL, true);
Marc-André Lureau8d45a3b2019-01-17 15:43:35 +0400862 s->fwd = g_slist_append(s->fwd, fwd);
Alexander Grafb412eb62012-06-03 09:45:01 +0200863 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000864 return 0;
865
866 fail_syntax:
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200867 error_setg(errp, "Invalid guest forwarding rule '%s'", config_str);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000868 return -1;
869}
870
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100871void hmp_info_usernet(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000872{
873 SlirpState *s;
874
875 QTAILQ_FOREACH(s, &slirp_stacks, entry) {
Stefan Hajnoczi90d87a32012-07-24 16:35:06 +0100876 int id;
Thomas Huth442da402018-04-30 20:02:24 +0200877 bool got_hub_id = net_hub_id_for_client(&s->nc, &id) == 0;
Marc-André Lureaub7f43bf2018-11-10 17:45:43 +0400878 char *info = slirp_connection_info(s->slirp);
879 monitor_printf(mon, "Hub %d (%s):\n%s",
Thomas Huth442da402018-04-30 20:02:24 +0200880 got_hub_id ? id : -1,
Marc-André Lureaub7f43bf2018-11-10 17:45:43 +0400881 s->nc.name, info);
882 g_free(info);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000883 }
884}
885
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200886static void
887net_init_slirp_configs(const StringList *fwd, int flags)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000888{
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200889 while (fwd) {
890 struct slirp_config_str *config;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000891
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200892 config = g_malloc0(sizeof(*config));
893 pstrcpy(config->str, sizeof(config->str), fwd->value->str);
894 config->flags = flags;
895 config->next = slirp_configs;
896 slirp_configs = config;
897
898 fwd = fwd->next;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000899 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000900}
901
Klaus Stengel63d29602012-10-27 19:53:39 +0200902static const char **slirp_dnssearch(const StringList *dnsname)
903{
904 const StringList *c = dnsname;
905 size_t i = 0, num_opts = 0;
906 const char **ret;
907
908 while (c) {
909 num_opts++;
910 c = c->next;
911 }
912
913 if (num_opts == 0) {
914 return NULL;
915 }
916
917 ret = g_malloc((num_opts + 1) * sizeof(*ret));
918 c = dnsname;
919 while (c) {
920 ret[i++] = c->value->str;
921 c = c->next;
922 }
923 ret[i] = NULL;
924 return ret;
925}
926
Kővágó, Zoltáncebea512016-07-13 21:50:12 -0600927int net_init_slirp(const Netdev *netdev, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200928 NetClientState *peer, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000929{
930 struct slirp_config_str *config;
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200931 char *vnet;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000932 int ret;
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200933 const NetdevUserOptions *user;
Klaus Stengel63d29602012-10-27 19:53:39 +0200934 const char **dnssearch;
Samuel Thibault0b11c032016-03-20 12:29:54 +0100935 bool ipv4 = true, ipv6 = true;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000936
Eric Blakef394b2e2016-07-13 21:50:23 -0600937 assert(netdev->type == NET_CLIENT_DRIVER_USER);
938 user = &netdev->u.user;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000939
Samuel Thibault0b11c032016-03-20 12:29:54 +0100940 if ((user->has_ipv6 && user->ipv6 && !user->has_ipv4) ||
941 (user->has_ipv4 && !user->ipv4)) {
942 ipv4 = 0;
943 }
944 if ((user->has_ipv4 && user->ipv4 && !user->has_ipv6) ||
945 (user->has_ipv6 && !user->ipv6)) {
946 ipv6 = 0;
947 }
948
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200949 vnet = user->has_net ? g_strdup(user->net) :
950 user->has_ip ? g_strdup_printf("%s/24", user->ip) :
951 NULL;
Jan Kiszkac54ed5b2011-07-20 12:20:14 +0200952
Klaus Stengel63d29602012-10-27 19:53:39 +0200953 dnssearch = slirp_dnssearch(user->dnssearch);
954
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200955 /* all optional fields are initialized to "all bits zero" */
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000956
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200957 net_init_slirp_configs(user->hostfwd, SLIRP_CFG_HOSTFWD);
958 net_init_slirp_configs(user->guestfwd, 0);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000959
Samuel Thibault0b11c032016-03-20 12:29:54 +0100960 ret = net_slirp_init(peer, "user", name, user->q_restrict,
961 ipv4, vnet, user->host,
962 ipv6, user->ipv6_prefix, user->ipv6_prefixlen,
Samuel Thibaultd8eb3862016-03-25 00:02:58 +0100963 user->ipv6_host, user->hostname, user->tftp,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100964 user->bootfile, user->dhcpstart,
Samuel Thibaultd8eb3862016-03-25 00:02:58 +0100965 user->dns, user->ipv6_dns, user->smb,
Fam Zheng0fca92b2018-09-14 15:26:16 +0800966 user->smbserver, dnssearch, user->domainname,
967 user->tftp_server_name, errp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000968
969 while (slirp_configs) {
970 config = slirp_configs;
971 slirp_configs = config->next;
Anthony Liguori7267c092011-08-20 22:09:37 -0500972 g_free(config);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000973 }
974
Anthony Liguori7267c092011-08-20 22:09:37 -0500975 g_free(vnet);
Klaus Stengel63d29602012-10-27 19:53:39 +0200976 g_free(dnssearch);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000977
978 return ret;
979}