blob: 35f819afb7befa900412e0c1134db6440d3dd8a7 [file] [log] [blame]
bellardd75a0b92008-10-17 17:31:57 +00001/*
2 * libslirp glue
3 *
4 * Copyright (c) 2004-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 */
aliguorie1c5a2b2009-01-08 19:18:21 +000024#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010025#include "qemu/timer.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020026#include "sysemu/char.h"
bellardf0cbd3e2004-04-22 00:10:48 +000027#include "slirp.h"
aliguori062e5522009-01-08 19:27:07 +000028#include "hw/hw.h"
bellardf0cbd3e2004-04-22 00:10:48 +000029
bellardf0cbd3e2004-04-22 00:10:48 +000030/* host loopback address */
31struct in_addr loopback_addr;
Anders Waldenborg648cd332012-07-13 22:54:17 +020032/* host loopback network mask */
Anthony Liguori0b8db8f2012-08-06 19:31:55 -050033unsigned long loopback_mask;
bellardf0cbd3e2004-04-22 00:10:48 +000034
Jan Kiszkaa13a4122009-06-24 14:42:28 +020035/* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +020036static const uint8_t special_ethaddr[ETH_ALEN] = {
Jan Kiszkaa13a4122009-06-24 14:42:28 +020037 0x52, 0x55, 0x00, 0x00, 0x00, 0x00
bellardf0cbd3e2004-04-22 00:10:48 +000038};
39
Jan Kiszkaf1d99bb2009-06-24 14:42:30 +020040u_int curtime;
Jan Kiszkaf1d99bb2009-06-24 14:42:30 +020041
Blue Swirl72cf2d42009-09-12 07:36:22 +000042static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances =
43 QTAILQ_HEAD_INITIALIZER(slirp_instances);
pbrook115defd2006-04-16 11:06:58 +000044
Stefan Weil9e3a95e2009-08-31 16:29:34 +020045static struct in_addr dns_addr;
46static u_int dns_addr_time;
Ed Swierkdf7a86e2009-08-20 19:00:31 -070047
Liu Ping Fan9b0ca6c2013-08-25 10:01:20 +080048#define TIMEOUT_FAST 2 /* milliseconds */
49#define TIMEOUT_SLOW 499 /* milliseconds */
50/* for the aging of certain requests like DNS */
51#define TIMEOUT_DEFAULT 1000 /* milliseconds */
52
bellardf0cbd3e2004-04-22 00:10:48 +000053#ifdef _WIN32
54
Ed Swierkdf7a86e2009-08-20 19:00:31 -070055int get_dns_addr(struct in_addr *pdns_addr)
bellardf0cbd3e2004-04-22 00:10:48 +000056{
bellard379ff532004-07-12 22:33:07 +000057 FIXED_INFO *FixedInfo=NULL;
58 ULONG BufLen;
59 DWORD ret;
60 IP_ADDR_STRING *pIPAddr;
61 struct in_addr tmp_addr;
ths3b46e622007-09-17 08:09:54 +000062
Liu Ping Fan9b0ca6c2013-08-25 10:01:20 +080063 if (dns_addr.s_addr != 0 && (curtime - dns_addr_time) < TIMEOUT_DEFAULT) {
Ed Swierkdf7a86e2009-08-20 19:00:31 -070064 *pdns_addr = dns_addr;
65 return 0;
66 }
67
bellard379ff532004-07-12 22:33:07 +000068 FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
69 BufLen = sizeof(FIXED_INFO);
ths3b46e622007-09-17 08:09:54 +000070
bellard379ff532004-07-12 22:33:07 +000071 if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen)) {
72 if (FixedInfo) {
73 GlobalFree(FixedInfo);
74 FixedInfo = NULL;
75 }
76 FixedInfo = GlobalAlloc(GPTR, BufLen);
77 }
ths5fafdf22007-09-16 21:08:06 +000078
bellard379ff532004-07-12 22:33:07 +000079 if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS) {
80 printf("GetNetworkParams failed. ret = %08x\n", (u_int)ret );
81 if (FixedInfo) {
82 GlobalFree(FixedInfo);
83 FixedInfo = NULL;
84 }
85 return -1;
86 }
ths3b46e622007-09-17 08:09:54 +000087
bellard379ff532004-07-12 22:33:07 +000088 pIPAddr = &(FixedInfo->DnsServerList);
89 inet_aton(pIPAddr->IpAddress.String, &tmp_addr);
90 *pdns_addr = tmp_addr;
Ed Swierkdf7a86e2009-08-20 19:00:31 -070091 dns_addr = tmp_addr;
92 dns_addr_time = curtime;
bellard379ff532004-07-12 22:33:07 +000093 if (FixedInfo) {
94 GlobalFree(FixedInfo);
95 FixedInfo = NULL;
96 }
97 return 0;
bellardf0cbd3e2004-04-22 00:10:48 +000098}
99
Jan Kiszkadf461892009-06-24 14:42:30 +0200100static void winsock_cleanup(void)
101{
102 WSACleanup();
103}
104
bellardf0cbd3e2004-04-22 00:10:48 +0000105#else
106
Blue Swirl1e6eec82009-09-05 10:14:07 +0000107static struct stat dns_addr_stat;
Ed Swierkdf7a86e2009-08-20 19:00:31 -0700108
109int get_dns_addr(struct in_addr *pdns_addr)
bellardf0cbd3e2004-04-22 00:10:48 +0000110{
111 char buff[512];
blueswir1363a37d2008-08-21 17:58:08 +0000112 char buff2[257];
bellardf0cbd3e2004-04-22 00:10:48 +0000113 FILE *f;
114 int found = 0;
115 struct in_addr tmp_addr;
ths3b46e622007-09-17 08:09:54 +0000116
Ed Swierkdf7a86e2009-08-20 19:00:31 -0700117 if (dns_addr.s_addr != 0) {
118 struct stat old_stat;
Liu Ping Fan9b0ca6c2013-08-25 10:01:20 +0800119 if ((curtime - dns_addr_time) < TIMEOUT_DEFAULT) {
Ed Swierkdf7a86e2009-08-20 19:00:31 -0700120 *pdns_addr = dns_addr;
121 return 0;
122 }
123 old_stat = dns_addr_stat;
124 if (stat("/etc/resolv.conf", &dns_addr_stat) != 0)
125 return -1;
126 if ((dns_addr_stat.st_dev == old_stat.st_dev)
127 && (dns_addr_stat.st_ino == old_stat.st_ino)
128 && (dns_addr_stat.st_size == old_stat.st_size)
129 && (dns_addr_stat.st_mtime == old_stat.st_mtime)) {
130 *pdns_addr = dns_addr;
131 return 0;
132 }
133 }
134
bellardf0cbd3e2004-04-22 00:10:48 +0000135 f = fopen("/etc/resolv.conf", "r");
136 if (!f)
137 return -1;
138
blueswir131a60e22007-10-26 18:42:59 +0000139#ifdef DEBUG
Cole Robinson02d16082014-03-21 19:42:20 -0400140 fprintf(stderr, "IP address of your DNS(s): ");
blueswir131a60e22007-10-26 18:42:59 +0000141#endif
bellardf0cbd3e2004-04-22 00:10:48 +0000142 while (fgets(buff, 512, f) != NULL) {
143 if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) {
144 if (!inet_aton(buff2, &tmp_addr))
145 continue;
bellardf0cbd3e2004-04-22 00:10:48 +0000146 /* If it's the first one, set it to dns_addr */
Ed Swierkdf7a86e2009-08-20 19:00:31 -0700147 if (!found) {
bellardf0cbd3e2004-04-22 00:10:48 +0000148 *pdns_addr = tmp_addr;
Ed Swierkdf7a86e2009-08-20 19:00:31 -0700149 dns_addr = tmp_addr;
150 dns_addr_time = curtime;
151 }
blueswir131a60e22007-10-26 18:42:59 +0000152#ifdef DEBUG
bellardf0cbd3e2004-04-22 00:10:48 +0000153 else
Cole Robinson02d16082014-03-21 19:42:20 -0400154 fprintf(stderr, ", ");
blueswir131a60e22007-10-26 18:42:59 +0000155#endif
bellardf0cbd3e2004-04-22 00:10:48 +0000156 if (++found > 3) {
blueswir131a60e22007-10-26 18:42:59 +0000157#ifdef DEBUG
Cole Robinson02d16082014-03-21 19:42:20 -0400158 fprintf(stderr, "(more)");
blueswir131a60e22007-10-26 18:42:59 +0000159#endif
bellardf0cbd3e2004-04-22 00:10:48 +0000160 break;
blueswir131a60e22007-10-26 18:42:59 +0000161 }
162#ifdef DEBUG
163 else
Cole Robinson02d16082014-03-21 19:42:20 -0400164 fprintf(stderr, "%s", inet_ntoa(tmp_addr));
blueswir131a60e22007-10-26 18:42:59 +0000165#endif
bellardf0cbd3e2004-04-22 00:10:48 +0000166 }
167 }
bellard1d43a712004-07-05 21:18:42 +0000168 fclose(f);
bellardf0cbd3e2004-04-22 00:10:48 +0000169 if (!found)
170 return -1;
171 return 0;
172}
173
174#endif
175
Jan Kiszkadf461892009-06-24 14:42:30 +0200176static void slirp_init_once(void)
bellard379ff532004-07-12 22:33:07 +0000177{
Jan Kiszkadf461892009-06-24 14:42:30 +0200178 static int initialized;
Jan Kiszkadf461892009-06-24 14:42:30 +0200179#ifdef _WIN32
180 WSADATA Data;
bellard379ff532004-07-12 22:33:07 +0000181#endif
182
Jan Kiszkadf461892009-06-24 14:42:30 +0200183 if (initialized) {
184 return;
185 }
186 initialized = 1;
187
188#ifdef _WIN32
189 WSAStartup(MAKEWORD(2,0), &Data);
190 atexit(winsock_cleanup);
191#endif
192
193 loopback_addr.s_addr = htonl(INADDR_LOOPBACK);
Anders Waldenborg648cd332012-07-13 22:54:17 +0200194 loopback_mask = htonl(IN_CLASSA_NET);
Jan Kiszkadf461892009-06-24 14:42:30 +0200195}
196
aliguori062e5522009-01-08 19:27:07 +0000197static void slirp_state_save(QEMUFile *f, void *opaque);
198static int slirp_state_load(QEMUFile *f, void *opaque, int version_id);
199
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200200Slirp *slirp_init(int restricted, struct in_addr vnetwork,
201 struct in_addr vnetmask, struct in_addr vhost,
202 const char *vhostname, const char *tftp_path,
203 const char *bootfile, struct in_addr vdhcp_start,
Klaus Stengel63d29602012-10-27 19:53:39 +0200204 struct in_addr vnameserver, const char **vdnssearch,
205 void *opaque)
bellardf0cbd3e2004-04-22 00:10:48 +0000206{
Anthony Liguori7267c092011-08-20 22:09:37 -0500207 Slirp *slirp = g_malloc0(sizeof(Slirp));
Jan Kiszka460fec62009-06-24 14:42:31 +0200208
Jan Kiszkadf461892009-06-24 14:42:30 +0200209 slirp_init_once();
bellard379ff532004-07-12 22:33:07 +0000210
Jan Kiszka460fec62009-06-24 14:42:31 +0200211 slirp->restricted = restricted;
bellardf0cbd3e2004-04-22 00:10:48 +0000212
Jan Kiszka460fec62009-06-24 14:42:31 +0200213 if_init(slirp);
214 ip_init(slirp);
bellardf0cbd3e2004-04-22 00:10:48 +0000215
216 /* Initialise mbufs *after* setting the MTU */
Jan Kiszka460fec62009-06-24 14:42:31 +0200217 m_init(slirp);
bellardf0cbd3e2004-04-22 00:10:48 +0000218
Jan Kiszka460fec62009-06-24 14:42:31 +0200219 slirp->vnetwork_addr = vnetwork;
220 slirp->vnetwork_mask = vnetmask;
221 slirp->vhost_addr = vhost;
Jan Kiszkac92ef6a2009-06-24 14:42:28 +0200222 if (vhostname) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200223 pstrcpy(slirp->client_hostname, sizeof(slirp->client_hostname),
224 vhostname);
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200225 }
Markus Armbrusterc64f50d2013-01-22 11:07:57 +0100226 slirp->tftp_prefix = g_strdup(tftp_path);
227 slirp->bootp_filename = g_strdup(bootfile);
Jan Kiszka460fec62009-06-24 14:42:31 +0200228 slirp->vdhcp_startaddr = vdhcp_start;
229 slirp->vnameserver_addr = vnameserver;
Jan Kiszkaad196a92009-06-24 14:42:28 +0200230
Klaus Stengel63d29602012-10-27 19:53:39 +0200231 if (vdnssearch) {
232 translate_dnssearch(slirp, vdnssearch);
233 }
234
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200235 slirp->opaque = opaque;
236
Alex Williamson0be71e32010-06-25 11:09:07 -0600237 register_savevm(NULL, "slirp", 0, 3,
238 slirp_state_save, slirp_state_load, slirp);
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200239
Blue Swirl72cf2d42009-09-12 07:36:22 +0000240 QTAILQ_INSERT_TAIL(&slirp_instances, slirp, entry);
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200241
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200242 return slirp;
bellardf0cbd3e2004-04-22 00:10:48 +0000243}
244
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200245void slirp_cleanup(Slirp *slirp)
246{
Blue Swirl72cf2d42009-09-12 07:36:22 +0000247 QTAILQ_REMOVE(&slirp_instances, slirp, entry);
Jan Kiszkab1c99fc2009-06-24 14:42:31 +0200248
Alex Williamson0be71e32010-06-25 11:09:07 -0600249 unregister_savevm(NULL, "slirp", slirp);
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200250
Jan Kiszkaa68adc22012-02-29 19:14:23 +0100251 ip_cleanup(slirp);
252 m_cleanup(slirp);
253
Klaus Stengel63d29602012-10-27 19:53:39 +0200254 g_free(slirp->vdnssearch);
Anthony Liguori7267c092011-08-20 22:09:37 -0500255 g_free(slirp->tftp_prefix);
256 g_free(slirp->bootp_filename);
257 g_free(slirp);
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200258}
259
bellardf0cbd3e2004-04-22 00:10:48 +0000260#define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
261#define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
bellardf0cbd3e2004-04-22 00:10:48 +0000262
Liu Ping Fana42e9c42013-08-25 10:01:21 +0800263static void slirp_update_timeout(uint32_t *timeout)
Stefano Stabellini7c7db752012-04-13 19:35:04 +0100264{
Liu Ping Fana42e9c42013-08-25 10:01:21 +0800265 Slirp *slirp;
266 uint32_t t;
267
268 if (*timeout <= TIMEOUT_FAST) {
269 return;
Stefano Stabellini7c7db752012-04-13 19:35:04 +0100270 }
Jan Kiszka426e3e62013-08-28 19:12:15 +0200271
272 t = MIN(1000, *timeout);
Liu Ping Fana42e9c42013-08-25 10:01:21 +0800273
274 /* If we have tcp timeout with slirp, then we will fill @timeout with
275 * more precise value.
276 */
277 QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
278 if (slirp->time_fasttimo) {
279 *timeout = TIMEOUT_FAST;
280 return;
281 }
282 if (slirp->do_slowtimo) {
283 t = MIN(TIMEOUT_SLOW, t);
284 }
285 }
286 *timeout = t;
Stefano Stabellini7c7db752012-04-13 19:35:04 +0100287}
288
Liu Ping Fana42e9c42013-08-25 10:01:21 +0800289void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout)
bellardf0cbd3e2004-04-22 00:10:48 +0000290{
Jan Kiszkab1c99fc2009-06-24 14:42:31 +0200291 Slirp *slirp;
bellardf0cbd3e2004-04-22 00:10:48 +0000292 struct socket *so, *so_next;
bellardf0cbd3e2004-04-22 00:10:48 +0000293
Blue Swirl72cf2d42009-09-12 07:36:22 +0000294 if (QTAILQ_EMPTY(&slirp_instances)) {
Jan Kiszkad918f232009-06-24 14:42:30 +0200295 return;
296 }
297
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100298 /*
299 * First, TCP sockets
300 */
Jan Kiszkad918f232009-06-24 14:42:30 +0200301
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100302 QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
303 /*
304 * *_slowtimo needs calling if there are IP fragments
305 * in the fragment queue, or there are TCP connections active
306 */
Liu Ping Fanfe0ff432013-08-25 10:01:19 +0800307 slirp->do_slowtimo = ((slirp->tcb.so_next != &slirp->tcb) ||
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100308 (&slirp->ipq.ip_link != slirp->ipq.ip_link.next));
ths3b46e622007-09-17 08:09:54 +0000309
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100310 for (so = slirp->tcb.so_next; so != &slirp->tcb;
311 so = so_next) {
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100312 int events = 0;
313
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100314 so_next = so->so_next;
ths3b46e622007-09-17 08:09:54 +0000315
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100316 so->pollfds_idx = -1;
317
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100318 /*
319 * See if we need a tcp_fasttimo
320 */
Liu Ping Fanfe0ff432013-08-25 10:01:19 +0800321 if (slirp->time_fasttimo == 0 &&
322 so->so_tcpcb->t_flags & TF_DELACK) {
323 slirp->time_fasttimo = curtime; /* Flag when want a fasttimo */
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100324 }
ths3b46e622007-09-17 08:09:54 +0000325
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100326 /*
327 * NOFDREF can include still connecting to local-host,
328 * newly socreated() sockets etc. Don't want to select these.
329 */
330 if (so->so_state & SS_NOFDREF || so->s == -1) {
331 continue;
332 }
ths3b46e622007-09-17 08:09:54 +0000333
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100334 /*
335 * Set for reading sockets which are accepting
336 */
337 if (so->so_state & SS_FACCEPTCONN) {
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100338 GPollFD pfd = {
339 .fd = so->s,
340 .events = G_IO_IN | G_IO_HUP | G_IO_ERR,
341 };
342 so->pollfds_idx = pollfds->len;
343 g_array_append_val(pollfds, pfd);
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100344 continue;
345 }
ths3b46e622007-09-17 08:09:54 +0000346
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100347 /*
348 * Set for writing sockets which are connecting
349 */
350 if (so->so_state & SS_ISFCONNECTING) {
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100351 GPollFD pfd = {
352 .fd = so->s,
353 .events = G_IO_OUT | G_IO_ERR,
354 };
355 so->pollfds_idx = pollfds->len;
356 g_array_append_val(pollfds, pfd);
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100357 continue;
358 }
ths3b46e622007-09-17 08:09:54 +0000359
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100360 /*
361 * Set for writing if we are connected, can send more, and
362 * we have something to send
363 */
364 if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100365 events |= G_IO_OUT | G_IO_ERR;
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100366 }
ths3b46e622007-09-17 08:09:54 +0000367
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100368 /*
369 * Set for reading (and urgent data) if we are connected, can
370 * receive more, and we have room for it XXX /2 ?
371 */
372 if (CONN_CANFRCV(so) &&
373 (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100374 events |= G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI;
375 }
376
377 if (events) {
378 GPollFD pfd = {
379 .fd = so->s,
380 .events = events,
381 };
382 so->pollfds_idx = pollfds->len;
383 g_array_append_val(pollfds, pfd);
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100384 }
385 }
ths3b46e622007-09-17 08:09:54 +0000386
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100387 /*
388 * UDP sockets
389 */
390 for (so = slirp->udb.so_next; so != &slirp->udb;
391 so = so_next) {
392 so_next = so->so_next;
ths3b46e622007-09-17 08:09:54 +0000393
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100394 so->pollfds_idx = -1;
395
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100396 /*
397 * See if it's timed out
398 */
399 if (so->so_expire) {
400 if (so->so_expire <= curtime) {
401 udp_detach(so);
402 continue;
403 } else {
Liu Ping Fanfe0ff432013-08-25 10:01:19 +0800404 slirp->do_slowtimo = true; /* Let socket expire */
Jan Kiszkae6d43cf2011-07-20 12:20:18 +0200405 }
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100406 }
ths5fafdf22007-09-16 21:08:06 +0000407
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100408 /*
409 * When UDP packets are received from over the
410 * link, they're sendto()'d straight away, so
411 * no need for setting for writing
412 * Limit the number of packets queued by this session
413 * to 4. Note that even though we try and limit this
414 * to 4 packets, the session could have more queued
415 * if the packets needed to be fragmented
416 * (XXX <= 4 ?)
417 */
418 if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100419 GPollFD pfd = {
420 .fd = so->s,
421 .events = G_IO_IN | G_IO_HUP | G_IO_ERR,
422 };
423 so->pollfds_idx = pollfds->len;
424 g_array_append_val(pollfds, pfd);
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100425 }
426 }
427
428 /*
429 * ICMP sockets
430 */
431 for (so = slirp->icmp.so_next; so != &slirp->icmp;
432 so = so_next) {
433 so_next = so->so_next;
434
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100435 so->pollfds_idx = -1;
436
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100437 /*
438 * See if it's timed out
439 */
440 if (so->so_expire) {
441 if (so->so_expire <= curtime) {
442 icmp_detach(so);
443 continue;
444 } else {
Liu Ping Fanfe0ff432013-08-25 10:01:19 +0800445 slirp->do_slowtimo = true; /* Let socket expire */
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100446 }
447 }
448
449 if (so->so_state & SS_ISFCONNECTED) {
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100450 GPollFD pfd = {
451 .fd = so->s,
452 .events = G_IO_IN | G_IO_HUP | G_IO_ERR,
453 };
454 so->pollfds_idx = pollfds->len;
455 g_array_append_val(pollfds, pfd);
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100456 }
457 }
458 }
Liu Ping Fana42e9c42013-08-25 10:01:21 +0800459 slirp_update_timeout(timeout);
ths5fafdf22007-09-16 21:08:06 +0000460}
bellardf0cbd3e2004-04-22 00:10:48 +0000461
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100462void slirp_pollfds_poll(GArray *pollfds, int select_error)
bellardf0cbd3e2004-04-22 00:10:48 +0000463{
Jan Kiszkab1c99fc2009-06-24 14:42:31 +0200464 Slirp *slirp;
bellardf0cbd3e2004-04-22 00:10:48 +0000465 struct socket *so, *so_next;
466 int ret;
467
Blue Swirl72cf2d42009-09-12 07:36:22 +0000468 if (QTAILQ_EMPTY(&slirp_instances)) {
Jan Kiszkad918f232009-06-24 14:42:30 +0200469 return;
470 }
471
Alex Blighbc72ad62013-08-21 16:03:08 +0100472 curtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
ths5fafdf22007-09-16 21:08:06 +0000473
Blue Swirl72cf2d42009-09-12 07:36:22 +0000474 QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100475 /*
476 * See if anything has timed out
477 */
Liu Ping Fan9b0ca6c2013-08-25 10:01:20 +0800478 if (slirp->time_fasttimo &&
479 ((curtime - slirp->time_fasttimo) >= TIMEOUT_FAST)) {
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100480 tcp_fasttimo(slirp);
Liu Ping Fanfe0ff432013-08-25 10:01:19 +0800481 slirp->time_fasttimo = 0;
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100482 }
Liu Ping Fan9b0ca6c2013-08-25 10:01:20 +0800483 if (slirp->do_slowtimo &&
484 ((curtime - slirp->last_slowtimo) >= TIMEOUT_SLOW)) {
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100485 ip_slowtimo(slirp);
486 tcp_slowtimo(slirp);
Liu Ping Fanfe0ff432013-08-25 10:01:19 +0800487 slirp->last_slowtimo = curtime;
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100488 }
ths5fafdf22007-09-16 21:08:06 +0000489
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100490 /*
491 * Check sockets
492 */
493 if (!select_error) {
494 /*
495 * Check TCP sockets
496 */
497 for (so = slirp->tcb.so_next; so != &slirp->tcb;
498 so = so_next) {
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100499 int revents;
500
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100501 so_next = so->so_next;
Jan Kiszkae6d43cf2011-07-20 12:20:18 +0200502
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100503 revents = 0;
504 if (so->pollfds_idx != -1) {
505 revents = g_array_index(pollfds, GPollFD,
506 so->pollfds_idx).revents;
507 }
508
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100509 if (so->so_state & SS_NOFDREF || so->s == -1) {
510 continue;
511 }
Jan Kiszkae6d43cf2011-07-20 12:20:18 +0200512
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100513 /*
514 * Check for URG data
515 * This will soread as well, so no need to
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100516 * test for G_IO_IN below if this succeeds
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100517 */
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100518 if (revents & G_IO_PRI) {
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100519 sorecvoob(so);
520 }
521 /*
522 * Check sockets for reading
523 */
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100524 else if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) {
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100525 /*
526 * Check for incoming connections
527 */
528 if (so->so_state & SS_FACCEPTCONN) {
529 tcp_connect(so);
530 continue;
531 } /* else */
532 ret = soread(so);
533
534 /* Output it if we read something */
535 if (ret > 0) {
536 tcp_output(sototcpcb(so));
Jan Kiszkae6d43cf2011-07-20 12:20:18 +0200537 }
538 }
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100539
540 /*
541 * Check sockets for writing
542 */
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100543 if (!(so->so_state & SS_NOFDREF) &&
544 (revents & (G_IO_OUT | G_IO_ERR))) {
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100545 /*
546 * Check for non-blocking, still-connecting sockets
547 */
548 if (so->so_state & SS_ISFCONNECTING) {
549 /* Connected */
550 so->so_state &= ~SS_ISFCONNECTING;
551
552 ret = send(so->s, (const void *) &ret, 0, 0);
553 if (ret < 0) {
554 /* XXXXX Must fix, zero bytes is a NOP */
555 if (errno == EAGAIN || errno == EWOULDBLOCK ||
556 errno == EINPROGRESS || errno == ENOTCONN) {
557 continue;
558 }
559
560 /* else failed */
561 so->so_state &= SS_PERSISTENT_MASK;
562 so->so_state |= SS_NOFDREF;
563 }
564 /* else so->so_state &= ~SS_ISFCONNECTING; */
565
566 /*
567 * Continue tcp_input
568 */
569 tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
570 /* continue; */
571 } else {
572 ret = sowrite(so);
573 }
574 /*
575 * XXXXX If we wrote something (a lot), there
576 * could be a need for a window update.
577 * In the worst case, the remote will send
578 * a window probe to get things going again
579 */
580 }
581
582 /*
583 * Probe a still-connecting, non-blocking socket
584 * to check if it's still alive
585 */
586#ifdef PROBE_CONN
587 if (so->so_state & SS_ISFCONNECTING) {
588 ret = qemu_recv(so->s, &ret, 0, 0);
589
590 if (ret < 0) {
591 /* XXX */
592 if (errno == EAGAIN || errno == EWOULDBLOCK ||
593 errno == EINPROGRESS || errno == ENOTCONN) {
594 continue; /* Still connecting, continue */
595 }
596
597 /* else failed */
598 so->so_state &= SS_PERSISTENT_MASK;
599 so->so_state |= SS_NOFDREF;
600
601 /* tcp_input will take care of it */
602 } else {
603 ret = send(so->s, &ret, 0, 0);
604 if (ret < 0) {
605 /* XXX */
606 if (errno == EAGAIN || errno == EWOULDBLOCK ||
607 errno == EINPROGRESS || errno == ENOTCONN) {
608 continue;
609 }
610 /* else failed */
611 so->so_state &= SS_PERSISTENT_MASK;
612 so->so_state |= SS_NOFDREF;
613 } else {
614 so->so_state &= ~SS_ISFCONNECTING;
615 }
616
617 }
618 tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
619 } /* SS_ISFCONNECTING */
620#endif
621 }
622
623 /*
624 * Now UDP sockets.
625 * Incoming packets are sent straight away, they're not buffered.
626 * Incoming UDP data isn't buffered either.
627 */
628 for (so = slirp->udb.so_next; so != &slirp->udb;
629 so = so_next) {
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100630 int revents;
631
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100632 so_next = so->so_next;
633
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100634 revents = 0;
635 if (so->pollfds_idx != -1) {
636 revents = g_array_index(pollfds, GPollFD,
637 so->pollfds_idx).revents;
638 }
639
640 if (so->s != -1 &&
641 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) {
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100642 sorecvfrom(so);
643 }
644 }
645
646 /*
647 * Check incoming ICMP relies.
648 */
649 for (so = slirp->icmp.so_next; so != &slirp->icmp;
650 so = so_next) {
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100651 int revents;
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100652
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100653 so_next = so->so_next;
654
655 revents = 0;
656 if (so->pollfds_idx != -1) {
657 revents = g_array_index(pollfds, GPollFD,
658 so->pollfds_idx).revents;
659 }
660
661 if (so->s != -1 &&
662 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) {
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100663 icmp_receive(so);
664 }
665 }
666 }
ths5fafdf22007-09-16 21:08:06 +0000667
Jan Kiszkaf3734312012-03-06 00:02:23 +0100668 if_start(slirp);
Jan Kiszkab1c99fc2009-06-24 14:42:31 +0200669 }
bellardf0cbd3e2004-04-22 00:10:48 +0000670}
671
Jan Kiszka460fec62009-06-24 14:42:31 +0200672static void arp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
bellardf0cbd3e2004-04-22 00:10:48 +0000673{
bellardf0cbd3e2004-04-22 00:10:48 +0000674 struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
Hervé Poussineaudbf3c4b2010-09-15 22:33:26 +0200675 uint8_t arp_reply[max(ETH_HLEN + sizeof(struct arphdr), 64)];
bellardf0cbd3e2004-04-22 00:10:48 +0000676 struct ethhdr *reh = (struct ethhdr *)arp_reply;
677 struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
678 int ar_op;
bellarda3d4af02004-09-05 23:10:26 +0000679 struct ex_list *ex_ptr;
bellardf0cbd3e2004-04-22 00:10:48 +0000680
681 ar_op = ntohs(ah->ar_op);
682 switch(ar_op) {
683 case ARPOP_REQUEST:
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200684 if (ah->ar_tip == ah->ar_sip) {
685 /* Gratuitous ARP */
686 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
687 return;
688 }
689
Jan Kiszka460fec62009-06-24 14:42:31 +0200690 if ((ah->ar_tip & slirp->vnetwork_mask.s_addr) ==
691 slirp->vnetwork_addr.s_addr) {
692 if (ah->ar_tip == slirp->vnameserver_addr.s_addr ||
693 ah->ar_tip == slirp->vhost_addr.s_addr)
bellarda3d4af02004-09-05 23:10:26 +0000694 goto arp_ok;
Jan Kiszka460fec62009-06-24 14:42:31 +0200695 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200696 if (ex_ptr->ex_addr.s_addr == ah->ar_tip)
bellarda3d4af02004-09-05 23:10:26 +0000697 goto arp_ok;
698 }
699 return;
700 arp_ok:
Hervé Poussineaudbf3c4b2010-09-15 22:33:26 +0200701 memset(arp_reply, 0, sizeof(arp_reply));
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200702
703 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
bellardf0cbd3e2004-04-22 00:10:48 +0000704
705 /* ARP request for alias/dns mac address */
706 memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200707 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
708 memcpy(&reh->h_source[2], &ah->ar_tip, 4);
bellardf0cbd3e2004-04-22 00:10:48 +0000709 reh->h_proto = htons(ETH_P_ARP);
710
711 rah->ar_hrd = htons(1);
712 rah->ar_pro = htons(ETH_P_IP);
713 rah->ar_hln = ETH_ALEN;
714 rah->ar_pln = 4;
715 rah->ar_op = htons(ARPOP_REPLY);
716 memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200717 rah->ar_sip = ah->ar_tip;
bellardf0cbd3e2004-04-22 00:10:48 +0000718 memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200719 rah->ar_tip = ah->ar_sip;
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200720 slirp_output(slirp->opaque, arp_reply, sizeof(arp_reply));
bellardf0cbd3e2004-04-22 00:10:48 +0000721 }
722 break;
bellardde806f02008-10-17 17:28:58 +0000723 case ARPOP_REPLY:
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200724 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
bellardde806f02008-10-17 17:28:58 +0000725 break;
bellardf0cbd3e2004-04-22 00:10:48 +0000726 default:
727 break;
728 }
729}
730
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200731void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
bellardf0cbd3e2004-04-22 00:10:48 +0000732{
733 struct mbuf *m;
734 int proto;
735
736 if (pkt_len < ETH_HLEN)
737 return;
ths3b46e622007-09-17 08:09:54 +0000738
bellardf0cbd3e2004-04-22 00:10:48 +0000739 proto = ntohs(*(uint16_t *)(pkt + 12));
740 switch(proto) {
741 case ETH_P_ARP:
Jan Kiszka460fec62009-06-24 14:42:31 +0200742 arp_input(slirp, pkt, pkt_len);
bellardf0cbd3e2004-04-22 00:10:48 +0000743 break;
744 case ETH_P_IP:
Jan Kiszka460fec62009-06-24 14:42:31 +0200745 m = m_get(slirp);
bellardf0cbd3e2004-04-22 00:10:48 +0000746 if (!m)
747 return;
bellard38f3e7c2006-05-10 19:21:58 +0000748 /* Note: we add to align the IP header */
aurel32e8e880a2008-12-07 18:15:23 +0000749 if (M_FREEROOM(m) < pkt_len + 2) {
750 m_inc(m, pkt_len + 2);
751 }
bellard38f3e7c2006-05-10 19:21:58 +0000752 m->m_len = pkt_len + 2;
753 memcpy(m->m_data + 2, pkt, pkt_len);
bellardf0cbd3e2004-04-22 00:10:48 +0000754
bellard38f3e7c2006-05-10 19:21:58 +0000755 m->m_data += 2 + ETH_HLEN;
756 m->m_len -= 2 + ETH_HLEN;
bellardf0cbd3e2004-04-22 00:10:48 +0000757
758 ip_input(m);
759 break;
760 default:
761 break;
762 }
763}
764
Fabien Chouteau1ab74ce2011-08-01 18:18:37 +0200765/* Output the IP packet to the ethernet device. Returns 0 if the packet must be
766 * re-queued.
767 */
768int if_encap(Slirp *slirp, struct mbuf *ifm)
bellardf0cbd3e2004-04-22 00:10:48 +0000769{
770 uint8_t buf[1600];
771 struct ethhdr *eh = (struct ethhdr *)buf;
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200772 uint8_t ethaddr[ETH_ALEN];
Fabien Chouteau1ab74ce2011-08-01 18:18:37 +0200773 const struct ip *iph = (const struct ip *)ifm->m_data;
bellardf0cbd3e2004-04-22 00:10:48 +0000774
Fabien Chouteau1ab74ce2011-08-01 18:18:37 +0200775 if (ifm->m_len + ETH_HLEN > sizeof(buf)) {
776 return 1;
777 }
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200778
Samuel Thibault959e4142014-05-14 03:13:09 +0200779 if (iph->ip_dst.s_addr == 0) {
780 /* 0.0.0.0 can not be a destination address, something went wrong,
781 * avoid making it worse */
782 return 1;
783 }
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200784 if (!arp_table_search(slirp, iph->ip_dst.s_addr, ethaddr)) {
bellardde806f02008-10-17 17:28:58 +0000785 uint8_t arp_req[ETH_HLEN + sizeof(struct arphdr)];
786 struct ethhdr *reh = (struct ethhdr *)arp_req;
787 struct arphdr *rah = (struct arphdr *)(arp_req + ETH_HLEN);
bellardf0cbd3e2004-04-22 00:10:48 +0000788
Fabien Chouteau1ab74ce2011-08-01 18:18:37 +0200789 if (!ifm->arp_requested) {
790 /* If the client addr is not known, send an ARP request */
791 memset(reh->h_dest, 0xff, ETH_ALEN);
792 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
793 memcpy(&reh->h_source[2], &slirp->vhost_addr, 4);
794 reh->h_proto = htons(ETH_P_ARP);
795 rah->ar_hrd = htons(1);
796 rah->ar_pro = htons(ETH_P_IP);
797 rah->ar_hln = ETH_ALEN;
798 rah->ar_pln = 4;
799 rah->ar_op = htons(ARPOP_REQUEST);
800
801 /* source hw addr */
802 memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN - 4);
803 memcpy(&rah->ar_sha[2], &slirp->vhost_addr, 4);
804
805 /* source IP */
806 rah->ar_sip = slirp->vhost_addr.s_addr;
807
808 /* target hw addr (none) */
809 memset(rah->ar_tha, 0, ETH_ALEN);
810
811 /* target IP */
812 rah->ar_tip = iph->ip_dst.s_addr;
813 slirp->client_ipaddr = iph->ip_dst;
814 slirp_output(slirp->opaque, arp_req, sizeof(arp_req));
815 ifm->arp_requested = true;
Jan Kiszkae3a110b2011-08-05 14:05:53 +0200816
817 /* Expire request and drop outgoing packet after 1 second */
Alex Blighbc72ad62013-08-21 16:03:08 +0100818 ifm->expiration_date = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + 1000000000ULL;
Fabien Chouteau1ab74ce2011-08-01 18:18:37 +0200819 }
820 return 0;
bellardde806f02008-10-17 17:28:58 +0000821 } else {
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200822 memcpy(eh->h_dest, ethaddr, ETH_ALEN);
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200823 memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 4);
bellardde806f02008-10-17 17:28:58 +0000824 /* XXX: not correct */
Jan Kiszka460fec62009-06-24 14:42:31 +0200825 memcpy(&eh->h_source[2], &slirp->vhost_addr, 4);
bellardde806f02008-10-17 17:28:58 +0000826 eh->h_proto = htons(ETH_P_IP);
Fabien Chouteau1ab74ce2011-08-01 18:18:37 +0200827 memcpy(buf + sizeof(struct ethhdr), ifm->m_data, ifm->m_len);
828 slirp_output(slirp->opaque, buf, ifm->m_len + ETH_HLEN);
829 return 1;
bellardde806f02008-10-17 17:28:58 +0000830 }
bellardf0cbd3e2004-04-22 00:10:48 +0000831}
bellard9bf05442004-08-25 22:12:49 +0000832
Jan Kiszka9c12a6f2009-06-24 14:42:29 +0200833/* Drop host forwarding rule, return 0 if found. */
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200834int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
835 int host_port)
Alexander Grafc1261d82009-05-26 13:03:26 +0200836{
837 struct socket *so;
Jan Kiszka460fec62009-06-24 14:42:31 +0200838 struct socket *head = (is_udp ? &slirp->udb : &slirp->tcb);
Jan Kiszka2ad82cf2009-06-24 14:42:28 +0200839 struct sockaddr_in addr;
840 int port = htons(host_port);
841 socklen_t addr_len;
Alexander Grafc1261d82009-05-26 13:03:26 +0200842
Alexander Grafc1261d82009-05-26 13:03:26 +0200843 for (so = head->so_next; so != head; so = so->so_next) {
Jan Kiszka2ad82cf2009-06-24 14:42:28 +0200844 addr_len = sizeof(addr);
Jan Kiszka9c12a6f2009-06-24 14:42:29 +0200845 if ((so->so_state & SS_HOSTFWD) &&
846 getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 &&
Jan Kiszka3c6a0582009-06-24 14:42:28 +0200847 addr.sin_addr.s_addr == host_addr.s_addr &&
Jan Kiszka2ad82cf2009-06-24 14:42:28 +0200848 addr.sin_port == port) {
Alexander Grafc1261d82009-05-26 13:03:26 +0200849 close(so->s);
850 sofree(so);
Jan Kiszka9c12a6f2009-06-24 14:42:29 +0200851 return 0;
Alexander Grafc1261d82009-05-26 13:03:26 +0200852 }
853 }
854
Jan Kiszka9c12a6f2009-06-24 14:42:29 +0200855 return -1;
Alexander Grafc1261d82009-05-26 13:03:26 +0200856}
857
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200858int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
859 int host_port, struct in_addr guest_addr, int guest_port)
bellard9bf05442004-08-25 22:12:49 +0000860{
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200861 if (!guest_addr.s_addr) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200862 guest_addr = slirp->vdhcp_startaddr;
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200863 }
bellard9bf05442004-08-25 22:12:49 +0000864 if (is_udp) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200865 if (!udp_listen(slirp, host_addr.s_addr, htons(host_port),
866 guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
bellard9bf05442004-08-25 22:12:49 +0000867 return -1;
868 } else {
Jan Kiszka460fec62009-06-24 14:42:31 +0200869 if (!tcp_listen(slirp, host_addr.s_addr, htons(host_port),
870 guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
bellard9bf05442004-08-25 22:12:49 +0000871 return -1;
872 }
873 return 0;
874}
bellarda3d4af02004-09-05 23:10:26 +0000875
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200876int slirp_add_exec(Slirp *slirp, int do_pty, const void *args,
Jan Kiszkabb53fc52009-07-22 17:03:52 +0200877 struct in_addr *guest_addr, int guest_port)
bellarda3d4af02004-09-05 23:10:26 +0000878{
Jan Kiszkabb53fc52009-07-22 17:03:52 +0200879 if (!guest_addr->s_addr) {
880 guest_addr->s_addr = slirp->vnetwork_addr.s_addr |
Jan Kiszka460fec62009-06-24 14:42:31 +0200881 (htonl(0x0204) & ~slirp->vnetwork_mask.s_addr);
Jan Kiszkac92ef6a2009-06-24 14:42:28 +0200882 }
Jan Kiszkabb53fc52009-07-22 17:03:52 +0200883 if ((guest_addr->s_addr & slirp->vnetwork_mask.s_addr) !=
Jan Kiszka460fec62009-06-24 14:42:31 +0200884 slirp->vnetwork_addr.s_addr ||
Jan Kiszkabb53fc52009-07-22 17:03:52 +0200885 guest_addr->s_addr == slirp->vhost_addr.s_addr ||
886 guest_addr->s_addr == slirp->vnameserver_addr.s_addr) {
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200887 return -1;
888 }
Jan Kiszkabb53fc52009-07-22 17:03:52 +0200889 return add_exec(&slirp->exec_list, do_pty, (char *)args, *guest_addr,
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200890 htons(guest_port));
bellarda3d4af02004-09-05 23:10:26 +0000891}
aliguorie1c5a2b2009-01-08 19:18:21 +0000892
893ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags)
894{
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100895 if (so->s == -1 && so->extra) {
896 qemu_chr_fe_write(so->extra, buf, len);
897 return len;
898 }
aliguorie1c5a2b2009-01-08 19:18:21 +0000899
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100900 return send(so->s, buf, len, flags);
aliguorie1c5a2b2009-01-08 19:18:21 +0000901}
902
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200903static struct socket *
Jan Kiszka460fec62009-06-24 14:42:31 +0200904slirp_find_ctl_socket(Slirp *slirp, struct in_addr guest_addr, int guest_port)
aliguorie1c5a2b2009-01-08 19:18:21 +0000905{
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200906 struct socket *so;
aliguorie1c5a2b2009-01-08 19:18:21 +0000907
Jan Kiszka460fec62009-06-24 14:42:31 +0200908 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200909 if (so->so_faddr.s_addr == guest_addr.s_addr &&
910 htons(so->so_fport) == guest_port) {
911 return so;
912 }
913 }
914 return NULL;
aliguorie1c5a2b2009-01-08 19:18:21 +0000915}
916
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200917size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr,
918 int guest_port)
aliguorie1c5a2b2009-01-08 19:18:21 +0000919{
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100920 struct iovec iov[2];
921 struct socket *so;
aliguorie1c5a2b2009-01-08 19:18:21 +0000922
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100923 so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
aliguorie1c5a2b2009-01-08 19:18:21 +0000924
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100925 if (!so || so->so_state & SS_NOFDREF) {
926 return 0;
927 }
aliguorie1c5a2b2009-01-08 19:18:21 +0000928
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100929 if (!CONN_CANFRCV(so) || so->so_snd.sb_cc >= (so->so_snd.sb_datalen/2)) {
930 return 0;
931 }
aliguorie1c5a2b2009-01-08 19:18:21 +0000932
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100933 return sopreprbuf(so, iov, NULL);
aliguorie1c5a2b2009-01-08 19:18:21 +0000934}
935
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200936void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port,
Jan Kiszkac92ef6a2009-06-24 14:42:28 +0200937 const uint8_t *buf, int size)
aliguorie1c5a2b2009-01-08 19:18:21 +0000938{
939 int ret;
Jan Kiszka460fec62009-06-24 14:42:31 +0200940 struct socket *so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200941
aliguorie1c5a2b2009-01-08 19:18:21 +0000942 if (!so)
943 return;
944
blueswir10580ac92009-01-12 17:51:06 +0000945 ret = soreadbuf(so, (const char *)buf, size);
aliguorie1c5a2b2009-01-08 19:18:21 +0000946
947 if (ret > 0)
948 tcp_output(sototcpcb(so));
949}
aliguori062e5522009-01-08 19:27:07 +0000950
951static void slirp_tcp_save(QEMUFile *f, struct tcpcb *tp)
952{
953 int i;
954
955 qemu_put_sbe16(f, tp->t_state);
956 for (i = 0; i < TCPT_NTIMERS; i++)
957 qemu_put_sbe16(f, tp->t_timer[i]);
958 qemu_put_sbe16(f, tp->t_rxtshift);
959 qemu_put_sbe16(f, tp->t_rxtcur);
960 qemu_put_sbe16(f, tp->t_dupacks);
961 qemu_put_be16(f, tp->t_maxseg);
962 qemu_put_sbyte(f, tp->t_force);
963 qemu_put_be16(f, tp->t_flags);
964 qemu_put_be32(f, tp->snd_una);
965 qemu_put_be32(f, tp->snd_nxt);
966 qemu_put_be32(f, tp->snd_up);
967 qemu_put_be32(f, tp->snd_wl1);
968 qemu_put_be32(f, tp->snd_wl2);
969 qemu_put_be32(f, tp->iss);
970 qemu_put_be32(f, tp->snd_wnd);
971 qemu_put_be32(f, tp->rcv_wnd);
972 qemu_put_be32(f, tp->rcv_nxt);
973 qemu_put_be32(f, tp->rcv_up);
974 qemu_put_be32(f, tp->irs);
975 qemu_put_be32(f, tp->rcv_adv);
976 qemu_put_be32(f, tp->snd_max);
977 qemu_put_be32(f, tp->snd_cwnd);
978 qemu_put_be32(f, tp->snd_ssthresh);
979 qemu_put_sbe16(f, tp->t_idle);
980 qemu_put_sbe16(f, tp->t_rtt);
981 qemu_put_be32(f, tp->t_rtseq);
982 qemu_put_sbe16(f, tp->t_srtt);
983 qemu_put_sbe16(f, tp->t_rttvar);
984 qemu_put_be16(f, tp->t_rttmin);
985 qemu_put_be32(f, tp->max_sndwnd);
986 qemu_put_byte(f, tp->t_oobflags);
987 qemu_put_byte(f, tp->t_iobc);
988 qemu_put_sbe16(f, tp->t_softerror);
989 qemu_put_byte(f, tp->snd_scale);
990 qemu_put_byte(f, tp->rcv_scale);
991 qemu_put_byte(f, tp->request_r_scale);
992 qemu_put_byte(f, tp->requested_s_scale);
993 qemu_put_be32(f, tp->ts_recent);
994 qemu_put_be32(f, tp->ts_recent_age);
995 qemu_put_be32(f, tp->last_ack_sent);
996}
997
998static void slirp_sbuf_save(QEMUFile *f, struct sbuf *sbuf)
999{
1000 uint32_t off;
1001
1002 qemu_put_be32(f, sbuf->sb_cc);
1003 qemu_put_be32(f, sbuf->sb_datalen);
1004 off = (uint32_t)(sbuf->sb_wptr - sbuf->sb_data);
1005 qemu_put_sbe32(f, off);
1006 off = (uint32_t)(sbuf->sb_rptr - sbuf->sb_data);
1007 qemu_put_sbe32(f, off);
1008 qemu_put_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen);
1009}
1010
1011static void slirp_socket_save(QEMUFile *f, struct socket *so)
1012{
1013 qemu_put_be32(f, so->so_urgc);
1014 qemu_put_be32(f, so->so_faddr.s_addr);
1015 qemu_put_be32(f, so->so_laddr.s_addr);
1016 qemu_put_be16(f, so->so_fport);
1017 qemu_put_be16(f, so->so_lport);
1018 qemu_put_byte(f, so->so_iptos);
1019 qemu_put_byte(f, so->so_emu);
1020 qemu_put_byte(f, so->so_type);
1021 qemu_put_be32(f, so->so_state);
1022 slirp_sbuf_save(f, &so->so_rcv);
1023 slirp_sbuf_save(f, &so->so_snd);
1024 slirp_tcp_save(f, so->so_tcpcb);
1025}
1026
Jan Kiszka0a1f8512009-06-24 14:42:31 +02001027static void slirp_bootp_save(QEMUFile *f, Slirp *slirp)
1028{
1029 int i;
1030
1031 for (i = 0; i < NB_BOOTP_CLIENTS; i++) {
1032 qemu_put_be16(f, slirp->bootp_clients[i].allocated);
1033 qemu_put_buffer(f, slirp->bootp_clients[i].macaddr, 6);
1034 }
1035}
1036
aliguori062e5522009-01-08 19:27:07 +00001037static void slirp_state_save(QEMUFile *f, void *opaque)
1038{
Jan Kiszka460fec62009-06-24 14:42:31 +02001039 Slirp *slirp = opaque;
aliguori062e5522009-01-08 19:27:07 +00001040 struct ex_list *ex_ptr;
1041
Jan Kiszka460fec62009-06-24 14:42:31 +02001042 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
aliguori062e5522009-01-08 19:27:07 +00001043 if (ex_ptr->ex_pty == 3) {
1044 struct socket *so;
Jan Kiszka460fec62009-06-24 14:42:31 +02001045 so = slirp_find_ctl_socket(slirp, ex_ptr->ex_addr,
1046 ntohs(ex_ptr->ex_fport));
aliguori062e5522009-01-08 19:27:07 +00001047 if (!so)
1048 continue;
1049
1050 qemu_put_byte(f, 42);
1051 slirp_socket_save(f, so);
1052 }
1053 qemu_put_byte(f, 0);
Jan Kiszka285f7a62009-06-24 14:42:30 +02001054
Jan Kiszka460fec62009-06-24 14:42:31 +02001055 qemu_put_be16(f, slirp->ip_id);
Jan Kiszka0a1f8512009-06-24 14:42:31 +02001056
1057 slirp_bootp_save(f, slirp);
aliguori062e5522009-01-08 19:27:07 +00001058}
1059
1060static void slirp_tcp_load(QEMUFile *f, struct tcpcb *tp)
1061{
1062 int i;
1063
1064 tp->t_state = qemu_get_sbe16(f);
1065 for (i = 0; i < TCPT_NTIMERS; i++)
1066 tp->t_timer[i] = qemu_get_sbe16(f);
1067 tp->t_rxtshift = qemu_get_sbe16(f);
1068 tp->t_rxtcur = qemu_get_sbe16(f);
1069 tp->t_dupacks = qemu_get_sbe16(f);
1070 tp->t_maxseg = qemu_get_be16(f);
1071 tp->t_force = qemu_get_sbyte(f);
1072 tp->t_flags = qemu_get_be16(f);
1073 tp->snd_una = qemu_get_be32(f);
1074 tp->snd_nxt = qemu_get_be32(f);
1075 tp->snd_up = qemu_get_be32(f);
1076 tp->snd_wl1 = qemu_get_be32(f);
1077 tp->snd_wl2 = qemu_get_be32(f);
1078 tp->iss = qemu_get_be32(f);
1079 tp->snd_wnd = qemu_get_be32(f);
1080 tp->rcv_wnd = qemu_get_be32(f);
1081 tp->rcv_nxt = qemu_get_be32(f);
1082 tp->rcv_up = qemu_get_be32(f);
1083 tp->irs = qemu_get_be32(f);
1084 tp->rcv_adv = qemu_get_be32(f);
1085 tp->snd_max = qemu_get_be32(f);
1086 tp->snd_cwnd = qemu_get_be32(f);
1087 tp->snd_ssthresh = qemu_get_be32(f);
1088 tp->t_idle = qemu_get_sbe16(f);
1089 tp->t_rtt = qemu_get_sbe16(f);
1090 tp->t_rtseq = qemu_get_be32(f);
1091 tp->t_srtt = qemu_get_sbe16(f);
1092 tp->t_rttvar = qemu_get_sbe16(f);
1093 tp->t_rttmin = qemu_get_be16(f);
1094 tp->max_sndwnd = qemu_get_be32(f);
1095 tp->t_oobflags = qemu_get_byte(f);
1096 tp->t_iobc = qemu_get_byte(f);
1097 tp->t_softerror = qemu_get_sbe16(f);
1098 tp->snd_scale = qemu_get_byte(f);
1099 tp->rcv_scale = qemu_get_byte(f);
1100 tp->request_r_scale = qemu_get_byte(f);
1101 tp->requested_s_scale = qemu_get_byte(f);
1102 tp->ts_recent = qemu_get_be32(f);
1103 tp->ts_recent_age = qemu_get_be32(f);
1104 tp->last_ack_sent = qemu_get_be32(f);
1105 tcp_template(tp);
1106}
1107
1108static int slirp_sbuf_load(QEMUFile *f, struct sbuf *sbuf)
1109{
1110 uint32_t off, sb_cc, sb_datalen;
1111
1112 sb_cc = qemu_get_be32(f);
1113 sb_datalen = qemu_get_be32(f);
1114
1115 sbreserve(sbuf, sb_datalen);
1116
1117 if (sbuf->sb_datalen != sb_datalen)
1118 return -ENOMEM;
1119
1120 sbuf->sb_cc = sb_cc;
1121
1122 off = qemu_get_sbe32(f);
1123 sbuf->sb_wptr = sbuf->sb_data + off;
1124 off = qemu_get_sbe32(f);
1125 sbuf->sb_rptr = sbuf->sb_data + off;
1126 qemu_get_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen);
1127
1128 return 0;
1129}
1130
1131static int slirp_socket_load(QEMUFile *f, struct socket *so)
1132{
1133 if (tcp_attach(so) < 0)
1134 return -ENOMEM;
1135
1136 so->so_urgc = qemu_get_be32(f);
1137 so->so_faddr.s_addr = qemu_get_be32(f);
1138 so->so_laddr.s_addr = qemu_get_be32(f);
1139 so->so_fport = qemu_get_be16(f);
1140 so->so_lport = qemu_get_be16(f);
1141 so->so_iptos = qemu_get_byte(f);
1142 so->so_emu = qemu_get_byte(f);
1143 so->so_type = qemu_get_byte(f);
1144 so->so_state = qemu_get_be32(f);
1145 if (slirp_sbuf_load(f, &so->so_rcv) < 0)
1146 return -ENOMEM;
1147 if (slirp_sbuf_load(f, &so->so_snd) < 0)
1148 return -ENOMEM;
1149 slirp_tcp_load(f, so->so_tcpcb);
1150
1151 return 0;
1152}
1153
Jan Kiszka0a1f8512009-06-24 14:42:31 +02001154static void slirp_bootp_load(QEMUFile *f, Slirp *slirp)
1155{
1156 int i;
1157
1158 for (i = 0; i < NB_BOOTP_CLIENTS; i++) {
1159 slirp->bootp_clients[i].allocated = qemu_get_be16(f);
1160 qemu_get_buffer(f, slirp->bootp_clients[i].macaddr, 6);
1161 }
1162}
1163
aliguori062e5522009-01-08 19:27:07 +00001164static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
1165{
Jan Kiszka460fec62009-06-24 14:42:31 +02001166 Slirp *slirp = opaque;
aliguori062e5522009-01-08 19:27:07 +00001167 struct ex_list *ex_ptr;
aliguori062e5522009-01-08 19:27:07 +00001168
Blue Swirlb0e04862010-03-07 13:45:38 +00001169 while (qemu_get_byte(f)) {
aliguori062e5522009-01-08 19:27:07 +00001170 int ret;
Jan Kiszka460fec62009-06-24 14:42:31 +02001171 struct socket *so = socreate(slirp);
aliguori062e5522009-01-08 19:27:07 +00001172
1173 if (!so)
1174 return -ENOMEM;
1175
1176 ret = slirp_socket_load(f, so);
1177
1178 if (ret < 0)
1179 return ret;
1180
Jan Kiszka460fec62009-06-24 14:42:31 +02001181 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) !=
1182 slirp->vnetwork_addr.s_addr) {
aliguori062e5522009-01-08 19:27:07 +00001183 return -EINVAL;
Jan Kiszkaa13a4122009-06-24 14:42:28 +02001184 }
Jan Kiszka460fec62009-06-24 14:42:31 +02001185 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
aliguori062e5522009-01-08 19:27:07 +00001186 if (ex_ptr->ex_pty == 3 &&
Jan Kiszkaa13a4122009-06-24 14:42:28 +02001187 so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr &&
1188 so->so_fport == ex_ptr->ex_fport) {
aliguori062e5522009-01-08 19:27:07 +00001189 break;
Jan Kiszkaa13a4122009-06-24 14:42:28 +02001190 }
1191 }
aliguori062e5522009-01-08 19:27:07 +00001192 if (!ex_ptr)
1193 return -EINVAL;
1194
blueswir10580ac92009-01-12 17:51:06 +00001195 so->extra = (void *)ex_ptr->ex_exec;
aliguori062e5522009-01-08 19:27:07 +00001196 }
1197
Jan Kiszka285f7a62009-06-24 14:42:30 +02001198 if (version_id >= 2) {
Jan Kiszka460fec62009-06-24 14:42:31 +02001199 slirp->ip_id = qemu_get_be16(f);
Jan Kiszka285f7a62009-06-24 14:42:30 +02001200 }
1201
Jan Kiszka0a1f8512009-06-24 14:42:31 +02001202 if (version_id >= 3) {
1203 slirp_bootp_load(f, slirp);
1204 }
1205
aliguori062e5522009-01-08 19:27:07 +00001206 return 0;
1207}