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