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