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