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 | */ |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 24 | #include "qemu-common.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 25 | #include "qemu/timer.h" |
Paolo Bonzini | dccfcd0 | 2013-04-08 16:55:25 +0200 | [diff] [blame] | 26 | #include "sysemu/char.h" |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 27 | #include "slirp.h" |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 28 | #include "hw/hw.h" |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 29 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 30 | /* host loopback address */ |
| 31 | struct in_addr loopback_addr; |
Anders Waldenborg | 648cd33 | 2012-07-13 22:54:17 +0200 | [diff] [blame] | 32 | /* host loopback network mask */ |
Anthony Liguori | 0b8db8f | 2012-08-06 19:31:55 -0500 | [diff] [blame] | 33 | unsigned long loopback_mask; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 34 | |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 35 | /* emulated hosts use the MAC addr 52:55:IP:IP:IP:IP */ |
Fabien Chouteau | 1a0ca1e | 2011-08-03 12:52:54 +0200 | [diff] [blame] | 36 | static const uint8_t special_ethaddr[ETH_ALEN] = { |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 37 | 0x52, 0x55, 0x00, 0x00, 0x00, 0x00 |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 38 | }; |
| 39 | |
Fabien Chouteau | 1a0ca1e | 2011-08-03 12:52:54 +0200 | [diff] [blame] | 40 | static const uint8_t zero_ethaddr[ETH_ALEN] = { 0, 0, 0, 0, 0, 0 }; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 41 | |
Jan Kiszka | f1d99bb | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 42 | u_int curtime; |
| 43 | static u_int time_fasttimo, last_slowtimo; |
| 44 | static int do_slowtimo; |
| 45 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 46 | static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances = |
| 47 | QTAILQ_HEAD_INITIALIZER(slirp_instances); |
pbrook | 115defd | 2006-04-16 11:06:58 +0000 | [diff] [blame] | 48 | |
Stefan Weil | 9e3a95e | 2009-08-31 16:29:34 +0200 | [diff] [blame] | 49 | static struct in_addr dns_addr; |
| 50 | static u_int dns_addr_time; |
Ed Swierk | df7a86e | 2009-08-20 19:00:31 -0700 | [diff] [blame] | 51 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 52 | #ifdef _WIN32 |
| 53 | |
Ed Swierk | df7a86e | 2009-08-20 19:00:31 -0700 | [diff] [blame] | 54 | int get_dns_addr(struct in_addr *pdns_addr) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 55 | { |
bellard | 379ff53 | 2004-07-12 22:33:07 +0000 | [diff] [blame] | 56 | FIXED_INFO *FixedInfo=NULL; |
| 57 | ULONG BufLen; |
| 58 | DWORD ret; |
| 59 | IP_ADDR_STRING *pIPAddr; |
| 60 | struct in_addr tmp_addr; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 61 | |
Ed Swierk | df7a86e | 2009-08-20 19:00:31 -0700 | [diff] [blame] | 62 | if (dns_addr.s_addr != 0 && (curtime - dns_addr_time) < 1000) { |
| 63 | *pdns_addr = dns_addr; |
| 64 | return 0; |
| 65 | } |
| 66 | |
bellard | 379ff53 | 2004-07-12 22:33:07 +0000 | [diff] [blame] | 67 | FixedInfo = (FIXED_INFO *)GlobalAlloc(GPTR, sizeof(FIXED_INFO)); |
| 68 | BufLen = sizeof(FIXED_INFO); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 69 | |
bellard | 379ff53 | 2004-07-12 22:33:07 +0000 | [diff] [blame] | 70 | if (ERROR_BUFFER_OVERFLOW == GetNetworkParams(FixedInfo, &BufLen)) { |
| 71 | if (FixedInfo) { |
| 72 | GlobalFree(FixedInfo); |
| 73 | FixedInfo = NULL; |
| 74 | } |
| 75 | FixedInfo = GlobalAlloc(GPTR, BufLen); |
| 76 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 77 | |
bellard | 379ff53 | 2004-07-12 22:33:07 +0000 | [diff] [blame] | 78 | if ((ret = GetNetworkParams(FixedInfo, &BufLen)) != ERROR_SUCCESS) { |
| 79 | printf("GetNetworkParams failed. ret = %08x\n", (u_int)ret ); |
| 80 | if (FixedInfo) { |
| 81 | GlobalFree(FixedInfo); |
| 82 | FixedInfo = NULL; |
| 83 | } |
| 84 | return -1; |
| 85 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 86 | |
bellard | 379ff53 | 2004-07-12 22:33:07 +0000 | [diff] [blame] | 87 | pIPAddr = &(FixedInfo->DnsServerList); |
| 88 | inet_aton(pIPAddr->IpAddress.String, &tmp_addr); |
| 89 | *pdns_addr = tmp_addr; |
Ed Swierk | df7a86e | 2009-08-20 19:00:31 -0700 | [diff] [blame] | 90 | dns_addr = tmp_addr; |
| 91 | dns_addr_time = curtime; |
bellard | 379ff53 | 2004-07-12 22:33:07 +0000 | [diff] [blame] | 92 | if (FixedInfo) { |
| 93 | GlobalFree(FixedInfo); |
| 94 | FixedInfo = NULL; |
| 95 | } |
| 96 | return 0; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Jan Kiszka | df46189 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 99 | static void winsock_cleanup(void) |
| 100 | { |
| 101 | WSACleanup(); |
| 102 | } |
| 103 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 104 | #else |
| 105 | |
Blue Swirl | 1e6eec8 | 2009-09-05 10:14:07 +0000 | [diff] [blame] | 106 | static struct stat dns_addr_stat; |
Ed Swierk | df7a86e | 2009-08-20 19:00:31 -0700 | [diff] [blame] | 107 | |
| 108 | int get_dns_addr(struct in_addr *pdns_addr) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 109 | { |
| 110 | char buff[512]; |
blueswir1 | 363a37d | 2008-08-21 17:58:08 +0000 | [diff] [blame] | 111 | char buff2[257]; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 112 | FILE *f; |
| 113 | int found = 0; |
| 114 | struct in_addr tmp_addr; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 115 | |
Ed Swierk | df7a86e | 2009-08-20 19:00:31 -0700 | [diff] [blame] | 116 | if (dns_addr.s_addr != 0) { |
| 117 | struct stat old_stat; |
| 118 | if ((curtime - dns_addr_time) < 1000) { |
| 119 | *pdns_addr = dns_addr; |
| 120 | return 0; |
| 121 | } |
| 122 | old_stat = dns_addr_stat; |
| 123 | if (stat("/etc/resolv.conf", &dns_addr_stat) != 0) |
| 124 | return -1; |
| 125 | if ((dns_addr_stat.st_dev == old_stat.st_dev) |
| 126 | && (dns_addr_stat.st_ino == old_stat.st_ino) |
| 127 | && (dns_addr_stat.st_size == old_stat.st_size) |
| 128 | && (dns_addr_stat.st_mtime == old_stat.st_mtime)) { |
| 129 | *pdns_addr = dns_addr; |
| 130 | return 0; |
| 131 | } |
| 132 | } |
| 133 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 134 | f = fopen("/etc/resolv.conf", "r"); |
| 135 | if (!f) |
| 136 | return -1; |
| 137 | |
blueswir1 | 31a60e2 | 2007-10-26 18:42:59 +0000 | [diff] [blame] | 138 | #ifdef DEBUG |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 139 | lprint("IP address of your DNS(s): "); |
blueswir1 | 31a60e2 | 2007-10-26 18:42:59 +0000 | [diff] [blame] | 140 | #endif |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 141 | while (fgets(buff, 512, f) != NULL) { |
| 142 | if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) { |
| 143 | if (!inet_aton(buff2, &tmp_addr)) |
| 144 | continue; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 145 | /* If it's the first one, set it to dns_addr */ |
Ed Swierk | df7a86e | 2009-08-20 19:00:31 -0700 | [diff] [blame] | 146 | if (!found) { |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 147 | *pdns_addr = tmp_addr; |
Ed Swierk | df7a86e | 2009-08-20 19:00:31 -0700 | [diff] [blame] | 148 | dns_addr = tmp_addr; |
| 149 | dns_addr_time = curtime; |
| 150 | } |
blueswir1 | 31a60e2 | 2007-10-26 18:42:59 +0000 | [diff] [blame] | 151 | #ifdef DEBUG |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 152 | else |
| 153 | lprint(", "); |
blueswir1 | 31a60e2 | 2007-10-26 18:42:59 +0000 | [diff] [blame] | 154 | #endif |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 155 | if (++found > 3) { |
blueswir1 | 31a60e2 | 2007-10-26 18:42:59 +0000 | [diff] [blame] | 156 | #ifdef DEBUG |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 157 | lprint("(more)"); |
blueswir1 | 31a60e2 | 2007-10-26 18:42:59 +0000 | [diff] [blame] | 158 | #endif |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 159 | break; |
blueswir1 | 31a60e2 | 2007-10-26 18:42:59 +0000 | [diff] [blame] | 160 | } |
| 161 | #ifdef DEBUG |
| 162 | else |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 163 | lprint("%s", inet_ntoa(tmp_addr)); |
blueswir1 | 31a60e2 | 2007-10-26 18:42:59 +0000 | [diff] [blame] | 164 | #endif |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
bellard | 1d43a71 | 2004-07-05 21:18:42 +0000 | [diff] [blame] | 167 | fclose(f); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 168 | if (!found) |
| 169 | return -1; |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | #endif |
| 174 | |
Jan Kiszka | df46189 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 175 | static void slirp_init_once(void) |
bellard | 379ff53 | 2004-07-12 22:33:07 +0000 | [diff] [blame] | 176 | { |
Jan Kiszka | df46189 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 177 | static int initialized; |
Jan Kiszka | df46189 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 178 | #ifdef _WIN32 |
| 179 | WSADATA Data; |
bellard | 379ff53 | 2004-07-12 22:33:07 +0000 | [diff] [blame] | 180 | #endif |
| 181 | |
Jan Kiszka | df46189 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 182 | if (initialized) { |
| 183 | return; |
| 184 | } |
| 185 | initialized = 1; |
| 186 | |
| 187 | #ifdef _WIN32 |
| 188 | WSAStartup(MAKEWORD(2,0), &Data); |
| 189 | atexit(winsock_cleanup); |
| 190 | #endif |
| 191 | |
| 192 | loopback_addr.s_addr = htonl(INADDR_LOOPBACK); |
Anders Waldenborg | 648cd33 | 2012-07-13 22:54:17 +0200 | [diff] [blame] | 193 | loopback_mask = htonl(IN_CLASSA_NET); |
Jan Kiszka | df46189 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 194 | } |
| 195 | |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 196 | static void slirp_state_save(QEMUFile *f, void *opaque); |
| 197 | static int slirp_state_load(QEMUFile *f, void *opaque, int version_id); |
| 198 | |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 199 | Slirp *slirp_init(int restricted, struct in_addr vnetwork, |
| 200 | struct in_addr vnetmask, struct in_addr vhost, |
| 201 | const char *vhostname, const char *tftp_path, |
| 202 | const char *bootfile, struct in_addr vdhcp_start, |
Klaus Stengel | 63d2960 | 2012-10-27 19:53:39 +0200 | [diff] [blame] | 203 | struct in_addr vnameserver, const char **vdnssearch, |
| 204 | void *opaque) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 205 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 206 | Slirp *slirp = g_malloc0(sizeof(Slirp)); |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 207 | |
Jan Kiszka | df46189 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 208 | slirp_init_once(); |
bellard | 379ff53 | 2004-07-12 22:33:07 +0000 | [diff] [blame] | 209 | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 210 | slirp->restricted = restricted; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 211 | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 212 | if_init(slirp); |
| 213 | ip_init(slirp); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 214 | |
| 215 | /* Initialise mbufs *after* setting the MTU */ |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 216 | m_init(slirp); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 217 | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 218 | slirp->vnetwork_addr = vnetwork; |
| 219 | slirp->vnetwork_mask = vnetmask; |
| 220 | slirp->vhost_addr = vhost; |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 221 | if (vhostname) { |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 222 | pstrcpy(slirp->client_hostname, sizeof(slirp->client_hostname), |
| 223 | vhostname); |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 224 | } |
Markus Armbruster | c64f50d | 2013-01-22 11:07:57 +0100 | [diff] [blame] | 225 | slirp->tftp_prefix = g_strdup(tftp_path); |
| 226 | slirp->bootp_filename = g_strdup(bootfile); |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 227 | slirp->vdhcp_startaddr = vdhcp_start; |
| 228 | slirp->vnameserver_addr = vnameserver; |
Jan Kiszka | ad196a9 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 229 | |
Klaus Stengel | 63d2960 | 2012-10-27 19:53:39 +0200 | [diff] [blame] | 230 | if (vdnssearch) { |
| 231 | translate_dnssearch(slirp, vdnssearch); |
| 232 | } |
| 233 | |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 234 | slirp->opaque = opaque; |
| 235 | |
Alex Williamson | 0be71e3 | 2010-06-25 11:09:07 -0600 | [diff] [blame] | 236 | register_savevm(NULL, "slirp", 0, 3, |
| 237 | slirp_state_save, slirp_state_load, slirp); |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 238 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 239 | QTAILQ_INSERT_TAIL(&slirp_instances, slirp, entry); |
Jan Kiszka | ad0d8c4 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 240 | |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 241 | return slirp; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Jan Kiszka | ad0d8c4 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 244 | void slirp_cleanup(Slirp *slirp) |
| 245 | { |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 246 | QTAILQ_REMOVE(&slirp_instances, slirp, entry); |
Jan Kiszka | b1c99fc | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 247 | |
Alex Williamson | 0be71e3 | 2010-06-25 11:09:07 -0600 | [diff] [blame] | 248 | unregister_savevm(NULL, "slirp", slirp); |
Jan Kiszka | ad0d8c4 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 249 | |
Jan Kiszka | a68adc2 | 2012-02-29 19:14:23 +0100 | [diff] [blame] | 250 | ip_cleanup(slirp); |
| 251 | m_cleanup(slirp); |
| 252 | |
Klaus Stengel | 63d2960 | 2012-10-27 19:53:39 +0200 | [diff] [blame] | 253 | g_free(slirp->vdnssearch); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 254 | g_free(slirp->tftp_prefix); |
| 255 | g_free(slirp->bootp_filename); |
| 256 | g_free(slirp); |
Jan Kiszka | ad0d8c4 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 257 | } |
| 258 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 259 | #define CONN_CANFSEND(so) (((so)->so_state & (SS_FCANTSENDMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED) |
| 260 | #define CONN_CANFRCV(so) (((so)->so_state & (SS_FCANTRCVMORE|SS_ISFCONNECTED)) == SS_ISFCONNECTED) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 261 | |
Stefano Stabellini | 7c7db75 | 2012-04-13 19:35:04 +0100 | [diff] [blame] | 262 | void slirp_update_timeout(uint32_t *timeout) |
| 263 | { |
| 264 | if (!QTAILQ_EMPTY(&slirp_instances)) { |
| 265 | *timeout = MIN(1000, *timeout); |
| 266 | } |
| 267 | } |
| 268 | |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 269 | void slirp_pollfds_fill(GArray *pollfds) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 270 | { |
Jan Kiszka | b1c99fc | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 271 | Slirp *slirp; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 272 | struct socket *so, *so_next; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 273 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 274 | if (QTAILQ_EMPTY(&slirp_instances)) { |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 275 | return; |
| 276 | } |
| 277 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 278 | /* |
| 279 | * First, TCP sockets |
| 280 | */ |
| 281 | do_slowtimo = 0; |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 282 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 283 | QTAILQ_FOREACH(slirp, &slirp_instances, entry) { |
| 284 | /* |
| 285 | * *_slowtimo needs calling if there are IP fragments |
| 286 | * in the fragment queue, or there are TCP connections active |
| 287 | */ |
| 288 | do_slowtimo |= ((slirp->tcb.so_next != &slirp->tcb) || |
| 289 | (&slirp->ipq.ip_link != slirp->ipq.ip_link.next)); |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 290 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 291 | for (so = slirp->tcb.so_next; so != &slirp->tcb; |
| 292 | so = so_next) { |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 293 | int events = 0; |
| 294 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 295 | so_next = so->so_next; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 296 | |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 297 | so->pollfds_idx = -1; |
| 298 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 299 | /* |
| 300 | * See if we need a tcp_fasttimo |
| 301 | */ |
| 302 | if (time_fasttimo == 0 && so->so_tcpcb->t_flags & TF_DELACK) { |
| 303 | time_fasttimo = curtime; /* Flag when we want a fasttimo */ |
| 304 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 305 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 306 | /* |
| 307 | * NOFDREF can include still connecting to local-host, |
| 308 | * newly socreated() sockets etc. Don't want to select these. |
| 309 | */ |
| 310 | if (so->so_state & SS_NOFDREF || so->s == -1) { |
| 311 | continue; |
| 312 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 313 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 314 | /* |
| 315 | * Set for reading sockets which are accepting |
| 316 | */ |
| 317 | if (so->so_state & SS_FACCEPTCONN) { |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 318 | GPollFD pfd = { |
| 319 | .fd = so->s, |
| 320 | .events = G_IO_IN | G_IO_HUP | G_IO_ERR, |
| 321 | }; |
| 322 | so->pollfds_idx = pollfds->len; |
| 323 | g_array_append_val(pollfds, pfd); |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 324 | continue; |
| 325 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 326 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 327 | /* |
| 328 | * Set for writing sockets which are connecting |
| 329 | */ |
| 330 | if (so->so_state & SS_ISFCONNECTING) { |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 331 | GPollFD pfd = { |
| 332 | .fd = so->s, |
| 333 | .events = G_IO_OUT | G_IO_ERR, |
| 334 | }; |
| 335 | so->pollfds_idx = pollfds->len; |
| 336 | g_array_append_val(pollfds, pfd); |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 337 | continue; |
| 338 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 339 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 340 | /* |
| 341 | * Set for writing if we are connected, can send more, and |
| 342 | * we have something to send |
| 343 | */ |
| 344 | if (CONN_CANFSEND(so) && so->so_rcv.sb_cc) { |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 345 | events |= G_IO_OUT | G_IO_ERR; |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 346 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 347 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 348 | /* |
| 349 | * Set for reading (and urgent data) if we are connected, can |
| 350 | * receive more, and we have room for it XXX /2 ? |
| 351 | */ |
| 352 | if (CONN_CANFRCV(so) && |
| 353 | (so->so_snd.sb_cc < (so->so_snd.sb_datalen/2))) { |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 354 | events |= G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI; |
| 355 | } |
| 356 | |
| 357 | if (events) { |
| 358 | GPollFD pfd = { |
| 359 | .fd = so->s, |
| 360 | .events = events, |
| 361 | }; |
| 362 | so->pollfds_idx = pollfds->len; |
| 363 | g_array_append_val(pollfds, pfd); |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 364 | } |
| 365 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 366 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 367 | /* |
| 368 | * UDP sockets |
| 369 | */ |
| 370 | for (so = slirp->udb.so_next; so != &slirp->udb; |
| 371 | so = so_next) { |
| 372 | so_next = so->so_next; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 373 | |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 374 | so->pollfds_idx = -1; |
| 375 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 376 | /* |
| 377 | * See if it's timed out |
| 378 | */ |
| 379 | if (so->so_expire) { |
| 380 | if (so->so_expire <= curtime) { |
| 381 | udp_detach(so); |
| 382 | continue; |
| 383 | } else { |
| 384 | do_slowtimo = 1; /* Let socket expire */ |
Jan Kiszka | e6d43cf | 2011-07-20 12:20:18 +0200 | [diff] [blame] | 385 | } |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 386 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 387 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 388 | /* |
| 389 | * When UDP packets are received from over the |
| 390 | * link, they're sendto()'d straight away, so |
| 391 | * no need for setting for writing |
| 392 | * Limit the number of packets queued by this session |
| 393 | * to 4. Note that even though we try and limit this |
| 394 | * to 4 packets, the session could have more queued |
| 395 | * if the packets needed to be fragmented |
| 396 | * (XXX <= 4 ?) |
| 397 | */ |
| 398 | if ((so->so_state & SS_ISFCONNECTED) && so->so_queued <= 4) { |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 399 | GPollFD pfd = { |
| 400 | .fd = so->s, |
| 401 | .events = G_IO_IN | G_IO_HUP | G_IO_ERR, |
| 402 | }; |
| 403 | so->pollfds_idx = pollfds->len; |
| 404 | g_array_append_val(pollfds, pfd); |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * ICMP sockets |
| 410 | */ |
| 411 | for (so = slirp->icmp.so_next; so != &slirp->icmp; |
| 412 | so = so_next) { |
| 413 | so_next = so->so_next; |
| 414 | |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 415 | so->pollfds_idx = -1; |
| 416 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 417 | /* |
| 418 | * See if it's timed out |
| 419 | */ |
| 420 | if (so->so_expire) { |
| 421 | if (so->so_expire <= curtime) { |
| 422 | icmp_detach(so); |
| 423 | continue; |
| 424 | } else { |
| 425 | do_slowtimo = 1; /* Let socket expire */ |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | if (so->so_state & SS_ISFCONNECTED) { |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 430 | GPollFD pfd = { |
| 431 | .fd = so->s, |
| 432 | .events = G_IO_IN | G_IO_HUP | G_IO_ERR, |
| 433 | }; |
| 434 | so->pollfds_idx = pollfds->len; |
| 435 | g_array_append_val(pollfds, pfd); |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 439 | } |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 440 | |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 441 | void slirp_pollfds_poll(GArray *pollfds, int select_error) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 442 | { |
Jan Kiszka | b1c99fc | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 443 | Slirp *slirp; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 444 | struct socket *so, *so_next; |
| 445 | int ret; |
| 446 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 447 | if (QTAILQ_EMPTY(&slirp_instances)) { |
Jan Kiszka | d918f23 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 448 | return; |
| 449 | } |
| 450 | |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 451 | curtime = qemu_clock_get_ms(QEMU_CLOCK_REALTIME); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 452 | |
Blue Swirl | 72cf2d4 | 2009-09-12 07:36:22 +0000 | [diff] [blame] | 453 | QTAILQ_FOREACH(slirp, &slirp_instances, entry) { |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 454 | /* |
| 455 | * See if anything has timed out |
| 456 | */ |
| 457 | if (time_fasttimo && ((curtime - time_fasttimo) >= 2)) { |
| 458 | tcp_fasttimo(slirp); |
| 459 | time_fasttimo = 0; |
| 460 | } |
| 461 | if (do_slowtimo && ((curtime - last_slowtimo) >= 499)) { |
| 462 | ip_slowtimo(slirp); |
| 463 | tcp_slowtimo(slirp); |
| 464 | last_slowtimo = curtime; |
| 465 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 466 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 467 | /* |
| 468 | * Check sockets |
| 469 | */ |
| 470 | if (!select_error) { |
| 471 | /* |
| 472 | * Check TCP sockets |
| 473 | */ |
| 474 | for (so = slirp->tcb.so_next; so != &slirp->tcb; |
| 475 | so = so_next) { |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 476 | int revents; |
| 477 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 478 | so_next = so->so_next; |
Jan Kiszka | e6d43cf | 2011-07-20 12:20:18 +0200 | [diff] [blame] | 479 | |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 480 | revents = 0; |
| 481 | if (so->pollfds_idx != -1) { |
| 482 | revents = g_array_index(pollfds, GPollFD, |
| 483 | so->pollfds_idx).revents; |
| 484 | } |
| 485 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 486 | if (so->so_state & SS_NOFDREF || so->s == -1) { |
| 487 | continue; |
| 488 | } |
Jan Kiszka | e6d43cf | 2011-07-20 12:20:18 +0200 | [diff] [blame] | 489 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 490 | /* |
| 491 | * Check for URG data |
| 492 | * This will soread as well, so no need to |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 493 | * test for G_IO_IN below if this succeeds |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 494 | */ |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 495 | if (revents & G_IO_PRI) { |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 496 | sorecvoob(so); |
| 497 | } |
| 498 | /* |
| 499 | * Check sockets for reading |
| 500 | */ |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 501 | else if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) { |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 502 | /* |
| 503 | * Check for incoming connections |
| 504 | */ |
| 505 | if (so->so_state & SS_FACCEPTCONN) { |
| 506 | tcp_connect(so); |
| 507 | continue; |
| 508 | } /* else */ |
| 509 | ret = soread(so); |
| 510 | |
| 511 | /* Output it if we read something */ |
| 512 | if (ret > 0) { |
| 513 | tcp_output(sototcpcb(so)); |
Jan Kiszka | e6d43cf | 2011-07-20 12:20:18 +0200 | [diff] [blame] | 514 | } |
| 515 | } |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 516 | |
| 517 | /* |
| 518 | * Check sockets for writing |
| 519 | */ |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 520 | if (!(so->so_state & SS_NOFDREF) && |
| 521 | (revents & (G_IO_OUT | G_IO_ERR))) { |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 522 | /* |
| 523 | * Check for non-blocking, still-connecting sockets |
| 524 | */ |
| 525 | if (so->so_state & SS_ISFCONNECTING) { |
| 526 | /* Connected */ |
| 527 | so->so_state &= ~SS_ISFCONNECTING; |
| 528 | |
| 529 | ret = send(so->s, (const void *) &ret, 0, 0); |
| 530 | if (ret < 0) { |
| 531 | /* XXXXX Must fix, zero bytes is a NOP */ |
| 532 | if (errno == EAGAIN || errno == EWOULDBLOCK || |
| 533 | errno == EINPROGRESS || errno == ENOTCONN) { |
| 534 | continue; |
| 535 | } |
| 536 | |
| 537 | /* else failed */ |
| 538 | so->so_state &= SS_PERSISTENT_MASK; |
| 539 | so->so_state |= SS_NOFDREF; |
| 540 | } |
| 541 | /* else so->so_state &= ~SS_ISFCONNECTING; */ |
| 542 | |
| 543 | /* |
| 544 | * Continue tcp_input |
| 545 | */ |
| 546 | tcp_input((struct mbuf *)NULL, sizeof(struct ip), so); |
| 547 | /* continue; */ |
| 548 | } else { |
| 549 | ret = sowrite(so); |
| 550 | } |
| 551 | /* |
| 552 | * XXXXX If we wrote something (a lot), there |
| 553 | * could be a need for a window update. |
| 554 | * In the worst case, the remote will send |
| 555 | * a window probe to get things going again |
| 556 | */ |
| 557 | } |
| 558 | |
| 559 | /* |
| 560 | * Probe a still-connecting, non-blocking socket |
| 561 | * to check if it's still alive |
| 562 | */ |
| 563 | #ifdef PROBE_CONN |
| 564 | if (so->so_state & SS_ISFCONNECTING) { |
| 565 | ret = qemu_recv(so->s, &ret, 0, 0); |
| 566 | |
| 567 | if (ret < 0) { |
| 568 | /* XXX */ |
| 569 | if (errno == EAGAIN || errno == EWOULDBLOCK || |
| 570 | errno == EINPROGRESS || errno == ENOTCONN) { |
| 571 | continue; /* Still connecting, continue */ |
| 572 | } |
| 573 | |
| 574 | /* else failed */ |
| 575 | so->so_state &= SS_PERSISTENT_MASK; |
| 576 | so->so_state |= SS_NOFDREF; |
| 577 | |
| 578 | /* tcp_input will take care of it */ |
| 579 | } else { |
| 580 | ret = send(so->s, &ret, 0, 0); |
| 581 | if (ret < 0) { |
| 582 | /* XXX */ |
| 583 | if (errno == EAGAIN || errno == EWOULDBLOCK || |
| 584 | errno == EINPROGRESS || errno == ENOTCONN) { |
| 585 | continue; |
| 586 | } |
| 587 | /* else failed */ |
| 588 | so->so_state &= SS_PERSISTENT_MASK; |
| 589 | so->so_state |= SS_NOFDREF; |
| 590 | } else { |
| 591 | so->so_state &= ~SS_ISFCONNECTING; |
| 592 | } |
| 593 | |
| 594 | } |
| 595 | tcp_input((struct mbuf *)NULL, sizeof(struct ip), so); |
| 596 | } /* SS_ISFCONNECTING */ |
| 597 | #endif |
| 598 | } |
| 599 | |
| 600 | /* |
| 601 | * Now UDP sockets. |
| 602 | * Incoming packets are sent straight away, they're not buffered. |
| 603 | * Incoming UDP data isn't buffered either. |
| 604 | */ |
| 605 | for (so = slirp->udb.so_next; so != &slirp->udb; |
| 606 | so = so_next) { |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 607 | int revents; |
| 608 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 609 | so_next = so->so_next; |
| 610 | |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 611 | revents = 0; |
| 612 | if (so->pollfds_idx != -1) { |
| 613 | revents = g_array_index(pollfds, GPollFD, |
| 614 | so->pollfds_idx).revents; |
| 615 | } |
| 616 | |
| 617 | if (so->s != -1 && |
| 618 | (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) { |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 619 | sorecvfrom(so); |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | /* |
| 624 | * Check incoming ICMP relies. |
| 625 | */ |
| 626 | for (so = slirp->icmp.so_next; so != &slirp->icmp; |
| 627 | so = so_next) { |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 628 | int revents; |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 629 | |
Stefan Hajnoczi | 8917c3b | 2013-02-20 11:28:28 +0100 | [diff] [blame] | 630 | so_next = so->so_next; |
| 631 | |
| 632 | revents = 0; |
| 633 | if (so->pollfds_idx != -1) { |
| 634 | revents = g_array_index(pollfds, GPollFD, |
| 635 | so->pollfds_idx).revents; |
| 636 | } |
| 637 | |
| 638 | if (so->s != -1 && |
| 639 | (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR))) { |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 640 | icmp_receive(so); |
| 641 | } |
| 642 | } |
| 643 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 644 | |
Jan Kiszka | f373431 | 2012-03-06 00:02:23 +0100 | [diff] [blame] | 645 | if_start(slirp); |
Jan Kiszka | b1c99fc | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 646 | } |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 649 | static void arp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 650 | { |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 651 | struct arphdr *ah = (struct arphdr *)(pkt + ETH_HLEN); |
Hervé Poussineau | dbf3c4b | 2010-09-15 22:33:26 +0200 | [diff] [blame] | 652 | uint8_t arp_reply[max(ETH_HLEN + sizeof(struct arphdr), 64)]; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 653 | struct ethhdr *reh = (struct ethhdr *)arp_reply; |
| 654 | struct arphdr *rah = (struct arphdr *)(arp_reply + ETH_HLEN); |
| 655 | int ar_op; |
bellard | a3d4af0 | 2004-09-05 23:10:26 +0000 | [diff] [blame] | 656 | struct ex_list *ex_ptr; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 657 | |
| 658 | ar_op = ntohs(ah->ar_op); |
| 659 | switch(ar_op) { |
| 660 | case ARPOP_REQUEST: |
Fabien Chouteau | 1a0ca1e | 2011-08-03 12:52:54 +0200 | [diff] [blame] | 661 | if (ah->ar_tip == ah->ar_sip) { |
| 662 | /* Gratuitous ARP */ |
| 663 | arp_table_add(slirp, ah->ar_sip, ah->ar_sha); |
| 664 | return; |
| 665 | } |
| 666 | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 667 | if ((ah->ar_tip & slirp->vnetwork_mask.s_addr) == |
| 668 | slirp->vnetwork_addr.s_addr) { |
| 669 | if (ah->ar_tip == slirp->vnameserver_addr.s_addr || |
| 670 | ah->ar_tip == slirp->vhost_addr.s_addr) |
bellard | a3d4af0 | 2004-09-05 23:10:26 +0000 | [diff] [blame] | 671 | goto arp_ok; |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 672 | 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] | 673 | if (ex_ptr->ex_addr.s_addr == ah->ar_tip) |
bellard | a3d4af0 | 2004-09-05 23:10:26 +0000 | [diff] [blame] | 674 | goto arp_ok; |
| 675 | } |
| 676 | return; |
| 677 | arp_ok: |
Hervé Poussineau | dbf3c4b | 2010-09-15 22:33:26 +0200 | [diff] [blame] | 678 | memset(arp_reply, 0, sizeof(arp_reply)); |
Fabien Chouteau | 1a0ca1e | 2011-08-03 12:52:54 +0200 | [diff] [blame] | 679 | |
| 680 | arp_table_add(slirp, ah->ar_sip, ah->ar_sha); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 681 | |
| 682 | /* ARP request for alias/dns mac address */ |
| 683 | memcpy(reh->h_dest, pkt + ETH_ALEN, ETH_ALEN); |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 684 | memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4); |
| 685 | memcpy(&reh->h_source[2], &ah->ar_tip, 4); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 686 | reh->h_proto = htons(ETH_P_ARP); |
| 687 | |
| 688 | rah->ar_hrd = htons(1); |
| 689 | rah->ar_pro = htons(ETH_P_IP); |
| 690 | rah->ar_hln = ETH_ALEN; |
| 691 | rah->ar_pln = 4; |
| 692 | rah->ar_op = htons(ARPOP_REPLY); |
| 693 | memcpy(rah->ar_sha, reh->h_source, ETH_ALEN); |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 694 | rah->ar_sip = ah->ar_tip; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 695 | memcpy(rah->ar_tha, ah->ar_sha, ETH_ALEN); |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 696 | rah->ar_tip = ah->ar_sip; |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 697 | slirp_output(slirp->opaque, arp_reply, sizeof(arp_reply)); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 698 | } |
| 699 | break; |
bellard | de806f0 | 2008-10-17 17:28:58 +0000 | [diff] [blame] | 700 | case ARPOP_REPLY: |
Fabien Chouteau | 1a0ca1e | 2011-08-03 12:52:54 +0200 | [diff] [blame] | 701 | arp_table_add(slirp, ah->ar_sip, ah->ar_sha); |
bellard | de806f0 | 2008-10-17 17:28:58 +0000 | [diff] [blame] | 702 | break; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 703 | default: |
| 704 | break; |
| 705 | } |
| 706 | } |
| 707 | |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 708 | void slirp_input(Slirp *slirp, const uint8_t *pkt, int pkt_len) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 709 | { |
| 710 | struct mbuf *m; |
| 711 | int proto; |
| 712 | |
| 713 | if (pkt_len < ETH_HLEN) |
| 714 | return; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 715 | |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 716 | proto = ntohs(*(uint16_t *)(pkt + 12)); |
| 717 | switch(proto) { |
| 718 | case ETH_P_ARP: |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 719 | arp_input(slirp, pkt, pkt_len); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 720 | break; |
| 721 | case ETH_P_IP: |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 722 | m = m_get(slirp); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 723 | if (!m) |
| 724 | return; |
bellard | 38f3e7c | 2006-05-10 19:21:58 +0000 | [diff] [blame] | 725 | /* Note: we add to align the IP header */ |
aurel32 | e8e880a | 2008-12-07 18:15:23 +0000 | [diff] [blame] | 726 | if (M_FREEROOM(m) < pkt_len + 2) { |
| 727 | m_inc(m, pkt_len + 2); |
| 728 | } |
bellard | 38f3e7c | 2006-05-10 19:21:58 +0000 | [diff] [blame] | 729 | m->m_len = pkt_len + 2; |
| 730 | memcpy(m->m_data + 2, pkt, pkt_len); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 731 | |
bellard | 38f3e7c | 2006-05-10 19:21:58 +0000 | [diff] [blame] | 732 | m->m_data += 2 + ETH_HLEN; |
| 733 | m->m_len -= 2 + ETH_HLEN; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 734 | |
| 735 | ip_input(m); |
| 736 | break; |
| 737 | default: |
| 738 | break; |
| 739 | } |
| 740 | } |
| 741 | |
Fabien Chouteau | 1ab74ce | 2011-08-01 18:18:37 +0200 | [diff] [blame] | 742 | /* Output the IP packet to the ethernet device. Returns 0 if the packet must be |
| 743 | * re-queued. |
| 744 | */ |
| 745 | int if_encap(Slirp *slirp, struct mbuf *ifm) |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 746 | { |
| 747 | uint8_t buf[1600]; |
| 748 | struct ethhdr *eh = (struct ethhdr *)buf; |
Fabien Chouteau | 1a0ca1e | 2011-08-03 12:52:54 +0200 | [diff] [blame] | 749 | uint8_t ethaddr[ETH_ALEN]; |
Fabien Chouteau | 1ab74ce | 2011-08-01 18:18:37 +0200 | [diff] [blame] | 750 | const struct ip *iph = (const struct ip *)ifm->m_data; |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 751 | |
Fabien Chouteau | 1ab74ce | 2011-08-01 18:18:37 +0200 | [diff] [blame] | 752 | if (ifm->m_len + ETH_HLEN > sizeof(buf)) { |
| 753 | return 1; |
| 754 | } |
Fabien Chouteau | 1a0ca1e | 2011-08-03 12:52:54 +0200 | [diff] [blame] | 755 | |
| 756 | if (!arp_table_search(slirp, iph->ip_dst.s_addr, ethaddr)) { |
bellard | de806f0 | 2008-10-17 17:28:58 +0000 | [diff] [blame] | 757 | uint8_t arp_req[ETH_HLEN + sizeof(struct arphdr)]; |
| 758 | struct ethhdr *reh = (struct ethhdr *)arp_req; |
| 759 | struct arphdr *rah = (struct arphdr *)(arp_req + ETH_HLEN); |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 760 | |
Fabien Chouteau | 1ab74ce | 2011-08-01 18:18:37 +0200 | [diff] [blame] | 761 | if (!ifm->arp_requested) { |
| 762 | /* If the client addr is not known, send an ARP request */ |
| 763 | memset(reh->h_dest, 0xff, ETH_ALEN); |
| 764 | memcpy(reh->h_source, special_ethaddr, ETH_ALEN - 4); |
| 765 | memcpy(&reh->h_source[2], &slirp->vhost_addr, 4); |
| 766 | reh->h_proto = htons(ETH_P_ARP); |
| 767 | rah->ar_hrd = htons(1); |
| 768 | rah->ar_pro = htons(ETH_P_IP); |
| 769 | rah->ar_hln = ETH_ALEN; |
| 770 | rah->ar_pln = 4; |
| 771 | rah->ar_op = htons(ARPOP_REQUEST); |
| 772 | |
| 773 | /* source hw addr */ |
| 774 | memcpy(rah->ar_sha, special_ethaddr, ETH_ALEN - 4); |
| 775 | memcpy(&rah->ar_sha[2], &slirp->vhost_addr, 4); |
| 776 | |
| 777 | /* source IP */ |
| 778 | rah->ar_sip = slirp->vhost_addr.s_addr; |
| 779 | |
| 780 | /* target hw addr (none) */ |
| 781 | memset(rah->ar_tha, 0, ETH_ALEN); |
| 782 | |
| 783 | /* target IP */ |
| 784 | rah->ar_tip = iph->ip_dst.s_addr; |
| 785 | slirp->client_ipaddr = iph->ip_dst; |
| 786 | slirp_output(slirp->opaque, arp_req, sizeof(arp_req)); |
| 787 | ifm->arp_requested = true; |
Jan Kiszka | e3a110b | 2011-08-05 14:05:53 +0200 | [diff] [blame] | 788 | |
| 789 | /* Expire request and drop outgoing packet after 1 second */ |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 790 | ifm->expiration_date = qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + 1000000000ULL; |
Fabien Chouteau | 1ab74ce | 2011-08-01 18:18:37 +0200 | [diff] [blame] | 791 | } |
| 792 | return 0; |
bellard | de806f0 | 2008-10-17 17:28:58 +0000 | [diff] [blame] | 793 | } else { |
Fabien Chouteau | 1a0ca1e | 2011-08-03 12:52:54 +0200 | [diff] [blame] | 794 | memcpy(eh->h_dest, ethaddr, ETH_ALEN); |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 795 | memcpy(eh->h_source, special_ethaddr, ETH_ALEN - 4); |
bellard | de806f0 | 2008-10-17 17:28:58 +0000 | [diff] [blame] | 796 | /* XXX: not correct */ |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 797 | memcpy(&eh->h_source[2], &slirp->vhost_addr, 4); |
bellard | de806f0 | 2008-10-17 17:28:58 +0000 | [diff] [blame] | 798 | eh->h_proto = htons(ETH_P_IP); |
Fabien Chouteau | 1ab74ce | 2011-08-01 18:18:37 +0200 | [diff] [blame] | 799 | memcpy(buf + sizeof(struct ethhdr), ifm->m_data, ifm->m_len); |
| 800 | slirp_output(slirp->opaque, buf, ifm->m_len + ETH_HLEN); |
| 801 | return 1; |
bellard | de806f0 | 2008-10-17 17:28:58 +0000 | [diff] [blame] | 802 | } |
bellard | f0cbd3e | 2004-04-22 00:10:48 +0000 | [diff] [blame] | 803 | } |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 804 | |
Jan Kiszka | 9c12a6f | 2009-06-24 14:42:29 +0200 | [diff] [blame] | 805 | /* Drop host forwarding rule, return 0 if found. */ |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 806 | int slirp_remove_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr, |
| 807 | int host_port) |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 808 | { |
| 809 | struct socket *so; |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 810 | struct socket *head = (is_udp ? &slirp->udb : &slirp->tcb); |
Jan Kiszka | 2ad82cf | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 811 | struct sockaddr_in addr; |
| 812 | int port = htons(host_port); |
| 813 | socklen_t addr_len; |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 814 | |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 815 | for (so = head->so_next; so != head; so = so->so_next) { |
Jan Kiszka | 2ad82cf | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 816 | addr_len = sizeof(addr); |
Jan Kiszka | 9c12a6f | 2009-06-24 14:42:29 +0200 | [diff] [blame] | 817 | if ((so->so_state & SS_HOSTFWD) && |
| 818 | getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 && |
Jan Kiszka | 3c6a058 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 819 | addr.sin_addr.s_addr == host_addr.s_addr && |
Jan Kiszka | 2ad82cf | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 820 | addr.sin_port == port) { |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 821 | close(so->s); |
| 822 | sofree(so); |
Jan Kiszka | 9c12a6f | 2009-06-24 14:42:29 +0200 | [diff] [blame] | 823 | return 0; |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 824 | } |
| 825 | } |
| 826 | |
Jan Kiszka | 9c12a6f | 2009-06-24 14:42:29 +0200 | [diff] [blame] | 827 | return -1; |
Alexander Graf | c1261d8 | 2009-05-26 13:03:26 +0200 | [diff] [blame] | 828 | } |
| 829 | |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 830 | int slirp_add_hostfwd(Slirp *slirp, int is_udp, struct in_addr host_addr, |
| 831 | int host_port, struct in_addr guest_addr, int guest_port) |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 832 | { |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 833 | if (!guest_addr.s_addr) { |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 834 | guest_addr = slirp->vdhcp_startaddr; |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 835 | } |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 836 | if (is_udp) { |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 837 | if (!udp_listen(slirp, host_addr.s_addr, htons(host_port), |
| 838 | guest_addr.s_addr, htons(guest_port), SS_HOSTFWD)) |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 839 | return -1; |
| 840 | } else { |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 841 | if (!tcp_listen(slirp, host_addr.s_addr, htons(host_port), |
| 842 | guest_addr.s_addr, htons(guest_port), SS_HOSTFWD)) |
bellard | 9bf0544 | 2004-08-25 22:12:49 +0000 | [diff] [blame] | 843 | return -1; |
| 844 | } |
| 845 | return 0; |
| 846 | } |
bellard | a3d4af0 | 2004-09-05 23:10:26 +0000 | [diff] [blame] | 847 | |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 848 | int slirp_add_exec(Slirp *slirp, int do_pty, const void *args, |
Jan Kiszka | bb53fc5 | 2009-07-22 17:03:52 +0200 | [diff] [blame] | 849 | struct in_addr *guest_addr, int guest_port) |
bellard | a3d4af0 | 2004-09-05 23:10:26 +0000 | [diff] [blame] | 850 | { |
Jan Kiszka | bb53fc5 | 2009-07-22 17:03:52 +0200 | [diff] [blame] | 851 | if (!guest_addr->s_addr) { |
| 852 | guest_addr->s_addr = slirp->vnetwork_addr.s_addr | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 853 | (htonl(0x0204) & ~slirp->vnetwork_mask.s_addr); |
Jan Kiszka | c92ef6a | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 854 | } |
Jan Kiszka | bb53fc5 | 2009-07-22 17:03:52 +0200 | [diff] [blame] | 855 | if ((guest_addr->s_addr & slirp->vnetwork_mask.s_addr) != |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 856 | slirp->vnetwork_addr.s_addr || |
Jan Kiszka | bb53fc5 | 2009-07-22 17:03:52 +0200 | [diff] [blame] | 857 | guest_addr->s_addr == slirp->vhost_addr.s_addr || |
| 858 | guest_addr->s_addr == slirp->vnameserver_addr.s_addr) { |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 859 | return -1; |
| 860 | } |
Jan Kiszka | bb53fc5 | 2009-07-22 17:03:52 +0200 | [diff] [blame] | 861 | return add_exec(&slirp->exec_list, do_pty, (char *)args, *guest_addr, |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 862 | htons(guest_port)); |
bellard | a3d4af0 | 2004-09-05 23:10:26 +0000 | [diff] [blame] | 863 | } |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 864 | |
| 865 | ssize_t slirp_send(struct socket *so, const void *buf, size_t len, int flags) |
| 866 | { |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 867 | if (so->s == -1 && so->extra) { |
| 868 | qemu_chr_fe_write(so->extra, buf, len); |
| 869 | return len; |
| 870 | } |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 871 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 872 | return send(so->s, buf, len, flags); |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 875 | static struct socket * |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 876 | 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] | 877 | { |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 878 | struct socket *so; |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 879 | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 880 | 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] | 881 | if (so->so_faddr.s_addr == guest_addr.s_addr && |
| 882 | htons(so->so_fport) == guest_port) { |
| 883 | return so; |
| 884 | } |
| 885 | } |
| 886 | return NULL; |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 889 | size_t slirp_socket_can_recv(Slirp *slirp, struct in_addr guest_addr, |
| 890 | int guest_port) |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 891 | { |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 892 | struct iovec iov[2]; |
| 893 | struct socket *so; |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 894 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 895 | so = slirp_find_ctl_socket(slirp, guest_addr, guest_port); |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 896 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 897 | if (!so || so->so_state & SS_NOFDREF) { |
| 898 | return 0; |
| 899 | } |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 900 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 901 | if (!CONN_CANFRCV(so) || so->so_snd.sb_cc >= (so->so_snd.sb_datalen/2)) { |
| 902 | return 0; |
| 903 | } |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 904 | |
Stefan Hajnoczi | cf1d078 | 2013-02-20 11:28:27 +0100 | [diff] [blame] | 905 | return sopreprbuf(so, iov, NULL); |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 906 | } |
| 907 | |
Jan Kiszka | 9f8bd04 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 908 | 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] | 909 | const uint8_t *buf, int size) |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 910 | { |
| 911 | int ret; |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 912 | struct socket *so = slirp_find_ctl_socket(slirp, guest_addr, guest_port); |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 913 | |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 914 | if (!so) |
| 915 | return; |
| 916 | |
blueswir1 | 0580ac9 | 2009-01-12 17:51:06 +0000 | [diff] [blame] | 917 | ret = soreadbuf(so, (const char *)buf, size); |
aliguori | e1c5a2b | 2009-01-08 19:18:21 +0000 | [diff] [blame] | 918 | |
| 919 | if (ret > 0) |
| 920 | tcp_output(sototcpcb(so)); |
| 921 | } |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 922 | |
| 923 | static void slirp_tcp_save(QEMUFile *f, struct tcpcb *tp) |
| 924 | { |
| 925 | int i; |
| 926 | |
| 927 | qemu_put_sbe16(f, tp->t_state); |
| 928 | for (i = 0; i < TCPT_NTIMERS; i++) |
| 929 | qemu_put_sbe16(f, tp->t_timer[i]); |
| 930 | qemu_put_sbe16(f, tp->t_rxtshift); |
| 931 | qemu_put_sbe16(f, tp->t_rxtcur); |
| 932 | qemu_put_sbe16(f, tp->t_dupacks); |
| 933 | qemu_put_be16(f, tp->t_maxseg); |
| 934 | qemu_put_sbyte(f, tp->t_force); |
| 935 | qemu_put_be16(f, tp->t_flags); |
| 936 | qemu_put_be32(f, tp->snd_una); |
| 937 | qemu_put_be32(f, tp->snd_nxt); |
| 938 | qemu_put_be32(f, tp->snd_up); |
| 939 | qemu_put_be32(f, tp->snd_wl1); |
| 940 | qemu_put_be32(f, tp->snd_wl2); |
| 941 | qemu_put_be32(f, tp->iss); |
| 942 | qemu_put_be32(f, tp->snd_wnd); |
| 943 | qemu_put_be32(f, tp->rcv_wnd); |
| 944 | qemu_put_be32(f, tp->rcv_nxt); |
| 945 | qemu_put_be32(f, tp->rcv_up); |
| 946 | qemu_put_be32(f, tp->irs); |
| 947 | qemu_put_be32(f, tp->rcv_adv); |
| 948 | qemu_put_be32(f, tp->snd_max); |
| 949 | qemu_put_be32(f, tp->snd_cwnd); |
| 950 | qemu_put_be32(f, tp->snd_ssthresh); |
| 951 | qemu_put_sbe16(f, tp->t_idle); |
| 952 | qemu_put_sbe16(f, tp->t_rtt); |
| 953 | qemu_put_be32(f, tp->t_rtseq); |
| 954 | qemu_put_sbe16(f, tp->t_srtt); |
| 955 | qemu_put_sbe16(f, tp->t_rttvar); |
| 956 | qemu_put_be16(f, tp->t_rttmin); |
| 957 | qemu_put_be32(f, tp->max_sndwnd); |
| 958 | qemu_put_byte(f, tp->t_oobflags); |
| 959 | qemu_put_byte(f, tp->t_iobc); |
| 960 | qemu_put_sbe16(f, tp->t_softerror); |
| 961 | qemu_put_byte(f, tp->snd_scale); |
| 962 | qemu_put_byte(f, tp->rcv_scale); |
| 963 | qemu_put_byte(f, tp->request_r_scale); |
| 964 | qemu_put_byte(f, tp->requested_s_scale); |
| 965 | qemu_put_be32(f, tp->ts_recent); |
| 966 | qemu_put_be32(f, tp->ts_recent_age); |
| 967 | qemu_put_be32(f, tp->last_ack_sent); |
| 968 | } |
| 969 | |
| 970 | static void slirp_sbuf_save(QEMUFile *f, struct sbuf *sbuf) |
| 971 | { |
| 972 | uint32_t off; |
| 973 | |
| 974 | qemu_put_be32(f, sbuf->sb_cc); |
| 975 | qemu_put_be32(f, sbuf->sb_datalen); |
| 976 | off = (uint32_t)(sbuf->sb_wptr - sbuf->sb_data); |
| 977 | qemu_put_sbe32(f, off); |
| 978 | off = (uint32_t)(sbuf->sb_rptr - sbuf->sb_data); |
| 979 | qemu_put_sbe32(f, off); |
| 980 | qemu_put_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen); |
| 981 | } |
| 982 | |
| 983 | static void slirp_socket_save(QEMUFile *f, struct socket *so) |
| 984 | { |
| 985 | qemu_put_be32(f, so->so_urgc); |
| 986 | qemu_put_be32(f, so->so_faddr.s_addr); |
| 987 | qemu_put_be32(f, so->so_laddr.s_addr); |
| 988 | qemu_put_be16(f, so->so_fport); |
| 989 | qemu_put_be16(f, so->so_lport); |
| 990 | qemu_put_byte(f, so->so_iptos); |
| 991 | qemu_put_byte(f, so->so_emu); |
| 992 | qemu_put_byte(f, so->so_type); |
| 993 | qemu_put_be32(f, so->so_state); |
| 994 | slirp_sbuf_save(f, &so->so_rcv); |
| 995 | slirp_sbuf_save(f, &so->so_snd); |
| 996 | slirp_tcp_save(f, so->so_tcpcb); |
| 997 | } |
| 998 | |
Jan Kiszka | 0a1f851 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 999 | static void slirp_bootp_save(QEMUFile *f, Slirp *slirp) |
| 1000 | { |
| 1001 | int i; |
| 1002 | |
| 1003 | for (i = 0; i < NB_BOOTP_CLIENTS; i++) { |
| 1004 | qemu_put_be16(f, slirp->bootp_clients[i].allocated); |
| 1005 | qemu_put_buffer(f, slirp->bootp_clients[i].macaddr, 6); |
| 1006 | } |
| 1007 | } |
| 1008 | |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1009 | static void slirp_state_save(QEMUFile *f, void *opaque) |
| 1010 | { |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1011 | Slirp *slirp = opaque; |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1012 | struct ex_list *ex_ptr; |
| 1013 | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1014 | 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] | 1015 | if (ex_ptr->ex_pty == 3) { |
| 1016 | struct socket *so; |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1017 | so = slirp_find_ctl_socket(slirp, ex_ptr->ex_addr, |
| 1018 | ntohs(ex_ptr->ex_fport)); |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1019 | if (!so) |
| 1020 | continue; |
| 1021 | |
| 1022 | qemu_put_byte(f, 42); |
| 1023 | slirp_socket_save(f, so); |
| 1024 | } |
| 1025 | qemu_put_byte(f, 0); |
Jan Kiszka | 285f7a6 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 1026 | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1027 | qemu_put_be16(f, slirp->ip_id); |
Jan Kiszka | 0a1f851 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1028 | |
| 1029 | slirp_bootp_save(f, slirp); |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
| 1032 | static void slirp_tcp_load(QEMUFile *f, struct tcpcb *tp) |
| 1033 | { |
| 1034 | int i; |
| 1035 | |
| 1036 | tp->t_state = qemu_get_sbe16(f); |
| 1037 | for (i = 0; i < TCPT_NTIMERS; i++) |
| 1038 | tp->t_timer[i] = qemu_get_sbe16(f); |
| 1039 | tp->t_rxtshift = qemu_get_sbe16(f); |
| 1040 | tp->t_rxtcur = qemu_get_sbe16(f); |
| 1041 | tp->t_dupacks = qemu_get_sbe16(f); |
| 1042 | tp->t_maxseg = qemu_get_be16(f); |
| 1043 | tp->t_force = qemu_get_sbyte(f); |
| 1044 | tp->t_flags = qemu_get_be16(f); |
| 1045 | tp->snd_una = qemu_get_be32(f); |
| 1046 | tp->snd_nxt = qemu_get_be32(f); |
| 1047 | tp->snd_up = qemu_get_be32(f); |
| 1048 | tp->snd_wl1 = qemu_get_be32(f); |
| 1049 | tp->snd_wl2 = qemu_get_be32(f); |
| 1050 | tp->iss = qemu_get_be32(f); |
| 1051 | tp->snd_wnd = qemu_get_be32(f); |
| 1052 | tp->rcv_wnd = qemu_get_be32(f); |
| 1053 | tp->rcv_nxt = qemu_get_be32(f); |
| 1054 | tp->rcv_up = qemu_get_be32(f); |
| 1055 | tp->irs = qemu_get_be32(f); |
| 1056 | tp->rcv_adv = qemu_get_be32(f); |
| 1057 | tp->snd_max = qemu_get_be32(f); |
| 1058 | tp->snd_cwnd = qemu_get_be32(f); |
| 1059 | tp->snd_ssthresh = qemu_get_be32(f); |
| 1060 | tp->t_idle = qemu_get_sbe16(f); |
| 1061 | tp->t_rtt = qemu_get_sbe16(f); |
| 1062 | tp->t_rtseq = qemu_get_be32(f); |
| 1063 | tp->t_srtt = qemu_get_sbe16(f); |
| 1064 | tp->t_rttvar = qemu_get_sbe16(f); |
| 1065 | tp->t_rttmin = qemu_get_be16(f); |
| 1066 | tp->max_sndwnd = qemu_get_be32(f); |
| 1067 | tp->t_oobflags = qemu_get_byte(f); |
| 1068 | tp->t_iobc = qemu_get_byte(f); |
| 1069 | tp->t_softerror = qemu_get_sbe16(f); |
| 1070 | tp->snd_scale = qemu_get_byte(f); |
| 1071 | tp->rcv_scale = qemu_get_byte(f); |
| 1072 | tp->request_r_scale = qemu_get_byte(f); |
| 1073 | tp->requested_s_scale = qemu_get_byte(f); |
| 1074 | tp->ts_recent = qemu_get_be32(f); |
| 1075 | tp->ts_recent_age = qemu_get_be32(f); |
| 1076 | tp->last_ack_sent = qemu_get_be32(f); |
| 1077 | tcp_template(tp); |
| 1078 | } |
| 1079 | |
| 1080 | static int slirp_sbuf_load(QEMUFile *f, struct sbuf *sbuf) |
| 1081 | { |
| 1082 | uint32_t off, sb_cc, sb_datalen; |
| 1083 | |
| 1084 | sb_cc = qemu_get_be32(f); |
| 1085 | sb_datalen = qemu_get_be32(f); |
| 1086 | |
| 1087 | sbreserve(sbuf, sb_datalen); |
| 1088 | |
| 1089 | if (sbuf->sb_datalen != sb_datalen) |
| 1090 | return -ENOMEM; |
| 1091 | |
| 1092 | sbuf->sb_cc = sb_cc; |
| 1093 | |
| 1094 | off = qemu_get_sbe32(f); |
| 1095 | sbuf->sb_wptr = sbuf->sb_data + off; |
| 1096 | off = qemu_get_sbe32(f); |
| 1097 | sbuf->sb_rptr = sbuf->sb_data + off; |
| 1098 | qemu_get_buffer(f, (unsigned char*)sbuf->sb_data, sbuf->sb_datalen); |
| 1099 | |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
| 1103 | static int slirp_socket_load(QEMUFile *f, struct socket *so) |
| 1104 | { |
| 1105 | if (tcp_attach(so) < 0) |
| 1106 | return -ENOMEM; |
| 1107 | |
| 1108 | so->so_urgc = qemu_get_be32(f); |
| 1109 | so->so_faddr.s_addr = qemu_get_be32(f); |
| 1110 | so->so_laddr.s_addr = qemu_get_be32(f); |
| 1111 | so->so_fport = qemu_get_be16(f); |
| 1112 | so->so_lport = qemu_get_be16(f); |
| 1113 | so->so_iptos = qemu_get_byte(f); |
| 1114 | so->so_emu = qemu_get_byte(f); |
| 1115 | so->so_type = qemu_get_byte(f); |
| 1116 | so->so_state = qemu_get_be32(f); |
| 1117 | if (slirp_sbuf_load(f, &so->so_rcv) < 0) |
| 1118 | return -ENOMEM; |
| 1119 | if (slirp_sbuf_load(f, &so->so_snd) < 0) |
| 1120 | return -ENOMEM; |
| 1121 | slirp_tcp_load(f, so->so_tcpcb); |
| 1122 | |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
Jan Kiszka | 0a1f851 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1126 | static void slirp_bootp_load(QEMUFile *f, Slirp *slirp) |
| 1127 | { |
| 1128 | int i; |
| 1129 | |
| 1130 | for (i = 0; i < NB_BOOTP_CLIENTS; i++) { |
| 1131 | slirp->bootp_clients[i].allocated = qemu_get_be16(f); |
| 1132 | qemu_get_buffer(f, slirp->bootp_clients[i].macaddr, 6); |
| 1133 | } |
| 1134 | } |
| 1135 | |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1136 | static int slirp_state_load(QEMUFile *f, void *opaque, int version_id) |
| 1137 | { |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1138 | Slirp *slirp = opaque; |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1139 | struct ex_list *ex_ptr; |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1140 | |
Blue Swirl | b0e0486 | 2010-03-07 13:45:38 +0000 | [diff] [blame] | 1141 | while (qemu_get_byte(f)) { |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1142 | int ret; |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1143 | struct socket *so = socreate(slirp); |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1144 | |
| 1145 | if (!so) |
| 1146 | return -ENOMEM; |
| 1147 | |
| 1148 | ret = slirp_socket_load(f, so); |
| 1149 | |
| 1150 | if (ret < 0) |
| 1151 | return ret; |
| 1152 | |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1153 | if ((so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) != |
| 1154 | slirp->vnetwork_addr.s_addr) { |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1155 | return -EINVAL; |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1156 | } |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1157 | 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] | 1158 | if (ex_ptr->ex_pty == 3 && |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1159 | so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr && |
| 1160 | so->so_fport == ex_ptr->ex_fport) { |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1161 | break; |
Jan Kiszka | a13a412 | 2009-06-24 14:42:28 +0200 | [diff] [blame] | 1162 | } |
| 1163 | } |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1164 | if (!ex_ptr) |
| 1165 | return -EINVAL; |
| 1166 | |
blueswir1 | 0580ac9 | 2009-01-12 17:51:06 +0000 | [diff] [blame] | 1167 | so->extra = (void *)ex_ptr->ex_exec; |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Jan Kiszka | 285f7a6 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 1170 | if (version_id >= 2) { |
Jan Kiszka | 460fec6 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1171 | slirp->ip_id = qemu_get_be16(f); |
Jan Kiszka | 285f7a6 | 2009-06-24 14:42:30 +0200 | [diff] [blame] | 1172 | } |
| 1173 | |
Jan Kiszka | 0a1f851 | 2009-06-24 14:42:31 +0200 | [diff] [blame] | 1174 | if (version_id >= 3) { |
| 1175 | slirp_bootp_load(f, slirp); |
| 1176 | } |
| 1177 | |
aliguori | 062e552 | 2009-01-08 19:27:07 +0000 | [diff] [blame] | 1178 | return 0; |
| 1179 | } |