blob: bad8dad02e97e5350bf966dfa627ccd862d3edd2 [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
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +020040static const uint8_t zero_ethaddr[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
bellardf0cbd3e2004-04-22 00:10:48 +000041
Jan Kiszkaf1d99bb2009-06-24 14:42:30 +020042u_int curtime;
Jan Kiszkaf1d99bb2009-06-24 14:42:30 +020043
Blue Swirl72cf2d42009-09-12 07:36:22 +000044static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances =
45 QTAILQ_HEAD_INITIALIZER(slirp_instances);
pbrook115defd2006-04-16 11:06:58 +000046
Stefan Weil9e3a95e2009-08-31 16:29:34 +020047static struct in_addr dns_addr;
48static u_int dns_addr_time;
Ed Swierkdf7a86e2009-08-20 19:00:31 -070049
Liu Ping Fan9b0ca6c2013-08-25 10:01:20 +080050#define TIMEOUT_FAST 2 /* milliseconds */
51#define TIMEOUT_SLOW 499 /* milliseconds */
52/* for the aging of certain requests like DNS */
53#define TIMEOUT_DEFAULT 1000 /* milliseconds */
54
bellardf0cbd3e2004-04-22 00:10:48 +000055#ifdef _WIN32
56
Ed Swierkdf7a86e2009-08-20 19:00:31 -070057int get_dns_addr(struct in_addr *pdns_addr)
bellardf0cbd3e2004-04-22 00:10:48 +000058{
bellard379ff532004-07-12 22:33:07 +000059 FIXED_INFO *FixedInfo=NULL;
60 ULONG BufLen;
61 DWORD ret;
62 IP_ADDR_STRING *pIPAddr;
63 struct in_addr tmp_addr;
ths3b46e622007-09-17 08:09:54 +000064
Liu Ping Fan9b0ca6c2013-08-25 10:01:20 +080065 if (dns_addr.s_addr != 0 && (curtime - dns_addr_time) < TIMEOUT_DEFAULT) {
Ed Swierkdf7a86e2009-08-20 19:00:31 -070066 *pdns_addr = dns_addr;
67 return 0;
68 }
69
bellard379ff532004-07-12 22:33:07 +000070 FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO));
71 BufLen = sizeof(FIXED_INFO);
ths3b46e622007-09-17 08:09:54 +000072
bellard379ff532004-07-12 22:33:07 +000073 if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen)) {
74 if (FixedInfo) {
75 GlobalFree(FixedInfo);
76 FixedInfo = NULL;
77 }
78 FixedInfo = GlobalAlloc(GPTR, BufLen);
79 }
ths5fafdf22007-09-16 21:08:06 +000080
bellard379ff532004-07-12 22:33:07 +000081 if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS) {
82 printf("GetNetworkParams failed. ret = %08x\n", (u_int)ret );
83 if (FixedInfo) {
84 GlobalFree(FixedInfo);
85 FixedInfo = NULL;
86 }
87 return -1;
88 }
ths3b46e622007-09-17 08:09:54 +000089
bellard379ff532004-07-12 22:33:07 +000090 pIPAddr = &(FixedInfo->DnsServerList);
91 inet_aton(pIPAddr->IpAddress.String, &tmp_addr);
92 *pdns_addr = tmp_addr;
Ed Swierkdf7a86e2009-08-20 19:00:31 -070093 dns_addr = tmp_addr;
94 dns_addr_time = curtime;
bellard379ff532004-07-12 22:33:07 +000095 if (FixedInfo) {
96 GlobalFree(FixedInfo);
97 FixedInfo = NULL;
98 }
99 return 0;
bellardf0cbd3e2004-04-22 00:10:48 +0000100}
101
Jan Kiszkadf461892009-06-24 14:42:30 +0200102static void winsock_cleanup(void)
103{
104 WSACleanup();
105}
106
bellardf0cbd3e2004-04-22 00:10:48 +0000107#else
108
Blue Swirl1e6eec82009-09-05 10:14:07 +0000109static struct stat dns_addr_stat;
Ed Swierkdf7a86e2009-08-20 19:00:31 -0700110
111int get_dns_addr(struct in_addr *pdns_addr)
bellardf0cbd3e2004-04-22 00:10:48 +0000112{
113 char buff[512];
blueswir1363a37d2008-08-21 17:58:08 +0000114 char buff2[257];
bellardf0cbd3e2004-04-22 00:10:48 +0000115 FILE *f;
116 int found = 0;
117 struct in_addr tmp_addr;
ths3b46e622007-09-17 08:09:54 +0000118
Ed Swierkdf7a86e2009-08-20 19:00:31 -0700119 if (dns_addr.s_addr != 0) {
120 struct stat old_stat;
Liu Ping Fan9b0ca6c2013-08-25 10:01:20 +0800121 if ((curtime - dns_addr_time) < TIMEOUT_DEFAULT) {
Ed Swierkdf7a86e2009-08-20 19:00:31 -0700122 *pdns_addr = dns_addr;
123 return 0;
124 }
125 old_stat = dns_addr_stat;
126 if (stat("/etc/resolv.conf", &dns_addr_stat) != 0)
127 return -1;
128 if ((dns_addr_stat.st_dev == old_stat.st_dev)
129 && (dns_addr_stat.st_ino == old_stat.st_ino)
130 && (dns_addr_stat.st_size == old_stat.st_size)
131 && (dns_addr_stat.st_mtime == old_stat.st_mtime)) {
132 *pdns_addr = dns_addr;
133 return 0;
134 }
135 }
136
bellardf0cbd3e2004-04-22 00:10:48 +0000137 f = fopen("/etc/resolv.conf", "r");
138 if (!f)
139 return -1;
140
blueswir131a60e22007-10-26 18:42:59 +0000141#ifdef DEBUG
bellardf0cbd3e2004-04-22 00:10:48 +0000142 lprint("IP address of your DNS(s): ");
blueswir131a60e22007-10-26 18:42:59 +0000143#endif
bellardf0cbd3e2004-04-22 00:10:48 +0000144 while (fgets(buff, 512, f) != NULL) {
145 if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) {
146 if (!inet_aton(buff2, &tmp_addr))
147 continue;
bellardf0cbd3e2004-04-22 00:10:48 +0000148 /* If it's the first one, set it to dns_addr */
Ed Swierkdf7a86e2009-08-20 19:00:31 -0700149 if (!found) {
bellardf0cbd3e2004-04-22 00:10:48 +0000150 *pdns_addr = tmp_addr;
Ed Swierkdf7a86e2009-08-20 19:00:31 -0700151 dns_addr = tmp_addr;
152 dns_addr_time = curtime;
153 }
blueswir131a60e22007-10-26 18:42:59 +0000154#ifdef DEBUG
bellardf0cbd3e2004-04-22 00:10:48 +0000155 else
156 lprint(", ");
blueswir131a60e22007-10-26 18:42:59 +0000157#endif
bellardf0cbd3e2004-04-22 00:10:48 +0000158 if (++found > 3) {
blueswir131a60e22007-10-26 18:42:59 +0000159#ifdef DEBUG
bellardf0cbd3e2004-04-22 00:10:48 +0000160 lprint("(more)");
blueswir131a60e22007-10-26 18:42:59 +0000161#endif
bellardf0cbd3e2004-04-22 00:10:48 +0000162 break;
blueswir131a60e22007-10-26 18:42:59 +0000163 }
164#ifdef DEBUG
165 else
bellardf0cbd3e2004-04-22 00:10:48 +0000166 lprint("%s", inet_ntoa(tmp_addr));
blueswir131a60e22007-10-26 18:42:59 +0000167#endif
bellardf0cbd3e2004-04-22 00:10:48 +0000168 }
169 }
bellard1d43a712004-07-05 21:18:42 +0000170 fclose(f);
bellardf0cbd3e2004-04-22 00:10:48 +0000171 if (!found)
172 return -1;
173 return 0;
174}
175
176#endif
177
Jan Kiszkadf461892009-06-24 14:42:30 +0200178static void slirp_init_once(void)
bellard379ff532004-07-12 22:33:07 +0000179{
Jan Kiszkadf461892009-06-24 14:42:30 +0200180 static int initialized;
Jan Kiszkadf461892009-06-24 14:42:30 +0200181#ifdef _WIN32
182 WSADATA Data;
bellard379ff532004-07-12 22:33:07 +0000183#endif
184
Jan Kiszkadf461892009-06-24 14:42:30 +0200185 if (initialized) {
186 return;
187 }
188 initialized = 1;
189
190#ifdef _WIN32
191 WSAStartup(MAKEWORD(2,0), &Data);
192 atexit(winsock_cleanup);
193#endif
194
195 loopback_addr.s_addr = htonl(INADDR_LOOPBACK);
Anders Waldenborg648cd332012-07-13 22:54:17 +0200196 loopback_mask = htonl(IN_CLASSA_NET);
Jan Kiszkadf461892009-06-24 14:42:30 +0200197}
198
aliguori062e5522009-01-08 19:27:07 +0000199static void slirp_state_save(QEMUFile *f, void *opaque);
200static int slirp_state_load(QEMUFile *f, void *opaque, int version_id);
201
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200202Slirp *slirp_init(int restricted, struct in_addr vnetwork,
203 struct in_addr vnetmask, struct in_addr vhost,
204 const char *vhostname, const char *tftp_path,
205 const char *bootfile, struct in_addr vdhcp_start,
Klaus Stengel63d29602012-10-27 19:53:39 +0200206 struct in_addr vnameserver, const char **vdnssearch,
207 void *opaque)
bellardf0cbd3e2004-04-22 00:10:48 +0000208{
Anthony Liguori7267c092011-08-20 22:09:37 -0500209 Slirp *slirp = g_malloc0(sizeof(Slirp));
Jan Kiszka460fec62009-06-24 14:42:31 +0200210
Jan Kiszkadf461892009-06-24 14:42:30 +0200211 slirp_init_once();
bellard379ff532004-07-12 22:33:07 +0000212
Jan Kiszka460fec62009-06-24 14:42:31 +0200213 slirp->restricted = restricted;
bellardf0cbd3e2004-04-22 00:10:48 +0000214
Jan Kiszka460fec62009-06-24 14:42:31 +0200215 if_init(slirp);
216 ip_init(slirp);
bellardf0cbd3e2004-04-22 00:10:48 +0000217
218 /* Initialise mbufs *after* setting the MTU */
Jan Kiszka460fec62009-06-24 14:42:31 +0200219 m_init(slirp);
bellardf0cbd3e2004-04-22 00:10:48 +0000220
Jan Kiszka460fec62009-06-24 14:42:31 +0200221 slirp->vnetwork_addr = vnetwork;
222 slirp->vnetwork_mask = vnetmask;
223 slirp->vhost_addr = vhost;
Jan Kiszkac92ef6a2009-06-24 14:42:28 +0200224 if (vhostname) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200225 pstrcpy(slirp->client_hostname, sizeof(slirp->client_hostname),
226 vhostname);
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200227 }
Markus Armbrusterc64f50d2013-01-22 11:07:57 +0100228 slirp->tftp_prefix = g_strdup(tftp_path);
229 slirp->bootp_filename = g_strdup(bootfile);
Jan Kiszka460fec62009-06-24 14:42:31 +0200230 slirp->vdhcp_startaddr = vdhcp_start;
231 slirp->vnameserver_addr = vnameserver;
Jan Kiszkaad196a92009-06-24 14:42:28 +0200232
Klaus Stengel63d29602012-10-27 19:53:39 +0200233 if (vdnssearch) {
234 translate_dnssearch(slirp, vdnssearch);
235 }
236
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200237 slirp->opaque = opaque;
238
Alex Williamson0be71e32010-06-25 11:09:07 -0600239 register_savevm(NULL, "slirp", 0, 3,
240 slirp_state_save, slirp_state_load, slirp);
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200241
Blue Swirl72cf2d42009-09-12 07:36:22 +0000242 QTAILQ_INSERT_TAIL(&slirp_instances, slirp, entry);
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200243
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200244 return slirp;
bellardf0cbd3e2004-04-22 00:10:48 +0000245}
246
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200247void slirp_cleanup(Slirp *slirp)
248{
Blue Swirl72cf2d42009-09-12 07:36:22 +0000249 QTAILQ_REMOVE(&slirp_instances, slirp, entry);
Jan Kiszkab1c99fc2009-06-24 14:42:31 +0200250
Alex Williamson0be71e32010-06-25 11:09:07 -0600251 unregister_savevm(NULL, "slirp", slirp);
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200252
Jan Kiszkaa68adc22012-02-29 19:14:23 +0100253 ip_cleanup(slirp);
254 m_cleanup(slirp);
255
Klaus Stengel63d29602012-10-27 19:53:39 +0200256 g_free(slirp->vdnssearch);
Anthony Liguori7267c092011-08-20 22:09:37 -0500257 g_free(slirp->tftp_prefix);
258 g_free(slirp->bootp_filename);
259 g_free(slirp);
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200260}
261
bellardf0cbd3e2004-04-22 00:10:48 +0000262#define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
263#define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
bellardf0cbd3e2004-04-22 00:10:48 +0000264
Liu Ping Fana42e9c42013-08-25 10:01:21 +0800265static void slirp_update_timeout(uint32_t *timeout)
Stefano Stabellini7c7db752012-04-13 19:35:04 +0100266{
Liu Ping Fana42e9c42013-08-25 10:01:21 +0800267 Slirp *slirp;
268 uint32_t t;
269
270 if (*timeout <= TIMEOUT_FAST) {
271 return;
Stefano Stabellini7c7db752012-04-13 19:35:04 +0100272 }
Jan Kiszka426e3e62013-08-28 19:12:15 +0200273
274 t = MIN(1000, *timeout);
Liu Ping Fana42e9c42013-08-25 10:01:21 +0800275
276 /* If we have tcp timeout with slirp, then we will fill @timeout with
277 * more precise value.
278 */
279 QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
280 if (slirp->time_fasttimo) {
281 *timeout = TIMEOUT_FAST;
282 return;
283 }
284 if (slirp->do_slowtimo) {
285 t = MIN(TIMEOUT_SLOW, t);
286 }
287 }
288 *timeout = t;
Stefano Stabellini7c7db752012-04-13 19:35:04 +0100289}
290
Liu Ping Fana42e9c42013-08-25 10:01:21 +0800291void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout)
bellardf0cbd3e2004-04-22 00:10:48 +0000292{
Jan Kiszkab1c99fc2009-06-24 14:42:31 +0200293 Slirp *slirp;
bellardf0cbd3e2004-04-22 00:10:48 +0000294 struct socket *so, *so_next;
bellardf0cbd3e2004-04-22 00:10:48 +0000295
Blue Swirl72cf2d42009-09-12 07:36:22 +0000296 if (QTAILQ_EMPTY(&slirp_instances)) {
Jan Kiszkad918f232009-06-24 14:42:30 +0200297 return;
298 }
299
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100300 /*
301 * First, TCP sockets
302 */
Jan Kiszkad918f232009-06-24 14:42:30 +0200303
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100304 QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
305 /*
306 * *_slowtimo needs calling if there are IP fragments
307 * in the fragment queue, or there are TCP connections active
308 */
Liu Ping Fanfe0ff432013-08-25 10:01:19 +0800309 slirp->do_slowtimo = ((slirp->tcb.so_next != &slirp->tcb) ||
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100310 (&slirp->ipq.ip_link != slirp->ipq.ip_link.next));
ths3b46e622007-09-17 08:09:54 +0000311
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100312 for (so = slirp->tcb.so_next; so != &slirp->tcb;
313 so = so_next) {
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100314 int events = 0;
315
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100316 so_next = so->so_next;
ths3b46e622007-09-17 08:09:54 +0000317
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100318 so->pollfds_idx = -1;
319
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100320 /*
321 * See if we need a tcp_fasttimo
322 */
Liu Ping Fanfe0ff432013-08-25 10:01:19 +0800323 if (slirp->time_fasttimo == 0 &&
324 so->so_tcpcb->t_flags & TF_DELACK) {
325 slirp->time_fasttimo = curtime; /* Flag when want a fasttimo */
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100326 }
ths3b46e622007-09-17 08:09:54 +0000327
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100328 /*
329 * NOFDREF can include still connecting to local-host,
330 * newly socreated() sockets etc. Don't want to select these.
331 */
332 if (so->so_state & SS_NOFDREF || so->s == -1) {
333 continue;
334 }
ths3b46e622007-09-17 08:09:54 +0000335
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100336 /*
337 * Set for reading sockets which are accepting
338 */
339 if (so->so_state & SS_FACCEPTCONN) {
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100340 GPollFD pfd = {
341 .fd = so->s,
342 .events = G_IO_IN | G_IO_HUP | G_IO_ERR,
343 };
344 so->pollfds_idx = pollfds->len;
345 g_array_append_val(pollfds, pfd);
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100346 continue;
347 }
ths3b46e622007-09-17 08:09:54 +0000348
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100349 /*
350 * Set for writing sockets which are connecting
351 */
352 if (so->so_state & SS_ISFCONNECTING) {
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100353 GPollFD pfd = {
354 .fd = so->s,
355 .events = G_IO_OUT | G_IO_ERR,
356 };
357 so->pollfds_idx = pollfds->len;
358 g_array_append_val(pollfds, pfd);
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100359 continue;
360 }
ths3b46e622007-09-17 08:09:54 +0000361
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100362 /*
363 * Set for writing if we are connected, can send more, and
364 * we have something to send
365 */
366 if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100367 events |= G_IO_OUT | G_IO_ERR;
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100368 }
ths3b46e622007-09-17 08:09:54 +0000369
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100370 /*
371 * Set for reading (and urgent data) if we are connected, can
372 * receive more, and we have room for it XXX /2 ?
373 */
374 if (CONN_CANFRCV(so) &&
375 (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100376 events |= G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI;
377 }
378
379 if (events) {
380 GPollFD pfd = {
381 .fd = so->s,
382 .events = events,
383 };
384 so->pollfds_idx = pollfds->len;
385 g_array_append_val(pollfds, pfd);
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100386 }
387 }
ths3b46e622007-09-17 08:09:54 +0000388
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100389 /*
390 * UDP sockets
391 */
392 for (so = slirp->udb.so_next; so != &slirp->udb;
393 so = so_next) {
394 so_next = so->so_next;
ths3b46e622007-09-17 08:09:54 +0000395
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100396 so->pollfds_idx = -1;
397
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100398 /*
399 * See if it's timed out
400 */
401 if (so->so_expire) {
402 if (so->so_expire <= curtime) {
403 udp_detach(so);
404 continue;
405 } else {
Liu Ping Fanfe0ff432013-08-25 10:01:19 +0800406 slirp->do_slowtimo = true; /* Let socket expire */
Jan Kiszkae6d43cf2011-07-20 12:20:18 +0200407 }
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100408 }
ths5fafdf22007-09-16 21:08:06 +0000409
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100410 /*
411 * When UDP packets are received from over the
412 * link, they're sendto()'d straight away, so
413 * no need for setting for writing
414 * Limit the number of packets queued by this session
415 * to 4. Note that even though we try and limit this
416 * to 4 packets, the session could have more queued
417 * if the packets needed to be fragmented
418 * (XXX <= 4 ?)
419 */
420 if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100421 GPollFD pfd = {
422 .fd = so->s,
423 .events = G_IO_IN | G_IO_HUP | G_IO_ERR,
424 };
425 so->pollfds_idx = pollfds->len;
426 g_array_append_val(pollfds, pfd);
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100427 }
428 }
429
430 /*
431 * ICMP sockets
432 */
433 for (so = slirp->icmp.so_next; so != &slirp->icmp;
434 so = so_next) {
435 so_next = so->so_next;
436
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100437 so->pollfds_idx = -1;
438
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100439 /*
440 * See if it's timed out
441 */
442 if (so->so_expire) {
443 if (so->so_expire <= curtime) {
444 icmp_detach(so);
445 continue;
446 } else {
Liu Ping Fanfe0ff432013-08-25 10:01:19 +0800447 slirp->do_slowtimo = true; /* Let socket expire */
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100448 }
449 }
450
451 if (so->so_state & SS_ISFCONNECTED) {
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100452 GPollFD pfd = {
453 .fd = so->s,
454 .events = G_IO_IN | G_IO_HUP | G_IO_ERR,
455 };
456 so->pollfds_idx = pollfds->len;
457 g_array_append_val(pollfds, pfd);
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100458 }
459 }
460 }
Liu Ping Fana42e9c42013-08-25 10:01:21 +0800461 slirp_update_timeout(timeout);
ths5fafdf22007-09-16 21:08:06 +0000462}
bellardf0cbd3e2004-04-22 00:10:48 +0000463
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100464void slirp_pollfds_poll(GArray *pollfds, int select_error)
bellardf0cbd3e2004-04-22 00:10:48 +0000465{
Jan Kiszkab1c99fc2009-06-24 14:42:31 +0200466 Slirp *slirp;
bellardf0cbd3e2004-04-22 00:10:48 +0000467 struct socket *so, *so_next;
468 int ret;
469
Blue Swirl72cf2d42009-09-12 07:36:22 +0000470 if (QTAILQ_EMPTY(&slirp_instances)) {
Jan Kiszkad918f232009-06-24 14:42:30 +0200471 return;
472 }
473
Alex Blighbc72ad62013-08-21 16:03:08 +0100474 curtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME);
ths5fafdf22007-09-16 21:08:06 +0000475
Blue Swirl72cf2d42009-09-12 07:36:22 +0000476 QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100477 /*
478 * See if anything has timed out
479 */
Liu Ping Fan9b0ca6c2013-08-25 10:01:20 +0800480 if (slirp->time_fasttimo &&
481 ((curtime - slirp->time_fasttimo) >= TIMEOUT_FAST)) {
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100482 tcp_fasttimo(slirp);
Liu Ping Fanfe0ff432013-08-25 10:01:19 +0800483 slirp->time_fasttimo = 0;
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100484 }
Liu Ping Fan9b0ca6c2013-08-25 10:01:20 +0800485 if (slirp->do_slowtimo &&
486 ((curtime - slirp->last_slowtimo) >= TIMEOUT_SLOW)) {
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100487 ip_slowtimo(slirp);
488 tcp_slowtimo(slirp);
Liu Ping Fanfe0ff432013-08-25 10:01:19 +0800489 slirp->last_slowtimo = curtime;
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100490 }
ths5fafdf22007-09-16 21:08:06 +0000491
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100492 /*
493 * Check sockets
494 */
495 if (!select_error) {
496 /*
497 * Check TCP sockets
498 */
499 for (so = slirp->tcb.so_next; so != &slirp->tcb;
500 so = so_next) {
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100501 int revents;
502
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100503 so_next = so->so_next;
Jan Kiszkae6d43cf2011-07-20 12:20:18 +0200504
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100505 revents = 0;
506 if (so->pollfds_idx != -1) {
507 revents = g_array_index(pollfds, GPollFD,
508 so->pollfds_idx).revents;
509 }
510
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100511 if (so->so_state & SS_NOFDREF || so->s == -1) {
512 continue;
513 }
Jan Kiszkae6d43cf2011-07-20 12:20:18 +0200514
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100515 /*
516 * Check for URG data
517 * This will soread as well, so no need to
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100518 * test for G_IO_IN below if this succeeds
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100519 */
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100520 if (revents & G_IO_PRI) {
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100521 sorecvoob(so);
522 }
523 /*
524 * Check sockets for reading
525 */
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100526 else if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) {
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100527 /*
528 * Check for incoming connections
529 */
530 if (so->so_state & SS_FACCEPTCONN) {
531 tcp_connect(so);
532 continue;
533 } /* else */
534 ret = soread(so);
535
536 /* Output it if we read something */
537 if (ret > 0) {
538 tcp_output(sototcpcb(so));
Jan Kiszkae6d43cf2011-07-20 12:20:18 +0200539 }
540 }
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100541
542 /*
543 * Check sockets for writing
544 */
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100545 if (!(so->so_state & SS_NOFDREF) &&
546 (revents & (G_IO_OUT | G_IO_ERR))) {
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100547 /*
548 * Check for non-blocking, still-connecting sockets
549 */
550 if (so->so_state & SS_ISFCONNECTING) {
551 /* Connected */
552 so->so_state &= ~SS_ISFCONNECTING;
553
554 ret = send(so->s, (const void *) &ret, 0, 0);
555 if (ret < 0) {
556 /* XXXXX Must fix, zero bytes is a NOP */
557 if (errno == EAGAIN || errno == EWOULDBLOCK ||
558 errno == EINPROGRESS || errno == ENOTCONN) {
559 continue;
560 }
561
562 /* else failed */
563 so->so_state &= SS_PERSISTENT_MASK;
564 so->so_state |= SS_NOFDREF;
565 }
566 /* else so->so_state &= ~SS_ISFCONNECTING; */
567
568 /*
569 * Continue tcp_input
570 */
571 tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
572 /* continue; */
573 } else {
574 ret = sowrite(so);
575 }
576 /*
577 * XXXXX If we wrote something (a lot), there
578 * could be a need for a window update.
579 * In the worst case, the remote will send
580 * a window probe to get things going again
581 */
582 }
583
584 /*
585 * Probe a still-connecting, non-blocking socket
586 * to check if it's still alive
587 */
588#ifdef PROBE_CONN
589 if (so->so_state & SS_ISFCONNECTING) {
590 ret = qemu_recv(so->s, &ret, 0, 0);
591
592 if (ret < 0) {
593 /* XXX */
594 if (errno == EAGAIN || errno == EWOULDBLOCK ||
595 errno == EINPROGRESS || errno == ENOTCONN) {
596 continue; /* Still connecting, continue */
597 }
598
599 /* else failed */
600 so->so_state &= SS_PERSISTENT_MASK;
601 so->so_state |= SS_NOFDREF;
602
603 /* tcp_input will take care of it */
604 } else {
605 ret = send(so->s, &ret, 0, 0);
606 if (ret < 0) {
607 /* XXX */
608 if (errno == EAGAIN || errno == EWOULDBLOCK ||
609 errno == EINPROGRESS || errno == ENOTCONN) {
610 continue;
611 }
612 /* else failed */
613 so->so_state &= SS_PERSISTENT_MASK;
614 so->so_state |= SS_NOFDREF;
615 } else {
616 so->so_state &= ~SS_ISFCONNECTING;
617 }
618
619 }
620 tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
621 } /* SS_ISFCONNECTING */
622#endif
623 }
624
625 /*
626 * Now UDP sockets.
627 * Incoming packets are sent straight away, they're not buffered.
628 * Incoming UDP data isn't buffered either.
629 */
630 for (so = slirp->udb.so_next; so != &slirp->udb;
631 so = so_next) {
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100632 int revents;
633
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100634 so_next = so->so_next;
635
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100636 revents = 0;
637 if (so->pollfds_idx != -1) {
638 revents = g_array_index(pollfds, GPollFD,
639 so->pollfds_idx).revents;
640 }
641
642 if (so->s != -1 &&
643 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) {
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100644 sorecvfrom(so);
645 }
646 }
647
648 /*
649 * Check incoming ICMP relies.
650 */
651 for (so = slirp->icmp.so_next; so != &slirp->icmp;
652 so = so_next) {
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100653 int revents;
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100654
Stefan Hajnoczi8917c3b2013-02-20 11:28:28 +0100655 so_next = so->so_next;
656
657 revents = 0;
658 if (so->pollfds_idx != -1) {
659 revents = g_array_index(pollfds, GPollFD,
660 so->pollfds_idx).revents;
661 }
662
663 if (so->s != -1 &&
664 (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) {
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100665 icmp_receive(so);
666 }
667 }
668 }
ths5fafdf22007-09-16 21:08:06 +0000669
Jan Kiszkaf3734312012-03-06 00:02:23 +0100670 if_start(slirp);
Jan Kiszkab1c99fc2009-06-24 14:42:31 +0200671 }
bellardf0cbd3e2004-04-22 00:10:48 +0000672}
673
Jan Kiszka460fec62009-06-24 14:42:31 +0200674static void arp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
bellardf0cbd3e2004-04-22 00:10:48 +0000675{
bellardf0cbd3e2004-04-22 00:10:48 +0000676 struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
Hervé Poussineaudbf3c4b2010-09-15 22:33:26 +0200677 uint8_t arp_reply[max(ETH_HLEN + sizeof(struct arphdr), 64)];
bellardf0cbd3e2004-04-22 00:10:48 +0000678 struct ethhdr *reh = (struct ethhdr *)arp_reply;
679 struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
680 int ar_op;
bellarda3d4af02004-09-05 23:10:26 +0000681 struct ex_list *ex_ptr;
bellardf0cbd3e2004-04-22 00:10:48 +0000682
683 ar_op = ntohs(ah->ar_op);
684 switch(ar_op) {
685 case ARPOP_REQUEST:
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200686 if (ah->ar_tip == ah->ar_sip) {
687 /* Gratuitous ARP */
688 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
689 return;
690 }
691
Jan Kiszka460fec62009-06-24 14:42:31 +0200692 if ((ah->ar_tip & slirp->vnetwork_mask.s_addr) ==
693 slirp->vnetwork_addr.s_addr) {
694 if (ah->ar_tip == slirp->vnameserver_addr.s_addr ||
695 ah->ar_tip == slirp->vhost_addr.s_addr)
bellarda3d4af02004-09-05 23:10:26 +0000696 goto arp_ok;
Jan Kiszka460fec62009-06-24 14:42:31 +0200697 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200698 if (ex_ptr->ex_addr.s_addr == ah->ar_tip)
bellarda3d4af02004-09-05 23:10:26 +0000699 goto arp_ok;
700 }
701 return;
702 arp_ok:
Hervé Poussineaudbf3c4b2010-09-15 22:33:26 +0200703 memset(arp_reply, 0, sizeof(arp_reply));
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200704
705 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
bellardf0cbd3e2004-04-22 00:10:48 +0000706
707 /* ARP request for alias/dns mac address */
708 memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200709 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
710 memcpy(&reh->h_source[2], &ah->ar_tip, 4);
bellardf0cbd3e2004-04-22 00:10:48 +0000711 reh->h_proto = htons(ETH_P_ARP);
712
713 rah->ar_hrd = htons(1);
714 rah->ar_pro = htons(ETH_P_IP);
715 rah->ar_hln = ETH_ALEN;
716 rah->ar_pln = 4;
717 rah->ar_op = htons(ARPOP_REPLY);
718 memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200719 rah->ar_sip = ah->ar_tip;
bellardf0cbd3e2004-04-22 00:10:48 +0000720 memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200721 rah->ar_tip = ah->ar_sip;
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200722 slirp_output(slirp->opaque, arp_reply, sizeof(arp_reply));
bellardf0cbd3e2004-04-22 00:10:48 +0000723 }
724 break;
bellardde806f02008-10-17 17:28:58 +0000725 case ARPOP_REPLY:
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200726 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
bellardde806f02008-10-17 17:28:58 +0000727 break;
bellardf0cbd3e2004-04-22 00:10:48 +0000728 default:
729 break;
730 }
731}
732
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200733void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
bellardf0cbd3e2004-04-22 00:10:48 +0000734{
735 struct mbuf *m;
736 int proto;
737
738 if (pkt_len < ETH_HLEN)
739 return;
ths3b46e622007-09-17 08:09:54 +0000740
bellardf0cbd3e2004-04-22 00:10:48 +0000741 proto = ntohs(*(uint16_t *)(pkt + 12));
742 switch(proto) {
743 case ETH_P_ARP:
Jan Kiszka460fec62009-06-24 14:42:31 +0200744 arp_input(slirp, pkt, pkt_len);
bellardf0cbd3e2004-04-22 00:10:48 +0000745 break;
746 case ETH_P_IP:
Jan Kiszka460fec62009-06-24 14:42:31 +0200747 m = m_get(slirp);
bellardf0cbd3e2004-04-22 00:10:48 +0000748 if (!m)
749 return;
bellard38f3e7c2006-05-10 19:21:58 +0000750 /* Note: we add to align the IP header */
aurel32e8e880a2008-12-07 18:15:23 +0000751 if (M_FREEROOM(m) < pkt_len + 2) {
752 m_inc(m, pkt_len + 2);
753 }
bellard38f3e7c2006-05-10 19:21:58 +0000754 m->m_len = pkt_len + 2;
755 memcpy(m->m_data + 2, pkt, pkt_len);
bellardf0cbd3e2004-04-22 00:10:48 +0000756
bellard38f3e7c2006-05-10 19:21:58 +0000757 m->m_data += 2 + ETH_HLEN;
758 m->m_len -= 2 + ETH_HLEN;
bellardf0cbd3e2004-04-22 00:10:48 +0000759
760 ip_input(m);
761 break;
762 default:
763 break;
764 }
765}
766
Fabien Chouteau1ab74ce2011-08-01 18:18:37 +0200767/* Output the IP packet to the ethernet device. Returns 0 if the packet must be
768 * re-queued.
769 */
770int if_encap(Slirp *slirp, struct mbuf *ifm)
bellardf0cbd3e2004-04-22 00:10:48 +0000771{
772 uint8_t buf[1600];
773 struct ethhdr *eh = (struct ethhdr *)buf;
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200774 uint8_t ethaddr[ETH_ALEN];
Fabien Chouteau1ab74ce2011-08-01 18:18:37 +0200775 const struct ip *iph = (const struct ip *)ifm->m_data;
bellardf0cbd3e2004-04-22 00:10:48 +0000776
Fabien Chouteau1ab74ce2011-08-01 18:18:37 +0200777 if (ifm->m_len + ETH_HLEN > sizeof(buf)) {
778 return 1;
779 }
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200780
781 if (!arp_table_search(slirp, iph->ip_dst.s_addr, ethaddr)) {
bellardde806f02008-10-17 17:28:58 +0000782 uint8_t arp_req[ETH_HLEN + sizeof(struct arphdr)];
783 struct ethhdr *reh = (struct ethhdr *)arp_req;
784 struct arphdr *rah = (struct arphdr *)(arp_req + ETH_HLEN);
bellardf0cbd3e2004-04-22 00:10:48 +0000785
Fabien Chouteau1ab74ce2011-08-01 18:18:37 +0200786 if (!ifm->arp_requested) {
787 /* If the client addr is not known, send an ARP request */
788 memset(reh->h_dest, 0xff, ETH_ALEN);
789 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
790 memcpy(&reh->h_source[2], &slirp->vhost_addr, 4);
791 reh->h_proto = htons(ETH_P_ARP);
792 rah->ar_hrd = htons(1);
793 rah->ar_pro = htons(ETH_P_IP);
794 rah->ar_hln = ETH_ALEN;
795 rah->ar_pln = 4;
796 rah->ar_op = htons(ARPOP_REQUEST);
797
798 /* source hw addr */
799 memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN - 4);
800 memcpy(&rah->ar_sha[2], &slirp->vhost_addr, 4);
801
802 /* source IP */
803 rah->ar_sip = slirp->vhost_addr.s_addr;
804
805 /* target hw addr (none) */
806 memset(rah->ar_tha, 0, ETH_ALEN);
807
808 /* target IP */
809 rah->ar_tip = iph->ip_dst.s_addr;
810 slirp->client_ipaddr = iph->ip_dst;
811 slirp_output(slirp->opaque, arp_req, sizeof(arp_req));
812 ifm->arp_requested = true;
Jan Kiszkae3a110b2011-08-05 14:05:53 +0200813
814 /* Expire request and drop outgoing packet after 1 second */
Alex Blighbc72ad62013-08-21 16:03:08 +0100815 ifm->expiration_date = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + 1000000000ULL;
Fabien Chouteau1ab74ce2011-08-01 18:18:37 +0200816 }
817 return 0;
bellardde806f02008-10-17 17:28:58 +0000818 } else {
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200819 memcpy(eh->h_dest, ethaddr, ETH_ALEN);
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200820 memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 4);
bellardde806f02008-10-17 17:28:58 +0000821 /* XXX: not correct */
Jan Kiszka460fec62009-06-24 14:42:31 +0200822 memcpy(&eh->h_source[2], &slirp->vhost_addr, 4);
bellardde806f02008-10-17 17:28:58 +0000823 eh->h_proto = htons(ETH_P_IP);
Fabien Chouteau1ab74ce2011-08-01 18:18:37 +0200824 memcpy(buf + sizeof(struct ethhdr), ifm->m_data, ifm->m_len);
825 slirp_output(slirp->opaque, buf, ifm->m_len + ETH_HLEN);
826 return 1;
bellardde806f02008-10-17 17:28:58 +0000827 }
bellardf0cbd3e2004-04-22 00:10:48 +0000828}
bellard9bf05442004-08-25 22:12:49 +0000829
Jan Kiszka9c12a6f2009-06-24 14:42:29 +0200830/* Drop host forwarding rule, return 0 if found. */
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200831int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
832 int host_port)
Alexander Grafc1261d82009-05-26 13:03:26 +0200833{
834 struct socket *so;
Jan Kiszka460fec62009-06-24 14:42:31 +0200835 struct socket *head = (is_udp ? &slirp->udb : &slirp->tcb);
Jan Kiszka2ad82cf2009-06-24 14:42:28 +0200836 struct sockaddr_in addr;
837 int port = htons(host_port);
838 socklen_t addr_len;
Alexander Grafc1261d82009-05-26 13:03:26 +0200839
Alexander Grafc1261d82009-05-26 13:03:26 +0200840 for (so = head->so_next; so != head; so = so->so_next) {
Jan Kiszka2ad82cf2009-06-24 14:42:28 +0200841 addr_len = sizeof(addr);
Jan Kiszka9c12a6f2009-06-24 14:42:29 +0200842 if ((so->so_state & SS_HOSTFWD) &&
843 getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 &&
Jan Kiszka3c6a0582009-06-24 14:42:28 +0200844 addr.sin_addr.s_addr == host_addr.s_addr &&
Jan Kiszka2ad82cf2009-06-24 14:42:28 +0200845 addr.sin_port == port) {
Alexander Grafc1261d82009-05-26 13:03:26 +0200846 close(so->s);
847 sofree(so);
Jan Kiszka9c12a6f2009-06-24 14:42:29 +0200848 return 0;
Alexander Grafc1261d82009-05-26 13:03:26 +0200849 }
850 }
851
Jan Kiszka9c12a6f2009-06-24 14:42:29 +0200852 return -1;
Alexander Grafc1261d82009-05-26 13:03:26 +0200853}
854
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200855int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
856 int host_port, struct in_addr guest_addr, int guest_port)
bellard9bf05442004-08-25 22:12:49 +0000857{
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200858 if (!guest_addr.s_addr) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200859 guest_addr = slirp->vdhcp_startaddr;
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200860 }
bellard9bf05442004-08-25 22:12:49 +0000861 if (is_udp) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200862 if (!udp_listen(slirp, host_addr.s_addr, htons(host_port),
863 guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
bellard9bf05442004-08-25 22:12:49 +0000864 return -1;
865 } else {
Jan Kiszka460fec62009-06-24 14:42:31 +0200866 if (!tcp_listen(slirp, host_addr.s_addr, htons(host_port),
867 guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
bellard9bf05442004-08-25 22:12:49 +0000868 return -1;
869 }
870 return 0;
871}
bellarda3d4af02004-09-05 23:10:26 +0000872
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200873int slirp_add_exec(Slirp *slirp, int do_pty, const void *args,
Jan Kiszkabb53fc52009-07-22 17:03:52 +0200874 struct in_addr *guest_addr, int guest_port)
bellarda3d4af02004-09-05 23:10:26 +0000875{
Jan Kiszkabb53fc52009-07-22 17:03:52 +0200876 if (!guest_addr->s_addr) {
877 guest_addr->s_addr = slirp->vnetwork_addr.s_addr |
Jan Kiszka460fec62009-06-24 14:42:31 +0200878 (htonl(0x0204) & ~slirp->vnetwork_mask.s_addr);
Jan Kiszkac92ef6a2009-06-24 14:42:28 +0200879 }
Jan Kiszkabb53fc52009-07-22 17:03:52 +0200880 if ((guest_addr->s_addr & slirp->vnetwork_mask.s_addr) !=
Jan Kiszka460fec62009-06-24 14:42:31 +0200881 slirp->vnetwork_addr.s_addr ||
Jan Kiszkabb53fc52009-07-22 17:03:52 +0200882 guest_addr->s_addr == slirp->vhost_addr.s_addr ||
883 guest_addr->s_addr == slirp->vnameserver_addr.s_addr) {
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200884 return -1;
885 }
Jan Kiszkabb53fc52009-07-22 17:03:52 +0200886 return add_exec(&slirp->exec_list, do_pty, (char *)args, *guest_addr,
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200887 htons(guest_port));
bellarda3d4af02004-09-05 23:10:26 +0000888}
aliguorie1c5a2b2009-01-08 19:18:21 +0000889
890ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags)
891{
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100892 if (so->s == -1 && so->extra) {
893 qemu_chr_fe_write(so->extra, buf, len);
894 return len;
895 }
aliguorie1c5a2b2009-01-08 19:18:21 +0000896
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100897 return send(so->s, buf, len, flags);
aliguorie1c5a2b2009-01-08 19:18:21 +0000898}
899
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200900static struct socket *
Jan Kiszka460fec62009-06-24 14:42:31 +0200901slirp_find_ctl_socket(Slirp *slirp, struct in_addr guest_addr, int guest_port)
aliguorie1c5a2b2009-01-08 19:18:21 +0000902{
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200903 struct socket *so;
aliguorie1c5a2b2009-01-08 19:18:21 +0000904
Jan Kiszka460fec62009-06-24 14:42:31 +0200905 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200906 if (so->so_faddr.s_addr == guest_addr.s_addr &&
907 htons(so->so_fport) == guest_port) {
908 return so;
909 }
910 }
911 return NULL;
aliguorie1c5a2b2009-01-08 19:18:21 +0000912}
913
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200914size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr,
915 int guest_port)
aliguorie1c5a2b2009-01-08 19:18:21 +0000916{
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100917 struct iovec iov[2];
918 struct socket *so;
aliguorie1c5a2b2009-01-08 19:18:21 +0000919
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100920 so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
aliguorie1c5a2b2009-01-08 19:18:21 +0000921
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100922 if (!so || so->so_state & SS_NOFDREF) {
923 return 0;
924 }
aliguorie1c5a2b2009-01-08 19:18:21 +0000925
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100926 if (!CONN_CANFRCV(so) || so->so_snd.sb_cc >= (so->so_snd.sb_datalen/2)) {
927 return 0;
928 }
aliguorie1c5a2b2009-01-08 19:18:21 +0000929
Stefan Hajnoczicf1d0782013-02-20 11:28:27 +0100930 return sopreprbuf(so, iov, NULL);
aliguorie1c5a2b2009-01-08 19:18:21 +0000931}
932
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200933void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port,
Jan Kiszkac92ef6a2009-06-24 14:42:28 +0200934 const uint8_t *buf, int size)
aliguorie1c5a2b2009-01-08 19:18:21 +0000935{
936 int ret;
Jan Kiszka460fec62009-06-24 14:42:31 +0200937 struct socket *so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200938
aliguorie1c5a2b2009-01-08 19:18:21 +0000939 if (!so)
940 return;
941
blueswir10580ac92009-01-12 17:51:06 +0000942 ret = soreadbuf(so, (const char *)buf, size);
aliguorie1c5a2b2009-01-08 19:18:21 +0000943
944 if (ret > 0)
945 tcp_output(sototcpcb(so));
946}
aliguori062e5522009-01-08 19:27:07 +0000947
948static void slirp_tcp_save(QEMUFile *f, struct tcpcb *tp)
949{
950 int i;
951
952 qemu_put_sbe16(f, tp->t_state);
953 for (i = 0; i < TCPT_NTIMERS; i++)
954 qemu_put_sbe16(f, tp->t_timer[i]);
955 qemu_put_sbe16(f, tp->t_rxtshift);
956 qemu_put_sbe16(f, tp->t_rxtcur);
957 qemu_put_sbe16(f, tp->t_dupacks);
958 qemu_put_be16(f, tp->t_maxseg);
959 qemu_put_sbyte(f, tp->t_force);
960 qemu_put_be16(f, tp->t_flags);
961 qemu_put_be32(f, tp->snd_una);
962 qemu_put_be32(f, tp->snd_nxt);
963 qemu_put_be32(f, tp->snd_up);
964 qemu_put_be32(f, tp->snd_wl1);
965 qemu_put_be32(f, tp->snd_wl2);
966 qemu_put_be32(f, tp->iss);
967 qemu_put_be32(f, tp->snd_wnd);
968 qemu_put_be32(f, tp->rcv_wnd);
969 qemu_put_be32(f, tp->rcv_nxt);
970 qemu_put_be32(f, tp->rcv_up);
971 qemu_put_be32(f, tp->irs);
972 qemu_put_be32(f, tp->rcv_adv);
973 qemu_put_be32(f, tp->snd_max);
974 qemu_put_be32(f, tp->snd_cwnd);
975 qemu_put_be32(f, tp->snd_ssthresh);
976 qemu_put_sbe16(f, tp->t_idle);
977 qemu_put_sbe16(f, tp->t_rtt);
978 qemu_put_be32(f, tp->t_rtseq);
979 qemu_put_sbe16(f, tp->t_srtt);
980 qemu_put_sbe16(f, tp->t_rttvar);
981 qemu_put_be16(f, tp->t_rttmin);
982 qemu_put_be32(f, tp->max_sndwnd);
983 qemu_put_byte(f, tp->t_oobflags);
984 qemu_put_byte(f, tp->t_iobc);
985 qemu_put_sbe16(f, tp->t_softerror);
986 qemu_put_byte(f, tp->snd_scale);
987 qemu_put_byte(f, tp->rcv_scale);
988 qemu_put_byte(f, tp->request_r_scale);
989 qemu_put_byte(f, tp->requested_s_scale);
990 qemu_put_be32(f, tp->ts_recent);
991 qemu_put_be32(f, tp->ts_recent_age);
992 qemu_put_be32(f, tp->last_ack_sent);
993}
994
995static void slirp_sbuf_save(QEMUFile *f, struct sbuf *sbuf)
996{
997 uint32_t off;
998
999 qemu_put_be32(f, sbuf->sb_cc);
1000 qemu_put_be32(f, sbuf->sb_datalen);
1001 off = (uint32_t)(sbuf->sb_wptr - sbuf->sb_data);
1002 qemu_put_sbe32(f, off);
1003 off = (uint32_t)(sbuf->sb_rptr - sbuf->sb_data);
1004 qemu_put_sbe32(f, off);
1005 qemu_put_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen);
1006}
1007
1008static void slirp_socket_save(QEMUFile *f, struct socket *so)
1009{
1010 qemu_put_be32(f, so->so_urgc);
1011 qemu_put_be32(f, so->so_faddr.s_addr);
1012 qemu_put_be32(f, so->so_laddr.s_addr);
1013 qemu_put_be16(f, so->so_fport);
1014 qemu_put_be16(f, so->so_lport);
1015 qemu_put_byte(f, so->so_iptos);
1016 qemu_put_byte(f, so->so_emu);
1017 qemu_put_byte(f, so->so_type);
1018 qemu_put_be32(f, so->so_state);
1019 slirp_sbuf_save(f, &so->so_rcv);
1020 slirp_sbuf_save(f, &so->so_snd);
1021 slirp_tcp_save(f, so->so_tcpcb);
1022}
1023
Jan Kiszka0a1f8512009-06-24 14:42:31 +02001024static void slirp_bootp_save(QEMUFile *f, Slirp *slirp)
1025{
1026 int i;
1027
1028 for (i = 0; i < NB_BOOTP_CLIENTS; i++) {
1029 qemu_put_be16(f, slirp->bootp_clients[i].allocated);
1030 qemu_put_buffer(f, slirp->bootp_clients[i].macaddr, 6);
1031 }
1032}
1033
aliguori062e5522009-01-08 19:27:07 +00001034static void slirp_state_save(QEMUFile *f, void *opaque)
1035{
Jan Kiszka460fec62009-06-24 14:42:31 +02001036 Slirp *slirp = opaque;
aliguori062e5522009-01-08 19:27:07 +00001037 struct ex_list *ex_ptr;
1038
Jan Kiszka460fec62009-06-24 14:42:31 +02001039 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
aliguori062e5522009-01-08 19:27:07 +00001040 if (ex_ptr->ex_pty == 3) {
1041 struct socket *so;
Jan Kiszka460fec62009-06-24 14:42:31 +02001042 so = slirp_find_ctl_socket(slirp, ex_ptr->ex_addr,
1043 ntohs(ex_ptr->ex_fport));
aliguori062e5522009-01-08 19:27:07 +00001044 if (!so)
1045 continue;
1046
1047 qemu_put_byte(f, 42);
1048 slirp_socket_save(f, so);
1049 }
1050 qemu_put_byte(f, 0);
Jan Kiszka285f7a62009-06-24 14:42:30 +02001051
Jan Kiszka460fec62009-06-24 14:42:31 +02001052 qemu_put_be16(f, slirp->ip_id);
Jan Kiszka0a1f8512009-06-24 14:42:31 +02001053
1054 slirp_bootp_save(f, slirp);
aliguori062e5522009-01-08 19:27:07 +00001055}
1056
1057static void slirp_tcp_load(QEMUFile *f, struct tcpcb *tp)
1058{
1059 int i;
1060
1061 tp->t_state = qemu_get_sbe16(f);
1062 for (i = 0; i < TCPT_NTIMERS; i++)
1063 tp->t_timer[i] = qemu_get_sbe16(f);
1064 tp->t_rxtshift = qemu_get_sbe16(f);
1065 tp->t_rxtcur = qemu_get_sbe16(f);
1066 tp->t_dupacks = qemu_get_sbe16(f);
1067 tp->t_maxseg = qemu_get_be16(f);
1068 tp->t_force = qemu_get_sbyte(f);
1069 tp->t_flags = qemu_get_be16(f);
1070 tp->snd_una = qemu_get_be32(f);
1071 tp->snd_nxt = qemu_get_be32(f);
1072 tp->snd_up = qemu_get_be32(f);
1073 tp->snd_wl1 = qemu_get_be32(f);
1074 tp->snd_wl2 = qemu_get_be32(f);
1075 tp->iss = qemu_get_be32(f);
1076 tp->snd_wnd = qemu_get_be32(f);
1077 tp->rcv_wnd = qemu_get_be32(f);
1078 tp->rcv_nxt = qemu_get_be32(f);
1079 tp->rcv_up = qemu_get_be32(f);
1080 tp->irs = qemu_get_be32(f);
1081 tp->rcv_adv = qemu_get_be32(f);
1082 tp->snd_max = qemu_get_be32(f);
1083 tp->snd_cwnd = qemu_get_be32(f);
1084 tp->snd_ssthresh = qemu_get_be32(f);
1085 tp->t_idle = qemu_get_sbe16(f);
1086 tp->t_rtt = qemu_get_sbe16(f);
1087 tp->t_rtseq = qemu_get_be32(f);
1088 tp->t_srtt = qemu_get_sbe16(f);
1089 tp->t_rttvar = qemu_get_sbe16(f);
1090 tp->t_rttmin = qemu_get_be16(f);
1091 tp->max_sndwnd = qemu_get_be32(f);
1092 tp->t_oobflags = qemu_get_byte(f);
1093 tp->t_iobc = qemu_get_byte(f);
1094 tp->t_softerror = qemu_get_sbe16(f);
1095 tp->snd_scale = qemu_get_byte(f);
1096 tp->rcv_scale = qemu_get_byte(f);
1097 tp->request_r_scale = qemu_get_byte(f);
1098 tp->requested_s_scale = qemu_get_byte(f);
1099 tp->ts_recent = qemu_get_be32(f);
1100 tp->ts_recent_age = qemu_get_be32(f);
1101 tp->last_ack_sent = qemu_get_be32(f);
1102 tcp_template(tp);
1103}
1104
1105static int slirp_sbuf_load(QEMUFile *f, struct sbuf *sbuf)
1106{
1107 uint32_t off, sb_cc, sb_datalen;
1108
1109 sb_cc = qemu_get_be32(f);
1110 sb_datalen = qemu_get_be32(f);
1111
1112 sbreserve(sbuf, sb_datalen);
1113
1114 if (sbuf->sb_datalen != sb_datalen)
1115 return -ENOMEM;
1116
1117 sbuf->sb_cc = sb_cc;
1118
1119 off = qemu_get_sbe32(f);
1120 sbuf->sb_wptr = sbuf->sb_data + off;
1121 off = qemu_get_sbe32(f);
1122 sbuf->sb_rptr = sbuf->sb_data + off;
1123 qemu_get_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen);
1124
1125 return 0;
1126}
1127
1128static int slirp_socket_load(QEMUFile *f, struct socket *so)
1129{
1130 if (tcp_attach(so) < 0)
1131 return -ENOMEM;
1132
1133 so->so_urgc = qemu_get_be32(f);
1134 so->so_faddr.s_addr = qemu_get_be32(f);
1135 so->so_laddr.s_addr = qemu_get_be32(f);
1136 so->so_fport = qemu_get_be16(f);
1137 so->so_lport = qemu_get_be16(f);
1138 so->so_iptos = qemu_get_byte(f);
1139 so->so_emu = qemu_get_byte(f);
1140 so->so_type = qemu_get_byte(f);
1141 so->so_state = qemu_get_be32(f);
1142 if (slirp_sbuf_load(f, &so->so_rcv) < 0)
1143 return -ENOMEM;
1144 if (slirp_sbuf_load(f, &so->so_snd) < 0)
1145 return -ENOMEM;
1146 slirp_tcp_load(f, so->so_tcpcb);
1147
1148 return 0;
1149}
1150
Jan Kiszka0a1f8512009-06-24 14:42:31 +02001151static void slirp_bootp_load(QEMUFile *f, Slirp *slirp)
1152{
1153 int i;
1154
1155 for (i = 0; i < NB_BOOTP_CLIENTS; i++) {
1156 slirp->bootp_clients[i].allocated = qemu_get_be16(f);
1157 qemu_get_buffer(f, slirp->bootp_clients[i].macaddr, 6);
1158 }
1159}
1160
aliguori062e5522009-01-08 19:27:07 +00001161static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
1162{
Jan Kiszka460fec62009-06-24 14:42:31 +02001163 Slirp *slirp = opaque;
aliguori062e5522009-01-08 19:27:07 +00001164 struct ex_list *ex_ptr;
aliguori062e5522009-01-08 19:27:07 +00001165
Blue Swirlb0e04862010-03-07 13:45:38 +00001166 while (qemu_get_byte(f)) {
aliguori062e5522009-01-08 19:27:07 +00001167 int ret;
Jan Kiszka460fec62009-06-24 14:42:31 +02001168 struct socket *so = socreate(slirp);
aliguori062e5522009-01-08 19:27:07 +00001169
1170 if (!so)
1171 return -ENOMEM;
1172
1173 ret = slirp_socket_load(f, so);
1174
1175 if (ret < 0)
1176 return ret;
1177
Jan Kiszka460fec62009-06-24 14:42:31 +02001178 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) !=
1179 slirp->vnetwork_addr.s_addr) {
aliguori062e5522009-01-08 19:27:07 +00001180 return -EINVAL;
Jan Kiszkaa13a4122009-06-24 14:42:28 +02001181 }
Jan Kiszka460fec62009-06-24 14:42:31 +02001182 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
aliguori062e5522009-01-08 19:27:07 +00001183 if (ex_ptr->ex_pty == 3 &&
Jan Kiszkaa13a4122009-06-24 14:42:28 +02001184 so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr &&
1185 so->so_fport == ex_ptr->ex_fport) {
aliguori062e5522009-01-08 19:27:07 +00001186 break;
Jan Kiszkaa13a4122009-06-24 14:42:28 +02001187 }
1188 }
aliguori062e5522009-01-08 19:27:07 +00001189 if (!ex_ptr)
1190 return -EINVAL;
1191
blueswir10580ac92009-01-12 17:51:06 +00001192 so->extra = (void *)ex_ptr->ex_exec;
aliguori062e5522009-01-08 19:27:07 +00001193 }
1194
Jan Kiszka285f7a62009-06-24 14:42:30 +02001195 if (version_id >= 2) {
Jan Kiszka460fec62009-06-24 14:42:31 +02001196 slirp->ip_id = qemu_get_be16(f);
Jan Kiszka285f7a62009-06-24 14:42:30 +02001197 }
1198
Jan Kiszka0a1f8512009-06-24 14:42:31 +02001199 if (version_id >= 3) {
1200 slirp_bootp_load(f, slirp);
1201 }
1202
aliguori062e5522009-01-08 19:27:07 +00001203 return 0;
1204}