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