xref: /openbmc/qemu/chardev/char-socket.c (revision 98979cdc)
1 /*
2  * QEMU System Emulator
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #include "qemu/osdep.h"
25 #include "chardev/char.h"
26 #include "io/channel-socket.h"
27 #include "io/channel-tls.h"
28 #include "qemu/error-report.h"
29 #include "qapi/error.h"
30 #include "qapi/clone-visitor.h"
31 
32 #include "chardev/char-io.h"
33 
34 /***********************************************************/
35 /* TCP Net console */
36 
37 #define TCP_MAX_FDS 16
38 
39 typedef struct {
40     Chardev parent;
41     QIOChannel *ioc; /* Client I/O channel */
42     QIOChannelSocket *sioc; /* Client master channel */
43     QIOChannelSocket *listen_ioc;
44     guint listen_tag;
45     QCryptoTLSCreds *tls_creds;
46     int connected;
47     int max_size;
48     int do_telnetopt;
49     int do_nodelay;
50     int *read_msgfds;
51     size_t read_msgfds_num;
52     int *write_msgfds;
53     size_t write_msgfds_num;
54 
55     SocketAddress *addr;
56     bool is_listen;
57     bool is_telnet;
58     bool is_tn3270;
59 
60     guint reconnect_timer;
61     int64_t reconnect_time;
62     bool connect_err_reported;
63 } SocketChardev;
64 
65 #define SOCKET_CHARDEV(obj)                                     \
66     OBJECT_CHECK(SocketChardev, (obj), TYPE_CHARDEV_SOCKET)
67 
68 static gboolean socket_reconnect_timeout(gpointer opaque);
69 
70 static void qemu_chr_socket_restart_timer(Chardev *chr)
71 {
72     SocketChardev *s = SOCKET_CHARDEV(chr);
73     char *name;
74 
75     assert(s->connected == 0);
76     s->reconnect_timer = g_timeout_add_seconds(s->reconnect_time,
77                                                socket_reconnect_timeout, chr);
78     name = g_strdup_printf("chardev-socket-reconnect-%s", chr->label);
79     g_source_set_name_by_id(s->reconnect_timer, name);
80     g_free(name);
81 }
82 
83 static void check_report_connect_error(Chardev *chr,
84                                        Error *err)
85 {
86     SocketChardev *s = SOCKET_CHARDEV(chr);
87 
88     if (!s->connect_err_reported) {
89         error_report("Unable to connect character device %s: %s",
90                      chr->label, error_get_pretty(err));
91         s->connect_err_reported = true;
92     }
93     qemu_chr_socket_restart_timer(chr);
94 }
95 
96 static gboolean tcp_chr_accept(QIOChannel *chan,
97                                GIOCondition cond,
98                                void *opaque);
99 
100 static int tcp_chr_read_poll(void *opaque);
101 static void tcp_chr_disconnect(Chardev *chr);
102 
103 /* Called with chr_write_lock held.  */
104 static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len)
105 {
106     SocketChardev *s = SOCKET_CHARDEV(chr);
107 
108     if (s->connected) {
109         int ret =  io_channel_send_full(s->ioc, buf, len,
110                                         s->write_msgfds,
111                                         s->write_msgfds_num);
112 
113         /* free the written msgfds, no matter what */
114         if (s->write_msgfds_num) {
115             g_free(s->write_msgfds);
116             s->write_msgfds = 0;
117             s->write_msgfds_num = 0;
118         }
119 
120         if (ret < 0 && errno != EAGAIN) {
121             if (tcp_chr_read_poll(chr) <= 0) {
122                 tcp_chr_disconnect(chr);
123                 return len;
124             } /* else let the read handler finish it properly */
125         }
126 
127         return ret;
128     } else {
129         /* XXX: indicate an error ? */
130         return len;
131     }
132 }
133 
134 static int tcp_chr_read_poll(void *opaque)
135 {
136     Chardev *chr = CHARDEV(opaque);
137     SocketChardev *s = SOCKET_CHARDEV(opaque);
138     if (!s->connected) {
139         return 0;
140     }
141     s->max_size = qemu_chr_be_can_write(chr);
142     return s->max_size;
143 }
144 
145 static void tcp_chr_process_IAC_bytes(Chardev *chr,
146                                       SocketChardev *s,
147                                       uint8_t *buf, int *size)
148 {
149     /* Handle any telnet or tn3270 client's basic IAC options.
150      * For telnet options, it satisfies char by char mode with no echo.
151      * For tn3270 options, it satisfies binary mode with EOR.
152      * All IAC options will be removed from the buf and the do_opt
153      * pointer will be used to track the state of the width of the
154      * IAC information.
155      *
156      * RFC854: "All TELNET commands consist of at least a two byte sequence.
157      * The commands dealing with option negotiation are three byte sequences,
158      * the third byte being the code for the option referenced."
159      * "IAC BREAK", "IAC IP", "IAC NOP" and the double IAC are two bytes.
160      * "IAC SB", "IAC SE" and "IAC EOR" are saved to split up data boundary
161      * for tn3270.
162      * NOP, Break and Interrupt Process(IP) might be encountered during a TN3270
163      * session, and NOP and IP need to be done later.
164      */
165 
166     int i;
167     int j = 0;
168 
169     for (i = 0; i < *size; i++) {
170         if (s->do_telnetopt > 1) {
171             if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
172                 /* Double IAC means send an IAC */
173                 if (j != i) {
174                     buf[j] = buf[i];
175                 }
176                 j++;
177                 s->do_telnetopt = 1;
178             } else {
179                 if ((unsigned char)buf[i] == IAC_BREAK
180                     && s->do_telnetopt == 2) {
181                     /* Handle IAC break commands by sending a serial break */
182                     qemu_chr_be_event(chr, CHR_EVENT_BREAK);
183                     s->do_telnetopt++;
184                 } else if (s->is_tn3270 && ((unsigned char)buf[i] == IAC_EOR
185                            || (unsigned char)buf[i] == IAC_SB
186                            || (unsigned char)buf[i] == IAC_SE)
187                            && s->do_telnetopt == 2) {
188                     buf[j++] = IAC;
189                     buf[j++] = buf[i];
190                     s->do_telnetopt++;
191                 } else if (s->is_tn3270 && ((unsigned char)buf[i] == IAC_IP
192                            || (unsigned char)buf[i] == IAC_NOP)
193                            && s->do_telnetopt == 2) {
194                     /* TODO: IP and NOP need to be implemented later. */
195                     s->do_telnetopt++;
196                 }
197                 s->do_telnetopt++;
198             }
199             if (s->do_telnetopt >= 4) {
200                 s->do_telnetopt = 1;
201             }
202         } else {
203             if ((unsigned char)buf[i] == IAC) {
204                 s->do_telnetopt = 2;
205             } else {
206                 if (j != i) {
207                     buf[j] = buf[i];
208                 }
209                 j++;
210             }
211         }
212     }
213     *size = j;
214 }
215 
216 static int tcp_get_msgfds(Chardev *chr, int *fds, int num)
217 {
218     SocketChardev *s = SOCKET_CHARDEV(chr);
219 
220     int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
221 
222     assert(num <= TCP_MAX_FDS);
223 
224     if (to_copy) {
225         int i;
226 
227         memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
228 
229         /* Close unused fds */
230         for (i = to_copy; i < s->read_msgfds_num; i++) {
231             close(s->read_msgfds[i]);
232         }
233 
234         g_free(s->read_msgfds);
235         s->read_msgfds = 0;
236         s->read_msgfds_num = 0;
237     }
238 
239     return to_copy;
240 }
241 
242 static int tcp_set_msgfds(Chardev *chr, int *fds, int num)
243 {
244     SocketChardev *s = SOCKET_CHARDEV(chr);
245 
246     /* clear old pending fd array */
247     g_free(s->write_msgfds);
248     s->write_msgfds = NULL;
249     s->write_msgfds_num = 0;
250 
251     if (!s->connected ||
252         !qio_channel_has_feature(s->ioc,
253                                  QIO_CHANNEL_FEATURE_FD_PASS)) {
254         return -1;
255     }
256 
257     if (num) {
258         s->write_msgfds = g_new(int, num);
259         memcpy(s->write_msgfds, fds, num * sizeof(int));
260     }
261 
262     s->write_msgfds_num = num;
263 
264     return 0;
265 }
266 
267 static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len)
268 {
269     SocketChardev *s = SOCKET_CHARDEV(chr);
270     struct iovec iov = { .iov_base = buf, .iov_len = len };
271     int ret;
272     size_t i;
273     int *msgfds = NULL;
274     size_t msgfds_num = 0;
275 
276     if (qio_channel_has_feature(s->ioc, QIO_CHANNEL_FEATURE_FD_PASS)) {
277         ret = qio_channel_readv_full(s->ioc, &iov, 1,
278                                      &msgfds, &msgfds_num,
279                                      NULL);
280     } else {
281         ret = qio_channel_readv_full(s->ioc, &iov, 1,
282                                      NULL, NULL,
283                                      NULL);
284     }
285 
286     if (ret == QIO_CHANNEL_ERR_BLOCK) {
287         errno = EAGAIN;
288         ret = -1;
289     } else if (ret == -1) {
290         errno = EIO;
291     }
292 
293     if (msgfds_num) {
294         /* close and clean read_msgfds */
295         for (i = 0; i < s->read_msgfds_num; i++) {
296             close(s->read_msgfds[i]);
297         }
298 
299         if (s->read_msgfds_num) {
300             g_free(s->read_msgfds);
301         }
302 
303         s->read_msgfds = msgfds;
304         s->read_msgfds_num = msgfds_num;
305     }
306 
307     for (i = 0; i < s->read_msgfds_num; i++) {
308         int fd = s->read_msgfds[i];
309         if (fd < 0) {
310             continue;
311         }
312 
313         /* O_NONBLOCK is preserved across SCM_RIGHTS so reset it */
314         qemu_set_block(fd);
315 
316 #ifndef MSG_CMSG_CLOEXEC
317         qemu_set_cloexec(fd);
318 #endif
319     }
320 
321     return ret;
322 }
323 
324 static GSource *tcp_chr_add_watch(Chardev *chr, GIOCondition cond)
325 {
326     SocketChardev *s = SOCKET_CHARDEV(chr);
327     return qio_channel_create_watch(s->ioc, cond);
328 }
329 
330 static void tcp_chr_free_connection(Chardev *chr)
331 {
332     SocketChardev *s = SOCKET_CHARDEV(chr);
333     int i;
334 
335     if (!s->connected) {
336         return;
337     }
338 
339     if (s->read_msgfds_num) {
340         for (i = 0; i < s->read_msgfds_num; i++) {
341             close(s->read_msgfds[i]);
342         }
343         g_free(s->read_msgfds);
344         s->read_msgfds = NULL;
345         s->read_msgfds_num = 0;
346     }
347 
348     tcp_set_msgfds(chr, NULL, 0);
349     remove_fd_in_watch(chr);
350     object_unref(OBJECT(s->sioc));
351     s->sioc = NULL;
352     object_unref(OBJECT(s->ioc));
353     s->ioc = NULL;
354     g_free(chr->filename);
355     chr->filename = NULL;
356     s->connected = 0;
357 }
358 
359 static char *SocketAddress_to_str(const char *prefix, SocketAddress *addr,
360                                   bool is_listen, bool is_telnet)
361 {
362     switch (addr->type) {
363     case SOCKET_ADDRESS_TYPE_INET:
364         return g_strdup_printf("%s%s:%s:%s%s", prefix,
365                                is_telnet ? "telnet" : "tcp",
366                                addr->u.inet.host,
367                                addr->u.inet.port,
368                                is_listen ? ",server" : "");
369         break;
370     case SOCKET_ADDRESS_TYPE_UNIX:
371         return g_strdup_printf("%sunix:%s%s", prefix,
372                                addr->u.q_unix.path,
373                                is_listen ? ",server" : "");
374         break;
375     case SOCKET_ADDRESS_TYPE_FD:
376         return g_strdup_printf("%sfd:%s%s", prefix, addr->u.fd.str,
377                                is_listen ? ",server" : "");
378         break;
379     case SOCKET_ADDRESS_TYPE_VSOCK:
380         return g_strdup_printf("%svsock:%s:%s", prefix,
381                                addr->u.vsock.cid,
382                                addr->u.vsock.port);
383     default:
384         abort();
385     }
386 }
387 
388 static void update_disconnected_filename(SocketChardev *s)
389 {
390     Chardev *chr = CHARDEV(s);
391 
392     g_free(chr->filename);
393     chr->filename = SocketAddress_to_str("disconnected:", s->addr,
394                                          s->is_listen, s->is_telnet);
395 }
396 
397 static void tcp_chr_disconnect(Chardev *chr)
398 {
399     SocketChardev *s = SOCKET_CHARDEV(chr);
400 
401     if (!s->connected) {
402         return;
403     }
404 
405     tcp_chr_free_connection(chr);
406 
407     if (s->listen_ioc) {
408         s->listen_tag = qio_channel_add_watch(
409             QIO_CHANNEL(s->listen_ioc), G_IO_IN, tcp_chr_accept, chr, NULL);
410     }
411     update_disconnected_filename(s);
412     qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
413     if (s->reconnect_time) {
414         qemu_chr_socket_restart_timer(chr);
415     }
416 }
417 
418 static gboolean tcp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)
419 {
420     Chardev *chr = CHARDEV(opaque);
421     SocketChardev *s = SOCKET_CHARDEV(opaque);
422     uint8_t buf[CHR_READ_BUF_LEN];
423     int len, size;
424 
425     if (!s->connected || s->max_size <= 0) {
426         return TRUE;
427     }
428     len = sizeof(buf);
429     if (len > s->max_size) {
430         len = s->max_size;
431     }
432     size = tcp_chr_recv(chr, (void *)buf, len);
433     if (size == 0 || size == -1) {
434         /* connection closed */
435         tcp_chr_disconnect(chr);
436     } else if (size > 0) {
437         if (s->do_telnetopt) {
438             tcp_chr_process_IAC_bytes(chr, s, buf, &size);
439         }
440         if (size > 0) {
441             qemu_chr_be_write(chr, buf, size);
442         }
443     }
444 
445     return TRUE;
446 }
447 
448 static int tcp_chr_sync_read(Chardev *chr, const uint8_t *buf, int len)
449 {
450     SocketChardev *s = SOCKET_CHARDEV(chr);
451     int size;
452 
453     if (!s->connected) {
454         return 0;
455     }
456 
457     qio_channel_set_blocking(s->ioc, true, NULL);
458     size = tcp_chr_recv(chr, (void *) buf, len);
459     qio_channel_set_blocking(s->ioc, false, NULL);
460     if (size == 0) {
461         /* connection closed */
462         tcp_chr_disconnect(chr);
463     }
464 
465     return size;
466 }
467 
468 static char *sockaddr_to_str(struct sockaddr_storage *ss, socklen_t ss_len,
469                              struct sockaddr_storage *ps, socklen_t ps_len,
470                              bool is_listen, bool is_telnet)
471 {
472     char shost[NI_MAXHOST], sserv[NI_MAXSERV];
473     char phost[NI_MAXHOST], pserv[NI_MAXSERV];
474     const char *left = "", *right = "";
475 
476     switch (ss->ss_family) {
477 #ifndef _WIN32
478     case AF_UNIX:
479         return g_strdup_printf("unix:%s%s",
480                                ((struct sockaddr_un *)(ss))->sun_path,
481                                is_listen ? ",server" : "");
482 #endif
483     case AF_INET6:
484         left  = "[";
485         right = "]";
486         /* fall through */
487     case AF_INET:
488         getnameinfo((struct sockaddr *) ss, ss_len, shost, sizeof(shost),
489                     sserv, sizeof(sserv), NI_NUMERICHOST | NI_NUMERICSERV);
490         getnameinfo((struct sockaddr *) ps, ps_len, phost, sizeof(phost),
491                     pserv, sizeof(pserv), NI_NUMERICHOST | NI_NUMERICSERV);
492         return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s",
493                                is_telnet ? "telnet" : "tcp",
494                                left, shost, right, sserv,
495                                is_listen ? ",server" : "",
496                                left, phost, right, pserv);
497 
498     default:
499         return g_strdup_printf("unknown");
500     }
501 }
502 
503 static void tcp_chr_connect(void *opaque)
504 {
505     Chardev *chr = CHARDEV(opaque);
506     SocketChardev *s = SOCKET_CHARDEV(opaque);
507 
508     g_free(chr->filename);
509     chr->filename = sockaddr_to_str(
510         &s->sioc->localAddr, s->sioc->localAddrLen,
511         &s->sioc->remoteAddr, s->sioc->remoteAddrLen,
512         s->is_listen, s->is_telnet);
513 
514     s->connected = 1;
515     if (s->ioc) {
516         chr->gsource = io_add_watch_poll(chr, s->ioc,
517                                            tcp_chr_read_poll,
518                                            tcp_chr_read,
519                                            chr, chr->gcontext);
520     }
521     qemu_chr_be_event(chr, CHR_EVENT_OPENED);
522 }
523 
524 static void tcp_chr_update_read_handler(Chardev *chr)
525 {
526     SocketChardev *s = SOCKET_CHARDEV(chr);
527 
528     if (!s->connected) {
529         return;
530     }
531 
532     remove_fd_in_watch(chr);
533     if (s->ioc) {
534         chr->gsource = io_add_watch_poll(chr, s->ioc,
535                                            tcp_chr_read_poll,
536                                            tcp_chr_read, chr,
537                                            chr->gcontext);
538     }
539 }
540 
541 typedef struct {
542     Chardev *chr;
543     char buf[21];
544     size_t buflen;
545 } TCPChardevTelnetInit;
546 
547 static gboolean tcp_chr_telnet_init_io(QIOChannel *ioc,
548                                        GIOCondition cond G_GNUC_UNUSED,
549                                        gpointer user_data)
550 {
551     TCPChardevTelnetInit *init = user_data;
552     ssize_t ret;
553 
554     ret = qio_channel_write(ioc, init->buf, init->buflen, NULL);
555     if (ret < 0) {
556         if (ret == QIO_CHANNEL_ERR_BLOCK) {
557             ret = 0;
558         } else {
559             tcp_chr_disconnect(init->chr);
560             return FALSE;
561         }
562     }
563     init->buflen -= ret;
564 
565     if (init->buflen == 0) {
566         tcp_chr_connect(init->chr);
567         return FALSE;
568     }
569 
570     memmove(init->buf, init->buf + ret, init->buflen);
571 
572     return TRUE;
573 }
574 
575 static void tcp_chr_telnet_init(Chardev *chr)
576 {
577     SocketChardev *s = SOCKET_CHARDEV(chr);
578     TCPChardevTelnetInit *init = g_new0(TCPChardevTelnetInit, 1);
579     size_t n = 0;
580 
581 #define IACSET(x, a, b, c)                      \
582     do {                                        \
583         x[n++] = a;                             \
584         x[n++] = b;                             \
585         x[n++] = c;                             \
586     } while (0)
587 
588     init->chr = chr;
589     if (!s->is_tn3270) {
590         init->buflen = 12;
591         /* Prep the telnet negotion to put telnet in binary,
592          * no echo, single char mode */
593         IACSET(init->buf, 0xff, 0xfb, 0x01);  /* IAC WILL ECHO */
594         IACSET(init->buf, 0xff, 0xfb, 0x03);  /* IAC WILL Suppress go ahead */
595         IACSET(init->buf, 0xff, 0xfb, 0x00);  /* IAC WILL Binary */
596         IACSET(init->buf, 0xff, 0xfd, 0x00);  /* IAC DO Binary */
597     } else {
598         init->buflen = 21;
599         /* Prep the TN3270 negotion based on RFC1576 */
600         IACSET(init->buf, 0xff, 0xfd, 0x19);  /* IAC DO EOR */
601         IACSET(init->buf, 0xff, 0xfb, 0x19);  /* IAC WILL EOR */
602         IACSET(init->buf, 0xff, 0xfd, 0x00);  /* IAC DO BINARY */
603         IACSET(init->buf, 0xff, 0xfb, 0x00);  /* IAC WILL BINARY */
604         IACSET(init->buf, 0xff, 0xfd, 0x18);  /* IAC DO TERMINAL TYPE */
605         IACSET(init->buf, 0xff, 0xfa, 0x18);  /* IAC SB TERMINAL TYPE */
606         IACSET(init->buf, 0x01, 0xff, 0xf0);  /* SEND IAC SE */
607     }
608 
609 #undef IACSET
610 
611     qio_channel_add_watch(
612         s->ioc, G_IO_OUT,
613         tcp_chr_telnet_init_io,
614         init, NULL);
615 }
616 
617 
618 static void tcp_chr_tls_handshake(QIOTask *task,
619                                   gpointer user_data)
620 {
621     Chardev *chr = user_data;
622     SocketChardev *s = user_data;
623 
624     if (qio_task_propagate_error(task, NULL)) {
625         tcp_chr_disconnect(chr);
626     } else {
627         /* tn3270 does not support TLS yet */
628         if (s->do_telnetopt && !s->is_tn3270) {
629             tcp_chr_telnet_init(chr);
630         } else {
631             tcp_chr_connect(chr);
632         }
633     }
634 }
635 
636 
637 static void tcp_chr_tls_init(Chardev *chr)
638 {
639     SocketChardev *s = SOCKET_CHARDEV(chr);
640     QIOChannelTLS *tioc;
641     Error *err = NULL;
642     gchar *name;
643 
644     if (s->is_listen) {
645         tioc = qio_channel_tls_new_server(
646             s->ioc, s->tls_creds,
647             NULL, /* XXX Use an ACL */
648             &err);
649     } else {
650         tioc = qio_channel_tls_new_client(
651             s->ioc, s->tls_creds,
652             s->addr->u.inet.host,
653             &err);
654     }
655     if (tioc == NULL) {
656         error_free(err);
657         tcp_chr_disconnect(chr);
658         return;
659     }
660     name = g_strdup_printf("chardev-tls-%s-%s",
661                            s->is_listen ? "server" : "client",
662                            chr->label);
663     qio_channel_set_name(QIO_CHANNEL(tioc), name);
664     g_free(name);
665     object_unref(OBJECT(s->ioc));
666     s->ioc = QIO_CHANNEL(tioc);
667 
668     qio_channel_tls_handshake(tioc,
669                               tcp_chr_tls_handshake,
670                               chr,
671                               NULL);
672 }
673 
674 
675 static void tcp_chr_set_client_ioc_name(Chardev *chr,
676                                         QIOChannelSocket *sioc)
677 {
678     SocketChardev *s = SOCKET_CHARDEV(chr);
679     char *name;
680     name = g_strdup_printf("chardev-tcp-%s-%s",
681                            s->is_listen ? "server" : "client",
682                            chr->label);
683     qio_channel_set_name(QIO_CHANNEL(sioc), name);
684     g_free(name);
685 
686 }
687 
688 static int tcp_chr_new_client(Chardev *chr, QIOChannelSocket *sioc)
689 {
690     SocketChardev *s = SOCKET_CHARDEV(chr);
691 
692     if (s->ioc != NULL) {
693         return -1;
694     }
695 
696     s->ioc = QIO_CHANNEL(sioc);
697     object_ref(OBJECT(sioc));
698     s->sioc = sioc;
699     object_ref(OBJECT(sioc));
700 
701     qio_channel_set_blocking(s->ioc, false, NULL);
702 
703     if (s->do_nodelay) {
704         qio_channel_set_delay(s->ioc, false);
705     }
706     if (s->listen_tag) {
707         g_source_remove(s->listen_tag);
708         s->listen_tag = 0;
709     }
710 
711     if (s->tls_creds) {
712         tcp_chr_tls_init(chr);
713     } else {
714         if (s->do_telnetopt) {
715             tcp_chr_telnet_init(chr);
716         } else {
717             tcp_chr_connect(chr);
718         }
719     }
720 
721     return 0;
722 }
723 
724 
725 static int tcp_chr_add_client(Chardev *chr, int fd)
726 {
727     int ret;
728     QIOChannelSocket *sioc;
729 
730     sioc = qio_channel_socket_new_fd(fd, NULL);
731     if (!sioc) {
732         return -1;
733     }
734     tcp_chr_set_client_ioc_name(chr, sioc);
735     ret = tcp_chr_new_client(chr, sioc);
736     object_unref(OBJECT(sioc));
737     return ret;
738 }
739 
740 static gboolean tcp_chr_accept(QIOChannel *channel,
741                                GIOCondition cond,
742                                void *opaque)
743 {
744     Chardev *chr = CHARDEV(opaque);
745     QIOChannelSocket *sioc;
746 
747     sioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(channel),
748                                      NULL);
749     if (!sioc) {
750         return TRUE;
751     }
752 
753     tcp_chr_new_client(chr, sioc);
754 
755     object_unref(OBJECT(sioc));
756 
757     return TRUE;
758 }
759 
760 static int tcp_chr_wait_connected(Chardev *chr, Error **errp)
761 {
762     SocketChardev *s = SOCKET_CHARDEV(chr);
763     QIOChannelSocket *sioc;
764 
765     /* It can't wait on s->connected, since it is set asynchronously
766      * in TLS and telnet cases, only wait for an accepted socket */
767     while (!s->ioc) {
768         if (s->is_listen) {
769             info_report("QEMU waiting for connection on: %s",
770                         chr->filename);
771             qio_channel_set_blocking(QIO_CHANNEL(s->listen_ioc), true, NULL);
772             tcp_chr_accept(QIO_CHANNEL(s->listen_ioc), G_IO_IN, chr);
773             qio_channel_set_blocking(QIO_CHANNEL(s->listen_ioc), false, NULL);
774         } else {
775             sioc = qio_channel_socket_new();
776             tcp_chr_set_client_ioc_name(chr, sioc);
777             if (qio_channel_socket_connect_sync(sioc, s->addr, errp) < 0) {
778                 object_unref(OBJECT(sioc));
779                 return -1;
780             }
781             tcp_chr_new_client(chr, sioc);
782             object_unref(OBJECT(sioc));
783         }
784     }
785 
786     return 0;
787 }
788 
789 static void char_socket_finalize(Object *obj)
790 {
791     Chardev *chr = CHARDEV(obj);
792     SocketChardev *s = SOCKET_CHARDEV(obj);
793 
794     tcp_chr_free_connection(chr);
795 
796     if (s->reconnect_timer) {
797         g_source_remove(s->reconnect_timer);
798         s->reconnect_timer = 0;
799     }
800     qapi_free_SocketAddress(s->addr);
801     if (s->listen_tag) {
802         g_source_remove(s->listen_tag);
803         s->listen_tag = 0;
804     }
805     if (s->listen_ioc) {
806         object_unref(OBJECT(s->listen_ioc));
807     }
808     if (s->tls_creds) {
809         object_unref(OBJECT(s->tls_creds));
810     }
811 
812     qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
813 }
814 
815 static void qemu_chr_socket_connected(QIOTask *task, void *opaque)
816 {
817     QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
818     Chardev *chr = CHARDEV(opaque);
819     SocketChardev *s = SOCKET_CHARDEV(chr);
820     Error *err = NULL;
821 
822     if (qio_task_propagate_error(task, &err)) {
823         check_report_connect_error(chr, err);
824         error_free(err);
825         goto cleanup;
826     }
827 
828     s->connect_err_reported = false;
829     tcp_chr_new_client(chr, sioc);
830 
831 cleanup:
832     object_unref(OBJECT(sioc));
833 }
834 
835 static gboolean socket_reconnect_timeout(gpointer opaque)
836 {
837     Chardev *chr = CHARDEV(opaque);
838     SocketChardev *s = SOCKET_CHARDEV(opaque);
839     QIOChannelSocket *sioc;
840 
841     s->reconnect_timer = 0;
842 
843     if (chr->be_open) {
844         return false;
845     }
846 
847     sioc = qio_channel_socket_new();
848     tcp_chr_set_client_ioc_name(chr, sioc);
849     qio_channel_socket_connect_async(sioc, s->addr,
850                                      qemu_chr_socket_connected,
851                                      chr, NULL);
852 
853     return false;
854 }
855 
856 static void qmp_chardev_open_socket(Chardev *chr,
857                                     ChardevBackend *backend,
858                                     bool *be_opened,
859                                     Error **errp)
860 {
861     SocketChardev *s = SOCKET_CHARDEV(chr);
862     ChardevSocket *sock = backend->u.socket.data;
863     bool do_nodelay     = sock->has_nodelay ? sock->nodelay : false;
864     bool is_listen      = sock->has_server  ? sock->server  : true;
865     bool is_telnet      = sock->has_telnet  ? sock->telnet  : false;
866     bool is_tn3270      = sock->has_tn3270  ? sock->tn3270  : false;
867     bool is_waitconnect = sock->has_wait    ? sock->wait    : false;
868     int64_t reconnect   = sock->has_reconnect ? sock->reconnect : 0;
869     QIOChannelSocket *sioc = NULL;
870     SocketAddress *addr;
871 
872     s->is_listen = is_listen;
873     s->is_telnet = is_telnet;
874     s->is_tn3270 = is_tn3270;
875     s->do_nodelay = do_nodelay;
876     if (sock->tls_creds) {
877         Object *creds;
878         creds = object_resolve_path_component(
879             object_get_objects_root(), sock->tls_creds);
880         if (!creds) {
881             error_setg(errp, "No TLS credentials with id '%s'",
882                        sock->tls_creds);
883             goto error;
884         }
885         s->tls_creds = (QCryptoTLSCreds *)
886             object_dynamic_cast(creds,
887                                 TYPE_QCRYPTO_TLS_CREDS);
888         if (!s->tls_creds) {
889             error_setg(errp, "Object with id '%s' is not TLS credentials",
890                        sock->tls_creds);
891             goto error;
892         }
893         object_ref(OBJECT(s->tls_creds));
894         if (is_listen) {
895             if (s->tls_creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
896                 error_setg(errp, "%s",
897                            "Expected TLS credentials for server endpoint");
898                 goto error;
899             }
900         } else {
901             if (s->tls_creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
902                 error_setg(errp, "%s",
903                            "Expected TLS credentials for client endpoint");
904                 goto error;
905             }
906         }
907     }
908 
909     s->addr = addr = socket_address_flatten(sock->addr);
910 
911     qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE);
912     /* TODO SOCKET_ADDRESS_FD where fd has AF_UNIX */
913     if (addr->type == SOCKET_ADDRESS_TYPE_UNIX) {
914         qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_FD_PASS);
915     }
916 
917     /* be isn't opened until we get a connection */
918     *be_opened = false;
919 
920     update_disconnected_filename(s);
921 
922     if (is_listen) {
923         if (is_telnet || is_tn3270) {
924             s->do_telnetopt = 1;
925         }
926     } else if (reconnect > 0) {
927         s->reconnect_time = reconnect;
928     }
929 
930     if (s->reconnect_time) {
931         sioc = qio_channel_socket_new();
932         tcp_chr_set_client_ioc_name(chr, sioc);
933         qio_channel_socket_connect_async(sioc, s->addr,
934                                          qemu_chr_socket_connected,
935                                          chr, NULL);
936     } else {
937         if (s->is_listen) {
938             char *name;
939             sioc = qio_channel_socket_new();
940 
941             name = g_strdup_printf("chardev-tcp-listener-%s", chr->label);
942             qio_channel_set_name(QIO_CHANNEL(sioc), name);
943             g_free(name);
944 
945             if (qio_channel_socket_listen_sync(sioc, s->addr, errp) < 0) {
946                 goto error;
947             }
948 
949             qapi_free_SocketAddress(s->addr);
950             s->addr = socket_local_address(sioc->fd, errp);
951             update_disconnected_filename(s);
952 
953             s->listen_ioc = sioc;
954             if (is_waitconnect &&
955                 qemu_chr_wait_connected(chr, errp) < 0) {
956                 return;
957             }
958             if (!s->ioc) {
959                 s->listen_tag = qio_channel_add_watch(
960                     QIO_CHANNEL(s->listen_ioc), G_IO_IN,
961                     tcp_chr_accept, chr, NULL);
962             }
963         } else if (qemu_chr_wait_connected(chr, errp) < 0) {
964             goto error;
965         }
966     }
967 
968     return;
969 
970 error:
971     if (sioc) {
972         object_unref(OBJECT(sioc));
973     }
974 }
975 
976 static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
977                                   Error **errp)
978 {
979     bool is_listen      = qemu_opt_get_bool(opts, "server", false);
980     bool is_waitconnect = is_listen && qemu_opt_get_bool(opts, "wait", true);
981     bool is_telnet      = qemu_opt_get_bool(opts, "telnet", false);
982     bool is_tn3270      = qemu_opt_get_bool(opts, "tn3270", false);
983     bool do_nodelay     = !qemu_opt_get_bool(opts, "delay", true);
984     int64_t reconnect   = qemu_opt_get_number(opts, "reconnect", 0);
985     const char *path = qemu_opt_get(opts, "path");
986     const char *host = qemu_opt_get(opts, "host");
987     const char *port = qemu_opt_get(opts, "port");
988     const char *tls_creds = qemu_opt_get(opts, "tls-creds");
989     SocketAddressLegacy *addr;
990     ChardevSocket *sock;
991 
992     backend->type = CHARDEV_BACKEND_KIND_SOCKET;
993     if (!path) {
994         if (!host) {
995             error_setg(errp, "chardev: socket: no host given");
996             return;
997         }
998         if (!port) {
999             error_setg(errp, "chardev: socket: no port given");
1000             return;
1001         }
1002     } else {
1003         if (tls_creds) {
1004             error_setg(errp, "TLS can only be used over TCP socket");
1005             return;
1006         }
1007     }
1008 
1009     sock = backend->u.socket.data = g_new0(ChardevSocket, 1);
1010     qemu_chr_parse_common(opts, qapi_ChardevSocket_base(sock));
1011 
1012     sock->has_nodelay = true;
1013     sock->nodelay = do_nodelay;
1014     sock->has_server = true;
1015     sock->server = is_listen;
1016     sock->has_telnet = true;
1017     sock->telnet = is_telnet;
1018     sock->has_tn3270 = true;
1019     sock->tn3270 = is_tn3270;
1020     sock->has_wait = true;
1021     sock->wait = is_waitconnect;
1022     sock->has_reconnect = true;
1023     sock->reconnect = reconnect;
1024     sock->tls_creds = g_strdup(tls_creds);
1025 
1026     addr = g_new0(SocketAddressLegacy, 1);
1027     if (path) {
1028         UnixSocketAddress *q_unix;
1029         addr->type = SOCKET_ADDRESS_LEGACY_KIND_UNIX;
1030         q_unix = addr->u.q_unix.data = g_new0(UnixSocketAddress, 1);
1031         q_unix->path = g_strdup(path);
1032     } else {
1033         addr->type = SOCKET_ADDRESS_LEGACY_KIND_INET;
1034         addr->u.inet.data = g_new(InetSocketAddress, 1);
1035         *addr->u.inet.data = (InetSocketAddress) {
1036             .host = g_strdup(host),
1037             .port = g_strdup(port),
1038             .has_to = qemu_opt_get(opts, "to"),
1039             .to = qemu_opt_get_number(opts, "to", 0),
1040             .has_ipv4 = qemu_opt_get(opts, "ipv4"),
1041             .ipv4 = qemu_opt_get_bool(opts, "ipv4", 0),
1042             .has_ipv6 = qemu_opt_get(opts, "ipv6"),
1043             .ipv6 = qemu_opt_get_bool(opts, "ipv6", 0),
1044         };
1045     }
1046     sock->addr = addr;
1047 }
1048 
1049 static void
1050 char_socket_get_addr(Object *obj, Visitor *v, const char *name,
1051                      void *opaque, Error **errp)
1052 {
1053     SocketChardev *s = SOCKET_CHARDEV(obj);
1054 
1055     visit_type_SocketAddress(v, name, &s->addr, errp);
1056 }
1057 
1058 static bool
1059 char_socket_get_connected(Object *obj, Error **errp)
1060 {
1061     SocketChardev *s = SOCKET_CHARDEV(obj);
1062 
1063     return s->connected;
1064 }
1065 
1066 static void char_socket_class_init(ObjectClass *oc, void *data)
1067 {
1068     ChardevClass *cc = CHARDEV_CLASS(oc);
1069 
1070     cc->parse = qemu_chr_parse_socket;
1071     cc->open = qmp_chardev_open_socket;
1072     cc->chr_wait_connected = tcp_chr_wait_connected;
1073     cc->chr_write = tcp_chr_write;
1074     cc->chr_sync_read = tcp_chr_sync_read;
1075     cc->chr_disconnect = tcp_chr_disconnect;
1076     cc->get_msgfds = tcp_get_msgfds;
1077     cc->set_msgfds = tcp_set_msgfds;
1078     cc->chr_add_client = tcp_chr_add_client;
1079     cc->chr_add_watch = tcp_chr_add_watch;
1080     cc->chr_update_read_handler = tcp_chr_update_read_handler;
1081 
1082     object_class_property_add(oc, "addr", "SocketAddress",
1083                               char_socket_get_addr, NULL,
1084                               NULL, NULL, &error_abort);
1085 
1086     object_class_property_add_bool(oc, "connected", char_socket_get_connected,
1087                                    NULL, &error_abort);
1088 }
1089 
1090 static const TypeInfo char_socket_type_info = {
1091     .name = TYPE_CHARDEV_SOCKET,
1092     .parent = TYPE_CHARDEV,
1093     .instance_size = sizeof(SocketChardev),
1094     .instance_finalize = char_socket_finalize,
1095     .class_init = char_socket_class_init,
1096 };
1097 
1098 static void register_types(void)
1099 {
1100     type_register_static(&char_socket_type_info);
1101 }
1102 
1103 type_init(register_types);
1104