1 /* 2 * QEMU System Emulator 3 * 4 * Copyright (c) 2003-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 "config-host.h" 25 26 #include "net/net.h" 27 #include "clients.h" 28 #include "monitor/monitor.h" 29 #include "qemu-common.h" 30 #include "qemu/error-report.h" 31 #include "qemu/option.h" 32 #include "qemu/sockets.h" 33 #include "qemu/iov.h" 34 35 typedef struct NetSocketState { 36 NetClientState nc; 37 int listen_fd; 38 int fd; 39 int state; /* 0 = getting length, 1 = getting data */ 40 unsigned int index; 41 unsigned int packet_len; 42 unsigned int send_index; /* number of bytes sent (only SOCK_STREAM) */ 43 uint8_t buf[4096]; 44 struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */ 45 IOHandler *send_fn; /* differs between SOCK_STREAM/SOCK_DGRAM */ 46 bool read_poll; /* waiting to receive data? */ 47 bool write_poll; /* waiting to transmit data? */ 48 } NetSocketState; 49 50 static void net_socket_accept(void *opaque); 51 static void net_socket_writable(void *opaque); 52 53 /* Only read packets from socket when peer can receive them */ 54 static int net_socket_can_send(void *opaque) 55 { 56 NetSocketState *s = opaque; 57 58 return qemu_can_send_packet(&s->nc); 59 } 60 61 static void net_socket_update_fd_handler(NetSocketState *s) 62 { 63 qemu_set_fd_handler2(s->fd, 64 s->read_poll ? net_socket_can_send : NULL, 65 s->read_poll ? s->send_fn : NULL, 66 s->write_poll ? net_socket_writable : NULL, 67 s); 68 } 69 70 static void net_socket_read_poll(NetSocketState *s, bool enable) 71 { 72 s->read_poll = enable; 73 net_socket_update_fd_handler(s); 74 } 75 76 static void net_socket_write_poll(NetSocketState *s, bool enable) 77 { 78 s->write_poll = enable; 79 net_socket_update_fd_handler(s); 80 } 81 82 static void net_socket_writable(void *opaque) 83 { 84 NetSocketState *s = opaque; 85 86 net_socket_write_poll(s, false); 87 88 qemu_flush_queued_packets(&s->nc); 89 } 90 91 static ssize_t net_socket_receive(NetClientState *nc, const uint8_t *buf, size_t size) 92 { 93 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc); 94 uint32_t len = htonl(size); 95 struct iovec iov[] = { 96 { 97 .iov_base = &len, 98 .iov_len = sizeof(len), 99 }, { 100 .iov_base = (void *)buf, 101 .iov_len = size, 102 }, 103 }; 104 size_t remaining; 105 ssize_t ret; 106 107 remaining = iov_size(iov, 2) - s->send_index; 108 ret = iov_send(s->fd, iov, 2, s->send_index, remaining); 109 110 if (ret == -1 && errno == EAGAIN) { 111 ret = 0; /* handled further down */ 112 } 113 if (ret == -1) { 114 s->send_index = 0; 115 return -errno; 116 } 117 if (ret < (ssize_t)remaining) { 118 s->send_index += ret; 119 net_socket_write_poll(s, true); 120 return 0; 121 } 122 s->send_index = 0; 123 return size; 124 } 125 126 static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, size_t size) 127 { 128 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc); 129 ssize_t ret; 130 131 do { 132 ret = qemu_sendto(s->fd, buf, size, 0, 133 (struct sockaddr *)&s->dgram_dst, 134 sizeof(s->dgram_dst)); 135 } while (ret == -1 && errno == EINTR); 136 137 if (ret == -1 && errno == EAGAIN) { 138 net_socket_write_poll(s, true); 139 return 0; 140 } 141 return ret; 142 } 143 144 static void net_socket_send(void *opaque) 145 { 146 NetSocketState *s = opaque; 147 int size, err; 148 unsigned l; 149 uint8_t buf1[4096]; 150 const uint8_t *buf; 151 152 size = qemu_recv(s->fd, buf1, sizeof(buf1), 0); 153 if (size < 0) { 154 err = socket_error(); 155 if (err != EWOULDBLOCK) 156 goto eoc; 157 } else if (size == 0) { 158 /* end of connection */ 159 eoc: 160 net_socket_read_poll(s, false); 161 net_socket_write_poll(s, false); 162 if (s->listen_fd != -1) { 163 qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s); 164 } 165 closesocket(s->fd); 166 167 s->fd = -1; 168 s->state = 0; 169 s->index = 0; 170 s->packet_len = 0; 171 s->nc.link_down = true; 172 memset(s->buf, 0, sizeof(s->buf)); 173 memset(s->nc.info_str, 0, sizeof(s->nc.info_str)); 174 175 return; 176 } 177 buf = buf1; 178 while (size > 0) { 179 /* reassemble a packet from the network */ 180 switch(s->state) { 181 case 0: 182 l = 4 - s->index; 183 if (l > size) 184 l = size; 185 memcpy(s->buf + s->index, buf, l); 186 buf += l; 187 size -= l; 188 s->index += l; 189 if (s->index == 4) { 190 /* got length */ 191 s->packet_len = ntohl(*(uint32_t *)s->buf); 192 s->index = 0; 193 s->state = 1; 194 } 195 break; 196 case 1: 197 l = s->packet_len - s->index; 198 if (l > size) 199 l = size; 200 if (s->index + l <= sizeof(s->buf)) { 201 memcpy(s->buf + s->index, buf, l); 202 } else { 203 fprintf(stderr, "serious error: oversized packet received," 204 "connection terminated.\n"); 205 s->state = 0; 206 goto eoc; 207 } 208 209 s->index += l; 210 buf += l; 211 size -= l; 212 if (s->index >= s->packet_len) { 213 qemu_send_packet(&s->nc, s->buf, s->packet_len); 214 s->index = 0; 215 s->state = 0; 216 } 217 break; 218 } 219 } 220 } 221 222 static void net_socket_send_dgram(void *opaque) 223 { 224 NetSocketState *s = opaque; 225 int size; 226 227 size = qemu_recv(s->fd, s->buf, sizeof(s->buf), 0); 228 if (size < 0) 229 return; 230 if (size == 0) { 231 /* end of connection */ 232 net_socket_read_poll(s, false); 233 net_socket_write_poll(s, false); 234 return; 235 } 236 qemu_send_packet(&s->nc, s->buf, size); 237 } 238 239 static int net_socket_mcast_create(struct sockaddr_in *mcastaddr, struct in_addr *localaddr) 240 { 241 struct ip_mreq imr; 242 int fd; 243 int val, ret; 244 #ifdef __OpenBSD__ 245 unsigned char loop; 246 #else 247 int loop; 248 #endif 249 250 if (!IN_MULTICAST(ntohl(mcastaddr->sin_addr.s_addr))) { 251 fprintf(stderr, "qemu: error: specified mcastaddr \"%s\" (0x%08x) " 252 "does not contain a multicast address\n", 253 inet_ntoa(mcastaddr->sin_addr), 254 (int)ntohl(mcastaddr->sin_addr.s_addr)); 255 return -1; 256 257 } 258 fd = qemu_socket(PF_INET, SOCK_DGRAM, 0); 259 if (fd < 0) { 260 perror("socket(PF_INET, SOCK_DGRAM)"); 261 return -1; 262 } 263 264 val = 1; 265 ret=setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, 266 (const char *)&val, sizeof(val)); 267 if (ret < 0) { 268 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); 269 goto fail; 270 } 271 272 ret = bind(fd, (struct sockaddr *)mcastaddr, sizeof(*mcastaddr)); 273 if (ret < 0) { 274 perror("bind"); 275 goto fail; 276 } 277 278 /* Add host to multicast group */ 279 imr.imr_multiaddr = mcastaddr->sin_addr; 280 if (localaddr) { 281 imr.imr_interface = *localaddr; 282 } else { 283 imr.imr_interface.s_addr = htonl(INADDR_ANY); 284 } 285 286 ret = setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, 287 (const char *)&imr, sizeof(struct ip_mreq)); 288 if (ret < 0) { 289 perror("setsockopt(IP_ADD_MEMBERSHIP)"); 290 goto fail; 291 } 292 293 /* Force mcast msgs to loopback (eg. several QEMUs in same host */ 294 loop = 1; 295 ret=setsockopt(fd, IPPROTO_IP, IP_MULTICAST_LOOP, 296 (const char *)&loop, sizeof(loop)); 297 if (ret < 0) { 298 perror("setsockopt(SOL_IP, IP_MULTICAST_LOOP)"); 299 goto fail; 300 } 301 302 /* If a bind address is given, only send packets from that address */ 303 if (localaddr != NULL) { 304 ret = setsockopt(fd, IPPROTO_IP, IP_MULTICAST_IF, 305 (const char *)localaddr, sizeof(*localaddr)); 306 if (ret < 0) { 307 perror("setsockopt(IP_MULTICAST_IF)"); 308 goto fail; 309 } 310 } 311 312 socket_set_nonblock(fd); 313 return fd; 314 fail: 315 if (fd >= 0) 316 closesocket(fd); 317 return -1; 318 } 319 320 static void net_socket_cleanup(NetClientState *nc) 321 { 322 NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc); 323 if (s->fd != -1) { 324 net_socket_read_poll(s, false); 325 net_socket_write_poll(s, false); 326 close(s->fd); 327 s->fd = -1; 328 } 329 if (s->listen_fd != -1) { 330 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); 331 closesocket(s->listen_fd); 332 s->listen_fd = -1; 333 } 334 } 335 336 static NetClientInfo net_dgram_socket_info = { 337 .type = NET_CLIENT_OPTIONS_KIND_SOCKET, 338 .size = sizeof(NetSocketState), 339 .receive = net_socket_receive_dgram, 340 .cleanup = net_socket_cleanup, 341 }; 342 343 static NetSocketState *net_socket_fd_init_dgram(NetClientState *peer, 344 const char *model, 345 const char *name, 346 int fd, int is_connected) 347 { 348 struct sockaddr_in saddr; 349 int newfd; 350 socklen_t saddr_len; 351 NetClientState *nc; 352 NetSocketState *s; 353 354 /* fd passed: multicast: "learn" dgram_dst address from bound address and save it 355 * Because this may be "shared" socket from a "master" process, datagrams would be recv() 356 * by ONLY ONE process: we must "clone" this dgram socket --jjo 357 */ 358 359 if (is_connected) { 360 if (getsockname(fd, (struct sockaddr *) &saddr, &saddr_len) == 0) { 361 /* must be bound */ 362 if (saddr.sin_addr.s_addr == 0) { 363 fprintf(stderr, "qemu: error: init_dgram: fd=%d unbound, " 364 "cannot setup multicast dst addr\n", fd); 365 goto err; 366 } 367 /* clone dgram socket */ 368 newfd = net_socket_mcast_create(&saddr, NULL); 369 if (newfd < 0) { 370 /* error already reported by net_socket_mcast_create() */ 371 goto err; 372 } 373 /* clone newfd to fd, close newfd */ 374 dup2(newfd, fd); 375 close(newfd); 376 377 } else { 378 fprintf(stderr, 379 "qemu: error: init_dgram: fd=%d failed getsockname(): %s\n", 380 fd, strerror(errno)); 381 goto err; 382 } 383 } 384 385 nc = qemu_new_net_client(&net_dgram_socket_info, peer, model, name); 386 387 snprintf(nc->info_str, sizeof(nc->info_str), 388 "socket: fd=%d (%s mcast=%s:%d)", 389 fd, is_connected ? "cloned" : "", 390 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); 391 392 s = DO_UPCAST(NetSocketState, nc, nc); 393 394 s->fd = fd; 395 s->listen_fd = -1; 396 s->send_fn = net_socket_send_dgram; 397 net_socket_read_poll(s, true); 398 399 /* mcast: save bound address as dst */ 400 if (is_connected) { 401 s->dgram_dst = saddr; 402 } 403 404 return s; 405 406 err: 407 closesocket(fd); 408 return NULL; 409 } 410 411 static void net_socket_connect(void *opaque) 412 { 413 NetSocketState *s = opaque; 414 s->send_fn = net_socket_send; 415 net_socket_read_poll(s, true); 416 } 417 418 static NetClientInfo net_socket_info = { 419 .type = NET_CLIENT_OPTIONS_KIND_SOCKET, 420 .size = sizeof(NetSocketState), 421 .receive = net_socket_receive, 422 .cleanup = net_socket_cleanup, 423 }; 424 425 static NetSocketState *net_socket_fd_init_stream(NetClientState *peer, 426 const char *model, 427 const char *name, 428 int fd, int is_connected) 429 { 430 NetClientState *nc; 431 NetSocketState *s; 432 433 nc = qemu_new_net_client(&net_socket_info, peer, model, name); 434 435 snprintf(nc->info_str, sizeof(nc->info_str), "socket: fd=%d", fd); 436 437 s = DO_UPCAST(NetSocketState, nc, nc); 438 439 s->fd = fd; 440 s->listen_fd = -1; 441 442 if (is_connected) { 443 net_socket_connect(s); 444 } else { 445 qemu_set_fd_handler(s->fd, NULL, net_socket_connect, s); 446 } 447 return s; 448 } 449 450 static NetSocketState *net_socket_fd_init(NetClientState *peer, 451 const char *model, const char *name, 452 int fd, int is_connected) 453 { 454 int so_type = -1, optlen=sizeof(so_type); 455 456 if(getsockopt(fd, SOL_SOCKET, SO_TYPE, (char *)&so_type, 457 (socklen_t *)&optlen)< 0) { 458 fprintf(stderr, "qemu: error: getsockopt(SO_TYPE) for fd=%d failed\n", 459 fd); 460 closesocket(fd); 461 return NULL; 462 } 463 switch(so_type) { 464 case SOCK_DGRAM: 465 return net_socket_fd_init_dgram(peer, model, name, fd, is_connected); 466 case SOCK_STREAM: 467 return net_socket_fd_init_stream(peer, model, name, fd, is_connected); 468 default: 469 /* who knows ... this could be a eg. a pty, do warn and continue as stream */ 470 fprintf(stderr, "qemu: warning: socket type=%d for fd=%d is not SOCK_DGRAM or SOCK_STREAM\n", so_type, fd); 471 return net_socket_fd_init_stream(peer, model, name, fd, is_connected); 472 } 473 return NULL; 474 } 475 476 static void net_socket_accept(void *opaque) 477 { 478 NetSocketState *s = opaque; 479 struct sockaddr_in saddr; 480 socklen_t len; 481 int fd; 482 483 for(;;) { 484 len = sizeof(saddr); 485 fd = qemu_accept(s->listen_fd, (struct sockaddr *)&saddr, &len); 486 if (fd < 0 && errno != EINTR) { 487 return; 488 } else if (fd >= 0) { 489 qemu_set_fd_handler(s->listen_fd, NULL, NULL, NULL); 490 break; 491 } 492 } 493 494 s->fd = fd; 495 s->nc.link_down = false; 496 net_socket_connect(s); 497 snprintf(s->nc.info_str, sizeof(s->nc.info_str), 498 "socket: connection from %s:%d", 499 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); 500 } 501 502 static int net_socket_listen_init(NetClientState *peer, 503 const char *model, 504 const char *name, 505 const char *host_str) 506 { 507 NetClientState *nc; 508 NetSocketState *s; 509 struct sockaddr_in saddr; 510 int fd, val, ret; 511 512 if (parse_host_port(&saddr, host_str) < 0) 513 return -1; 514 515 fd = qemu_socket(PF_INET, SOCK_STREAM, 0); 516 if (fd < 0) { 517 perror("socket"); 518 return -1; 519 } 520 socket_set_nonblock(fd); 521 522 /* allow fast reuse */ 523 val = 1; 524 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&val, sizeof(val)); 525 526 ret = bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)); 527 if (ret < 0) { 528 perror("bind"); 529 closesocket(fd); 530 return -1; 531 } 532 ret = listen(fd, 0); 533 if (ret < 0) { 534 perror("listen"); 535 closesocket(fd); 536 return -1; 537 } 538 539 nc = qemu_new_net_client(&net_socket_info, peer, model, name); 540 s = DO_UPCAST(NetSocketState, nc, nc); 541 s->fd = -1; 542 s->listen_fd = fd; 543 s->nc.link_down = true; 544 545 qemu_set_fd_handler(s->listen_fd, net_socket_accept, NULL, s); 546 return 0; 547 } 548 549 static int net_socket_connect_init(NetClientState *peer, 550 const char *model, 551 const char *name, 552 const char *host_str) 553 { 554 NetSocketState *s; 555 int fd, connected, ret, err; 556 struct sockaddr_in saddr; 557 558 if (parse_host_port(&saddr, host_str) < 0) 559 return -1; 560 561 fd = qemu_socket(PF_INET, SOCK_STREAM, 0); 562 if (fd < 0) { 563 perror("socket"); 564 return -1; 565 } 566 socket_set_nonblock(fd); 567 568 connected = 0; 569 for(;;) { 570 ret = connect(fd, (struct sockaddr *)&saddr, sizeof(saddr)); 571 if (ret < 0) { 572 err = socket_error(); 573 if (err == EINTR || err == EWOULDBLOCK) { 574 } else if (err == EINPROGRESS) { 575 break; 576 #ifdef _WIN32 577 } else if (err == WSAEALREADY || err == WSAEINVAL) { 578 break; 579 #endif 580 } else { 581 perror("connect"); 582 closesocket(fd); 583 return -1; 584 } 585 } else { 586 connected = 1; 587 break; 588 } 589 } 590 s = net_socket_fd_init(peer, model, name, fd, connected); 591 if (!s) 592 return -1; 593 snprintf(s->nc.info_str, sizeof(s->nc.info_str), 594 "socket: connect to %s:%d", 595 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); 596 return 0; 597 } 598 599 static int net_socket_mcast_init(NetClientState *peer, 600 const char *model, 601 const char *name, 602 const char *host_str, 603 const char *localaddr_str) 604 { 605 NetSocketState *s; 606 int fd; 607 struct sockaddr_in saddr; 608 struct in_addr localaddr, *param_localaddr; 609 610 if (parse_host_port(&saddr, host_str) < 0) 611 return -1; 612 613 if (localaddr_str != NULL) { 614 if (inet_aton(localaddr_str, &localaddr) == 0) 615 return -1; 616 param_localaddr = &localaddr; 617 } else { 618 param_localaddr = NULL; 619 } 620 621 fd = net_socket_mcast_create(&saddr, param_localaddr); 622 if (fd < 0) 623 return -1; 624 625 s = net_socket_fd_init(peer, model, name, fd, 0); 626 if (!s) 627 return -1; 628 629 s->dgram_dst = saddr; 630 631 snprintf(s->nc.info_str, sizeof(s->nc.info_str), 632 "socket: mcast=%s:%d", 633 inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port)); 634 return 0; 635 636 } 637 638 static int net_socket_udp_init(NetClientState *peer, 639 const char *model, 640 const char *name, 641 const char *rhost, 642 const char *lhost) 643 { 644 NetSocketState *s; 645 int fd, val, ret; 646 struct sockaddr_in laddr, raddr; 647 648 if (parse_host_port(&laddr, lhost) < 0) { 649 return -1; 650 } 651 652 if (parse_host_port(&raddr, rhost) < 0) { 653 return -1; 654 } 655 656 fd = qemu_socket(PF_INET, SOCK_DGRAM, 0); 657 if (fd < 0) { 658 perror("socket(PF_INET, SOCK_DGRAM)"); 659 return -1; 660 } 661 val = 1; 662 ret = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, 663 (const char *)&val, sizeof(val)); 664 if (ret < 0) { 665 perror("setsockopt(SOL_SOCKET, SO_REUSEADDR)"); 666 closesocket(fd); 667 return -1; 668 } 669 ret = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr)); 670 if (ret < 0) { 671 perror("bind"); 672 closesocket(fd); 673 return -1; 674 } 675 676 s = net_socket_fd_init(peer, model, name, fd, 0); 677 if (!s) { 678 return -1; 679 } 680 681 s->dgram_dst = raddr; 682 683 snprintf(s->nc.info_str, sizeof(s->nc.info_str), 684 "socket: udp=%s:%d", 685 inet_ntoa(raddr.sin_addr), ntohs(raddr.sin_port)); 686 return 0; 687 } 688 689 int net_init_socket(const NetClientOptions *opts, const char *name, 690 NetClientState *peer) 691 { 692 const NetdevSocketOptions *sock; 693 694 assert(opts->kind == NET_CLIENT_OPTIONS_KIND_SOCKET); 695 sock = opts->socket; 696 697 if (sock->has_fd + sock->has_listen + sock->has_connect + sock->has_mcast + 698 sock->has_udp != 1) { 699 error_report("exactly one of fd=, listen=, connect=, mcast= or udp=" 700 " is required"); 701 return -1; 702 } 703 704 if (sock->has_localaddr && !sock->has_mcast && !sock->has_udp) { 705 error_report("localaddr= is only valid with mcast= or udp="); 706 return -1; 707 } 708 709 if (sock->has_fd) { 710 int fd; 711 712 fd = monitor_handle_fd_param(cur_mon, sock->fd); 713 if (fd == -1 || !net_socket_fd_init(peer, "socket", name, fd, 1)) { 714 return -1; 715 } 716 return 0; 717 } 718 719 if (sock->has_listen) { 720 if (net_socket_listen_init(peer, "socket", name, sock->listen) == -1) { 721 return -1; 722 } 723 return 0; 724 } 725 726 if (sock->has_connect) { 727 if (net_socket_connect_init(peer, "socket", name, sock->connect) == 728 -1) { 729 return -1; 730 } 731 return 0; 732 } 733 734 if (sock->has_mcast) { 735 /* if sock->localaddr is missing, it has been initialized to "all bits 736 * zero" */ 737 if (net_socket_mcast_init(peer, "socket", name, sock->mcast, 738 sock->localaddr) == -1) { 739 return -1; 740 } 741 return 0; 742 } 743 744 assert(sock->has_udp); 745 if (!sock->has_localaddr) { 746 error_report("localaddr= is mandatory with udp="); 747 return -1; 748 } 749 if (net_socket_udp_init(peer, "socket", name, sock->udp, sock->localaddr) == 750 -1) { 751 return -1; 752 } 753 return 0; 754 } 755