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