blob: 8991816bbfc81ff5fb7aae0ca3066fd354544ad5 [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,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200160 const char **dnssearch, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000161{
162 /* default settings according to historic slirp */
163 struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */
164 struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */
165 struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */
166 struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */
167 struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */
Yann Bordenave7aac5312016-03-15 10:31:22 +0100168 struct in6_addr ip6_prefix;
169 struct in6_addr ip6_host;
170 struct in6_addr ip6_dns;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000171#ifndef _WIN32
172 struct in_addr smbsrv = { .s_addr = 0 };
173#endif
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100174 NetClientState *nc;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000175 SlirpState *s;
176 char buf[20];
177 uint32_t addr;
178 int shift;
179 char *end;
180 struct slirp_config_str *config;
181
Samuel Thibault0b11c032016-03-20 12:29:54 +0100182 if (!ipv4 && (vnetwork || vhost || vnameserver)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200183 error_setg(errp, "IPv4 disabled but netmask/host/dns provided");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100184 return -1;
185 }
186
187 if (!ipv6 && (vprefix6 || vhost6 || vnameserver6)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200188 error_setg(errp, "IPv6 disabled but prefix/host6/dns6 provided");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100189 return -1;
190 }
191
192 if (!ipv4 && !ipv6) {
193 /* It doesn't make sense to disable both */
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200194 error_setg(errp, "IPv4 and IPv6 disabled");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100195 return -1;
196 }
197
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000198 if (!tftp_export) {
199 tftp_export = legacy_tftp_prefix;
200 }
201 if (!bootfile) {
202 bootfile = legacy_bootp_filename;
203 }
204
205 if (vnetwork) {
206 if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) {
207 if (!inet_aton(vnetwork, &net)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200208 error_setg(errp, "Failed to parse netmask");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000209 return -1;
210 }
211 addr = ntohl(net.s_addr);
212 if (!(addr & 0x80000000)) {
213 mask.s_addr = htonl(0xff000000); /* class A */
214 } else if ((addr & 0xfff00000) == 0xac100000) {
215 mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */
216 } else if ((addr & 0xc0000000) == 0x80000000) {
217 mask.s_addr = htonl(0xffff0000); /* class B */
218 } else if ((addr & 0xffff0000) == 0xc0a80000) {
219 mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */
220 } else if ((addr & 0xffff0000) == 0xc6120000) {
221 mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */
222 } else if ((addr & 0xe0000000) == 0xe0000000) {
223 mask.s_addr = htonl(0xffffff00); /* class C */
224 } else {
225 mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */
226 }
227 } else {
228 if (!inet_aton(buf, &net)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200229 error_setg(errp, "Failed to parse netmask");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000230 return -1;
231 }
232 shift = strtol(vnetwork, &end, 10);
233 if (*end != '\0') {
234 if (!inet_aton(vnetwork, &mask)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200235 error_setg(errp,
236 "Failed to parse netmask (trailing chars)");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000237 return -1;
238 }
239 } else if (shift < 4 || shift > 32) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200240 error_setg(errp,
241 "Invalid netmask provided (must be in range 4-32)");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000242 return -1;
243 } else {
244 mask.s_addr = htonl(0xffffffff << (32 - shift));
245 }
246 }
247 net.s_addr &= mask.s_addr;
248 host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr);
249 dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr);
250 dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr);
251 }
252
253 if (vhost && !inet_aton(vhost, &host)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200254 error_setg(errp, "Failed to parse host");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000255 return -1;
256 }
257 if ((host.s_addr & mask.s_addr) != net.s_addr) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200258 error_setg(errp, "Host doesn't belong to network");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000259 return -1;
260 }
261
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000262 if (vnameserver && !inet_aton(vnameserver, &dns)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200263 error_setg(errp, "Failed to parse DNS");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000264 return -1;
265 }
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200266 if ((dns.s_addr & mask.s_addr) != net.s_addr) {
267 error_setg(errp, "DNS doesn't belong to network");
268 return -1;
269 }
270 if (dns.s_addr == host.s_addr) {
271 error_setg(errp, "DNS must be different from host");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000272 return -1;
273 }
274
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200275 if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200276 error_setg(errp, "Failed to parse DHCP start address");
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200277 return -1;
278 }
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200279 if ((dhcp.s_addr & mask.s_addr) != net.s_addr) {
280 error_setg(errp, "DHCP doesn't belong to network");
281 return -1;
282 }
283 if (dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) {
284 error_setg(errp, "DNS must be different from host and DNS");
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200285 return -1;
286 }
287
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000288#ifndef _WIN32
289 if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200290 error_setg(errp, "Failed to parse SMB address");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000291 return -1;
292 }
293#endif
294
Yann Bordenave7aac5312016-03-15 10:31:22 +0100295#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
296 /* No inet_pton helper before Vista... */
297 if (vprefix6) {
298 /* Unsupported */
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200299 error_setg(errp, "IPv6 prefix not supported");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100300 return -1;
301 }
302 memset(&ip6_prefix, 0, sizeof(ip6_prefix));
303 ip6_prefix.s6_addr[0] = 0xfe;
304 ip6_prefix.s6_addr[1] = 0xc0;
305#else
306 if (!vprefix6) {
307 vprefix6 = "fec0::";
308 }
309 if (!inet_pton(AF_INET6, vprefix6, &ip6_prefix)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200310 error_setg(errp, "Failed to parse IPv6 prefix");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100311 return -1;
312 }
313#endif
314
315 if (!vprefix6_len) {
316 vprefix6_len = 64;
317 }
318 if (vprefix6_len < 0 || vprefix6_len > 126) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200319 error_setg(errp,
320 "Invalid prefix provided (prefix len must be in range 0-126");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100321 return -1;
322 }
323
324 if (vhost6) {
325#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200326 error_setg(errp, "IPv6 host not supported");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100327 return -1;
328#else
329 if (!inet_pton(AF_INET6, vhost6, &ip6_host)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200330 error_setg(errp, "Failed to parse IPv6 host");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100331 return -1;
332 }
333 if (!in6_equal_net(&ip6_prefix, &ip6_host, vprefix6_len)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200334 error_setg(errp, "IPv6 Host doesn't belong to network");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100335 return -1;
336 }
337#endif
338 } else {
339 ip6_host = ip6_prefix;
340 ip6_host.s6_addr[15] |= 2;
341 }
342
343 if (vnameserver6) {
344#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200345 error_setg(errp, "IPv6 DNS not supported");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100346 return -1;
347#else
348 if (!inet_pton(AF_INET6, vnameserver6, &ip6_dns)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200349 error_setg(errp, "Failed to parse IPv6 DNS");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100350 return -1;
351 }
352 if (!in6_equal_net(&ip6_prefix, &ip6_dns, vprefix6_len)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200353 error_setg(errp, "IPv6 DNS doesn't belong to network");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100354 return -1;
355 }
356#endif
357 } else {
358 ip6_dns = ip6_prefix;
359 ip6_dns.s6_addr[15] |= 3;
360 }
361
362
Stefan Hajnocziab5f3f82012-07-24 16:35:08 +0100363 nc = qemu_new_net_client(&net_slirp_info, peer, model, name);
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000364
365 snprintf(nc->info_str, sizeof(nc->info_str),
Jan Kiszkac54ed5b2011-07-20 12:20:14 +0200366 "net=%s,restrict=%s", inet_ntoa(net),
367 restricted ? "on" : "off");
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000368
369 s = DO_UPCAST(SlirpState, nc, nc);
370
Samuel Thibault0b11c032016-03-20 12:29:54 +0100371 s->slirp = slirp_init(restricted, ipv4, net, mask, host,
372 ipv6, ip6_prefix, vprefix6_len, ip6_host,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100373 vhostname, tftp_export, bootfile, dhcp,
374 dns, ip6_dns, dnssearch, s);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000375 QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry);
376
377 for (config = slirp_configs; config; config = config->next) {
378 if (config->flags & SLIRP_CFG_HOSTFWD) {
379 if (slirp_hostfwd(s, config->str,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200380 config->flags & SLIRP_CFG_LEGACY, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000381 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200382 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000383 } else {
384 if (slirp_guestfwd(s, config->str,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200385 config->flags & SLIRP_CFG_LEGACY, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000386 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200387 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000388 }
389 }
390#ifndef _WIN32
391 if (!smb_export) {
392 smb_export = legacy_smb_export;
393 }
394 if (smb_export) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200395 if (slirp_smb(s, smb_export, smbsrv, 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#endif
400
Paolo Bonzinif6c2e662016-07-12 09:57:12 +0200401 s->exit_notifier.notify = slirp_smb_exit;
402 qemu_add_exit_notifier(&s->exit_notifier);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000403 return 0;
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000404
405error:
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100406 qemu_del_net_client(nc);
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000407 return -1;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000408}
409
Thomas Huth93653062018-01-11 21:02:40 +0100410static SlirpState *slirp_lookup(Monitor *mon, const char *hub_id,
411 const char *name)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000412{
Thomas Huth93653062018-01-11 21:02:40 +0100413 if (name) {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100414 NetClientState *nc;
Thomas Huth93653062018-01-11 21:02:40 +0100415 if (hub_id) {
416 nc = net_hub_find_client_by_name(strtol(hub_id, NULL, 0), name);
417 if (!nc) {
418 monitor_printf(mon, "unrecognized (vlan-id, stackname) pair\n");
419 return NULL;
420 }
421 } 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
489 host_port = atoi(p);
490
Peter Maydell70381662014-06-16 16:47:49 +0100491 err = slirp_remove_hostfwd(s->slirp, is_udp, host_addr, host_port);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000492
493 monitor_printf(mon, "host forwarding rule for %s %s\n", src_str,
Geoffrey Thomasb15ba6c2011-12-17 04:23:59 -0500494 err ? "not found" : "removed");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000495 return;
496
497 fail_syntax:
498 monitor_printf(mon, "invalid format\n");
499}
500
501static int slirp_hostfwd(SlirpState *s, const char *redir_str,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200502 int legacy_format, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000503{
504 struct in_addr host_addr = { .s_addr = INADDR_ANY };
505 struct in_addr guest_addr = { .s_addr = 0 };
506 int host_port, guest_port;
507 const char *p;
508 char buf[256];
509 int is_udp;
510 char *end;
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100511 const char *fail_reason = "Unknown reason";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000512
513 p = redir_str;
514 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100515 fail_reason = "No : separators";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000516 goto fail_syntax;
517 }
518 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
519 is_udp = 0;
520 } else if (!strcmp(buf, "udp")) {
521 is_udp = 1;
522 } else {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100523 fail_reason = "Bad protocol name";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000524 goto fail_syntax;
525 }
526
527 if (!legacy_format) {
528 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100529 fail_reason = "Missing : separator";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000530 goto fail_syntax;
531 }
532 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100533 fail_reason = "Bad host address";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000534 goto fail_syntax;
535 }
536 }
537
538 if (get_str_sep(buf, sizeof(buf), &p, legacy_format ? ':' : '-') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100539 fail_reason = "Bad host port separator";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000540 goto fail_syntax;
541 }
542 host_port = strtol(buf, &end, 0);
Vincent Bernat0bed71e2017-02-25 22:31:58 +0100543 if (*end != '\0' || host_port < 0 || host_port > 65535) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100544 fail_reason = "Bad host port";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000545 goto fail_syntax;
546 }
547
548 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100549 fail_reason = "Missing guest address";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000550 goto fail_syntax;
551 }
552 if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100553 fail_reason = "Bad guest address";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000554 goto fail_syntax;
555 }
556
557 guest_port = strtol(p, &end, 0);
558 if (*end != '\0' || guest_port < 1 || guest_port > 65535) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100559 fail_reason = "Bad guest port";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000560 goto fail_syntax;
561 }
562
563 if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr,
564 guest_port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200565 error_setg(errp, "Could not set up host forwarding rule '%s'",
566 redir_str);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000567 return -1;
568 }
569 return 0;
570
571 fail_syntax:
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100572 error_setg(errp, "Invalid host forwarding rule '%s' (%s)", redir_str,
573 fail_reason);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000574 return -1;
575}
576
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100577void hmp_hostfwd_add(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000578{
579 const char *redir_str;
580 SlirpState *s;
581 const char *arg1 = qdict_get_str(qdict, "arg1");
582 const char *arg2 = qdict_get_try_str(qdict, "arg2");
583 const char *arg3 = qdict_get_try_str(qdict, "arg3");
584
Thomas Huth93653062018-01-11 21:02:40 +0100585 if (arg3) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000586 s = slirp_lookup(mon, arg1, arg2);
587 redir_str = arg3;
Thomas Huth93653062018-01-11 21:02:40 +0100588 } else if (arg2) {
589 s = slirp_lookup(mon, NULL, arg1);
590 redir_str = arg2;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000591 } else {
592 s = slirp_lookup(mon, NULL, NULL);
593 redir_str = arg1;
594 }
595 if (s) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200596 Error *err = NULL;
597 if (slirp_hostfwd(s, redir_str, 0, &err) < 0) {
598 error_report_err(err);
599 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000600 }
601
602}
603
604int net_slirp_redir(const char *redir_str)
605{
606 struct slirp_config_str *config;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200607 Error *err = NULL;
608 int res;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000609
610 if (QTAILQ_EMPTY(&slirp_stacks)) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500611 config = g_malloc(sizeof(*config));
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000612 pstrcpy(config->str, sizeof(config->str), redir_str);
613 config->flags = SLIRP_CFG_HOSTFWD | SLIRP_CFG_LEGACY;
614 config->next = slirp_configs;
615 slirp_configs = config;
616 return 0;
617 }
618
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200619 res = slirp_hostfwd(QTAILQ_FIRST(&slirp_stacks), redir_str, 1, &err);
620 if (res < 0) {
621 error_report_err(err);
622 }
623 return res;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000624}
625
626#ifndef _WIN32
627
628/* automatic user mode samba server configuration */
629static void slirp_smb_cleanup(SlirpState *s)
630{
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100631 int ret;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000632
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100633 if (s->smb_dir) {
634 gchar *cmd = g_strdup_printf("rm -rf %s", s->smb_dir);
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100635 ret = system(cmd);
Juan Quintela24ac07d2010-03-04 10:00:31 +0100636 if (ret == -1 || !WIFEXITED(ret)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100637 error_report("'%s' failed.", cmd);
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100638 } else if (WEXITSTATUS(ret)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100639 error_report("'%s' failed. Error code: %d",
640 cmd, WEXITSTATUS(ret));
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100641 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100642 g_free(cmd);
643 g_free(s->smb_dir);
644 s->smb_dir = NULL;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000645 }
646}
647
648static int slirp_smb(SlirpState* s, const char *exported_dir,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200649 struct in_addr vserver_addr, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000650{
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100651 char *smb_conf;
652 char *smb_cmdline;
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200653 struct passwd *passwd;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000654 FILE *f;
655
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200656 passwd = getpwuid(geteuid());
657 if (!passwd) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200658 error_setg(errp, "Failed to retrieve user name");
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200659 return -1;
660 }
661
Dunrong Huang927d8112012-07-06 14:04:43 +0800662 if (access(CONFIG_SMBD_COMMAND, F_OK)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200663 error_setg(errp, "Could not find '%s', please install it",
664 CONFIG_SMBD_COMMAND);
Dunrong Huang927d8112012-07-06 14:04:43 +0800665 return -1;
666 }
667
668 if (access(exported_dir, R_OK | X_OK)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200669 error_setg(errp, "Error accessing shared directory '%s': %s",
670 exported_dir, strerror(errno));
Dunrong Huang927d8112012-07-06 14:04:43 +0800671 return -1;
672 }
673
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100674 s->smb_dir = g_dir_make_tmp("qemu-smb.XXXXXX", NULL);
675 if (!s->smb_dir) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200676 error_setg(errp, "Could not create samba server dir");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000677 return -1;
678 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100679 smb_conf = g_strdup_printf("%s/%s", s->smb_dir, "smb.conf");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000680
681 f = fopen(smb_conf, "w");
682 if (!f) {
683 slirp_smb_cleanup(s);
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200684 error_setg(errp,
685 "Could not create samba server configuration file '%s'",
686 smb_conf);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100687 g_free(smb_conf);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000688 return -1;
689 }
690 fprintf(f,
691 "[global]\n"
692 "private dir=%s\n"
Peter Wu7912d042014-11-03 11:52:10 +0100693 "interfaces=127.0.0.1\n"
694 "bind interfaces only=yes\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000695 "pid directory=%s\n"
696 "lock directory=%s\n"
Nikolaus Rath276eda52012-04-25 09:57:19 -0400697 "state directory=%s\n"
Peter Wu7912d042014-11-03 11:52:10 +0100698 "cache directory=%s\n"
Michael Bueschb87b8a82014-04-27 14:54:12 +0400699 "ncalrpc dir=%s/ncalrpc\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000700 "log file=%s/log.smbd\n"
701 "smb passwd file=%s/smbpasswd\n"
Michael Bueschc2804ee2013-11-01 12:23:49 +0100702 "security = user\n"
703 "map to guest = Bad User\n"
Peter Wu7912d042014-11-03 11:52:10 +0100704 "load printers = no\n"
705 "printing = bsd\n"
706 "disable spoolss = yes\n"
707 "usershare max shares = 0\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000708 "[qemu]\n"
709 "path=%s\n"
710 "read only=no\n"
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200711 "guest ok=yes\n"
712 "force user=%s\n",
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000713 s->smb_dir,
714 s->smb_dir,
715 s->smb_dir,
716 s->smb_dir,
717 s->smb_dir,
Nikolaus Rath276eda52012-04-25 09:57:19 -0400718 s->smb_dir,
Michael Bueschb87b8a82014-04-27 14:54:12 +0400719 s->smb_dir,
Peter Wu7912d042014-11-03 11:52:10 +0100720 s->smb_dir,
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200721 exported_dir,
722 passwd->pw_name
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000723 );
724 fclose(f);
725
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100726 smb_cmdline = g_strdup_printf("%s -l %s -s %s",
Michael Tokarev44d8d2b2014-10-25 00:29:50 +0400727 CONFIG_SMBD_COMMAND, s->smb_dir, smb_conf);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100728 g_free(smb_conf);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000729
Michael Tokarev5c1e1892013-11-28 23:32:55 +0400730 if (slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 139) < 0 ||
731 slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 445) < 0) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000732 slirp_smb_cleanup(s);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100733 g_free(smb_cmdline);
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200734 error_setg(errp, "Conflicting/invalid smbserver address");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000735 return -1;
736 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100737 g_free(smb_cmdline);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000738 return 0;
739}
740
741/* automatic user mode samba server configuration (legacy interface) */
742int net_slirp_smb(const char *exported_dir)
743{
744 struct in_addr vserver_addr = { .s_addr = 0 };
745
746 if (legacy_smb_export) {
747 fprintf(stderr, "-smb given twice\n");
748 return -1;
749 }
750 legacy_smb_export = exported_dir;
751 if (!QTAILQ_EMPTY(&slirp_stacks)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200752 Error *err = NULL;
753 int res = slirp_smb(QTAILQ_FIRST(&slirp_stacks), exported_dir,
754 vserver_addr, &err);
755 if (res < 0) {
756 error_report_err(err);
757 }
758 return res;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000759 }
760 return 0;
761}
762
763#endif /* !defined(_WIN32) */
764
765struct GuestFwd {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300766 CharBackend hd;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000767 struct in_addr server;
768 int port;
769 Slirp *slirp;
770};
771
772static int guestfwd_can_read(void *opaque)
773{
774 struct GuestFwd *fwd = opaque;
775 return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port);
776}
777
778static void guestfwd_read(void *opaque, const uint8_t *buf, int size)
779{
780 struct GuestFwd *fwd = opaque;
781 slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size);
782}
783
784static int slirp_guestfwd(SlirpState *s, const char *config_str,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200785 int legacy_format, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000786{
787 struct in_addr server = { .s_addr = 0 };
788 struct GuestFwd *fwd;
789 const char *p;
790 char buf[128];
791 char *end;
792 int port;
793
794 p = config_str;
795 if (legacy_format) {
796 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
797 goto fail_syntax;
798 }
799 } else {
800 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
801 goto fail_syntax;
802 }
803 if (strcmp(buf, "tcp") && buf[0] != '\0') {
804 goto fail_syntax;
805 }
806 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
807 goto fail_syntax;
808 }
809 if (buf[0] != '\0' && !inet_aton(buf, &server)) {
810 goto fail_syntax;
811 }
812 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
813 goto fail_syntax;
814 }
815 }
816 port = strtol(buf, &end, 10);
817 if (*end != '\0' || port < 1 || port > 65535) {
818 goto fail_syntax;
819 }
820
Alexander Grafa9899992011-06-04 07:25:59 +0200821 snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000822
Alexander Grafb412eb62012-06-03 09:45:01 +0200823 if ((strlen(p) > 4) && !strncmp(p, "cmd:", 4)) {
824 if (slirp_add_exec(s->slirp, 0, &p[4], &server, port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200825 error_setg(errp, "Conflicting/invalid host:port in guest "
826 "forwarding rule '%s'", config_str);
Alexander Grafb412eb62012-06-03 09:45:01 +0200827 return -1;
828 }
829 } else {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300830 Error *err = NULL;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300831 Chardev *chr = qemu_chr_new(buf, p);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300832
833 if (!chr) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200834 error_setg(errp, "Could not open guest forwarding device '%s'",
835 buf);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300836 return -1;
837 }
838
839 fwd = g_new(struct GuestFwd, 1);
840 qemu_chr_fe_init(&fwd->hd, chr, &err);
841 if (err) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200842 error_propagate(errp, err);
Alexander Grafb412eb62012-06-03 09:45:01 +0200843 g_free(fwd);
844 return -1;
845 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000846
Paolo Bonzinid14fabd2016-10-27 22:04:58 +0200847 if (slirp_add_exec(s->slirp, 3, &fwd->hd, &server, port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200848 error_setg(errp, "Conflicting/invalid host:port in guest "
849 "forwarding rule '%s'", config_str);
Alexander Grafb412eb62012-06-03 09:45:01 +0200850 g_free(fwd);
851 return -1;
852 }
853 fwd->server = server;
854 fwd->port = port;
855 fwd->slirp = s->slirp;
856
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300857 qemu_chr_fe_set_handlers(&fwd->hd, guestfwd_can_read, guestfwd_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +0300858 NULL, NULL, fwd, NULL, true);
Alexander Grafb412eb62012-06-03 09:45:01 +0200859 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000860 return 0;
861
862 fail_syntax:
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200863 error_setg(errp, "Invalid guest forwarding rule '%s'", config_str);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000864 return -1;
865}
866
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100867void hmp_info_usernet(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000868{
869 SlirpState *s;
870
871 QTAILQ_FOREACH(s, &slirp_stacks, entry) {
Stefan Hajnoczi90d87a32012-07-24 16:35:06 +0100872 int id;
873 bool got_vlan_id = net_hub_id_for_client(&s->nc, &id) == 0;
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000874 monitor_printf(mon, "VLAN %d (%s):\n",
Stefan Hajnoczi90d87a32012-07-24 16:35:06 +0100875 got_vlan_id ? id : -1,
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000876 s->nc.name);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000877 slirp_connection_info(s->slirp, mon);
878 }
879}
880
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200881static void
882net_init_slirp_configs(const StringList *fwd, int flags)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000883{
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200884 while (fwd) {
885 struct slirp_config_str *config;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000886
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200887 config = g_malloc0(sizeof(*config));
888 pstrcpy(config->str, sizeof(config->str), fwd->value->str);
889 config->flags = flags;
890 config->next = slirp_configs;
891 slirp_configs = config;
892
893 fwd = fwd->next;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000894 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000895}
896
Klaus Stengel63d29602012-10-27 19:53:39 +0200897static const char **slirp_dnssearch(const StringList *dnsname)
898{
899 const StringList *c = dnsname;
900 size_t i = 0, num_opts = 0;
901 const char **ret;
902
903 while (c) {
904 num_opts++;
905 c = c->next;
906 }
907
908 if (num_opts == 0) {
909 return NULL;
910 }
911
912 ret = g_malloc((num_opts + 1) * sizeof(*ret));
913 c = dnsname;
914 while (c) {
915 ret[i++] = c->value->str;
916 c = c->next;
917 }
918 ret[i] = NULL;
919 return ret;
920}
921
Kővágó, Zoltáncebea512016-07-13 21:50:12 -0600922int net_init_slirp(const Netdev *netdev, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200923 NetClientState *peer, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000924{
925 struct slirp_config_str *config;
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200926 char *vnet;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000927 int ret;
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200928 const NetdevUserOptions *user;
Klaus Stengel63d29602012-10-27 19:53:39 +0200929 const char **dnssearch;
Samuel Thibault0b11c032016-03-20 12:29:54 +0100930 bool ipv4 = true, ipv6 = true;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000931
Eric Blakef394b2e2016-07-13 21:50:23 -0600932 assert(netdev->type == NET_CLIENT_DRIVER_USER);
933 user = &netdev->u.user;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000934
Samuel Thibault0b11c032016-03-20 12:29:54 +0100935 if ((user->has_ipv6 && user->ipv6 && !user->has_ipv4) ||
936 (user->has_ipv4 && !user->ipv4)) {
937 ipv4 = 0;
938 }
939 if ((user->has_ipv4 && user->ipv4 && !user->has_ipv6) ||
940 (user->has_ipv6 && !user->ipv6)) {
941 ipv6 = 0;
942 }
943
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200944 vnet = user->has_net ? g_strdup(user->net) :
945 user->has_ip ? g_strdup_printf("%s/24", user->ip) :
946 NULL;
Jan Kiszkac54ed5b2011-07-20 12:20:14 +0200947
Klaus Stengel63d29602012-10-27 19:53:39 +0200948 dnssearch = slirp_dnssearch(user->dnssearch);
949
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200950 /* all optional fields are initialized to "all bits zero" */
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000951
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200952 net_init_slirp_configs(user->hostfwd, SLIRP_CFG_HOSTFWD);
953 net_init_slirp_configs(user->guestfwd, 0);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000954
Samuel Thibault0b11c032016-03-20 12:29:54 +0100955 ret = net_slirp_init(peer, "user", name, user->q_restrict,
956 ipv4, vnet, user->host,
957 ipv6, user->ipv6_prefix, user->ipv6_prefixlen,
Samuel Thibaultd8eb3862016-03-25 00:02:58 +0100958 user->ipv6_host, user->hostname, user->tftp,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100959 user->bootfile, user->dhcpstart,
Samuel Thibaultd8eb3862016-03-25 00:02:58 +0100960 user->dns, user->ipv6_dns, user->smb,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200961 user->smbserver, dnssearch, errp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000962
963 while (slirp_configs) {
964 config = slirp_configs;
965 slirp_configs = config->next;
Anthony Liguori7267c092011-08-20 22:09:37 -0500966 g_free(config);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000967 }
968
Anthony Liguori7267c092011-08-20 22:09:37 -0500969 g_free(vnet);
Klaus Stengel63d29602012-10-27 19:53:39 +0200970 g_free(dnssearch);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000971
972 return ret;
973}