blob: ec07f662c00f897604557c203e480e8496d5aa2b [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
78typedef struct SlirpState {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010079 NetClientState nc;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000080 QTAILQ_ENTRY(SlirpState) entry;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000081 Slirp *slirp;
Paolo Bonzinif6c2e662016-07-12 09:57:12 +020082 Notifier exit_notifier;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000083#ifndef _WIN32
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +010084 gchar *smb_dir;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000085#endif
86} SlirpState;
87
88static struct slirp_config_str *slirp_configs;
Paolo Bonzinib58deb32018-12-06 11:58:10 +010089static QTAILQ_HEAD(, SlirpState) slirp_stacks =
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000090 QTAILQ_HEAD_INITIALIZER(slirp_stacks);
91
Thomas Huthd18572d2018-08-22 15:43:30 +020092static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp);
93static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000094
95#ifndef _WIN32
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000096static int slirp_smb(SlirpState *s, const char *exported_dir,
Hervé Poussineau5c843af2017-07-15 18:43:50 +020097 struct in_addr vserver_addr, Error **errp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000098static void slirp_smb_cleanup(SlirpState *s);
99#else
100static inline void slirp_smb_cleanup(SlirpState *s) { }
101#endif
102
Marc-André Lureau62c1d2c2018-11-10 17:45:36 +0400103static void net_slirp_output(void *opaque, const uint8_t *pkt, int pkt_len)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000104{
105 SlirpState *s = opaque;
106
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000107 qemu_send_packet(&s->nc, pkt, pkt_len);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000108}
109
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100110static ssize_t net_slirp_receive(NetClientState *nc, const uint8_t *buf, size_t size)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000111{
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000112 SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000113
114 slirp_input(s->slirp, buf, size);
115
116 return size;
117}
118
Paolo Bonzinif6c2e662016-07-12 09:57:12 +0200119static void slirp_smb_exit(Notifier *n, void *data)
120{
121 SlirpState *s = container_of(n, SlirpState, exit_notifier);
122 slirp_smb_cleanup(s);
123}
124
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100125static void net_slirp_cleanup(NetClientState *nc)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000126{
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000127 SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000128
129 slirp_cleanup(s->slirp);
Marc-André Lureau67f32802016-08-18 17:44:05 +0400130 if (s->exit_notifier.notify) {
131 qemu_remove_exit_notifier(&s->exit_notifier);
132 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000133 slirp_smb_cleanup(s);
134 QTAILQ_REMOVE(&slirp_stacks, s, entry);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000135}
136
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000137static NetClientInfo net_slirp_info = {
Eric Blakef394b2e2016-07-13 21:50:23 -0600138 .type = NET_CLIENT_DRIVER_USER,
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000139 .size = sizeof(SlirpState),
140 .receive = net_slirp_receive,
141 .cleanup = net_slirp_cleanup,
142};
143
Marc-André Lureau2addc8f2018-11-14 16:36:33 +0400144static void net_slirp_guest_error(const char *msg)
145{
146 qemu_log_mask(LOG_GUEST_ERROR, "%s", msg);
147}
148
Marc-André Lureaue6dbff32018-11-22 02:06:28 +0400149static int64_t net_slirp_clock_get_ns(void)
150{
151 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
152}
153
Marc-André Lureaud846b922018-11-14 16:36:07 +0400154static const SlirpCb slirp_cb = {
155 .output = net_slirp_output,
Marc-André Lureau2addc8f2018-11-14 16:36:33 +0400156 .guest_error = net_slirp_guest_error,
Marc-André Lureaue6dbff32018-11-22 02:06:28 +0400157 .clock_get_ns = net_slirp_clock_get_ns,
Marc-André Lureaud846b922018-11-14 16:36:07 +0400158};
159
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100160static int net_slirp_init(NetClientState *peer, const char *model,
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000161 const char *name, int restricted,
Samuel Thibault0b11c032016-03-20 12:29:54 +0100162 bool ipv4, const char *vnetwork, const char *vhost,
163 bool ipv6, const char *vprefix6, int vprefix6_len,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100164 const char *vhost6,
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000165 const char *vhostname, const char *tftp_export,
166 const char *bootfile, const char *vdhcp_start,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100167 const char *vnameserver, const char *vnameserver6,
168 const char *smb_export, const char *vsmbserver,
Benjamin Drungf18d1372018-02-27 17:06:01 +0100169 const char **dnssearch, const char *vdomainname,
Fam Zheng0fca92b2018-09-14 15:26:16 +0800170 const char *tftp_server_name,
Benjamin Drungf18d1372018-02-27 17:06:01 +0100171 Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000172{
173 /* default settings according to historic slirp */
174 struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */
175 struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */
176 struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */
177 struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */
178 struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */
Yann Bordenave7aac5312016-03-15 10:31:22 +0100179 struct in6_addr ip6_prefix;
180 struct in6_addr ip6_host;
181 struct in6_addr ip6_dns;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000182#ifndef _WIN32
183 struct in_addr smbsrv = { .s_addr = 0 };
184#endif
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100185 NetClientState *nc;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000186 SlirpState *s;
187 char buf[20];
188 uint32_t addr;
189 int shift;
190 char *end;
191 struct slirp_config_str *config;
192
Samuel Thibault0b11c032016-03-20 12:29:54 +0100193 if (!ipv4 && (vnetwork || vhost || vnameserver)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200194 error_setg(errp, "IPv4 disabled but netmask/host/dns provided");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100195 return -1;
196 }
197
198 if (!ipv6 && (vprefix6 || vhost6 || vnameserver6)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200199 error_setg(errp, "IPv6 disabled but prefix/host6/dns6 provided");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100200 return -1;
201 }
202
203 if (!ipv4 && !ipv6) {
204 /* It doesn't make sense to disable both */
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200205 error_setg(errp, "IPv4 and IPv6 disabled");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100206 return -1;
207 }
208
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000209 if (vnetwork) {
210 if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) {
211 if (!inet_aton(vnetwork, &net)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200212 error_setg(errp, "Failed to parse netmask");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000213 return -1;
214 }
215 addr = ntohl(net.s_addr);
216 if (!(addr & 0x80000000)) {
217 mask.s_addr = htonl(0xff000000); /* class A */
218 } else if ((addr & 0xfff00000) == 0xac100000) {
219 mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */
220 } else if ((addr & 0xc0000000) == 0x80000000) {
221 mask.s_addr = htonl(0xffff0000); /* class B */
222 } else if ((addr & 0xffff0000) == 0xc0a80000) {
223 mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */
224 } else if ((addr & 0xffff0000) == 0xc6120000) {
225 mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */
226 } else if ((addr & 0xe0000000) == 0xe0000000) {
227 mask.s_addr = htonl(0xffffff00); /* class C */
228 } else {
229 mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */
230 }
231 } else {
232 if (!inet_aton(buf, &net)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200233 error_setg(errp, "Failed to parse netmask");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000234 return -1;
235 }
236 shift = strtol(vnetwork, &end, 10);
237 if (*end != '\0') {
238 if (!inet_aton(vnetwork, &mask)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200239 error_setg(errp,
240 "Failed to parse netmask (trailing chars)");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000241 return -1;
242 }
243 } else if (shift < 4 || shift > 32) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200244 error_setg(errp,
245 "Invalid netmask provided (must be in range 4-32)");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000246 return -1;
247 } else {
248 mask.s_addr = htonl(0xffffffff << (32 - shift));
249 }
250 }
251 net.s_addr &= mask.s_addr;
252 host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr);
253 dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr);
254 dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr);
255 }
256
257 if (vhost && !inet_aton(vhost, &host)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200258 error_setg(errp, "Failed to parse host");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000259 return -1;
260 }
261 if ((host.s_addr & mask.s_addr) != net.s_addr) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200262 error_setg(errp, "Host doesn't belong to network");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000263 return -1;
264 }
265
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000266 if (vnameserver && !inet_aton(vnameserver, &dns)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200267 error_setg(errp, "Failed to parse DNS");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000268 return -1;
269 }
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200270 if ((dns.s_addr & mask.s_addr) != net.s_addr) {
271 error_setg(errp, "DNS doesn't belong to network");
272 return -1;
273 }
274 if (dns.s_addr == host.s_addr) {
275 error_setg(errp, "DNS must be different from host");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000276 return -1;
277 }
278
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200279 if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200280 error_setg(errp, "Failed to parse DHCP start address");
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200281 return -1;
282 }
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200283 if ((dhcp.s_addr & mask.s_addr) != net.s_addr) {
284 error_setg(errp, "DHCP doesn't belong to network");
285 return -1;
286 }
287 if (dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) {
288 error_setg(errp, "DNS must be different from host and DNS");
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200289 return -1;
290 }
291
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000292#ifndef _WIN32
293 if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200294 error_setg(errp, "Failed to parse SMB address");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000295 return -1;
296 }
297#endif
298
Yann Bordenave7aac5312016-03-15 10:31:22 +0100299 if (!vprefix6) {
300 vprefix6 = "fec0::";
301 }
302 if (!inet_pton(AF_INET6, vprefix6, &ip6_prefix)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200303 error_setg(errp, "Failed to parse IPv6 prefix");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100304 return -1;
305 }
Yann Bordenave7aac5312016-03-15 10:31:22 +0100306
307 if (!vprefix6_len) {
308 vprefix6_len = 64;
309 }
310 if (vprefix6_len < 0 || vprefix6_len > 126) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200311 error_setg(errp,
312 "Invalid prefix provided (prefix len must be in range 0-126");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100313 return -1;
314 }
315
316 if (vhost6) {
Yann Bordenave7aac5312016-03-15 10:31:22 +0100317 if (!inet_pton(AF_INET6, vhost6, &ip6_host)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200318 error_setg(errp, "Failed to parse IPv6 host");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100319 return -1;
320 }
321 if (!in6_equal_net(&ip6_prefix, &ip6_host, vprefix6_len)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200322 error_setg(errp, "IPv6 Host doesn't belong to network");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100323 return -1;
324 }
Yann Bordenave7aac5312016-03-15 10:31:22 +0100325 } else {
326 ip6_host = ip6_prefix;
327 ip6_host.s6_addr[15] |= 2;
328 }
329
330 if (vnameserver6) {
Yann Bordenave7aac5312016-03-15 10:31:22 +0100331 if (!inet_pton(AF_INET6, vnameserver6, &ip6_dns)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200332 error_setg(errp, "Failed to parse IPv6 DNS");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100333 return -1;
334 }
335 if (!in6_equal_net(&ip6_prefix, &ip6_dns, vprefix6_len)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200336 error_setg(errp, "IPv6 DNS doesn't belong to network");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100337 return -1;
338 }
Yann Bordenave7aac5312016-03-15 10:31:22 +0100339 } else {
340 ip6_dns = ip6_prefix;
341 ip6_dns.s6_addr[15] |= 3;
342 }
343
Benjamin Drungf18d1372018-02-27 17:06:01 +0100344 if (vdomainname && !*vdomainname) {
345 error_setg(errp, "'domainname' parameter cannot be empty");
346 return -1;
347 }
348
Fam Zheng6e157a02018-09-14 15:26:15 +0800349 if (vdomainname && strlen(vdomainname) > 255) {
350 error_setg(errp, "'domainname' parameter cannot exceed 255 bytes");
351 return -1;
352 }
353
354 if (vhostname && strlen(vhostname) > 255) {
355 error_setg(errp, "'vhostname' parameter cannot exceed 255 bytes");
356 return -1;
357 }
Yann Bordenave7aac5312016-03-15 10:31:22 +0100358
Fam Zheng0fca92b2018-09-14 15:26:16 +0800359 if (tftp_server_name && strlen(tftp_server_name) > 255) {
360 error_setg(errp, "'tftp-server-name' parameter cannot exceed 255 bytes");
361 return -1;
362 }
363
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100364 nc = qemu_new_net_client(&net_slirp_info, peer, model, name);
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000365
366 snprintf(nc->info_str, sizeof(nc->info_str),
Jan Kiszkac54ed5b2011-07-20 12:20:14 +0200367 "net=%s,restrict=%s", inet_ntoa(net),
368 restricted ? "on" : "off");
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000369
370 s = DO_UPCAST(SlirpState, nc, nc);
371
Samuel Thibault0b11c032016-03-20 12:29:54 +0100372 s->slirp = slirp_init(restricted, ipv4, net, mask, host,
373 ipv6, ip6_prefix, vprefix6_len, ip6_host,
Fam Zheng0fca92b2018-09-14 15:26:16 +0800374 vhostname, tftp_server_name,
375 tftp_export, bootfile, dhcp,
Marc-André Lureau62c1d2c2018-11-10 17:45:36 +0400376 dns, ip6_dns, dnssearch, vdomainname,
Marc-André Lureaud846b922018-11-14 16:36:07 +0400377 &slirp_cb, s);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000378 QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry);
379
380 for (config = slirp_configs; config; config = config->next) {
381 if (config->flags & SLIRP_CFG_HOSTFWD) {
Thomas Huthd18572d2018-08-22 15:43:30 +0200382 if (slirp_hostfwd(s, config->str, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000383 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200384 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000385 } else {
Thomas Huthd18572d2018-08-22 15:43:30 +0200386 if (slirp_guestfwd(s, config->str, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000387 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200388 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000389 }
390 }
391#ifndef _WIN32
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000392 if (smb_export) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200393 if (slirp_smb(s, smb_export, smbsrv, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000394 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200395 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000396 }
397#endif
398
Paolo Bonzinif6c2e662016-07-12 09:57:12 +0200399 s->exit_notifier.notify = slirp_smb_exit;
400 qemu_add_exit_notifier(&s->exit_notifier);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000401 return 0;
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000402
403error:
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100404 qemu_del_net_client(nc);
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000405 return -1;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000406}
407
Thomas Huth93653062018-01-11 21:02:40 +0100408static SlirpState *slirp_lookup(Monitor *mon, const char *hub_id,
409 const char *name)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000410{
Thomas Huth93653062018-01-11 21:02:40 +0100411 if (name) {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100412 NetClientState *nc;
Thomas Huth93653062018-01-11 21:02:40 +0100413 if (hub_id) {
414 nc = net_hub_find_client_by_name(strtol(hub_id, NULL, 0), name);
415 if (!nc) {
Thomas Huth442da402018-04-30 20:02:24 +0200416 monitor_printf(mon, "unrecognized (hub-id, stackname) pair\n");
Thomas Huth93653062018-01-11 21:02:40 +0100417 return NULL;
418 }
Thomas Huth68cb29e2018-09-20 10:22:27 +0200419 warn_report("Using 'hub-id' is deprecated, specify the netdev id "
420 "directly instead");
Thomas Huth93653062018-01-11 21:02:40 +0100421 } else {
422 nc = qemu_find_netdev(name);
423 if (!nc) {
424 monitor_printf(mon, "unrecognized netdev id '%s'\n", name);
425 return NULL;
426 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000427 }
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000428 if (strcmp(nc->model, "user")) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000429 monitor_printf(mon, "invalid device specified\n");
430 return NULL;
431 }
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000432 return DO_UPCAST(SlirpState, nc, nc);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000433 } else {
434 if (QTAILQ_EMPTY(&slirp_stacks)) {
435 monitor_printf(mon, "user mode network stack not in use\n");
436 return NULL;
437 }
438 return QTAILQ_FIRST(&slirp_stacks);
439 }
440}
441
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100442void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000443{
444 struct in_addr host_addr = { .s_addr = INADDR_ANY };
445 int host_port;
Markus Armbrustere30e5eb2011-11-16 15:45:59 +0100446 char buf[256];
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000447 const char *src_str, *p;
448 SlirpState *s;
449 int is_udp = 0;
450 int err;
451 const char *arg1 = qdict_get_str(qdict, "arg1");
452 const char *arg2 = qdict_get_try_str(qdict, "arg2");
453 const char *arg3 = qdict_get_try_str(qdict, "arg3");
454
Thomas Huth93653062018-01-11 21:02:40 +0100455 if (arg3) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000456 s = slirp_lookup(mon, arg1, arg2);
457 src_str = arg3;
Thomas Huth93653062018-01-11 21:02:40 +0100458 } else if (arg2) {
459 s = slirp_lookup(mon, NULL, arg1);
460 src_str = arg2;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000461 } else {
462 s = slirp_lookup(mon, NULL, NULL);
463 src_str = arg1;
464 }
465 if (!s) {
466 return;
467 }
468
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000469 p = src_str;
Markus Armbrustere30e5eb2011-11-16 15:45:59 +0100470 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
471 goto fail_syntax;
472 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000473
474 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
475 is_udp = 0;
476 } else if (!strcmp(buf, "udp")) {
477 is_udp = 1;
478 } else {
479 goto fail_syntax;
480 }
481
482 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
483 goto fail_syntax;
484 }
485 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
486 goto fail_syntax;
487 }
488
Nia Alarie1fb3f7f2018-03-16 14:39:21 +0000489 if (qemu_strtoi(p, NULL, 10, &host_port)) {
490 goto fail_syntax;
491 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000492
Peter Maydell70381662014-06-16 16:47:49 +0100493 err = slirp_remove_hostfwd(s->slirp, is_udp, host_addr, host_port);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000494
495 monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
Geoffrey Thomasb15ba6c2011-12-17 04:23:59 -0500496 err ? "not found" : "removed");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000497 return;
498
499 fail_syntax:
500 monitor_printf(mon, "invalid format\n");
501}
502
Thomas Huthd18572d2018-08-22 15:43:30 +0200503static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000504{
505 struct in_addr host_addr = { .s_addr = INADDR_ANY };
506 struct in_addr guest_addr = { .s_addr = 0 };
507 int host_port, guest_port;
508 const char *p;
509 char buf[256];
510 int is_udp;
511 char *end;
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100512 const char *fail_reason = "Unknown reason";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000513
514 p = redir_str;
515 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100516 fail_reason = "No : separators";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000517 goto fail_syntax;
518 }
519 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
520 is_udp = 0;
521 } else if (!strcmp(buf, "udp")) {
522 is_udp = 1;
523 } else {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100524 fail_reason = "Bad protocol name";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000525 goto fail_syntax;
526 }
527
Thomas Huthd18572d2018-08-22 15:43:30 +0200528 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
529 fail_reason = "Missing : separator";
530 goto fail_syntax;
531 }
532 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
533 fail_reason = "Bad host address";
534 goto fail_syntax;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000535 }
536
Thomas Huthd18572d2018-08-22 15:43:30 +0200537 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100538 fail_reason = "Bad host port separator";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000539 goto fail_syntax;
540 }
541 host_port = strtol(buf, &end, 0);
Vincent Bernat0bed71e2017-02-25 22:31:58 +0100542 if (*end != '\0' || host_port < 0 || host_port > 65535) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100543 fail_reason = "Bad host port";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000544 goto fail_syntax;
545 }
546
547 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100548 fail_reason = "Missing guest address";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000549 goto fail_syntax;
550 }
551 if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100552 fail_reason = "Bad guest address";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000553 goto fail_syntax;
554 }
555
556 guest_port = strtol(p, &end, 0);
557 if (*end != '\0' || guest_port < 1 || guest_port > 65535) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100558 fail_reason = "Bad guest port";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000559 goto fail_syntax;
560 }
561
562 if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr,
563 guest_port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200564 error_setg(errp, "Could not set up host forwarding rule '%s'",
565 redir_str);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000566 return -1;
567 }
568 return 0;
569
570 fail_syntax:
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100571 error_setg(errp, "Invalid host forwarding rule '%s' (%s)", redir_str,
572 fail_reason);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000573 return -1;
574}
575
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100576void hmp_hostfwd_add(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000577{
578 const char *redir_str;
579 SlirpState *s;
580 const char *arg1 = qdict_get_str(qdict, "arg1");
581 const char *arg2 = qdict_get_try_str(qdict, "arg2");
582 const char *arg3 = qdict_get_try_str(qdict, "arg3");
583
Thomas Huth93653062018-01-11 21:02:40 +0100584 if (arg3) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000585 s = slirp_lookup(mon, arg1, arg2);
586 redir_str = arg3;
Thomas Huth93653062018-01-11 21:02:40 +0100587 } else if (arg2) {
588 s = slirp_lookup(mon, NULL, arg1);
589 redir_str = arg2;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000590 } else {
591 s = slirp_lookup(mon, NULL, NULL);
592 redir_str = arg1;
593 }
594 if (s) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200595 Error *err = NULL;
Thomas Huthd18572d2018-08-22 15:43:30 +0200596 if (slirp_hostfwd(s, redir_str, &err) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200597 error_report_err(err);
598 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000599 }
600
601}
602
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000603#ifndef _WIN32
604
605/* automatic user mode samba server configuration */
606static void slirp_smb_cleanup(SlirpState *s)
607{
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100608 int ret;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000609
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100610 if (s->smb_dir) {
611 gchar *cmd = g_strdup_printf("rm -rf %s", s->smb_dir);
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100612 ret = system(cmd);
Juan Quintela24ac07d2010-03-04 10:00:31 +0100613 if (ret == -1 || !WIFEXITED(ret)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100614 error_report("'%s' failed.", cmd);
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100615 } else if (WEXITSTATUS(ret)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100616 error_report("'%s' failed. Error code: %d",
617 cmd, WEXITSTATUS(ret));
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100618 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100619 g_free(cmd);
620 g_free(s->smb_dir);
621 s->smb_dir = NULL;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000622 }
623}
624
625static int slirp_smb(SlirpState* s, const char *exported_dir,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200626 struct in_addr vserver_addr, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000627{
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100628 char *smb_conf;
629 char *smb_cmdline;
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200630 struct passwd *passwd;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000631 FILE *f;
632
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200633 passwd = getpwuid(geteuid());
634 if (!passwd) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200635 error_setg(errp, "Failed to retrieve user name");
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200636 return -1;
637 }
638
Dunrong Huang927d8112012-07-06 14:04:43 +0800639 if (access(CONFIG_SMBD_COMMAND, F_OK)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200640 error_setg(errp, "Could not find '%s', please install it",
641 CONFIG_SMBD_COMMAND);
Dunrong Huang927d8112012-07-06 14:04:43 +0800642 return -1;
643 }
644
645 if (access(exported_dir, R_OK | X_OK)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200646 error_setg(errp, "Error accessing shared directory '%s': %s",
647 exported_dir, strerror(errno));
Dunrong Huang927d8112012-07-06 14:04:43 +0800648 return -1;
649 }
650
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100651 s->smb_dir = g_dir_make_tmp("qemu-smb.XXXXXX", NULL);
652 if (!s->smb_dir) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200653 error_setg(errp, "Could not create samba server dir");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000654 return -1;
655 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100656 smb_conf = g_strdup_printf("%s/%s", s->smb_dir, "smb.conf");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000657
658 f = fopen(smb_conf, "w");
659 if (!f) {
660 slirp_smb_cleanup(s);
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200661 error_setg(errp,
662 "Could not create samba server configuration file '%s'",
663 smb_conf);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100664 g_free(smb_conf);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000665 return -1;
666 }
667 fprintf(f,
668 "[global]\n"
669 "private dir=%s\n"
Peter Wu7912d042014-11-03 11:52:10 +0100670 "interfaces=127.0.0.1\n"
671 "bind interfaces only=yes\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000672 "pid directory=%s\n"
673 "lock directory=%s\n"
Nikolaus Rath276eda52012-04-25 09:57:19 -0400674 "state directory=%s\n"
Peter Wu7912d042014-11-03 11:52:10 +0100675 "cache directory=%s\n"
Michael Bueschb87b8a82014-04-27 14:54:12 +0400676 "ncalrpc dir=%s/ncalrpc\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000677 "log file=%s/log.smbd\n"
678 "smb passwd file=%s/smbpasswd\n"
Michael Bueschc2804ee2013-11-01 12:23:49 +0100679 "security = user\n"
680 "map to guest = Bad User\n"
Peter Wu7912d042014-11-03 11:52:10 +0100681 "load printers = no\n"
682 "printing = bsd\n"
683 "disable spoolss = yes\n"
684 "usershare max shares = 0\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000685 "[qemu]\n"
686 "path=%s\n"
687 "read only=no\n"
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200688 "guest ok=yes\n"
689 "force user=%s\n",
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000690 s->smb_dir,
691 s->smb_dir,
692 s->smb_dir,
693 s->smb_dir,
694 s->smb_dir,
Nikolaus Rath276eda52012-04-25 09:57:19 -0400695 s->smb_dir,
Michael Bueschb87b8a82014-04-27 14:54:12 +0400696 s->smb_dir,
Peter Wu7912d042014-11-03 11:52:10 +0100697 s->smb_dir,
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200698 exported_dir,
699 passwd->pw_name
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000700 );
701 fclose(f);
702
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100703 smb_cmdline = g_strdup_printf("%s -l %s -s %s",
Michael Tokarev44d8d2b2014-10-25 00:29:50 +0400704 CONFIG_SMBD_COMMAND, s->smb_dir, smb_conf);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100705 g_free(smb_conf);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000706
Marc-André Lureau44b4ff22019-01-17 15:43:33 +0400707 if (slirp_add_exec(s->slirp, smb_cmdline, &vserver_addr, 139) < 0 ||
708 slirp_add_exec(s->slirp, smb_cmdline, &vserver_addr, 445) < 0) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000709 slirp_smb_cleanup(s);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100710 g_free(smb_cmdline);
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200711 error_setg(errp, "Conflicting/invalid smbserver address");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000712 return -1;
713 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100714 g_free(smb_cmdline);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000715 return 0;
716}
717
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000718#endif /* !defined(_WIN32) */
719
720struct GuestFwd {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300721 CharBackend hd;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000722 struct in_addr server;
723 int port;
724 Slirp *slirp;
725};
726
727static int guestfwd_can_read(void *opaque)
728{
729 struct GuestFwd *fwd = opaque;
730 return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port);
731}
732
733static void guestfwd_read(void *opaque, const uint8_t *buf, int size)
734{
735 struct GuestFwd *fwd = opaque;
736 slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size);
737}
738
Marc-André Lureau44b4ff22019-01-17 15:43:33 +0400739static int guestfwd_write(const void *buf, size_t len, void *chr)
740{
741 return qemu_chr_fe_write_all(chr, buf, len);
742}
743
Thomas Huthd18572d2018-08-22 15:43:30 +0200744static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000745{
746 struct in_addr server = { .s_addr = 0 };
747 struct GuestFwd *fwd;
748 const char *p;
749 char buf[128];
750 char *end;
751 int port;
752
753 p = config_str;
Thomas Huthd18572d2018-08-22 15:43:30 +0200754 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
755 goto fail_syntax;
756 }
757 if (strcmp(buf, "tcp") && buf[0] != '\0') {
758 goto fail_syntax;
759 }
760 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
761 goto fail_syntax;
762 }
763 if (buf[0] != '\0' && !inet_aton(buf, &server)) {
764 goto fail_syntax;
765 }
766 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
767 goto fail_syntax;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000768 }
769 port = strtol(buf, &end, 10);
770 if (*end != '\0' || port < 1 || port > 65535) {
771 goto fail_syntax;
772 }
773
Alexander Grafa9899992011-06-04 07:25:59 +0200774 snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000775
Alexander Grafb412eb62012-06-03 09:45:01 +0200776 if ((strlen(p) > 4) && !strncmp(p, "cmd:", 4)) {
Marc-André Lureau44b4ff22019-01-17 15:43:33 +0400777 if (slirp_add_exec(s->slirp, &p[4], &server, port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200778 error_setg(errp, "Conflicting/invalid host:port in guest "
779 "forwarding rule '%s'", config_str);
Alexander Grafb412eb62012-06-03 09:45:01 +0200780 return -1;
781 }
782 } else {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300783 Error *err = NULL;
Marc-André Lureau95e30b22018-08-22 19:19:42 +0200784 /*
785 * FIXME: sure we want to support implicit
786 * muxed monitors here?
787 */
788 Chardev *chr = qemu_chr_new_mux_mon(buf, p);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300789
790 if (!chr) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200791 error_setg(errp, "Could not open guest forwarding device '%s'",
792 buf);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300793 return -1;
794 }
795
796 fwd = g_new(struct GuestFwd, 1);
797 qemu_chr_fe_init(&fwd->hd, chr, &err);
798 if (err) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200799 error_propagate(errp, err);
Alexander Grafb412eb62012-06-03 09:45:01 +0200800 g_free(fwd);
801 return -1;
802 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000803
Marc-André Lureau44b4ff22019-01-17 15:43:33 +0400804 if (slirp_add_guestfwd(s->slirp, guestfwd_write, &fwd->hd,
805 &server, port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200806 error_setg(errp, "Conflicting/invalid host:port in guest "
807 "forwarding rule '%s'", config_str);
Alexander Grafb412eb62012-06-03 09:45:01 +0200808 g_free(fwd);
809 return -1;
810 }
811 fwd->server = server;
812 fwd->port = port;
813 fwd->slirp = s->slirp;
814
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300815 qemu_chr_fe_set_handlers(&fwd->hd, guestfwd_can_read, guestfwd_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +0300816 NULL, NULL, fwd, NULL, true);
Alexander Grafb412eb62012-06-03 09:45:01 +0200817 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000818 return 0;
819
820 fail_syntax:
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200821 error_setg(errp, "Invalid guest forwarding rule '%s'", config_str);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000822 return -1;
823}
824
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100825void hmp_info_usernet(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000826{
827 SlirpState *s;
828
829 QTAILQ_FOREACH(s, &slirp_stacks, entry) {
Stefan Hajnoczi90d87a32012-07-24 16:35:06 +0100830 int id;
Thomas Huth442da402018-04-30 20:02:24 +0200831 bool got_hub_id = net_hub_id_for_client(&s->nc, &id) == 0;
Marc-André Lureaub7f43bf2018-11-10 17:45:43 +0400832 char *info = slirp_connection_info(s->slirp);
833 monitor_printf(mon, "Hub %d (%s):\n%s",
Thomas Huth442da402018-04-30 20:02:24 +0200834 got_hub_id ? id : -1,
Marc-André Lureaub7f43bf2018-11-10 17:45:43 +0400835 s->nc.name, info);
836 g_free(info);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000837 }
838}
839
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200840static void
841net_init_slirp_configs(const StringList *fwd, int flags)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000842{
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200843 while (fwd) {
844 struct slirp_config_str *config;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000845
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200846 config = g_malloc0(sizeof(*config));
847 pstrcpy(config->str, sizeof(config->str), fwd->value->str);
848 config->flags = flags;
849 config->next = slirp_configs;
850 slirp_configs = config;
851
852 fwd = fwd->next;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000853 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000854}
855
Klaus Stengel63d29602012-10-27 19:53:39 +0200856static const char **slirp_dnssearch(const StringList *dnsname)
857{
858 const StringList *c = dnsname;
859 size_t i = 0, num_opts = 0;
860 const char **ret;
861
862 while (c) {
863 num_opts++;
864 c = c->next;
865 }
866
867 if (num_opts == 0) {
868 return NULL;
869 }
870
871 ret = g_malloc((num_opts + 1) * sizeof(*ret));
872 c = dnsname;
873 while (c) {
874 ret[i++] = c->value->str;
875 c = c->next;
876 }
877 ret[i] = NULL;
878 return ret;
879}
880
Kővágó, Zoltáncebea512016-07-13 21:50:12 -0600881int net_init_slirp(const Netdev *netdev, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200882 NetClientState *peer, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000883{
884 struct slirp_config_str *config;
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200885 char *vnet;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000886 int ret;
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200887 const NetdevUserOptions *user;
Klaus Stengel63d29602012-10-27 19:53:39 +0200888 const char **dnssearch;
Samuel Thibault0b11c032016-03-20 12:29:54 +0100889 bool ipv4 = true, ipv6 = true;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000890
Eric Blakef394b2e2016-07-13 21:50:23 -0600891 assert(netdev->type == NET_CLIENT_DRIVER_USER);
892 user = &netdev->u.user;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000893
Samuel Thibault0b11c032016-03-20 12:29:54 +0100894 if ((user->has_ipv6 && user->ipv6 && !user->has_ipv4) ||
895 (user->has_ipv4 && !user->ipv4)) {
896 ipv4 = 0;
897 }
898 if ((user->has_ipv4 && user->ipv4 && !user->has_ipv6) ||
899 (user->has_ipv6 && !user->ipv6)) {
900 ipv6 = 0;
901 }
902
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200903 vnet = user->has_net ? g_strdup(user->net) :
904 user->has_ip ? g_strdup_printf("%s/24", user->ip) :
905 NULL;
Jan Kiszkac54ed5b2011-07-20 12:20:14 +0200906
Klaus Stengel63d29602012-10-27 19:53:39 +0200907 dnssearch = slirp_dnssearch(user->dnssearch);
908
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200909 /* all optional fields are initialized to "all bits zero" */
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000910
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200911 net_init_slirp_configs(user->hostfwd, SLIRP_CFG_HOSTFWD);
912 net_init_slirp_configs(user->guestfwd, 0);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000913
Samuel Thibault0b11c032016-03-20 12:29:54 +0100914 ret = net_slirp_init(peer, "user", name, user->q_restrict,
915 ipv4, vnet, user->host,
916 ipv6, user->ipv6_prefix, user->ipv6_prefixlen,
Samuel Thibaultd8eb3862016-03-25 00:02:58 +0100917 user->ipv6_host, user->hostname, user->tftp,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100918 user->bootfile, user->dhcpstart,
Samuel Thibaultd8eb3862016-03-25 00:02:58 +0100919 user->dns, user->ipv6_dns, user->smb,
Fam Zheng0fca92b2018-09-14 15:26:16 +0800920 user->smbserver, dnssearch, user->domainname,
921 user->tftp_server_name, errp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000922
923 while (slirp_configs) {
924 config = slirp_configs;
925 slirp_configs = config->next;
Anthony Liguori7267c092011-08-20 22:09:37 -0500926 g_free(config);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000927 }
928
Anthony Liguori7267c092011-08-20 22:09:37 -0500929 g_free(vnet);
Klaus Stengel63d29602012-10-27 19:53:39 +0200930 g_free(dnssearch);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000931
932 return ret;
933}