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