blob: 031c324f02915f2ad2358984f6a7c05715168481 [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"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000026#include "net/slirp.h"
27
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000028
Blue Swirl28b150b2010-01-27 17:47:33 +000029#ifndef _WIN32
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +020030#include <pwd.h>
Blue Swirl28b150b2010-01-27 17:47:33 +000031#include <sys/wait.h>
32#endif
Paolo Bonzini1422e322012-10-24 08:43:34 +020033#include "net/net.h"
Paolo Bonzinia245fc12012-09-17 18:43:51 +020034#include "clients.h"
35#include "hub.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010036#include "monitor/monitor.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010037#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010038#include "qemu/sockets.h"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000039#include "slirp/libslirp.h"
Yann Bordenave7aac5312016-03-15 10:31:22 +010040#include "slirp/ip6.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"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000046
47static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
48{
49 const char *p, *p1;
50 int len;
51 p = *pp;
52 p1 = strchr(p, sep);
53 if (!p1)
54 return -1;
55 len = p1 - p;
56 p1++;
57 if (buf_size > 0) {
58 if (len > buf_size - 1)
59 len = buf_size - 1;
60 memcpy(buf, p, len);
61 buf[len] = '\0';
62 }
63 *pp = p1;
64 return 0;
65}
66
67/* slirp network adapter */
68
69#define SLIRP_CFG_HOSTFWD 1
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000070
71struct slirp_config_str {
72 struct slirp_config_str *next;
73 int flags;
74 char str[1024];
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000075};
76
77typedef struct SlirpState {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010078 NetClientState nc;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000079 QTAILQ_ENTRY(SlirpState) entry;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000080 Slirp *slirp;
Paolo Bonzinif6c2e662016-07-12 09:57:12 +020081 Notifier exit_notifier;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000082#ifndef _WIN32
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +010083 gchar *smb_dir;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000084#endif
85} SlirpState;
86
87static struct slirp_config_str *slirp_configs;
Paolo Bonzinib58deb32018-12-06 11:58:10 +010088static QTAILQ_HEAD(, SlirpState) slirp_stacks =
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000089 QTAILQ_HEAD_INITIALIZER(slirp_stacks);
90
Thomas Huthd18572d2018-08-22 15:43:30 +020091static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp);
92static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000093
94#ifndef _WIN32
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000095static int slirp_smb(SlirpState *s, const char *exported_dir,
Hervé Poussineau5c843af2017-07-15 18:43:50 +020096 struct in_addr vserver_addr, Error **errp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000097static void slirp_smb_cleanup(SlirpState *s);
98#else
99static inline void slirp_smb_cleanup(SlirpState *s) { }
100#endif
101
Marc-André Lureau62c1d2c2018-11-10 17:45:36 +0400102static void net_slirp_output(void *opaque, const uint8_t *pkt, int pkt_len)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000103{
104 SlirpState *s = opaque;
105
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000106 qemu_send_packet(&s->nc, pkt, pkt_len);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000107}
108
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100109static ssize_t net_slirp_receive(NetClientState *nc, const uint8_t *buf, size_t size)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000110{
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000111 SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000112
113 slirp_input(s->slirp, buf, size);
114
115 return size;
116}
117
Paolo Bonzinif6c2e662016-07-12 09:57:12 +0200118static void slirp_smb_exit(Notifier *n, void *data)
119{
120 SlirpState *s = container_of(n, SlirpState, exit_notifier);
121 slirp_smb_cleanup(s);
122}
123
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100124static void net_slirp_cleanup(NetClientState *nc)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000125{
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000126 SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000127
128 slirp_cleanup(s->slirp);
Marc-André Lureau67f32802016-08-18 17:44:05 +0400129 if (s->exit_notifier.notify) {
130 qemu_remove_exit_notifier(&s->exit_notifier);
131 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000132 slirp_smb_cleanup(s);
133 QTAILQ_REMOVE(&slirp_stacks, s, entry);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000134}
135
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000136static NetClientInfo net_slirp_info = {
Eric Blakef394b2e2016-07-13 21:50:23 -0600137 .type = NET_CLIENT_DRIVER_USER,
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000138 .size = sizeof(SlirpState),
139 .receive = net_slirp_receive,
140 .cleanup = net_slirp_cleanup,
141};
142
Marc-André Lureaud846b922018-11-14 16:36:07 +0400143static const SlirpCb slirp_cb = {
144 .output = net_slirp_output,
145};
146
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100147static int net_slirp_init(NetClientState *peer, const char *model,
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000148 const char *name, int restricted,
Samuel Thibault0b11c032016-03-20 12:29:54 +0100149 bool ipv4, const char *vnetwork, const char *vhost,
150 bool ipv6, const char *vprefix6, int vprefix6_len,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100151 const char *vhost6,
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000152 const char *vhostname, const char *tftp_export,
153 const char *bootfile, const char *vdhcp_start,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100154 const char *vnameserver, const char *vnameserver6,
155 const char *smb_export, const char *vsmbserver,
Benjamin Drungf18d1372018-02-27 17:06:01 +0100156 const char **dnssearch, const char *vdomainname,
Fam Zheng0fca92b2018-09-14 15:26:16 +0800157 const char *tftp_server_name,
Benjamin Drungf18d1372018-02-27 17:06:01 +0100158 Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000159{
160 /* default settings according to historic slirp */
161 struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */
162 struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */
163 struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */
164 struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */
165 struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */
Yann Bordenave7aac5312016-03-15 10:31:22 +0100166 struct in6_addr ip6_prefix;
167 struct in6_addr ip6_host;
168 struct in6_addr ip6_dns;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000169#ifndef _WIN32
170 struct in_addr smbsrv = { .s_addr = 0 };
171#endif
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100172 NetClientState *nc;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000173 SlirpState *s;
174 char buf[20];
175 uint32_t addr;
176 int shift;
177 char *end;
178 struct slirp_config_str *config;
179
Samuel Thibault0b11c032016-03-20 12:29:54 +0100180 if (!ipv4 && (vnetwork || vhost || vnameserver)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200181 error_setg(errp, "IPv4 disabled but netmask/host/dns provided");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100182 return -1;
183 }
184
185 if (!ipv6 && (vprefix6 || vhost6 || vnameserver6)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200186 error_setg(errp, "IPv6 disabled but prefix/host6/dns6 provided");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100187 return -1;
188 }
189
190 if (!ipv4 && !ipv6) {
191 /* It doesn't make sense to disable both */
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200192 error_setg(errp, "IPv4 and IPv6 disabled");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100193 return -1;
194 }
195
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000196 if (vnetwork) {
197 if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) {
198 if (!inet_aton(vnetwork, &net)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200199 error_setg(errp, "Failed to parse netmask");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000200 return -1;
201 }
202 addr = ntohl(net.s_addr);
203 if (!(addr & 0x80000000)) {
204 mask.s_addr = htonl(0xff000000); /* class A */
205 } else if ((addr & 0xfff00000) == 0xac100000) {
206 mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */
207 } else if ((addr & 0xc0000000) == 0x80000000) {
208 mask.s_addr = htonl(0xffff0000); /* class B */
209 } else if ((addr & 0xffff0000) == 0xc0a80000) {
210 mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */
211 } else if ((addr & 0xffff0000) == 0xc6120000) {
212 mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */
213 } else if ((addr & 0xe0000000) == 0xe0000000) {
214 mask.s_addr = htonl(0xffffff00); /* class C */
215 } else {
216 mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */
217 }
218 } else {
219 if (!inet_aton(buf, &net)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200220 error_setg(errp, "Failed to parse netmask");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000221 return -1;
222 }
223 shift = strtol(vnetwork, &end, 10);
224 if (*end != '\0') {
225 if (!inet_aton(vnetwork, &mask)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200226 error_setg(errp,
227 "Failed to parse netmask (trailing chars)");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000228 return -1;
229 }
230 } else if (shift < 4 || shift > 32) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200231 error_setg(errp,
232 "Invalid netmask provided (must be in range 4-32)");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000233 return -1;
234 } else {
235 mask.s_addr = htonl(0xffffffff << (32 - shift));
236 }
237 }
238 net.s_addr &= mask.s_addr;
239 host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr);
240 dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr);
241 dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr);
242 }
243
244 if (vhost && !inet_aton(vhost, &host)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200245 error_setg(errp, "Failed to parse host");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000246 return -1;
247 }
248 if ((host.s_addr & mask.s_addr) != net.s_addr) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200249 error_setg(errp, "Host doesn't belong to network");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000250 return -1;
251 }
252
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000253 if (vnameserver && !inet_aton(vnameserver, &dns)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200254 error_setg(errp, "Failed to parse DNS");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000255 return -1;
256 }
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200257 if ((dns.s_addr & mask.s_addr) != net.s_addr) {
258 error_setg(errp, "DNS doesn't belong to network");
259 return -1;
260 }
261 if (dns.s_addr == host.s_addr) {
262 error_setg(errp, "DNS must be different from host");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000263 return -1;
264 }
265
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200266 if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200267 error_setg(errp, "Failed to parse DHCP start address");
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200268 return -1;
269 }
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200270 if ((dhcp.s_addr & mask.s_addr) != net.s_addr) {
271 error_setg(errp, "DHCP doesn't belong to network");
272 return -1;
273 }
274 if (dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) {
275 error_setg(errp, "DNS must be different from host and DNS");
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200276 return -1;
277 }
278
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000279#ifndef _WIN32
280 if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200281 error_setg(errp, "Failed to parse SMB address");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000282 return -1;
283 }
284#endif
285
Yann Bordenave7aac5312016-03-15 10:31:22 +0100286#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
287 /* No inet_pton helper before Vista... */
288 if (vprefix6) {
289 /* Unsupported */
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200290 error_setg(errp, "IPv6 prefix not supported");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100291 return -1;
292 }
293 memset(&ip6_prefix, 0, sizeof(ip6_prefix));
294 ip6_prefix.s6_addr[0] = 0xfe;
295 ip6_prefix.s6_addr[1] = 0xc0;
296#else
297 if (!vprefix6) {
298 vprefix6 = "fec0::";
299 }
300 if (!inet_pton(AF_INET6, vprefix6, &ip6_prefix)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200301 error_setg(errp, "Failed to parse IPv6 prefix");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100302 return -1;
303 }
304#endif
305
306 if (!vprefix6_len) {
307 vprefix6_len = 64;
308 }
309 if (vprefix6_len < 0 || vprefix6_len > 126) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200310 error_setg(errp,
311 "Invalid prefix provided (prefix len must be in range 0-126");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100312 return -1;
313 }
314
315 if (vhost6) {
316#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200317 error_setg(errp, "IPv6 host not supported");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100318 return -1;
319#else
320 if (!inet_pton(AF_INET6, vhost6, &ip6_host)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200321 error_setg(errp, "Failed to parse IPv6 host");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100322 return -1;
323 }
324 if (!in6_equal_net(&ip6_prefix, &ip6_host, vprefix6_len)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200325 error_setg(errp, "IPv6 Host doesn't belong to network");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100326 return -1;
327 }
328#endif
329 } else {
330 ip6_host = ip6_prefix;
331 ip6_host.s6_addr[15] |= 2;
332 }
333
334 if (vnameserver6) {
335#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200336 error_setg(errp, "IPv6 DNS not supported");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100337 return -1;
338#else
339 if (!inet_pton(AF_INET6, vnameserver6, &ip6_dns)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200340 error_setg(errp, "Failed to parse IPv6 DNS");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100341 return -1;
342 }
343 if (!in6_equal_net(&ip6_prefix, &ip6_dns, vprefix6_len)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200344 error_setg(errp, "IPv6 DNS doesn't belong to network");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100345 return -1;
346 }
347#endif
348 } else {
349 ip6_dns = ip6_prefix;
350 ip6_dns.s6_addr[15] |= 3;
351 }
352
Benjamin Drungf18d1372018-02-27 17:06:01 +0100353 if (vdomainname && !*vdomainname) {
354 error_setg(errp, "'domainname' parameter cannot be empty");
355 return -1;
356 }
357
Fam Zheng6e157a02018-09-14 15:26:15 +0800358 if (vdomainname && strlen(vdomainname) > 255) {
359 error_setg(errp, "'domainname' parameter cannot exceed 255 bytes");
360 return -1;
361 }
362
363 if (vhostname && strlen(vhostname) > 255) {
364 error_setg(errp, "'vhostname' parameter cannot exceed 255 bytes");
365 return -1;
366 }
Yann Bordenave7aac5312016-03-15 10:31:22 +0100367
Fam Zheng0fca92b2018-09-14 15:26:16 +0800368 if (tftp_server_name && strlen(tftp_server_name) > 255) {
369 error_setg(errp, "'tftp-server-name' parameter cannot exceed 255 bytes");
370 return -1;
371 }
372
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100373 nc = qemu_new_net_client(&net_slirp_info, peer, model, name);
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000374
375 snprintf(nc->info_str, sizeof(nc->info_str),
Jan Kiszkac54ed5b2011-07-20 12:20:14 +0200376 "net=%s,restrict=%s", inet_ntoa(net),
377 restricted ? "on" : "off");
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000378
379 s = DO_UPCAST(SlirpState, nc, nc);
380
Samuel Thibault0b11c032016-03-20 12:29:54 +0100381 s->slirp = slirp_init(restricted, ipv4, net, mask, host,
382 ipv6, ip6_prefix, vprefix6_len, ip6_host,
Fam Zheng0fca92b2018-09-14 15:26:16 +0800383 vhostname, tftp_server_name,
384 tftp_export, bootfile, dhcp,
Marc-André Lureau62c1d2c2018-11-10 17:45:36 +0400385 dns, ip6_dns, dnssearch, vdomainname,
Marc-André Lureaud846b922018-11-14 16:36:07 +0400386 &slirp_cb, s);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000387 QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry);
388
389 for (config = slirp_configs; config; config = config->next) {
390 if (config->flags & SLIRP_CFG_HOSTFWD) {
Thomas Huthd18572d2018-08-22 15:43:30 +0200391 if (slirp_hostfwd(s, config->str, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000392 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200393 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000394 } else {
Thomas Huthd18572d2018-08-22 15:43:30 +0200395 if (slirp_guestfwd(s, config->str, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000396 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200397 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000398 }
399 }
400#ifndef _WIN32
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000401 if (smb_export) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200402 if (slirp_smb(s, smb_export, smbsrv, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000403 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200404 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000405 }
406#endif
407
Paolo Bonzinif6c2e662016-07-12 09:57:12 +0200408 s->exit_notifier.notify = slirp_smb_exit;
409 qemu_add_exit_notifier(&s->exit_notifier);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000410 return 0;
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000411
412error:
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100413 qemu_del_net_client(nc);
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000414 return -1;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000415}
416
Thomas Huth93653062018-01-11 21:02:40 +0100417static SlirpState *slirp_lookup(Monitor *mon, const char *hub_id,
418 const char *name)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000419{
Thomas Huth93653062018-01-11 21:02:40 +0100420 if (name) {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100421 NetClientState *nc;
Thomas Huth93653062018-01-11 21:02:40 +0100422 if (hub_id) {
423 nc = net_hub_find_client_by_name(strtol(hub_id, NULL, 0), name);
424 if (!nc) {
Thomas Huth442da402018-04-30 20:02:24 +0200425 monitor_printf(mon, "unrecognized (hub-id, stackname) pair\n");
Thomas Huth93653062018-01-11 21:02:40 +0100426 return NULL;
427 }
Thomas Huth68cb29e2018-09-20 10:22:27 +0200428 warn_report("Using 'hub-id' is deprecated, specify the netdev id "
429 "directly instead");
Thomas Huth93653062018-01-11 21:02:40 +0100430 } else {
431 nc = qemu_find_netdev(name);
432 if (!nc) {
433 monitor_printf(mon, "unrecognized netdev id '%s'\n", name);
434 return NULL;
435 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000436 }
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000437 if (strcmp(nc->model, "user")) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000438 monitor_printf(mon, "invalid device specified\n");
439 return NULL;
440 }
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000441 return DO_UPCAST(SlirpState, nc, nc);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000442 } else {
443 if (QTAILQ_EMPTY(&slirp_stacks)) {
444 monitor_printf(mon, "user mode network stack not in use\n");
445 return NULL;
446 }
447 return QTAILQ_FIRST(&slirp_stacks);
448 }
449}
450
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100451void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000452{
453 struct in_addr host_addr = { .s_addr = INADDR_ANY };
454 int host_port;
Markus Armbrustere30e5eb2011-11-16 15:45:59 +0100455 char buf[256];
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000456 const char *src_str, *p;
457 SlirpState *s;
458 int is_udp = 0;
459 int err;
460 const char *arg1 = qdict_get_str(qdict, "arg1");
461 const char *arg2 = qdict_get_try_str(qdict, "arg2");
462 const char *arg3 = qdict_get_try_str(qdict, "arg3");
463
Thomas Huth93653062018-01-11 21:02:40 +0100464 if (arg3) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000465 s = slirp_lookup(mon, arg1, arg2);
466 src_str = arg3;
Thomas Huth93653062018-01-11 21:02:40 +0100467 } else if (arg2) {
468 s = slirp_lookup(mon, NULL, arg1);
469 src_str = arg2;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000470 } else {
471 s = slirp_lookup(mon, NULL, NULL);
472 src_str = arg1;
473 }
474 if (!s) {
475 return;
476 }
477
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000478 p = src_str;
Markus Armbrustere30e5eb2011-11-16 15:45:59 +0100479 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
480 goto fail_syntax;
481 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000482
483 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
484 is_udp = 0;
485 } else if (!strcmp(buf, "udp")) {
486 is_udp = 1;
487 } else {
488 goto fail_syntax;
489 }
490
491 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
492 goto fail_syntax;
493 }
494 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
495 goto fail_syntax;
496 }
497
Nia Alarie1fb3f7f2018-03-16 14:39:21 +0000498 if (qemu_strtoi(p, NULL, 10, &host_port)) {
499 goto fail_syntax;
500 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000501
Peter Maydell70381662014-06-16 16:47:49 +0100502 err = slirp_remove_hostfwd(s->slirp, is_udp, host_addr, host_port);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000503
504 monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
Geoffrey Thomasb15ba6c2011-12-17 04:23:59 -0500505 err ? "not found" : "removed");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000506 return;
507
508 fail_syntax:
509 monitor_printf(mon, "invalid format\n");
510}
511
Thomas Huthd18572d2018-08-22 15:43:30 +0200512static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000513{
514 struct in_addr host_addr = { .s_addr = INADDR_ANY };
515 struct in_addr guest_addr = { .s_addr = 0 };
516 int host_port, guest_port;
517 const char *p;
518 char buf[256];
519 int is_udp;
520 char *end;
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100521 const char *fail_reason = "Unknown reason";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000522
523 p = redir_str;
524 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100525 fail_reason = "No : separators";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000526 goto fail_syntax;
527 }
528 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
529 is_udp = 0;
530 } else if (!strcmp(buf, "udp")) {
531 is_udp = 1;
532 } else {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100533 fail_reason = "Bad protocol name";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000534 goto fail_syntax;
535 }
536
Thomas Huthd18572d2018-08-22 15:43:30 +0200537 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
538 fail_reason = "Missing : separator";
539 goto fail_syntax;
540 }
541 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
542 fail_reason = "Bad host address";
543 goto fail_syntax;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000544 }
545
Thomas Huthd18572d2018-08-22 15:43:30 +0200546 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100547 fail_reason = "Bad host port separator";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000548 goto fail_syntax;
549 }
550 host_port = strtol(buf, &end, 0);
Vincent Bernat0bed71e2017-02-25 22:31:58 +0100551 if (*end != '\0' || host_port < 0 || host_port > 65535) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100552 fail_reason = "Bad host port";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000553 goto fail_syntax;
554 }
555
556 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100557 fail_reason = "Missing guest address";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000558 goto fail_syntax;
559 }
560 if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100561 fail_reason = "Bad guest address";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000562 goto fail_syntax;
563 }
564
565 guest_port = strtol(p, &end, 0);
566 if (*end != '\0' || guest_port < 1 || guest_port > 65535) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100567 fail_reason = "Bad guest port";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000568 goto fail_syntax;
569 }
570
571 if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr,
572 guest_port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200573 error_setg(errp, "Could not set up host forwarding rule '%s'",
574 redir_str);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000575 return -1;
576 }
577 return 0;
578
579 fail_syntax:
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100580 error_setg(errp, "Invalid host forwarding rule '%s' (%s)", redir_str,
581 fail_reason);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000582 return -1;
583}
584
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100585void hmp_hostfwd_add(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000586{
587 const char *redir_str;
588 SlirpState *s;
589 const char *arg1 = qdict_get_str(qdict, "arg1");
590 const char *arg2 = qdict_get_try_str(qdict, "arg2");
591 const char *arg3 = qdict_get_try_str(qdict, "arg3");
592
Thomas Huth93653062018-01-11 21:02:40 +0100593 if (arg3) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000594 s = slirp_lookup(mon, arg1, arg2);
595 redir_str = arg3;
Thomas Huth93653062018-01-11 21:02:40 +0100596 } else if (arg2) {
597 s = slirp_lookup(mon, NULL, arg1);
598 redir_str = arg2;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000599 } else {
600 s = slirp_lookup(mon, NULL, NULL);
601 redir_str = arg1;
602 }
603 if (s) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200604 Error *err = NULL;
Thomas Huthd18572d2018-08-22 15:43:30 +0200605 if (slirp_hostfwd(s, redir_str, &err) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200606 error_report_err(err);
607 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000608 }
609
610}
611
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000612#ifndef _WIN32
613
614/* automatic user mode samba server configuration */
615static void slirp_smb_cleanup(SlirpState *s)
616{
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100617 int ret;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000618
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100619 if (s->smb_dir) {
620 gchar *cmd = g_strdup_printf("rm -rf %s", s->smb_dir);
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100621 ret = system(cmd);
Juan Quintela24ac07d2010-03-04 10:00:31 +0100622 if (ret == -1 || !WIFEXITED(ret)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100623 error_report("'%s' failed.", cmd);
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100624 } else if (WEXITSTATUS(ret)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100625 error_report("'%s' failed. Error code: %d",
626 cmd, WEXITSTATUS(ret));
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100627 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100628 g_free(cmd);
629 g_free(s->smb_dir);
630 s->smb_dir = NULL;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000631 }
632}
633
634static int slirp_smb(SlirpState* s, const char *exported_dir,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200635 struct in_addr vserver_addr, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000636{
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100637 char *smb_conf;
638 char *smb_cmdline;
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200639 struct passwd *passwd;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000640 FILE *f;
641
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200642 passwd = getpwuid(geteuid());
643 if (!passwd) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200644 error_setg(errp, "Failed to retrieve user name");
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200645 return -1;
646 }
647
Dunrong Huang927d8112012-07-06 14:04:43 +0800648 if (access(CONFIG_SMBD_COMMAND, F_OK)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200649 error_setg(errp, "Could not find '%s', please install it",
650 CONFIG_SMBD_COMMAND);
Dunrong Huang927d8112012-07-06 14:04:43 +0800651 return -1;
652 }
653
654 if (access(exported_dir, R_OK | X_OK)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200655 error_setg(errp, "Error accessing shared directory '%s': %s",
656 exported_dir, strerror(errno));
Dunrong Huang927d8112012-07-06 14:04:43 +0800657 return -1;
658 }
659
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100660 s->smb_dir = g_dir_make_tmp("qemu-smb.XXXXXX", NULL);
661 if (!s->smb_dir) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200662 error_setg(errp, "Could not create samba server dir");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000663 return -1;
664 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100665 smb_conf = g_strdup_printf("%s/%s", s->smb_dir, "smb.conf");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000666
667 f = fopen(smb_conf, "w");
668 if (!f) {
669 slirp_smb_cleanup(s);
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200670 error_setg(errp,
671 "Could not create samba server configuration file '%s'",
672 smb_conf);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100673 g_free(smb_conf);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000674 return -1;
675 }
676 fprintf(f,
677 "[global]\n"
678 "private dir=%s\n"
Peter Wu7912d042014-11-03 11:52:10 +0100679 "interfaces=127.0.0.1\n"
680 "bind interfaces only=yes\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000681 "pid directory=%s\n"
682 "lock directory=%s\n"
Nikolaus Rath276eda52012-04-25 09:57:19 -0400683 "state directory=%s\n"
Peter Wu7912d042014-11-03 11:52:10 +0100684 "cache directory=%s\n"
Michael Bueschb87b8a82014-04-27 14:54:12 +0400685 "ncalrpc dir=%s/ncalrpc\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000686 "log file=%s/log.smbd\n"
687 "smb passwd file=%s/smbpasswd\n"
Michael Bueschc2804ee2013-11-01 12:23:49 +0100688 "security = user\n"
689 "map to guest = Bad User\n"
Peter Wu7912d042014-11-03 11:52:10 +0100690 "load printers = no\n"
691 "printing = bsd\n"
692 "disable spoolss = yes\n"
693 "usershare max shares = 0\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000694 "[qemu]\n"
695 "path=%s\n"
696 "read only=no\n"
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200697 "guest ok=yes\n"
698 "force user=%s\n",
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000699 s->smb_dir,
700 s->smb_dir,
701 s->smb_dir,
702 s->smb_dir,
703 s->smb_dir,
Nikolaus Rath276eda52012-04-25 09:57:19 -0400704 s->smb_dir,
Michael Bueschb87b8a82014-04-27 14:54:12 +0400705 s->smb_dir,
Peter Wu7912d042014-11-03 11:52:10 +0100706 s->smb_dir,
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200707 exported_dir,
708 passwd->pw_name
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000709 );
710 fclose(f);
711
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100712 smb_cmdline = g_strdup_printf("%s -l %s -s %s",
Michael Tokarev44d8d2b2014-10-25 00:29:50 +0400713 CONFIG_SMBD_COMMAND, s->smb_dir, smb_conf);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100714 g_free(smb_conf);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000715
Marc-André Lureau3ed9f822018-11-10 17:45:39 +0400716 if (slirp_add_exec(s->slirp, NULL, smb_cmdline, &vserver_addr, 139) < 0 ||
717 slirp_add_exec(s->slirp, NULL, smb_cmdline, &vserver_addr, 445) < 0) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000718 slirp_smb_cleanup(s);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100719 g_free(smb_cmdline);
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200720 error_setg(errp, "Conflicting/invalid smbserver address");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000721 return -1;
722 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100723 g_free(smb_cmdline);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000724 return 0;
725}
726
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000727#endif /* !defined(_WIN32) */
728
729struct GuestFwd {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300730 CharBackend hd;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000731 struct in_addr server;
732 int port;
733 Slirp *slirp;
734};
735
736static int guestfwd_can_read(void *opaque)
737{
738 struct GuestFwd *fwd = opaque;
739 return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port);
740}
741
742static void guestfwd_read(void *opaque, const uint8_t *buf, int size)
743{
744 struct GuestFwd *fwd = opaque;
745 slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size);
746}
747
Thomas Huthd18572d2018-08-22 15:43:30 +0200748static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000749{
750 struct in_addr server = { .s_addr = 0 };
751 struct GuestFwd *fwd;
752 const char *p;
753 char buf[128];
754 char *end;
755 int port;
756
757 p = config_str;
Thomas Huthd18572d2018-08-22 15:43:30 +0200758 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
759 goto fail_syntax;
760 }
761 if (strcmp(buf, "tcp") && buf[0] != '\0') {
762 goto fail_syntax;
763 }
764 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
765 goto fail_syntax;
766 }
767 if (buf[0] != '\0' && !inet_aton(buf, &server)) {
768 goto fail_syntax;
769 }
770 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
771 goto fail_syntax;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000772 }
773 port = strtol(buf, &end, 10);
774 if (*end != '\0' || port < 1 || port > 65535) {
775 goto fail_syntax;
776 }
777
Alexander Grafa9899992011-06-04 07:25:59 +0200778 snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000779
Alexander Grafb412eb62012-06-03 09:45:01 +0200780 if ((strlen(p) > 4) && !strncmp(p, "cmd:", 4)) {
Marc-André Lureau3ed9f822018-11-10 17:45:39 +0400781 if (slirp_add_exec(s->slirp, NULL, &p[4], &server, port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200782 error_setg(errp, "Conflicting/invalid host:port in guest "
783 "forwarding rule '%s'", config_str);
Alexander Grafb412eb62012-06-03 09:45:01 +0200784 return -1;
785 }
786 } else {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300787 Error *err = NULL;
Marc-André Lureau95e30b22018-08-22 19:19:42 +0200788 /*
789 * FIXME: sure we want to support implicit
790 * muxed monitors here?
791 */
792 Chardev *chr = qemu_chr_new_mux_mon(buf, p);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300793
794 if (!chr) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200795 error_setg(errp, "Could not open guest forwarding device '%s'",
796 buf);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300797 return -1;
798 }
799
800 fwd = g_new(struct GuestFwd, 1);
801 qemu_chr_fe_init(&fwd->hd, chr, &err);
802 if (err) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200803 error_propagate(errp, err);
Alexander Grafb412eb62012-06-03 09:45:01 +0200804 g_free(fwd);
805 return -1;
806 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000807
Marc-André Lureau3ed9f822018-11-10 17:45:39 +0400808 if (slirp_add_exec(s->slirp, &fwd->hd, NULL, &server, port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200809 error_setg(errp, "Conflicting/invalid host:port in guest "
810 "forwarding rule '%s'", config_str);
Alexander Grafb412eb62012-06-03 09:45:01 +0200811 g_free(fwd);
812 return -1;
813 }
814 fwd->server = server;
815 fwd->port = port;
816 fwd->slirp = s->slirp;
817
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300818 qemu_chr_fe_set_handlers(&fwd->hd, guestfwd_can_read, guestfwd_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +0300819 NULL, NULL, fwd, NULL, true);
Alexander Grafb412eb62012-06-03 09:45:01 +0200820 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000821 return 0;
822
823 fail_syntax:
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200824 error_setg(errp, "Invalid guest forwarding rule '%s'", config_str);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000825 return -1;
826}
827
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100828void hmp_info_usernet(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000829{
830 SlirpState *s;
831
832 QTAILQ_FOREACH(s, &slirp_stacks, entry) {
Stefan Hajnoczi90d87a32012-07-24 16:35:06 +0100833 int id;
Thomas Huth442da402018-04-30 20:02:24 +0200834 bool got_hub_id = net_hub_id_for_client(&s->nc, &id) == 0;
Marc-André Lureaub7f43bf2018-11-10 17:45:43 +0400835 char *info = slirp_connection_info(s->slirp);
836 monitor_printf(mon, "Hub %d (%s):\n%s",
Thomas Huth442da402018-04-30 20:02:24 +0200837 got_hub_id ? id : -1,
Marc-André Lureaub7f43bf2018-11-10 17:45:43 +0400838 s->nc.name, info);
839 g_free(info);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000840 }
841}
842
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200843static void
844net_init_slirp_configs(const StringList *fwd, int flags)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000845{
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200846 while (fwd) {
847 struct slirp_config_str *config;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000848
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200849 config = g_malloc0(sizeof(*config));
850 pstrcpy(config->str, sizeof(config->str), fwd->value->str);
851 config->flags = flags;
852 config->next = slirp_configs;
853 slirp_configs = config;
854
855 fwd = fwd->next;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000856 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000857}
858
Klaus Stengel63d29602012-10-27 19:53:39 +0200859static const char **slirp_dnssearch(const StringList *dnsname)
860{
861 const StringList *c = dnsname;
862 size_t i = 0, num_opts = 0;
863 const char **ret;
864
865 while (c) {
866 num_opts++;
867 c = c->next;
868 }
869
870 if (num_opts == 0) {
871 return NULL;
872 }
873
874 ret = g_malloc((num_opts + 1) * sizeof(*ret));
875 c = dnsname;
876 while (c) {
877 ret[i++] = c->value->str;
878 c = c->next;
879 }
880 ret[i] = NULL;
881 return ret;
882}
883
Kővágó, Zoltáncebea512016-07-13 21:50:12 -0600884int net_init_slirp(const Netdev *netdev, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200885 NetClientState *peer, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000886{
887 struct slirp_config_str *config;
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200888 char *vnet;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000889 int ret;
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200890 const NetdevUserOptions *user;
Klaus Stengel63d29602012-10-27 19:53:39 +0200891 const char **dnssearch;
Samuel Thibault0b11c032016-03-20 12:29:54 +0100892 bool ipv4 = true, ipv6 = true;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000893
Eric Blakef394b2e2016-07-13 21:50:23 -0600894 assert(netdev->type == NET_CLIENT_DRIVER_USER);
895 user = &netdev->u.user;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000896
Samuel Thibault0b11c032016-03-20 12:29:54 +0100897 if ((user->has_ipv6 && user->ipv6 && !user->has_ipv4) ||
898 (user->has_ipv4 && !user->ipv4)) {
899 ipv4 = 0;
900 }
901 if ((user->has_ipv4 && user->ipv4 && !user->has_ipv6) ||
902 (user->has_ipv6 && !user->ipv6)) {
903 ipv6 = 0;
904 }
905
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200906 vnet = user->has_net ? g_strdup(user->net) :
907 user->has_ip ? g_strdup_printf("%s/24", user->ip) :
908 NULL;
Jan Kiszkac54ed5b2011-07-20 12:20:14 +0200909
Klaus Stengel63d29602012-10-27 19:53:39 +0200910 dnssearch = slirp_dnssearch(user->dnssearch);
911
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200912 /* all optional fields are initialized to "all bits zero" */
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000913
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200914 net_init_slirp_configs(user->hostfwd, SLIRP_CFG_HOSTFWD);
915 net_init_slirp_configs(user->guestfwd, 0);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000916
Samuel Thibault0b11c032016-03-20 12:29:54 +0100917 ret = net_slirp_init(peer, "user", name, user->q_restrict,
918 ipv4, vnet, user->host,
919 ipv6, user->ipv6_prefix, user->ipv6_prefixlen,
Samuel Thibaultd8eb3862016-03-25 00:02:58 +0100920 user->ipv6_host, user->hostname, user->tftp,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100921 user->bootfile, user->dhcpstart,
Samuel Thibaultd8eb3862016-03-25 00:02:58 +0100922 user->dns, user->ipv6_dns, user->smb,
Fam Zheng0fca92b2018-09-14 15:26:16 +0800923 user->smbserver, dnssearch, user->domainname,
924 user->tftp_server_name, errp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000925
926 while (slirp_configs) {
927 config = slirp_configs;
928 slirp_configs = config->next;
Anthony Liguori7267c092011-08-20 22:09:37 -0500929 g_free(config);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000930 }
931
Anthony Liguori7267c092011-08-20 22:09:37 -0500932 g_free(vnet);
Klaus Stengel63d29602012-10-27 19:53:39 +0200933 g_free(dnssearch);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000934
935 return ret;
936}