]>
Commit | Line | Data |
---|---|---|
d75a0b97 FB |
1 | /* |
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 | */ | |
e1c5a2b3 | 24 | #include "qemu-common.h" |
0580ac91 | 25 | #include "qemu-char.h" |
f0cbd3ec | 26 | #include "slirp.h" |
062e5527 | 27 | #include "hw/hw.h" |
f0cbd3ec FB |
28 | |
29 | /* host address */ | |
30 | struct in_addr our_addr; | |
31 | /* host dns address */ | |
32 | struct in_addr dns_addr; | |
33 | /* host loopback address */ | |
34 | struct in_addr loopback_addr; | |
35 | ||
a13a4126 JK |
36 | /* virtual network configuration */ |
37 | struct in_addr vnetwork_addr; | |
38 | struct in_addr vnetwork_mask; | |
39 | struct in_addr vhost_addr; | |
40 | struct in_addr vdhcp_startaddr; | |
41 | struct in_addr vnameserver_addr; | |
42 | ||
43 | /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */ | |
9634d903 | 44 | static const uint8_t special_ethaddr[6] = { |
a13a4126 | 45 | 0x52, 0x55, 0x00, 0x00, 0x00, 0x00 |
f0cbd3ec FB |
46 | }; |
47 | ||
de806f07 | 48 | /* ARP cache for the guest IP addresses (XXX: allow many entries) */ |
f0cbd3ec | 49 | uint8_t client_ethaddr[6]; |
de806f07 FB |
50 | static struct in_addr client_ipaddr; |
51 | ||
52 | static const uint8_t zero_ethaddr[6] = { 0, 0, 0, 0, 0, 0 }; | |
f0cbd3ec | 53 | |
a9ba3a85 | 54 | int slirp_restrict; |
511d2b14 | 55 | static int do_slowtimo; |
f0cbd3ec FB |
56 | int link_up; |
57 | struct timeval tt; | |
58 | FILE *lfd; | |
a3d4af03 | 59 | struct ex_list *exec_list; |
f0cbd3ec FB |
60 | |
61 | /* XXX: suppress those select globals */ | |
62 | fd_set *global_readfds, *global_writefds, *global_xfds; | |
63 | ||
3f423c9c | 64 | char slirp_hostname[33]; |
115defd1 | 65 | |
f0cbd3ec FB |
66 | #ifdef _WIN32 |
67 | ||
68 | static int get_dns_addr(struct in_addr *pdns_addr) | |
69 | { | |
379ff53d FB |
70 | FIXED_INFO *FixedInfo=NULL; |
71 | ULONG BufLen; | |
72 | DWORD ret; | |
73 | IP_ADDR_STRING *pIPAddr; | |
74 | struct in_addr tmp_addr; | |
3b46e624 | 75 | |
379ff53d FB |
76 | FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO)); |
77 | BufLen = sizeof(FIXED_INFO); | |
3b46e624 | 78 | |
379ff53d FB |
79 | if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen)) { |
80 | if (FixedInfo) { | |
81 | GlobalFree(FixedInfo); | |
82 | FixedInfo = NULL; | |
83 | } | |
84 | FixedInfo = GlobalAlloc(GPTR, BufLen); | |
85 | } | |
5fafdf24 | 86 | |
379ff53d FB |
87 | if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS) { |
88 | printf("GetNetworkParams failed. ret = %08x\n", (u_int)ret ); | |
89 | if (FixedInfo) { | |
90 | GlobalFree(FixedInfo); | |
91 | FixedInfo = NULL; | |
92 | } | |
93 | return -1; | |
94 | } | |
3b46e624 | 95 | |
379ff53d FB |
96 | pIPAddr = &(FixedInfo->DnsServerList); |
97 | inet_aton(pIPAddr->IpAddress.String, &tmp_addr); | |
98 | *pdns_addr = tmp_addr; | |
99 | #if 0 | |
100 | printf( "DNS Servers:\n" ); | |
101 | printf( "DNS Addr:%s\n", pIPAddr->IpAddress.String ); | |
3b46e624 | 102 | |
379ff53d FB |
103 | pIPAddr = FixedInfo -> DnsServerList.Next; |
104 | while ( pIPAddr ) { | |
105 | printf( "DNS Addr:%s\n", pIPAddr ->IpAddress.String ); | |
106 | pIPAddr = pIPAddr ->Next; | |
107 | } | |
108 | #endif | |
109 | if (FixedInfo) { | |
110 | GlobalFree(FixedInfo); | |
111 | FixedInfo = NULL; | |
112 | } | |
113 | return 0; | |
f0cbd3ec FB |
114 | } |
115 | ||
116 | #else | |
117 | ||
118 | static int get_dns_addr(struct in_addr *pdns_addr) | |
119 | { | |
120 | char buff[512]; | |
363a37d5 | 121 | char buff2[257]; |
f0cbd3ec FB |
122 | FILE *f; |
123 | int found = 0; | |
124 | struct in_addr tmp_addr; | |
3b46e624 | 125 | |
f0cbd3ec FB |
126 | f = fopen("/etc/resolv.conf", "r"); |
127 | if (!f) | |
128 | return -1; | |
129 | ||
31a60e22 | 130 | #ifdef DEBUG |
f0cbd3ec | 131 | lprint("IP address of your DNS(s): "); |
31a60e22 | 132 | #endif |
f0cbd3ec FB |
133 | while (fgets(buff, 512, f) != NULL) { |
134 | if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) { | |
135 | if (!inet_aton(buff2, &tmp_addr)) | |
136 | continue; | |
137 | if (tmp_addr.s_addr == loopback_addr.s_addr) | |
138 | tmp_addr = our_addr; | |
139 | /* If it's the first one, set it to dns_addr */ | |
140 | if (!found) | |
141 | *pdns_addr = tmp_addr; | |
31a60e22 | 142 | #ifdef DEBUG |
f0cbd3ec FB |
143 | else |
144 | lprint(", "); | |
31a60e22 | 145 | #endif |
f0cbd3ec | 146 | if (++found > 3) { |
31a60e22 | 147 | #ifdef DEBUG |
f0cbd3ec | 148 | lprint("(more)"); |
31a60e22 | 149 | #endif |
f0cbd3ec | 150 | break; |
31a60e22 BS |
151 | } |
152 | #ifdef DEBUG | |
153 | else | |
f0cbd3ec | 154 | lprint("%s", inet_ntoa(tmp_addr)); |
31a60e22 | 155 | #endif |
f0cbd3ec FB |
156 | } |
157 | } | |
1d43a717 | 158 | fclose(f); |
f0cbd3ec FB |
159 | if (!found) |
160 | return -1; | |
161 | return 0; | |
162 | } | |
163 | ||
164 | #endif | |
165 | ||
379ff53d | 166 | #ifdef _WIN32 |
9634d903 | 167 | static void slirp_cleanup(void) |
379ff53d FB |
168 | { |
169 | WSACleanup(); | |
170 | } | |
171 | #endif | |
172 | ||
062e5527 AL |
173 | static void slirp_state_save(QEMUFile *f, void *opaque); |
174 | static int slirp_state_load(QEMUFile *f, void *opaque, int version_id); | |
175 | ||
ad196a9d JK |
176 | void slirp_init(int restricted, const char *special_ip, const char *tftp_path, |
177 | const char *bootfile) | |
f0cbd3ec | 178 | { |
512176db | 179 | // debug_init("/tmp/slirp.log", DEBUG_DEFAULT); |
3b46e624 | 180 | |
a13a4126 | 181 | struct in_addr special_addr = { .s_addr = htonl(0x0a000200) }; |
379ff53d | 182 | #ifdef _WIN32 |
a13a4126 JK |
183 | WSADATA Data; |
184 | ||
185 | WSAStartup(MAKEWORD(2,0), &Data); | |
186 | atexit(slirp_cleanup); | |
379ff53d FB |
187 | #endif |
188 | ||
f0cbd3ec | 189 | link_up = 1; |
511d2b14 | 190 | slirp_restrict = restricted; |
f0cbd3ec FB |
191 | |
192 | if_init(); | |
193 | ip_init(); | |
194 | ||
195 | /* Initialise mbufs *after* setting the MTU */ | |
196 | m_init(); | |
197 | ||
198 | /* set default addresses */ | |
f0cbd3ec FB |
199 | inet_aton("127.0.0.1", &loopback_addr); |
200 | ||
201 | if (get_dns_addr(&dns_addr) < 0) { | |
0f8134bf PB |
202 | dns_addr = loopback_addr; |
203 | fprintf (stderr, "Warning: No DNS servers found\n"); | |
f0cbd3ec FB |
204 | } |
205 | ||
a13a4126 JK |
206 | if (special_ip) { |
207 | inet_aton(special_ip, &special_addr); | |
208 | } | |
ad196a9d JK |
209 | qemu_free(tftp_prefix); |
210 | tftp_prefix = NULL; | |
211 | if (tftp_path) { | |
212 | tftp_prefix = qemu_strdup(tftp_path); | |
213 | } | |
214 | qemu_free(bootp_filename); | |
215 | bootp_filename = NULL; | |
216 | if (bootfile) { | |
217 | bootp_filename = qemu_strdup(bootfile); | |
218 | } | |
219 | ||
a13a4126 JK |
220 | vnetwork_addr = special_addr; |
221 | vnetwork_mask.s_addr = htonl(0xffffff00); | |
222 | vhost_addr.s_addr = special_addr.s_addr | htonl(2); | |
223 | vdhcp_startaddr.s_addr = special_addr.s_addr | htonl(15); | |
224 | vnameserver_addr.s_addr = special_addr.s_addr | htonl(3); | |
f4e15b4b | 225 | getouraddr(); |
062e5527 | 226 | register_savevm("slirp", 0, 1, slirp_state_save, slirp_state_load, NULL); |
f0cbd3ec FB |
227 | } |
228 | ||
229 | #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED) | |
230 | #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED) | |
231 | #define UPD_NFDS(x) if (nfds < (x)) nfds = (x) | |
232 | ||
233 | /* | |
234 | * curtime kept to an accuracy of 1ms | |
235 | */ | |
379ff53d FB |
236 | #ifdef _WIN32 |
237 | static void updtime(void) | |
238 | { | |
239 | struct _timeb tb; | |
240 | ||
241 | _ftime(&tb); | |
242 | curtime = (u_int)tb.time * (u_int)1000; | |
243 | curtime += (u_int)tb.millitm; | |
244 | } | |
245 | #else | |
f0cbd3ec FB |
246 | static void updtime(void) |
247 | { | |
511d2b14 | 248 | gettimeofday(&tt, NULL); |
5fafdf24 | 249 | |
f0cbd3ec FB |
250 | curtime = (u_int)tt.tv_sec * (u_int)1000; |
251 | curtime += (u_int)tt.tv_usec / (u_int)1000; | |
5fafdf24 | 252 | |
f0cbd3ec FB |
253 | if ((tt.tv_usec % 1000) >= 500) |
254 | curtime++; | |
255 | } | |
379ff53d | 256 | #endif |
f0cbd3ec | 257 | |
5fafdf24 | 258 | void slirp_select_fill(int *pnfds, |
f0cbd3ec FB |
259 | fd_set *readfds, fd_set *writefds, fd_set *xfds) |
260 | { | |
261 | struct socket *so, *so_next; | |
262 | struct timeval timeout; | |
263 | int nfds; | |
264 | int tmp_time; | |
265 | ||
266 | /* fail safe */ | |
267 | global_readfds = NULL; | |
268 | global_writefds = NULL; | |
269 | global_xfds = NULL; | |
3b46e624 | 270 | |
f0cbd3ec FB |
271 | nfds = *pnfds; |
272 | /* | |
273 | * First, TCP sockets | |
274 | */ | |
275 | do_slowtimo = 0; | |
276 | if (link_up) { | |
5fafdf24 | 277 | /* |
f0cbd3ec FB |
278 | * *_slowtimo needs calling if there are IP fragments |
279 | * in the fragment queue, or there are TCP connections active | |
280 | */ | |
281 | do_slowtimo = ((tcb.so_next != &tcb) || | |
429d0a3d | 282 | (&ipq.ip_link != ipq.ip_link.next)); |
3b46e624 | 283 | |
f0cbd3ec FB |
284 | for (so = tcb.so_next; so != &tcb; so = so_next) { |
285 | so_next = so->so_next; | |
3b46e624 | 286 | |
f0cbd3ec FB |
287 | /* |
288 | * See if we need a tcp_fasttimo | |
289 | */ | |
290 | if (time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK) | |
291 | time_fasttimo = curtime; /* Flag when we want a fasttimo */ | |
3b46e624 | 292 | |
f0cbd3ec FB |
293 | /* |
294 | * NOFDREF can include still connecting to local-host, | |
295 | * newly socreated() sockets etc. Don't want to select these. | |
296 | */ | |
297 | if (so->so_state & SS_NOFDREF || so->s == -1) | |
298 | continue; | |
3b46e624 | 299 | |
f0cbd3ec FB |
300 | /* |
301 | * Set for reading sockets which are accepting | |
302 | */ | |
303 | if (so->so_state & SS_FACCEPTCONN) { | |
304 | FD_SET(so->s, readfds); | |
305 | UPD_NFDS(so->s); | |
306 | continue; | |
307 | } | |
3b46e624 | 308 | |
f0cbd3ec FB |
309 | /* |
310 | * Set for writing sockets which are connecting | |
311 | */ | |
312 | if (so->so_state & SS_ISFCONNECTING) { | |
313 | FD_SET(so->s, writefds); | |
314 | UPD_NFDS(so->s); | |
315 | continue; | |
316 | } | |
3b46e624 | 317 | |
f0cbd3ec FB |
318 | /* |
319 | * Set for writing if we are connected, can send more, and | |
320 | * we have something to send | |
321 | */ | |
322 | if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) { | |
323 | FD_SET(so->s, writefds); | |
324 | UPD_NFDS(so->s); | |
325 | } | |
3b46e624 | 326 | |
f0cbd3ec FB |
327 | /* |
328 | * Set for reading (and urgent data) if we are connected, can | |
329 | * receive more, and we have room for it XXX /2 ? | |
330 | */ | |
331 | if (CONN_CANFRCV(so) && (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) { | |
332 | FD_SET(so->s, readfds); | |
333 | FD_SET(so->s, xfds); | |
334 | UPD_NFDS(so->s); | |
335 | } | |
336 | } | |
3b46e624 | 337 | |
f0cbd3ec FB |
338 | /* |
339 | * UDP sockets | |
340 | */ | |
341 | for (so = udb.so_next; so != &udb; so = so_next) { | |
342 | so_next = so->so_next; | |
3b46e624 | 343 | |
f0cbd3ec FB |
344 | /* |
345 | * See if it's timed out | |
346 | */ | |
347 | if (so->so_expire) { | |
348 | if (so->so_expire <= curtime) { | |
349 | udp_detach(so); | |
350 | continue; | |
351 | } else | |
352 | do_slowtimo = 1; /* Let socket expire */ | |
353 | } | |
3b46e624 | 354 | |
f0cbd3ec FB |
355 | /* |
356 | * When UDP packets are received from over the | |
357 | * link, they're sendto()'d straight away, so | |
358 | * no need for setting for writing | |
359 | * Limit the number of packets queued by this session | |
360 | * to 4. Note that even though we try and limit this | |
361 | * to 4 packets, the session could have more queued | |
362 | * if the packets needed to be fragmented | |
363 | * (XXX <= 4 ?) | |
364 | */ | |
365 | if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) { | |
366 | FD_SET(so->s, readfds); | |
367 | UPD_NFDS(so->s); | |
368 | } | |
369 | } | |
370 | } | |
5fafdf24 | 371 | |
f0cbd3ec FB |
372 | /* |
373 | * Setup timeout to use minimum CPU usage, especially when idle | |
374 | */ | |
5fafdf24 TS |
375 | |
376 | /* | |
f0cbd3ec FB |
377 | * First, see the timeout needed by *timo |
378 | */ | |
379 | timeout.tv_sec = 0; | |
380 | timeout.tv_usec = -1; | |
381 | /* | |
382 | * If a slowtimo is needed, set timeout to 500ms from the last | |
383 | * slow timeout. If a fast timeout is needed, set timeout within | |
384 | * 200ms of when it was requested. | |
385 | */ | |
386 | if (do_slowtimo) { | |
387 | /* XXX + 10000 because some select()'s aren't that accurate */ | |
388 | timeout.tv_usec = ((500 - (curtime - last_slowtimo)) * 1000) + 10000; | |
389 | if (timeout.tv_usec < 0) | |
390 | timeout.tv_usec = 0; | |
391 | else if (timeout.tv_usec > 510000) | |
392 | timeout.tv_usec = 510000; | |
3b46e624 | 393 | |
f0cbd3ec FB |
394 | /* Can only fasttimo if we also slowtimo */ |
395 | if (time_fasttimo) { | |
396 | tmp_time = (200 - (curtime - time_fasttimo)) * 1000; | |
397 | if (tmp_time < 0) | |
398 | tmp_time = 0; | |
3b46e624 | 399 | |
f0cbd3ec FB |
400 | /* Choose the smallest of the 2 */ |
401 | if (tmp_time < timeout.tv_usec) | |
402 | timeout.tv_usec = (u_int)tmp_time; | |
403 | } | |
404 | } | |
405 | *pnfds = nfds; | |
5fafdf24 | 406 | } |
f0cbd3ec FB |
407 | |
408 | void slirp_select_poll(fd_set *readfds, fd_set *writefds, fd_set *xfds) | |
409 | { | |
410 | struct socket *so, *so_next; | |
411 | int ret; | |
412 | ||
413 | global_readfds = readfds; | |
414 | global_writefds = writefds; | |
415 | global_xfds = xfds; | |
416 | ||
417 | /* Update time */ | |
418 | updtime(); | |
5fafdf24 | 419 | |
f0cbd3ec | 420 | /* |
5fafdf24 | 421 | * See if anything has timed out |
f0cbd3ec FB |
422 | */ |
423 | if (link_up) { | |
df5f8956 | 424 | if (time_fasttimo && ((curtime - time_fasttimo) >= 2)) { |
f0cbd3ec FB |
425 | tcp_fasttimo(); |
426 | time_fasttimo = 0; | |
427 | } | |
428 | if (do_slowtimo && ((curtime - last_slowtimo) >= 499)) { | |
429 | ip_slowtimo(); | |
430 | tcp_slowtimo(); | |
431 | last_slowtimo = curtime; | |
432 | } | |
433 | } | |
5fafdf24 | 434 | |
f0cbd3ec FB |
435 | /* |
436 | * Check sockets | |
437 | */ | |
438 | if (link_up) { | |
439 | /* | |
440 | * Check TCP sockets | |
441 | */ | |
442 | for (so = tcb.so_next; so != &tcb; so = so_next) { | |
443 | so_next = so->so_next; | |
3b46e624 | 444 | |
f0cbd3ec FB |
445 | /* |
446 | * FD_ISSET is meaningless on these sockets | |
447 | * (and they can crash the program) | |
448 | */ | |
449 | if (so->so_state & SS_NOFDREF || so->s == -1) | |
450 | continue; | |
3b46e624 | 451 | |
f0cbd3ec FB |
452 | /* |
453 | * Check for URG data | |
454 | * This will soread as well, so no need to | |
455 | * test for readfds below if this succeeds | |
456 | */ | |
457 | if (FD_ISSET(so->s, xfds)) | |
458 | sorecvoob(so); | |
459 | /* | |
460 | * Check sockets for reading | |
461 | */ | |
462 | else if (FD_ISSET(so->s, readfds)) { | |
463 | /* | |
464 | * Check for incoming connections | |
465 | */ | |
466 | if (so->so_state & SS_FACCEPTCONN) { | |
467 | tcp_connect(so); | |
468 | continue; | |
469 | } /* else */ | |
470 | ret = soread(so); | |
3b46e624 | 471 | |
f0cbd3ec FB |
472 | /* Output it if we read something */ |
473 | if (ret > 0) | |
474 | tcp_output(sototcpcb(so)); | |
475 | } | |
3b46e624 | 476 | |
f0cbd3ec FB |
477 | /* |
478 | * Check sockets for writing | |
479 | */ | |
480 | if (FD_ISSET(so->s, writefds)) { | |
481 | /* | |
482 | * Check for non-blocking, still-connecting sockets | |
483 | */ | |
484 | if (so->so_state & SS_ISFCONNECTING) { | |
485 | /* Connected */ | |
486 | so->so_state &= ~SS_ISFCONNECTING; | |
3b46e624 | 487 | |
0a656f5f | 488 | ret = send(so->s, (const void *) &ret, 0, 0); |
f0cbd3ec FB |
489 | if (ret < 0) { |
490 | /* XXXXX Must fix, zero bytes is a NOP */ | |
491 | if (errno == EAGAIN || errno == EWOULDBLOCK || | |
492 | errno == EINPROGRESS || errno == ENOTCONN) | |
493 | continue; | |
3b46e624 | 494 | |
f0cbd3ec FB |
495 | /* else failed */ |
496 | so->so_state = SS_NOFDREF; | |
497 | } | |
498 | /* else so->so_state &= ~SS_ISFCONNECTING; */ | |
3b46e624 | 499 | |
f0cbd3ec FB |
500 | /* |
501 | * Continue tcp_input | |
502 | */ | |
503 | tcp_input((struct mbuf *)NULL, sizeof(struct ip), so); | |
504 | /* continue; */ | |
505 | } else | |
506 | ret = sowrite(so); | |
507 | /* | |
5fafdf24 | 508 | * XXXXX If we wrote something (a lot), there |
f0cbd3ec FB |
509 | * could be a need for a window update. |
510 | * In the worst case, the remote will send | |
511 | * a window probe to get things going again | |
512 | */ | |
513 | } | |
3b46e624 | 514 | |
f0cbd3ec FB |
515 | /* |
516 | * Probe a still-connecting, non-blocking socket | |
517 | * to check if it's still alive | |
518 | */ | |
519 | #ifdef PROBE_CONN | |
520 | if (so->so_state & SS_ISFCONNECTING) { | |
02d2c54c | 521 | ret = recv(so->s, (char *)&ret, 0,0); |
3b46e624 | 522 | |
f0cbd3ec FB |
523 | if (ret < 0) { |
524 | /* XXX */ | |
525 | if (errno == EAGAIN || errno == EWOULDBLOCK || | |
526 | errno == EINPROGRESS || errno == ENOTCONN) | |
527 | continue; /* Still connecting, continue */ | |
3b46e624 | 528 | |
f0cbd3ec FB |
529 | /* else failed */ |
530 | so->so_state = SS_NOFDREF; | |
3b46e624 | 531 | |
f0cbd3ec FB |
532 | /* tcp_input will take care of it */ |
533 | } else { | |
02d2c54c | 534 | ret = send(so->s, &ret, 0,0); |
f0cbd3ec FB |
535 | if (ret < 0) { |
536 | /* XXX */ | |
537 | if (errno == EAGAIN || errno == EWOULDBLOCK || | |
538 | errno == EINPROGRESS || errno == ENOTCONN) | |
539 | continue; | |
540 | /* else failed */ | |
541 | so->so_state = SS_NOFDREF; | |
542 | } else | |
543 | so->so_state &= ~SS_ISFCONNECTING; | |
3b46e624 | 544 | |
f0cbd3ec FB |
545 | } |
546 | tcp_input((struct mbuf *)NULL, sizeof(struct ip),so); | |
547 | } /* SS_ISFCONNECTING */ | |
548 | #endif | |
549 | } | |
3b46e624 | 550 | |
f0cbd3ec FB |
551 | /* |
552 | * Now UDP sockets. | |
553 | * Incoming packets are sent straight away, they're not buffered. | |
554 | * Incoming UDP data isn't buffered either. | |
555 | */ | |
556 | for (so = udb.so_next; so != &udb; so = so_next) { | |
557 | so_next = so->so_next; | |
3b46e624 | 558 | |
f0cbd3ec FB |
559 | if (so->s != -1 && FD_ISSET(so->s, readfds)) { |
560 | sorecvfrom(so); | |
561 | } | |
562 | } | |
563 | } | |
5fafdf24 | 564 | |
f0cbd3ec FB |
565 | /* |
566 | * See if we can start outputting | |
567 | */ | |
568 | if (if_queued && link_up) | |
569 | if_start(); | |
02d2c54c FB |
570 | |
571 | /* clear global file descriptor sets. | |
572 | * these reside on the stack in vl.c | |
573 | * so they're unusable if we're not in | |
574 | * slirp_select_fill or slirp_select_poll. | |
575 | */ | |
576 | global_readfds = NULL; | |
577 | global_writefds = NULL; | |
578 | global_xfds = NULL; | |
f0cbd3ec FB |
579 | } |
580 | ||
581 | #define ETH_ALEN 6 | |
582 | #define ETH_HLEN 14 | |
583 | ||
584 | #define ETH_P_IP 0x0800 /* Internet Protocol packet */ | |
585 | #define ETH_P_ARP 0x0806 /* Address Resolution packet */ | |
586 | ||
587 | #define ARPOP_REQUEST 1 /* ARP request */ | |
588 | #define ARPOP_REPLY 2 /* ARP reply */ | |
589 | ||
5fafdf24 | 590 | struct ethhdr |
f0cbd3ec FB |
591 | { |
592 | unsigned char h_dest[ETH_ALEN]; /* destination eth addr */ | |
593 | unsigned char h_source[ETH_ALEN]; /* source ether addr */ | |
594 | unsigned short h_proto; /* packet type ID field */ | |
595 | }; | |
596 | ||
597 | struct arphdr | |
598 | { | |
599 | unsigned short ar_hrd; /* format of hardware address */ | |
600 | unsigned short ar_pro; /* format of protocol address */ | |
601 | unsigned char ar_hln; /* length of hardware address */ | |
602 | unsigned char ar_pln; /* length of protocol address */ | |
603 | unsigned short ar_op; /* ARP opcode (command) */ | |
604 | ||
605 | /* | |
606 | * Ethernet looks like this : This bit is variable sized however... | |
607 | */ | |
608 | unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */ | |
a13a4126 | 609 | uint32_t ar_sip; /* sender IP address */ |
f0cbd3ec | 610 | unsigned char ar_tha[ETH_ALEN]; /* target hardware address */ |
a13a4126 JK |
611 | uint32_t ar_tip ; /* target IP address */ |
612 | } __attribute__((packed)); | |
f0cbd3ec | 613 | |
8fcd3692 | 614 | static void arp_input(const uint8_t *pkt, int pkt_len) |
f0cbd3ec FB |
615 | { |
616 | struct ethhdr *eh = (struct ethhdr *)pkt; | |
617 | struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN); | |
618 | uint8_t arp_reply[ETH_HLEN + sizeof(struct arphdr)]; | |
619 | struct ethhdr *reh = (struct ethhdr *)arp_reply; | |
620 | struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN); | |
621 | int ar_op; | |
a3d4af03 | 622 | struct ex_list *ex_ptr; |
f0cbd3ec FB |
623 | |
624 | ar_op = ntohs(ah->ar_op); | |
625 | switch(ar_op) { | |
626 | case ARPOP_REQUEST: | |
a13a4126 JK |
627 | if ((ah->ar_tip & vnetwork_mask.s_addr) == vnetwork_addr.s_addr) { |
628 | if (ah->ar_tip == vnameserver_addr.s_addr || | |
629 | ah->ar_tip == vhost_addr.s_addr) | |
a3d4af03 FB |
630 | goto arp_ok; |
631 | for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) { | |
a13a4126 | 632 | if (ex_ptr->ex_addr.s_addr == ah->ar_tip) |
a3d4af03 FB |
633 | goto arp_ok; |
634 | } | |
635 | return; | |
636 | arp_ok: | |
f0cbd3ec FB |
637 | /* XXX: make an ARP request to have the client address */ |
638 | memcpy(client_ethaddr, eh->h_source, ETH_ALEN); | |
639 | ||
640 | /* ARP request for alias/dns mac address */ | |
641 | memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN); | |
a13a4126 JK |
642 | memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4); |
643 | memcpy(&reh->h_source[2], &ah->ar_tip, 4); | |
f0cbd3ec FB |
644 | reh->h_proto = htons(ETH_P_ARP); |
645 | ||
646 | rah->ar_hrd = htons(1); | |
647 | rah->ar_pro = htons(ETH_P_IP); | |
648 | rah->ar_hln = ETH_ALEN; | |
649 | rah->ar_pln = 4; | |
650 | rah->ar_op = htons(ARPOP_REPLY); | |
651 | memcpy(rah->ar_sha, reh->h_source, ETH_ALEN); | |
a13a4126 | 652 | rah->ar_sip = ah->ar_tip; |
f0cbd3ec | 653 | memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN); |
a13a4126 | 654 | rah->ar_tip = ah->ar_sip; |
f0cbd3ec FB |
655 | slirp_output(arp_reply, sizeof(arp_reply)); |
656 | } | |
657 | break; | |
de806f07 FB |
658 | case ARPOP_REPLY: |
659 | /* reply to request of client mac address ? */ | |
660 | if (!memcmp(client_ethaddr, zero_ethaddr, ETH_ALEN) && | |
a13a4126 | 661 | ah->ar_sip == client_ipaddr.s_addr) { |
de806f07 FB |
662 | memcpy(client_ethaddr, ah->ar_sha, ETH_ALEN); |
663 | } | |
664 | break; | |
f0cbd3ec FB |
665 | default: |
666 | break; | |
667 | } | |
668 | } | |
669 | ||
670 | void slirp_input(const uint8_t *pkt, int pkt_len) | |
671 | { | |
672 | struct mbuf *m; | |
673 | int proto; | |
674 | ||
675 | if (pkt_len < ETH_HLEN) | |
676 | return; | |
3b46e624 | 677 | |
f0cbd3ec FB |
678 | proto = ntohs(*(uint16_t *)(pkt + 12)); |
679 | switch(proto) { | |
680 | case ETH_P_ARP: | |
681 | arp_input(pkt, pkt_len); | |
682 | break; | |
683 | case ETH_P_IP: | |
684 | m = m_get(); | |
685 | if (!m) | |
686 | return; | |
38f3e7c2 | 687 | /* Note: we add to align the IP header */ |
e8e880a7 AJ |
688 | if (M_FREEROOM(m) < pkt_len + 2) { |
689 | m_inc(m, pkt_len + 2); | |
690 | } | |
38f3e7c2 FB |
691 | m->m_len = pkt_len + 2; |
692 | memcpy(m->m_data + 2, pkt, pkt_len); | |
f0cbd3ec | 693 | |
38f3e7c2 FB |
694 | m->m_data += 2 + ETH_HLEN; |
695 | m->m_len -= 2 + ETH_HLEN; | |
f0cbd3ec FB |
696 | |
697 | ip_input(m); | |
698 | break; | |
699 | default: | |
700 | break; | |
701 | } | |
702 | } | |
703 | ||
704 | /* output the IP packet to the ethernet device */ | |
705 | void if_encap(const uint8_t *ip_data, int ip_data_len) | |
706 | { | |
707 | uint8_t buf[1600]; | |
708 | struct ethhdr *eh = (struct ethhdr *)buf; | |
709 | ||
710 | if (ip_data_len + ETH_HLEN > sizeof(buf)) | |
711 | return; | |
de806f07 FB |
712 | |
713 | if (!memcmp(client_ethaddr, zero_ethaddr, ETH_ALEN)) { | |
714 | uint8_t arp_req[ETH_HLEN + sizeof(struct arphdr)]; | |
715 | struct ethhdr *reh = (struct ethhdr *)arp_req; | |
716 | struct arphdr *rah = (struct arphdr *)(arp_req + ETH_HLEN); | |
717 | const struct ip *iph = (const struct ip *)ip_data; | |
718 | ||
719 | /* If the client addr is not known, there is no point in | |
720 | sending the packet to it. Normally the sender should have | |
721 | done an ARP request to get its MAC address. Here we do it | |
722 | in place of sending the packet and we hope that the sender | |
723 | will retry sending its packet. */ | |
724 | memset(reh->h_dest, 0xff, ETH_ALEN); | |
a13a4126 JK |
725 | memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4); |
726 | memcpy(&reh->h_source[2], &vhost_addr, 4); | |
de806f07 FB |
727 | reh->h_proto = htons(ETH_P_ARP); |
728 | rah->ar_hrd = htons(1); | |
729 | rah->ar_pro = htons(ETH_P_IP); | |
730 | rah->ar_hln = ETH_ALEN; | |
731 | rah->ar_pln = 4; | |
732 | rah->ar_op = htons(ARPOP_REQUEST); | |
733 | /* source hw addr */ | |
a13a4126 JK |
734 | memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN - 4); |
735 | memcpy(&rah->ar_sha[2], &vhost_addr, 4); | |
de806f07 | 736 | /* source IP */ |
a13a4126 | 737 | rah->ar_sip = vhost_addr.s_addr; |
de806f07 FB |
738 | /* target hw addr (none) */ |
739 | memset(rah->ar_tha, 0, ETH_ALEN); | |
740 | /* target IP */ | |
a13a4126 | 741 | rah->ar_tip = iph->ip_dst.s_addr; |
de806f07 FB |
742 | client_ipaddr = iph->ip_dst; |
743 | slirp_output(arp_req, sizeof(arp_req)); | |
744 | } else { | |
745 | memcpy(eh->h_dest, client_ethaddr, ETH_ALEN); | |
a13a4126 | 746 | memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 4); |
de806f07 | 747 | /* XXX: not correct */ |
a13a4126 | 748 | memcpy(&eh->h_source[2], &vhost_addr, 4); |
de806f07 FB |
749 | eh->h_proto = htons(ETH_P_IP); |
750 | memcpy(buf + sizeof(struct ethhdr), ip_data, ip_data_len); | |
751 | slirp_output(buf, ip_data_len + ETH_HLEN); | |
752 | } | |
f0cbd3ec | 753 | } |
9bf05444 | 754 | |
c1261d8d AG |
755 | /* Unlistens a redirection |
756 | * | |
757 | * Return value: number of redirs removed */ | |
758 | int slirp_redir_rm(int is_udp, int host_port) | |
759 | { | |
760 | struct socket *so; | |
761 | struct socket *head = (is_udp ? &udb : &tcb); | |
762 | int fport = htons(host_port); | |
763 | int n = 0; | |
764 | ||
765 | loop_again: | |
766 | for (so = head->so_next; so != head; so = so->so_next) { | |
767 | if (so->so_fport == fport) { | |
768 | close(so->s); | |
769 | sofree(so); | |
770 | n++; | |
771 | goto loop_again; | |
772 | } | |
773 | } | |
774 | ||
775 | return n; | |
776 | } | |
777 | ||
5fafdf24 | 778 | int slirp_redir(int is_udp, int host_port, |
9bf05444 FB |
779 | struct in_addr guest_addr, int guest_port) |
780 | { | |
a13a4126 JK |
781 | if (!guest_addr.s_addr) { |
782 | guest_addr = vdhcp_startaddr; | |
783 | } | |
9bf05444 | 784 | if (is_udp) { |
5fafdf24 | 785 | if (!udp_listen(htons(host_port), guest_addr.s_addr, |
9bf05444 FB |
786 | htons(guest_port), 0)) |
787 | return -1; | |
788 | } else { | |
5fafdf24 | 789 | if (!solisten(htons(host_port), guest_addr.s_addr, |
9bf05444 FB |
790 | htons(guest_port), 0)) |
791 | return -1; | |
792 | } | |
793 | return 0; | |
794 | } | |
a3d4af03 | 795 | |
e1c5a2b3 | 796 | int slirp_add_exec(int do_pty, const void *args, int addr_low_byte, |
a3d4af03 FB |
797 | int guest_port) |
798 | { | |
a13a4126 JK |
799 | struct in_addr guest_addr = { |
800 | .s_addr = vnetwork_addr.s_addr | htonl(addr_low_byte) | |
801 | }; | |
802 | ||
803 | if ((guest_addr.s_addr & vnetwork_mask.s_addr) != vnetwork_addr.s_addr || | |
804 | guest_addr.s_addr == vhost_addr.s_addr || | |
805 | guest_addr.s_addr == vnameserver_addr.s_addr) { | |
806 | return -1; | |
807 | } | |
808 | return add_exec(&exec_list, do_pty, (char *)args, guest_addr, | |
809 | htons(guest_port)); | |
a3d4af03 | 810 | } |
e1c5a2b3 AL |
811 | |
812 | ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags) | |
813 | { | |
814 | if (so->s == -1 && so->extra) { | |
815 | qemu_chr_write(so->extra, buf, len); | |
816 | return len; | |
817 | } | |
818 | ||
819 | return send(so->s, buf, len, flags); | |
820 | } | |
821 | ||
a13a4126 JK |
822 | static struct socket * |
823 | slirp_find_ctl_socket(struct in_addr guest_addr, int guest_port) | |
e1c5a2b3 | 824 | { |
a13a4126 | 825 | struct socket *so; |
e1c5a2b3 | 826 | |
a13a4126 JK |
827 | for (so = tcb.so_next; so != &tcb; so = so->so_next) { |
828 | if (so->so_faddr.s_addr == guest_addr.s_addr && | |
829 | htons(so->so_fport) == guest_port) { | |
830 | return so; | |
831 | } | |
832 | } | |
833 | return NULL; | |
e1c5a2b3 AL |
834 | } |
835 | ||
836 | size_t slirp_socket_can_recv(int addr_low_byte, int guest_port) | |
837 | { | |
a13a4126 JK |
838 | struct in_addr guest_addr = { |
839 | .s_addr = vnetwork_addr.s_addr | htonl(addr_low_byte) | |
840 | }; | |
e1c5a2b3 AL |
841 | struct iovec iov[2]; |
842 | struct socket *so; | |
843 | ||
844 | if (!link_up) | |
845 | return 0; | |
846 | ||
a13a4126 | 847 | so = slirp_find_ctl_socket(guest_addr, guest_port); |
e1c5a2b3 AL |
848 | |
849 | if (!so || so->so_state & SS_NOFDREF) | |
850 | return 0; | |
851 | ||
852 | if (!CONN_CANFRCV(so) || so->so_snd.sb_cc >= (so->so_snd.sb_datalen/2)) | |
853 | return 0; | |
854 | ||
855 | return sopreprbuf(so, iov, NULL); | |
856 | } | |
857 | ||
858 | void slirp_socket_recv(int addr_low_byte, int guest_port, const uint8_t *buf, | |
859 | int size) | |
860 | { | |
861 | int ret; | |
a13a4126 JK |
862 | struct in_addr guest_addr = { |
863 | .s_addr = vnetwork_addr.s_addr | htonl(addr_low_byte) | |
864 | }; | |
865 | struct socket *so = slirp_find_ctl_socket(guest_addr, guest_port); | |
866 | ||
e1c5a2b3 AL |
867 | if (!so) |
868 | return; | |
869 | ||
0580ac91 | 870 | ret = soreadbuf(so, (const char *)buf, size); |
e1c5a2b3 AL |
871 | |
872 | if (ret > 0) | |
873 | tcp_output(sototcpcb(so)); | |
874 | } | |
062e5527 AL |
875 | |
876 | static void slirp_tcp_save(QEMUFile *f, struct tcpcb *tp) | |
877 | { | |
878 | int i; | |
879 | ||
880 | qemu_put_sbe16(f, tp->t_state); | |
881 | for (i = 0; i < TCPT_NTIMERS; i++) | |
882 | qemu_put_sbe16(f, tp->t_timer[i]); | |
883 | qemu_put_sbe16(f, tp->t_rxtshift); | |
884 | qemu_put_sbe16(f, tp->t_rxtcur); | |
885 | qemu_put_sbe16(f, tp->t_dupacks); | |
886 | qemu_put_be16(f, tp->t_maxseg); | |
887 | qemu_put_sbyte(f, tp->t_force); | |
888 | qemu_put_be16(f, tp->t_flags); | |
889 | qemu_put_be32(f, tp->snd_una); | |
890 | qemu_put_be32(f, tp->snd_nxt); | |
891 | qemu_put_be32(f, tp->snd_up); | |
892 | qemu_put_be32(f, tp->snd_wl1); | |
893 | qemu_put_be32(f, tp->snd_wl2); | |
894 | qemu_put_be32(f, tp->iss); | |
895 | qemu_put_be32(f, tp->snd_wnd); | |
896 | qemu_put_be32(f, tp->rcv_wnd); | |
897 | qemu_put_be32(f, tp->rcv_nxt); | |
898 | qemu_put_be32(f, tp->rcv_up); | |
899 | qemu_put_be32(f, tp->irs); | |
900 | qemu_put_be32(f, tp->rcv_adv); | |
901 | qemu_put_be32(f, tp->snd_max); | |
902 | qemu_put_be32(f, tp->snd_cwnd); | |
903 | qemu_put_be32(f, tp->snd_ssthresh); | |
904 | qemu_put_sbe16(f, tp->t_idle); | |
905 | qemu_put_sbe16(f, tp->t_rtt); | |
906 | qemu_put_be32(f, tp->t_rtseq); | |
907 | qemu_put_sbe16(f, tp->t_srtt); | |
908 | qemu_put_sbe16(f, tp->t_rttvar); | |
909 | qemu_put_be16(f, tp->t_rttmin); | |
910 | qemu_put_be32(f, tp->max_sndwnd); | |
911 | qemu_put_byte(f, tp->t_oobflags); | |
912 | qemu_put_byte(f, tp->t_iobc); | |
913 | qemu_put_sbe16(f, tp->t_softerror); | |
914 | qemu_put_byte(f, tp->snd_scale); | |
915 | qemu_put_byte(f, tp->rcv_scale); | |
916 | qemu_put_byte(f, tp->request_r_scale); | |
917 | qemu_put_byte(f, tp->requested_s_scale); | |
918 | qemu_put_be32(f, tp->ts_recent); | |
919 | qemu_put_be32(f, tp->ts_recent_age); | |
920 | qemu_put_be32(f, tp->last_ack_sent); | |
921 | } | |
922 | ||
923 | static void slirp_sbuf_save(QEMUFile *f, struct sbuf *sbuf) | |
924 | { | |
925 | uint32_t off; | |
926 | ||
927 | qemu_put_be32(f, sbuf->sb_cc); | |
928 | qemu_put_be32(f, sbuf->sb_datalen); | |
929 | off = (uint32_t)(sbuf->sb_wptr - sbuf->sb_data); | |
930 | qemu_put_sbe32(f, off); | |
931 | off = (uint32_t)(sbuf->sb_rptr - sbuf->sb_data); | |
932 | qemu_put_sbe32(f, off); | |
933 | qemu_put_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen); | |
934 | } | |
935 | ||
936 | static void slirp_socket_save(QEMUFile *f, struct socket *so) | |
937 | { | |
938 | qemu_put_be32(f, so->so_urgc); | |
939 | qemu_put_be32(f, so->so_faddr.s_addr); | |
940 | qemu_put_be32(f, so->so_laddr.s_addr); | |
941 | qemu_put_be16(f, so->so_fport); | |
942 | qemu_put_be16(f, so->so_lport); | |
943 | qemu_put_byte(f, so->so_iptos); | |
944 | qemu_put_byte(f, so->so_emu); | |
945 | qemu_put_byte(f, so->so_type); | |
946 | qemu_put_be32(f, so->so_state); | |
947 | slirp_sbuf_save(f, &so->so_rcv); | |
948 | slirp_sbuf_save(f, &so->so_snd); | |
949 | slirp_tcp_save(f, so->so_tcpcb); | |
950 | } | |
951 | ||
952 | static void slirp_state_save(QEMUFile *f, void *opaque) | |
953 | { | |
954 | struct ex_list *ex_ptr; | |
955 | ||
956 | for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) | |
957 | if (ex_ptr->ex_pty == 3) { | |
958 | struct socket *so; | |
959 | so = slirp_find_ctl_socket(ex_ptr->ex_addr, ntohs(ex_ptr->ex_fport)); | |
960 | if (!so) | |
961 | continue; | |
962 | ||
963 | qemu_put_byte(f, 42); | |
964 | slirp_socket_save(f, so); | |
965 | } | |
966 | qemu_put_byte(f, 0); | |
967 | } | |
968 | ||
969 | static void slirp_tcp_load(QEMUFile *f, struct tcpcb *tp) | |
970 | { | |
971 | int i; | |
972 | ||
973 | tp->t_state = qemu_get_sbe16(f); | |
974 | for (i = 0; i < TCPT_NTIMERS; i++) | |
975 | tp->t_timer[i] = qemu_get_sbe16(f); | |
976 | tp->t_rxtshift = qemu_get_sbe16(f); | |
977 | tp->t_rxtcur = qemu_get_sbe16(f); | |
978 | tp->t_dupacks = qemu_get_sbe16(f); | |
979 | tp->t_maxseg = qemu_get_be16(f); | |
980 | tp->t_force = qemu_get_sbyte(f); | |
981 | tp->t_flags = qemu_get_be16(f); | |
982 | tp->snd_una = qemu_get_be32(f); | |
983 | tp->snd_nxt = qemu_get_be32(f); | |
984 | tp->snd_up = qemu_get_be32(f); | |
985 | tp->snd_wl1 = qemu_get_be32(f); | |
986 | tp->snd_wl2 = qemu_get_be32(f); | |
987 | tp->iss = qemu_get_be32(f); | |
988 | tp->snd_wnd = qemu_get_be32(f); | |
989 | tp->rcv_wnd = qemu_get_be32(f); | |
990 | tp->rcv_nxt = qemu_get_be32(f); | |
991 | tp->rcv_up = qemu_get_be32(f); | |
992 | tp->irs = qemu_get_be32(f); | |
993 | tp->rcv_adv = qemu_get_be32(f); | |
994 | tp->snd_max = qemu_get_be32(f); | |
995 | tp->snd_cwnd = qemu_get_be32(f); | |
996 | tp->snd_ssthresh = qemu_get_be32(f); | |
997 | tp->t_idle = qemu_get_sbe16(f); | |
998 | tp->t_rtt = qemu_get_sbe16(f); | |
999 | tp->t_rtseq = qemu_get_be32(f); | |
1000 | tp->t_srtt = qemu_get_sbe16(f); | |
1001 | tp->t_rttvar = qemu_get_sbe16(f); | |
1002 | tp->t_rttmin = qemu_get_be16(f); | |
1003 | tp->max_sndwnd = qemu_get_be32(f); | |
1004 | tp->t_oobflags = qemu_get_byte(f); | |
1005 | tp->t_iobc = qemu_get_byte(f); | |
1006 | tp->t_softerror = qemu_get_sbe16(f); | |
1007 | tp->snd_scale = qemu_get_byte(f); | |
1008 | tp->rcv_scale = qemu_get_byte(f); | |
1009 | tp->request_r_scale = qemu_get_byte(f); | |
1010 | tp->requested_s_scale = qemu_get_byte(f); | |
1011 | tp->ts_recent = qemu_get_be32(f); | |
1012 | tp->ts_recent_age = qemu_get_be32(f); | |
1013 | tp->last_ack_sent = qemu_get_be32(f); | |
1014 | tcp_template(tp); | |
1015 | } | |
1016 | ||
1017 | static int slirp_sbuf_load(QEMUFile *f, struct sbuf *sbuf) | |
1018 | { | |
1019 | uint32_t off, sb_cc, sb_datalen; | |
1020 | ||
1021 | sb_cc = qemu_get_be32(f); | |
1022 | sb_datalen = qemu_get_be32(f); | |
1023 | ||
1024 | sbreserve(sbuf, sb_datalen); | |
1025 | ||
1026 | if (sbuf->sb_datalen != sb_datalen) | |
1027 | return -ENOMEM; | |
1028 | ||
1029 | sbuf->sb_cc = sb_cc; | |
1030 | ||
1031 | off = qemu_get_sbe32(f); | |
1032 | sbuf->sb_wptr = sbuf->sb_data + off; | |
1033 | off = qemu_get_sbe32(f); | |
1034 | sbuf->sb_rptr = sbuf->sb_data + off; | |
1035 | qemu_get_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen); | |
1036 | ||
1037 | return 0; | |
1038 | } | |
1039 | ||
1040 | static int slirp_socket_load(QEMUFile *f, struct socket *so) | |
1041 | { | |
1042 | if (tcp_attach(so) < 0) | |
1043 | return -ENOMEM; | |
1044 | ||
1045 | so->so_urgc = qemu_get_be32(f); | |
1046 | so->so_faddr.s_addr = qemu_get_be32(f); | |
1047 | so->so_laddr.s_addr = qemu_get_be32(f); | |
1048 | so->so_fport = qemu_get_be16(f); | |
1049 | so->so_lport = qemu_get_be16(f); | |
1050 | so->so_iptos = qemu_get_byte(f); | |
1051 | so->so_emu = qemu_get_byte(f); | |
1052 | so->so_type = qemu_get_byte(f); | |
1053 | so->so_state = qemu_get_be32(f); | |
1054 | if (slirp_sbuf_load(f, &so->so_rcv) < 0) | |
1055 | return -ENOMEM; | |
1056 | if (slirp_sbuf_load(f, &so->so_snd) < 0) | |
1057 | return -ENOMEM; | |
1058 | slirp_tcp_load(f, so->so_tcpcb); | |
1059 | ||
1060 | return 0; | |
1061 | } | |
1062 | ||
1063 | static int slirp_state_load(QEMUFile *f, void *opaque, int version_id) | |
1064 | { | |
1065 | struct ex_list *ex_ptr; | |
1066 | int r; | |
1067 | ||
1068 | while ((r = qemu_get_byte(f))) { | |
1069 | int ret; | |
1070 | struct socket *so = socreate(); | |
1071 | ||
1072 | if (!so) | |
1073 | return -ENOMEM; | |
1074 | ||
1075 | ret = slirp_socket_load(f, so); | |
1076 | ||
1077 | if (ret < 0) | |
1078 | return ret; | |
1079 | ||
a13a4126 JK |
1080 | if ((so->so_faddr.s_addr & vnetwork_mask.s_addr) != |
1081 | vnetwork_addr.s_addr) { | |
062e5527 | 1082 | return -EINVAL; |
a13a4126 JK |
1083 | } |
1084 | for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) { | |
062e5527 | 1085 | if (ex_ptr->ex_pty == 3 && |
a13a4126 JK |
1086 | so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr && |
1087 | so->so_fport == ex_ptr->ex_fport) { | |
062e5527 | 1088 | break; |
a13a4126 JK |
1089 | } |
1090 | } | |
062e5527 AL |
1091 | if (!ex_ptr) |
1092 | return -EINVAL; | |
1093 | ||
0580ac91 | 1094 | so->extra = (void *)ex_ptr->ex_exec; |
062e5527 AL |
1095 | } |
1096 | ||
1097 | return 0; | |
1098 | } |