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 "monitor/monitor.h" 25 #include "qapi/clone-visitor.h" 26 #include "qapi/error.h" 27 #include "qapi/qapi-visit-sockets.h" 28 #include "qemu/sockets.h" 29 #include "qemu/main-loop.h" 30 #include "qapi/qobject-input-visitor.h" 31 #include "qapi/qobject-output-visitor.h" 32 #include "qemu/cutils.h" 33 #include "trace.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 !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 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 int num, 211 Error **errp) 212 { 213 ERRP_GUARD(); 214 struct addrinfo ai, *res, *e; 215 char port[33]; 216 char uaddr[INET6_ADDRSTRLEN+1]; 217 char uport[33]; 218 int rc, port_min, port_max, p; 219 int slisten = -1; 220 int saved_errno = 0; 221 bool socket_created = false; 222 223 if (saddr->keep_alive) { 224 error_setg(errp, "keep-alive option is not supported for passive " 225 "sockets"); 226 return -1; 227 } 228 229 memset(&ai,0, sizeof(ai)); 230 ai.ai_flags = AI_PASSIVE; 231 if (saddr->has_numeric && saddr->numeric) { 232 ai.ai_flags |= AI_NUMERICHOST | AI_NUMERICSERV; 233 } 234 ai.ai_socktype = SOCK_STREAM; 235 ai.ai_family = inet_ai_family_from_address(saddr, errp); 236 if (*errp) { 237 return -1; 238 } 239 240 if (saddr->host == NULL) { 241 error_setg(errp, "host not specified"); 242 return -1; 243 } 244 if (saddr->port != NULL) { 245 pstrcpy(port, sizeof(port), saddr->port); 246 } else { 247 port[0] = '\0'; 248 } 249 250 /* lookup */ 251 if (port_offset) { 252 uint64_t baseport; 253 if (strlen(port) == 0) { 254 error_setg(errp, "port not specified"); 255 return -1; 256 } 257 if (parse_uint_full(port, 10, &baseport) < 0) { 258 error_setg(errp, "can't convert to a number: %s", port); 259 return -1; 260 } 261 if (baseport > 65535 || 262 baseport + port_offset > 65535) { 263 error_setg(errp, "port %s out of range", port); 264 return -1; 265 } 266 snprintf(port, sizeof(port), "%d", (int)baseport + port_offset); 267 } 268 rc = getaddrinfo(strlen(saddr->host) ? saddr->host : NULL, 269 strlen(port) ? port : NULL, &ai, &res); 270 if (rc != 0) { 271 error_setg(errp, "address resolution failed for %s:%s: %s", 272 saddr->host, port, gai_strerror(rc)); 273 return -1; 274 } 275 276 /* create socket + bind/listen */ 277 for (e = res; e != NULL; e = e->ai_next) { 278 #ifdef HAVE_IPPROTO_MPTCP 279 if (saddr->has_mptcp && saddr->mptcp) { 280 e->ai_protocol = IPPROTO_MPTCP; 281 } 282 #endif 283 getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen, 284 uaddr,INET6_ADDRSTRLEN,uport,32, 285 NI_NUMERICHOST | NI_NUMERICSERV); 286 287 port_min = inet_getport(e); 288 port_max = saddr->has_to ? saddr->to + port_offset : port_min; 289 for (p = port_min; p <= port_max; p++) { 290 inet_setport(e, p); 291 292 slisten = create_fast_reuse_socket(e); 293 if (slisten < 0) { 294 /* First time we expect we might fail to create the socket 295 * eg if 'e' has AF_INET6 but ipv6 kmod is not loaded. 296 * Later iterations should always succeed if first iteration 297 * worked though, so treat that as fatal. 298 */ 299 if (p == port_min) { 300 continue; 301 } else { 302 error_setg_errno(errp, errno, 303 "Failed to recreate failed listening socket"); 304 goto listen_failed; 305 } 306 } 307 socket_created = true; 308 309 rc = try_bind(slisten, saddr, e); 310 if (rc < 0) { 311 if (errno != EADDRINUSE) { 312 error_setg_errno(errp, errno, "Failed to bind socket"); 313 goto listen_failed; 314 } 315 } else { 316 if (!listen(slisten, num)) { 317 goto listen_ok; 318 } 319 if (errno != EADDRINUSE) { 320 error_setg_errno(errp, errno, "Failed to listen on socket"); 321 goto listen_failed; 322 } 323 } 324 /* Someone else managed to bind to the same port and beat us 325 * to listen on it! Socket semantics does not allow us to 326 * recover from this situation, so we need to recreate the 327 * socket to allow bind attempts for subsequent ports: 328 */ 329 close(slisten); 330 slisten = -1; 331 } 332 } 333 error_setg_errno(errp, errno, 334 socket_created ? 335 "Failed to find an available port" : 336 "Failed to create a socket"); 337 listen_failed: 338 saved_errno = errno; 339 if (slisten >= 0) { 340 close(slisten); 341 } 342 freeaddrinfo(res); 343 errno = saved_errno; 344 return -1; 345 346 listen_ok: 347 freeaddrinfo(res); 348 return slisten; 349 } 350 351 #ifdef _WIN32 352 #define QEMU_SOCKET_RC_INPROGRESS(rc) \ 353 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY) 354 #else 355 #define QEMU_SOCKET_RC_INPROGRESS(rc) \ 356 ((rc) == -EINPROGRESS) 357 #endif 358 359 static int inet_connect_addr(const InetSocketAddress *saddr, 360 struct addrinfo *addr, Error **errp) 361 { 362 int sock, rc; 363 364 sock = qemu_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); 365 if (sock < 0) { 366 error_setg_errno(errp, errno, "Failed to create socket family %d", 367 addr->ai_family); 368 return -1; 369 } 370 socket_set_fast_reuse(sock); 371 372 /* connect to peer */ 373 do { 374 rc = 0; 375 if (connect(sock, addr->ai_addr, addr->ai_addrlen) < 0) { 376 rc = -errno; 377 } 378 } while (rc == -EINTR); 379 380 if (rc < 0) { 381 error_setg_errno(errp, errno, "Failed to connect to '%s:%s'", 382 saddr->host, saddr->port); 383 close(sock); 384 return -1; 385 } 386 387 return sock; 388 } 389 390 static struct addrinfo *inet_parse_connect_saddr(InetSocketAddress *saddr, 391 Error **errp) 392 { 393 ERRP_GUARD(); 394 struct addrinfo ai, *res; 395 int rc; 396 static int useV4Mapped = 1; 397 398 memset(&ai, 0, sizeof(ai)); 399 400 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG; 401 if (qatomic_read(&useV4Mapped)) { 402 ai.ai_flags |= AI_V4MAPPED; 403 } 404 ai.ai_socktype = SOCK_STREAM; 405 ai.ai_family = inet_ai_family_from_address(saddr, errp); 406 if (*errp) { 407 return NULL; 408 } 409 410 if (saddr->host == NULL || saddr->port == NULL) { 411 error_setg(errp, "host and/or port not specified"); 412 return NULL; 413 } 414 415 /* lookup */ 416 rc = getaddrinfo(saddr->host, saddr->port, &ai, &res); 417 418 /* At least FreeBSD and OS-X 10.6 declare AI_V4MAPPED but 419 * then don't implement it in their getaddrinfo(). Detect 420 * this and retry without the flag since that's preferable 421 * to a fatal error 422 */ 423 if (rc == EAI_BADFLAGS && 424 (ai.ai_flags & AI_V4MAPPED)) { 425 qatomic_set(&useV4Mapped, 0); 426 ai.ai_flags &= ~AI_V4MAPPED; 427 rc = getaddrinfo(saddr->host, saddr->port, &ai, &res); 428 } 429 if (rc != 0) { 430 error_setg(errp, "address resolution failed for %s:%s: %s", 431 saddr->host, saddr->port, gai_strerror(rc)); 432 return NULL; 433 } 434 return res; 435 } 436 437 /** 438 * Create a socket and connect it to an address. 439 * 440 * @saddr: Inet socket address specification 441 * @errp: set on error 442 * 443 * Returns: -1 on error, file descriptor on success. 444 */ 445 int inet_connect_saddr(InetSocketAddress *saddr, Error **errp) 446 { 447 Error *local_err = NULL; 448 struct addrinfo *res, *e; 449 int sock = -1; 450 451 res = inet_parse_connect_saddr(saddr, errp); 452 if (!res) { 453 return -1; 454 } 455 456 for (e = res; e != NULL; e = e->ai_next) { 457 error_free(local_err); 458 local_err = NULL; 459 460 #ifdef HAVE_IPPROTO_MPTCP 461 if (saddr->has_mptcp && saddr->mptcp) { 462 e->ai_protocol = IPPROTO_MPTCP; 463 } 464 #endif 465 466 sock = inet_connect_addr(saddr, e, &local_err); 467 if (sock >= 0) { 468 break; 469 } 470 } 471 472 freeaddrinfo(res); 473 474 if (sock < 0) { 475 error_propagate(errp, local_err); 476 return sock; 477 } 478 479 if (saddr->keep_alive) { 480 int val = 1; 481 int ret = setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, 482 &val, sizeof(val)); 483 484 if (ret < 0) { 485 error_setg_errno(errp, errno, "Unable to set KEEPALIVE"); 486 close(sock); 487 return -1; 488 } 489 } 490 491 return sock; 492 } 493 494 static int inet_dgram_saddr(InetSocketAddress *sraddr, 495 InetSocketAddress *sladdr, 496 Error **errp) 497 { 498 ERRP_GUARD(); 499 struct addrinfo ai, *peer = NULL, *local = NULL; 500 const char *addr; 501 const char *port; 502 int sock = -1, rc; 503 504 /* lookup peer addr */ 505 memset(&ai,0, sizeof(ai)); 506 ai.ai_flags = AI_CANONNAME | AI_V4MAPPED | AI_ADDRCONFIG; 507 ai.ai_socktype = SOCK_DGRAM; 508 ai.ai_family = inet_ai_family_from_address(sraddr, errp); 509 if (*errp) { 510 goto err; 511 } 512 513 addr = sraddr->host; 514 port = sraddr->port; 515 if (addr == NULL || strlen(addr) == 0) { 516 addr = "localhost"; 517 } 518 if (port == NULL || strlen(port) == 0) { 519 error_setg(errp, "remote port not specified"); 520 goto err; 521 } 522 523 if ((rc = getaddrinfo(addr, port, &ai, &peer)) != 0) { 524 error_setg(errp, "address resolution failed for %s:%s: %s", addr, port, 525 gai_strerror(rc)); 526 goto err; 527 } 528 529 /* lookup local addr */ 530 memset(&ai,0, sizeof(ai)); 531 ai.ai_flags = AI_PASSIVE; 532 ai.ai_family = peer->ai_family; 533 ai.ai_socktype = SOCK_DGRAM; 534 535 if (sladdr) { 536 addr = sladdr->host; 537 port = sladdr->port; 538 if (addr == NULL || strlen(addr) == 0) { 539 addr = NULL; 540 } 541 if (!port || strlen(port) == 0) { 542 port = "0"; 543 } 544 } else { 545 addr = NULL; 546 port = "0"; 547 } 548 549 if ((rc = getaddrinfo(addr, port, &ai, &local)) != 0) { 550 error_setg(errp, "address resolution failed for %s:%s: %s", addr, port, 551 gai_strerror(rc)); 552 goto err; 553 } 554 555 /* create socket */ 556 sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol); 557 if (sock < 0) { 558 error_setg_errno(errp, errno, "Failed to create socket family %d", 559 peer->ai_family); 560 goto err; 561 } 562 socket_set_fast_reuse(sock); 563 564 /* bind socket */ 565 if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) { 566 error_setg_errno(errp, errno, "Failed to bind socket"); 567 goto err; 568 } 569 570 /* connect to peer */ 571 if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) { 572 error_setg_errno(errp, errno, "Failed to connect to '%s:%s'", 573 addr, port); 574 goto err; 575 } 576 577 freeaddrinfo(local); 578 freeaddrinfo(peer); 579 return sock; 580 581 err: 582 if (sock != -1) { 583 close(sock); 584 } 585 if (local) { 586 freeaddrinfo(local); 587 } 588 if (peer) { 589 freeaddrinfo(peer); 590 } 591 592 return -1; 593 } 594 595 /* compatibility wrapper */ 596 static int inet_parse_flag(const char *flagname, const char *optstr, bool *val, 597 Error **errp) 598 { 599 char *end; 600 size_t len; 601 602 end = strstr(optstr, ","); 603 if (end) { 604 if (end[1] == ',') { /* Reject 'ipv6=on,,foo' */ 605 error_setg(errp, "error parsing '%s' flag '%s'", flagname, optstr); 606 return -1; 607 } 608 len = end - optstr; 609 } else { 610 len = strlen(optstr); 611 } 612 if (len == 0 || (len == 3 && strncmp(optstr, "=on", len) == 0)) { 613 *val = true; 614 } else if (len == 4 && strncmp(optstr, "=off", len) == 0) { 615 *val = false; 616 } else { 617 error_setg(errp, "error parsing '%s' flag '%s'", flagname, optstr); 618 return -1; 619 } 620 return 0; 621 } 622 623 int inet_parse(InetSocketAddress *addr, const char *str, Error **errp) 624 { 625 const char *optstr, *h; 626 char host[65]; 627 char port[33]; 628 int to; 629 int pos; 630 char *begin; 631 632 memset(addr, 0, sizeof(*addr)); 633 634 /* parse address */ 635 if (str[0] == ':') { 636 /* no host given */ 637 host[0] = '\0'; 638 if (sscanf(str, ":%32[^,]%n", port, &pos) != 1) { 639 error_setg(errp, "error parsing port in address '%s'", str); 640 return -1; 641 } 642 } else if (str[0] == '[') { 643 /* IPv6 addr */ 644 if (sscanf(str, "[%64[^]]]:%32[^,]%n", host, port, &pos) != 2) { 645 error_setg(errp, "error parsing IPv6 address '%s'", str); 646 return -1; 647 } 648 } else { 649 /* hostname or IPv4 addr */ 650 if (sscanf(str, "%64[^:]:%32[^,]%n", host, port, &pos) != 2) { 651 error_setg(errp, "error parsing address '%s'", str); 652 return -1; 653 } 654 } 655 656 addr->host = g_strdup(host); 657 addr->port = g_strdup(port); 658 659 /* parse options */ 660 optstr = str + pos; 661 h = strstr(optstr, ",to="); 662 if (h) { 663 h += 4; 664 if (sscanf(h, "%d%n", &to, &pos) != 1 || 665 (h[pos] != '\0' && h[pos] != ',')) { 666 error_setg(errp, "error parsing to= argument"); 667 return -1; 668 } 669 addr->has_to = true; 670 addr->to = to; 671 } 672 begin = strstr(optstr, ",ipv4"); 673 if (begin) { 674 if (inet_parse_flag("ipv4", begin + 5, &addr->ipv4, errp) < 0) { 675 return -1; 676 } 677 addr->has_ipv4 = true; 678 } 679 begin = strstr(optstr, ",ipv6"); 680 if (begin) { 681 if (inet_parse_flag("ipv6", begin + 5, &addr->ipv6, errp) < 0) { 682 return -1; 683 } 684 addr->has_ipv6 = true; 685 } 686 begin = strstr(optstr, ",keep-alive"); 687 if (begin) { 688 if (inet_parse_flag("keep-alive", begin + strlen(",keep-alive"), 689 &addr->keep_alive, errp) < 0) 690 { 691 return -1; 692 } 693 addr->has_keep_alive = true; 694 } 695 #ifdef HAVE_IPPROTO_MPTCP 696 begin = strstr(optstr, ",mptcp"); 697 if (begin) { 698 if (inet_parse_flag("mptcp", begin + strlen(",mptcp"), 699 &addr->mptcp, errp) < 0) 700 { 701 return -1; 702 } 703 addr->has_mptcp = true; 704 } 705 #endif 706 return 0; 707 } 708 709 710 #ifdef CONFIG_AF_VSOCK 711 static bool vsock_parse_vaddr_to_sockaddr(const VsockSocketAddress *vaddr, 712 struct sockaddr_vm *svm, 713 Error **errp) 714 { 715 uint64_t val; 716 717 memset(svm, 0, sizeof(*svm)); 718 svm->svm_family = AF_VSOCK; 719 720 if (parse_uint_full(vaddr->cid, 10, &val) < 0 || 721 val > UINT32_MAX) { 722 error_setg(errp, "Failed to parse cid '%s'", vaddr->cid); 723 return false; 724 } 725 svm->svm_cid = val; 726 727 if (parse_uint_full(vaddr->port, 10, &val) < 0 || 728 val > UINT32_MAX) { 729 error_setg(errp, "Failed to parse port '%s'", vaddr->port); 730 return false; 731 } 732 svm->svm_port = val; 733 734 return true; 735 } 736 737 static int vsock_connect_addr(const VsockSocketAddress *vaddr, 738 const struct sockaddr_vm *svm, Error **errp) 739 { 740 int sock, rc; 741 742 sock = qemu_socket(AF_VSOCK, SOCK_STREAM, 0); 743 if (sock < 0) { 744 error_setg_errno(errp, errno, "Failed to create socket family %d", 745 AF_VSOCK); 746 return -1; 747 } 748 749 /* connect to peer */ 750 do { 751 rc = 0; 752 if (connect(sock, (const struct sockaddr *)svm, sizeof(*svm)) < 0) { 753 rc = -errno; 754 } 755 } while (rc == -EINTR); 756 757 if (rc < 0) { 758 error_setg_errno(errp, errno, "Failed to connect to '%s:%s'", 759 vaddr->cid, vaddr->port); 760 close(sock); 761 return -1; 762 } 763 764 return sock; 765 } 766 767 static int vsock_connect_saddr(VsockSocketAddress *vaddr, Error **errp) 768 { 769 struct sockaddr_vm svm; 770 771 if (!vsock_parse_vaddr_to_sockaddr(vaddr, &svm, errp)) { 772 return -1; 773 } 774 775 return vsock_connect_addr(vaddr, &svm, errp); 776 } 777 778 static int vsock_listen_saddr(VsockSocketAddress *vaddr, 779 int num, 780 Error **errp) 781 { 782 struct sockaddr_vm svm; 783 int slisten; 784 785 if (!vsock_parse_vaddr_to_sockaddr(vaddr, &svm, errp)) { 786 return -1; 787 } 788 789 slisten = qemu_socket(AF_VSOCK, SOCK_STREAM, 0); 790 if (slisten < 0) { 791 error_setg_errno(errp, errno, "Failed to create socket"); 792 return -1; 793 } 794 795 if (bind(slisten, (const struct sockaddr *)&svm, sizeof(svm)) != 0) { 796 error_setg_errno(errp, errno, "Failed to bind socket"); 797 close(slisten); 798 return -1; 799 } 800 801 if (listen(slisten, num) != 0) { 802 error_setg_errno(errp, errno, "Failed to listen on socket"); 803 close(slisten); 804 return -1; 805 } 806 return slisten; 807 } 808 809 static int vsock_parse(VsockSocketAddress *addr, const char *str, 810 Error **errp) 811 { 812 char cid[33]; 813 char port[33]; 814 int n; 815 816 if (sscanf(str, "%32[^:]:%32[^,]%n", cid, port, &n) != 2) { 817 error_setg(errp, "error parsing address '%s'", str); 818 return -1; 819 } 820 if (str[n] != '\0') { 821 error_setg(errp, "trailing characters in address '%s'", str); 822 return -1; 823 } 824 825 addr->cid = g_strdup(cid); 826 addr->port = g_strdup(port); 827 return 0; 828 } 829 #else 830 static void vsock_unsupported(Error **errp) 831 { 832 error_setg(errp, "socket family AF_VSOCK unsupported"); 833 } 834 835 static int vsock_connect_saddr(VsockSocketAddress *vaddr, Error **errp) 836 { 837 vsock_unsupported(errp); 838 return -1; 839 } 840 841 static int vsock_listen_saddr(VsockSocketAddress *vaddr, 842 int num, 843 Error **errp) 844 { 845 vsock_unsupported(errp); 846 return -1; 847 } 848 849 static int vsock_parse(VsockSocketAddress *addr, const char *str, 850 Error **errp) 851 { 852 vsock_unsupported(errp); 853 return -1; 854 } 855 #endif /* CONFIG_AF_VSOCK */ 856 857 static bool saddr_is_abstract(UnixSocketAddress *saddr) 858 { 859 #ifdef CONFIG_LINUX 860 return saddr->abstract; 861 #else 862 return false; 863 #endif 864 } 865 866 static bool saddr_is_tight(UnixSocketAddress *saddr) 867 { 868 #ifdef CONFIG_LINUX 869 return !saddr->has_tight || saddr->tight; 870 #else 871 return false; 872 #endif 873 } 874 875 static int unix_listen_saddr(UnixSocketAddress *saddr, 876 int num, 877 Error **errp) 878 { 879 bool abstract = saddr_is_abstract(saddr); 880 struct sockaddr_un un; 881 int sock, fd; 882 char *pathbuf = NULL; 883 const char *path; 884 size_t pathlen; 885 size_t addrlen; 886 887 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0); 888 if (sock < 0) { 889 error_setg_errno(errp, errno, "Failed to create Unix socket"); 890 return -1; 891 } 892 893 if (saddr->path[0] || abstract) { 894 path = saddr->path; 895 } else { 896 path = pathbuf = g_strdup_printf("%s/qemu-socket-XXXXXX", 897 g_get_tmp_dir()); 898 } 899 900 pathlen = strlen(path); 901 if (pathlen > sizeof(un.sun_path) || 902 (abstract && pathlen > (sizeof(un.sun_path) - 1))) { 903 error_setg(errp, "UNIX socket path '%s' is too long", path); 904 error_append_hint(errp, "Path must be less than %zu bytes\n", 905 abstract ? sizeof(un.sun_path) - 1 : 906 sizeof(un.sun_path)); 907 goto err; 908 } 909 910 if (pathbuf != NULL) { 911 /* 912 * This dummy fd usage silences the mktemp() insecure warning. 913 * Using mkstemp() doesn't make things more secure here 914 * though. bind() complains about existing files, so we have 915 * to unlink first and thus re-open the race window. The 916 * worst case possible is bind() failing, i.e. a DoS attack. 917 */ 918 fd = mkstemp(pathbuf); 919 if (fd < 0) { 920 error_setg_errno(errp, errno, 921 "Failed to make a temporary socket %s", pathbuf); 922 goto err; 923 } 924 close(fd); 925 } 926 927 if (!abstract && unlink(path) < 0 && errno != ENOENT) { 928 error_setg_errno(errp, errno, 929 "Failed to unlink socket %s", path); 930 goto err; 931 } 932 933 memset(&un, 0, sizeof(un)); 934 un.sun_family = AF_UNIX; 935 addrlen = sizeof(un); 936 937 if (abstract) { 938 un.sun_path[0] = '\0'; 939 memcpy(&un.sun_path[1], path, pathlen); 940 if (saddr_is_tight(saddr)) { 941 addrlen = offsetof(struct sockaddr_un, sun_path) + 1 + pathlen; 942 } 943 } else { 944 memcpy(un.sun_path, path, pathlen); 945 } 946 947 if (bind(sock, (struct sockaddr *) &un, addrlen) < 0) { 948 error_setg_errno(errp, errno, "Failed to bind socket to %s", path); 949 goto err; 950 } 951 if (listen(sock, num) < 0) { 952 error_setg_errno(errp, errno, "Failed to listen on socket"); 953 goto err; 954 } 955 956 g_free(pathbuf); 957 return sock; 958 959 err: 960 g_free(pathbuf); 961 close(sock); 962 return -1; 963 } 964 965 static int unix_connect_saddr(UnixSocketAddress *saddr, Error **errp) 966 { 967 bool abstract = saddr_is_abstract(saddr); 968 struct sockaddr_un un; 969 int sock, rc; 970 size_t pathlen; 971 size_t addrlen; 972 973 if (saddr->path == NULL) { 974 error_setg(errp, "unix connect: no path specified"); 975 return -1; 976 } 977 978 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0); 979 if (sock < 0) { 980 error_setg_errno(errp, errno, "Failed to create socket"); 981 return -1; 982 } 983 984 pathlen = strlen(saddr->path); 985 if (pathlen > sizeof(un.sun_path) || 986 (abstract && pathlen > (sizeof(un.sun_path) - 1))) { 987 error_setg(errp, "UNIX socket path '%s' is too long", saddr->path); 988 error_append_hint(errp, "Path must be less than %zu bytes\n", 989 abstract ? sizeof(un.sun_path) - 1 : 990 sizeof(un.sun_path)); 991 goto err; 992 } 993 994 memset(&un, 0, sizeof(un)); 995 un.sun_family = AF_UNIX; 996 addrlen = sizeof(un); 997 998 if (abstract) { 999 un.sun_path[0] = '\0'; 1000 memcpy(&un.sun_path[1], saddr->path, pathlen); 1001 if (saddr_is_tight(saddr)) { 1002 addrlen = offsetof(struct sockaddr_un, sun_path) + 1 + pathlen; 1003 } 1004 } else { 1005 memcpy(un.sun_path, saddr->path, pathlen); 1006 } 1007 /* connect to peer */ 1008 do { 1009 rc = 0; 1010 if (connect(sock, (struct sockaddr *) &un, addrlen) < 0) { 1011 rc = -errno; 1012 } 1013 } while (rc == -EINTR); 1014 1015 if (rc < 0) { 1016 error_setg_errno(errp, -rc, "Failed to connect to '%s'", 1017 saddr->path); 1018 goto err; 1019 } 1020 1021 return sock; 1022 1023 err: 1024 close(sock); 1025 return -1; 1026 } 1027 1028 /* compatibility wrapper */ 1029 int unix_listen(const char *str, Error **errp) 1030 { 1031 UnixSocketAddress *saddr; 1032 int sock; 1033 1034 saddr = g_new0(UnixSocketAddress, 1); 1035 saddr->path = g_strdup(str); 1036 sock = unix_listen_saddr(saddr, 1, errp); 1037 qapi_free_UnixSocketAddress(saddr); 1038 return sock; 1039 } 1040 1041 int unix_connect(const char *path, Error **errp) 1042 { 1043 UnixSocketAddress *saddr; 1044 int sock; 1045 1046 saddr = g_new0(UnixSocketAddress, 1); 1047 saddr->path = g_strdup(path); 1048 sock = unix_connect_saddr(saddr, errp); 1049 qapi_free_UnixSocketAddress(saddr); 1050 return sock; 1051 } 1052 1053 char *socket_uri(SocketAddress *addr) 1054 { 1055 switch (addr->type) { 1056 case SOCKET_ADDRESS_TYPE_INET: 1057 return g_strdup_printf("tcp:%s:%s", 1058 addr->u.inet.host, 1059 addr->u.inet.port); 1060 case SOCKET_ADDRESS_TYPE_UNIX: 1061 return g_strdup_printf("unix:%s", 1062 addr->u.q_unix.path); 1063 case SOCKET_ADDRESS_TYPE_FD: 1064 return g_strdup_printf("fd:%s", addr->u.fd.str); 1065 case SOCKET_ADDRESS_TYPE_VSOCK: 1066 return g_strdup_printf("vsock:%s:%s", 1067 addr->u.vsock.cid, 1068 addr->u.vsock.port); 1069 default: 1070 return g_strdup("unknown address type"); 1071 } 1072 } 1073 1074 SocketAddress *socket_parse(const char *str, Error **errp) 1075 { 1076 SocketAddress *addr; 1077 1078 addr = g_new0(SocketAddress, 1); 1079 if (strstart(str, "unix:", NULL)) { 1080 if (str[5] == '\0') { 1081 error_setg(errp, "invalid Unix socket address"); 1082 goto fail; 1083 } else { 1084 addr->type = SOCKET_ADDRESS_TYPE_UNIX; 1085 addr->u.q_unix.path = g_strdup(str + 5); 1086 } 1087 } else if (strstart(str, "fd:", NULL)) { 1088 if (str[3] == '\0') { 1089 error_setg(errp, "invalid file descriptor address"); 1090 goto fail; 1091 } else { 1092 addr->type = SOCKET_ADDRESS_TYPE_FD; 1093 addr->u.fd.str = g_strdup(str + 3); 1094 } 1095 } else if (strstart(str, "vsock:", NULL)) { 1096 addr->type = SOCKET_ADDRESS_TYPE_VSOCK; 1097 if (vsock_parse(&addr->u.vsock, str + strlen("vsock:"), errp)) { 1098 goto fail; 1099 } 1100 } else if (strstart(str, "tcp:", NULL)) { 1101 addr->type = SOCKET_ADDRESS_TYPE_INET; 1102 if (inet_parse(&addr->u.inet, str + strlen("tcp:"), errp)) { 1103 goto fail; 1104 } 1105 } else { 1106 addr->type = SOCKET_ADDRESS_TYPE_INET; 1107 if (inet_parse(&addr->u.inet, str, errp)) { 1108 goto fail; 1109 } 1110 } 1111 return addr; 1112 1113 fail: 1114 qapi_free_SocketAddress(addr); 1115 return NULL; 1116 } 1117 1118 static int socket_get_fd(const char *fdstr, Error **errp) 1119 { 1120 Monitor *cur_mon = monitor_cur(); 1121 int fd; 1122 if (cur_mon) { 1123 fd = monitor_get_fd(cur_mon, fdstr, errp); 1124 if (fd < 0) { 1125 return -1; 1126 } 1127 } else { 1128 if (qemu_strtoi(fdstr, NULL, 10, &fd) < 0) { 1129 error_setg_errno(errp, errno, 1130 "Unable to parse FD number %s", 1131 fdstr); 1132 return -1; 1133 } 1134 } 1135 if (!fd_is_socket(fd)) { 1136 error_setg(errp, "File descriptor '%s' is not a socket", fdstr); 1137 close(fd); 1138 return -1; 1139 } 1140 return fd; 1141 } 1142 1143 int socket_address_parse_named_fd(SocketAddress *addr, Error **errp) 1144 { 1145 int fd; 1146 1147 if (addr->type != SOCKET_ADDRESS_TYPE_FD) { 1148 return 0; 1149 } 1150 1151 fd = socket_get_fd(addr->u.fd.str, errp); 1152 if (fd < 0) { 1153 return fd; 1154 } 1155 1156 g_free(addr->u.fd.str); 1157 addr->u.fd.str = g_strdup_printf("%d", fd); 1158 1159 return 0; 1160 } 1161 1162 int socket_connect(SocketAddress *addr, Error **errp) 1163 { 1164 int fd; 1165 1166 switch (addr->type) { 1167 case SOCKET_ADDRESS_TYPE_INET: 1168 fd = inet_connect_saddr(&addr->u.inet, errp); 1169 break; 1170 1171 case SOCKET_ADDRESS_TYPE_UNIX: 1172 fd = unix_connect_saddr(&addr->u.q_unix, errp); 1173 break; 1174 1175 case SOCKET_ADDRESS_TYPE_FD: 1176 fd = socket_get_fd(addr->u.fd.str, errp); 1177 break; 1178 1179 case SOCKET_ADDRESS_TYPE_VSOCK: 1180 fd = vsock_connect_saddr(&addr->u.vsock, errp); 1181 break; 1182 1183 default: 1184 abort(); 1185 } 1186 return fd; 1187 } 1188 1189 int socket_listen(SocketAddress *addr, int num, Error **errp) 1190 { 1191 int fd; 1192 1193 trace_socket_listen(num); 1194 switch (addr->type) { 1195 case SOCKET_ADDRESS_TYPE_INET: 1196 fd = inet_listen_saddr(&addr->u.inet, 0, num, errp); 1197 break; 1198 1199 case SOCKET_ADDRESS_TYPE_UNIX: 1200 fd = unix_listen_saddr(&addr->u.q_unix, num, errp); 1201 break; 1202 1203 case SOCKET_ADDRESS_TYPE_FD: 1204 fd = socket_get_fd(addr->u.fd.str, errp); 1205 if (fd < 0) { 1206 return -1; 1207 } 1208 1209 /* 1210 * If the socket is not yet in the listen state, then transition it to 1211 * the listen state now. 1212 * 1213 * If it's already listening then this updates the backlog value as 1214 * requested. 1215 * 1216 * If this socket cannot listen because it's already in another state 1217 * (e.g. unbound or connected) then we'll catch the error here. 1218 */ 1219 if (listen(fd, num) != 0) { 1220 error_setg_errno(errp, errno, "Failed to listen on fd socket"); 1221 close(fd); 1222 return -1; 1223 } 1224 break; 1225 1226 case SOCKET_ADDRESS_TYPE_VSOCK: 1227 fd = vsock_listen_saddr(&addr->u.vsock, num, errp); 1228 break; 1229 1230 default: 1231 abort(); 1232 } 1233 return fd; 1234 } 1235 1236 void socket_listen_cleanup(int fd, Error **errp) 1237 { 1238 SocketAddress *addr; 1239 1240 addr = socket_local_address(fd, errp); 1241 if (!addr) { 1242 return; 1243 } 1244 1245 if (addr->type == SOCKET_ADDRESS_TYPE_UNIX 1246 && addr->u.q_unix.path) { 1247 if (unlink(addr->u.q_unix.path) < 0 && errno != ENOENT) { 1248 error_setg_errno(errp, errno, 1249 "Failed to unlink socket %s", 1250 addr->u.q_unix.path); 1251 } 1252 } 1253 1254 qapi_free_SocketAddress(addr); 1255 } 1256 1257 int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp) 1258 { 1259 int fd; 1260 1261 /* 1262 * TODO SOCKET_ADDRESS_TYPE_FD when fd is AF_INET or AF_INET6 1263 * (although other address families can do SOCK_DGRAM, too) 1264 */ 1265 switch (remote->type) { 1266 case SOCKET_ADDRESS_TYPE_INET: 1267 fd = inet_dgram_saddr(&remote->u.inet, 1268 local ? &local->u.inet : NULL, errp); 1269 break; 1270 1271 default: 1272 error_setg(errp, "socket type unsupported for datagram"); 1273 fd = -1; 1274 } 1275 return fd; 1276 } 1277 1278 1279 static SocketAddress * 1280 socket_sockaddr_to_address_inet(struct sockaddr_storage *sa, 1281 socklen_t salen, 1282 Error **errp) 1283 { 1284 char host[NI_MAXHOST]; 1285 char serv[NI_MAXSERV]; 1286 SocketAddress *addr; 1287 InetSocketAddress *inet; 1288 int ret; 1289 1290 ret = getnameinfo((struct sockaddr *)sa, salen, 1291 host, sizeof(host), 1292 serv, sizeof(serv), 1293 NI_NUMERICHOST | NI_NUMERICSERV); 1294 if (ret != 0) { 1295 error_setg(errp, "Cannot format numeric socket address: %s", 1296 gai_strerror(ret)); 1297 return NULL; 1298 } 1299 1300 addr = g_new0(SocketAddress, 1); 1301 addr->type = SOCKET_ADDRESS_TYPE_INET; 1302 inet = &addr->u.inet; 1303 inet->host = g_strdup(host); 1304 inet->port = g_strdup(serv); 1305 if (sa->ss_family == AF_INET) { 1306 inet->has_ipv4 = inet->ipv4 = true; 1307 } else { 1308 inet->has_ipv6 = inet->ipv6 = true; 1309 } 1310 1311 return addr; 1312 } 1313 1314 1315 static SocketAddress * 1316 socket_sockaddr_to_address_unix(struct sockaddr_storage *sa, 1317 socklen_t salen, 1318 Error **errp) 1319 { 1320 SocketAddress *addr; 1321 struct sockaddr_un *su = (struct sockaddr_un *)sa; 1322 1323 addr = g_new0(SocketAddress, 1); 1324 addr->type = SOCKET_ADDRESS_TYPE_UNIX; 1325 salen -= offsetof(struct sockaddr_un, sun_path); 1326 #ifdef CONFIG_LINUX 1327 if (salen > 0 && !su->sun_path[0]) { 1328 /* Linux abstract socket */ 1329 addr->u.q_unix.path = g_strndup(su->sun_path + 1, salen - 1); 1330 addr->u.q_unix.has_abstract = true; 1331 addr->u.q_unix.abstract = true; 1332 addr->u.q_unix.has_tight = true; 1333 addr->u.q_unix.tight = salen < sizeof(su->sun_path); 1334 return addr; 1335 } 1336 #endif 1337 1338 addr->u.q_unix.path = g_strndup(su->sun_path, salen); 1339 return addr; 1340 } 1341 1342 #ifdef CONFIG_AF_VSOCK 1343 static SocketAddress * 1344 socket_sockaddr_to_address_vsock(struct sockaddr_storage *sa, 1345 socklen_t salen, 1346 Error **errp) 1347 { 1348 SocketAddress *addr; 1349 VsockSocketAddress *vaddr; 1350 struct sockaddr_vm *svm = (struct sockaddr_vm *)sa; 1351 1352 addr = g_new0(SocketAddress, 1); 1353 addr->type = SOCKET_ADDRESS_TYPE_VSOCK; 1354 vaddr = &addr->u.vsock; 1355 vaddr->cid = g_strdup_printf("%u", svm->svm_cid); 1356 vaddr->port = g_strdup_printf("%u", svm->svm_port); 1357 1358 return addr; 1359 } 1360 #endif /* CONFIG_AF_VSOCK */ 1361 1362 SocketAddress * 1363 socket_sockaddr_to_address(struct sockaddr_storage *sa, 1364 socklen_t salen, 1365 Error **errp) 1366 { 1367 switch (sa->ss_family) { 1368 case AF_INET: 1369 case AF_INET6: 1370 return socket_sockaddr_to_address_inet(sa, salen, errp); 1371 1372 case AF_UNIX: 1373 return socket_sockaddr_to_address_unix(sa, salen, errp); 1374 1375 #ifdef CONFIG_AF_VSOCK 1376 case AF_VSOCK: 1377 return socket_sockaddr_to_address_vsock(sa, salen, errp); 1378 #endif 1379 1380 default: 1381 error_setg(errp, "socket family %d unsupported", 1382 sa->ss_family); 1383 return NULL; 1384 } 1385 return 0; 1386 } 1387 1388 1389 SocketAddress *socket_local_address(int fd, Error **errp) 1390 { 1391 struct sockaddr_storage ss; 1392 socklen_t sslen = sizeof(ss); 1393 1394 if (getsockname(fd, (struct sockaddr *)&ss, &sslen) < 0) { 1395 error_setg_errno(errp, errno, "%s", 1396 "Unable to query local socket address"); 1397 return NULL; 1398 } 1399 1400 return socket_sockaddr_to_address(&ss, sslen, errp); 1401 } 1402 1403 1404 SocketAddress *socket_address_flatten(SocketAddressLegacy *addr_legacy) 1405 { 1406 SocketAddress *addr; 1407 1408 if (!addr_legacy) { 1409 return NULL; 1410 } 1411 1412 addr = g_new(SocketAddress, 1); 1413 1414 switch (addr_legacy->type) { 1415 case SOCKET_ADDRESS_TYPE_INET: 1416 addr->type = SOCKET_ADDRESS_TYPE_INET; 1417 QAPI_CLONE_MEMBERS(InetSocketAddress, &addr->u.inet, 1418 addr_legacy->u.inet.data); 1419 break; 1420 case SOCKET_ADDRESS_TYPE_UNIX: 1421 addr->type = SOCKET_ADDRESS_TYPE_UNIX; 1422 QAPI_CLONE_MEMBERS(UnixSocketAddress, &addr->u.q_unix, 1423 addr_legacy->u.q_unix.data); 1424 break; 1425 case SOCKET_ADDRESS_TYPE_VSOCK: 1426 addr->type = SOCKET_ADDRESS_TYPE_VSOCK; 1427 QAPI_CLONE_MEMBERS(VsockSocketAddress, &addr->u.vsock, 1428 addr_legacy->u.vsock.data); 1429 break; 1430 case SOCKET_ADDRESS_TYPE_FD: 1431 addr->type = SOCKET_ADDRESS_TYPE_FD; 1432 QAPI_CLONE_MEMBERS(FdSocketAddress, &addr->u.fd, 1433 addr_legacy->u.fd.data); 1434 break; 1435 default: 1436 abort(); 1437 } 1438 1439 return addr; 1440 } 1441