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