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