blob: 19d69eb623c29f7042ef55c84311d8bcac7cd74a [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"
Ed Swierkaaf10d92009-07-23 14:13:34 -070025#include "qemu-timer.h"
blueswir10580ac92009-01-12 17:51:06 +000026#include "qemu-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;
32
Jan Kiszkaa13a4122009-06-24 14:42:28 +020033/* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +020034static const uint8_t special_ethaddr[ETH_ALEN] = {
Jan Kiszkaa13a4122009-06-24 14:42:28 +020035 0x52, 0x55, 0x00, 0x00, 0x00, 0x00
bellardf0cbd3e2004-04-22 00:10:48 +000036};
37
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +020038static const uint8_t zero_ethaddr[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 };
bellardf0cbd3e2004-04-22 00:10:48 +000039
bellardf0cbd3e2004-04-22 00:10:48 +000040/* XXX: suppress those select globals */
41fd_set *global_readfds, *global_writefds, *global_xfds;
42
Jan Kiszkaf1d99bb2009-06-24 14:42:30 +020043u_int curtime;
44static u_int time_fasttimo, last_slowtimo;
45static int do_slowtimo;
46
Blue Swirl72cf2d42009-09-12 07:36:22 +000047static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances =
48 QTAILQ_HEAD_INITIALIZER(slirp_instances);
pbrook115defd2006-04-16 11:06:58 +000049
Stefan Weil9e3a95e2009-08-31 16:29:34 +020050static struct in_addr dns_addr;
51static u_int dns_addr_time;
Ed Swierkdf7a86e2009-08-20 19:00:31 -070052
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
Ed Swierkdf7a86e2009-08-20 19:00:31 -070063 if (dns_addr.s_addr != 0 && (curtime - dns_addr_time) < 1000) {
64 *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;
119 if ((curtime - dns_addr_time) < 1000) {
120 *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
bellardf0cbd3e2004-04-22 00:10:48 +0000140 lprint("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
154 lprint(", ");
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
bellardf0cbd3e2004-04-22 00:10:48 +0000158 lprint("(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
bellardf0cbd3e2004-04-22 00:10:48 +0000164 lprint("%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);
Jan Kiszkadf461892009-06-24 14:42:30 +0200194}
195
aliguori062e5522009-01-08 19:27:07 +0000196static void slirp_state_save(QEMUFile *f, void *opaque);
197static int slirp_state_load(QEMUFile *f, void *opaque, int version_id);
198
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200199Slirp *slirp_init(int restricted, struct in_addr vnetwork,
200 struct in_addr vnetmask, struct in_addr vhost,
201 const char *vhostname, const char *tftp_path,
202 const char *bootfile, struct in_addr vdhcp_start,
203 struct in_addr vnameserver, void *opaque)
bellardf0cbd3e2004-04-22 00:10:48 +0000204{
Anthony Liguori7267c092011-08-20 22:09:37 -0500205 Slirp *slirp = g_malloc0(sizeof(Slirp));
Jan Kiszka460fec62009-06-24 14:42:31 +0200206
Jan Kiszkadf461892009-06-24 14:42:30 +0200207 slirp_init_once();
bellard379ff532004-07-12 22:33:07 +0000208
Jan Kiszka460fec62009-06-24 14:42:31 +0200209 slirp->restricted = restricted;
bellardf0cbd3e2004-04-22 00:10:48 +0000210
Jan Kiszka460fec62009-06-24 14:42:31 +0200211 if_init(slirp);
212 ip_init(slirp);
bellardf0cbd3e2004-04-22 00:10:48 +0000213
214 /* Initialise mbufs *after* setting the MTU */
Jan Kiszka460fec62009-06-24 14:42:31 +0200215 m_init(slirp);
bellardf0cbd3e2004-04-22 00:10:48 +0000216
Jan Kiszka460fec62009-06-24 14:42:31 +0200217 slirp->vnetwork_addr = vnetwork;
218 slirp->vnetwork_mask = vnetmask;
219 slirp->vhost_addr = vhost;
Jan Kiszkac92ef6a2009-06-24 14:42:28 +0200220 if (vhostname) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200221 pstrcpy(slirp->client_hostname, sizeof(slirp->client_hostname),
222 vhostname);
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200223 }
Jan Kiszkaad196a92009-06-24 14:42:28 +0200224 if (tftp_path) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500225 slirp->tftp_prefix = g_strdup(tftp_path);
Jan Kiszkaad196a92009-06-24 14:42:28 +0200226 }
Jan Kiszkaad196a92009-06-24 14:42:28 +0200227 if (bootfile) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500228 slirp->bootp_filename = g_strdup(bootfile);
Jan Kiszkaad196a92009-06-24 14:42:28 +0200229 }
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
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200233 slirp->opaque = opaque;
234
Alex Williamson0be71e32010-06-25 11:09:07 -0600235 register_savevm(NULL, "slirp", 0, 3,
236 slirp_state_save, slirp_state_load, slirp);
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200237
Blue Swirl72cf2d42009-09-12 07:36:22 +0000238 QTAILQ_INSERT_TAIL(&slirp_instances, slirp, entry);
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200239
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200240 return slirp;
bellardf0cbd3e2004-04-22 00:10:48 +0000241}
242
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200243void slirp_cleanup(Slirp *slirp)
244{
Blue Swirl72cf2d42009-09-12 07:36:22 +0000245 QTAILQ_REMOVE(&slirp_instances, slirp, entry);
Jan Kiszkab1c99fc2009-06-24 14:42:31 +0200246
Alex Williamson0be71e32010-06-25 11:09:07 -0600247 unregister_savevm(NULL, "slirp", slirp);
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200248
Anthony Liguori7267c092011-08-20 22:09:37 -0500249 g_free(slirp->tftp_prefix);
250 g_free(slirp->bootp_filename);
251 g_free(slirp);
Jan Kiszkaad0d8c42009-06-24 14:42:31 +0200252}
253
bellardf0cbd3e2004-04-22 00:10:48 +0000254#define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
255#define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED)
256#define UPD_NFDS(x) if (nfds < (x)) nfds = (x)
257
ths5fafdf22007-09-16 21:08:06 +0000258void slirp_select_fill(int *pnfds,
bellardf0cbd3e2004-04-22 00:10:48 +0000259 fd_set *readfds, fd_set *writefds, fd_set *xfds)
260{
Jan Kiszkab1c99fc2009-06-24 14:42:31 +0200261 Slirp *slirp;
bellardf0cbd3e2004-04-22 00:10:48 +0000262 struct socket *so, *so_next;
bellardf0cbd3e2004-04-22 00:10:48 +0000263 int nfds;
bellardf0cbd3e2004-04-22 00:10:48 +0000264
Blue Swirl72cf2d42009-09-12 07:36:22 +0000265 if (QTAILQ_EMPTY(&slirp_instances)) {
Jan Kiszkad918f232009-06-24 14:42:30 +0200266 return;
267 }
268
bellardf0cbd3e2004-04-22 00:10:48 +0000269 /* fail safe */
270 global_readfds = NULL;
271 global_writefds = NULL;
272 global_xfds = NULL;
ths3b46e622007-09-17 08:09:54 +0000273
bellardf0cbd3e2004-04-22 00:10:48 +0000274 nfds = *pnfds;
275 /*
276 * First, TCP sockets
277 */
278 do_slowtimo = 0;
Jan Kiszkad918f232009-06-24 14:42:30 +0200279
Blue Swirl72cf2d42009-09-12 07:36:22 +0000280 QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
ths5fafdf22007-09-16 21:08:06 +0000281 /*
bellardf0cbd3e2004-04-22 00:10:48 +0000282 * *_slowtimo needs calling if there are IP fragments
283 * in the fragment queue, or there are TCP connections active
284 */
Jan Kiszkab1c99fc2009-06-24 14:42:31 +0200285 do_slowtimo |= ((slirp->tcb.so_next != &slirp->tcb) ||
Jan Kiszka460fec62009-06-24 14:42:31 +0200286 (&slirp->ipq.ip_link != slirp->ipq.ip_link.next));
ths3b46e622007-09-17 08:09:54 +0000287
Jan Kiszka460fec62009-06-24 14:42:31 +0200288 for (so = slirp->tcb.so_next; so != &slirp->tcb;
289 so = so_next) {
bellardf0cbd3e2004-04-22 00:10:48 +0000290 so_next = so->so_next;
ths3b46e622007-09-17 08:09:54 +0000291
bellardf0cbd3e2004-04-22 00:10:48 +0000292 /*
293 * See if we need a tcp_fasttimo
294 */
295 if (time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK)
296 time_fasttimo = curtime; /* Flag when we want a fasttimo */
ths3b46e622007-09-17 08:09:54 +0000297
bellardf0cbd3e2004-04-22 00:10:48 +0000298 /*
299 * NOFDREF can include still connecting to local-host,
300 * newly socreated() sockets etc. Don't want to select these.
301 */
302 if (so->so_state & SS_NOFDREF || so->s == -1)
303 continue;
ths3b46e622007-09-17 08:09:54 +0000304
bellardf0cbd3e2004-04-22 00:10:48 +0000305 /*
306 * Set for reading sockets which are accepting
307 */
308 if (so->so_state & SS_FACCEPTCONN) {
309 FD_SET(so->s, readfds);
310 UPD_NFDS(so->s);
311 continue;
312 }
ths3b46e622007-09-17 08:09:54 +0000313
bellardf0cbd3e2004-04-22 00:10:48 +0000314 /*
315 * Set for writing sockets which are connecting
316 */
317 if (so->so_state & SS_ISFCONNECTING) {
318 FD_SET(so->s, writefds);
319 UPD_NFDS(so->s);
320 continue;
321 }
ths3b46e622007-09-17 08:09:54 +0000322
bellardf0cbd3e2004-04-22 00:10:48 +0000323 /*
324 * Set for writing if we are connected, can send more, and
325 * we have something to send
326 */
327 if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) {
328 FD_SET(so->s, writefds);
329 UPD_NFDS(so->s);
330 }
ths3b46e622007-09-17 08:09:54 +0000331
bellardf0cbd3e2004-04-22 00:10:48 +0000332 /*
333 * Set for reading (and urgent data) if we are connected, can
334 * receive more, and we have room for it XXX /2 ?
335 */
336 if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) {
337 FD_SET(so->s, readfds);
338 FD_SET(so->s, xfds);
339 UPD_NFDS(so->s);
340 }
341 }
ths3b46e622007-09-17 08:09:54 +0000342
bellardf0cbd3e2004-04-22 00:10:48 +0000343 /*
344 * UDP sockets
345 */
Jan Kiszka460fec62009-06-24 14:42:31 +0200346 for (so = slirp->udb.so_next; so != &slirp->udb;
347 so = so_next) {
bellardf0cbd3e2004-04-22 00:10:48 +0000348 so_next = so->so_next;
ths3b46e622007-09-17 08:09:54 +0000349
bellardf0cbd3e2004-04-22 00:10:48 +0000350 /*
351 * See if it's timed out
352 */
353 if (so->so_expire) {
354 if (so->so_expire <= curtime) {
355 udp_detach(so);
356 continue;
357 } else
358 do_slowtimo = 1; /* Let socket expire */
359 }
ths3b46e622007-09-17 08:09:54 +0000360
bellardf0cbd3e2004-04-22 00:10:48 +0000361 /*
362 * When UDP packets are received from over the
363 * link, they're sendto()'d straight away, so
364 * no need for setting for writing
365 * Limit the number of packets queued by this session
366 * to 4. Note that even though we try and limit this
367 * to 4 packets, the session could have more queued
368 * if the packets needed to be fragmented
369 * (XXX <= 4 ?)
370 */
371 if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) {
372 FD_SET(so->s, readfds);
373 UPD_NFDS(so->s);
374 }
375 }
Jan Kiszkae6d43cf2011-07-20 12:20:18 +0200376
377 /*
378 * ICMP sockets
379 */
380 for (so = slirp->icmp.so_next; so != &slirp->icmp;
381 so = so_next) {
382 so_next = so->so_next;
383
384 /*
385 * See if it's timed out
386 */
387 if (so->so_expire) {
388 if (so->so_expire <= curtime) {
389 icmp_detach(so);
390 continue;
391 } else {
392 do_slowtimo = 1; /* Let socket expire */
393 }
394 }
395
396 if (so->so_state & SS_ISFCONNECTED) {
397 FD_SET(so->s, readfds);
398 UPD_NFDS(so->s);
399 }
400 }
Jan Kiszkab1c99fc2009-06-24 14:42:31 +0200401 }
ths5fafdf22007-09-16 21:08:06 +0000402
bellardf0cbd3e2004-04-22 00:10:48 +0000403 *pnfds = nfds;
ths5fafdf22007-09-16 21:08:06 +0000404}
bellardf0cbd3e2004-04-22 00:10:48 +0000405
Jan Kiszkad918f232009-06-24 14:42:30 +0200406void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds,
407 int select_error)
bellardf0cbd3e2004-04-22 00:10:48 +0000408{
Jan Kiszkab1c99fc2009-06-24 14:42:31 +0200409 Slirp *slirp;
bellardf0cbd3e2004-04-22 00:10:48 +0000410 struct socket *so, *so_next;
411 int ret;
412
Blue Swirl72cf2d42009-09-12 07:36:22 +0000413 if (QTAILQ_EMPTY(&slirp_instances)) {
Jan Kiszkad918f232009-06-24 14:42:30 +0200414 return;
415 }
416
bellardf0cbd3e2004-04-22 00:10:48 +0000417 global_readfds = readfds;
418 global_writefds = writefds;
419 global_xfds = xfds;
420
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100421 curtime = qemu_get_clock_ms(rt_clock);
ths5fafdf22007-09-16 21:08:06 +0000422
Blue Swirl72cf2d42009-09-12 07:36:22 +0000423 QTAILQ_FOREACH(slirp, &slirp_instances, entry) {
bellardf0cbd3e2004-04-22 00:10:48 +0000424 /*
ths5fafdf22007-09-16 21:08:06 +0000425 * See if anything has timed out
bellardf0cbd3e2004-04-22 00:10:48 +0000426 */
bellarddf5f8952005-09-03 10:45:09 +0000427 if (time_fasttimo && ((curtime - time_fasttimo) >= 2)) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200428 tcp_fasttimo(slirp);
bellardf0cbd3e2004-04-22 00:10:48 +0000429 time_fasttimo = 0;
430 }
431 if (do_slowtimo && ((curtime - last_slowtimo) >= 499)) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200432 ip_slowtimo(slirp);
433 tcp_slowtimo(slirp);
bellardf0cbd3e2004-04-22 00:10:48 +0000434 last_slowtimo = curtime;
435 }
ths5fafdf22007-09-16 21:08:06 +0000436
bellardf0cbd3e2004-04-22 00:10:48 +0000437 /*
438 * Check sockets
439 */
Jan Kiszkad918f232009-06-24 14:42:30 +0200440 if (!select_error) {
bellardf0cbd3e2004-04-22 00:10:48 +0000441 /*
442 * Check TCP sockets
443 */
Jan Kiszka460fec62009-06-24 14:42:31 +0200444 for (so = slirp->tcb.so_next; so != &slirp->tcb;
445 so = so_next) {
bellardf0cbd3e2004-04-22 00:10:48 +0000446 so_next = so->so_next;
ths3b46e622007-09-17 08:09:54 +0000447
bellardf0cbd3e2004-04-22 00:10:48 +0000448 /*
449 * FD_ISSET is meaningless on these sockets
450 * (and they can crash the program)
451 */
452 if (so->so_state & SS_NOFDREF || so->s == -1)
453 continue;
ths3b46e622007-09-17 08:09:54 +0000454
bellardf0cbd3e2004-04-22 00:10:48 +0000455 /*
456 * Check for URG data
457 * This will soread as well, so no need to
458 * test for readfds below if this succeeds
459 */
460 if (FD_ISSET(so->s, xfds))
461 sorecvoob(so);
462 /*
463 * Check sockets for reading
464 */
465 else if (FD_ISSET(so->s, readfds)) {
466 /*
467 * Check for incoming connections
468 */
469 if (so->so_state & SS_FACCEPTCONN) {
470 tcp_connect(so);
471 continue;
472 } /* else */
473 ret = soread(so);
ths3b46e622007-09-17 08:09:54 +0000474
bellardf0cbd3e2004-04-22 00:10:48 +0000475 /* Output it if we read something */
476 if (ret > 0)
477 tcp_output(sototcpcb(so));
478 }
ths3b46e622007-09-17 08:09:54 +0000479
bellardf0cbd3e2004-04-22 00:10:48 +0000480 /*
481 * Check sockets for writing
482 */
483 if (FD_ISSET(so->s, writefds)) {
484 /*
485 * Check for non-blocking, still-connecting sockets
486 */
487 if (so->so_state & SS_ISFCONNECTING) {
488 /* Connected */
489 so->so_state &= ~SS_ISFCONNECTING;
ths3b46e622007-09-17 08:09:54 +0000490
malc0a656f52009-05-21 05:26:23 +0400491 ret = send(so->s, (const void *) &ret, 0, 0);
bellardf0cbd3e2004-04-22 00:10:48 +0000492 if (ret < 0) {
493 /* XXXXX Must fix, zero bytes is a NOP */
494 if (errno == EAGAIN || errno == EWOULDBLOCK ||
495 errno == EINPROGRESS || errno == ENOTCONN)
496 continue;
ths3b46e622007-09-17 08:09:54 +0000497
bellardf0cbd3e2004-04-22 00:10:48 +0000498 /* else failed */
Jan Kiszkaf932b6c2009-06-24 14:42:29 +0200499 so->so_state &= SS_PERSISTENT_MASK;
500 so->so_state |= SS_NOFDREF;
bellardf0cbd3e2004-04-22 00:10:48 +0000501 }
502 /* else so->so_state &= ~SS_ISFCONNECTING; */
ths3b46e622007-09-17 08:09:54 +0000503
bellardf0cbd3e2004-04-22 00:10:48 +0000504 /*
505 * Continue tcp_input
506 */
507 tcp_input((struct mbuf *)NULL, sizeof(struct ip), so);
508 /* continue; */
509 } else
510 ret = sowrite(so);
511 /*
ths5fafdf22007-09-16 21:08:06 +0000512 * XXXXX If we wrote something (a lot), there
bellardf0cbd3e2004-04-22 00:10:48 +0000513 * could be a need for a window update.
514 * In the worst case, the remote will send
515 * a window probe to get things going again
516 */
517 }
ths3b46e622007-09-17 08:09:54 +0000518
bellardf0cbd3e2004-04-22 00:10:48 +0000519 /*
520 * Probe a still-connecting, non-blocking socket
521 * to check if it's still alive
522 */
523#ifdef PROBE_CONN
524 if (so->so_state & SS_ISFCONNECTING) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000525 ret = qemu_recv(so->s, &ret, 0,0);
ths3b46e622007-09-17 08:09:54 +0000526
bellardf0cbd3e2004-04-22 00:10:48 +0000527 if (ret < 0) {
528 /* XXX */
529 if (errno == EAGAIN || errno == EWOULDBLOCK ||
530 errno == EINPROGRESS || errno == ENOTCONN)
531 continue; /* Still connecting, continue */
ths3b46e622007-09-17 08:09:54 +0000532
bellardf0cbd3e2004-04-22 00:10:48 +0000533 /* else failed */
Jan Kiszkaf932b6c2009-06-24 14:42:29 +0200534 so->so_state &= SS_PERSISTENT_MASK;
535 so->so_state |= SS_NOFDREF;
ths3b46e622007-09-17 08:09:54 +0000536
bellardf0cbd3e2004-04-22 00:10:48 +0000537 /* tcp_input will take care of it */
538 } else {
bellard02d2c542004-10-07 23:27:35 +0000539 ret = send(so->s, &ret, 0,0);
bellardf0cbd3e2004-04-22 00:10:48 +0000540 if (ret < 0) {
541 /* XXX */
542 if (errno == EAGAIN || errno == EWOULDBLOCK ||
543 errno == EINPROGRESS || errno == ENOTCONN)
544 continue;
545 /* else failed */
Jan Kiszkaf932b6c2009-06-24 14:42:29 +0200546 so->so_state &= SS_PERSISTENT_MASK;
547 so->so_state |= SS_NOFDREF;
bellardf0cbd3e2004-04-22 00:10:48 +0000548 } else
549 so->so_state &= ~SS_ISFCONNECTING;
ths3b46e622007-09-17 08:09:54 +0000550
bellardf0cbd3e2004-04-22 00:10:48 +0000551 }
552 tcp_input((struct mbuf *)NULL, sizeof(struct ip),so);
553 } /* SS_ISFCONNECTING */
554#endif
555 }
ths3b46e622007-09-17 08:09:54 +0000556
bellardf0cbd3e2004-04-22 00:10:48 +0000557 /*
558 * Now UDP sockets.
559 * Incoming packets are sent straight away, they're not buffered.
560 * Incoming UDP data isn't buffered either.
561 */
Jan Kiszka460fec62009-06-24 14:42:31 +0200562 for (so = slirp->udb.so_next; so != &slirp->udb;
563 so = so_next) {
bellardf0cbd3e2004-04-22 00:10:48 +0000564 so_next = so->so_next;
ths3b46e622007-09-17 08:09:54 +0000565
bellardf0cbd3e2004-04-22 00:10:48 +0000566 if (so->s != -1 && FD_ISSET(so->s, readfds)) {
567 sorecvfrom(so);
568 }
569 }
Jan Kiszkae6d43cf2011-07-20 12:20:18 +0200570
571 /*
572 * Check incoming ICMP relies.
573 */
574 for (so = slirp->icmp.so_next; so != &slirp->icmp;
575 so = so_next) {
576 so_next = so->so_next;
577
578 if (so->s != -1 && FD_ISSET(so->s, readfds)) {
579 icmp_receive(so);
580 }
581 }
bellardf0cbd3e2004-04-22 00:10:48 +0000582 }
ths5fafdf22007-09-16 21:08:06 +0000583
bellardf0cbd3e2004-04-22 00:10:48 +0000584 /*
585 * See if we can start outputting
586 */
Jan Kiszka460fec62009-06-24 14:42:31 +0200587 if (slirp->if_queued) {
588 if_start(slirp);
589 }
Jan Kiszkab1c99fc2009-06-24 14:42:31 +0200590 }
bellard02d2c542004-10-07 23:27:35 +0000591
592 /* clear global file descriptor sets.
593 * these reside on the stack in vl.c
594 * so they're unusable if we're not in
595 * slirp_select_fill or slirp_select_poll.
596 */
597 global_readfds = NULL;
598 global_writefds = NULL;
599 global_xfds = NULL;
bellardf0cbd3e2004-04-22 00:10:48 +0000600}
601
Jan Kiszka460fec62009-06-24 14:42:31 +0200602static void arp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
bellardf0cbd3e2004-04-22 00:10:48 +0000603{
bellardf0cbd3e2004-04-22 00:10:48 +0000604 struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN);
Hervé Poussineaudbf3c4b2010-09-15 22:33:26 +0200605 uint8_t arp_reply[max(ETH_HLEN + sizeof(struct arphdr), 64)];
bellardf0cbd3e2004-04-22 00:10:48 +0000606 struct ethhdr *reh = (struct ethhdr *)arp_reply;
607 struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN);
608 int ar_op;
bellarda3d4af02004-09-05 23:10:26 +0000609 struct ex_list *ex_ptr;
bellardf0cbd3e2004-04-22 00:10:48 +0000610
611 ar_op = ntohs(ah->ar_op);
612 switch(ar_op) {
613 case ARPOP_REQUEST:
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200614 if (ah->ar_tip == ah->ar_sip) {
615 /* Gratuitous ARP */
616 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
617 return;
618 }
619
Jan Kiszka460fec62009-06-24 14:42:31 +0200620 if ((ah->ar_tip & slirp->vnetwork_mask.s_addr) ==
621 slirp->vnetwork_addr.s_addr) {
622 if (ah->ar_tip == slirp->vnameserver_addr.s_addr ||
623 ah->ar_tip == slirp->vhost_addr.s_addr)
bellarda3d4af02004-09-05 23:10:26 +0000624 goto arp_ok;
Jan Kiszka460fec62009-06-24 14:42:31 +0200625 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200626 if (ex_ptr->ex_addr.s_addr == ah->ar_tip)
bellarda3d4af02004-09-05 23:10:26 +0000627 goto arp_ok;
628 }
629 return;
630 arp_ok:
Hervé Poussineaudbf3c4b2010-09-15 22:33:26 +0200631 memset(arp_reply, 0, sizeof(arp_reply));
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200632
633 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
bellardf0cbd3e2004-04-22 00:10:48 +0000634
635 /* ARP request for alias/dns mac address */
636 memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN);
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200637 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
638 memcpy(&reh->h_source[2], &ah->ar_tip, 4);
bellardf0cbd3e2004-04-22 00:10:48 +0000639 reh->h_proto = htons(ETH_P_ARP);
640
641 rah->ar_hrd = htons(1);
642 rah->ar_pro = htons(ETH_P_IP);
643 rah->ar_hln = ETH_ALEN;
644 rah->ar_pln = 4;
645 rah->ar_op = htons(ARPOP_REPLY);
646 memcpy(rah->ar_sha, reh->h_source, ETH_ALEN);
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200647 rah->ar_sip = ah->ar_tip;
bellardf0cbd3e2004-04-22 00:10:48 +0000648 memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN);
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200649 rah->ar_tip = ah->ar_sip;
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200650 slirp_output(slirp->opaque, arp_reply, sizeof(arp_reply));
bellardf0cbd3e2004-04-22 00:10:48 +0000651 }
652 break;
bellardde806f02008-10-17 17:28:58 +0000653 case ARPOP_REPLY:
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200654 arp_table_add(slirp, ah->ar_sip, ah->ar_sha);
bellardde806f02008-10-17 17:28:58 +0000655 break;
bellardf0cbd3e2004-04-22 00:10:48 +0000656 default:
657 break;
658 }
659}
660
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200661void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len)
bellardf0cbd3e2004-04-22 00:10:48 +0000662{
663 struct mbuf *m;
664 int proto;
665
666 if (pkt_len < ETH_HLEN)
667 return;
ths3b46e622007-09-17 08:09:54 +0000668
bellardf0cbd3e2004-04-22 00:10:48 +0000669 proto = ntohs(*(uint16_t *)(pkt + 12));
670 switch(proto) {
671 case ETH_P_ARP:
Jan Kiszka460fec62009-06-24 14:42:31 +0200672 arp_input(slirp, pkt, pkt_len);
bellardf0cbd3e2004-04-22 00:10:48 +0000673 break;
674 case ETH_P_IP:
Jan Kiszka460fec62009-06-24 14:42:31 +0200675 m = m_get(slirp);
bellardf0cbd3e2004-04-22 00:10:48 +0000676 if (!m)
677 return;
bellard38f3e7c2006-05-10 19:21:58 +0000678 /* Note: we add to align the IP header */
aurel32e8e880a2008-12-07 18:15:23 +0000679 if (M_FREEROOM(m) < pkt_len + 2) {
680 m_inc(m, pkt_len + 2);
681 }
bellard38f3e7c2006-05-10 19:21:58 +0000682 m->m_len = pkt_len + 2;
683 memcpy(m->m_data + 2, pkt, pkt_len);
bellardf0cbd3e2004-04-22 00:10:48 +0000684
bellard38f3e7c2006-05-10 19:21:58 +0000685 m->m_data += 2 + ETH_HLEN;
686 m->m_len -= 2 + ETH_HLEN;
bellardf0cbd3e2004-04-22 00:10:48 +0000687
688 ip_input(m);
689 break;
690 default:
691 break;
692 }
693}
694
Fabien Chouteau1ab74ce2011-08-01 18:18:37 +0200695/* Output the IP packet to the ethernet device. Returns 0 if the packet must be
696 * re-queued.
697 */
698int if_encap(Slirp *slirp, struct mbuf *ifm)
bellardf0cbd3e2004-04-22 00:10:48 +0000699{
700 uint8_t buf[1600];
701 struct ethhdr *eh = (struct ethhdr *)buf;
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200702 uint8_t ethaddr[ETH_ALEN];
Fabien Chouteau1ab74ce2011-08-01 18:18:37 +0200703 const struct ip *iph = (const struct ip *)ifm->m_data;
bellardf0cbd3e2004-04-22 00:10:48 +0000704
Fabien Chouteau1ab74ce2011-08-01 18:18:37 +0200705 if (ifm->m_len + ETH_HLEN > sizeof(buf)) {
706 return 1;
707 }
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200708
709 if (!arp_table_search(slirp, iph->ip_dst.s_addr, ethaddr)) {
bellardde806f02008-10-17 17:28:58 +0000710 uint8_t arp_req[ETH_HLEN + sizeof(struct arphdr)];
711 struct ethhdr *reh = (struct ethhdr *)arp_req;
712 struct arphdr *rah = (struct arphdr *)(arp_req + ETH_HLEN);
bellardf0cbd3e2004-04-22 00:10:48 +0000713
Fabien Chouteau1ab74ce2011-08-01 18:18:37 +0200714 if (!ifm->arp_requested) {
715 /* If the client addr is not known, send an ARP request */
716 memset(reh->h_dest, 0xff, ETH_ALEN);
717 memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4);
718 memcpy(&reh->h_source[2], &slirp->vhost_addr, 4);
719 reh->h_proto = htons(ETH_P_ARP);
720 rah->ar_hrd = htons(1);
721 rah->ar_pro = htons(ETH_P_IP);
722 rah->ar_hln = ETH_ALEN;
723 rah->ar_pln = 4;
724 rah->ar_op = htons(ARPOP_REQUEST);
725
726 /* source hw addr */
727 memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN - 4);
728 memcpy(&rah->ar_sha[2], &slirp->vhost_addr, 4);
729
730 /* source IP */
731 rah->ar_sip = slirp->vhost_addr.s_addr;
732
733 /* target hw addr (none) */
734 memset(rah->ar_tha, 0, ETH_ALEN);
735
736 /* target IP */
737 rah->ar_tip = iph->ip_dst.s_addr;
738 slirp->client_ipaddr = iph->ip_dst;
739 slirp_output(slirp->opaque, arp_req, sizeof(arp_req));
740 ifm->arp_requested = true;
Jan Kiszkae3a110b2011-08-05 14:05:53 +0200741
742 /* Expire request and drop outgoing packet after 1 second */
743 ifm->expiration_date = qemu_get_clock_ns(rt_clock) + 1000000000ULL;
Fabien Chouteau1ab74ce2011-08-01 18:18:37 +0200744 }
745 return 0;
bellardde806f02008-10-17 17:28:58 +0000746 } else {
Fabien Chouteau1a0ca1e2011-08-03 12:52:54 +0200747 memcpy(eh->h_dest, ethaddr, ETH_ALEN);
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200748 memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 4);
bellardde806f02008-10-17 17:28:58 +0000749 /* XXX: not correct */
Jan Kiszka460fec62009-06-24 14:42:31 +0200750 memcpy(&eh->h_source[2], &slirp->vhost_addr, 4);
bellardde806f02008-10-17 17:28:58 +0000751 eh->h_proto = htons(ETH_P_IP);
Fabien Chouteau1ab74ce2011-08-01 18:18:37 +0200752 memcpy(buf + sizeof(struct ethhdr), ifm->m_data, ifm->m_len);
753 slirp_output(slirp->opaque, buf, ifm->m_len + ETH_HLEN);
754 return 1;
bellardde806f02008-10-17 17:28:58 +0000755 }
bellardf0cbd3e2004-04-22 00:10:48 +0000756}
bellard9bf05442004-08-25 22:12:49 +0000757
Jan Kiszka9c12a6f2009-06-24 14:42:29 +0200758/* Drop host forwarding rule, return 0 if found. */
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200759int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
760 int host_port)
Alexander Grafc1261d82009-05-26 13:03:26 +0200761{
762 struct socket *so;
Jan Kiszka460fec62009-06-24 14:42:31 +0200763 struct socket *head = (is_udp ? &slirp->udb : &slirp->tcb);
Jan Kiszka2ad82cf2009-06-24 14:42:28 +0200764 struct sockaddr_in addr;
765 int port = htons(host_port);
766 socklen_t addr_len;
Alexander Grafc1261d82009-05-26 13:03:26 +0200767
Alexander Grafc1261d82009-05-26 13:03:26 +0200768 for (so = head->so_next; so != head; so = so->so_next) {
Jan Kiszka2ad82cf2009-06-24 14:42:28 +0200769 addr_len = sizeof(addr);
Jan Kiszka9c12a6f2009-06-24 14:42:29 +0200770 if ((so->so_state & SS_HOSTFWD) &&
771 getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 &&
Jan Kiszka3c6a0582009-06-24 14:42:28 +0200772 addr.sin_addr.s_addr == host_addr.s_addr &&
Jan Kiszka2ad82cf2009-06-24 14:42:28 +0200773 addr.sin_port == port) {
Alexander Grafc1261d82009-05-26 13:03:26 +0200774 close(so->s);
775 sofree(so);
Jan Kiszka9c12a6f2009-06-24 14:42:29 +0200776 return 0;
Alexander Grafc1261d82009-05-26 13:03:26 +0200777 }
778 }
779
Jan Kiszka9c12a6f2009-06-24 14:42:29 +0200780 return -1;
Alexander Grafc1261d82009-05-26 13:03:26 +0200781}
782
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200783int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr,
784 int host_port, struct in_addr guest_addr, int guest_port)
bellard9bf05442004-08-25 22:12:49 +0000785{
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200786 if (!guest_addr.s_addr) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200787 guest_addr = slirp->vdhcp_startaddr;
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200788 }
bellard9bf05442004-08-25 22:12:49 +0000789 if (is_udp) {
Jan Kiszka460fec62009-06-24 14:42:31 +0200790 if (!udp_listen(slirp, host_addr.s_addr, htons(host_port),
791 guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
bellard9bf05442004-08-25 22:12:49 +0000792 return -1;
793 } else {
Jan Kiszka460fec62009-06-24 14:42:31 +0200794 if (!tcp_listen(slirp, host_addr.s_addr, htons(host_port),
795 guest_addr.s_addr, htons(guest_port), SS_HOSTFWD))
bellard9bf05442004-08-25 22:12:49 +0000796 return -1;
797 }
798 return 0;
799}
bellarda3d4af02004-09-05 23:10:26 +0000800
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200801int slirp_add_exec(Slirp *slirp, int do_pty, const void *args,
Jan Kiszkabb53fc52009-07-22 17:03:52 +0200802 struct in_addr *guest_addr, int guest_port)
bellarda3d4af02004-09-05 23:10:26 +0000803{
Jan Kiszkabb53fc52009-07-22 17:03:52 +0200804 if (!guest_addr->s_addr) {
805 guest_addr->s_addr = slirp->vnetwork_addr.s_addr |
Jan Kiszka460fec62009-06-24 14:42:31 +0200806 (htonl(0x0204) & ~slirp->vnetwork_mask.s_addr);
Jan Kiszkac92ef6a2009-06-24 14:42:28 +0200807 }
Jan Kiszkabb53fc52009-07-22 17:03:52 +0200808 if ((guest_addr->s_addr & slirp->vnetwork_mask.s_addr) !=
Jan Kiszka460fec62009-06-24 14:42:31 +0200809 slirp->vnetwork_addr.s_addr ||
Jan Kiszkabb53fc52009-07-22 17:03:52 +0200810 guest_addr->s_addr == slirp->vhost_addr.s_addr ||
811 guest_addr->s_addr == slirp->vnameserver_addr.s_addr) {
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200812 return -1;
813 }
Jan Kiszkabb53fc52009-07-22 17:03:52 +0200814 return add_exec(&slirp->exec_list, do_pty, (char *)args, *guest_addr,
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200815 htons(guest_port));
bellarda3d4af02004-09-05 23:10:26 +0000816}
aliguorie1c5a2b2009-01-08 19:18:21 +0000817
818ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags)
819{
820 if (so->s == -1 && so->extra) {
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500821 qemu_chr_fe_write(so->extra, buf, len);
aliguorie1c5a2b2009-01-08 19:18:21 +0000822 return len;
823 }
824
825 return send(so->s, buf, len, flags);
826}
827
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200828static struct socket *
Jan Kiszka460fec62009-06-24 14:42:31 +0200829slirp_find_ctl_socket(Slirp *slirp, struct in_addr guest_addr, int guest_port)
aliguorie1c5a2b2009-01-08 19:18:21 +0000830{
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200831 struct socket *so;
aliguorie1c5a2b2009-01-08 19:18:21 +0000832
Jan Kiszka460fec62009-06-24 14:42:31 +0200833 for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) {
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200834 if (so->so_faddr.s_addr == guest_addr.s_addr &&
835 htons(so->so_fport) == guest_port) {
836 return so;
837 }
838 }
839 return NULL;
aliguorie1c5a2b2009-01-08 19:18:21 +0000840}
841
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200842size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr,
843 int guest_port)
aliguorie1c5a2b2009-01-08 19:18:21 +0000844{
845 struct iovec iov[2];
846 struct socket *so;
847
Jan Kiszka460fec62009-06-24 14:42:31 +0200848 so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
aliguorie1c5a2b2009-01-08 19:18:21 +0000849
850 if (!so || so->so_state & SS_NOFDREF)
851 return 0;
852
853 if (!CONN_CANFRCV(so) || so->so_snd.sb_cc >= (so->so_snd.sb_datalen/2))
854 return 0;
855
856 return sopreprbuf(so, iov, NULL);
857}
858
Jan Kiszka9f8bd042009-06-24 14:42:31 +0200859void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port,
Jan Kiszkac92ef6a2009-06-24 14:42:28 +0200860 const uint8_t *buf, int size)
aliguorie1c5a2b2009-01-08 19:18:21 +0000861{
862 int ret;
Jan Kiszka460fec62009-06-24 14:42:31 +0200863 struct socket *so = slirp_find_ctl_socket(slirp, guest_addr, guest_port);
Jan Kiszkaa13a4122009-06-24 14:42:28 +0200864
aliguorie1c5a2b2009-01-08 19:18:21 +0000865 if (!so)
866 return;
867
blueswir10580ac92009-01-12 17:51:06 +0000868 ret = soreadbuf(so, (const char *)buf, size);
aliguorie1c5a2b2009-01-08 19:18:21 +0000869
870 if (ret > 0)
871 tcp_output(sototcpcb(so));
872}
aliguori062e5522009-01-08 19:27:07 +0000873
874static void slirp_tcp_save(QEMUFile *f, struct tcpcb *tp)
875{
876 int i;
877
878 qemu_put_sbe16(f, tp->t_state);
879 for (i = 0; i < TCPT_NTIMERS; i++)
880 qemu_put_sbe16(f, tp->t_timer[i]);
881 qemu_put_sbe16(f, tp->t_rxtshift);
882 qemu_put_sbe16(f, tp->t_rxtcur);
883 qemu_put_sbe16(f, tp->t_dupacks);
884 qemu_put_be16(f, tp->t_maxseg);
885 qemu_put_sbyte(f, tp->t_force);
886 qemu_put_be16(f, tp->t_flags);
887 qemu_put_be32(f, tp->snd_una);
888 qemu_put_be32(f, tp->snd_nxt);
889 qemu_put_be32(f, tp->snd_up);
890 qemu_put_be32(f, tp->snd_wl1);
891 qemu_put_be32(f, tp->snd_wl2);
892 qemu_put_be32(f, tp->iss);
893 qemu_put_be32(f, tp->snd_wnd);
894 qemu_put_be32(f, tp->rcv_wnd);
895 qemu_put_be32(f, tp->rcv_nxt);
896 qemu_put_be32(f, tp->rcv_up);
897 qemu_put_be32(f, tp->irs);
898 qemu_put_be32(f, tp->rcv_adv);
899 qemu_put_be32(f, tp->snd_max);
900 qemu_put_be32(f, tp->snd_cwnd);
901 qemu_put_be32(f, tp->snd_ssthresh);
902 qemu_put_sbe16(f, tp->t_idle);
903 qemu_put_sbe16(f, tp->t_rtt);
904 qemu_put_be32(f, tp->t_rtseq);
905 qemu_put_sbe16(f, tp->t_srtt);
906 qemu_put_sbe16(f, tp->t_rttvar);
907 qemu_put_be16(f, tp->t_rttmin);
908 qemu_put_be32(f, tp->max_sndwnd);
909 qemu_put_byte(f, tp->t_oobflags);
910 qemu_put_byte(f, tp->t_iobc);
911 qemu_put_sbe16(f, tp->t_softerror);
912 qemu_put_byte(f, tp->snd_scale);
913 qemu_put_byte(f, tp->rcv_scale);
914 qemu_put_byte(f, tp->request_r_scale);
915 qemu_put_byte(f, tp->requested_s_scale);
916 qemu_put_be32(f, tp->ts_recent);
917 qemu_put_be32(f, tp->ts_recent_age);
918 qemu_put_be32(f, tp->last_ack_sent);
919}
920
921static void slirp_sbuf_save(QEMUFile *f, struct sbuf *sbuf)
922{
923 uint32_t off;
924
925 qemu_put_be32(f, sbuf->sb_cc);
926 qemu_put_be32(f, sbuf->sb_datalen);
927 off = (uint32_t)(sbuf->sb_wptr - sbuf->sb_data);
928 qemu_put_sbe32(f, off);
929 off = (uint32_t)(sbuf->sb_rptr - sbuf->sb_data);
930 qemu_put_sbe32(f, off);
931 qemu_put_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen);
932}
933
934static void slirp_socket_save(QEMUFile *f, struct socket *so)
935{
936 qemu_put_be32(f, so->so_urgc);
937 qemu_put_be32(f, so->so_faddr.s_addr);
938 qemu_put_be32(f, so->so_laddr.s_addr);
939 qemu_put_be16(f, so->so_fport);
940 qemu_put_be16(f, so->so_lport);
941 qemu_put_byte(f, so->so_iptos);
942 qemu_put_byte(f, so->so_emu);
943 qemu_put_byte(f, so->so_type);
944 qemu_put_be32(f, so->so_state);
945 slirp_sbuf_save(f, &so->so_rcv);
946 slirp_sbuf_save(f, &so->so_snd);
947 slirp_tcp_save(f, so->so_tcpcb);
948}
949
Jan Kiszka0a1f8512009-06-24 14:42:31 +0200950static void slirp_bootp_save(QEMUFile *f, Slirp *slirp)
951{
952 int i;
953
954 for (i = 0; i < NB_BOOTP_CLIENTS; i++) {
955 qemu_put_be16(f, slirp->bootp_clients[i].allocated);
956 qemu_put_buffer(f, slirp->bootp_clients[i].macaddr, 6);
957 }
958}
959
aliguori062e5522009-01-08 19:27:07 +0000960static void slirp_state_save(QEMUFile *f, void *opaque)
961{
Jan Kiszka460fec62009-06-24 14:42:31 +0200962 Slirp *slirp = opaque;
aliguori062e5522009-01-08 19:27:07 +0000963 struct ex_list *ex_ptr;
964
Jan Kiszka460fec62009-06-24 14:42:31 +0200965 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
aliguori062e5522009-01-08 19:27:07 +0000966 if (ex_ptr->ex_pty == 3) {
967 struct socket *so;
Jan Kiszka460fec62009-06-24 14:42:31 +0200968 so = slirp_find_ctl_socket(slirp, ex_ptr->ex_addr,
969 ntohs(ex_ptr->ex_fport));
aliguori062e5522009-01-08 19:27:07 +0000970 if (!so)
971 continue;
972
973 qemu_put_byte(f, 42);
974 slirp_socket_save(f, so);
975 }
976 qemu_put_byte(f, 0);
Jan Kiszka285f7a62009-06-24 14:42:30 +0200977
Jan Kiszka460fec62009-06-24 14:42:31 +0200978 qemu_put_be16(f, slirp->ip_id);
Jan Kiszka0a1f8512009-06-24 14:42:31 +0200979
980 slirp_bootp_save(f, slirp);
aliguori062e5522009-01-08 19:27:07 +0000981}
982
983static void slirp_tcp_load(QEMUFile *f, struct tcpcb *tp)
984{
985 int i;
986
987 tp->t_state = qemu_get_sbe16(f);
988 for (i = 0; i < TCPT_NTIMERS; i++)
989 tp->t_timer[i] = qemu_get_sbe16(f);
990 tp->t_rxtshift = qemu_get_sbe16(f);
991 tp->t_rxtcur = qemu_get_sbe16(f);
992 tp->t_dupacks = qemu_get_sbe16(f);
993 tp->t_maxseg = qemu_get_be16(f);
994 tp->t_force = qemu_get_sbyte(f);
995 tp->t_flags = qemu_get_be16(f);
996 tp->snd_una = qemu_get_be32(f);
997 tp->snd_nxt = qemu_get_be32(f);
998 tp->snd_up = qemu_get_be32(f);
999 tp->snd_wl1 = qemu_get_be32(f);
1000 tp->snd_wl2 = qemu_get_be32(f);
1001 tp->iss = qemu_get_be32(f);
1002 tp->snd_wnd = qemu_get_be32(f);
1003 tp->rcv_wnd = qemu_get_be32(f);
1004 tp->rcv_nxt = qemu_get_be32(f);
1005 tp->rcv_up = qemu_get_be32(f);
1006 tp->irs = qemu_get_be32(f);
1007 tp->rcv_adv = qemu_get_be32(f);
1008 tp->snd_max = qemu_get_be32(f);
1009 tp->snd_cwnd = qemu_get_be32(f);
1010 tp->snd_ssthresh = qemu_get_be32(f);
1011 tp->t_idle = qemu_get_sbe16(f);
1012 tp->t_rtt = qemu_get_sbe16(f);
1013 tp->t_rtseq = qemu_get_be32(f);
1014 tp->t_srtt = qemu_get_sbe16(f);
1015 tp->t_rttvar = qemu_get_sbe16(f);
1016 tp->t_rttmin = qemu_get_be16(f);
1017 tp->max_sndwnd = qemu_get_be32(f);
1018 tp->t_oobflags = qemu_get_byte(f);
1019 tp->t_iobc = qemu_get_byte(f);
1020 tp->t_softerror = qemu_get_sbe16(f);
1021 tp->snd_scale = qemu_get_byte(f);
1022 tp->rcv_scale = qemu_get_byte(f);
1023 tp->request_r_scale = qemu_get_byte(f);
1024 tp->requested_s_scale = qemu_get_byte(f);
1025 tp->ts_recent = qemu_get_be32(f);
1026 tp->ts_recent_age = qemu_get_be32(f);
1027 tp->last_ack_sent = qemu_get_be32(f);
1028 tcp_template(tp);
1029}
1030
1031static int slirp_sbuf_load(QEMUFile *f, struct sbuf *sbuf)
1032{
1033 uint32_t off, sb_cc, sb_datalen;
1034
1035 sb_cc = qemu_get_be32(f);
1036 sb_datalen = qemu_get_be32(f);
1037
1038 sbreserve(sbuf, sb_datalen);
1039
1040 if (sbuf->sb_datalen != sb_datalen)
1041 return -ENOMEM;
1042
1043 sbuf->sb_cc = sb_cc;
1044
1045 off = qemu_get_sbe32(f);
1046 sbuf->sb_wptr = sbuf->sb_data + off;
1047 off = qemu_get_sbe32(f);
1048 sbuf->sb_rptr = sbuf->sb_data + off;
1049 qemu_get_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen);
1050
1051 return 0;
1052}
1053
1054static int slirp_socket_load(QEMUFile *f, struct socket *so)
1055{
1056 if (tcp_attach(so) < 0)
1057 return -ENOMEM;
1058
1059 so->so_urgc = qemu_get_be32(f);
1060 so->so_faddr.s_addr = qemu_get_be32(f);
1061 so->so_laddr.s_addr = qemu_get_be32(f);
1062 so->so_fport = qemu_get_be16(f);
1063 so->so_lport = qemu_get_be16(f);
1064 so->so_iptos = qemu_get_byte(f);
1065 so->so_emu = qemu_get_byte(f);
1066 so->so_type = qemu_get_byte(f);
1067 so->so_state = qemu_get_be32(f);
1068 if (slirp_sbuf_load(f, &so->so_rcv) < 0)
1069 return -ENOMEM;
1070 if (slirp_sbuf_load(f, &so->so_snd) < 0)
1071 return -ENOMEM;
1072 slirp_tcp_load(f, so->so_tcpcb);
1073
1074 return 0;
1075}
1076
Jan Kiszka0a1f8512009-06-24 14:42:31 +02001077static void slirp_bootp_load(QEMUFile *f, Slirp *slirp)
1078{
1079 int i;
1080
1081 for (i = 0; i < NB_BOOTP_CLIENTS; i++) {
1082 slirp->bootp_clients[i].allocated = qemu_get_be16(f);
1083 qemu_get_buffer(f, slirp->bootp_clients[i].macaddr, 6);
1084 }
1085}
1086
aliguori062e5522009-01-08 19:27:07 +00001087static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
1088{
Jan Kiszka460fec62009-06-24 14:42:31 +02001089 Slirp *slirp = opaque;
aliguori062e5522009-01-08 19:27:07 +00001090 struct ex_list *ex_ptr;
aliguori062e5522009-01-08 19:27:07 +00001091
Blue Swirlb0e04862010-03-07 13:45:38 +00001092 while (qemu_get_byte(f)) {
aliguori062e5522009-01-08 19:27:07 +00001093 int ret;
Jan Kiszka460fec62009-06-24 14:42:31 +02001094 struct socket *so = socreate(slirp);
aliguori062e5522009-01-08 19:27:07 +00001095
1096 if (!so)
1097 return -ENOMEM;
1098
1099 ret = slirp_socket_load(f, so);
1100
1101 if (ret < 0)
1102 return ret;
1103
Jan Kiszka460fec62009-06-24 14:42:31 +02001104 if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) !=
1105 slirp->vnetwork_addr.s_addr) {
aliguori062e5522009-01-08 19:27:07 +00001106 return -EINVAL;
Jan Kiszkaa13a4122009-06-24 14:42:28 +02001107 }
Jan Kiszka460fec62009-06-24 14:42:31 +02001108 for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
aliguori062e5522009-01-08 19:27:07 +00001109 if (ex_ptr->ex_pty == 3 &&
Jan Kiszkaa13a4122009-06-24 14:42:28 +02001110 so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr &&
1111 so->so_fport == ex_ptr->ex_fport) {
aliguori062e5522009-01-08 19:27:07 +00001112 break;
Jan Kiszkaa13a4122009-06-24 14:42:28 +02001113 }
1114 }
aliguori062e5522009-01-08 19:27:07 +00001115 if (!ex_ptr)
1116 return -EINVAL;
1117
blueswir10580ac92009-01-12 17:51:06 +00001118 so->extra = (void *)ex_ptr->ex_exec;
aliguori062e5522009-01-08 19:27:07 +00001119 }
1120
Jan Kiszka285f7a62009-06-24 14:42:30 +02001121 if (version_id >= 2) {
Jan Kiszka460fec62009-06-24 14:42:31 +02001122 slirp->ip_id = qemu_get_be16(f);
Jan Kiszka285f7a62009-06-24 14:42:30 +02001123 }
1124
Jan Kiszka0a1f8512009-06-24 14:42:31 +02001125 if (version_id >= 3) {
1126 slirp_bootp_load(f, slirp);
1127 }
1128
aliguori062e5522009-01-08 19:27:07 +00001129 return 0;
1130}