]>
Commit | Line | Data |
---|---|---|
6087fd53 | 1 | /* SPDX-License-Identifier: MIT */ |
d75a0b97 FB |
2 | /* |
3 | * libslirp glue | |
4 | * | |
5 | * Copyright (c) 2004-2008 Fabrice Bellard | |
6 | * | |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 | * of this software and associated documentation files (the "Software"), to deal | |
9 | * in the Software without restriction, including without limitation the rights | |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the Software is | |
12 | * furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
23 | * THE SOFTWARE. | |
24 | */ | |
f0cbd3ec FB |
25 | #include "slirp.h" |
26 | ||
268c95d7 | 27 | |
ef763fa4 ST |
28 | #ifndef _WIN32 |
29 | #include <net/if.h> | |
30 | #endif | |
31 | ||
039d5b63 MAL |
32 | int slirp_debug; |
33 | ||
987d3a51 MAL |
34 | /* Define to 1 if you want KEEPALIVE timers */ |
35 | bool slirp_do_keepalive; | |
36 | ||
f0cbd3ec FB |
37 | /* host loopback address */ |
38 | struct in_addr loopback_addr; | |
648cd33e | 39 | /* host loopback network mask */ |
0b8db8fe | 40 | unsigned long loopback_mask; |
f0cbd3ec | 41 | |
a13a4126 | 42 | /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */ |
1a0ca1e1 | 43 | static const uint8_t special_ethaddr[ETH_ALEN] = { |
a13a4126 | 44 | 0x52, 0x55, 0x00, 0x00, 0x00, 0x00 |
f0cbd3ec FB |
45 | }; |
46 | ||
d7df0b41 | 47 | unsigned curtime; |
f1d99bbd | 48 | |
9e3a95ef | 49 | static struct in_addr dns_addr; |
ef763fa4 | 50 | #ifndef _WIN32 |
1d17654e | 51 | static struct in6_addr dns6_addr; |
ef763fa4 | 52 | #endif |
d7df0b41 | 53 | static unsigned dns_addr_time; |
ef763fa4 | 54 | #ifndef _WIN32 |
d7df0b41 | 55 | static unsigned dns6_addr_time; |
ef763fa4 | 56 | #endif |
df7a86ed | 57 | |
9b0ca6cc LPF |
58 | #define TIMEOUT_FAST 2 /* milliseconds */ |
59 | #define TIMEOUT_SLOW 499 /* milliseconds */ | |
60 | /* for the aging of certain requests like DNS */ | |
61 | #define TIMEOUT_DEFAULT 1000 /* milliseconds */ | |
62 | ||
f0cbd3ec FB |
63 | #ifdef _WIN32 |
64 | ||
df7a86ed | 65 | int get_dns_addr(struct in_addr *pdns_addr) |
f0cbd3ec | 66 | { |
379ff53d FB |
67 | FIXED_INFO *FixedInfo=NULL; |
68 | ULONG BufLen; | |
69 | DWORD ret; | |
70 | IP_ADDR_STRING *pIPAddr; | |
71 | struct in_addr tmp_addr; | |
3b46e624 | 72 | |
9b0ca6cc | 73 | if (dns_addr.s_addr != 0 && (curtime - dns_addr_time) < TIMEOUT_DEFAULT) { |
df7a86ed ES |
74 | *pdns_addr = dns_addr; |
75 | return 0; | |
76 | } | |
77 | ||
379ff53d FB |
78 | FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO)); |
79 | BufLen = sizeof(FIXED_INFO); | |
3b46e624 | 80 | |
379ff53d FB |
81 | if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen)) { |
82 | if (FixedInfo) { | |
83 | GlobalFree(FixedInfo); | |
84 | FixedInfo = NULL; | |
85 | } | |
86 | FixedInfo = GlobalAlloc(GPTR, BufLen); | |
87 | } | |
5fafdf24 | 88 | |
379ff53d | 89 | if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS) { |
d7df0b41 | 90 | printf("GetNetworkParams failed. ret = %08x\n", (unsigned)ret ); |
379ff53d FB |
91 | if (FixedInfo) { |
92 | GlobalFree(FixedInfo); | |
93 | FixedInfo = NULL; | |
94 | } | |
95 | return -1; | |
96 | } | |
3b46e624 | 97 | |
379ff53d FB |
98 | pIPAddr = &(FixedInfo->DnsServerList); |
99 | inet_aton(pIPAddr->IpAddress.String, &tmp_addr); | |
100 | *pdns_addr = tmp_addr; | |
df7a86ed ES |
101 | dns_addr = tmp_addr; |
102 | dns_addr_time = curtime; | |
379ff53d FB |
103 | if (FixedInfo) { |
104 | GlobalFree(FixedInfo); | |
105 | FixedInfo = NULL; | |
106 | } | |
107 | return 0; | |
f0cbd3ec FB |
108 | } |
109 | ||
ef763fa4 | 110 | int get_dns6_addr(struct in6_addr *pdns6_addr, uint32_t *scope_id) |
1d17654e ST |
111 | { |
112 | return -1; | |
113 | } | |
114 | ||
df461894 JK |
115 | static void winsock_cleanup(void) |
116 | { | |
117 | WSACleanup(); | |
118 | } | |
119 | ||
f0cbd3ec FB |
120 | #else |
121 | ||
1d17654e ST |
122 | static int get_dns_addr_cached(void *pdns_addr, void *cached_addr, |
123 | socklen_t addrlen, | |
d7df0b41 | 124 | struct stat *cached_stat, unsigned *cached_time) |
972487b8 ST |
125 | { |
126 | struct stat old_stat; | |
1d17654e ST |
127 | if (curtime - *cached_time < TIMEOUT_DEFAULT) { |
128 | memcpy(pdns_addr, cached_addr, addrlen); | |
972487b8 ST |
129 | return 0; |
130 | } | |
1d17654e ST |
131 | old_stat = *cached_stat; |
132 | if (stat("/etc/resolv.conf", cached_stat) != 0) { | |
972487b8 ST |
133 | return -1; |
134 | } | |
1d17654e ST |
135 | if (cached_stat->st_dev == old_stat.st_dev |
136 | && cached_stat->st_ino == old_stat.st_ino | |
137 | && cached_stat->st_size == old_stat.st_size | |
138 | && cached_stat->st_mtime == old_stat.st_mtime) { | |
139 | memcpy(pdns_addr, cached_addr, addrlen); | |
972487b8 ST |
140 | return 0; |
141 | } | |
142 | return 1; | |
143 | } | |
144 | ||
1d17654e | 145 | static int get_dns_addr_resolv_conf(int af, void *pdns_addr, void *cached_addr, |
ef763fa4 | 146 | socklen_t addrlen, uint32_t *scope_id, |
d7df0b41 | 147 | unsigned *cached_time) |
f0cbd3ec FB |
148 | { |
149 | char buff[512]; | |
363a37d5 | 150 | char buff2[257]; |
f0cbd3ec FB |
151 | FILE *f; |
152 | int found = 0; | |
1d17654e | 153 | void *tmp_addr = alloca(addrlen); |
ef763fa4 | 154 | unsigned if_index; |
3b46e624 | 155 | |
f0cbd3ec FB |
156 | f = fopen("/etc/resolv.conf", "r"); |
157 | if (!f) | |
158 | return -1; | |
159 | ||
226ea7a9 | 160 | DEBUG_MISC("IP address of your DNS(s):"); |
f0cbd3ec FB |
161 | while (fgets(buff, 512, f) != NULL) { |
162 | if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) { | |
ef763fa4 ST |
163 | char *c = strchr(buff2, '%'); |
164 | if (c) { | |
165 | if_index = if_nametoindex(c + 1); | |
166 | *c = '\0'; | |
167 | } else { | |
168 | if_index = 0; | |
169 | } | |
170 | ||
1d17654e | 171 | if (!inet_pton(af, buff2, tmp_addr)) { |
f0cbd3ec | 172 | continue; |
1d17654e | 173 | } |
f0cbd3ec | 174 | /* If it's the first one, set it to dns_addr */ |
df7a86ed | 175 | if (!found) { |
1d17654e ST |
176 | memcpy(pdns_addr, tmp_addr, addrlen); |
177 | memcpy(cached_addr, tmp_addr, addrlen); | |
ef763fa4 ST |
178 | if (scope_id) { |
179 | *scope_id = if_index; | |
180 | } | |
1d17654e | 181 | *cached_time = curtime; |
df7a86ed | 182 | } |
71761518 | 183 | |
f0cbd3ec | 184 | if (++found > 3) { |
226ea7a9 | 185 | DEBUG_MISC(" (more)"); |
f0cbd3ec | 186 | break; |
eb0b1596 | 187 | } else if (slirp_debug & DBG_MISC) { |
1d17654e | 188 | char s[INET6_ADDRSTRLEN]; |
51149a2a | 189 | const char *res = inet_ntop(af, tmp_addr, s, sizeof(s)); |
1d17654e | 190 | if (!res) { |
226ea7a9 | 191 | res = " (string conversion error)"; |
1d17654e | 192 | } |
226ea7a9 | 193 | DEBUG_MISC(" %s", res); |
1d17654e | 194 | } |
f0cbd3ec FB |
195 | } |
196 | } | |
1d43a717 | 197 | fclose(f); |
f0cbd3ec FB |
198 | if (!found) |
199 | return -1; | |
200 | return 0; | |
201 | } | |
202 | ||
972487b8 ST |
203 | int get_dns_addr(struct in_addr *pdns_addr) |
204 | { | |
1d17654e ST |
205 | static struct stat dns_addr_stat; |
206 | ||
972487b8 ST |
207 | if (dns_addr.s_addr != 0) { |
208 | int ret; | |
1d17654e ST |
209 | ret = get_dns_addr_cached(pdns_addr, &dns_addr, sizeof(dns_addr), |
210 | &dns_addr_stat, &dns_addr_time); | |
211 | if (ret <= 0) { | |
212 | return ret; | |
213 | } | |
214 | } | |
215 | return get_dns_addr_resolv_conf(AF_INET, pdns_addr, &dns_addr, | |
ef763fa4 | 216 | sizeof(dns_addr), NULL, &dns_addr_time); |
1d17654e ST |
217 | } |
218 | ||
ef763fa4 | 219 | int get_dns6_addr(struct in6_addr *pdns6_addr, uint32_t *scope_id) |
1d17654e ST |
220 | { |
221 | static struct stat dns6_addr_stat; | |
222 | ||
223 | if (!in6_zero(&dns6_addr)) { | |
224 | int ret; | |
225 | ret = get_dns_addr_cached(pdns6_addr, &dns6_addr, sizeof(dns6_addr), | |
226 | &dns6_addr_stat, &dns6_addr_time); | |
972487b8 ST |
227 | if (ret <= 0) { |
228 | return ret; | |
229 | } | |
230 | } | |
1d17654e | 231 | return get_dns_addr_resolv_conf(AF_INET6, pdns6_addr, &dns6_addr, |
ef763fa4 ST |
232 | sizeof(dns6_addr), |
233 | scope_id, &dns6_addr_time); | |
972487b8 ST |
234 | } |
235 | ||
f0cbd3ec FB |
236 | #endif |
237 | ||
df461894 | 238 | static void slirp_init_once(void) |
379ff53d | 239 | { |
df461894 | 240 | static int initialized; |
039d5b63 | 241 | const char *debug; |
df461894 JK |
242 | #ifdef _WIN32 |
243 | WSADATA Data; | |
379ff53d FB |
244 | #endif |
245 | ||
df461894 JK |
246 | if (initialized) { |
247 | return; | |
248 | } | |
249 | initialized = 1; | |
250 | ||
251 | #ifdef _WIN32 | |
252 | WSAStartup(MAKEWORD(2,0), &Data); | |
253 | atexit(winsock_cleanup); | |
254 | #endif | |
255 | ||
256 | loopback_addr.s_addr = htonl(INADDR_LOOPBACK); | |
648cd33e | 257 | loopback_mask = htonl(IN_CLASSA_NET); |
039d5b63 MAL |
258 | |
259 | debug = g_getenv("SLIRP_DEBUG"); | |
260 | if (debug) { | |
261 | const GDebugKey keys[] = { | |
262 | { "call", DBG_CALL }, | |
263 | { "misc", DBG_MISC }, | |
264 | { "error", DBG_ERROR }, | |
7fce7172 | 265 | { "tftp", DBG_TFTP }, |
039d5b63 MAL |
266 | }; |
267 | slirp_debug = g_parse_debug_string(debug, keys, G_N_ELEMENTS(keys)); | |
268 | } | |
269 | ||
270 | ||
df461894 JK |
271 | } |
272 | ||
0b11c036 | 273 | Slirp *slirp_init(int restricted, bool in_enabled, struct in_addr vnetwork, |
9f8bd042 | 274 | struct in_addr vnetmask, struct in_addr vhost, |
0b11c036 | 275 | bool in6_enabled, |
7aac531e YB |
276 | struct in6_addr vprefix_addr6, uint8_t vprefix_len, |
277 | struct in6_addr vhost6, const char *vhostname, | |
0fca92b9 | 278 | const char *tftp_server_name, |
7aac531e YB |
279 | const char *tftp_path, const char *bootfile, |
280 | struct in_addr vdhcp_start, struct in_addr vnameserver, | |
281 | struct in6_addr vnameserver6, const char **vdnssearch, | |
62c1d2c4 | 282 | const char *vdomainname, |
d846b927 | 283 | const SlirpCb *callbacks, |
62c1d2c4 | 284 | void *opaque) |
f0cbd3ec | 285 | { |
7267c094 | 286 | Slirp *slirp = g_malloc0(sizeof(Slirp)); |
460fec67 | 287 | |
df461894 | 288 | slirp_init_once(); |
379ff53d | 289 | |
3e0fad3a | 290 | slirp->opaque = opaque; |
d846b927 | 291 | slirp->cb = callbacks; |
0d6ff71a | 292 | slirp->grand = g_rand_new(); |
460fec67 | 293 | slirp->restricted = restricted; |
f0cbd3ec | 294 | |
0b11c036 ST |
295 | slirp->in_enabled = in_enabled; |
296 | slirp->in6_enabled = in6_enabled; | |
297 | ||
460fec67 JK |
298 | if_init(slirp); |
299 | ip_init(slirp); | |
0d6ff71a | 300 | ip6_init(slirp); |
f0cbd3ec FB |
301 | |
302 | /* Initialise mbufs *after* setting the MTU */ | |
460fec67 | 303 | m_init(slirp); |
f0cbd3ec | 304 | |
460fec67 JK |
305 | slirp->vnetwork_addr = vnetwork; |
306 | slirp->vnetwork_mask = vnetmask; | |
307 | slirp->vhost_addr = vhost; | |
7aac531e YB |
308 | slirp->vprefix_addr6 = vprefix_addr6; |
309 | slirp->vprefix_len = vprefix_len; | |
310 | slirp->vhost_addr6 = vhost6; | |
c92ef6a2 | 311 | if (vhostname) { |
d1c4b3e9 MAL |
312 | slirp_pstrcpy(slirp->client_hostname, sizeof(slirp->client_hostname), |
313 | vhostname); | |
a13a4126 | 314 | } |
c64f50d1 MA |
315 | slirp->tftp_prefix = g_strdup(tftp_path); |
316 | slirp->bootp_filename = g_strdup(bootfile); | |
f18d1375 | 317 | slirp->vdomainname = g_strdup(vdomainname); |
460fec67 JK |
318 | slirp->vdhcp_startaddr = vdhcp_start; |
319 | slirp->vnameserver_addr = vnameserver; | |
7aac531e | 320 | slirp->vnameserver_addr6 = vnameserver6; |
0fca92b9 | 321 | slirp->tftp_server_name = g_strdup(tftp_server_name); |
ad196a9d | 322 | |
63d2960b KS |
323 | if (vdnssearch) { |
324 | translate_dnssearch(slirp, vdnssearch); | |
325 | } | |
326 | ||
9f8bd042 | 327 | return slirp; |
f0cbd3ec FB |
328 | } |
329 | ||
ad0d8c4c JK |
330 | void slirp_cleanup(Slirp *slirp) |
331 | { | |
5d300fc9 | 332 | struct gfwd_list *e, *next; |
c27f2478 | 333 | |
5d300fc9 | 334 | for (e = slirp->guestfwd_list; e; e = next) { |
c27f2478 MAL |
335 | next = e->ex_next; |
336 | g_free(e->ex_exec); | |
337 | g_free(e); | |
338 | } | |
339 | ||
a68adc22 | 340 | ip_cleanup(slirp); |
0d6ff71a | 341 | ip6_cleanup(slirp); |
a68adc22 JK |
342 | m_cleanup(slirp); |
343 | ||
0d6ff71a GS |
344 | g_rand_free(slirp->grand); |
345 | ||
63d2960b | 346 | g_free(slirp->vdnssearch); |
7267c094 AL |
347 | g_free(slirp->tftp_prefix); |
348 | g_free(slirp->bootp_filename); | |
f18d1375 | 349 | g_free(slirp->vdomainname); |
7267c094 | 350 | g_free(slirp); |
ad0d8c4c JK |
351 | } |
352 | ||
f0cbd3ec FB |
353 | #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED) |
354 | #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED) | |
f0cbd3ec | 355 | |
1ab67b98 | 356 | static void slirp_update_timeout(Slirp *slirp, uint32_t *timeout) |
7c7db755 | 357 | { |
a42e9c41 LPF |
358 | uint32_t t; |
359 | ||
360 | if (*timeout <= TIMEOUT_FAST) { | |
361 | return; | |
362 | } | |
426e3e6c JK |
363 | |
364 | t = MIN(1000, *timeout); | |
a42e9c41 LPF |
365 | |
366 | /* If we have tcp timeout with slirp, then we will fill @timeout with | |
367 | * more precise value. | |
368 | */ | |
1ab67b98 MAL |
369 | if (slirp->time_fasttimo) { |
370 | *timeout = TIMEOUT_FAST; | |
371 | return; | |
372 | } | |
373 | if (slirp->do_slowtimo) { | |
374 | t = MIN(TIMEOUT_SLOW, t); | |
7c7db755 | 375 | } |
a42e9c41 | 376 | *timeout = t; |
7c7db755 SS |
377 | } |
378 | ||
deaeb3f7 MAL |
379 | void slirp_pollfds_fill(Slirp *slirp, uint32_t *timeout, |
380 | SlirpAddPollCb add_poll, void *opaque) | |
f0cbd3ec FB |
381 | { |
382 | struct socket *so, *so_next; | |
f0cbd3ec | 383 | |
cf1d078e SH |
384 | /* |
385 | * First, TCP sockets | |
386 | */ | |
e6d43cfb | 387 | |
1ab67b98 MAL |
388 | /* |
389 | * *_slowtimo needs calling if there are IP fragments | |
390 | * in the fragment queue, or there are TCP connections active | |
391 | */ | |
392 | slirp->do_slowtimo = ((slirp->tcb.so_next != &slirp->tcb) || | |
393 | (&slirp->ipq.ip_link != slirp->ipq.ip_link.next)); | |
cf1d078e | 394 | |
1ab67b98 MAL |
395 | for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so_next) { |
396 | int events = 0; | |
8917c3bd | 397 | |
1ab67b98 | 398 | so_next = so->so_next; |
e6d43cfb | 399 | |
1ab67b98 | 400 | so->pollfds_idx = -1; |
e6d43cfb | 401 | |
1ab67b98 MAL |
402 | /* |
403 | * See if we need a tcp_fasttimo | |
404 | */ | |
405 | if (slirp->time_fasttimo == 0 && | |
406 | so->so_tcpcb->t_flags & TF_DELACK) { | |
407 | slirp->time_fasttimo = curtime; /* Flag when want a fasttimo */ | |
408 | } | |
cf1d078e | 409 | |
1ab67b98 MAL |
410 | /* |
411 | * NOFDREF can include still connecting to local-host, | |
412 | * newly socreated() sockets etc. Don't want to select these. | |
413 | */ | |
414 | if (so->so_state & SS_NOFDREF || so->s == -1) { | |
415 | continue; | |
416 | } | |
cf1d078e | 417 | |
1ab67b98 MAL |
418 | /* |
419 | * Set for reading sockets which are accepting | |
420 | */ | |
421 | if (so->so_state & SS_FACCEPTCONN) { | |
deaeb3f7 MAL |
422 | so->pollfds_idx = add_poll(so->s, |
423 | SLIRP_POLL_IN | SLIRP_POLL_HUP | SLIRP_POLL_ERR, opaque); | |
1ab67b98 MAL |
424 | continue; |
425 | } | |
cf1d078e | 426 | |
1ab67b98 MAL |
427 | /* |
428 | * Set for writing sockets which are connecting | |
429 | */ | |
430 | if (so->so_state & SS_ISFCONNECTING) { | |
deaeb3f7 MAL |
431 | so->pollfds_idx = add_poll(so->s, |
432 | SLIRP_POLL_OUT | SLIRP_POLL_ERR, opaque); | |
1ab67b98 MAL |
433 | continue; |
434 | } | |
8917c3bd | 435 | |
1ab67b98 MAL |
436 | /* |
437 | * Set for writing if we are connected, can send more, and | |
438 | * we have something to send | |
439 | */ | |
440 | if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) { | |
deaeb3f7 | 441 | events |= SLIRP_POLL_OUT | SLIRP_POLL_ERR; |
cf1d078e SH |
442 | } |
443 | ||
444 | /* | |
1ab67b98 MAL |
445 | * Set for reading (and urgent data) if we are connected, can |
446 | * receive more, and we have room for it XXX /2 ? | |
cf1d078e | 447 | */ |
1ab67b98 MAL |
448 | if (CONN_CANFRCV(so) && |
449 | (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) { | |
deaeb3f7 MAL |
450 | events |= SLIRP_POLL_IN | SLIRP_POLL_HUP | |
451 | SLIRP_POLL_ERR | SLIRP_POLL_PRI; | |
1ab67b98 | 452 | } |
cf1d078e | 453 | |
1ab67b98 | 454 | if (events) { |
deaeb3f7 | 455 | so->pollfds_idx = add_poll(so->s, events, opaque); |
1ab67b98 MAL |
456 | } |
457 | } | |
8917c3bd | 458 | |
1ab67b98 MAL |
459 | /* |
460 | * UDP sockets | |
461 | */ | |
462 | for (so = slirp->udb.so_next; so != &slirp->udb; so = so_next) { | |
463 | so_next = so->so_next; | |
cf1d078e | 464 | |
1ab67b98 MAL |
465 | so->pollfds_idx = -1; |
466 | ||
467 | /* | |
468 | * See if it's timed out | |
469 | */ | |
470 | if (so->so_expire) { | |
471 | if (so->so_expire <= curtime) { | |
472 | udp_detach(so); | |
473 | continue; | |
474 | } else { | |
475 | slirp->do_slowtimo = true; /* Let socket expire */ | |
cf1d078e SH |
476 | } |
477 | } | |
5fafdf24 | 478 | |
cf1d078e | 479 | /* |
1ab67b98 MAL |
480 | * When UDP packets are received from over the |
481 | * link, they're sendto()'d straight away, so | |
482 | * no need for setting for writing | |
483 | * Limit the number of packets queued by this session | |
484 | * to 4. Note that even though we try and limit this | |
485 | * to 4 packets, the session could have more queued | |
486 | * if the packets needed to be fragmented | |
487 | * (XXX <= 4 ?) | |
cf1d078e | 488 | */ |
1ab67b98 | 489 | if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) { |
deaeb3f7 MAL |
490 | so->pollfds_idx = add_poll(so->s, |
491 | SLIRP_POLL_IN | SLIRP_POLL_HUP | SLIRP_POLL_ERR, opaque); | |
1ab67b98 MAL |
492 | } |
493 | } | |
cf1d078e | 494 | |
1ab67b98 MAL |
495 | /* |
496 | * ICMP sockets | |
497 | */ | |
498 | for (so = slirp->icmp.so_next; so != &slirp->icmp; so = so_next) { | |
499 | so_next = so->so_next; | |
8917c3bd | 500 | |
1ab67b98 | 501 | so->pollfds_idx = -1; |
cf1d078e | 502 | |
1ab67b98 MAL |
503 | /* |
504 | * See if it's timed out | |
505 | */ | |
506 | if (so->so_expire) { | |
507 | if (so->so_expire <= curtime) { | |
508 | icmp_detach(so); | |
509 | continue; | |
510 | } else { | |
511 | slirp->do_slowtimo = true; /* Let socket expire */ | |
cf1d078e SH |
512 | } |
513 | } | |
1ab67b98 MAL |
514 | |
515 | if (so->so_state & SS_ISFCONNECTED) { | |
deaeb3f7 MAL |
516 | so->pollfds_idx = add_poll(so->s, |
517 | SLIRP_POLL_IN | SLIRP_POLL_HUP | SLIRP_POLL_ERR, opaque); | |
1ab67b98 | 518 | } |
cf1d078e | 519 | } |
1ab67b98 MAL |
520 | |
521 | slirp_update_timeout(slirp, timeout); | |
5fafdf24 | 522 | } |
f0cbd3ec | 523 | |
deaeb3f7 MAL |
524 | void slirp_pollfds_poll(Slirp *slirp, int select_error, |
525 | SlirpGetREventsCb get_revents, void *opaque) | |
f0cbd3ec FB |
526 | { |
527 | struct socket *so, *so_next; | |
528 | int ret; | |
529 | ||
3e0fad3a | 530 | curtime = slirp->cb->clock_get_ns(slirp->opaque) / SCALE_MS; |
5fafdf24 | 531 | |
1ab67b98 MAL |
532 | /* |
533 | * See if anything has timed out | |
534 | */ | |
535 | if (slirp->time_fasttimo && | |
536 | ((curtime - slirp->time_fasttimo) >= TIMEOUT_FAST)) { | |
537 | tcp_fasttimo(slirp); | |
538 | slirp->time_fasttimo = 0; | |
539 | } | |
540 | if (slirp->do_slowtimo && | |
541 | ((curtime - slirp->last_slowtimo) >= TIMEOUT_SLOW)) { | |
542 | ip_slowtimo(slirp); | |
543 | tcp_slowtimo(slirp); | |
544 | slirp->last_slowtimo = curtime; | |
545 | } | |
cf1d078e | 546 | |
1ab67b98 MAL |
547 | /* |
548 | * Check sockets | |
549 | */ | |
550 | if (!select_error) { | |
cf1d078e | 551 | /* |
1ab67b98 | 552 | * Check TCP sockets |
cf1d078e | 553 | */ |
1ab67b98 MAL |
554 | for (so = slirp->tcb.so_next; so != &slirp->tcb; |
555 | so = so_next) { | |
556 | int revents; | |
8917c3bd | 557 | |
1ab67b98 | 558 | so_next = so->so_next; |
cf1d078e | 559 | |
1ab67b98 MAL |
560 | revents = 0; |
561 | if (so->pollfds_idx != -1) { | |
deaeb3f7 | 562 | revents = get_revents(so->pollfds_idx, opaque); |
1ab67b98 | 563 | } |
8917c3bd | 564 | |
1ab67b98 MAL |
565 | if (so->so_state & SS_NOFDREF || so->s == -1) { |
566 | continue; | |
567 | } | |
568 | ||
569 | /* | |
570 | * Check for URG data | |
571 | * This will soread as well, so no need to | |
deaeb3f7 | 572 | * test for SLIRP_POLL_IN below if this succeeds |
1ab67b98 | 573 | */ |
deaeb3f7 | 574 | if (revents & SLIRP_POLL_PRI) { |
1ab67b98 MAL |
575 | ret = sorecvoob(so); |
576 | if (ret < 0) { | |
577 | /* Socket error might have resulted in the socket being | |
578 | * removed, do not try to do anything more with it. */ | |
cf1d078e SH |
579 | continue; |
580 | } | |
1ab67b98 MAL |
581 | } |
582 | /* | |
583 | * Check sockets for reading | |
584 | */ | |
deaeb3f7 MAL |
585 | else if (revents & |
586 | (SLIRP_POLL_IN | SLIRP_POLL_HUP | SLIRP_POLL_ERR)) { | |
cf1d078e | 587 | /* |
1ab67b98 | 588 | * Check for incoming connections |
cf1d078e | 589 | */ |
1ab67b98 MAL |
590 | if (so->so_state & SS_FACCEPTCONN) { |
591 | tcp_connect(so); | |
592 | continue; | |
593 | } /* else */ | |
594 | ret = soread(so); | |
595 | ||
596 | /* Output it if we read something */ | |
597 | if (ret > 0) { | |
598 | tcp_output(sototcpcb(so)); | |
599 | } | |
600 | if (ret < 0) { | |
601 | /* Socket error might have resulted in the socket being | |
602 | * removed, do not try to do anything more with it. */ | |
603 | continue; | |
cf1d078e | 604 | } |
1ab67b98 MAL |
605 | } |
606 | ||
607 | /* | |
608 | * Check sockets for writing | |
609 | */ | |
610 | if (!(so->so_state & SS_NOFDREF) && | |
deaeb3f7 | 611 | (revents & (SLIRP_POLL_OUT | SLIRP_POLL_ERR))) { |
cf1d078e | 612 | /* |
1ab67b98 | 613 | * Check for non-blocking, still-connecting sockets |
cf1d078e | 614 | */ |
1ab67b98 MAL |
615 | if (so->so_state & SS_ISFCONNECTING) { |
616 | /* Connected */ | |
617 | so->so_state &= ~SS_ISFCONNECTING; | |
cf1d078e | 618 | |
1ab67b98 | 619 | ret = send(so->s, (const void *) &ret, 0, 0); |
bfb1ac14 | 620 | if (ret < 0) { |
1ab67b98 MAL |
621 | /* XXXXX Must fix, zero bytes is a NOP */ |
622 | if (errno == EAGAIN || errno == EWOULDBLOCK || | |
623 | errno == EINPROGRESS || errno == ENOTCONN) { | |
624 | continue; | |
625 | } | |
626 | ||
627 | /* else failed */ | |
628 | so->so_state &= SS_PERSISTENT_MASK; | |
629 | so->so_state |= SS_NOFDREF; | |
bfb1ac14 | 630 | } |
1ab67b98 | 631 | /* else so->so_state &= ~SS_ISFCONNECTING; */ |
cf1d078e | 632 | |
cf1d078e | 633 | /* |
1ab67b98 | 634 | * Continue tcp_input |
cf1d078e | 635 | */ |
1ab67b98 MAL |
636 | tcp_input((struct mbuf *)NULL, sizeof(struct ip), so, |
637 | so->so_ffamily); | |
638 | /* continue; */ | |
639 | } else { | |
640 | ret = sowrite(so); | |
641 | if (ret > 0) { | |
642 | /* Call tcp_output in case we need to send a window | |
643 | * update to the guest, otherwise it will be stuck | |
644 | * until it sends a window probe. */ | |
645 | tcp_output(sototcpcb(so)); | |
cf1d078e | 646 | } |
cf1d078e | 647 | } |
cf1d078e | 648 | } |
1ab67b98 | 649 | } |
cf1d078e | 650 | |
1ab67b98 MAL |
651 | /* |
652 | * Now UDP sockets. | |
653 | * Incoming packets are sent straight away, they're not buffered. | |
654 | * Incoming UDP data isn't buffered either. | |
655 | */ | |
656 | for (so = slirp->udb.so_next; so != &slirp->udb; | |
657 | so = so_next) { | |
658 | int revents; | |
8917c3bd | 659 | |
1ab67b98 | 660 | so_next = so->so_next; |
cf1d078e | 661 | |
1ab67b98 MAL |
662 | revents = 0; |
663 | if (so->pollfds_idx != -1) { | |
deaeb3f7 | 664 | revents = get_revents(so->pollfds_idx, opaque); |
1ab67b98 | 665 | } |
8917c3bd | 666 | |
1ab67b98 | 667 | if (so->s != -1 && |
deaeb3f7 | 668 | (revents & (SLIRP_POLL_IN | SLIRP_POLL_HUP | SLIRP_POLL_ERR))) { |
1ab67b98 | 669 | sorecvfrom(so); |
cf1d078e | 670 | } |
1ab67b98 | 671 | } |
cf1d078e | 672 | |
1ab67b98 MAL |
673 | /* |
674 | * Check incoming ICMP relies. | |
675 | */ | |
676 | for (so = slirp->icmp.so_next; so != &slirp->icmp; | |
677 | so = so_next) { | |
678 | int revents; | |
8917c3bd | 679 | |
1ab67b98 | 680 | so_next = so->so_next; |
8917c3bd | 681 | |
1ab67b98 MAL |
682 | revents = 0; |
683 | if (so->pollfds_idx != -1) { | |
deaeb3f7 | 684 | revents = get_revents(so->pollfds_idx, opaque); |
1ab67b98 | 685 | } |
cf1d078e | 686 | |
1ab67b98 | 687 | if (so->s != -1 && |
deaeb3f7 | 688 | (revents & (SLIRP_POLL_IN | SLIRP_POLL_HUP | SLIRP_POLL_ERR))) { |
1ab67b98 | 689 | icmp_receive(so); |
cf1d078e SH |
690 | } |
691 | } | |
b1c99fcd | 692 | } |
1ab67b98 MAL |
693 | |
694 | if_start(slirp); | |
f0cbd3ec FB |
695 | } |
696 | ||
460fec67 | 697 | static void arp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) |
f0cbd3ec | 698 | { |
1f8b56e7 | 699 | struct slirp_arphdr *ah = (struct slirp_arphdr *)(pkt + ETH_HLEN); |
893dcdbf | 700 | uint8_t arp_reply[MAX(ETH_HLEN + sizeof(struct slirp_arphdr), 64)]; |
f0cbd3ec | 701 | struct ethhdr *reh = (struct ethhdr *)arp_reply; |
1f8b56e7 | 702 | struct slirp_arphdr *rah = (struct slirp_arphdr *)(arp_reply + ETH_HLEN); |
f0cbd3ec | 703 | int ar_op; |
5d300fc9 | 704 | struct gfwd_list *ex_ptr; |
f0cbd3ec | 705 | |
0b11c036 ST |
706 | if (!slirp->in_enabled) { |
707 | return; | |
708 | } | |
709 | ||
f0cbd3ec FB |
710 | ar_op = ntohs(ah->ar_op); |
711 | switch(ar_op) { | |
712 | case ARPOP_REQUEST: | |
1a0ca1e1 FC |
713 | if (ah->ar_tip == ah->ar_sip) { |
714 | /* Gratuitous ARP */ | |
715 | arp_table_add(slirp, ah->ar_sip, ah->ar_sha); | |
716 | return; | |
717 | } | |
718 | ||
460fec67 JK |
719 | if ((ah->ar_tip & slirp->vnetwork_mask.s_addr) == |
720 | slirp->vnetwork_addr.s_addr) { | |
721 | if (ah->ar_tip == slirp->vnameserver_addr.s_addr || | |
722 | ah->ar_tip == slirp->vhost_addr.s_addr) | |
a3d4af03 | 723 | goto arp_ok; |
ffe02f55 | 724 | /* TODO: IPv6 */ |
5d300fc9 | 725 | for (ex_ptr = slirp->guestfwd_list; ex_ptr; ex_ptr = ex_ptr->ex_next) { |
a13a4126 | 726 | if (ex_ptr->ex_addr.s_addr == ah->ar_tip) |
a3d4af03 FB |
727 | goto arp_ok; |
728 | } | |
729 | return; | |
730 | arp_ok: | |
dbf3c4b4 | 731 | memset(arp_reply, 0, sizeof(arp_reply)); |
1a0ca1e1 FC |
732 | |
733 | arp_table_add(slirp, ah->ar_sip, ah->ar_sha); | |
f0cbd3ec FB |
734 | |
735 | /* ARP request for alias/dns mac address */ | |
736 | memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN); | |
a13a4126 JK |
737 | memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4); |
738 | memcpy(&reh->h_source[2], &ah->ar_tip, 4); | |
f0cbd3ec FB |
739 | reh->h_proto = htons(ETH_P_ARP); |
740 | ||
741 | rah->ar_hrd = htons(1); | |
742 | rah->ar_pro = htons(ETH_P_IP); | |
743 | rah->ar_hln = ETH_ALEN; | |
744 | rah->ar_pln = 4; | |
745 | rah->ar_op = htons(ARPOP_REPLY); | |
746 | memcpy(rah->ar_sha, reh->h_source, ETH_ALEN); | |
a13a4126 | 747 | rah->ar_sip = ah->ar_tip; |
f0cbd3ec | 748 | memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN); |
a13a4126 | 749 | rah->ar_tip = ah->ar_sip; |
625a526b | 750 | slirp_send_packet_all(slirp, arp_reply, sizeof(arp_reply)); |
f0cbd3ec FB |
751 | } |
752 | break; | |
de806f07 | 753 | case ARPOP_REPLY: |
1a0ca1e1 | 754 | arp_table_add(slirp, ah->ar_sip, ah->ar_sha); |
de806f07 | 755 | break; |
f0cbd3ec FB |
756 | default: |
757 | break; | |
758 | } | |
759 | } | |
760 | ||
9f8bd042 | 761 | void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) |
f0cbd3ec FB |
762 | { |
763 | struct mbuf *m; | |
764 | int proto; | |
765 | ||
766 | if (pkt_len < ETH_HLEN) | |
767 | return; | |
3b46e624 | 768 | |
59fa06ac | 769 | proto = (((uint16_t) pkt[12]) << 8) + pkt[13]; |
f0cbd3ec FB |
770 | switch(proto) { |
771 | case ETH_P_ARP: | |
460fec67 | 772 | arp_input(slirp, pkt, pkt_len); |
f0cbd3ec FB |
773 | break; |
774 | case ETH_P_IP: | |
0d6ff71a | 775 | case ETH_P_IPV6: |
460fec67 | 776 | m = m_get(slirp); |
f0cbd3ec FB |
777 | if (!m) |
778 | return; | |
98c63057 GS |
779 | /* Note: we add 2 to align the IP header on 4 bytes, |
780 | * and add the margin for the tcpiphdr overhead */ | |
781 | if (M_FREEROOM(m) < pkt_len + TCPIPHDR_DELTA + 2) { | |
782 | m_inc(m, pkt_len + TCPIPHDR_DELTA + 2); | |
e8e880a7 | 783 | } |
98c63057 GS |
784 | m->m_len = pkt_len + TCPIPHDR_DELTA + 2; |
785 | memcpy(m->m_data + TCPIPHDR_DELTA + 2, pkt, pkt_len); | |
f0cbd3ec | 786 | |
98c63057 GS |
787 | m->m_data += TCPIPHDR_DELTA + 2 + ETH_HLEN; |
788 | m->m_len -= TCPIPHDR_DELTA + 2 + ETH_HLEN; | |
f0cbd3ec | 789 | |
0d6ff71a GS |
790 | if (proto == ETH_P_IP) { |
791 | ip_input(m); | |
792 | } else if (proto == ETH_P_IPV6) { | |
793 | ip6_input(m); | |
794 | } | |
f0cbd3ec | 795 | break; |
0d6ff71a | 796 | |
47bb83ca CLG |
797 | case ETH_P_NCSI: |
798 | ncsi_input(slirp, pkt, pkt_len); | |
799 | break; | |
800 | ||
f0cbd3ec FB |
801 | default: |
802 | break; | |
803 | } | |
804 | } | |
805 | ||
18137fba GS |
806 | /* Prepare the IPv4 packet to be sent to the ethernet device. Returns 1 if no |
807 | * packet should be sent, 0 if the packet must be re-queued, 2 if the packet | |
808 | * is ready to go. | |
1ab74cea | 809 | */ |
18137fba GS |
810 | static int if_encap4(Slirp *slirp, struct mbuf *ifm, struct ethhdr *eh, |
811 | uint8_t ethaddr[ETH_ALEN]) | |
f0cbd3ec | 812 | { |
1ab74cea | 813 | const struct ip *iph = (const struct ip *)ifm->m_data; |
f0cbd3ec | 814 | |
959e4147 ST |
815 | if (iph->ip_dst.s_addr == 0) { |
816 | /* 0.0.0.0 can not be a destination address, something went wrong, | |
817 | * avoid making it worse */ | |
818 | return 1; | |
819 | } | |
1a0ca1e1 | 820 | if (!arp_table_search(slirp, iph->ip_dst.s_addr, ethaddr)) { |
1f8b56e7 | 821 | uint8_t arp_req[ETH_HLEN + sizeof(struct slirp_arphdr)]; |
de806f07 | 822 | struct ethhdr *reh = (struct ethhdr *)arp_req; |
1f8b56e7 | 823 | struct slirp_arphdr *rah = (struct slirp_arphdr *)(arp_req + ETH_HLEN); |
de806f07 | 824 | |
fc3779a1 | 825 | if (!ifm->resolution_requested) { |
1ab74cea FC |
826 | /* If the client addr is not known, send an ARP request */ |
827 | memset(reh->h_dest, 0xff, ETH_ALEN); | |
828 | memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4); | |
829 | memcpy(&reh->h_source[2], &slirp->vhost_addr, 4); | |
830 | reh->h_proto = htons(ETH_P_ARP); | |
831 | rah->ar_hrd = htons(1); | |
832 | rah->ar_pro = htons(ETH_P_IP); | |
833 | rah->ar_hln = ETH_ALEN; | |
834 | rah->ar_pln = 4; | |
835 | rah->ar_op = htons(ARPOP_REQUEST); | |
836 | ||
837 | /* source hw addr */ | |
838 | memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN - 4); | |
839 | memcpy(&rah->ar_sha[2], &slirp->vhost_addr, 4); | |
840 | ||
841 | /* source IP */ | |
842 | rah->ar_sip = slirp->vhost_addr.s_addr; | |
843 | ||
844 | /* target hw addr (none) */ | |
845 | memset(rah->ar_tha, 0, ETH_ALEN); | |
846 | ||
847 | /* target IP */ | |
848 | rah->ar_tip = iph->ip_dst.s_addr; | |
849 | slirp->client_ipaddr = iph->ip_dst; | |
625a526b | 850 | slirp_send_packet_all(slirp, arp_req, sizeof(arp_req)); |
fc3779a1 | 851 | ifm->resolution_requested = true; |
e3a110b5 JK |
852 | |
853 | /* Expire request and drop outgoing packet after 1 second */ | |
3e0fad3a MAL |
854 | ifm->expiration_date = |
855 | slirp->cb->clock_get_ns(slirp->opaque) + 1000000000ULL; | |
1ab74cea FC |
856 | } |
857 | return 0; | |
de806f07 | 858 | } else { |
a13a4126 | 859 | memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 4); |
de806f07 | 860 | /* XXX: not correct */ |
460fec67 | 861 | memcpy(&eh->h_source[2], &slirp->vhost_addr, 4); |
de806f07 | 862 | eh->h_proto = htons(ETH_P_IP); |
18137fba GS |
863 | |
864 | /* Send this */ | |
865 | return 2; | |
866 | } | |
867 | } | |
868 | ||
0d6ff71a GS |
869 | /* Prepare the IPv6 packet to be sent to the ethernet device. Returns 1 if no |
870 | * packet should be sent, 0 if the packet must be re-queued, 2 if the packet | |
871 | * is ready to go. | |
872 | */ | |
873 | static int if_encap6(Slirp *slirp, struct mbuf *ifm, struct ethhdr *eh, | |
874 | uint8_t ethaddr[ETH_ALEN]) | |
875 | { | |
876 | const struct ip6 *ip6h = mtod(ifm, const struct ip6 *); | |
877 | if (!ndp_table_search(slirp, ip6h->ip_dst, ethaddr)) { | |
878 | if (!ifm->resolution_requested) { | |
879 | ndp_send_ns(slirp, ip6h->ip_dst); | |
880 | ifm->resolution_requested = true; | |
3e0fad3a | 881 | ifm->expiration_date = slirp->cb->clock_get_ns(slirp->opaque) + 1000000000ULL; |
0d6ff71a GS |
882 | } |
883 | return 0; | |
884 | } else { | |
885 | eh->h_proto = htons(ETH_P_IPV6); | |
886 | in6_compute_ethaddr(ip6h->ip_src, eh->h_source); | |
887 | ||
888 | /* Send this */ | |
889 | return 2; | |
890 | } | |
891 | } | |
892 | ||
18137fba GS |
893 | /* Output the IP packet to the ethernet device. Returns 0 if the packet must be |
894 | * re-queued. | |
895 | */ | |
896 | int if_encap(Slirp *slirp, struct mbuf *ifm) | |
897 | { | |
898 | uint8_t buf[1600]; | |
899 | struct ethhdr *eh = (struct ethhdr *)buf; | |
900 | uint8_t ethaddr[ETH_ALEN]; | |
901 | const struct ip *iph = (const struct ip *)ifm->m_data; | |
902 | int ret; | |
903 | ||
904 | if (ifm->m_len + ETH_HLEN > sizeof(buf)) { | |
1ab74cea | 905 | return 1; |
de806f07 | 906 | } |
18137fba GS |
907 | |
908 | switch (iph->ip_v) { | |
909 | case IPVERSION: | |
910 | ret = if_encap4(slirp, ifm, eh, ethaddr); | |
911 | if (ret < 2) { | |
912 | return ret; | |
913 | } | |
914 | break; | |
915 | ||
0d6ff71a GS |
916 | case IP6VERSION: |
917 | ret = if_encap6(slirp, ifm, eh, ethaddr); | |
918 | if (ret < 2) { | |
919 | return ret; | |
920 | } | |
921 | break; | |
922 | ||
18137fba | 923 | default: |
0d6ff71a | 924 | g_assert_not_reached(); |
18137fba GS |
925 | break; |
926 | } | |
927 | ||
928 | memcpy(eh->h_dest, ethaddr, ETH_ALEN); | |
a857d91d MAL |
929 | DEBUG_ARG("src = %02x:%02x:%02x:%02x:%02x:%02x", |
930 | eh->h_source[0], eh->h_source[1], eh->h_source[2], | |
931 | eh->h_source[3], eh->h_source[4], eh->h_source[5]); | |
932 | DEBUG_ARG("dst = %02x:%02x:%02x:%02x:%02x:%02x", | |
933 | eh->h_dest[0], eh->h_dest[1], eh->h_dest[2], | |
934 | eh->h_dest[3], eh->h_dest[4], eh->h_dest[5]); | |
18137fba | 935 | memcpy(buf + sizeof(struct ethhdr), ifm->m_data, ifm->m_len); |
625a526b | 936 | slirp_send_packet_all(slirp, buf, ifm->m_len + ETH_HLEN); |
18137fba | 937 | return 1; |
f0cbd3ec | 938 | } |
9bf05444 | 939 | |
9c12a6f2 | 940 | /* Drop host forwarding rule, return 0 if found. */ |
ffe02f55 | 941 | /* TODO: IPv6 */ |
9f8bd042 JK |
942 | int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr, |
943 | int host_port) | |
c1261d8d AG |
944 | { |
945 | struct socket *so; | |
460fec67 | 946 | struct socket *head = (is_udp ? &slirp->udb : &slirp->tcb); |
2ad82cf9 JK |
947 | struct sockaddr_in addr; |
948 | int port = htons(host_port); | |
949 | socklen_t addr_len; | |
c1261d8d | 950 | |
c1261d8d | 951 | for (so = head->so_next; so != head; so = so->so_next) { |
2ad82cf9 | 952 | addr_len = sizeof(addr); |
9c12a6f2 JK |
953 | if ((so->so_state & SS_HOSTFWD) && |
954 | getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 && | |
3c6a0580 | 955 | addr.sin_addr.s_addr == host_addr.s_addr && |
2ad82cf9 | 956 | addr.sin_port == port) { |
3e0fad3a | 957 | so->slirp->cb->unregister_poll_fd(so->s, so->slirp->opaque); |
fdbfba8c | 958 | closesocket(so->s); |
c1261d8d | 959 | sofree(so); |
9c12a6f2 | 960 | return 0; |
c1261d8d AG |
961 | } |
962 | } | |
963 | ||
9c12a6f2 | 964 | return -1; |
c1261d8d AG |
965 | } |
966 | ||
ffe02f55 | 967 | /* TODO: IPv6 */ |
9f8bd042 JK |
968 | int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr, |
969 | int host_port, struct in_addr guest_addr, int guest_port) | |
9bf05444 | 970 | { |
a13a4126 | 971 | if (!guest_addr.s_addr) { |
460fec67 | 972 | guest_addr = slirp->vdhcp_startaddr; |
a13a4126 | 973 | } |
9bf05444 | 974 | if (is_udp) { |
460fec67 JK |
975 | if (!udp_listen(slirp, host_addr.s_addr, htons(host_port), |
976 | guest_addr.s_addr, htons(guest_port), SS_HOSTFWD)) | |
9bf05444 FB |
977 | return -1; |
978 | } else { | |
460fec67 JK |
979 | if (!tcp_listen(slirp, host_addr.s_addr, htons(host_port), |
980 | guest_addr.s_addr, htons(guest_port), SS_HOSTFWD)) | |
9bf05444 FB |
981 | return -1; |
982 | } | |
983 | return 0; | |
984 | } | |
a3d4af03 | 985 | |
ffe02f55 | 986 | /* TODO: IPv6 */ |
4246e8ca MAL |
987 | static bool |
988 | check_guestfwd(Slirp *slirp, struct in_addr *guest_addr, int guest_port) | |
a3d4af03 | 989 | { |
4246e8ca MAL |
990 | struct gfwd_list *tmp_ptr; |
991 | ||
bb53fc53 JK |
992 | if (!guest_addr->s_addr) { |
993 | guest_addr->s_addr = slirp->vnetwork_addr.s_addr | | |
460fec67 | 994 | (htonl(0x0204) & ~slirp->vnetwork_mask.s_addr); |
c92ef6a2 | 995 | } |
bb53fc53 | 996 | if ((guest_addr->s_addr & slirp->vnetwork_mask.s_addr) != |
460fec67 | 997 | slirp->vnetwork_addr.s_addr || |
bb53fc53 JK |
998 | guest_addr->s_addr == slirp->vhost_addr.s_addr || |
999 | guest_addr->s_addr == slirp->vnameserver_addr.s_addr) { | |
4246e8ca MAL |
1000 | return false; |
1001 | } | |
1002 | ||
1003 | /* check if the port is "bound" */ | |
1004 | for (tmp_ptr = slirp->guestfwd_list; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) { | |
1005 | if (guest_port == tmp_ptr->ex_fport && | |
1006 | guest_addr->s_addr == tmp_ptr->ex_addr.s_addr) | |
1007 | return false; | |
1008 | } | |
1009 | ||
1010 | return true; | |
1011 | } | |
1012 | ||
44b4ff24 | 1013 | int slirp_add_exec(Slirp *slirp, const char *cmdline, |
4246e8ca MAL |
1014 | struct in_addr *guest_addr, int guest_port) |
1015 | { | |
1016 | if (!check_guestfwd(slirp, guest_addr, guest_port)) { | |
a13a4126 JK |
1017 | return -1; |
1018 | } | |
3ed9f823 | 1019 | |
44b4ff24 MAL |
1020 | add_exec(&slirp->guestfwd_list, cmdline, *guest_addr, htons(guest_port)); |
1021 | return 0; | |
1022 | } | |
1023 | ||
1024 | int slirp_add_guestfwd(Slirp *slirp, SlirpWriteCb write_cb, void *opaque, | |
1025 | struct in_addr *guest_addr, int guest_port) | |
1026 | { | |
1027 | if (!check_guestfwd(slirp, guest_addr, guest_port)) { | |
1028 | return -1; | |
1029 | } | |
1030 | ||
1031 | add_guestfwd(&slirp->guestfwd_list, write_cb, opaque, | |
1032 | *guest_addr, htons(guest_port)); | |
1033 | return 0; | |
a3d4af03 | 1034 | } |
e1c5a2b3 AL |
1035 | |
1036 | ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags) | |
1037 | { | |
44b4ff24 | 1038 | if (so->s == -1 && so->guestfwd) { |
6ab3fc32 DB |
1039 | /* XXX this blocks entire thread. Rewrite to use |
1040 | * qemu_chr_fe_write and background I/O callbacks */ | |
44b4ff24 | 1041 | so->guestfwd->write_cb(buf, len, so->guestfwd->opaque); |
cf1d078e SH |
1042 | return len; |
1043 | } | |
e1c5a2b3 | 1044 | |
c4186815 PM |
1045 | if (so->s == -1) { |
1046 | /* | |
1047 | * This should in theory not happen but it is hard to be | |
1048 | * sure because some code paths will end up with so->s == -1 | |
1049 | * on a failure but don't dispose of the struct socket. | |
1050 | * Check specifically, so we don't pass -1 to send(). | |
1051 | */ | |
1052 | errno = EBADF; | |
1053 | return -1; | |
1054 | } | |
1055 | ||
cf1d078e | 1056 | return send(so->s, buf, len, flags); |
e1c5a2b3 AL |
1057 | } |
1058 | ||
268c95d7 | 1059 | struct socket * |
460fec67 | 1060 | slirp_find_ctl_socket(Slirp *slirp, struct in_addr guest_addr, int guest_port) |
e1c5a2b3 | 1061 | { |
a13a4126 | 1062 | struct socket *so; |
e1c5a2b3 | 1063 | |
ffe02f55 | 1064 | /* TODO: IPv6 */ |
460fec67 | 1065 | for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) { |
a13a4126 JK |
1066 | if (so->so_faddr.s_addr == guest_addr.s_addr && |
1067 | htons(so->so_fport) == guest_port) { | |
1068 | return so; | |
1069 | } | |
1070 | } | |
1071 | return NULL; | |
e1c5a2b3 AL |
1072 | } |
1073 | ||
9f8bd042 JK |
1074 | size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr, |
1075 | int guest_port) | |
e1c5a2b3 | 1076 | { |
cf1d078e SH |
1077 | struct iovec iov[2]; |
1078 | struct socket *so; | |
e1c5a2b3 | 1079 | |
cf1d078e | 1080 | so = slirp_find_ctl_socket(slirp, guest_addr, guest_port); |
e1c5a2b3 | 1081 | |
cf1d078e SH |
1082 | if (!so || so->so_state & SS_NOFDREF) { |
1083 | return 0; | |
1084 | } | |
e1c5a2b3 | 1085 | |
cf1d078e SH |
1086 | if (!CONN_CANFRCV(so) || so->so_snd.sb_cc >= (so->so_snd.sb_datalen/2)) { |
1087 | return 0; | |
1088 | } | |
e1c5a2b3 | 1089 | |
cf1d078e | 1090 | return sopreprbuf(so, iov, NULL); |
e1c5a2b3 AL |
1091 | } |
1092 | ||
9f8bd042 | 1093 | void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port, |
c92ef6a2 | 1094 | const uint8_t *buf, int size) |
e1c5a2b3 AL |
1095 | { |
1096 | int ret; | |
460fec67 | 1097 | struct socket *so = slirp_find_ctl_socket(slirp, guest_addr, guest_port); |
a13a4126 | 1098 | |
e1c5a2b3 AL |
1099 | if (!so) |
1100 | return; | |
1101 | ||
0580ac91 | 1102 | ret = soreadbuf(so, (const char *)buf, size); |
e1c5a2b3 AL |
1103 | |
1104 | if (ret > 0) | |
1105 | tcp_output(sototcpcb(so)); | |
1106 | } | |
625a526b MAL |
1107 | |
1108 | void slirp_send_packet_all(Slirp *slirp, const void *buf, size_t len) | |
1109 | { | |
1110 | ssize_t ret = slirp->cb->send_packet(buf, len, slirp->opaque); | |
1111 | ||
1112 | if (ret < 0) { | |
1113 | g_critical("Failed to send packet, ret: %ld", (long) ret); | |
1114 | } else if (ret < len) { | |
1115 | DEBUG_ERROR("send_packet() didn't send all data: %ld < %lu", | |
1116 | (long) ret, (unsigned long) len); | |
1117 | } | |
1118 | } |