blob: 005c2675e638135933b91125d5560d23180c5ce9 [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
70#define SLIRP_CFG_LEGACY 2
71
72struct slirp_config_str {
73 struct slirp_config_str *next;
74 int flags;
75 char str[1024];
76 int legacy_format;
77};
78
79typedef struct SlirpState {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +010080 NetClientState nc;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000081 QTAILQ_ENTRY(SlirpState) entry;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000082 Slirp *slirp;
Paolo Bonzinif6c2e662016-07-12 09:57:12 +020083 Notifier exit_notifier;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000084#ifndef _WIN32
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +010085 gchar *smb_dir;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000086#endif
87} SlirpState;
88
89static struct slirp_config_str *slirp_configs;
90const char *legacy_tftp_prefix;
91const char *legacy_bootp_filename;
92static QTAILQ_HEAD(slirp_stacks, SlirpState) slirp_stacks =
93 QTAILQ_HEAD_INITIALIZER(slirp_stacks);
94
95static int slirp_hostfwd(SlirpState *s, const char *redir_str,
Hervé Poussineau5c843af2017-07-15 18:43:50 +020096 int legacy_format, Error **errp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000097static int slirp_guestfwd(SlirpState *s, const char *config_str,
Hervé Poussineau5c843af2017-07-15 18:43:50 +020098 int legacy_format, Error **errp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000099
100#ifndef _WIN32
101static const char *legacy_smb_export;
102
103static int slirp_smb(SlirpState *s, const char *exported_dir,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200104 struct in_addr vserver_addr, Error **errp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000105static void slirp_smb_cleanup(SlirpState *s);
106#else
107static inline void slirp_smb_cleanup(SlirpState *s) { }
108#endif
109
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000110void slirp_output(void *opaque, const uint8_t *pkt, int pkt_len)
111{
112 SlirpState *s = opaque;
113
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000114 qemu_send_packet(&s->nc, pkt, pkt_len);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000115}
116
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100117static ssize_t net_slirp_receive(NetClientState *nc, const uint8_t *buf, size_t size)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000118{
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000119 SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000120
121 slirp_input(s->slirp, buf, size);
122
123 return size;
124}
125
Paolo Bonzinif6c2e662016-07-12 09:57:12 +0200126static void slirp_smb_exit(Notifier *n, void *data)
127{
128 SlirpState *s = container_of(n, SlirpState, exit_notifier);
129 slirp_smb_cleanup(s);
130}
131
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100132static void net_slirp_cleanup(NetClientState *nc)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000133{
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000134 SlirpState *s = DO_UPCAST(SlirpState, nc, nc);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000135
136 slirp_cleanup(s->slirp);
Marc-André Lureau67f32802016-08-18 17:44:05 +0400137 if (s->exit_notifier.notify) {
138 qemu_remove_exit_notifier(&s->exit_notifier);
139 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000140 slirp_smb_cleanup(s);
141 QTAILQ_REMOVE(&slirp_stacks, s, entry);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000142}
143
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000144static NetClientInfo net_slirp_info = {
Eric Blakef394b2e2016-07-13 21:50:23 -0600145 .type = NET_CLIENT_DRIVER_USER,
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000146 .size = sizeof(SlirpState),
147 .receive = net_slirp_receive,
148 .cleanup = net_slirp_cleanup,
149};
150
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100151static int net_slirp_init(NetClientState *peer, const char *model,
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000152 const char *name, int restricted,
Samuel Thibault0b11c032016-03-20 12:29:54 +0100153 bool ipv4, const char *vnetwork, const char *vhost,
154 bool ipv6, const char *vprefix6, int vprefix6_len,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100155 const char *vhost6,
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000156 const char *vhostname, const char *tftp_export,
157 const char *bootfile, const char *vdhcp_start,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100158 const char *vnameserver, const char *vnameserver6,
159 const char *smb_export, const char *vsmbserver,
Benjamin Drungf18d1372018-02-27 17:06:01 +0100160 const char **dnssearch, const char *vdomainname,
161 Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000162{
163 /* default settings according to historic slirp */
164 struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */
165 struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */
166 struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */
167 struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */
168 struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */
Yann Bordenave7aac5312016-03-15 10:31:22 +0100169 struct in6_addr ip6_prefix;
170 struct in6_addr ip6_host;
171 struct in6_addr ip6_dns;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000172#ifndef _WIN32
173 struct in_addr smbsrv = { .s_addr = 0 };
174#endif
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100175 NetClientState *nc;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000176 SlirpState *s;
177 char buf[20];
178 uint32_t addr;
179 int shift;
180 char *end;
181 struct slirp_config_str *config;
182
Samuel Thibault0b11c032016-03-20 12:29:54 +0100183 if (!ipv4 && (vnetwork || vhost || vnameserver)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200184 error_setg(errp, "IPv4 disabled but netmask/host/dns provided");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100185 return -1;
186 }
187
188 if (!ipv6 && (vprefix6 || vhost6 || vnameserver6)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200189 error_setg(errp, "IPv6 disabled but prefix/host6/dns6 provided");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100190 return -1;
191 }
192
193 if (!ipv4 && !ipv6) {
194 /* It doesn't make sense to disable both */
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200195 error_setg(errp, "IPv4 and IPv6 disabled");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100196 return -1;
197 }
198
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000199 if (!tftp_export) {
200 tftp_export = legacy_tftp_prefix;
201 }
202 if (!bootfile) {
203 bootfile = legacy_bootp_filename;
204 }
205
206 if (vnetwork) {
207 if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) {
208 if (!inet_aton(vnetwork, &net)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200209 error_setg(errp, "Failed to parse netmask");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000210 return -1;
211 }
212 addr = ntohl(net.s_addr);
213 if (!(addr & 0x80000000)) {
214 mask.s_addr = htonl(0xff000000); /* class A */
215 } else if ((addr & 0xfff00000) == 0xac100000) {
216 mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */
217 } else if ((addr & 0xc0000000) == 0x80000000) {
218 mask.s_addr = htonl(0xffff0000); /* class B */
219 } else if ((addr & 0xffff0000) == 0xc0a80000) {
220 mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */
221 } else if ((addr & 0xffff0000) == 0xc6120000) {
222 mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */
223 } else if ((addr & 0xe0000000) == 0xe0000000) {
224 mask.s_addr = htonl(0xffffff00); /* class C */
225 } else {
226 mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */
227 }
228 } else {
229 if (!inet_aton(buf, &net)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200230 error_setg(errp, "Failed to parse netmask");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000231 return -1;
232 }
233 shift = strtol(vnetwork, &end, 10);
234 if (*end != '\0') {
235 if (!inet_aton(vnetwork, &mask)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200236 error_setg(errp,
237 "Failed to parse netmask (trailing chars)");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000238 return -1;
239 }
240 } else if (shift < 4 || shift > 32) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200241 error_setg(errp,
242 "Invalid netmask provided (must be in range 4-32)");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000243 return -1;
244 } else {
245 mask.s_addr = htonl(0xffffffff << (32 - shift));
246 }
247 }
248 net.s_addr &= mask.s_addr;
249 host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr);
250 dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr);
251 dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr);
252 }
253
254 if (vhost && !inet_aton(vhost, &host)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200255 error_setg(errp, "Failed to parse host");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000256 return -1;
257 }
258 if ((host.s_addr & mask.s_addr) != net.s_addr) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200259 error_setg(errp, "Host doesn't belong to network");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000260 return -1;
261 }
262
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000263 if (vnameserver && !inet_aton(vnameserver, &dns)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200264 error_setg(errp, "Failed to parse DNS");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000265 return -1;
266 }
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200267 if ((dns.s_addr & mask.s_addr) != net.s_addr) {
268 error_setg(errp, "DNS doesn't belong to network");
269 return -1;
270 }
271 if (dns.s_addr == host.s_addr) {
272 error_setg(errp, "DNS must be different from host");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000273 return -1;
274 }
275
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200276 if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200277 error_setg(errp, "Failed to parse DHCP start address");
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200278 return -1;
279 }
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200280 if ((dhcp.s_addr & mask.s_addr) != net.s_addr) {
281 error_setg(errp, "DHCP doesn't belong to network");
282 return -1;
283 }
284 if (dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) {
285 error_setg(errp, "DNS must be different from host and DNS");
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200286 return -1;
287 }
288
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000289#ifndef _WIN32
290 if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200291 error_setg(errp, "Failed to parse SMB address");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000292 return -1;
293 }
294#endif
295
Yann Bordenave7aac5312016-03-15 10:31:22 +0100296#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
297 /* No inet_pton helper before Vista... */
298 if (vprefix6) {
299 /* Unsupported */
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200300 error_setg(errp, "IPv6 prefix not supported");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100301 return -1;
302 }
303 memset(&ip6_prefix, 0, sizeof(ip6_prefix));
304 ip6_prefix.s6_addr[0] = 0xfe;
305 ip6_prefix.s6_addr[1] = 0xc0;
306#else
307 if (!vprefix6) {
308 vprefix6 = "fec0::";
309 }
310 if (!inet_pton(AF_INET6, vprefix6, &ip6_prefix)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200311 error_setg(errp, "Failed to parse IPv6 prefix");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100312 return -1;
313 }
314#endif
315
316 if (!vprefix6_len) {
317 vprefix6_len = 64;
318 }
319 if (vprefix6_len < 0 || vprefix6_len > 126) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200320 error_setg(errp,
321 "Invalid prefix provided (prefix len must be in range 0-126");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100322 return -1;
323 }
324
325 if (vhost6) {
326#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200327 error_setg(errp, "IPv6 host not supported");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100328 return -1;
329#else
330 if (!inet_pton(AF_INET6, vhost6, &ip6_host)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200331 error_setg(errp, "Failed to parse IPv6 host");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100332 return -1;
333 }
334 if (!in6_equal_net(&ip6_prefix, &ip6_host, vprefix6_len)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200335 error_setg(errp, "IPv6 Host doesn't belong to network");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100336 return -1;
337 }
338#endif
339 } else {
340 ip6_host = ip6_prefix;
341 ip6_host.s6_addr[15] |= 2;
342 }
343
344 if (vnameserver6) {
345#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200346 error_setg(errp, "IPv6 DNS not supported");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100347 return -1;
348#else
349 if (!inet_pton(AF_INET6, vnameserver6, &ip6_dns)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200350 error_setg(errp, "Failed to parse IPv6 DNS");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100351 return -1;
352 }
353 if (!in6_equal_net(&ip6_prefix, &ip6_dns, vprefix6_len)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200354 error_setg(errp, "IPv6 DNS doesn't belong to network");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100355 return -1;
356 }
357#endif
358 } else {
359 ip6_dns = ip6_prefix;
360 ip6_dns.s6_addr[15] |= 3;
361 }
362
Benjamin Drungf18d1372018-02-27 17:06:01 +0100363 if (vdomainname && !*vdomainname) {
364 error_setg(errp, "'domainname' parameter cannot be empty");
365 return -1;
366 }
367
Yann Bordenave7aac5312016-03-15 10:31:22 +0100368
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100369 nc = qemu_new_net_client(&net_slirp_info, peer, model, name);
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000370
371 snprintf(nc->info_str, sizeof(nc->info_str),
Jan Kiszkac54ed5b2011-07-20 12:20:14 +0200372 "net=%s,restrict=%s", inet_ntoa(net),
373 restricted ? "on" : "off");
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000374
375 s = DO_UPCAST(SlirpState, nc, nc);
376
Samuel Thibault0b11c032016-03-20 12:29:54 +0100377 s->slirp = slirp_init(restricted, ipv4, net, mask, host,
378 ipv6, ip6_prefix, vprefix6_len, ip6_host,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100379 vhostname, tftp_export, bootfile, dhcp,
Benjamin Drungf18d1372018-02-27 17:06:01 +0100380 dns, ip6_dns, dnssearch, vdomainname, s);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000381 QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry);
382
383 for (config = slirp_configs; config; config = config->next) {
384 if (config->flags & SLIRP_CFG_HOSTFWD) {
385 if (slirp_hostfwd(s, config->str,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200386 config->flags & SLIRP_CFG_LEGACY, 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 } else {
390 if (slirp_guestfwd(s, config->str,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200391 config->flags & SLIRP_CFG_LEGACY, 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 }
395 }
396#ifndef _WIN32
397 if (!smb_export) {
398 smb_export = legacy_smb_export;
399 }
400 if (smb_export) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200401 if (slirp_smb(s, smb_export, smbsrv, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000402 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200403 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000404 }
405#endif
406
Paolo Bonzinif6c2e662016-07-12 09:57:12 +0200407 s->exit_notifier.notify = slirp_smb_exit;
408 qemu_add_exit_notifier(&s->exit_notifier);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000409 return 0;
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000410
411error:
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100412 qemu_del_net_client(nc);
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000413 return -1;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000414}
415
Thomas Huth93653062018-01-11 21:02:40 +0100416static SlirpState *slirp_lookup(Monitor *mon, const char *hub_id,
417 const char *name)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000418{
Thomas Huth93653062018-01-11 21:02:40 +0100419 if (name) {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100420 NetClientState *nc;
Thomas Huth93653062018-01-11 21:02:40 +0100421 if (hub_id) {
422 nc = net_hub_find_client_by_name(strtol(hub_id, NULL, 0), name);
423 if (!nc) {
Thomas Huth442da402018-04-30 20:02:24 +0200424 monitor_printf(mon, "unrecognized (hub-id, stackname) pair\n");
Thomas Huth93653062018-01-11 21:02:40 +0100425 return NULL;
426 }
427 } else {
428 nc = qemu_find_netdev(name);
429 if (!nc) {
430 monitor_printf(mon, "unrecognized netdev id '%s'\n", name);
431 return NULL;
432 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000433 }
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000434 if (strcmp(nc->model, "user")) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000435 monitor_printf(mon, "invalid device specified\n");
436 return NULL;
437 }
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000438 return DO_UPCAST(SlirpState, nc, nc);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000439 } else {
440 if (QTAILQ_EMPTY(&slirp_stacks)) {
441 monitor_printf(mon, "user mode network stack not in use\n");
442 return NULL;
443 }
444 return QTAILQ_FIRST(&slirp_stacks);
445 }
446}
447
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100448void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000449{
450 struct in_addr host_addr = { .s_addr = INADDR_ANY };
451 int host_port;
Markus Armbrustere30e5eb2011-11-16 15:45:59 +0100452 char buf[256];
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000453 const char *src_str, *p;
454 SlirpState *s;
455 int is_udp = 0;
456 int err;
457 const char *arg1 = qdict_get_str(qdict, "arg1");
458 const char *arg2 = qdict_get_try_str(qdict, "arg2");
459 const char *arg3 = qdict_get_try_str(qdict, "arg3");
460
Thomas Huth93653062018-01-11 21:02:40 +0100461 if (arg3) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000462 s = slirp_lookup(mon, arg1, arg2);
463 src_str = arg3;
Thomas Huth93653062018-01-11 21:02:40 +0100464 } else if (arg2) {
465 s = slirp_lookup(mon, NULL, arg1);
466 src_str = arg2;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000467 } else {
468 s = slirp_lookup(mon, NULL, NULL);
469 src_str = arg1;
470 }
471 if (!s) {
472 return;
473 }
474
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000475 p = src_str;
Markus Armbrustere30e5eb2011-11-16 15:45:59 +0100476 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
477 goto fail_syntax;
478 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000479
480 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
481 is_udp = 0;
482 } else if (!strcmp(buf, "udp")) {
483 is_udp = 1;
484 } else {
485 goto fail_syntax;
486 }
487
488 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
489 goto fail_syntax;
490 }
491 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
492 goto fail_syntax;
493 }
494
495 host_port = atoi(p);
496
Peter Maydell70381662014-06-16 16:47:49 +0100497 err = slirp_remove_hostfwd(s->slirp, is_udp, host_addr, host_port);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000498
499 monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
Geoffrey Thomasb15ba6c2011-12-17 04:23:59 -0500500 err ? "not found" : "removed");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000501 return;
502
503 fail_syntax:
504 monitor_printf(mon, "invalid format\n");
505}
506
507static int slirp_hostfwd(SlirpState *s, const char *redir_str,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200508 int legacy_format, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000509{
510 struct in_addr host_addr = { .s_addr = INADDR_ANY };
511 struct in_addr guest_addr = { .s_addr = 0 };
512 int host_port, guest_port;
513 const char *p;
514 char buf[256];
515 int is_udp;
516 char *end;
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100517 const char *fail_reason = "Unknown reason";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000518
519 p = redir_str;
520 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100521 fail_reason = "No : separators";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000522 goto fail_syntax;
523 }
524 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
525 is_udp = 0;
526 } else if (!strcmp(buf, "udp")) {
527 is_udp = 1;
528 } else {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100529 fail_reason = "Bad protocol name";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000530 goto fail_syntax;
531 }
532
533 if (!legacy_format) {
534 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100535 fail_reason = "Missing : separator";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000536 goto fail_syntax;
537 }
538 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100539 fail_reason = "Bad host address";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000540 goto fail_syntax;
541 }
542 }
543
544 if (get_str_sep(buf, sizeof(buf), &p, legacy_format ? ':' : '-') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100545 fail_reason = "Bad host port separator";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000546 goto fail_syntax;
547 }
548 host_port = strtol(buf, &end, 0);
Vincent Bernat0bed71e2017-02-25 22:31:58 +0100549 if (*end != '\0' || host_port < 0 || host_port > 65535) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100550 fail_reason = "Bad host port";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000551 goto fail_syntax;
552 }
553
554 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100555 fail_reason = "Missing guest address";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000556 goto fail_syntax;
557 }
558 if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100559 fail_reason = "Bad guest address";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000560 goto fail_syntax;
561 }
562
563 guest_port = strtol(p, &end, 0);
564 if (*end != '\0' || guest_port < 1 || guest_port > 65535) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100565 fail_reason = "Bad guest port";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000566 goto fail_syntax;
567 }
568
569 if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr,
570 guest_port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200571 error_setg(errp, "Could not set up host forwarding rule '%s'",
572 redir_str);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000573 return -1;
574 }
575 return 0;
576
577 fail_syntax:
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100578 error_setg(errp, "Invalid host forwarding rule '%s' (%s)", redir_str,
579 fail_reason);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000580 return -1;
581}
582
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100583void hmp_hostfwd_add(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000584{
585 const char *redir_str;
586 SlirpState *s;
587 const char *arg1 = qdict_get_str(qdict, "arg1");
588 const char *arg2 = qdict_get_try_str(qdict, "arg2");
589 const char *arg3 = qdict_get_try_str(qdict, "arg3");
590
Thomas Huth93653062018-01-11 21:02:40 +0100591 if (arg3) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000592 s = slirp_lookup(mon, arg1, arg2);
593 redir_str = arg3;
Thomas Huth93653062018-01-11 21:02:40 +0100594 } else if (arg2) {
595 s = slirp_lookup(mon, NULL, arg1);
596 redir_str = arg2;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000597 } else {
598 s = slirp_lookup(mon, NULL, NULL);
599 redir_str = arg1;
600 }
601 if (s) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200602 Error *err = NULL;
603 if (slirp_hostfwd(s, redir_str, 0, &err) < 0) {
604 error_report_err(err);
605 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000606 }
607
608}
609
610int net_slirp_redir(const char *redir_str)
611{
612 struct slirp_config_str *config;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200613 Error *err = NULL;
614 int res;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000615
616 if (QTAILQ_EMPTY(&slirp_stacks)) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500617 config = g_malloc(sizeof(*config));
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000618 pstrcpy(config->str, sizeof(config->str), redir_str);
619 config->flags = SLIRP_CFG_HOSTFWD | SLIRP_CFG_LEGACY;
620 config->next = slirp_configs;
621 slirp_configs = config;
622 return 0;
623 }
624
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200625 res = slirp_hostfwd(QTAILQ_FIRST(&slirp_stacks), redir_str, 1, &err);
626 if (res < 0) {
627 error_report_err(err);
628 }
629 return res;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000630}
631
632#ifndef _WIN32
633
634/* automatic user mode samba server configuration */
635static void slirp_smb_cleanup(SlirpState *s)
636{
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100637 int ret;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000638
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100639 if (s->smb_dir) {
640 gchar *cmd = g_strdup_printf("rm -rf %s", s->smb_dir);
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100641 ret = system(cmd);
Juan Quintela24ac07d2010-03-04 10:00:31 +0100642 if (ret == -1 || !WIFEXITED(ret)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100643 error_report("'%s' failed.", cmd);
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100644 } else if (WEXITSTATUS(ret)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100645 error_report("'%s' failed. Error code: %d",
646 cmd, WEXITSTATUS(ret));
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100647 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100648 g_free(cmd);
649 g_free(s->smb_dir);
650 s->smb_dir = NULL;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000651 }
652}
653
654static int slirp_smb(SlirpState* s, const char *exported_dir,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200655 struct in_addr vserver_addr, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000656{
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100657 char *smb_conf;
658 char *smb_cmdline;
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200659 struct passwd *passwd;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000660 FILE *f;
661
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200662 passwd = getpwuid(geteuid());
663 if (!passwd) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200664 error_setg(errp, "Failed to retrieve user name");
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200665 return -1;
666 }
667
Dunrong Huang927d8112012-07-06 14:04:43 +0800668 if (access(CONFIG_SMBD_COMMAND, F_OK)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200669 error_setg(errp, "Could not find '%s', please install it",
670 CONFIG_SMBD_COMMAND);
Dunrong Huang927d8112012-07-06 14:04:43 +0800671 return -1;
672 }
673
674 if (access(exported_dir, R_OK | X_OK)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200675 error_setg(errp, "Error accessing shared directory '%s': %s",
676 exported_dir, strerror(errno));
Dunrong Huang927d8112012-07-06 14:04:43 +0800677 return -1;
678 }
679
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100680 s->smb_dir = g_dir_make_tmp("qemu-smb.XXXXXX", NULL);
681 if (!s->smb_dir) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200682 error_setg(errp, "Could not create samba server dir");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000683 return -1;
684 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100685 smb_conf = g_strdup_printf("%s/%s", s->smb_dir, "smb.conf");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000686
687 f = fopen(smb_conf, "w");
688 if (!f) {
689 slirp_smb_cleanup(s);
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200690 error_setg(errp,
691 "Could not create samba server configuration file '%s'",
692 smb_conf);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100693 g_free(smb_conf);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000694 return -1;
695 }
696 fprintf(f,
697 "[global]\n"
698 "private dir=%s\n"
Peter Wu7912d042014-11-03 11:52:10 +0100699 "interfaces=127.0.0.1\n"
700 "bind interfaces only=yes\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000701 "pid directory=%s\n"
702 "lock directory=%s\n"
Nikolaus Rath276eda52012-04-25 09:57:19 -0400703 "state directory=%s\n"
Peter Wu7912d042014-11-03 11:52:10 +0100704 "cache directory=%s\n"
Michael Bueschb87b8a82014-04-27 14:54:12 +0400705 "ncalrpc dir=%s/ncalrpc\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000706 "log file=%s/log.smbd\n"
707 "smb passwd file=%s/smbpasswd\n"
Michael Bueschc2804ee2013-11-01 12:23:49 +0100708 "security = user\n"
709 "map to guest = Bad User\n"
Peter Wu7912d042014-11-03 11:52:10 +0100710 "load printers = no\n"
711 "printing = bsd\n"
712 "disable spoolss = yes\n"
713 "usershare max shares = 0\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000714 "[qemu]\n"
715 "path=%s\n"
716 "read only=no\n"
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200717 "guest ok=yes\n"
718 "force user=%s\n",
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000719 s->smb_dir,
720 s->smb_dir,
721 s->smb_dir,
722 s->smb_dir,
723 s->smb_dir,
Nikolaus Rath276eda52012-04-25 09:57:19 -0400724 s->smb_dir,
Michael Bueschb87b8a82014-04-27 14:54:12 +0400725 s->smb_dir,
Peter Wu7912d042014-11-03 11:52:10 +0100726 s->smb_dir,
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200727 exported_dir,
728 passwd->pw_name
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000729 );
730 fclose(f);
731
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100732 smb_cmdline = g_strdup_printf("%s -l %s -s %s",
Michael Tokarev44d8d2b2014-10-25 00:29:50 +0400733 CONFIG_SMBD_COMMAND, s->smb_dir, smb_conf);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100734 g_free(smb_conf);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000735
Michael Tokarev5c1e1892013-11-28 23:32:55 +0400736 if (slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 139) < 0 ||
737 slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 445) < 0) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000738 slirp_smb_cleanup(s);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100739 g_free(smb_cmdline);
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200740 error_setg(errp, "Conflicting/invalid smbserver address");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000741 return -1;
742 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100743 g_free(smb_cmdline);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000744 return 0;
745}
746
747/* automatic user mode samba server configuration (legacy interface) */
748int net_slirp_smb(const char *exported_dir)
749{
750 struct in_addr vserver_addr = { .s_addr = 0 };
751
752 if (legacy_smb_export) {
753 fprintf(stderr, "-smb given twice\n");
754 return -1;
755 }
756 legacy_smb_export = exported_dir;
757 if (!QTAILQ_EMPTY(&slirp_stacks)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200758 Error *err = NULL;
759 int res = slirp_smb(QTAILQ_FIRST(&slirp_stacks), exported_dir,
760 vserver_addr, &err);
761 if (res < 0) {
762 error_report_err(err);
763 }
764 return res;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000765 }
766 return 0;
767}
768
769#endif /* !defined(_WIN32) */
770
771struct GuestFwd {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300772 CharBackend hd;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000773 struct in_addr server;
774 int port;
775 Slirp *slirp;
776};
777
778static int guestfwd_can_read(void *opaque)
779{
780 struct GuestFwd *fwd = opaque;
781 return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port);
782}
783
784static void guestfwd_read(void *opaque, const uint8_t *buf, int size)
785{
786 struct GuestFwd *fwd = opaque;
787 slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size);
788}
789
790static int slirp_guestfwd(SlirpState *s, const char *config_str,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200791 int legacy_format, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000792{
793 struct in_addr server = { .s_addr = 0 };
794 struct GuestFwd *fwd;
795 const char *p;
796 char buf[128];
797 char *end;
798 int port;
799
800 p = config_str;
801 if (legacy_format) {
802 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
803 goto fail_syntax;
804 }
805 } else {
806 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
807 goto fail_syntax;
808 }
809 if (strcmp(buf, "tcp") && buf[0] != '\0') {
810 goto fail_syntax;
811 }
812 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
813 goto fail_syntax;
814 }
815 if (buf[0] != '\0' && !inet_aton(buf, &server)) {
816 goto fail_syntax;
817 }
818 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
819 goto fail_syntax;
820 }
821 }
822 port = strtol(buf, &end, 10);
823 if (*end != '\0' || port < 1 || port > 65535) {
824 goto fail_syntax;
825 }
826
Alexander Grafa9899992011-06-04 07:25:59 +0200827 snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000828
Alexander Grafb412eb62012-06-03 09:45:01 +0200829 if ((strlen(p) > 4) && !strncmp(p, "cmd:", 4)) {
830 if (slirp_add_exec(s->slirp, 0, &p[4], &server, port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200831 error_setg(errp, "Conflicting/invalid host:port in guest "
832 "forwarding rule '%s'", config_str);
Alexander Grafb412eb62012-06-03 09:45:01 +0200833 return -1;
834 }
835 } else {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300836 Error *err = NULL;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300837 Chardev *chr = qemu_chr_new(buf, p);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300838
839 if (!chr) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200840 error_setg(errp, "Could not open guest forwarding device '%s'",
841 buf);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300842 return -1;
843 }
844
845 fwd = g_new(struct GuestFwd, 1);
846 qemu_chr_fe_init(&fwd->hd, chr, &err);
847 if (err) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200848 error_propagate(errp, err);
Alexander Grafb412eb62012-06-03 09:45:01 +0200849 g_free(fwd);
850 return -1;
851 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000852
Paolo Bonzinid14fabd2016-10-27 22:04:58 +0200853 if (slirp_add_exec(s->slirp, 3, &fwd->hd, &server, port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200854 error_setg(errp, "Conflicting/invalid host:port in guest "
855 "forwarding rule '%s'", config_str);
Alexander Grafb412eb62012-06-03 09:45:01 +0200856 g_free(fwd);
857 return -1;
858 }
859 fwd->server = server;
860 fwd->port = port;
861 fwd->slirp = s->slirp;
862
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300863 qemu_chr_fe_set_handlers(&fwd->hd, guestfwd_can_read, guestfwd_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +0300864 NULL, NULL, fwd, NULL, true);
Alexander Grafb412eb62012-06-03 09:45:01 +0200865 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000866 return 0;
867
868 fail_syntax:
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200869 error_setg(errp, "Invalid guest forwarding rule '%s'", config_str);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000870 return -1;
871}
872
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100873void hmp_info_usernet(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000874{
875 SlirpState *s;
876
877 QTAILQ_FOREACH(s, &slirp_stacks, entry) {
Stefan Hajnoczi90d87a32012-07-24 16:35:06 +0100878 int id;
Thomas Huth442da402018-04-30 20:02:24 +0200879 bool got_hub_id = net_hub_id_for_client(&s->nc, &id) == 0;
880 monitor_printf(mon, "Hub %d (%s):\n",
881 got_hub_id ? id : -1,
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000882 s->nc.name);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000883 slirp_connection_info(s->slirp, mon);
884 }
885}
886
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200887static void
888net_init_slirp_configs(const StringList *fwd, int flags)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000889{
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200890 while (fwd) {
891 struct slirp_config_str *config;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000892
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200893 config = g_malloc0(sizeof(*config));
894 pstrcpy(config->str, sizeof(config->str), fwd->value->str);
895 config->flags = flags;
896 config->next = slirp_configs;
897 slirp_configs = config;
898
899 fwd = fwd->next;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000900 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000901}
902
Klaus Stengel63d29602012-10-27 19:53:39 +0200903static const char **slirp_dnssearch(const StringList *dnsname)
904{
905 const StringList *c = dnsname;
906 size_t i = 0, num_opts = 0;
907 const char **ret;
908
909 while (c) {
910 num_opts++;
911 c = c->next;
912 }
913
914 if (num_opts == 0) {
915 return NULL;
916 }
917
918 ret = g_malloc((num_opts + 1) * sizeof(*ret));
919 c = dnsname;
920 while (c) {
921 ret[i++] = c->value->str;
922 c = c->next;
923 }
924 ret[i] = NULL;
925 return ret;
926}
927
Kővágó, Zoltáncebea512016-07-13 21:50:12 -0600928int net_init_slirp(const Netdev *netdev, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200929 NetClientState *peer, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000930{
931 struct slirp_config_str *config;
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200932 char *vnet;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000933 int ret;
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200934 const NetdevUserOptions *user;
Klaus Stengel63d29602012-10-27 19:53:39 +0200935 const char **dnssearch;
Samuel Thibault0b11c032016-03-20 12:29:54 +0100936 bool ipv4 = true, ipv6 = true;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000937
Eric Blakef394b2e2016-07-13 21:50:23 -0600938 assert(netdev->type == NET_CLIENT_DRIVER_USER);
939 user = &netdev->u.user;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000940
Samuel Thibault0b11c032016-03-20 12:29:54 +0100941 if ((user->has_ipv6 && user->ipv6 && !user->has_ipv4) ||
942 (user->has_ipv4 && !user->ipv4)) {
943 ipv4 = 0;
944 }
945 if ((user->has_ipv4 && user->ipv4 && !user->has_ipv6) ||
946 (user->has_ipv6 && !user->ipv6)) {
947 ipv6 = 0;
948 }
949
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200950 vnet = user->has_net ? g_strdup(user->net) :
951 user->has_ip ? g_strdup_printf("%s/24", user->ip) :
952 NULL;
Jan Kiszkac54ed5b2011-07-20 12:20:14 +0200953
Klaus Stengel63d29602012-10-27 19:53:39 +0200954 dnssearch = slirp_dnssearch(user->dnssearch);
955
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200956 /* all optional fields are initialized to "all bits zero" */
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000957
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200958 net_init_slirp_configs(user->hostfwd, SLIRP_CFG_HOSTFWD);
959 net_init_slirp_configs(user->guestfwd, 0);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000960
Samuel Thibault0b11c032016-03-20 12:29:54 +0100961 ret = net_slirp_init(peer, "user", name, user->q_restrict,
962 ipv4, vnet, user->host,
963 ipv6, user->ipv6_prefix, user->ipv6_prefixlen,
Samuel Thibaultd8eb3862016-03-25 00:02:58 +0100964 user->ipv6_host, user->hostname, user->tftp,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100965 user->bootfile, user->dhcpstart,
Samuel Thibaultd8eb3862016-03-25 00:02:58 +0100966 user->dns, user->ipv6_dns, user->smb,
Benjamin Drungf18d1372018-02-27 17:06:01 +0100967 user->smbserver, dnssearch, user->domainname, 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}