blob: f6dc03963af77afb30a0665f671c9ddf923d0f3f [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;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +000088static QTAILQ_HEAD(slirp_stacks, SlirpState) slirp_stacks =
89 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
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000102void slirp_output(void *opaque, const uint8_t *pkt, int pkt_len)
103{
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
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100143static int net_slirp_init(NetClientState *peer, const char *model,
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000144 const char *name, int restricted,
Samuel Thibault0b11c032016-03-20 12:29:54 +0100145 bool ipv4, const char *vnetwork, const char *vhost,
146 bool ipv6, const char *vprefix6, int vprefix6_len,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100147 const char *vhost6,
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000148 const char *vhostname, const char *tftp_export,
149 const char *bootfile, const char *vdhcp_start,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100150 const char *vnameserver, const char *vnameserver6,
151 const char *smb_export, const char *vsmbserver,
Benjamin Drungf18d1372018-02-27 17:06:01 +0100152 const char **dnssearch, const char *vdomainname,
Fam Zheng0fca92b2018-09-14 15:26:16 +0800153 const char *tftp_server_name,
Benjamin Drungf18d1372018-02-27 17:06:01 +0100154 Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000155{
156 /* default settings according to historic slirp */
157 struct in_addr net = { .s_addr = htonl(0x0a000200) }; /* 10.0.2.0 */
158 struct in_addr mask = { .s_addr = htonl(0xffffff00) }; /* 255.255.255.0 */
159 struct in_addr host = { .s_addr = htonl(0x0a000202) }; /* 10.0.2.2 */
160 struct in_addr dhcp = { .s_addr = htonl(0x0a00020f) }; /* 10.0.2.15 */
161 struct in_addr dns = { .s_addr = htonl(0x0a000203) }; /* 10.0.2.3 */
Yann Bordenave7aac5312016-03-15 10:31:22 +0100162 struct in6_addr ip6_prefix;
163 struct in6_addr ip6_host;
164 struct in6_addr ip6_dns;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000165#ifndef _WIN32
166 struct in_addr smbsrv = { .s_addr = 0 };
167#endif
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100168 NetClientState *nc;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000169 SlirpState *s;
170 char buf[20];
171 uint32_t addr;
172 int shift;
173 char *end;
174 struct slirp_config_str *config;
175
Samuel Thibault0b11c032016-03-20 12:29:54 +0100176 if (!ipv4 && (vnetwork || vhost || vnameserver)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200177 error_setg(errp, "IPv4 disabled but netmask/host/dns provided");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100178 return -1;
179 }
180
181 if (!ipv6 && (vprefix6 || vhost6 || vnameserver6)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200182 error_setg(errp, "IPv6 disabled but prefix/host6/dns6 provided");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100183 return -1;
184 }
185
186 if (!ipv4 && !ipv6) {
187 /* It doesn't make sense to disable both */
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200188 error_setg(errp, "IPv4 and IPv6 disabled");
Samuel Thibault0b11c032016-03-20 12:29:54 +0100189 return -1;
190 }
191
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000192 if (vnetwork) {
193 if (get_str_sep(buf, sizeof(buf), &vnetwork, '/') < 0) {
194 if (!inet_aton(vnetwork, &net)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200195 error_setg(errp, "Failed to parse netmask");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000196 return -1;
197 }
198 addr = ntohl(net.s_addr);
199 if (!(addr & 0x80000000)) {
200 mask.s_addr = htonl(0xff000000); /* class A */
201 } else if ((addr & 0xfff00000) == 0xac100000) {
202 mask.s_addr = htonl(0xfff00000); /* priv. 172.16.0.0/12 */
203 } else if ((addr & 0xc0000000) == 0x80000000) {
204 mask.s_addr = htonl(0xffff0000); /* class B */
205 } else if ((addr & 0xffff0000) == 0xc0a80000) {
206 mask.s_addr = htonl(0xffff0000); /* priv. 192.168.0.0/16 */
207 } else if ((addr & 0xffff0000) == 0xc6120000) {
208 mask.s_addr = htonl(0xfffe0000); /* tests 198.18.0.0/15 */
209 } else if ((addr & 0xe0000000) == 0xe0000000) {
210 mask.s_addr = htonl(0xffffff00); /* class C */
211 } else {
212 mask.s_addr = htonl(0xfffffff0); /* multicast/reserved */
213 }
214 } else {
215 if (!inet_aton(buf, &net)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200216 error_setg(errp, "Failed to parse netmask");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000217 return -1;
218 }
219 shift = strtol(vnetwork, &end, 10);
220 if (*end != '\0') {
221 if (!inet_aton(vnetwork, &mask)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200222 error_setg(errp,
223 "Failed to parse netmask (trailing chars)");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000224 return -1;
225 }
226 } else if (shift < 4 || shift > 32) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200227 error_setg(errp,
228 "Invalid netmask provided (must be in range 4-32)");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000229 return -1;
230 } else {
231 mask.s_addr = htonl(0xffffffff << (32 - shift));
232 }
233 }
234 net.s_addr &= mask.s_addr;
235 host.s_addr = net.s_addr | (htonl(0x0202) & ~mask.s_addr);
236 dhcp.s_addr = net.s_addr | (htonl(0x020f) & ~mask.s_addr);
237 dns.s_addr = net.s_addr | (htonl(0x0203) & ~mask.s_addr);
238 }
239
240 if (vhost && !inet_aton(vhost, &host)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200241 error_setg(errp, "Failed to parse host");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000242 return -1;
243 }
244 if ((host.s_addr & mask.s_addr) != net.s_addr) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200245 error_setg(errp, "Host doesn't belong to network");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000246 return -1;
247 }
248
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000249 if (vnameserver && !inet_aton(vnameserver, &dns)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200250 error_setg(errp, "Failed to parse DNS");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000251 return -1;
252 }
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200253 if ((dns.s_addr & mask.s_addr) != net.s_addr) {
254 error_setg(errp, "DNS doesn't belong to network");
255 return -1;
256 }
257 if (dns.s_addr == host.s_addr) {
258 error_setg(errp, "DNS must be different from host");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000259 return -1;
260 }
261
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200262 if (vdhcp_start && !inet_aton(vdhcp_start, &dhcp)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200263 error_setg(errp, "Failed to parse DHCP start address");
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200264 return -1;
265 }
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200266 if ((dhcp.s_addr & mask.s_addr) != net.s_addr) {
267 error_setg(errp, "DHCP doesn't belong to network");
268 return -1;
269 }
270 if (dhcp.s_addr == host.s_addr || dhcp.s_addr == dns.s_addr) {
271 error_setg(errp, "DNS must be different from host and DNS");
Bas van Sisseren68756ba2013-06-03 15:11:49 +0200272 return -1;
273 }
274
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000275#ifndef _WIN32
276 if (vsmbserver && !inet_aton(vsmbserver, &smbsrv)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200277 error_setg(errp, "Failed to parse SMB address");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000278 return -1;
279 }
280#endif
281
Yann Bordenave7aac5312016-03-15 10:31:22 +0100282#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
283 /* No inet_pton helper before Vista... */
284 if (vprefix6) {
285 /* Unsupported */
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200286 error_setg(errp, "IPv6 prefix not supported");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100287 return -1;
288 }
289 memset(&ip6_prefix, 0, sizeof(ip6_prefix));
290 ip6_prefix.s6_addr[0] = 0xfe;
291 ip6_prefix.s6_addr[1] = 0xc0;
292#else
293 if (!vprefix6) {
294 vprefix6 = "fec0::";
295 }
296 if (!inet_pton(AF_INET6, vprefix6, &ip6_prefix)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200297 error_setg(errp, "Failed to parse IPv6 prefix");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100298 return -1;
299 }
300#endif
301
302 if (!vprefix6_len) {
303 vprefix6_len = 64;
304 }
305 if (vprefix6_len < 0 || vprefix6_len > 126) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200306 error_setg(errp,
307 "Invalid prefix provided (prefix len must be in range 0-126");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100308 return -1;
309 }
310
311 if (vhost6) {
312#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200313 error_setg(errp, "IPv6 host not supported");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100314 return -1;
315#else
316 if (!inet_pton(AF_INET6, vhost6, &ip6_host)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200317 error_setg(errp, "Failed to parse IPv6 host");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100318 return -1;
319 }
320 if (!in6_equal_net(&ip6_prefix, &ip6_host, vprefix6_len)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200321 error_setg(errp, "IPv6 Host doesn't belong to network");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100322 return -1;
323 }
324#endif
325 } else {
326 ip6_host = ip6_prefix;
327 ip6_host.s6_addr[15] |= 2;
328 }
329
330 if (vnameserver6) {
331#if defined(_WIN32) && (_WIN32_WINNT < 0x0600)
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200332 error_setg(errp, "IPv6 DNS not supported");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100333 return -1;
334#else
335 if (!inet_pton(AF_INET6, vnameserver6, &ip6_dns)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200336 error_setg(errp, "Failed to parse IPv6 DNS");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100337 return -1;
338 }
339 if (!in6_equal_net(&ip6_prefix, &ip6_dns, vprefix6_len)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200340 error_setg(errp, "IPv6 DNS doesn't belong to network");
Yann Bordenave7aac5312016-03-15 10:31:22 +0100341 return -1;
342 }
343#endif
344 } else {
345 ip6_dns = ip6_prefix;
346 ip6_dns.s6_addr[15] |= 3;
347 }
348
Benjamin Drungf18d1372018-02-27 17:06:01 +0100349 if (vdomainname && !*vdomainname) {
350 error_setg(errp, "'domainname' parameter cannot be empty");
351 return -1;
352 }
353
Fam Zheng6e157a02018-09-14 15:26:15 +0800354 if (vdomainname && strlen(vdomainname) > 255) {
355 error_setg(errp, "'domainname' parameter cannot exceed 255 bytes");
356 return -1;
357 }
358
359 if (vhostname && strlen(vhostname) > 255) {
360 error_setg(errp, "'vhostname' parameter cannot exceed 255 bytes");
361 return -1;
362 }
Yann Bordenave7aac5312016-03-15 10:31:22 +0100363
Fam Zheng0fca92b2018-09-14 15:26:16 +0800364 if (tftp_server_name && strlen(tftp_server_name) > 255) {
365 error_setg(errp, "'tftp-server-name' parameter cannot exceed 255 bytes");
366 return -1;
367 }
368
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,
Fam Zheng0fca92b2018-09-14 15:26:16 +0800379 vhostname, tftp_server_name,
380 tftp_export, bootfile, dhcp,
Benjamin Drungf18d1372018-02-27 17:06:01 +0100381 dns, ip6_dns, dnssearch, vdomainname, s);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000382 QTAILQ_INSERT_TAIL(&slirp_stacks, s, entry);
383
384 for (config = slirp_configs; config; config = config->next) {
385 if (config->flags & SLIRP_CFG_HOSTFWD) {
Thomas Huthd18572d2018-08-22 15:43:30 +0200386 if (slirp_hostfwd(s, config->str, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000387 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200388 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000389 } else {
Thomas Huthd18572d2018-08-22 15:43:30 +0200390 if (slirp_guestfwd(s, config->str, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000391 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200392 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000393 }
394 }
395#ifndef _WIN32
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000396 if (smb_export) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200397 if (slirp_smb(s, smb_export, smbsrv, errp) < 0) {
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000398 goto error;
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200399 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000400 }
401#endif
402
Paolo Bonzinif6c2e662016-07-12 09:57:12 +0200403 s->exit_notifier.notify = slirp_smb_exit;
404 qemu_add_exit_notifier(&s->exit_notifier);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000405 return 0;
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000406
407error:
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +0100408 qemu_del_net_client(nc);
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000409 return -1;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000410}
411
Thomas Huth93653062018-01-11 21:02:40 +0100412static SlirpState *slirp_lookup(Monitor *mon, const char *hub_id,
413 const char *name)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000414{
Thomas Huth93653062018-01-11 21:02:40 +0100415 if (name) {
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100416 NetClientState *nc;
Thomas Huth93653062018-01-11 21:02:40 +0100417 if (hub_id) {
418 nc = net_hub_find_client_by_name(strtol(hub_id, NULL, 0), name);
419 if (!nc) {
Thomas Huth442da402018-04-30 20:02:24 +0200420 monitor_printf(mon, "unrecognized (hub-id, stackname) pair\n");
Thomas Huth93653062018-01-11 21:02:40 +0100421 return NULL;
422 }
Thomas Huth68cb29e2018-09-20 10:22:27 +0200423 warn_report("Using 'hub-id' is deprecated, specify the netdev id "
424 "directly instead");
Thomas Huth93653062018-01-11 21:02:40 +0100425 } else {
426 nc = qemu_find_netdev(name);
427 if (!nc) {
428 monitor_printf(mon, "unrecognized netdev id '%s'\n", name);
429 return NULL;
430 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000431 }
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000432 if (strcmp(nc->model, "user")) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000433 monitor_printf(mon, "invalid device specified\n");
434 return NULL;
435 }
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000436 return DO_UPCAST(SlirpState, nc, nc);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000437 } else {
438 if (QTAILQ_EMPTY(&slirp_stacks)) {
439 monitor_printf(mon, "user mode network stack not in use\n");
440 return NULL;
441 }
442 return QTAILQ_FIRST(&slirp_stacks);
443 }
444}
445
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100446void hmp_hostfwd_remove(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000447{
448 struct in_addr host_addr = { .s_addr = INADDR_ANY };
449 int host_port;
Markus Armbrustere30e5eb2011-11-16 15:45:59 +0100450 char buf[256];
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000451 const char *src_str, *p;
452 SlirpState *s;
453 int is_udp = 0;
454 int err;
455 const char *arg1 = qdict_get_str(qdict, "arg1");
456 const char *arg2 = qdict_get_try_str(qdict, "arg2");
457 const char *arg3 = qdict_get_try_str(qdict, "arg3");
458
Thomas Huth93653062018-01-11 21:02:40 +0100459 if (arg3) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000460 s = slirp_lookup(mon, arg1, arg2);
461 src_str = arg3;
Thomas Huth93653062018-01-11 21:02:40 +0100462 } else if (arg2) {
463 s = slirp_lookup(mon, NULL, arg1);
464 src_str = arg2;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000465 } else {
466 s = slirp_lookup(mon, NULL, NULL);
467 src_str = arg1;
468 }
469 if (!s) {
470 return;
471 }
472
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000473 p = src_str;
Markus Armbrustere30e5eb2011-11-16 15:45:59 +0100474 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
475 goto fail_syntax;
476 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000477
478 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
479 is_udp = 0;
480 } else if (!strcmp(buf, "udp")) {
481 is_udp = 1;
482 } else {
483 goto fail_syntax;
484 }
485
486 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
487 goto fail_syntax;
488 }
489 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
490 goto fail_syntax;
491 }
492
Nia Alarie1fb3f7f2018-03-16 14:39:21 +0000493 if (qemu_strtoi(p, NULL, 10, &host_port)) {
494 goto fail_syntax;
495 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000496
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
Thomas Huthd18572d2018-08-22 15:43:30 +0200507static int slirp_hostfwd(SlirpState *s, const char *redir_str, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000508{
509 struct in_addr host_addr = { .s_addr = INADDR_ANY };
510 struct in_addr guest_addr = { .s_addr = 0 };
511 int host_port, guest_port;
512 const char *p;
513 char buf[256];
514 int is_udp;
515 char *end;
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100516 const char *fail_reason = "Unknown reason";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000517
518 p = redir_str;
519 if (!p || get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100520 fail_reason = "No : separators";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000521 goto fail_syntax;
522 }
523 if (!strcmp(buf, "tcp") || buf[0] == '\0') {
524 is_udp = 0;
525 } else if (!strcmp(buf, "udp")) {
526 is_udp = 1;
527 } else {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100528 fail_reason = "Bad protocol name";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000529 goto fail_syntax;
530 }
531
Thomas Huthd18572d2018-08-22 15:43:30 +0200532 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
533 fail_reason = "Missing : separator";
534 goto fail_syntax;
535 }
536 if (buf[0] != '\0' && !inet_aton(buf, &host_addr)) {
537 fail_reason = "Bad host address";
538 goto fail_syntax;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000539 }
540
Thomas Huthd18572d2018-08-22 15:43:30 +0200541 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100542 fail_reason = "Bad host port separator";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000543 goto fail_syntax;
544 }
545 host_port = strtol(buf, &end, 0);
Vincent Bernat0bed71e2017-02-25 22:31:58 +0100546 if (*end != '\0' || host_port < 0 || host_port > 65535) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100547 fail_reason = "Bad host port";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000548 goto fail_syntax;
549 }
550
551 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100552 fail_reason = "Missing guest address";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000553 goto fail_syntax;
554 }
555 if (buf[0] != '\0' && !inet_aton(buf, &guest_addr)) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100556 fail_reason = "Bad guest address";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000557 goto fail_syntax;
558 }
559
560 guest_port = strtol(p, &end, 0);
561 if (*end != '\0' || guest_port < 1 || guest_port > 65535) {
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100562 fail_reason = "Bad guest port";
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000563 goto fail_syntax;
564 }
565
566 if (slirp_add_hostfwd(s->slirp, is_udp, host_addr, host_port, guest_addr,
567 guest_port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200568 error_setg(errp, "Could not set up host forwarding rule '%s'",
569 redir_str);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000570 return -1;
571 }
572 return 0;
573
574 fail_syntax:
Dr. David Alan Gilbert0e7e4fb2017-09-08 16:53:59 +0100575 error_setg(errp, "Invalid host forwarding rule '%s' (%s)", redir_str,
576 fail_reason);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000577 return -1;
578}
579
Markus Armbruster3e5a50d2015-02-06 13:55:43 +0100580void hmp_hostfwd_add(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000581{
582 const char *redir_str;
583 SlirpState *s;
584 const char *arg1 = qdict_get_str(qdict, "arg1");
585 const char *arg2 = qdict_get_try_str(qdict, "arg2");
586 const char *arg3 = qdict_get_try_str(qdict, "arg3");
587
Thomas Huth93653062018-01-11 21:02:40 +0100588 if (arg3) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000589 s = slirp_lookup(mon, arg1, arg2);
590 redir_str = arg3;
Thomas Huth93653062018-01-11 21:02:40 +0100591 } else if (arg2) {
592 s = slirp_lookup(mon, NULL, arg1);
593 redir_str = arg2;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000594 } else {
595 s = slirp_lookup(mon, NULL, NULL);
596 redir_str = arg1;
597 }
598 if (s) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200599 Error *err = NULL;
Thomas Huthd18572d2018-08-22 15:43:30 +0200600 if (slirp_hostfwd(s, redir_str, &err) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200601 error_report_err(err);
602 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000603 }
604
605}
606
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000607#ifndef _WIN32
608
609/* automatic user mode samba server configuration */
610static void slirp_smb_cleanup(SlirpState *s)
611{
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100612 int ret;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000613
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100614 if (s->smb_dir) {
615 gchar *cmd = g_strdup_printf("rm -rf %s", s->smb_dir);
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100616 ret = system(cmd);
Juan Quintela24ac07d2010-03-04 10:00:31 +0100617 if (ret == -1 || !WIFEXITED(ret)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100618 error_report("'%s' failed.", cmd);
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100619 } else if (WEXITSTATUS(ret)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100620 error_report("'%s' failed. Error code: %d",
621 cmd, WEXITSTATUS(ret));
Kirill A. Shutemov5a01e992010-01-20 00:56:16 +0100622 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100623 g_free(cmd);
624 g_free(s->smb_dir);
625 s->smb_dir = NULL;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000626 }
627}
628
629static int slirp_smb(SlirpState* s, const char *exported_dir,
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200630 struct in_addr vserver_addr, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000631{
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100632 char *smb_conf;
633 char *smb_cmdline;
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200634 struct passwd *passwd;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000635 FILE *f;
636
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200637 passwd = getpwuid(geteuid());
638 if (!passwd) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200639 error_setg(errp, "Failed to retrieve user name");
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200640 return -1;
641 }
642
Dunrong Huang927d8112012-07-06 14:04:43 +0800643 if (access(CONFIG_SMBD_COMMAND, F_OK)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200644 error_setg(errp, "Could not find '%s', please install it",
645 CONFIG_SMBD_COMMAND);
Dunrong Huang927d8112012-07-06 14:04:43 +0800646 return -1;
647 }
648
649 if (access(exported_dir, R_OK | X_OK)) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200650 error_setg(errp, "Error accessing shared directory '%s': %s",
651 exported_dir, strerror(errno));
Dunrong Huang927d8112012-07-06 14:04:43 +0800652 return -1;
653 }
654
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100655 s->smb_dir = g_dir_make_tmp("qemu-smb.XXXXXX", NULL);
656 if (!s->smb_dir) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200657 error_setg(errp, "Could not create samba server dir");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000658 return -1;
659 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100660 smb_conf = g_strdup_printf("%s/%s", s->smb_dir, "smb.conf");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000661
662 f = fopen(smb_conf, "w");
663 if (!f) {
664 slirp_smb_cleanup(s);
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200665 error_setg(errp,
666 "Could not create samba server configuration file '%s'",
667 smb_conf);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100668 g_free(smb_conf);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000669 return -1;
670 }
671 fprintf(f,
672 "[global]\n"
673 "private dir=%s\n"
Peter Wu7912d042014-11-03 11:52:10 +0100674 "interfaces=127.0.0.1\n"
675 "bind interfaces only=yes\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000676 "pid directory=%s\n"
677 "lock directory=%s\n"
Nikolaus Rath276eda52012-04-25 09:57:19 -0400678 "state directory=%s\n"
Peter Wu7912d042014-11-03 11:52:10 +0100679 "cache directory=%s\n"
Michael Bueschb87b8a82014-04-27 14:54:12 +0400680 "ncalrpc dir=%s/ncalrpc\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000681 "log file=%s/log.smbd\n"
682 "smb passwd file=%s/smbpasswd\n"
Michael Bueschc2804ee2013-11-01 12:23:49 +0100683 "security = user\n"
684 "map to guest = Bad User\n"
Peter Wu7912d042014-11-03 11:52:10 +0100685 "load printers = no\n"
686 "printing = bsd\n"
687 "disable spoolss = yes\n"
688 "usershare max shares = 0\n"
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000689 "[qemu]\n"
690 "path=%s\n"
691 "read only=no\n"
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200692 "guest ok=yes\n"
693 "force user=%s\n",
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000694 s->smb_dir,
695 s->smb_dir,
696 s->smb_dir,
697 s->smb_dir,
698 s->smb_dir,
Nikolaus Rath276eda52012-04-25 09:57:19 -0400699 s->smb_dir,
Michael Bueschb87b8a82014-04-27 14:54:12 +0400700 s->smb_dir,
Peter Wu7912d042014-11-03 11:52:10 +0100701 s->smb_dir,
Jan Kiszka1cb1c5d2012-07-05 19:35:57 +0200702 exported_dir,
703 passwd->pw_name
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000704 );
705 fclose(f);
706
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100707 smb_cmdline = g_strdup_printf("%s -l %s -s %s",
Michael Tokarev44d8d2b2014-10-25 00:29:50 +0400708 CONFIG_SMBD_COMMAND, s->smb_dir, smb_conf);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100709 g_free(smb_conf);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000710
Michael Tokarev5c1e1892013-11-28 23:32:55 +0400711 if (slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 139) < 0 ||
712 slirp_add_exec(s->slirp, 0, smb_cmdline, &vserver_addr, 445) < 0) {
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000713 slirp_smb_cleanup(s);
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100714 g_free(smb_cmdline);
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200715 error_setg(errp, "Conflicting/invalid smbserver address");
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000716 return -1;
717 }
Dr. David Alan Gilbertf95cc8b2017-04-07 15:32:54 +0100718 g_free(smb_cmdline);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000719 return 0;
720}
721
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000722#endif /* !defined(_WIN32) */
723
724struct GuestFwd {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300725 CharBackend hd;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000726 struct in_addr server;
727 int port;
728 Slirp *slirp;
729};
730
731static int guestfwd_can_read(void *opaque)
732{
733 struct GuestFwd *fwd = opaque;
734 return slirp_socket_can_recv(fwd->slirp, fwd->server, fwd->port);
735}
736
737static void guestfwd_read(void *opaque, const uint8_t *buf, int size)
738{
739 struct GuestFwd *fwd = opaque;
740 slirp_socket_recv(fwd->slirp, fwd->server, fwd->port, buf, size);
741}
742
Thomas Huthd18572d2018-08-22 15:43:30 +0200743static int slirp_guestfwd(SlirpState *s, const char *config_str, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000744{
745 struct in_addr server = { .s_addr = 0 };
746 struct GuestFwd *fwd;
747 const char *p;
748 char buf[128];
749 char *end;
750 int port;
751
752 p = config_str;
Thomas Huthd18572d2018-08-22 15:43:30 +0200753 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
754 goto fail_syntax;
755 }
756 if (strcmp(buf, "tcp") && buf[0] != '\0') {
757 goto fail_syntax;
758 }
759 if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) {
760 goto fail_syntax;
761 }
762 if (buf[0] != '\0' && !inet_aton(buf, &server)) {
763 goto fail_syntax;
764 }
765 if (get_str_sep(buf, sizeof(buf), &p, '-') < 0) {
766 goto fail_syntax;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000767 }
768 port = strtol(buf, &end, 10);
769 if (*end != '\0' || port < 1 || port > 65535) {
770 goto fail_syntax;
771 }
772
Alexander Grafa9899992011-06-04 07:25:59 +0200773 snprintf(buf, sizeof(buf), "guestfwd.tcp.%d", port);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000774
Alexander Grafb412eb62012-06-03 09:45:01 +0200775 if ((strlen(p) > 4) && !strncmp(p, "cmd:", 4)) {
776 if (slirp_add_exec(s->slirp, 0, &p[4], &server, port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200777 error_setg(errp, "Conflicting/invalid host:port in guest "
778 "forwarding rule '%s'", config_str);
Alexander Grafb412eb62012-06-03 09:45:01 +0200779 return -1;
780 }
781 } else {
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300782 Error *err = NULL;
Marc-André Lureau95e30b22018-08-22 19:19:42 +0200783 /*
784 * FIXME: sure we want to support implicit
785 * muxed monitors here?
786 */
787 Chardev *chr = qemu_chr_new_mux_mon(buf, p);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300788
789 if (!chr) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200790 error_setg(errp, "Could not open guest forwarding device '%s'",
791 buf);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300792 return -1;
793 }
794
795 fwd = g_new(struct GuestFwd, 1);
796 qemu_chr_fe_init(&fwd->hd, chr, &err);
797 if (err) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200798 error_propagate(errp, err);
Alexander Grafb412eb62012-06-03 09:45:01 +0200799 g_free(fwd);
800 return -1;
801 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000802
Paolo Bonzinid14fabd2016-10-27 22:04:58 +0200803 if (slirp_add_exec(s->slirp, 3, &fwd->hd, &server, port) < 0) {
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200804 error_setg(errp, "Conflicting/invalid host:port in guest "
805 "forwarding rule '%s'", config_str);
Alexander Grafb412eb62012-06-03 09:45:01 +0200806 g_free(fwd);
807 return -1;
808 }
809 fwd->server = server;
810 fwd->port = port;
811 fwd->slirp = s->slirp;
812
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300813 qemu_chr_fe_set_handlers(&fwd->hd, guestfwd_can_read, guestfwd_read,
Anton Nefedov81517ba2017-07-06 15:08:49 +0300814 NULL, NULL, fwd, NULL, true);
Alexander Grafb412eb62012-06-03 09:45:01 +0200815 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000816 return 0;
817
818 fail_syntax:
Hervé Poussineau5c843af2017-07-15 18:43:50 +0200819 error_setg(errp, "Invalid guest forwarding rule '%s'", config_str);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000820 return -1;
821}
822
Markus Armbruster1ce6be22015-02-06 14:18:24 +0100823void hmp_info_usernet(Monitor *mon, const QDict *qdict)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000824{
825 SlirpState *s;
826
827 QTAILQ_FOREACH(s, &slirp_stacks, entry) {
Stefan Hajnoczi90d87a32012-07-24 16:35:06 +0100828 int id;
Thomas Huth442da402018-04-30 20:02:24 +0200829 bool got_hub_id = net_hub_id_for_client(&s->nc, &id) == 0;
830 monitor_printf(mon, "Hub %d (%s):\n",
831 got_hub_id ? id : -1,
Mark McLoughlince20b5b2009-11-25 18:49:06 +0000832 s->nc.name);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000833 slirp_connection_info(s->slirp, mon);
834 }
835}
836
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200837static void
838net_init_slirp_configs(const StringList *fwd, int flags)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000839{
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200840 while (fwd) {
841 struct slirp_config_str *config;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000842
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200843 config = g_malloc0(sizeof(*config));
844 pstrcpy(config->str, sizeof(config->str), fwd->value->str);
845 config->flags = flags;
846 config->next = slirp_configs;
847 slirp_configs = config;
848
849 fwd = fwd->next;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000850 }
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000851}
852
Klaus Stengel63d29602012-10-27 19:53:39 +0200853static const char **slirp_dnssearch(const StringList *dnsname)
854{
855 const StringList *c = dnsname;
856 size_t i = 0, num_opts = 0;
857 const char **ret;
858
859 while (c) {
860 num_opts++;
861 c = c->next;
862 }
863
864 if (num_opts == 0) {
865 return NULL;
866 }
867
868 ret = g_malloc((num_opts + 1) * sizeof(*ret));
869 c = dnsname;
870 while (c) {
871 ret[i++] = c->value->str;
872 c = c->next;
873 }
874 ret[i] = NULL;
875 return ret;
876}
877
Kővágó, Zoltáncebea512016-07-13 21:50:12 -0600878int net_init_slirp(const Netdev *netdev, const char *name,
Markus Armbrustera30ecde2015-05-15 13:58:50 +0200879 NetClientState *peer, Error **errp)
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000880{
881 struct slirp_config_str *config;
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200882 char *vnet;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000883 int ret;
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200884 const NetdevUserOptions *user;
Klaus Stengel63d29602012-10-27 19:53:39 +0200885 const char **dnssearch;
Samuel Thibault0b11c032016-03-20 12:29:54 +0100886 bool ipv4 = true, ipv6 = true;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000887
Eric Blakef394b2e2016-07-13 21:50:23 -0600888 assert(netdev->type == NET_CLIENT_DRIVER_USER);
889 user = &netdev->u.user;
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000890
Samuel Thibault0b11c032016-03-20 12:29:54 +0100891 if ((user->has_ipv6 && user->ipv6 && !user->has_ipv4) ||
892 (user->has_ipv4 && !user->ipv4)) {
893 ipv4 = 0;
894 }
895 if ((user->has_ipv4 && user->ipv4 && !user->has_ipv6) ||
896 (user->has_ipv6 && !user->ipv6)) {
897 ipv6 = 0;
898 }
899
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200900 vnet = user->has_net ? g_strdup(user->net) :
901 user->has_ip ? g_strdup_printf("%s/24", user->ip) :
902 NULL;
Jan Kiszkac54ed5b2011-07-20 12:20:14 +0200903
Klaus Stengel63d29602012-10-27 19:53:39 +0200904 dnssearch = slirp_dnssearch(user->dnssearch);
905
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200906 /* all optional fields are initialized to "all bits zero" */
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000907
Laszlo Ersek094f15c2012-07-17 16:17:16 +0200908 net_init_slirp_configs(user->hostfwd, SLIRP_CFG_HOSTFWD);
909 net_init_slirp_configs(user->guestfwd, 0);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000910
Samuel Thibault0b11c032016-03-20 12:29:54 +0100911 ret = net_slirp_init(peer, "user", name, user->q_restrict,
912 ipv4, vnet, user->host,
913 ipv6, user->ipv6_prefix, user->ipv6_prefixlen,
Samuel Thibaultd8eb3862016-03-25 00:02:58 +0100914 user->ipv6_host, user->hostname, user->tftp,
Yann Bordenave7aac5312016-03-15 10:31:22 +0100915 user->bootfile, user->dhcpstart,
Samuel Thibaultd8eb3862016-03-25 00:02:58 +0100916 user->dns, user->ipv6_dns, user->smb,
Fam Zheng0fca92b2018-09-14 15:26:16 +0800917 user->smbserver, dnssearch, user->domainname,
918 user->tftp_server_name, errp);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000919
920 while (slirp_configs) {
921 config = slirp_configs;
922 slirp_configs = config->next;
Anthony Liguori7267c092011-08-20 22:09:37 -0500923 g_free(config);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000924 }
925
Anthony Liguori7267c092011-08-20 22:09:37 -0500926 g_free(vnet);
Klaus Stengel63d29602012-10-27 19:53:39 +0200927 g_free(dnssearch);
Mark McLoughlin68ac40d2009-11-25 18:48:54 +0000928
929 return ret;
930}