bellard | d75a0b9 | 2008-10-17 17:31:57 +0000 | [diff] [blame] | 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 | */ |
Peter Maydell | 7df7482 | 2016-01-29 17:49:59 +0000 | [diff] [blame] | 24 | #include "qemu/osdep.h" |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 25 | #include "qemu-common.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 26 | #include "qemu/timer.h" |
Guillaume Subiron | eae303f | 2015-12-19 22:24:58 +0100 | [diff] [blame] | 27 | #include "qemu/error-report.h" |
Marc-André Lureau | 4d43a60 | 2017-01-26 18:26:44 +0400 | [diff] [blame] | 28 | #include "chardev/char-fe.h" |
Juan Quintela | f2a8f0a | 2017-04-24 13:42:55 +0200 | [diff] [blame] | 29 | #include "migration/register.h" |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 30 | #include "slirp.h" |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 31 | #include "hw/hw.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 32 | #include "qemu/cutils.h" |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 33 | |
Samuel Thibault | ef763fa | 2016-03-20 15:16:21 +0100 | [diff] [blame] | 34 | #ifndef _WIN32 |
| 35 | #include <net/if.h> |
| 36 | #endif |
| 37 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 38 | /* host loopback address */ |
| 39 | struct in_addr loopback_addr; |
Anders Waldenborg | 648cd33 | 2012-07-13 22:54:17 +0200 | [diff] [blame] | 40 | /* host loopback network mask */ |
Anthony Liguori | 0b8db8f | 2012-08-06 19:31:55 -0500 | [diff] [blame] | 41 | unsigned long loopback_mask; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 42 | |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 43 | /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */ |
Fabien Chouteau | 1a0ca1e | 2011-08-03 12:52:54 +0200 | [diff] [blame] | 44 | static const uint8_t special_ethaddr[ETH_ALEN] = { |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 45 | 0x52, 0x55, 0x00, 0x00, 0x00, 0x00 |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
Jan Kiszka | f1d99bb | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 48 | u_int curtime; |
Jan Kiszka | f1d99bb | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 49 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 50 | static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances = |
| 51 | QTAILQ_HEAD_INITIALIZER(slirp_instances); |
pbrook | 115defd | 2006-04-16 11:06:58 +0000 | [diff] [blame] | 52 | |
Stefan Weil | 9e3a95e | 2009-08-31 16:29:34 +0200 | [diff] [blame] | 53 | static struct in_addr dns_addr; |
Samuel Thibault | ef763fa | 2016-03-20 15:16:21 +0100 | [diff] [blame] | 54 | #ifndef _WIN32 |
Samuel Thibault | 1d17654 | 2016-03-20 15:09:09 +0100 | [diff] [blame] | 55 | static struct in6_addr dns6_addr; |
Samuel Thibault | ef763fa | 2016-03-20 15:16:21 +0100 | [diff] [blame] | 56 | #endif |
Stefan Weil | 9e3a95e | 2009-08-31 16:29:34 +0200 | [diff] [blame] | 57 | static u_int dns_addr_time; |
Samuel Thibault | ef763fa | 2016-03-20 15:16:21 +0100 | [diff] [blame] | 58 | #ifndef _WIN32 |
Samuel Thibault | 1d17654 | 2016-03-20 15:09:09 +0100 | [diff] [blame] | 59 | static u_int dns6_addr_time; |
Samuel Thibault | ef763fa | 2016-03-20 15:16:21 +0100 | [diff] [blame] | 60 | #endif |
Ed Swierk | df7a86e | 2009-08-20 19:00:31 -0700 | [diff] [blame] | 61 | |
Liu Ping Fan | 9b0ca6c | 2013-08-25 10:01:20 +0800 | [diff] [blame] | 62 | #define TIMEOUT_FAST 2 /* milliseconds */ |
| 63 | #define TIMEOUT_SLOW 499 /* milliseconds */ |
| 64 | /* for the aging of certain requests like DNS */ |
| 65 | #define TIMEOUT_DEFAULT 1000 /* milliseconds */ |
| 66 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 67 | #ifdef _WIN32 |
| 68 | |
Ed Swierk | df7a86e | 2009-08-20 19:00:31 -0700 | [diff] [blame] | 69 | int get_dns_addr(struct in_addr *pdns_addr) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 70 | { |
bellard | 379ff53 | 2004-07-12 22:33:07 +0000 | [diff] [blame] | 71 | FIXED_INFO *FixedInfo=NULL; |
| 72 | ULONG BufLen; |
| 73 | DWORD ret; |
| 74 | IP_ADDR_STRING *pIPAddr; |
| 75 | struct in_addr tmp_addr; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 76 | |
Liu Ping Fan | 9b0ca6c | 2013-08-25 10:01:20 +0800 | [diff] [blame] | 77 | if (dns_addr.s_addr != 0 && (curtime - dns_addr_time) < TIMEOUT_DEFAULT) { |
Ed Swierk | df7a86e | 2009-08-20 19:00:31 -0700 | [diff] [blame] | 78 | *pdns_addr = dns_addr; |
| 79 | return 0; |
| 80 | } |
| 81 | |
bellard | 379ff53 | 2004-07-12 22:33:07 +0000 | [diff] [blame] | 82 | FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO)); |
| 83 | BufLen = sizeof(FIXED_INFO); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 84 | |
bellard | 379ff53 | 2004-07-12 22:33:07 +0000 | [diff] [blame] | 85 | if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen)) { |
| 86 | if (FixedInfo) { |
| 87 | GlobalFree(FixedInfo); |
| 88 | FixedInfo = NULL; |
| 89 | } |
| 90 | FixedInfo = GlobalAlloc(GPTR, BufLen); |
| 91 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 92 | |
bellard | 379ff53 | 2004-07-12 22:33:07 +0000 | [diff] [blame] | 93 | if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS) { |
| 94 | printf("GetNetworkParams failed. ret = %08x\n", (u_int)ret ); |
| 95 | if (FixedInfo) { |
| 96 | GlobalFree(FixedInfo); |
| 97 | FixedInfo = NULL; |
| 98 | } |
| 99 | return -1; |
| 100 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 101 | |
bellard | 379ff53 | 2004-07-12 22:33:07 +0000 | [diff] [blame] | 102 | pIPAddr = &(FixedInfo->DnsServerList); |
| 103 | inet_aton(pIPAddr->IpAddress.String, &tmp_addr); |
| 104 | *pdns_addr = tmp_addr; |
Ed Swierk | df7a86e | 2009-08-20 19:00:31 -0700 | [diff] [blame] | 105 | dns_addr = tmp_addr; |
| 106 | dns_addr_time = curtime; |
bellard | 379ff53 | 2004-07-12 22:33:07 +0000 | [diff] [blame] | 107 | if (FixedInfo) { |
| 108 | GlobalFree(FixedInfo); |
| 109 | FixedInfo = NULL; |
| 110 | } |
| 111 | return 0; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Samuel Thibault | ef763fa | 2016-03-20 15:16:21 +0100 | [diff] [blame] | 114 | int get_dns6_addr(struct in6_addr *pdns6_addr, uint32_t *scope_id) |
Samuel Thibault | 1d17654 | 2016-03-20 15:09:09 +0100 | [diff] [blame] | 115 | { |
| 116 | return -1; |
| 117 | } |
| 118 | |
Jan Kiszka | df46189 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 119 | static void winsock_cleanup(void) |
| 120 | { |
| 121 | WSACleanup(); |
| 122 | } |
| 123 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 124 | #else |
| 125 | |
Samuel Thibault | 1d17654 | 2016-03-20 15:09:09 +0100 | [diff] [blame] | 126 | static int get_dns_addr_cached(void *pdns_addr, void *cached_addr, |
| 127 | socklen_t addrlen, |
| 128 | struct stat *cached_stat, u_int *cached_time) |
Samuel Thibault | 972487b | 2016-03-20 13:20:42 +0100 | [diff] [blame] | 129 | { |
| 130 | struct stat old_stat; |
Samuel Thibault | 1d17654 | 2016-03-20 15:09:09 +0100 | [diff] [blame] | 131 | if (curtime - *cached_time < TIMEOUT_DEFAULT) { |
| 132 | memcpy(pdns_addr, cached_addr, addrlen); |
Samuel Thibault | 972487b | 2016-03-20 13:20:42 +0100 | [diff] [blame] | 133 | return 0; |
| 134 | } |
Samuel Thibault | 1d17654 | 2016-03-20 15:09:09 +0100 | [diff] [blame] | 135 | old_stat = *cached_stat; |
| 136 | if (stat("/etc/resolv.conf", cached_stat) != 0) { |
Samuel Thibault | 972487b | 2016-03-20 13:20:42 +0100 | [diff] [blame] | 137 | return -1; |
| 138 | } |
Samuel Thibault | 1d17654 | 2016-03-20 15:09:09 +0100 | [diff] [blame] | 139 | if (cached_stat->st_dev == old_stat.st_dev |
| 140 | && cached_stat->st_ino == old_stat.st_ino |
| 141 | && cached_stat->st_size == old_stat.st_size |
| 142 | && cached_stat->st_mtime == old_stat.st_mtime) { |
| 143 | memcpy(pdns_addr, cached_addr, addrlen); |
Samuel Thibault | 972487b | 2016-03-20 13:20:42 +0100 | [diff] [blame] | 144 | return 0; |
| 145 | } |
| 146 | return 1; |
| 147 | } |
| 148 | |
Samuel Thibault | 1d17654 | 2016-03-20 15:09:09 +0100 | [diff] [blame] | 149 | static int get_dns_addr_resolv_conf(int af, void *pdns_addr, void *cached_addr, |
Samuel Thibault | ef763fa | 2016-03-20 15:16:21 +0100 | [diff] [blame] | 150 | socklen_t addrlen, uint32_t *scope_id, |
| 151 | u_int *cached_time) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 152 | { |
| 153 | char buff[512]; |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 154 | char buff2[257]; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 155 | FILE *f; |
| 156 | int found = 0; |
Samuel Thibault | 1d17654 | 2016-03-20 15:09:09 +0100 | [diff] [blame] | 157 | void *tmp_addr = alloca(addrlen); |
Samuel Thibault | ef763fa | 2016-03-20 15:16:21 +0100 | [diff] [blame] | 158 | unsigned if_index; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 159 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 160 | f = fopen("/etc/resolv.conf", "r"); |
| 161 | if (!f) |
| 162 | return -1; |
| 163 | |
blueswir1 | 31a60e2 | 2007-10-26 18:42:59 +0000 | [diff] [blame] | 164 | #ifdef DEBUG |
Cole Robinson | 02d1608 | 2014-03-21 19:42:20 -0400 | [diff] [blame] | 165 | fprintf(stderr, "IP address of your DNS(s): "); |
blueswir1 | 31a60e2 | 2007-10-26 18:42:59 +0000 | [diff] [blame] | 166 | #endif |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 167 | while (fgets(buff, 512, f) != NULL) { |
| 168 | if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) { |
Samuel Thibault | ef763fa | 2016-03-20 15:16:21 +0100 | [diff] [blame] | 169 | char *c = strchr(buff2, '%'); |
| 170 | if (c) { |
| 171 | if_index = if_nametoindex(c + 1); |
| 172 | *c = '\0'; |
| 173 | } else { |
| 174 | if_index = 0; |
| 175 | } |
| 176 | |
Samuel Thibault | 1d17654 | 2016-03-20 15:09:09 +0100 | [diff] [blame] | 177 | if (!inet_pton(af, buff2, tmp_addr)) { |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 178 | continue; |
Samuel Thibault | 1d17654 | 2016-03-20 15:09:09 +0100 | [diff] [blame] | 179 | } |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 180 | /* If it's the first one, set it to dns_addr */ |
Ed Swierk | df7a86e | 2009-08-20 19:00:31 -0700 | [diff] [blame] | 181 | if (!found) { |
Samuel Thibault | 1d17654 | 2016-03-20 15:09:09 +0100 | [diff] [blame] | 182 | memcpy(pdns_addr, tmp_addr, addrlen); |
| 183 | memcpy(cached_addr, tmp_addr, addrlen); |
Samuel Thibault | ef763fa | 2016-03-20 15:16:21 +0100 | [diff] [blame] | 184 | if (scope_id) { |
| 185 | *scope_id = if_index; |
| 186 | } |
Samuel Thibault | 1d17654 | 2016-03-20 15:09:09 +0100 | [diff] [blame] | 187 | *cached_time = curtime; |
Ed Swierk | df7a86e | 2009-08-20 19:00:31 -0700 | [diff] [blame] | 188 | } |
blueswir1 | 31a60e2 | 2007-10-26 18:42:59 +0000 | [diff] [blame] | 189 | #ifdef DEBUG |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 190 | else |
Cole Robinson | 02d1608 | 2014-03-21 19:42:20 -0400 | [diff] [blame] | 191 | fprintf(stderr, ", "); |
blueswir1 | 31a60e2 | 2007-10-26 18:42:59 +0000 | [diff] [blame] | 192 | #endif |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 193 | if (++found > 3) { |
blueswir1 | 31a60e2 | 2007-10-26 18:42:59 +0000 | [diff] [blame] | 194 | #ifdef DEBUG |
Cole Robinson | 02d1608 | 2014-03-21 19:42:20 -0400 | [diff] [blame] | 195 | fprintf(stderr, "(more)"); |
blueswir1 | 31a60e2 | 2007-10-26 18:42:59 +0000 | [diff] [blame] | 196 | #endif |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 197 | break; |
blueswir1 | 31a60e2 | 2007-10-26 18:42:59 +0000 | [diff] [blame] | 198 | } |
| 199 | #ifdef DEBUG |
Samuel Thibault | 1d17654 | 2016-03-20 15:09:09 +0100 | [diff] [blame] | 200 | else { |
| 201 | char s[INET6_ADDRSTRLEN]; |
Laurent Vivier | 51149a2 | 2017-03-27 10:46:12 +0200 | [diff] [blame] | 202 | const char *res = inet_ntop(af, tmp_addr, s, sizeof(s)); |
Samuel Thibault | 1d17654 | 2016-03-20 15:09:09 +0100 | [diff] [blame] | 203 | if (!res) { |
| 204 | res = "(string conversion error)"; |
| 205 | } |
| 206 | fprintf(stderr, "%s", res); |
| 207 | } |
blueswir1 | 31a60e2 | 2007-10-26 18:42:59 +0000 | [diff] [blame] | 208 | #endif |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
bellard | 1d43a71 | 2004-07-05 21:18:42 +0000 | [diff] [blame] | 211 | fclose(f); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 212 | if (!found) |
| 213 | return -1; |
| 214 | return 0; |
| 215 | } |
| 216 | |
Samuel Thibault | 972487b | 2016-03-20 13:20:42 +0100 | [diff] [blame] | 217 | int get_dns_addr(struct in_addr *pdns_addr) |
| 218 | { |
Samuel Thibault | 1d17654 | 2016-03-20 15:09:09 +0100 | [diff] [blame] | 219 | static struct stat dns_addr_stat; |
| 220 | |
Samuel Thibault | 972487b | 2016-03-20 13:20:42 +0100 | [diff] [blame] | 221 | if (dns_addr.s_addr != 0) { |
| 222 | int ret; |
Samuel Thibault | 1d17654 | 2016-03-20 15:09:09 +0100 | [diff] [blame] | 223 | ret = get_dns_addr_cached(pdns_addr, &dns_addr, sizeof(dns_addr), |
| 224 | &dns_addr_stat, &dns_addr_time); |
Samuel Thibault | 972487b | 2016-03-20 13:20:42 +0100 | [diff] [blame] | 225 | if (ret <= 0) { |
| 226 | return ret; |
| 227 | } |
| 228 | } |
Samuel Thibault | 1d17654 | 2016-03-20 15:09:09 +0100 | [diff] [blame] | 229 | return get_dns_addr_resolv_conf(AF_INET, pdns_addr, &dns_addr, |
Samuel Thibault | ef763fa | 2016-03-20 15:16:21 +0100 | [diff] [blame] | 230 | sizeof(dns_addr), NULL, &dns_addr_time); |
Samuel Thibault | 1d17654 | 2016-03-20 15:09:09 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Samuel Thibault | ef763fa | 2016-03-20 15:16:21 +0100 | [diff] [blame] | 233 | int get_dns6_addr(struct in6_addr *pdns6_addr, uint32_t *scope_id) |
Samuel Thibault | 1d17654 | 2016-03-20 15:09:09 +0100 | [diff] [blame] | 234 | { |
| 235 | static struct stat dns6_addr_stat; |
| 236 | |
| 237 | if (!in6_zero(&dns6_addr)) { |
| 238 | int ret; |
| 239 | ret = get_dns_addr_cached(pdns6_addr, &dns6_addr, sizeof(dns6_addr), |
| 240 | &dns6_addr_stat, &dns6_addr_time); |
| 241 | if (ret <= 0) { |
| 242 | return ret; |
| 243 | } |
| 244 | } |
| 245 | return get_dns_addr_resolv_conf(AF_INET6, pdns6_addr, &dns6_addr, |
Samuel Thibault | ef763fa | 2016-03-20 15:16:21 +0100 | [diff] [blame] | 246 | sizeof(dns6_addr), |
| 247 | scope_id, &dns6_addr_time); |
Samuel Thibault | 972487b | 2016-03-20 13:20:42 +0100 | [diff] [blame] | 248 | } |
| 249 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 250 | #endif |
| 251 | |
Jan Kiszka | df46189 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 252 | static void slirp_init_once(void) |
bellard | 379ff53 | 2004-07-12 22:33:07 +0000 | [diff] [blame] | 253 | { |
Jan Kiszka | df46189 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 254 | static int initialized; |
Jan Kiszka | df46189 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 255 | #ifdef _WIN32 |
| 256 | WSADATA Data; |
bellard | 379ff53 | 2004-07-12 22:33:07 +0000 | [diff] [blame] | 257 | #endif |
| 258 | |
Jan Kiszka | df46189 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 259 | if (initialized) { |
| 260 | return; |
| 261 | } |
| 262 | initialized = 1; |
| 263 | |
| 264 | #ifdef _WIN32 |
| 265 | WSAStartup(MAKEWORD(2,0), &Data); |
| 266 | atexit(winsock_cleanup); |
| 267 | #endif |
| 268 | |
| 269 | loopback_addr.s_addr = htonl(INADDR_LOOPBACK); |
Anders Waldenborg | 648cd33 | 2012-07-13 22:54:17 +0200 | [diff] [blame] | 270 | loopback_mask = htonl(IN_CLASSA_NET); |
Jan Kiszka | df46189 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 271 | } |
| 272 | |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 273 | static void slirp_state_save(QEMUFile *f, void *opaque); |
| 274 | static int slirp_state_load(QEMUFile *f, void *opaque, int version_id); |
| 275 | |
Laurent Vivier | 1b6e748 | 2017-05-24 14:10:48 +0200 | [diff] [blame] | 276 | static SaveVMHandlers savevm_slirp_state = { |
| 277 | .save_state = slirp_state_save, |
| 278 | .load_state = slirp_state_load, |
| 279 | }; |
| 280 | |
Samuel Thibault | 0b11c03 | 2016-03-20 12:29:54 +0100 | [diff] [blame] | 281 | Slirp *slirp_init(int restricted, bool in_enabled, struct in_addr vnetwork, |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 282 | struct in_addr vnetmask, struct in_addr vhost, |
Samuel Thibault | 0b11c03 | 2016-03-20 12:29:54 +0100 | [diff] [blame] | 283 | bool in6_enabled, |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 284 | struct in6_addr vprefix_addr6, uint8_t vprefix_len, |
| 285 | struct in6_addr vhost6, const char *vhostname, |
| 286 | const char *tftp_path, const char *bootfile, |
| 287 | struct in_addr vdhcp_start, struct in_addr vnameserver, |
| 288 | struct in6_addr vnameserver6, const char **vdnssearch, |
Klaus Stengel | 63d2960 | 2012-10-27 19:53:39 +0200 | [diff] [blame] | 289 | void *opaque) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 290 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 291 | Slirp *slirp = g_malloc0(sizeof(Slirp)); |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 292 | |
Jan Kiszka | df46189 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 293 | slirp_init_once(); |
bellard | 379ff53 | 2004-07-12 22:33:07 +0000 | [diff] [blame] | 294 | |
Guillaume Subiron | 0d6ff71 | 2016-03-15 10:31:19 +0100 | [diff] [blame] | 295 | slirp->grand = g_rand_new(); |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 296 | slirp->restricted = restricted; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 297 | |
Samuel Thibault | 0b11c03 | 2016-03-20 12:29:54 +0100 | [diff] [blame] | 298 | slirp->in_enabled = in_enabled; |
| 299 | slirp->in6_enabled = in6_enabled; |
| 300 | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 301 | if_init(slirp); |
| 302 | ip_init(slirp); |
Guillaume Subiron | 0d6ff71 | 2016-03-15 10:31:19 +0100 | [diff] [blame] | 303 | ip6_init(slirp); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 304 | |
| 305 | /* Initialise mbufs *after* setting the MTU */ |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 306 | m_init(slirp); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 307 | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 308 | slirp->vnetwork_addr = vnetwork; |
| 309 | slirp->vnetwork_mask = vnetmask; |
| 310 | slirp->vhost_addr = vhost; |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 311 | slirp->vprefix_addr6 = vprefix_addr6; |
| 312 | slirp->vprefix_len = vprefix_len; |
| 313 | slirp->vhost_addr6 = vhost6; |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 314 | if (vhostname) { |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 315 | pstrcpy(slirp->client_hostname, sizeof(slirp->client_hostname), |
| 316 | vhostname); |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 317 | } |
Markus Armbruster | c64f50d | 2013-01-22 11:07:57 +0100 | [diff] [blame] | 318 | slirp->tftp_prefix = g_strdup(tftp_path); |
| 319 | slirp->bootp_filename = g_strdup(bootfile); |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 320 | slirp->vdhcp_startaddr = vdhcp_start; |
| 321 | slirp->vnameserver_addr = vnameserver; |
Yann Bordenave | 7aac531 | 2016-03-15 10:31:22 +0100 | [diff] [blame] | 322 | slirp->vnameserver_addr6 = vnameserver6; |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 323 | |
Klaus Stengel | 63d2960 | 2012-10-27 19:53:39 +0200 | [diff] [blame] | 324 | if (vdnssearch) { |
| 325 | translate_dnssearch(slirp, vdnssearch); |
| 326 | } |
| 327 | |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 328 | slirp->opaque = opaque; |
| 329 | |
Laurent Vivier | 1b6e748 | 2017-05-24 14:10:48 +0200 | [diff] [blame] | 330 | register_savevm_live(NULL, "slirp", 0, 4, &savevm_slirp_state, slirp); |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 331 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 332 | QTAILQ_INSERT_TAIL(&slirp_instances, slirp, entry); |
Jan Kiszka | ad0d8c4 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 333 | |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 334 | return slirp; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Jan Kiszka | ad0d8c4 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 337 | void slirp_cleanup(Slirp *slirp) |
| 338 | { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 339 | QTAILQ_REMOVE(&slirp_instances, slirp, entry); |
Jan Kiszka | b1c99fc | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 340 | |
Alex Williamson | 0be71e3 | 2010-06-25 11:09:07 -0600 | [diff] [blame] | 341 | unregister_savevm(NULL, "slirp", slirp); |
Jan Kiszka | ad0d8c4 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 342 | |
Jan Kiszka | a68adc2 | 2012-02-29 19:14:23 +0100 | [diff] [blame] | 343 | ip_cleanup(slirp); |
Guillaume Subiron | 0d6ff71 | 2016-03-15 10:31:19 +0100 | [diff] [blame] | 344 | ip6_cleanup(slirp); |
Jan Kiszka | a68adc2 | 2012-02-29 19:14:23 +0100 | [diff] [blame] | 345 | m_cleanup(slirp); |
| 346 | |
Guillaume Subiron | 0d6ff71 | 2016-03-15 10:31:19 +0100 | [diff] [blame] | 347 | g_rand_free(slirp->grand); |
| 348 | |
Klaus Stengel | 63d2960 | 2012-10-27 19:53:39 +0200 | [diff] [blame] | 349 | g_free(slirp->vdnssearch); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 350 | g_free(slirp->tftp_prefix); |
| 351 | g_free(slirp->bootp_filename); |
| 352 | g_free(slirp); |
Jan Kiszka | ad0d8c4 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 353 | } |
| 354 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 355 | #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED) |
| 356 | #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 357 | |
Liu Ping Fan | a42e9c4 | 2013-08-25 10:01:21 +0800 | [diff] [blame] | 358 | static void slirp_update_timeout(uint32_t *timeout) |
Stefano Stabellini | 7c7db75 | 2012-04-13 19:35:04 +0100 | [diff] [blame] | 359 | { |
Liu Ping Fan | a42e9c4 | 2013-08-25 10:01:21 +0800 | [diff] [blame] | 360 | Slirp *slirp; |
| 361 | uint32_t t; |
| 362 | |
| 363 | if (*timeout <= TIMEOUT_FAST) { |
| 364 | return; |
Stefano Stabellini | 7c7db75 | 2012-04-13 19:35:04 +0100 | [diff] [blame] | 365 | } |
Jan Kiszka | 426e3e6 | 2013-08-28 19:12:15 +0200 | [diff] [blame] | 366 | |
| 367 | t = MIN(1000, *timeout); |
Liu Ping Fan | a42e9c4 | 2013-08-25 10:01:21 +0800 | [diff] [blame] | 368 | |
| 369 | /* If we have tcp timeout with slirp, then we will fill @timeout with |
| 370 | * more precise value. |
| 371 | */ |
| 372 | QTAILQ_FOREACH(slirp, &slirp_instances, entry) { |
| 373 | if (slirp->time_fasttimo) { |
| 374 | *timeout = TIMEOUT_FAST; |
| 375 | return; |
| 376 | } |
| 377 | if (slirp->do_slowtimo) { |
| 378 | t = MIN(TIMEOUT_SLOW, t); |
| 379 | } |
| 380 | } |
| 381 | *timeout = t; |
Stefano Stabellini | 7c7db75 | 2012-04-13 19:35:04 +0100 | [diff] [blame] | 382 | } |
| 383 | |
Liu Ping Fan | a42e9c4 | 2013-08-25 10:01:21 +0800 | [diff] [blame] | 384 | void slirp_pollfds_fill(GArray *pollfds, uint32_t *timeout) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 385 | { |
Jan Kiszka | b1c99fc | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 386 | Slirp *slirp; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 387 | struct socket *so, *so_next; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 388 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 389 | if (QTAILQ_EMPTY(&slirp_instances)) { |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 390 | return; |
| 391 | } |
| 392 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 393 | /* |
| 394 | * First, TCP sockets |
| 395 | */ |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 396 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 397 | QTAILQ_FOREACH(slirp, &slirp_instances, entry) { |
| 398 | /* |
| 399 | * *_slowtimo needs calling if there are IP fragments |
| 400 | * in the fragment queue, or there are TCP connections active |
| 401 | */ |
Liu Ping Fan | fe0ff43 | 2013-08-25 10:01:19 +0800 | [diff] [blame] | 402 | slirp->do_slowtimo = ((slirp->tcb.so_next != &slirp->tcb) || |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 403 | (&slirp->ipq.ip_link != slirp->ipq.ip_link.next)); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 404 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 405 | for (so = slirp->tcb.so_next; so != &slirp->tcb; |
| 406 | so = so_next) { |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 407 | int events = 0; |
| 408 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 409 | so_next = so->so_next; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 410 | |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 411 | so->pollfds_idx = -1; |
| 412 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 413 | /* |
| 414 | * See if we need a tcp_fasttimo |
| 415 | */ |
Liu Ping Fan | fe0ff43 | 2013-08-25 10:01:19 +0800 | [diff] [blame] | 416 | if (slirp->time_fasttimo == 0 && |
| 417 | so->so_tcpcb->t_flags & TF_DELACK) { |
| 418 | slirp->time_fasttimo = curtime; /* Flag when want a fasttimo */ |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 419 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 420 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 421 | /* |
| 422 | * NOFDREF can include still connecting to local-host, |
| 423 | * newly socreated() sockets etc. Don't want to select these. |
| 424 | */ |
| 425 | if (so->so_state & SS_NOFDREF || so->s == -1) { |
| 426 | continue; |
| 427 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 428 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 429 | /* |
| 430 | * Set for reading sockets which are accepting |
| 431 | */ |
| 432 | if (so->so_state & SS_FACCEPTCONN) { |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 433 | GPollFD pfd = { |
| 434 | .fd = so->s, |
| 435 | .events = G_IO_IN | G_IO_HUP | G_IO_ERR, |
| 436 | }; |
| 437 | so->pollfds_idx = pollfds->len; |
| 438 | g_array_append_val(pollfds, pfd); |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 439 | continue; |
| 440 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 441 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 442 | /* |
| 443 | * Set for writing sockets which are connecting |
| 444 | */ |
| 445 | if (so->so_state & SS_ISFCONNECTING) { |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 446 | GPollFD pfd = { |
| 447 | .fd = so->s, |
| 448 | .events = G_IO_OUT | G_IO_ERR, |
| 449 | }; |
| 450 | so->pollfds_idx = pollfds->len; |
| 451 | g_array_append_val(pollfds, pfd); |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 452 | continue; |
| 453 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 454 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 455 | /* |
| 456 | * Set for writing if we are connected, can send more, and |
| 457 | * we have something to send |
| 458 | */ |
| 459 | if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) { |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 460 | events |= G_IO_OUT | G_IO_ERR; |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 461 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 462 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 463 | /* |
| 464 | * Set for reading (and urgent data) if we are connected, can |
| 465 | * receive more, and we have room for it XXX /2 ? |
| 466 | */ |
| 467 | if (CONN_CANFRCV(so) && |
| 468 | (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) { |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 469 | events |= G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI; |
| 470 | } |
| 471 | |
| 472 | if (events) { |
| 473 | GPollFD pfd = { |
| 474 | .fd = so->s, |
| 475 | .events = events, |
| 476 | }; |
| 477 | so->pollfds_idx = pollfds->len; |
| 478 | g_array_append_val(pollfds, pfd); |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 479 | } |
| 480 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 481 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 482 | /* |
| 483 | * UDP sockets |
| 484 | */ |
| 485 | for (so = slirp->udb.so_next; so != &slirp->udb; |
| 486 | so = so_next) { |
| 487 | so_next = so->so_next; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 488 | |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 489 | so->pollfds_idx = -1; |
| 490 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 491 | /* |
| 492 | * See if it's timed out |
| 493 | */ |
| 494 | if (so->so_expire) { |
| 495 | if (so->so_expire <= curtime) { |
| 496 | udp_detach(so); |
| 497 | continue; |
| 498 | } else { |
Liu Ping Fan | fe0ff43 | 2013-08-25 10:01:19 +0800 | [diff] [blame] | 499 | slirp->do_slowtimo = true; /* Let socket expire */ |
Jan Kiszka | e6d43cf | 2011-07-20 12:20:18 +0200 | [diff] [blame] | 500 | } |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 501 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 502 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 503 | /* |
| 504 | * When UDP packets are received from over the |
| 505 | * link, they're sendto()'d straight away, so |
| 506 | * no need for setting for writing |
| 507 | * Limit the number of packets queued by this session |
| 508 | * to 4. Note that even though we try and limit this |
| 509 | * to 4 packets, the session could have more queued |
| 510 | * if the packets needed to be fragmented |
| 511 | * (XXX <= 4 ?) |
| 512 | */ |
| 513 | if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) { |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 514 | GPollFD pfd = { |
| 515 | .fd = so->s, |
| 516 | .events = G_IO_IN | G_IO_HUP | G_IO_ERR, |
| 517 | }; |
| 518 | so->pollfds_idx = pollfds->len; |
| 519 | g_array_append_val(pollfds, pfd); |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
| 523 | /* |
| 524 | * ICMP sockets |
| 525 | */ |
| 526 | for (so = slirp->icmp.so_next; so != &slirp->icmp; |
| 527 | so = so_next) { |
| 528 | so_next = so->so_next; |
| 529 | |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 530 | so->pollfds_idx = -1; |
| 531 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 532 | /* |
| 533 | * See if it's timed out |
| 534 | */ |
| 535 | if (so->so_expire) { |
| 536 | if (so->so_expire <= curtime) { |
| 537 | icmp_detach(so); |
| 538 | continue; |
| 539 | } else { |
Liu Ping Fan | fe0ff43 | 2013-08-25 10:01:19 +0800 | [diff] [blame] | 540 | slirp->do_slowtimo = true; /* Let socket expire */ |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
| 544 | if (so->so_state & SS_ISFCONNECTED) { |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 545 | GPollFD pfd = { |
| 546 | .fd = so->s, |
| 547 | .events = G_IO_IN | G_IO_HUP | G_IO_ERR, |
| 548 | }; |
| 549 | so->pollfds_idx = pollfds->len; |
| 550 | g_array_append_val(pollfds, pfd); |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 551 | } |
| 552 | } |
| 553 | } |
Liu Ping Fan | a42e9c4 | 2013-08-25 10:01:21 +0800 | [diff] [blame] | 554 | slirp_update_timeout(timeout); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 555 | } |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 556 | |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 557 | void slirp_pollfds_poll(GArray *pollfds, int select_error) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 558 | { |
Jan Kiszka | b1c99fc | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 559 | Slirp *slirp; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 560 | struct socket *so, *so_next; |
| 561 | int ret; |
| 562 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 563 | if (QTAILQ_EMPTY(&slirp_instances)) { |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 564 | return; |
| 565 | } |
| 566 | |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 567 | curtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 568 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 569 | QTAILQ_FOREACH(slirp, &slirp_instances, entry) { |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 570 | /* |
| 571 | * See if anything has timed out |
| 572 | */ |
Liu Ping Fan | 9b0ca6c | 2013-08-25 10:01:20 +0800 | [diff] [blame] | 573 | if (slirp->time_fasttimo && |
| 574 | ((curtime - slirp->time_fasttimo) >= TIMEOUT_FAST)) { |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 575 | tcp_fasttimo(slirp); |
Liu Ping Fan | fe0ff43 | 2013-08-25 10:01:19 +0800 | [diff] [blame] | 576 | slirp->time_fasttimo = 0; |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 577 | } |
Liu Ping Fan | 9b0ca6c | 2013-08-25 10:01:20 +0800 | [diff] [blame] | 578 | if (slirp->do_slowtimo && |
| 579 | ((curtime - slirp->last_slowtimo) >= TIMEOUT_SLOW)) { |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 580 | ip_slowtimo(slirp); |
| 581 | tcp_slowtimo(slirp); |
Liu Ping Fan | fe0ff43 | 2013-08-25 10:01:19 +0800 | [diff] [blame] | 582 | slirp->last_slowtimo = curtime; |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 583 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 584 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 585 | /* |
| 586 | * Check sockets |
| 587 | */ |
| 588 | if (!select_error) { |
| 589 | /* |
| 590 | * Check TCP sockets |
| 591 | */ |
| 592 | for (so = slirp->tcb.so_next; so != &slirp->tcb; |
| 593 | so = so_next) { |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 594 | int revents; |
| 595 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 596 | so_next = so->so_next; |
Jan Kiszka | e6d43cf | 2011-07-20 12:20:18 +0200 | [diff] [blame] | 597 | |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 598 | revents = 0; |
| 599 | if (so->pollfds_idx != -1) { |
| 600 | revents = g_array_index(pollfds, GPollFD, |
| 601 | so->pollfds_idx).revents; |
| 602 | } |
| 603 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 604 | if (so->so_state & SS_NOFDREF || so->s == -1) { |
| 605 | continue; |
| 606 | } |
Jan Kiszka | e6d43cf | 2011-07-20 12:20:18 +0200 | [diff] [blame] | 607 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 608 | /* |
| 609 | * Check for URG data |
| 610 | * This will soread as well, so no need to |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 611 | * test for G_IO_IN below if this succeeds |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 612 | */ |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 613 | if (revents & G_IO_PRI) { |
Steven Luo | bfb1ac1 | 2016-04-06 22:04:32 -0700 | [diff] [blame] | 614 | ret = sorecvoob(so); |
| 615 | if (ret < 0) { |
| 616 | /* Socket error might have resulted in the socket being |
| 617 | * removed, do not try to do anything more with it. */ |
| 618 | continue; |
| 619 | } |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 620 | } |
| 621 | /* |
| 622 | * Check sockets for reading |
| 623 | */ |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 624 | else if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) { |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 625 | /* |
| 626 | * Check for incoming connections |
| 627 | */ |
| 628 | if (so->so_state & SS_FACCEPTCONN) { |
| 629 | tcp_connect(so); |
| 630 | continue; |
| 631 | } /* else */ |
| 632 | ret = soread(so); |
| 633 | |
| 634 | /* Output it if we read something */ |
| 635 | if (ret > 0) { |
| 636 | tcp_output(sototcpcb(so)); |
Jan Kiszka | e6d43cf | 2011-07-20 12:20:18 +0200 | [diff] [blame] | 637 | } |
Steven Luo | bfb1ac1 | 2016-04-06 22:04:32 -0700 | [diff] [blame] | 638 | if (ret < 0) { |
| 639 | /* Socket error might have resulted in the socket being |
| 640 | * removed, do not try to do anything more with it. */ |
| 641 | continue; |
| 642 | } |
Jan Kiszka | e6d43cf | 2011-07-20 12:20:18 +0200 | [diff] [blame] | 643 | } |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 644 | |
| 645 | /* |
| 646 | * Check sockets for writing |
| 647 | */ |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 648 | if (!(so->so_state & SS_NOFDREF) && |
| 649 | (revents & (G_IO_OUT | G_IO_ERR))) { |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 650 | /* |
| 651 | * Check for non-blocking, still-connecting sockets |
| 652 | */ |
| 653 | if (so->so_state & SS_ISFCONNECTING) { |
| 654 | /* Connected */ |
| 655 | so->so_state &= ~SS_ISFCONNECTING; |
| 656 | |
| 657 | ret = send(so->s, (const void *) &ret, 0, 0); |
| 658 | if (ret < 0) { |
| 659 | /* XXXXX Must fix, zero bytes is a NOP */ |
| 660 | if (errno == EAGAIN || errno == EWOULDBLOCK || |
| 661 | errno == EINPROGRESS || errno == ENOTCONN) { |
| 662 | continue; |
| 663 | } |
| 664 | |
| 665 | /* else failed */ |
| 666 | so->so_state &= SS_PERSISTENT_MASK; |
| 667 | so->so_state |= SS_NOFDREF; |
| 668 | } |
| 669 | /* else so->so_state &= ~SS_ISFCONNECTING; */ |
| 670 | |
| 671 | /* |
| 672 | * Continue tcp_input |
| 673 | */ |
Guillaume Subiron | 9dfbf25 | 2016-03-15 10:31:21 +0100 | [diff] [blame] | 674 | tcp_input((struct mbuf *)NULL, sizeof(struct ip), so, |
| 675 | so->so_ffamily); |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 676 | /* continue; */ |
| 677 | } else { |
| 678 | ret = sowrite(so); |
| 679 | } |
| 680 | /* |
| 681 | * XXXXX If we wrote something (a lot), there |
| 682 | * could be a need for a window update. |
| 683 | * In the worst case, the remote will send |
| 684 | * a window probe to get things going again |
| 685 | */ |
| 686 | } |
| 687 | |
| 688 | /* |
| 689 | * Probe a still-connecting, non-blocking socket |
| 690 | * to check if it's still alive |
| 691 | */ |
| 692 | #ifdef PROBE_CONN |
| 693 | if (so->so_state & SS_ISFCONNECTING) { |
| 694 | ret = qemu_recv(so->s, &ret, 0, 0); |
| 695 | |
| 696 | if (ret < 0) { |
| 697 | /* XXX */ |
| 698 | if (errno == EAGAIN || errno == EWOULDBLOCK || |
| 699 | errno == EINPROGRESS || errno == ENOTCONN) { |
| 700 | continue; /* Still connecting, continue */ |
| 701 | } |
| 702 | |
| 703 | /* else failed */ |
| 704 | so->so_state &= SS_PERSISTENT_MASK; |
| 705 | so->so_state |= SS_NOFDREF; |
| 706 | |
| 707 | /* tcp_input will take care of it */ |
| 708 | } else { |
| 709 | ret = send(so->s, &ret, 0, 0); |
| 710 | if (ret < 0) { |
| 711 | /* XXX */ |
| 712 | if (errno == EAGAIN || errno == EWOULDBLOCK || |
| 713 | errno == EINPROGRESS || errno == ENOTCONN) { |
| 714 | continue; |
| 715 | } |
| 716 | /* else failed */ |
| 717 | so->so_state &= SS_PERSISTENT_MASK; |
| 718 | so->so_state |= SS_NOFDREF; |
| 719 | } else { |
| 720 | so->so_state &= ~SS_ISFCONNECTING; |
| 721 | } |
| 722 | |
| 723 | } |
Guillaume Subiron | 9dfbf25 | 2016-03-15 10:31:21 +0100 | [diff] [blame] | 724 | tcp_input((struct mbuf *)NULL, sizeof(struct ip), so, |
| 725 | so->so_ffamily); |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 726 | } /* SS_ISFCONNECTING */ |
| 727 | #endif |
| 728 | } |
| 729 | |
| 730 | /* |
| 731 | * Now UDP sockets. |
| 732 | * Incoming packets are sent straight away, they're not buffered. |
| 733 | * Incoming UDP data isn't buffered either. |
| 734 | */ |
| 735 | for (so = slirp->udb.so_next; so != &slirp->udb; |
| 736 | so = so_next) { |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 737 | int revents; |
| 738 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 739 | so_next = so->so_next; |
| 740 | |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 741 | revents = 0; |
| 742 | if (so->pollfds_idx != -1) { |
| 743 | revents = g_array_index(pollfds, GPollFD, |
| 744 | so->pollfds_idx).revents; |
| 745 | } |
| 746 | |
| 747 | if (so->s != -1 && |
| 748 | (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) { |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 749 | sorecvfrom(so); |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | /* |
| 754 | * Check incoming ICMP relies. |
| 755 | */ |
| 756 | for (so = slirp->icmp.so_next; so != &slirp->icmp; |
| 757 | so = so_next) { |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 758 | int revents; |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 759 | |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 760 | so_next = so->so_next; |
| 761 | |
| 762 | revents = 0; |
| 763 | if (so->pollfds_idx != -1) { |
| 764 | revents = g_array_index(pollfds, GPollFD, |
| 765 | so->pollfds_idx).revents; |
| 766 | } |
| 767 | |
| 768 | if (so->s != -1 && |
| 769 | (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) { |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 770 | icmp_receive(so); |
| 771 | } |
| 772 | } |
| 773 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 774 | |
Jan Kiszka | f373431 | 2012-03-06 00:02:23 +0100 | [diff] [blame] | 775 | if_start(slirp); |
Jan Kiszka | b1c99fc | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 776 | } |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 779 | static void arp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 780 | { |
Thomas Huth | 1f8b56e | 2016-08-15 10:24:54 +0200 | [diff] [blame] | 781 | struct slirp_arphdr *ah = (struct slirp_arphdr *)(pkt + ETH_HLEN); |
Yuval Shaia | 893dcdb | 2016-11-29 17:07:34 +0200 | [diff] [blame] | 782 | uint8_t arp_reply[MAX(ETH_HLEN + sizeof(struct slirp_arphdr), 64)]; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 783 | struct ethhdr *reh = (struct ethhdr *)arp_reply; |
Thomas Huth | 1f8b56e | 2016-08-15 10:24:54 +0200 | [diff] [blame] | 784 | struct slirp_arphdr *rah = (struct slirp_arphdr *)(arp_reply + ETH_HLEN); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 785 | int ar_op; |
bellard | a3d4af0 | 2004-09-05 23:10:26 +0000 | [diff] [blame] | 786 | struct ex_list *ex_ptr; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 787 | |
Samuel Thibault | 0b11c03 | 2016-03-20 12:29:54 +0100 | [diff] [blame] | 788 | if (!slirp->in_enabled) { |
| 789 | return; |
| 790 | } |
| 791 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 792 | ar_op = ntohs(ah->ar_op); |
| 793 | switch(ar_op) { |
| 794 | case ARPOP_REQUEST: |
Fabien Chouteau | 1a0ca1e | 2011-08-03 12:52:54 +0200 | [diff] [blame] | 795 | if (ah->ar_tip == ah->ar_sip) { |
| 796 | /* Gratuitous ARP */ |
| 797 | arp_table_add(slirp, ah->ar_sip, ah->ar_sha); |
| 798 | return; |
| 799 | } |
| 800 | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 801 | if ((ah->ar_tip & slirp->vnetwork_mask.s_addr) == |
| 802 | slirp->vnetwork_addr.s_addr) { |
| 803 | if (ah->ar_tip == slirp->vnameserver_addr.s_addr || |
| 804 | ah->ar_tip == slirp->vhost_addr.s_addr) |
bellard | a3d4af0 | 2004-09-05 23:10:26 +0000 | [diff] [blame] | 805 | goto arp_ok; |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 806 | for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) { |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 807 | if (ex_ptr->ex_addr.s_addr == ah->ar_tip) |
bellard | a3d4af0 | 2004-09-05 23:10:26 +0000 | [diff] [blame] | 808 | goto arp_ok; |
| 809 | } |
| 810 | return; |
| 811 | arp_ok: |
Hervé Poussineau | dbf3c4b | 2010-09-15 22:33:26 +0200 | [diff] [blame] | 812 | memset(arp_reply, 0, sizeof(arp_reply)); |
Fabien Chouteau | 1a0ca1e | 2011-08-03 12:52:54 +0200 | [diff] [blame] | 813 | |
| 814 | arp_table_add(slirp, ah->ar_sip, ah->ar_sha); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 815 | |
| 816 | /* ARP request for alias/dns mac address */ |
| 817 | memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN); |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 818 | memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4); |
| 819 | memcpy(&reh->h_source[2], &ah->ar_tip, 4); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 820 | reh->h_proto = htons(ETH_P_ARP); |
| 821 | |
| 822 | rah->ar_hrd = htons(1); |
| 823 | rah->ar_pro = htons(ETH_P_IP); |
| 824 | rah->ar_hln = ETH_ALEN; |
| 825 | rah->ar_pln = 4; |
| 826 | rah->ar_op = htons(ARPOP_REPLY); |
| 827 | memcpy(rah->ar_sha, reh->h_source, ETH_ALEN); |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 828 | rah->ar_sip = ah->ar_tip; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 829 | memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN); |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 830 | rah->ar_tip = ah->ar_sip; |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 831 | slirp_output(slirp->opaque, arp_reply, sizeof(arp_reply)); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 832 | } |
| 833 | break; |
bellard | de806f0 | 2008-10-17 17:28:58 +0000 | [diff] [blame] | 834 | case ARPOP_REPLY: |
Fabien Chouteau | 1a0ca1e | 2011-08-03 12:52:54 +0200 | [diff] [blame] | 835 | arp_table_add(slirp, ah->ar_sip, ah->ar_sha); |
bellard | de806f0 | 2008-10-17 17:28:58 +0000 | [diff] [blame] | 836 | break; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 837 | default: |
| 838 | break; |
| 839 | } |
| 840 | } |
| 841 | |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 842 | void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 843 | { |
| 844 | struct mbuf *m; |
| 845 | int proto; |
| 846 | |
| 847 | if (pkt_len < ETH_HLEN) |
| 848 | return; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 849 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 850 | proto = ntohs(*(uint16_t *)(pkt + 12)); |
| 851 | switch(proto) { |
| 852 | case ETH_P_ARP: |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 853 | arp_input(slirp, pkt, pkt_len); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 854 | break; |
| 855 | case ETH_P_IP: |
Guillaume Subiron | 0d6ff71 | 2016-03-15 10:31:19 +0100 | [diff] [blame] | 856 | case ETH_P_IPV6: |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 857 | m = m_get(slirp); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 858 | if (!m) |
| 859 | return; |
Guillaume Subiron | 98c6305 | 2016-03-15 10:31:20 +0100 | [diff] [blame] | 860 | /* Note: we add 2 to align the IP header on 4 bytes, |
| 861 | * and add the margin for the tcpiphdr overhead */ |
| 862 | if (M_FREEROOM(m) < pkt_len + TCPIPHDR_DELTA + 2) { |
| 863 | m_inc(m, pkt_len + TCPIPHDR_DELTA + 2); |
aurel32 | e8e880a | 2008-12-07 18:15:23 +0000 | [diff] [blame] | 864 | } |
Guillaume Subiron | 98c6305 | 2016-03-15 10:31:20 +0100 | [diff] [blame] | 865 | m->m_len = pkt_len + TCPIPHDR_DELTA + 2; |
| 866 | memcpy(m->m_data + TCPIPHDR_DELTA + 2, pkt, pkt_len); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 867 | |
Guillaume Subiron | 98c6305 | 2016-03-15 10:31:20 +0100 | [diff] [blame] | 868 | m->m_data += TCPIPHDR_DELTA + 2 + ETH_HLEN; |
| 869 | m->m_len -= TCPIPHDR_DELTA + 2 + ETH_HLEN; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 870 | |
Guillaume Subiron | 0d6ff71 | 2016-03-15 10:31:19 +0100 | [diff] [blame] | 871 | if (proto == ETH_P_IP) { |
| 872 | ip_input(m); |
| 873 | } else if (proto == ETH_P_IPV6) { |
| 874 | ip6_input(m); |
| 875 | } |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 876 | break; |
Guillaume Subiron | 0d6ff71 | 2016-03-15 10:31:19 +0100 | [diff] [blame] | 877 | |
CĂ©dric Le Goater | 47bb83c | 2017-04-14 10:35:03 +0200 | [diff] [blame] | 878 | case ETH_P_NCSI: |
| 879 | ncsi_input(slirp, pkt, pkt_len); |
| 880 | break; |
| 881 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 882 | default: |
| 883 | break; |
| 884 | } |
| 885 | } |
| 886 | |
Guillaume Subiron | 18137fb | 2015-12-19 22:24:57 +0100 | [diff] [blame] | 887 | /* Prepare the IPv4 packet to be sent to the ethernet device. Returns 1 if no |
| 888 | * packet should be sent, 0 if the packet must be re-queued, 2 if the packet |
| 889 | * is ready to go. |
Fabien Chouteau | 1ab74ce | 2011-08-01 18:18:37 +0200 | [diff] [blame] | 890 | */ |
Guillaume Subiron | 18137fb | 2015-12-19 22:24:57 +0100 | [diff] [blame] | 891 | static int if_encap4(Slirp *slirp, struct mbuf *ifm, struct ethhdr *eh, |
| 892 | uint8_t ethaddr[ETH_ALEN]) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 893 | { |
Fabien Chouteau | 1ab74ce | 2011-08-01 18:18:37 +0200 | [diff] [blame] | 894 | const struct ip *iph = (const struct ip *)ifm->m_data; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 895 | |
Samuel Thibault | 959e414 | 2014-05-14 03:13:09 +0200 | [diff] [blame] | 896 | if (iph->ip_dst.s_addr == 0) { |
| 897 | /* 0.0.0.0 can not be a destination address, something went wrong, |
| 898 | * avoid making it worse */ |
| 899 | return 1; |
| 900 | } |
Fabien Chouteau | 1a0ca1e | 2011-08-03 12:52:54 +0200 | [diff] [blame] | 901 | if (!arp_table_search(slirp, iph->ip_dst.s_addr, ethaddr)) { |
Thomas Huth | 1f8b56e | 2016-08-15 10:24:54 +0200 | [diff] [blame] | 902 | uint8_t arp_req[ETH_HLEN + sizeof(struct slirp_arphdr)]; |
bellard | de806f0 | 2008-10-17 17:28:58 +0000 | [diff] [blame] | 903 | struct ethhdr *reh = (struct ethhdr *)arp_req; |
Thomas Huth | 1f8b56e | 2016-08-15 10:24:54 +0200 | [diff] [blame] | 904 | struct slirp_arphdr *rah = (struct slirp_arphdr *)(arp_req + ETH_HLEN); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 905 | |
Guillaume Subiron | fc3779a | 2015-12-19 22:24:56 +0100 | [diff] [blame] | 906 | if (!ifm->resolution_requested) { |
Fabien Chouteau | 1ab74ce | 2011-08-01 18:18:37 +0200 | [diff] [blame] | 907 | /* If the client addr is not known, send an ARP request */ |
| 908 | memset(reh->h_dest, 0xff, ETH_ALEN); |
| 909 | memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4); |
| 910 | memcpy(&reh->h_source[2], &slirp->vhost_addr, 4); |
| 911 | reh->h_proto = htons(ETH_P_ARP); |
| 912 | rah->ar_hrd = htons(1); |
| 913 | rah->ar_pro = htons(ETH_P_IP); |
| 914 | rah->ar_hln = ETH_ALEN; |
| 915 | rah->ar_pln = 4; |
| 916 | rah->ar_op = htons(ARPOP_REQUEST); |
| 917 | |
| 918 | /* source hw addr */ |
| 919 | memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN - 4); |
| 920 | memcpy(&rah->ar_sha[2], &slirp->vhost_addr, 4); |
| 921 | |
| 922 | /* source IP */ |
| 923 | rah->ar_sip = slirp->vhost_addr.s_addr; |
| 924 | |
| 925 | /* target hw addr (none) */ |
| 926 | memset(rah->ar_tha, 0, ETH_ALEN); |
| 927 | |
| 928 | /* target IP */ |
| 929 | rah->ar_tip = iph->ip_dst.s_addr; |
| 930 | slirp->client_ipaddr = iph->ip_dst; |
| 931 | slirp_output(slirp->opaque, arp_req, sizeof(arp_req)); |
Guillaume Subiron | fc3779a | 2015-12-19 22:24:56 +0100 | [diff] [blame] | 932 | ifm->resolution_requested = true; |
Jan Kiszka | e3a110b | 2011-08-05 14:05:53 +0200 | [diff] [blame] | 933 | |
| 934 | /* Expire request and drop outgoing packet after 1 second */ |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 935 | ifm->expiration_date = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + 1000000000ULL; |
Fabien Chouteau | 1ab74ce | 2011-08-01 18:18:37 +0200 | [diff] [blame] | 936 | } |
| 937 | return 0; |
bellard | de806f0 | 2008-10-17 17:28:58 +0000 | [diff] [blame] | 938 | } else { |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 939 | memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 4); |
bellard | de806f0 | 2008-10-17 17:28:58 +0000 | [diff] [blame] | 940 | /* XXX: not correct */ |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 941 | memcpy(&eh->h_source[2], &slirp->vhost_addr, 4); |
bellard | de806f0 | 2008-10-17 17:28:58 +0000 | [diff] [blame] | 942 | eh->h_proto = htons(ETH_P_IP); |
Guillaume Subiron | 18137fb | 2015-12-19 22:24:57 +0100 | [diff] [blame] | 943 | |
| 944 | /* Send this */ |
| 945 | return 2; |
| 946 | } |
| 947 | } |
| 948 | |
Guillaume Subiron | 0d6ff71 | 2016-03-15 10:31:19 +0100 | [diff] [blame] | 949 | /* Prepare the IPv6 packet to be sent to the ethernet device. Returns 1 if no |
| 950 | * packet should be sent, 0 if the packet must be re-queued, 2 if the packet |
| 951 | * is ready to go. |
| 952 | */ |
| 953 | static int if_encap6(Slirp *slirp, struct mbuf *ifm, struct ethhdr *eh, |
| 954 | uint8_t ethaddr[ETH_ALEN]) |
| 955 | { |
| 956 | const struct ip6 *ip6h = mtod(ifm, const struct ip6 *); |
| 957 | if (!ndp_table_search(slirp, ip6h->ip_dst, ethaddr)) { |
| 958 | if (!ifm->resolution_requested) { |
| 959 | ndp_send_ns(slirp, ip6h->ip_dst); |
| 960 | ifm->resolution_requested = true; |
| 961 | ifm->expiration_date = |
| 962 | qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + 1000000000ULL; |
| 963 | } |
| 964 | return 0; |
| 965 | } else { |
| 966 | eh->h_proto = htons(ETH_P_IPV6); |
| 967 | in6_compute_ethaddr(ip6h->ip_src, eh->h_source); |
| 968 | |
| 969 | /* Send this */ |
| 970 | return 2; |
| 971 | } |
| 972 | } |
| 973 | |
Guillaume Subiron | 18137fb | 2015-12-19 22:24:57 +0100 | [diff] [blame] | 974 | /* Output the IP packet to the ethernet device. Returns 0 if the packet must be |
| 975 | * re-queued. |
| 976 | */ |
| 977 | int if_encap(Slirp *slirp, struct mbuf *ifm) |
| 978 | { |
| 979 | uint8_t buf[1600]; |
| 980 | struct ethhdr *eh = (struct ethhdr *)buf; |
| 981 | uint8_t ethaddr[ETH_ALEN]; |
| 982 | const struct ip *iph = (const struct ip *)ifm->m_data; |
| 983 | int ret; |
| 984 | |
| 985 | if (ifm->m_len + ETH_HLEN > sizeof(buf)) { |
Fabien Chouteau | 1ab74ce | 2011-08-01 18:18:37 +0200 | [diff] [blame] | 986 | return 1; |
bellard | de806f0 | 2008-10-17 17:28:58 +0000 | [diff] [blame] | 987 | } |
Guillaume Subiron | 18137fb | 2015-12-19 22:24:57 +0100 | [diff] [blame] | 988 | |
| 989 | switch (iph->ip_v) { |
| 990 | case IPVERSION: |
| 991 | ret = if_encap4(slirp, ifm, eh, ethaddr); |
| 992 | if (ret < 2) { |
| 993 | return ret; |
| 994 | } |
| 995 | break; |
| 996 | |
Guillaume Subiron | 0d6ff71 | 2016-03-15 10:31:19 +0100 | [diff] [blame] | 997 | case IP6VERSION: |
| 998 | ret = if_encap6(slirp, ifm, eh, ethaddr); |
| 999 | if (ret < 2) { |
| 1000 | return ret; |
| 1001 | } |
| 1002 | break; |
| 1003 | |
Guillaume Subiron | 18137fb | 2015-12-19 22:24:57 +0100 | [diff] [blame] | 1004 | default: |
Guillaume Subiron | 0d6ff71 | 2016-03-15 10:31:19 +0100 | [diff] [blame] | 1005 | g_assert_not_reached(); |
Guillaume Subiron | 18137fb | 2015-12-19 22:24:57 +0100 | [diff] [blame] | 1006 | break; |
| 1007 | } |
| 1008 | |
| 1009 | memcpy(eh->h_dest, ethaddr, ETH_ALEN); |
| 1010 | DEBUG_ARGS((dfd, " src = %02x:%02x:%02x:%02x:%02x:%02x\n", |
| 1011 | eh->h_source[0], eh->h_source[1], eh->h_source[2], |
| 1012 | eh->h_source[3], eh->h_source[4], eh->h_source[5])); |
| 1013 | DEBUG_ARGS((dfd, " dst = %02x:%02x:%02x:%02x:%02x:%02x\n", |
| 1014 | eh->h_dest[0], eh->h_dest[1], eh->h_dest[2], |
| 1015 | eh->h_dest[3], eh->h_dest[4], eh->h_dest[5])); |
| 1016 | memcpy(buf + sizeof(struct ethhdr), ifm->m_data, ifm->m_len); |
| 1017 | slirp_output(slirp->opaque, buf, ifm->m_len + ETH_HLEN); |
| 1018 | return 1; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 1019 | } |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 1020 | |
Jan Kiszka | 9c12a6f | 2009-06-24 14:42:29 +0200 | [diff] [blame] | 1021 | /* Drop host forwarding rule, return 0 if found. */ |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1022 | int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr, |
| 1023 | int host_port) |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 1024 | { |
| 1025 | struct socket *so; |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1026 | struct socket *head = (is_udp ? &slirp->udb : &slirp->tcb); |
Jan Kiszka | 2ad82cf | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1027 | struct sockaddr_in addr; |
| 1028 | int port = htons(host_port); |
| 1029 | socklen_t addr_len; |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 1030 | |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 1031 | for (so = head->so_next; so != head; so = so->so_next) { |
Jan Kiszka | 2ad82cf | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1032 | addr_len = sizeof(addr); |
Jan Kiszka | 9c12a6f | 2009-06-24 14:42:29 +0200 | [diff] [blame] | 1033 | if ((so->so_state & SS_HOSTFWD) && |
| 1034 | getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 && |
Jan Kiszka | 3c6a058 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1035 | addr.sin_addr.s_addr == host_addr.s_addr && |
Jan Kiszka | 2ad82cf | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1036 | addr.sin_port == port) { |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 1037 | close(so->s); |
| 1038 | sofree(so); |
Jan Kiszka | 9c12a6f | 2009-06-24 14:42:29 +0200 | [diff] [blame] | 1039 | return 0; |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 1040 | } |
| 1041 | } |
| 1042 | |
Jan Kiszka | 9c12a6f | 2009-06-24 14:42:29 +0200 | [diff] [blame] | 1043 | return -1; |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 1044 | } |
| 1045 | |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1046 | int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr, |
| 1047 | int host_port, struct in_addr guest_addr, int guest_port) |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 1048 | { |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1049 | if (!guest_addr.s_addr) { |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1050 | guest_addr = slirp->vdhcp_startaddr; |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1051 | } |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 1052 | if (is_udp) { |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1053 | if (!udp_listen(slirp, host_addr.s_addr, htons(host_port), |
| 1054 | guest_addr.s_addr, htons(guest_port), SS_HOSTFWD)) |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 1055 | return -1; |
| 1056 | } else { |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1057 | if (!tcp_listen(slirp, host_addr.s_addr, htons(host_port), |
| 1058 | guest_addr.s_addr, htons(guest_port), SS_HOSTFWD)) |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 1059 | return -1; |
| 1060 | } |
| 1061 | return 0; |
| 1062 | } |
bellard | a3d4af0 | 2004-09-05 23:10:26 +0000 | [diff] [blame] | 1063 | |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1064 | int slirp_add_exec(Slirp *slirp, int do_pty, const void *args, |
Jan Kiszka | bb53fc5 | 2009-07-22 17:03:52 +0200 | [diff] [blame] | 1065 | struct in_addr *guest_addr, int guest_port) |
bellard | a3d4af0 | 2004-09-05 23:10:26 +0000 | [diff] [blame] | 1066 | { |
Jan Kiszka | bb53fc5 | 2009-07-22 17:03:52 +0200 | [diff] [blame] | 1067 | if (!guest_addr->s_addr) { |
| 1068 | guest_addr->s_addr = slirp->vnetwork_addr.s_addr | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1069 | (htonl(0x0204) & ~slirp->vnetwork_mask.s_addr); |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1070 | } |
Jan Kiszka | bb53fc5 | 2009-07-22 17:03:52 +0200 | [diff] [blame] | 1071 | if ((guest_addr->s_addr & slirp->vnetwork_mask.s_addr) != |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1072 | slirp->vnetwork_addr.s_addr || |
Jan Kiszka | bb53fc5 | 2009-07-22 17:03:52 +0200 | [diff] [blame] | 1073 | guest_addr->s_addr == slirp->vhost_addr.s_addr || |
| 1074 | guest_addr->s_addr == slirp->vnameserver_addr.s_addr) { |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1075 | return -1; |
| 1076 | } |
Jan Kiszka | bb53fc5 | 2009-07-22 17:03:52 +0200 | [diff] [blame] | 1077 | return add_exec(&slirp->exec_list, do_pty, (char *)args, *guest_addr, |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1078 | htons(guest_port)); |
bellard | a3d4af0 | 2004-09-05 23:10:26 +0000 | [diff] [blame] | 1079 | } |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 1080 | |
| 1081 | ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags) |
| 1082 | { |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 1083 | if (so->s == -1 && so->extra) { |
Daniel P. Berrange | 6ab3fc3 | 2016-09-06 14:56:04 +0100 | [diff] [blame] | 1084 | /* XXX this blocks entire thread. Rewrite to use |
| 1085 | * qemu_chr_fe_write and background I/O callbacks */ |
| 1086 | qemu_chr_fe_write_all(so->extra, buf, len); |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 1087 | return len; |
| 1088 | } |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 1089 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 1090 | return send(so->s, buf, len, flags); |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1093 | static struct socket * |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1094 | slirp_find_ctl_socket(Slirp *slirp, struct in_addr guest_addr, int guest_port) |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 1095 | { |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1096 | struct socket *so; |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 1097 | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1098 | for (so = slirp->tcb.so_next; so != &slirp->tcb; so = so->so_next) { |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1099 | if (so->so_faddr.s_addr == guest_addr.s_addr && |
| 1100 | htons(so->so_fport) == guest_port) { |
| 1101 | return so; |
| 1102 | } |
| 1103 | } |
| 1104 | return NULL; |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1107 | size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr, |
| 1108 | int guest_port) |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 1109 | { |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 1110 | struct iovec iov[2]; |
| 1111 | struct socket *so; |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 1112 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 1113 | so = slirp_find_ctl_socket(slirp, guest_addr, guest_port); |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 1114 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 1115 | if (!so || so->so_state & SS_NOFDREF) { |
| 1116 | return 0; |
| 1117 | } |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 1118 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 1119 | if (!CONN_CANFRCV(so) || so->so_snd.sb_cc >= (so->so_snd.sb_datalen/2)) { |
| 1120 | return 0; |
| 1121 | } |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 1122 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 1123 | return sopreprbuf(so, iov, NULL); |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 1124 | } |
| 1125 | |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1126 | void slirp_socket_recv(Slirp *slirp, struct in_addr guest_addr, int guest_port, |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1127 | const uint8_t *buf, int size) |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 1128 | { |
| 1129 | int ret; |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1130 | struct socket *so = slirp_find_ctl_socket(slirp, guest_addr, guest_port); |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1131 | |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 1132 | if (!so) |
| 1133 | return; |
| 1134 | |
blueswir1 | 0580ac9 | 2009-01-12 17:51:06 +0000 | [diff] [blame] | 1135 | ret = soreadbuf(so, (const char *)buf, size); |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 1136 | |
| 1137 | if (ret > 0) |
| 1138 | tcp_output(sototcpcb(so)); |
| 1139 | } |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1140 | |
Dr. David Alan Gilbert | e3ec38f | 2017-02-20 18:50:16 +0000 | [diff] [blame] | 1141 | static int slirp_tcp_post_load(void *opaque, int version) |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1142 | { |
Dr. David Alan Gilbert | e3ec38f | 2017-02-20 18:50:16 +0000 | [diff] [blame] | 1143 | tcp_template((struct tcpcb *)opaque); |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1144 | |
Dr. David Alan Gilbert | e3ec38f | 2017-02-20 18:50:16 +0000 | [diff] [blame] | 1145 | return 0; |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
Dr. David Alan Gilbert | e3ec38f | 2017-02-20 18:50:16 +0000 | [diff] [blame] | 1148 | static const VMStateDescription vmstate_slirp_tcp = { |
| 1149 | .name = "slirp-tcp", |
| 1150 | .version_id = 0, |
| 1151 | .post_load = slirp_tcp_post_load, |
| 1152 | .fields = (VMStateField[]) { |
| 1153 | VMSTATE_INT16(t_state, struct tcpcb), |
| 1154 | VMSTATE_INT16_ARRAY(t_timer, struct tcpcb, TCPT_NTIMERS), |
| 1155 | VMSTATE_INT16(t_rxtshift, struct tcpcb), |
| 1156 | VMSTATE_INT16(t_rxtcur, struct tcpcb), |
| 1157 | VMSTATE_INT16(t_dupacks, struct tcpcb), |
| 1158 | VMSTATE_UINT16(t_maxseg, struct tcpcb), |
| 1159 | VMSTATE_UINT8(t_force, struct tcpcb), |
| 1160 | VMSTATE_UINT16(t_flags, struct tcpcb), |
| 1161 | VMSTATE_UINT32(snd_una, struct tcpcb), |
| 1162 | VMSTATE_UINT32(snd_nxt, struct tcpcb), |
| 1163 | VMSTATE_UINT32(snd_up, struct tcpcb), |
| 1164 | VMSTATE_UINT32(snd_wl1, struct tcpcb), |
| 1165 | VMSTATE_UINT32(snd_wl2, struct tcpcb), |
| 1166 | VMSTATE_UINT32(iss, struct tcpcb), |
| 1167 | VMSTATE_UINT32(snd_wnd, struct tcpcb), |
| 1168 | VMSTATE_UINT32(rcv_wnd, struct tcpcb), |
| 1169 | VMSTATE_UINT32(rcv_nxt, struct tcpcb), |
| 1170 | VMSTATE_UINT32(rcv_up, struct tcpcb), |
| 1171 | VMSTATE_UINT32(irs, struct tcpcb), |
| 1172 | VMSTATE_UINT32(rcv_adv, struct tcpcb), |
| 1173 | VMSTATE_UINT32(snd_max, struct tcpcb), |
| 1174 | VMSTATE_UINT32(snd_cwnd, struct tcpcb), |
| 1175 | VMSTATE_UINT32(snd_ssthresh, struct tcpcb), |
| 1176 | VMSTATE_INT16(t_idle, struct tcpcb), |
| 1177 | VMSTATE_INT16(t_rtt, struct tcpcb), |
| 1178 | VMSTATE_UINT32(t_rtseq, struct tcpcb), |
| 1179 | VMSTATE_INT16(t_srtt, struct tcpcb), |
| 1180 | VMSTATE_INT16(t_rttvar, struct tcpcb), |
| 1181 | VMSTATE_UINT16(t_rttmin, struct tcpcb), |
| 1182 | VMSTATE_UINT32(max_sndwnd, struct tcpcb), |
| 1183 | VMSTATE_UINT8(t_oobflags, struct tcpcb), |
| 1184 | VMSTATE_UINT8(t_iobc, struct tcpcb), |
| 1185 | VMSTATE_INT16(t_softerror, struct tcpcb), |
| 1186 | VMSTATE_UINT8(snd_scale, struct tcpcb), |
| 1187 | VMSTATE_UINT8(rcv_scale, struct tcpcb), |
| 1188 | VMSTATE_UINT8(request_r_scale, struct tcpcb), |
| 1189 | VMSTATE_UINT8(requested_s_scale, struct tcpcb), |
| 1190 | VMSTATE_UINT32(ts_recent, struct tcpcb), |
| 1191 | VMSTATE_UINT32(ts_recent_age, struct tcpcb), |
| 1192 | VMSTATE_UINT32(last_ack_sent, struct tcpcb), |
| 1193 | VMSTATE_END_OF_LIST() |
| 1194 | } |
| 1195 | }; |
| 1196 | |
Dr. David Alan Gilbert | 2a7cab9 | 2017-02-20 18:50:17 +0000 | [diff] [blame] | 1197 | /* The sbuf has a pair of pointers that are migrated as offsets; |
| 1198 | * we calculate the offsets and restore the pointers using |
| 1199 | * pre_save/post_load on a tmp structure. |
| 1200 | */ |
| 1201 | struct sbuf_tmp { |
| 1202 | struct sbuf *parent; |
| 1203 | uint32_t roff, woff; |
| 1204 | }; |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1205 | |
Dr. David Alan Gilbert | 44b1ff3 | 2017-09-25 12:29:12 +0100 | [diff] [blame] | 1206 | static int sbuf_tmp_pre_save(void *opaque) |
Dr. David Alan Gilbert | 2a7cab9 | 2017-02-20 18:50:17 +0000 | [diff] [blame] | 1207 | { |
| 1208 | struct sbuf_tmp *tmp = opaque; |
| 1209 | tmp->woff = tmp->parent->sb_wptr - tmp->parent->sb_data; |
| 1210 | tmp->roff = tmp->parent->sb_rptr - tmp->parent->sb_data; |
Dr. David Alan Gilbert | 44b1ff3 | 2017-09-25 12:29:12 +0100 | [diff] [blame] | 1211 | |
| 1212 | return 0; |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1213 | } |
| 1214 | |
Dr. David Alan Gilbert | 2a7cab9 | 2017-02-20 18:50:17 +0000 | [diff] [blame] | 1215 | static int sbuf_tmp_post_load(void *opaque, int version) |
| 1216 | { |
| 1217 | struct sbuf_tmp *tmp = opaque; |
| 1218 | uint32_t requested_len = tmp->parent->sb_datalen; |
| 1219 | |
| 1220 | /* Allocate the buffer space used by the field after the tmp */ |
| 1221 | sbreserve(tmp->parent, tmp->parent->sb_datalen); |
| 1222 | |
| 1223 | if (tmp->parent->sb_datalen != requested_len) { |
| 1224 | return -ENOMEM; |
| 1225 | } |
| 1226 | if (tmp->woff >= requested_len || |
| 1227 | tmp->roff >= requested_len) { |
| 1228 | error_report("invalid sbuf offsets r/w=%u/%u len=%u", |
| 1229 | tmp->roff, tmp->woff, requested_len); |
| 1230 | return -EINVAL; |
| 1231 | } |
| 1232 | |
| 1233 | tmp->parent->sb_wptr = tmp->parent->sb_data + tmp->woff; |
| 1234 | tmp->parent->sb_rptr = tmp->parent->sb_data + tmp->roff; |
| 1235 | |
| 1236 | return 0; |
| 1237 | } |
| 1238 | |
| 1239 | |
| 1240 | static const VMStateDescription vmstate_slirp_sbuf_tmp = { |
| 1241 | .name = "slirp-sbuf-tmp", |
| 1242 | .post_load = sbuf_tmp_post_load, |
| 1243 | .pre_save = sbuf_tmp_pre_save, |
| 1244 | .version_id = 0, |
| 1245 | .fields = (VMStateField[]) { |
| 1246 | VMSTATE_UINT32(woff, struct sbuf_tmp), |
| 1247 | VMSTATE_UINT32(roff, struct sbuf_tmp), |
| 1248 | VMSTATE_END_OF_LIST() |
| 1249 | } |
| 1250 | }; |
| 1251 | |
| 1252 | static const VMStateDescription vmstate_slirp_sbuf = { |
| 1253 | .name = "slirp-sbuf", |
| 1254 | .version_id = 0, |
| 1255 | .fields = (VMStateField[]) { |
| 1256 | VMSTATE_UINT32(sb_cc, struct sbuf), |
| 1257 | VMSTATE_UINT32(sb_datalen, struct sbuf), |
| 1258 | VMSTATE_WITH_TMP(struct sbuf, struct sbuf_tmp, vmstate_slirp_sbuf_tmp), |
| 1259 | VMSTATE_VBUFFER_UINT32(sb_data, struct sbuf, 0, NULL, sb_datalen), |
| 1260 | VMSTATE_END_OF_LIST() |
| 1261 | } |
| 1262 | }; |
| 1263 | |
Dr. David Alan Gilbert | 14650df | 2017-04-03 18:18:42 +0100 | [diff] [blame] | 1264 | static bool slirp_older_than_v4(void *opaque, int version_id) |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1265 | { |
Dr. David Alan Gilbert | 14650df | 2017-04-03 18:18:42 +0100 | [diff] [blame] | 1266 | return version_id < 4; |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
Dr. David Alan Gilbert | 14650df | 2017-04-03 18:18:42 +0100 | [diff] [blame] | 1269 | static bool slirp_family_inet(void *opaque, int version_id) |
| 1270 | { |
| 1271 | union slirp_sockaddr *ssa = (union slirp_sockaddr *)opaque; |
| 1272 | return ssa->ss.ss_family == AF_INET; |
| 1273 | } |
| 1274 | |
| 1275 | static int slirp_socket_pre_load(void *opaque) |
| 1276 | { |
| 1277 | struct socket *so = opaque; |
| 1278 | if (tcp_attach(so) < 0) { |
| 1279 | return -ENOMEM; |
| 1280 | } |
| 1281 | /* Older versions don't load these fields */ |
| 1282 | so->so_ffamily = AF_INET; |
| 1283 | so->so_lfamily = AF_INET; |
| 1284 | return 0; |
| 1285 | } |
| 1286 | |
| 1287 | #ifndef _WIN32 |
| 1288 | #define VMSTATE_SIN4_ADDR(f, s, t) VMSTATE_UINT32_TEST(f, s, t) |
| 1289 | #else |
| 1290 | /* Win uses u_long rather than uint32_t - but it's still 32bits long */ |
| 1291 | #define VMSTATE_SIN4_ADDR(f, s, t) VMSTATE_SINGLE_TEST(f, s, t, 0, \ |
| 1292 | vmstate_info_uint32, u_long) |
| 1293 | #endif |
| 1294 | |
| 1295 | /* The OS provided ss_family field isn't that portable; it's size |
| 1296 | * and type varies (16/8 bit, signed, unsigned) |
| 1297 | * and the values it contains aren't fully portable. |
| 1298 | */ |
| 1299 | typedef struct SS_FamilyTmpStruct { |
| 1300 | union slirp_sockaddr *parent; |
| 1301 | uint16_t portable_family; |
| 1302 | } SS_FamilyTmpStruct; |
| 1303 | |
| 1304 | #define SS_FAMILY_MIG_IPV4 2 /* Linux, BSD, Win... */ |
| 1305 | #define SS_FAMILY_MIG_IPV6 10 /* Linux */ |
| 1306 | #define SS_FAMILY_MIG_OTHER 0xffff |
| 1307 | |
Dr. David Alan Gilbert | 44b1ff3 | 2017-09-25 12:29:12 +0100 | [diff] [blame] | 1308 | static int ss_family_pre_save(void *opaque) |
Dr. David Alan Gilbert | 14650df | 2017-04-03 18:18:42 +0100 | [diff] [blame] | 1309 | { |
| 1310 | SS_FamilyTmpStruct *tss = opaque; |
| 1311 | |
| 1312 | tss->portable_family = SS_FAMILY_MIG_OTHER; |
| 1313 | |
| 1314 | if (tss->parent->ss.ss_family == AF_INET) { |
| 1315 | tss->portable_family = SS_FAMILY_MIG_IPV4; |
| 1316 | } else if (tss->parent->ss.ss_family == AF_INET6) { |
| 1317 | tss->portable_family = SS_FAMILY_MIG_IPV6; |
| 1318 | } |
Dr. David Alan Gilbert | 44b1ff3 | 2017-09-25 12:29:12 +0100 | [diff] [blame] | 1319 | |
| 1320 | return 0; |
Dr. David Alan Gilbert | 14650df | 2017-04-03 18:18:42 +0100 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | static int ss_family_post_load(void *opaque, int version_id) |
| 1324 | { |
| 1325 | SS_FamilyTmpStruct *tss = opaque; |
| 1326 | |
| 1327 | switch (tss->portable_family) { |
| 1328 | case SS_FAMILY_MIG_IPV4: |
| 1329 | tss->parent->ss.ss_family = AF_INET; |
| 1330 | break; |
| 1331 | case SS_FAMILY_MIG_IPV6: |
| 1332 | case 23: /* compatibility: AF_INET6 from mingw */ |
| 1333 | case 28: /* compatibility: AF_INET6 from FreeBSD sys/socket.h */ |
| 1334 | tss->parent->ss.ss_family = AF_INET6; |
| 1335 | break; |
| 1336 | default: |
| 1337 | error_report("invalid ss_family type %x", tss->portable_family); |
| 1338 | return -EINVAL; |
| 1339 | } |
| 1340 | |
| 1341 | return 0; |
| 1342 | } |
| 1343 | |
| 1344 | static const VMStateDescription vmstate_slirp_ss_family = { |
| 1345 | .name = "slirp-socket-addr/ss_family", |
| 1346 | .pre_save = ss_family_pre_save, |
| 1347 | .post_load = ss_family_post_load, |
| 1348 | .fields = (VMStateField[]) { |
| 1349 | VMSTATE_UINT16(portable_family, SS_FamilyTmpStruct), |
| 1350 | VMSTATE_END_OF_LIST() |
| 1351 | } |
| 1352 | }; |
| 1353 | |
| 1354 | static const VMStateDescription vmstate_slirp_socket_addr = { |
| 1355 | .name = "slirp-socket-addr", |
| 1356 | .version_id = 4, |
| 1357 | .fields = (VMStateField[]) { |
| 1358 | VMSTATE_WITH_TMP(union slirp_sockaddr, SS_FamilyTmpStruct, |
| 1359 | vmstate_slirp_ss_family), |
| 1360 | VMSTATE_SIN4_ADDR(sin.sin_addr.s_addr, union slirp_sockaddr, |
| 1361 | slirp_family_inet), |
| 1362 | VMSTATE_UINT16_TEST(sin.sin_port, union slirp_sockaddr, |
| 1363 | slirp_family_inet), |
| 1364 | |
| 1365 | #if 0 |
| 1366 | /* Untested: Needs checking by someone with IPv6 test */ |
| 1367 | VMSTATE_BUFFER_TEST(sin6.sin6_addr, union slirp_sockaddr, |
| 1368 | slirp_family_inet6), |
| 1369 | VMSTATE_UINT16_TEST(sin6.sin6_port, union slirp_sockaddr, |
| 1370 | slirp_family_inet6), |
| 1371 | VMSTATE_UINT32_TEST(sin6.sin6_flowinfo, union slirp_sockaddr, |
| 1372 | slirp_family_inet6), |
| 1373 | VMSTATE_UINT32_TEST(sin6.sin6_scope_id, union slirp_sockaddr, |
| 1374 | slirp_family_inet6), |
| 1375 | #endif |
| 1376 | |
| 1377 | VMSTATE_END_OF_LIST() |
| 1378 | } |
| 1379 | }; |
| 1380 | |
| 1381 | static const VMStateDescription vmstate_slirp_socket = { |
| 1382 | .name = "slirp-socket", |
| 1383 | .version_id = 4, |
| 1384 | .pre_load = slirp_socket_pre_load, |
| 1385 | .fields = (VMStateField[]) { |
| 1386 | VMSTATE_UINT32(so_urgc, struct socket), |
| 1387 | /* Pre-v4 versions */ |
| 1388 | VMSTATE_SIN4_ADDR(so_faddr.s_addr, struct socket, |
| 1389 | slirp_older_than_v4), |
| 1390 | VMSTATE_SIN4_ADDR(so_laddr.s_addr, struct socket, |
| 1391 | slirp_older_than_v4), |
| 1392 | VMSTATE_UINT16_TEST(so_fport, struct socket, slirp_older_than_v4), |
| 1393 | VMSTATE_UINT16_TEST(so_lport, struct socket, slirp_older_than_v4), |
| 1394 | /* v4 and newer */ |
| 1395 | VMSTATE_STRUCT(fhost, struct socket, 4, vmstate_slirp_socket_addr, |
| 1396 | union slirp_sockaddr), |
| 1397 | VMSTATE_STRUCT(lhost, struct socket, 4, vmstate_slirp_socket_addr, |
| 1398 | union slirp_sockaddr), |
| 1399 | |
| 1400 | VMSTATE_UINT8(so_iptos, struct socket), |
| 1401 | VMSTATE_UINT8(so_emu, struct socket), |
| 1402 | VMSTATE_UINT8(so_type, struct socket), |
| 1403 | VMSTATE_INT32(so_state, struct socket), |
| 1404 | VMSTATE_STRUCT(so_rcv, struct socket, 0, vmstate_slirp_sbuf, |
| 1405 | struct sbuf), |
| 1406 | VMSTATE_STRUCT(so_snd, struct socket, 0, vmstate_slirp_sbuf, |
| 1407 | struct sbuf), |
| 1408 | VMSTATE_STRUCT_POINTER(so_tcpcb, struct socket, vmstate_slirp_tcp, |
| 1409 | struct tcpcb), |
| 1410 | VMSTATE_END_OF_LIST() |
| 1411 | } |
| 1412 | }; |
| 1413 | |
Dr. David Alan Gilbert | eb5d4f5 | 2017-04-03 18:18:43 +0100 | [diff] [blame] | 1414 | static const VMStateDescription vmstate_slirp_bootp_client = { |
| 1415 | .name = "slirp_bootpclient", |
| 1416 | .fields = (VMStateField[]) { |
| 1417 | VMSTATE_UINT16(allocated, BOOTPClient), |
| 1418 | VMSTATE_BUFFER(macaddr, BOOTPClient), |
| 1419 | VMSTATE_END_OF_LIST() |
Jan Kiszka | 0a1f851 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1420 | } |
Dr. David Alan Gilbert | eb5d4f5 | 2017-04-03 18:18:43 +0100 | [diff] [blame] | 1421 | }; |
| 1422 | |
| 1423 | static const VMStateDescription vmstate_slirp = { |
| 1424 | .name = "slirp", |
| 1425 | .version_id = 4, |
| 1426 | .fields = (VMStateField[]) { |
| 1427 | VMSTATE_UINT16_V(ip_id, Slirp, 2), |
| 1428 | VMSTATE_STRUCT_ARRAY(bootp_clients, Slirp, NB_BOOTP_CLIENTS, 3, |
| 1429 | vmstate_slirp_bootp_client, BOOTPClient), |
| 1430 | VMSTATE_END_OF_LIST() |
| 1431 | } |
| 1432 | }; |
Jan Kiszka | 0a1f851 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1433 | |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1434 | static void slirp_state_save(QEMUFile *f, void *opaque) |
| 1435 | { |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1436 | Slirp *slirp = opaque; |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1437 | struct ex_list *ex_ptr; |
| 1438 | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1439 | for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1440 | if (ex_ptr->ex_pty == 3) { |
| 1441 | struct socket *so; |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1442 | so = slirp_find_ctl_socket(slirp, ex_ptr->ex_addr, |
| 1443 | ntohs(ex_ptr->ex_fport)); |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1444 | if (!so) |
| 1445 | continue; |
| 1446 | |
| 1447 | qemu_put_byte(f, 42); |
Dr. David Alan Gilbert | 14650df | 2017-04-03 18:18:42 +0100 | [diff] [blame] | 1448 | vmstate_save_state(f, &vmstate_slirp_socket, so, NULL); |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1449 | } |
| 1450 | qemu_put_byte(f, 0); |
Jan Kiszka | 285f7a6 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 1451 | |
Dr. David Alan Gilbert | eb5d4f5 | 2017-04-03 18:18:43 +0100 | [diff] [blame] | 1452 | vmstate_save_state(f, &vmstate_slirp, slirp, NULL); |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1453 | } |
| 1454 | |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1455 | |
| 1456 | static int slirp_state_load(QEMUFile *f, void *opaque, int version_id) |
| 1457 | { |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1458 | Slirp *slirp = opaque; |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1459 | struct ex_list *ex_ptr; |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1460 | |
Blue Swirl | b0e0486 | 2010-03-07 13:45:38 +0000 | [diff] [blame] | 1461 | while (qemu_get_byte(f)) { |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1462 | int ret; |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1463 | struct socket *so = socreate(slirp); |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1464 | |
| 1465 | if (!so) |
| 1466 | return -ENOMEM; |
| 1467 | |
Dr. David Alan Gilbert | 14650df | 2017-04-03 18:18:42 +0100 | [diff] [blame] | 1468 | ret = vmstate_load_state(f, &vmstate_slirp_socket, so, version_id); |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1469 | |
| 1470 | if (ret < 0) |
| 1471 | return ret; |
| 1472 | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1473 | if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) != |
| 1474 | slirp->vnetwork_addr.s_addr) { |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1475 | return -EINVAL; |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1476 | } |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1477 | for (ex_ptr = slirp->exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) { |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1478 | if (ex_ptr->ex_pty == 3 && |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1479 | so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr && |
| 1480 | so->so_fport == ex_ptr->ex_fport) { |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1481 | break; |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1482 | } |
| 1483 | } |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1484 | if (!ex_ptr) |
| 1485 | return -EINVAL; |
| 1486 | |
blueswir1 | 0580ac9 | 2009-01-12 17:51:06 +0000 | [diff] [blame] | 1487 | so->extra = (void *)ex_ptr->ex_exec; |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Dr. David Alan Gilbert | eb5d4f5 | 2017-04-03 18:18:43 +0100 | [diff] [blame] | 1490 | return vmstate_load_state(f, &vmstate_slirp, slirp, version_id); |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1491 | } |