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