1 /* 2 * inet and unix socket functions for qemu 3 * 4 * (c) 2008 Gerd Hoffmann <kraxel@redhat.com> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; under version 2 of the License. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * Contributions after 2012-01-13 are licensed under the terms of the 16 * GNU GPL, version 2 or (at your option) any later version. 17 */ 18 #include "qemu/osdep.h" 19 20 #ifdef CONFIG_AF_VSOCK 21 #include <linux/vm_sockets.h> 22 #endif /* CONFIG_AF_VSOCK */ 23 24 #include "qemu-common.h" 25 #include "monitor/monitor.h" 26 #include "qapi/clone-visitor.h" 27 #include "qapi/error.h" 28 #include "qapi/qapi-visit-sockets.h" 29 #include "qemu/sockets.h" 30 #include "qemu/main-loop.h" 31 #include "qapi/qobject-input-visitor.h" 32 #include "qapi/qobject-output-visitor.h" 33 #include "qemu/cutils.h" 34 35 #ifndef AI_ADDRCONFIG 36 # define AI_ADDRCONFIG 0 37 #endif 38 39 #ifndef AI_V4MAPPED 40 # define AI_V4MAPPED 0 41 #endif 42 43 #ifndef AI_NUMERICSERV 44 # define AI_NUMERICSERV 0 45 #endif 46 47 48 static int inet_getport(struct addrinfo *e) 49 { 50 struct sockaddr_in *i4; 51 struct sockaddr_in6 *i6; 52 53 switch (e->ai_family) { 54 case PF_INET6: 55 i6 = (void*)e->ai_addr; 56 return ntohs(i6->sin6_port); 57 case PF_INET: 58 i4 = (void*)e->ai_addr; 59 return ntohs(i4->sin_port); 60 default: 61 return 0; 62 } 63 } 64 65 static void inet_setport(struct addrinfo *e, int port) 66 { 67 struct sockaddr_in *i4; 68 struct sockaddr_in6 *i6; 69 70 switch (e->ai_family) { 71 case PF_INET6: 72 i6 = (void*)e->ai_addr; 73 i6->sin6_port = htons(port); 74 break; 75 case PF_INET: 76 i4 = (void*)e->ai_addr; 77 i4->sin_port = htons(port); 78 break; 79 } 80 } 81 82 NetworkAddressFamily inet_netfamily(int family) 83 { 84 switch (family) { 85 case PF_INET6: return NETWORK_ADDRESS_FAMILY_IPV6; 86 case PF_INET: return NETWORK_ADDRESS_FAMILY_IPV4; 87 case PF_UNIX: return NETWORK_ADDRESS_FAMILY_UNIX; 88 #ifdef CONFIG_AF_VSOCK 89 case PF_VSOCK: return NETWORK_ADDRESS_FAMILY_VSOCK; 90 #endif /* CONFIG_AF_VSOCK */ 91 } 92 return NETWORK_ADDRESS_FAMILY_UNKNOWN; 93 } 94 95 bool fd_is_socket(int fd) 96 { 97 int optval; 98 socklen_t optlen = sizeof(optval); 99 return !qemu_getsockopt(fd, SOL_SOCKET, SO_TYPE, &optval, &optlen); 100 } 101 102 103 /* 104 * Matrix we're trying to apply 105 * 106 * ipv4 ipv6 family 107 * - - PF_UNSPEC 108 * - f PF_INET 109 * - t PF_INET6 110 * f - PF_INET6 111 * f f <error> 112 * f t PF_INET6 113 * t - PF_INET 114 * t f PF_INET 115 * t t PF_INET6/PF_UNSPEC 116 * 117 * NB, this matrix is only about getting the necessary results 118 * from getaddrinfo(). Some of the cases require further work 119 * after reading results from getaddrinfo in order to fully 120 * apply the logic the end user wants. 121 * 122 * In the first and last cases, we must set IPV6_V6ONLY=0 123 * when binding, to allow a single listener to potentially 124 * accept both IPv4+6 addresses. 125 */ 126 int inet_ai_family_from_address(InetSocketAddress *addr, 127 Error **errp) 128 { 129 if (addr->has_ipv6 && addr->has_ipv4 && 130 !addr->ipv6 && !addr->ipv4) { 131 error_setg(errp, "Cannot disable IPv4 and IPv6 at same time"); 132 return PF_UNSPEC; 133 } 134 if ((addr->has_ipv6 && addr->ipv6) && (addr->has_ipv4 && addr->ipv4)) { 135 /* 136 * Some backends can only do a single listener. In that case 137 * we want empty hostname to resolve to "::" and then use the 138 * flag IPV6_V6ONLY==0 to get both protocols on 1 socket. This 139 * doesn't work for addresses other than "", so they're just 140 * inevitably broken until multiple listeners can be used, 141 * and thus we honour getaddrinfo automatic protocol detection 142 * Once all backends do multi-listener, remove the PF_INET6 143 * branch entirely. 144 */ 145 if (!addr->host || g_str_equal(addr->host, "")) { 146 return PF_INET6; 147 } else { 148 return PF_UNSPEC; 149 } 150 } 151 if ((addr->has_ipv6 && addr->ipv6) || (addr->has_ipv4 && !addr->ipv4)) { 152 return PF_INET6; 153 } 154 if ((addr->has_ipv4 && addr->ipv4) || (addr->has_ipv6 && !addr->ipv6)) { 155 return PF_INET; 156 } 157 return PF_UNSPEC; 158 } 159 160 static int create_fast_reuse_socket(struct addrinfo *e) 161 { 162 int slisten = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol); 163 if (slisten < 0) { 164 return -1; 165 } 166 socket_set_fast_reuse(slisten); 167 return slisten; 168 } 169 170 static int try_bind(int socket, InetSocketAddress *saddr, struct addrinfo *e) 171 { 172 #ifndef IPV6_V6ONLY 173 return bind(socket, e->ai_addr, e->ai_addrlen); 174 #else 175 /* 176 * Deals with first & last cases in matrix in comment 177 * for inet_ai_family_from_address(). 178 */ 179 int v6only = 180 ((!saddr->has_ipv4 && !saddr->has_ipv6) || 181 (saddr->has_ipv4 && saddr->ipv4 && 182 saddr->has_ipv6 && saddr->ipv6)) ? 0 : 1; 183 int stat; 184 185 rebind: 186 if (e->ai_family == PF_INET6) { 187 qemu_setsockopt(socket, IPPROTO_IPV6, IPV6_V6ONLY, &v6only, 188 sizeof(v6only)); 189 } 190 191 stat = bind(socket, e->ai_addr, e->ai_addrlen); 192 if (!stat) { 193 return 0; 194 } 195 196 /* If we got EADDRINUSE from an IPv6 bind & v6only is unset, 197 * it could be that the IPv4 port is already claimed, so retry 198 * with v6only set 199 */ 200 if (e->ai_family == PF_INET6 && errno == EADDRINUSE && !v6only) { 201 v6only = 1; 202 goto rebind; 203 } 204 return stat; 205 #endif 206 } 207 208 static int inet_listen_saddr(InetSocketAddress *saddr, 209 int port_offset, 210 Error **errp) 211 { 212 struct addrinfo ai,*res,*e; 213 char port[33]; 214 char uaddr[INET6_ADDRSTRLEN+1]; 215 char uport[33]; 216 int rc, port_min, port_max, p; 217 int slisten = -1; 218 int saved_errno = 0; 219 bool socket_created = false; 220 Error *err = NULL; 221 222 if (saddr->keep_alive) { 223 error_setg(errp, "keep-alive option is not supported for passive " 224 "sockets"); 225 return -1; 226 } 227 228 memset(&ai,0, sizeof(ai)); 229 ai.ai_flags = AI_PASSIVE; 230 if (saddr->has_numeric && saddr->numeric) { 231 ai.ai_flags |= AI_NUMERICHOST | AI_NUMERICSERV; 232 } 233 ai.ai_family = inet_ai_family_from_address(saddr, &err); 234 ai.ai_socktype = SOCK_STREAM; 235 236 if (err) { 237 error_propagate(errp, err); 238 return -1; 239 } 240 241 if (saddr->host == NULL) { 242 error_setg(errp, "host not specified"); 243 return -1; 244 } 245 if (saddr->port != NULL) { 246 pstrcpy(port, sizeof(port), saddr->port); 247 } else { 248 port[0] = '\0'; 249 } 250 251 /* lookup */ 252 if (port_offset) { 253 unsigned long long baseport; 254 if (strlen(port) == 0) { 255 error_setg(errp, "port not specified"); 256 return -1; 257 } 258 if (parse_uint_full(port, &baseport, 10) < 0) { 259 error_setg(errp, "can't convert to a number: %s", port); 260 return -1; 261 } 262 if (baseport > 65535 || 263 baseport + port_offset > 65535) { 264 error_setg(errp, "port %s out of range", port); 265 return -1; 266 } 267 snprintf(port, sizeof(port), "%d", (int)baseport + port_offset); 268 } 269 rc = getaddrinfo(strlen(saddr->host) ? saddr->host : NULL, 270 strlen(port) ? port : NULL, &ai, &res); 271 if (rc != 0) { 272 error_setg(errp, "address resolution failed for %s:%s: %s", 273 saddr->host, port, gai_strerror(rc)); 274 return -1; 275 } 276 277 /* create socket + bind/listen */ 278 for (e = res; e != NULL; e = e->ai_next) { 279 getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen, 280 uaddr,INET6_ADDRSTRLEN,uport,32, 281 NI_NUMERICHOST | NI_NUMERICSERV); 282 283 port_min = inet_getport(e); 284 port_max = saddr->has_to ? saddr->to + port_offset : port_min; 285 for (p = port_min; p <= port_max; p++) { 286 inet_setport(e, p); 287 288 slisten = create_fast_reuse_socket(e); 289 if (slisten < 0) { 290 /* First time we expect we might fail to create the socket 291 * eg if 'e' has AF_INET6 but ipv6 kmod is not loaded. 292 * Later iterations should always succeed if first iteration 293 * worked though, so treat that as fatal. 294 */ 295 if (p == port_min) { 296 continue; 297 } else { 298 error_setg_errno(errp, errno, 299 "Failed to recreate failed listening socket"); 300 goto listen_failed; 301 } 302 } 303 socket_created = true; 304 305 rc = try_bind(slisten, saddr, e); 306 if (rc < 0) { 307 if (errno != EADDRINUSE) { 308 error_setg_errno(errp, errno, "Failed to bind socket"); 309 goto listen_failed; 310 } 311 } else { 312 if (!listen(slisten, 1)) { 313 goto listen_ok; 314 } 315 if (errno != EADDRINUSE) { 316 error_setg_errno(errp, errno, "Failed to listen on socket"); 317 goto listen_failed; 318 } 319 } 320 /* Someone else managed to bind to the same port and beat us 321 * to listen on it! Socket semantics does not allow us to 322 * recover from this situation, so we need to recreate the 323 * socket to allow bind attempts for subsequent ports: 324 */ 325 closesocket(slisten); 326 slisten = -1; 327 } 328 } 329 error_setg_errno(errp, errno, 330 socket_created ? 331 "Failed to find an available port" : 332 "Failed to create a socket"); 333 listen_failed: 334 saved_errno = errno; 335 if (slisten >= 0) { 336 closesocket(slisten); 337 } 338 freeaddrinfo(res); 339 errno = saved_errno; 340 return -1; 341 342 listen_ok: 343 freeaddrinfo(res); 344 return slisten; 345 } 346 347 #ifdef _WIN32 348 #define QEMU_SOCKET_RC_INPROGRESS(rc) \ 349 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY) 350 #else 351 #define QEMU_SOCKET_RC_INPROGRESS(rc) \ 352 ((rc) == -EINPROGRESS) 353 #endif 354 355 static int inet_connect_addr(struct addrinfo *addr, Error **errp); 356 357 static int inet_connect_addr(struct addrinfo *addr, Error **errp) 358 { 359 int sock, rc; 360 361 sock = qemu_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); 362 if (sock < 0) { 363 error_setg_errno(errp, errno, "Failed to create socket"); 364 return -1; 365 } 366 socket_set_fast_reuse(sock); 367 368 /* connect to peer */ 369 do { 370 rc = 0; 371 if (connect(sock, addr->ai_addr, addr->ai_addrlen) < 0) { 372 rc = -errno; 373 } 374 } while (rc == -EINTR); 375 376 if (rc < 0) { 377 error_setg_errno(errp, errno, "Failed to connect socket"); 378 closesocket(sock); 379 return -1; 380 } 381 382 return sock; 383 } 384 385 static struct addrinfo *inet_parse_connect_saddr(InetSocketAddress *saddr, 386 Error **errp) 387 { 388 struct addrinfo ai, *res; 389 int rc; 390 Error *err = NULL; 391 static int useV4Mapped = 1; 392 393 memset(&ai, 0, sizeof(ai)); 394 395 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG; 396 if (atomic_read(&useV4Mapped)) { 397 ai.ai_flags |= AI_V4MAPPED; 398 } 399 ai.ai_family = inet_ai_family_from_address(saddr, &err); 400 ai.ai_socktype = SOCK_STREAM; 401 402 if (err) { 403 error_propagate(errp, err); 404 return NULL; 405 } 406 407 if (saddr->host == NULL || saddr->port == NULL) { 408 error_setg(errp, "host and/or port not specified"); 409 return NULL; 410 } 411 412 /* lookup */ 413 rc = getaddrinfo(saddr->host, saddr->port, &ai, &res); 414 415 /* At least FreeBSD and OS-X 10.6 declare AI_V4MAPPED but 416 * then don't implement it in their getaddrinfo(). Detect 417 * this and retry without the flag since that's preferrable 418 * to a fatal error 419 */ 420 if (rc == EAI_BADFLAGS && 421 (ai.ai_flags & AI_V4MAPPED)) { 422 atomic_set(&useV4Mapped, 0); 423 ai.ai_flags &= ~AI_V4MAPPED; 424 rc = getaddrinfo(saddr->host, saddr->port, &ai, &res); 425 } 426 if (rc != 0) { 427 error_setg(errp, "address resolution failed for %s:%s: %s", 428 saddr->host, saddr->port, gai_strerror(rc)); 429 return NULL; 430 } 431 return res; 432 } 433 434 /** 435 * Create a socket and connect it to an address. 436 * 437 * @saddr: Inet socket address specification 438 * @errp: set on error 439 * 440 * Returns: -1 on error, file descriptor on success. 441 */ 442 int inet_connect_saddr(InetSocketAddress *saddr, Error **errp) 443 { 444 Error *local_err = NULL; 445 struct addrinfo *res, *e; 446 int sock = -1; 447 448 res = inet_parse_connect_saddr(saddr, errp); 449 if (!res) { 450 return -1; 451 } 452 453 for (e = res; e != NULL; e = e->ai_next) { 454 error_free(local_err); 455 local_err = NULL; 456 sock = inet_connect_addr(e, &local_err); 457 if (sock >= 0) { 458 break; 459 } 460 } 461 462 if (sock < 0) { 463 error_propagate(errp, local_err); 464 } 465 466 freeaddrinfo(res); 467 468 if (saddr->keep_alive) { 469 int val = 1; 470 int ret = qemu_setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, 471 &val, sizeof(val)); 472 473 if (ret < 0) { 474 error_setg_errno(errp, errno, "Unable to set KEEPALIVE"); 475 close(sock); 476 return -1; 477 } 478 } 479 480 return sock; 481 } 482 483 static int inet_dgram_saddr(InetSocketAddress *sraddr, 484 InetSocketAddress *sladdr, 485 Error **errp) 486 { 487 struct addrinfo ai, *peer = NULL, *local = NULL; 488 const char *addr; 489 const char *port; 490 int sock = -1, rc; 491 Error *err = NULL; 492 493 /* lookup peer addr */ 494 memset(&ai,0, sizeof(ai)); 495 ai.ai_flags = AI_CANONNAME | AI_V4MAPPED | AI_ADDRCONFIG; 496 ai.ai_family = inet_ai_family_from_address(sraddr, &err); 497 ai.ai_socktype = SOCK_DGRAM; 498 499 if (err) { 500 error_propagate(errp, err); 501 goto err; 502 } 503 504 addr = sraddr->host; 505 port = sraddr->port; 506 if (addr == NULL || strlen(addr) == 0) { 507 addr = "localhost"; 508 } 509 if (port == NULL || strlen(port) == 0) { 510 error_setg(errp, "remote port not specified"); 511 goto err; 512 } 513 514 if ((rc = getaddrinfo(addr, port, &ai, &peer)) != 0) { 515 error_setg(errp, "address resolution failed for %s:%s: %s", addr, port, 516 gai_strerror(rc)); 517 goto err; 518 } 519 520 /* lookup local addr */ 521 memset(&ai,0, sizeof(ai)); 522 ai.ai_flags = AI_PASSIVE; 523 ai.ai_family = peer->ai_family; 524 ai.ai_socktype = SOCK_DGRAM; 525 526 if (sladdr) { 527 addr = sladdr->host; 528 port = sladdr->port; 529 if (addr == NULL || strlen(addr) == 0) { 530 addr = NULL; 531 } 532 if (!port || strlen(port) == 0) { 533 port = "0"; 534 } 535 } else { 536 addr = NULL; 537 port = "0"; 538 } 539 540 if ((rc = getaddrinfo(addr, port, &ai, &local)) != 0) { 541 error_setg(errp, "address resolution failed for %s:%s: %s", addr, port, 542 gai_strerror(rc)); 543 goto err; 544 } 545 546 /* create socket */ 547 sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol); 548 if (sock < 0) { 549 error_setg_errno(errp, errno, "Failed to create socket"); 550 goto err; 551 } 552 socket_set_fast_reuse(sock); 553 554 /* bind socket */ 555 if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) { 556 error_setg_errno(errp, errno, "Failed to bind socket"); 557 goto err; 558 } 559 560 /* connect to peer */ 561 if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) { 562 error_setg_errno(errp, errno, "Failed to connect socket"); 563 goto err; 564 } 565 566 freeaddrinfo(local); 567 freeaddrinfo(peer); 568 return sock; 569 570 err: 571 if (sock != -1) { 572 closesocket(sock); 573 } 574 if (local) { 575 freeaddrinfo(local); 576 } 577 if (peer) { 578 freeaddrinfo(peer); 579 } 580 581 return -1; 582 } 583 584 /* compatibility wrapper */ 585 static int inet_parse_flag(const char *flagname, const char *optstr, bool *val, 586 Error **errp) 587 { 588 char *end; 589 size_t len; 590 591 end = strstr(optstr, ","); 592 if (end) { 593 if (end[1] == ',') { /* Reject 'ipv6=on,,foo' */ 594 error_setg(errp, "error parsing '%s' flag '%s'", flagname, optstr); 595 return -1; 596 } 597 len = end - optstr; 598 } else { 599 len = strlen(optstr); 600 } 601 if (len == 0 || (len == 3 && strncmp(optstr, "=on", len) == 0)) { 602 *val = true; 603 } else if (len == 4 && strncmp(optstr, "=off", len) == 0) { 604 *val = false; 605 } else { 606 error_setg(errp, "error parsing '%s' flag '%s'", flagname, optstr); 607 return -1; 608 } 609 return 0; 610 } 611 612 int inet_parse(InetSocketAddress *addr, const char *str, Error **errp) 613 { 614 const char *optstr, *h; 615 char host[65]; 616 char port[33]; 617 int to; 618 int pos; 619 char *begin; 620 621 memset(addr, 0, sizeof(*addr)); 622 623 /* parse address */ 624 if (str[0] == ':') { 625 /* no host given */ 626 host[0] = '\0'; 627 if (sscanf(str, ":%32[^,]%n", port, &pos) != 1) { 628 error_setg(errp, "error parsing port in address '%s'", str); 629 return -1; 630 } 631 } else if (str[0] == '[') { 632 /* IPv6 addr */ 633 if (sscanf(str, "[%64[^]]]:%32[^,]%n", host, port, &pos) != 2) { 634 error_setg(errp, "error parsing IPv6 address '%s'", str); 635 return -1; 636 } 637 } else { 638 /* hostname or IPv4 addr */ 639 if (sscanf(str, "%64[^:]:%32[^,]%n", host, port, &pos) != 2) { 640 error_setg(errp, "error parsing address '%s'", str); 641 return -1; 642 } 643 } 644 645 addr->host = g_strdup(host); 646 addr->port = g_strdup(port); 647 648 /* parse options */ 649 optstr = str + pos; 650 h = strstr(optstr, ",to="); 651 if (h) { 652 h += 4; 653 if (sscanf(h, "%d%n", &to, &pos) != 1 || 654 (h[pos] != '\0' && h[pos] != ',')) { 655 error_setg(errp, "error parsing to= argument"); 656 return -1; 657 } 658 addr->has_to = true; 659 addr->to = to; 660 } 661 begin = strstr(optstr, ",ipv4"); 662 if (begin) { 663 if (inet_parse_flag("ipv4", begin + 5, &addr->ipv4, errp) < 0) { 664 return -1; 665 } 666 addr->has_ipv4 = true; 667 } 668 begin = strstr(optstr, ",ipv6"); 669 if (begin) { 670 if (inet_parse_flag("ipv6", begin + 5, &addr->ipv6, errp) < 0) { 671 return -1; 672 } 673 addr->has_ipv6 = true; 674 } 675 begin = strstr(optstr, ",keep-alive"); 676 if (begin) { 677 if (inet_parse_flag("keep-alive", begin + strlen(",keep-alive"), 678 &addr->keep_alive, errp) < 0) 679 { 680 return -1; 681 } 682 addr->has_keep_alive = true; 683 } 684 return 0; 685 } 686 687 688 /** 689 * Create a blocking socket and connect it to an address. 690 * 691 * @str: address string 692 * @errp: set in case of an error 693 * 694 * Returns -1 in case of error, file descriptor on success 695 **/ 696 int inet_connect(const char *str, Error **errp) 697 { 698 int sock = -1; 699 InetSocketAddress *addr = g_new(InetSocketAddress, 1); 700 701 if (!inet_parse(addr, str, errp)) { 702 sock = inet_connect_saddr(addr, errp); 703 } 704 qapi_free_InetSocketAddress(addr); 705 return sock; 706 } 707 708 #ifdef CONFIG_AF_VSOCK 709 static bool vsock_parse_vaddr_to_sockaddr(const VsockSocketAddress *vaddr, 710 struct sockaddr_vm *svm, 711 Error **errp) 712 { 713 unsigned long long val; 714 715 memset(svm, 0, sizeof(*svm)); 716 svm->svm_family = AF_VSOCK; 717 718 if (parse_uint_full(vaddr->cid, &val, 10) < 0 || 719 val > UINT32_MAX) { 720 error_setg(errp, "Failed to parse cid '%s'", vaddr->cid); 721 return false; 722 } 723 svm->svm_cid = val; 724 725 if (parse_uint_full(vaddr->port, &val, 10) < 0 || 726 val > UINT32_MAX) { 727 error_setg(errp, "Failed to parse port '%s'", vaddr->port); 728 return false; 729 } 730 svm->svm_port = val; 731 732 return true; 733 } 734 735 static int vsock_connect_addr(const struct sockaddr_vm *svm, Error **errp) 736 { 737 int sock, rc; 738 739 sock = qemu_socket(AF_VSOCK, SOCK_STREAM, 0); 740 if (sock < 0) { 741 error_setg_errno(errp, errno, "Failed to create socket"); 742 return -1; 743 } 744 745 /* connect to peer */ 746 do { 747 rc = 0; 748 if (connect(sock, (const struct sockaddr *)svm, sizeof(*svm)) < 0) { 749 rc = -errno; 750 } 751 } while (rc == -EINTR); 752 753 if (rc < 0) { 754 error_setg_errno(errp, errno, "Failed to connect socket"); 755 closesocket(sock); 756 return -1; 757 } 758 759 return sock; 760 } 761 762 static int vsock_connect_saddr(VsockSocketAddress *vaddr, Error **errp) 763 { 764 struct sockaddr_vm svm; 765 int sock = -1; 766 767 if (!vsock_parse_vaddr_to_sockaddr(vaddr, &svm, errp)) { 768 return -1; 769 } 770 771 sock = vsock_connect_addr(&svm, errp); 772 773 return sock; 774 } 775 776 static int vsock_listen_saddr(VsockSocketAddress *vaddr, 777 Error **errp) 778 { 779 struct sockaddr_vm svm; 780 int slisten; 781 782 if (!vsock_parse_vaddr_to_sockaddr(vaddr, &svm, errp)) { 783 return -1; 784 } 785 786 slisten = qemu_socket(AF_VSOCK, SOCK_STREAM, 0); 787 if (slisten < 0) { 788 error_setg_errno(errp, errno, "Failed to create socket"); 789 return -1; 790 } 791 792 if (bind(slisten, (const struct sockaddr *)&svm, sizeof(svm)) != 0) { 793 error_setg_errno(errp, errno, "Failed to bind socket"); 794 closesocket(slisten); 795 return -1; 796 } 797 798 if (listen(slisten, 1) != 0) { 799 error_setg_errno(errp, errno, "Failed to listen on socket"); 800 closesocket(slisten); 801 return -1; 802 } 803 return slisten; 804 } 805 806 static int vsock_parse(VsockSocketAddress *addr, const char *str, 807 Error **errp) 808 { 809 char cid[33]; 810 char port[33]; 811 int n; 812 813 if (sscanf(str, "%32[^:]:%32[^,]%n", cid, port, &n) != 2) { 814 error_setg(errp, "error parsing address '%s'", str); 815 return -1; 816 } 817 if (str[n] != '\0') { 818 error_setg(errp, "trailing characters in address '%s'", str); 819 return -1; 820 } 821 822 addr->cid = g_strdup(cid); 823 addr->port = g_strdup(port); 824 return 0; 825 } 826 #else 827 static void vsock_unsupported(Error **errp) 828 { 829 error_setg(errp, "socket family AF_VSOCK unsupported"); 830 } 831 832 static int vsock_connect_saddr(VsockSocketAddress *vaddr, Error **errp) 833 { 834 vsock_unsupported(errp); 835 return -1; 836 } 837 838 static int vsock_listen_saddr(VsockSocketAddress *vaddr, 839 Error **errp) 840 { 841 vsock_unsupported(errp); 842 return -1; 843 } 844 845 static int vsock_parse(VsockSocketAddress *addr, const char *str, 846 Error **errp) 847 { 848 vsock_unsupported(errp); 849 return -1; 850 } 851 #endif /* CONFIG_AF_VSOCK */ 852 853 #ifndef _WIN32 854 855 static int unix_listen_saddr(UnixSocketAddress *saddr, 856 Error **errp) 857 { 858 struct sockaddr_un un; 859 int sock, fd; 860 char *pathbuf = NULL; 861 const char *path; 862 size_t pathlen; 863 864 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0); 865 if (sock < 0) { 866 error_setg_errno(errp, errno, "Failed to create Unix socket"); 867 return -1; 868 } 869 870 if (saddr->path && saddr->path[0]) { 871 path = saddr->path; 872 } else { 873 const char *tmpdir = getenv("TMPDIR"); 874 tmpdir = tmpdir ? tmpdir : "/tmp"; 875 path = pathbuf = g_strdup_printf("%s/qemu-socket-XXXXXX", tmpdir); 876 } 877 878 pathlen = strlen(path); 879 if (pathlen > sizeof(un.sun_path)) { 880 error_setg(errp, "UNIX socket path '%s' is too long", path); 881 error_append_hint(errp, "Path must be less than %zu bytes\n", 882 sizeof(un.sun_path)); 883 goto err; 884 } 885 886 if (pathbuf != NULL) { 887 /* 888 * This dummy fd usage silences the mktemp() unsecure warning. 889 * Using mkstemp() doesn't make things more secure here 890 * though. bind() complains about existing files, so we have 891 * to unlink first and thus re-open the race window. The 892 * worst case possible is bind() failing, i.e. a DoS attack. 893 */ 894 fd = mkstemp(pathbuf); 895 if (fd < 0) { 896 error_setg_errno(errp, errno, 897 "Failed to make a temporary socket %s", pathbuf); 898 goto err; 899 } 900 close(fd); 901 } 902 903 if (unlink(path) < 0 && errno != ENOENT) { 904 error_setg_errno(errp, errno, 905 "Failed to unlink socket %s", path); 906 goto err; 907 } 908 909 memset(&un, 0, sizeof(un)); 910 un.sun_family = AF_UNIX; 911 memcpy(un.sun_path, path, pathlen); 912 913 if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) { 914 error_setg_errno(errp, errno, "Failed to bind socket to %s", path); 915 goto err; 916 } 917 if (listen(sock, 1) < 0) { 918 error_setg_errno(errp, errno, "Failed to listen on socket"); 919 goto err; 920 } 921 922 g_free(pathbuf); 923 return sock; 924 925 err: 926 g_free(pathbuf); 927 closesocket(sock); 928 return -1; 929 } 930 931 static int unix_connect_saddr(UnixSocketAddress *saddr, Error **errp) 932 { 933 struct sockaddr_un un; 934 int sock, rc; 935 size_t pathlen; 936 937 if (saddr->path == NULL) { 938 error_setg(errp, "unix connect: no path specified"); 939 return -1; 940 } 941 942 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0); 943 if (sock < 0) { 944 error_setg_errno(errp, errno, "Failed to create socket"); 945 return -1; 946 } 947 948 pathlen = strlen(saddr->path); 949 if (pathlen > sizeof(un.sun_path)) { 950 error_setg(errp, "UNIX socket path '%s' is too long", saddr->path); 951 error_append_hint(errp, "Path must be less than %zu bytes\n", 952 sizeof(un.sun_path)); 953 goto err; 954 } 955 956 memset(&un, 0, sizeof(un)); 957 un.sun_family = AF_UNIX; 958 memcpy(un.sun_path, saddr->path, pathlen); 959 960 /* connect to peer */ 961 do { 962 rc = 0; 963 if (connect(sock, (struct sockaddr *) &un, sizeof(un)) < 0) { 964 rc = -errno; 965 } 966 } while (rc == -EINTR); 967 968 if (rc < 0) { 969 error_setg_errno(errp, -rc, "Failed to connect socket %s", 970 saddr->path); 971 goto err; 972 } 973 974 return sock; 975 976 err: 977 close(sock); 978 return -1; 979 } 980 981 #else 982 983 static int unix_listen_saddr(UnixSocketAddress *saddr, 984 Error **errp) 985 { 986 error_setg(errp, "unix sockets are not available on windows"); 987 errno = ENOTSUP; 988 return -1; 989 } 990 991 static int unix_connect_saddr(UnixSocketAddress *saddr, Error **errp) 992 { 993 error_setg(errp, "unix sockets are not available on windows"); 994 errno = ENOTSUP; 995 return -1; 996 } 997 #endif 998 999 /* compatibility wrapper */ 1000 int unix_listen(const char *str, Error **errp) 1001 { 1002 UnixSocketAddress *saddr; 1003 int sock; 1004 1005 saddr = g_new0(UnixSocketAddress, 1); 1006 saddr->path = g_strdup(str); 1007 sock = unix_listen_saddr(saddr, errp); 1008 qapi_free_UnixSocketAddress(saddr); 1009 return sock; 1010 } 1011 1012 int unix_connect(const char *path, Error **errp) 1013 { 1014 UnixSocketAddress *saddr; 1015 int sock; 1016 1017 saddr = g_new0(UnixSocketAddress, 1); 1018 saddr->path = g_strdup(path); 1019 sock = unix_connect_saddr(saddr, errp); 1020 qapi_free_UnixSocketAddress(saddr); 1021 return sock; 1022 } 1023 1024 1025 SocketAddress *socket_parse(const char *str, Error **errp) 1026 { 1027 SocketAddress *addr; 1028 1029 addr = g_new0(SocketAddress, 1); 1030 if (strstart(str, "unix:", NULL)) { 1031 if (str[5] == '\0') { 1032 error_setg(errp, "invalid Unix socket address"); 1033 goto fail; 1034 } else { 1035 addr->type = SOCKET_ADDRESS_TYPE_UNIX; 1036 addr->u.q_unix.path = g_strdup(str + 5); 1037 } 1038 } else if (strstart(str, "fd:", NULL)) { 1039 if (str[3] == '\0') { 1040 error_setg(errp, "invalid file descriptor address"); 1041 goto fail; 1042 } else { 1043 addr->type = SOCKET_ADDRESS_TYPE_FD; 1044 addr->u.fd.str = g_strdup(str + 3); 1045 } 1046 } else if (strstart(str, "vsock:", NULL)) { 1047 addr->type = SOCKET_ADDRESS_TYPE_VSOCK; 1048 if (vsock_parse(&addr->u.vsock, str + strlen("vsock:"), errp)) { 1049 goto fail; 1050 } 1051 } else { 1052 addr->type = SOCKET_ADDRESS_TYPE_INET; 1053 if (inet_parse(&addr->u.inet, str, errp)) { 1054 goto fail; 1055 } 1056 } 1057 return addr; 1058 1059 fail: 1060 qapi_free_SocketAddress(addr); 1061 return NULL; 1062 } 1063 1064 static int socket_get_fd(const char *fdstr, Error **errp) 1065 { 1066 int fd; 1067 if (cur_mon) { 1068 fd = monitor_get_fd(cur_mon, fdstr, errp); 1069 if (fd < 0) { 1070 return -1; 1071 } 1072 } else { 1073 if (qemu_strtoi(fdstr, NULL, 10, &fd) < 0) { 1074 error_setg_errno(errp, errno, 1075 "Unable to parse FD number %s", 1076 fdstr); 1077 return -1; 1078 } 1079 } 1080 if (!fd_is_socket(fd)) { 1081 error_setg(errp, "File descriptor '%s' is not a socket", fdstr); 1082 close(fd); 1083 return -1; 1084 } 1085 return fd; 1086 } 1087 1088 int socket_connect(SocketAddress *addr, Error **errp) 1089 { 1090 int fd; 1091 1092 switch (addr->type) { 1093 case SOCKET_ADDRESS_TYPE_INET: 1094 fd = inet_connect_saddr(&addr->u.inet, errp); 1095 break; 1096 1097 case SOCKET_ADDRESS_TYPE_UNIX: 1098 fd = unix_connect_saddr(&addr->u.q_unix, errp); 1099 break; 1100 1101 case SOCKET_ADDRESS_TYPE_FD: 1102 fd = socket_get_fd(addr->u.fd.str, errp); 1103 break; 1104 1105 case SOCKET_ADDRESS_TYPE_VSOCK: 1106 fd = vsock_connect_saddr(&addr->u.vsock, errp); 1107 break; 1108 1109 default: 1110 abort(); 1111 } 1112 return fd; 1113 } 1114 1115 int socket_listen(SocketAddress *addr, Error **errp) 1116 { 1117 int fd; 1118 1119 switch (addr->type) { 1120 case SOCKET_ADDRESS_TYPE_INET: 1121 fd = inet_listen_saddr(&addr->u.inet, 0, errp); 1122 break; 1123 1124 case SOCKET_ADDRESS_TYPE_UNIX: 1125 fd = unix_listen_saddr(&addr->u.q_unix, errp); 1126 break; 1127 1128 case SOCKET_ADDRESS_TYPE_FD: 1129 fd = socket_get_fd(addr->u.fd.str, errp); 1130 break; 1131 1132 case SOCKET_ADDRESS_TYPE_VSOCK: 1133 fd = vsock_listen_saddr(&addr->u.vsock, errp); 1134 break; 1135 1136 default: 1137 abort(); 1138 } 1139 return fd; 1140 } 1141 1142 void socket_listen_cleanup(int fd, Error **errp) 1143 { 1144 SocketAddress *addr; 1145 1146 addr = socket_local_address(fd, errp); 1147 if (!addr) { 1148 return; 1149 } 1150 1151 if (addr->type == SOCKET_ADDRESS_TYPE_UNIX 1152 && addr->u.q_unix.path) { 1153 if (unlink(addr->u.q_unix.path) < 0 && errno != ENOENT) { 1154 error_setg_errno(errp, errno, 1155 "Failed to unlink socket %s", 1156 addr->u.q_unix.path); 1157 } 1158 } 1159 1160 qapi_free_SocketAddress(addr); 1161 } 1162 1163 int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp) 1164 { 1165 int fd; 1166 1167 /* 1168 * TODO SOCKET_ADDRESS_TYPE_FD when fd is AF_INET or AF_INET6 1169 * (although other address families can do SOCK_DGRAM, too) 1170 */ 1171 switch (remote->type) { 1172 case SOCKET_ADDRESS_TYPE_INET: 1173 fd = inet_dgram_saddr(&remote->u.inet, 1174 local ? &local->u.inet : NULL, errp); 1175 break; 1176 1177 default: 1178 error_setg(errp, "socket type unsupported for datagram"); 1179 fd = -1; 1180 } 1181 return fd; 1182 } 1183 1184 1185 static SocketAddress * 1186 socket_sockaddr_to_address_inet(struct sockaddr_storage *sa, 1187 socklen_t salen, 1188 Error **errp) 1189 { 1190 char host[NI_MAXHOST]; 1191 char serv[NI_MAXSERV]; 1192 SocketAddress *addr; 1193 InetSocketAddress *inet; 1194 int ret; 1195 1196 ret = getnameinfo((struct sockaddr *)sa, salen, 1197 host, sizeof(host), 1198 serv, sizeof(serv), 1199 NI_NUMERICHOST | NI_NUMERICSERV); 1200 if (ret != 0) { 1201 error_setg(errp, "Cannot format numeric socket address: %s", 1202 gai_strerror(ret)); 1203 return NULL; 1204 } 1205 1206 addr = g_new0(SocketAddress, 1); 1207 addr->type = SOCKET_ADDRESS_TYPE_INET; 1208 inet = &addr->u.inet; 1209 inet->host = g_strdup(host); 1210 inet->port = g_strdup(serv); 1211 if (sa->ss_family == AF_INET) { 1212 inet->has_ipv4 = inet->ipv4 = true; 1213 } else { 1214 inet->has_ipv6 = inet->ipv6 = true; 1215 } 1216 1217 return addr; 1218 } 1219 1220 1221 #ifndef WIN32 1222 static SocketAddress * 1223 socket_sockaddr_to_address_unix(struct sockaddr_storage *sa, 1224 socklen_t salen, 1225 Error **errp) 1226 { 1227 SocketAddress *addr; 1228 struct sockaddr_un *su = (struct sockaddr_un *)sa; 1229 1230 addr = g_new0(SocketAddress, 1); 1231 addr->type = SOCKET_ADDRESS_TYPE_UNIX; 1232 if (su->sun_path[0]) { 1233 addr->u.q_unix.path = g_strndup(su->sun_path, sizeof(su->sun_path)); 1234 } 1235 1236 return addr; 1237 } 1238 #endif /* WIN32 */ 1239 1240 #ifdef CONFIG_AF_VSOCK 1241 static SocketAddress * 1242 socket_sockaddr_to_address_vsock(struct sockaddr_storage *sa, 1243 socklen_t salen, 1244 Error **errp) 1245 { 1246 SocketAddress *addr; 1247 VsockSocketAddress *vaddr; 1248 struct sockaddr_vm *svm = (struct sockaddr_vm *)sa; 1249 1250 addr = g_new0(SocketAddress, 1); 1251 addr->type = SOCKET_ADDRESS_TYPE_VSOCK; 1252 vaddr = &addr->u.vsock; 1253 vaddr->cid = g_strdup_printf("%u", svm->svm_cid); 1254 vaddr->port = g_strdup_printf("%u", svm->svm_port); 1255 1256 return addr; 1257 } 1258 #endif /* CONFIG_AF_VSOCK */ 1259 1260 SocketAddress * 1261 socket_sockaddr_to_address(struct sockaddr_storage *sa, 1262 socklen_t salen, 1263 Error **errp) 1264 { 1265 switch (sa->ss_family) { 1266 case AF_INET: 1267 case AF_INET6: 1268 return socket_sockaddr_to_address_inet(sa, salen, errp); 1269 1270 #ifndef WIN32 1271 case AF_UNIX: 1272 return socket_sockaddr_to_address_unix(sa, salen, errp); 1273 #endif /* WIN32 */ 1274 1275 #ifdef CONFIG_AF_VSOCK 1276 case AF_VSOCK: 1277 return socket_sockaddr_to_address_vsock(sa, salen, errp); 1278 #endif 1279 1280 default: 1281 error_setg(errp, "socket family %d unsupported", 1282 sa->ss_family); 1283 return NULL; 1284 } 1285 return 0; 1286 } 1287 1288 1289 SocketAddress *socket_local_address(int fd, Error **errp) 1290 { 1291 struct sockaddr_storage ss; 1292 socklen_t sslen = sizeof(ss); 1293 1294 if (getsockname(fd, (struct sockaddr *)&ss, &sslen) < 0) { 1295 error_setg_errno(errp, errno, "%s", 1296 "Unable to query local socket address"); 1297 return NULL; 1298 } 1299 1300 return socket_sockaddr_to_address(&ss, sslen, errp); 1301 } 1302 1303 1304 SocketAddress *socket_remote_address(int fd, Error **errp) 1305 { 1306 struct sockaddr_storage ss; 1307 socklen_t sslen = sizeof(ss); 1308 1309 if (getpeername(fd, (struct sockaddr *)&ss, &sslen) < 0) { 1310 error_setg_errno(errp, errno, "%s", 1311 "Unable to query remote socket address"); 1312 return NULL; 1313 } 1314 1315 return socket_sockaddr_to_address(&ss, sslen, errp); 1316 } 1317 1318 1319 SocketAddress *socket_address_flatten(SocketAddressLegacy *addr_legacy) 1320 { 1321 SocketAddress *addr; 1322 1323 if (!addr_legacy) { 1324 return NULL; 1325 } 1326 1327 addr = g_new(SocketAddress, 1); 1328 1329 switch (addr_legacy->type) { 1330 case SOCKET_ADDRESS_LEGACY_KIND_INET: 1331 addr->type = SOCKET_ADDRESS_TYPE_INET; 1332 QAPI_CLONE_MEMBERS(InetSocketAddress, &addr->u.inet, 1333 addr_legacy->u.inet.data); 1334 break; 1335 case SOCKET_ADDRESS_LEGACY_KIND_UNIX: 1336 addr->type = SOCKET_ADDRESS_TYPE_UNIX; 1337 QAPI_CLONE_MEMBERS(UnixSocketAddress, &addr->u.q_unix, 1338 addr_legacy->u.q_unix.data); 1339 break; 1340 case SOCKET_ADDRESS_LEGACY_KIND_VSOCK: 1341 addr->type = SOCKET_ADDRESS_TYPE_VSOCK; 1342 QAPI_CLONE_MEMBERS(VsockSocketAddress, &addr->u.vsock, 1343 addr_legacy->u.vsock.data); 1344 break; 1345 case SOCKET_ADDRESS_LEGACY_KIND_FD: 1346 addr->type = SOCKET_ADDRESS_TYPE_FD; 1347 QAPI_CLONE_MEMBERS(String, &addr->u.fd, addr_legacy->u.fd.data); 1348 break; 1349 default: 1350 abort(); 1351 } 1352 1353 return addr; 1354 } 1355