xref: /openbmc/qemu/chardev/char-socket.c (revision 6edfca9e)
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 
25 #include "qemu/osdep.h"
26 #include "chardev/char.h"
27 #include "io/channel-socket.h"
28 #include "io/channel-websock.h"
29 #include "qemu/error-report.h"
30 #include "qemu/module.h"
31 #include "qemu/option.h"
32 #include "qapi/error.h"
33 #include "qapi/clone-visitor.h"
34 #include "qapi/qapi-visit-sockets.h"
35 #include "qemu/yank.h"
36 
37 #include "chardev/char-io.h"
38 #include "chardev/char-socket.h"
39 
40 static gboolean socket_reconnect_timeout(gpointer opaque);
41 static void tcp_chr_telnet_init(Chardev *chr);
42 
43 static void tcp_chr_change_state(SocketChardev *s, TCPChardevState state)
44 {
45     switch (state) {
46     case TCP_CHARDEV_STATE_DISCONNECTED:
47         break;
48     case TCP_CHARDEV_STATE_CONNECTING:
49         assert(s->state == TCP_CHARDEV_STATE_DISCONNECTED);
50         break;
51     case TCP_CHARDEV_STATE_CONNECTED:
52         assert(s->state == TCP_CHARDEV_STATE_CONNECTING);
53         break;
54     }
55     s->state = state;
56 }
57 
58 static void tcp_chr_reconn_timer_cancel(SocketChardev *s)
59 {
60     if (s->reconnect_timer) {
61         g_source_destroy(s->reconnect_timer);
62         g_source_unref(s->reconnect_timer);
63         s->reconnect_timer = NULL;
64     }
65 }
66 
67 static void qemu_chr_socket_restart_timer(Chardev *chr)
68 {
69     SocketChardev *s = SOCKET_CHARDEV(chr);
70     char *name;
71 
72     assert(s->state == TCP_CHARDEV_STATE_DISCONNECTED);
73     assert(!s->reconnect_timer);
74     name = g_strdup_printf("chardev-socket-reconnect-%s", chr->label);
75     s->reconnect_timer = qemu_chr_timeout_add_ms(chr,
76                                                  s->reconnect_time * 1000,
77                                                  socket_reconnect_timeout,
78                                                  chr);
79     g_source_set_name(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_reportf_err(err,
90                           "Unable to connect character device %s: ",
91                           chr->label);
92         s->connect_err_reported = true;
93     } else {
94         error_free(err);
95     }
96     qemu_chr_socket_restart_timer(chr);
97 }
98 
99 static void tcp_chr_accept(QIONetListener *listener,
100                            QIOChannelSocket *cioc,
101                            void *opaque);
102 
103 static int tcp_chr_read_poll(void *opaque);
104 static void tcp_chr_disconnect_locked(Chardev *chr);
105 
106 /* Called with chr_write_lock held.  */
107 static int tcp_chr_write(Chardev *chr, const uint8_t *buf, int len)
108 {
109     SocketChardev *s = SOCKET_CHARDEV(chr);
110 
111     if (s->state == TCP_CHARDEV_STATE_CONNECTED) {
112         int ret =  io_channel_send_full(s->ioc, buf, len,
113                                         s->write_msgfds,
114                                         s->write_msgfds_num);
115 
116         /* free the written msgfds in any cases
117          * other than ret < 0 && errno == EAGAIN
118          */
119         if (!(ret < 0 && EAGAIN == errno)
120             && s->write_msgfds_num) {
121             g_free(s->write_msgfds);
122             s->write_msgfds = 0;
123             s->write_msgfds_num = 0;
124         }
125 
126         if (ret < 0 && errno != EAGAIN) {
127             if (tcp_chr_read_poll(chr) <= 0) {
128                 /* Perform disconnect and return error. */
129                 tcp_chr_disconnect_locked(chr);
130             } /* else let the read handler finish it properly */
131         }
132 
133         return ret;
134     } else {
135         /* Indicate an error. */
136         errno = EIO;
137         return -1;
138     }
139 }
140 
141 static int tcp_chr_read_poll(void *opaque)
142 {
143     Chardev *chr = CHARDEV(opaque);
144     SocketChardev *s = SOCKET_CHARDEV(opaque);
145     if (s->state != TCP_CHARDEV_STATE_CONNECTED) {
146         return 0;
147     }
148     s->max_size = qemu_chr_be_can_write(chr);
149     return s->max_size;
150 }
151 
152 static void tcp_chr_process_IAC_bytes(Chardev *chr,
153                                       SocketChardev *s,
154                                       uint8_t *buf, int *size)
155 {
156     /* Handle any telnet or tn3270 client's basic IAC options.
157      * For telnet options, it satisfies char by char mode with no echo.
158      * For tn3270 options, it satisfies binary mode with EOR.
159      * All IAC options will be removed from the buf and the do_opt
160      * pointer will be used to track the state of the width of the
161      * IAC information.
162      *
163      * RFC854: "All TELNET commands consist of at least a two byte sequence.
164      * The commands dealing with option negotiation are three byte sequences,
165      * the third byte being the code for the option referenced."
166      * "IAC BREAK", "IAC IP", "IAC NOP" and the double IAC are two bytes.
167      * "IAC SB", "IAC SE" and "IAC EOR" are saved to split up data boundary
168      * for tn3270.
169      * NOP, Break and Interrupt Process(IP) might be encountered during a TN3270
170      * session, and NOP and IP need to be done later.
171      */
172 
173     int i;
174     int j = 0;
175 
176     for (i = 0; i < *size; i++) {
177         if (s->do_telnetopt > 1) {
178             if ((unsigned char)buf[i] == IAC && s->do_telnetopt == 2) {
179                 /* Double IAC means send an IAC */
180                 if (j != i) {
181                     buf[j] = buf[i];
182                 }
183                 j++;
184                 s->do_telnetopt = 1;
185             } else {
186                 if ((unsigned char)buf[i] == IAC_BREAK
187                     && s->do_telnetopt == 2) {
188                     /* Handle IAC break commands by sending a serial break */
189                     qemu_chr_be_event(chr, CHR_EVENT_BREAK);
190                     s->do_telnetopt++;
191                 } else if (s->is_tn3270 && ((unsigned char)buf[i] == IAC_EOR
192                            || (unsigned char)buf[i] == IAC_SB
193                            || (unsigned char)buf[i] == IAC_SE)
194                            && s->do_telnetopt == 2) {
195                     buf[j++] = IAC;
196                     buf[j++] = buf[i];
197                     s->do_telnetopt++;
198                 } else if (s->is_tn3270 && ((unsigned char)buf[i] == IAC_IP
199                            || (unsigned char)buf[i] == IAC_NOP)
200                            && s->do_telnetopt == 2) {
201                     /* TODO: IP and NOP need to be implemented later. */
202                     s->do_telnetopt++;
203                 }
204                 s->do_telnetopt++;
205             }
206             if (s->do_telnetopt >= 4) {
207                 s->do_telnetopt = 1;
208             }
209         } else {
210             if ((unsigned char)buf[i] == IAC) {
211                 s->do_telnetopt = 2;
212             } else {
213                 if (j != i) {
214                     buf[j] = buf[i];
215                 }
216                 j++;
217             }
218         }
219     }
220     *size = j;
221 }
222 
223 static int tcp_get_msgfds(Chardev *chr, int *fds, int num)
224 {
225     SocketChardev *s = SOCKET_CHARDEV(chr);
226 
227     int to_copy = (s->read_msgfds_num < num) ? s->read_msgfds_num : num;
228 
229     assert(num <= TCP_MAX_FDS);
230 
231     if (to_copy) {
232         int i;
233 
234         memcpy(fds, s->read_msgfds, to_copy * sizeof(int));
235 
236         /* Close unused fds */
237         for (i = to_copy; i < s->read_msgfds_num; i++) {
238             close(s->read_msgfds[i]);
239         }
240 
241         g_free(s->read_msgfds);
242         s->read_msgfds = 0;
243         s->read_msgfds_num = 0;
244     }
245 
246     return to_copy;
247 }
248 
249 static int tcp_set_msgfds(Chardev *chr, int *fds, int num)
250 {
251     SocketChardev *s = SOCKET_CHARDEV(chr);
252 
253     /* clear old pending fd array */
254     g_free(s->write_msgfds);
255     s->write_msgfds = NULL;
256     s->write_msgfds_num = 0;
257 
258     if ((s->state != TCP_CHARDEV_STATE_CONNECTED) ||
259         !qio_channel_has_feature(s->ioc,
260                                  QIO_CHANNEL_FEATURE_FD_PASS)) {
261         return -1;
262     }
263 
264     if (num) {
265         s->write_msgfds = g_new(int, num);
266         memcpy(s->write_msgfds, fds, num * sizeof(int));
267     }
268 
269     s->write_msgfds_num = num;
270 
271     return 0;
272 }
273 
274 static ssize_t tcp_chr_recv(Chardev *chr, char *buf, size_t len)
275 {
276     SocketChardev *s = SOCKET_CHARDEV(chr);
277     struct iovec iov = { .iov_base = buf, .iov_len = len };
278     int ret;
279     size_t i;
280     int *msgfds = NULL;
281     size_t msgfds_num = 0;
282 
283     if (qio_channel_has_feature(s->ioc, QIO_CHANNEL_FEATURE_FD_PASS)) {
284         ret = qio_channel_readv_full(s->ioc, &iov, 1,
285                                      &msgfds, &msgfds_num,
286                                      0, NULL);
287     } else {
288         ret = qio_channel_readv_full(s->ioc, &iov, 1,
289                                      NULL, NULL,
290                                      0, NULL);
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_socket_set_block(fd);
315 
316 #ifndef MSG_CMSG_CLOEXEC
317         qemu_set_cloexec(fd);
318 #endif
319     }
320 
321     if (ret == QIO_CHANNEL_ERR_BLOCK) {
322         errno = EAGAIN;
323         ret = -1;
324     } else if (ret == -1) {
325         errno = EIO;
326     }
327 
328     return ret;
329 }
330 
331 static GSource *tcp_chr_add_watch(Chardev *chr, GIOCondition cond)
332 {
333     SocketChardev *s = SOCKET_CHARDEV(chr);
334     if (!s->ioc) {
335         return NULL;
336     }
337     return qio_channel_create_watch(s->ioc, cond);
338 }
339 
340 static void remove_hup_source(SocketChardev *s)
341 {
342     if (s->hup_source != NULL) {
343         g_source_destroy(s->hup_source);
344         g_source_unref(s->hup_source);
345         s->hup_source = NULL;
346     }
347 }
348 
349 static void char_socket_yank_iochannel(void *opaque)
350 {
351     QIOChannel *ioc = QIO_CHANNEL(opaque);
352 
353     qio_channel_shutdown(ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
354 }
355 
356 static void tcp_chr_free_connection(Chardev *chr)
357 {
358     SocketChardev *s = SOCKET_CHARDEV(chr);
359     int i;
360 
361     if (s->read_msgfds_num) {
362         for (i = 0; i < s->read_msgfds_num; i++) {
363             close(s->read_msgfds[i]);
364         }
365         g_free(s->read_msgfds);
366         s->read_msgfds = NULL;
367         s->read_msgfds_num = 0;
368     }
369 
370     remove_hup_source(s);
371 
372     tcp_set_msgfds(chr, NULL, 0);
373     remove_fd_in_watch(chr);
374     if (s->registered_yank &&
375         (s->state == TCP_CHARDEV_STATE_CONNECTING
376         || s->state == TCP_CHARDEV_STATE_CONNECTED)) {
377         yank_unregister_function(CHARDEV_YANK_INSTANCE(chr->label),
378                                  char_socket_yank_iochannel,
379                                  QIO_CHANNEL(s->sioc));
380     }
381     object_unref(OBJECT(s->sioc));
382     s->sioc = NULL;
383     object_unref(OBJECT(s->ioc));
384     s->ioc = NULL;
385     g_free(chr->filename);
386     chr->filename = NULL;
387     tcp_chr_change_state(s, TCP_CHARDEV_STATE_DISCONNECTED);
388 }
389 
390 static const char *qemu_chr_socket_protocol(SocketChardev *s)
391 {
392     if (s->is_telnet) {
393         return "telnet";
394     }
395     return s->is_websock ? "websocket" : "tcp";
396 }
397 
398 static char *qemu_chr_socket_address(SocketChardev *s, const char *prefix)
399 {
400     switch (s->addr->type) {
401     case SOCKET_ADDRESS_TYPE_INET:
402         return g_strdup_printf("%s%s:%s:%s%s", prefix,
403                                qemu_chr_socket_protocol(s),
404                                s->addr->u.inet.host,
405                                s->addr->u.inet.port,
406                                s->is_listen ? ",server=on" : "");
407         break;
408     case SOCKET_ADDRESS_TYPE_UNIX:
409     {
410         const char *tight = "", *abstract = "";
411         UnixSocketAddress *sa = &s->addr->u.q_unix;
412 
413 #ifdef CONFIG_LINUX
414         if (sa->has_abstract && sa->abstract) {
415             abstract = ",abstract=on";
416             if (sa->has_tight && sa->tight) {
417                 tight = ",tight=on";
418             }
419         }
420 #endif
421 
422         return g_strdup_printf("%sunix:%s%s%s%s", prefix, sa->path,
423                                abstract, tight,
424                                s->is_listen ? ",server=on" : "");
425         break;
426     }
427     case SOCKET_ADDRESS_TYPE_FD:
428         return g_strdup_printf("%sfd:%s%s", prefix, s->addr->u.fd.str,
429                                s->is_listen ? ",server=on" : "");
430         break;
431     case SOCKET_ADDRESS_TYPE_VSOCK:
432         return g_strdup_printf("%svsock:%s:%s", prefix,
433                                s->addr->u.vsock.cid,
434                                s->addr->u.vsock.port);
435     default:
436         abort();
437     }
438 }
439 
440 static void update_disconnected_filename(SocketChardev *s)
441 {
442     Chardev *chr = CHARDEV(s);
443 
444     g_free(chr->filename);
445     if (s->addr) {
446         chr->filename = qemu_chr_socket_address(s, "disconnected:");
447     } else {
448         chr->filename = g_strdup("disconnected:socket");
449     }
450 }
451 
452 /* NB may be called even if tcp_chr_connect has not been
453  * reached, due to TLS or telnet initialization failure,
454  * so can *not* assume s->state == TCP_CHARDEV_STATE_CONNECTED
455  * This must be called with chr->chr_write_lock held.
456  */
457 static void tcp_chr_disconnect_locked(Chardev *chr)
458 {
459     SocketChardev *s = SOCKET_CHARDEV(chr);
460     bool emit_close = s->state == TCP_CHARDEV_STATE_CONNECTED;
461 
462     tcp_chr_free_connection(chr);
463 
464     if (s->listener) {
465         qio_net_listener_set_client_func_full(s->listener, tcp_chr_accept,
466                                               chr, NULL, chr->gcontext);
467     }
468     update_disconnected_filename(s);
469     if (emit_close) {
470         qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
471     }
472     if (s->reconnect_time && !s->reconnect_timer) {
473         qemu_chr_socket_restart_timer(chr);
474     }
475 }
476 
477 static void tcp_chr_disconnect(Chardev *chr)
478 {
479     qemu_mutex_lock(&chr->chr_write_lock);
480     tcp_chr_disconnect_locked(chr);
481     qemu_mutex_unlock(&chr->chr_write_lock);
482 }
483 
484 static gboolean tcp_chr_read(QIOChannel *chan, GIOCondition cond, void *opaque)
485 {
486     Chardev *chr = CHARDEV(opaque);
487     SocketChardev *s = SOCKET_CHARDEV(opaque);
488     uint8_t buf[CHR_READ_BUF_LEN];
489     int len, size;
490 
491     if ((s->state != TCP_CHARDEV_STATE_CONNECTED) ||
492         s->max_size <= 0) {
493         return TRUE;
494     }
495     len = sizeof(buf);
496     if (len > s->max_size) {
497         len = s->max_size;
498     }
499     size = tcp_chr_recv(chr, (void *)buf, len);
500     if (size == 0 || (size == -1 && errno != EAGAIN)) {
501         /* connection closed */
502         tcp_chr_disconnect(chr);
503     } else if (size > 0) {
504         if (s->do_telnetopt) {
505             tcp_chr_process_IAC_bytes(chr, s, buf, &size);
506         }
507         if (size > 0) {
508             qemu_chr_be_write(chr, buf, size);
509         }
510     }
511 
512     return TRUE;
513 }
514 
515 static gboolean tcp_chr_hup(QIOChannel *channel,
516                                GIOCondition cond,
517                                void *opaque)
518 {
519     Chardev *chr = CHARDEV(opaque);
520     tcp_chr_disconnect(chr);
521     return G_SOURCE_REMOVE;
522 }
523 
524 static int tcp_chr_sync_read(Chardev *chr, const uint8_t *buf, int len)
525 {
526     SocketChardev *s = SOCKET_CHARDEV(chr);
527     int size;
528     int saved_errno;
529 
530     if (s->state != TCP_CHARDEV_STATE_CONNECTED) {
531         return 0;
532     }
533 
534     qio_channel_set_blocking(s->ioc, true, NULL);
535     size = tcp_chr_recv(chr, (void *) buf, len);
536     saved_errno = errno;
537     if (s->state != TCP_CHARDEV_STATE_DISCONNECTED) {
538         qio_channel_set_blocking(s->ioc, false, NULL);
539     }
540     if (size == 0) {
541         /* connection closed */
542         tcp_chr_disconnect(chr);
543     }
544 
545     errno = saved_errno;
546     return size;
547 }
548 
549 static char *qemu_chr_compute_filename(SocketChardev *s)
550 {
551     struct sockaddr_storage *ss = &s->sioc->localAddr;
552     struct sockaddr_storage *ps = &s->sioc->remoteAddr;
553     socklen_t ss_len = s->sioc->localAddrLen;
554     socklen_t ps_len = s->sioc->remoteAddrLen;
555     char shost[NI_MAXHOST], sserv[NI_MAXSERV];
556     char phost[NI_MAXHOST], pserv[NI_MAXSERV];
557     const char *left = "", *right = "";
558 
559     switch (ss->ss_family) {
560     case AF_UNIX:
561         return g_strdup_printf("unix:%s%s",
562                                ((struct sockaddr_un *)(ss))->sun_path,
563                                s->is_listen ? ",server=on" : "");
564     case AF_INET6:
565         left  = "[";
566         right = "]";
567         /* fall through */
568     case AF_INET:
569         getnameinfo((struct sockaddr *) ss, ss_len, shost, sizeof(shost),
570                     sserv, sizeof(sserv), NI_NUMERICHOST | NI_NUMERICSERV);
571         getnameinfo((struct sockaddr *) ps, ps_len, phost, sizeof(phost),
572                     pserv, sizeof(pserv), NI_NUMERICHOST | NI_NUMERICSERV);
573         return g_strdup_printf("%s:%s%s%s:%s%s <-> %s%s%s:%s",
574                                qemu_chr_socket_protocol(s),
575                                left, shost, right, sserv,
576                                s->is_listen ? ",server=on" : "",
577                                left, phost, right, pserv);
578 
579     default:
580         return g_strdup_printf("unknown");
581     }
582 }
583 
584 static void update_ioc_handlers(SocketChardev *s)
585 {
586     Chardev *chr = CHARDEV(s);
587 
588     if (s->state != TCP_CHARDEV_STATE_CONNECTED) {
589         return;
590     }
591 
592     remove_fd_in_watch(chr);
593     chr->gsource = io_add_watch_poll(chr, s->ioc,
594                                      tcp_chr_read_poll,
595                                      tcp_chr_read, chr,
596                                      chr->gcontext);
597 
598     remove_hup_source(s);
599     s->hup_source = qio_channel_create_watch(s->ioc, G_IO_HUP);
600     g_source_set_callback(s->hup_source, (GSourceFunc)tcp_chr_hup,
601                           chr, NULL);
602     g_source_attach(s->hup_source, chr->gcontext);
603 }
604 
605 static void tcp_chr_connect(void *opaque)
606 {
607     Chardev *chr = CHARDEV(opaque);
608     SocketChardev *s = SOCKET_CHARDEV(opaque);
609 
610     g_free(chr->filename);
611     chr->filename = qemu_chr_compute_filename(s);
612 
613     tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTED);
614     update_ioc_handlers(s);
615     qemu_chr_be_event(chr, CHR_EVENT_OPENED);
616 }
617 
618 static void tcp_chr_telnet_destroy(SocketChardev *s)
619 {
620     if (s->telnet_source) {
621         g_source_destroy(s->telnet_source);
622         g_source_unref(s->telnet_source);
623         s->telnet_source = NULL;
624     }
625 }
626 
627 static void tcp_chr_update_read_handler(Chardev *chr)
628 {
629     SocketChardev *s = SOCKET_CHARDEV(chr);
630 
631     if (s->listener && s->state == TCP_CHARDEV_STATE_DISCONNECTED) {
632         /*
633          * It's possible that chardev context is changed in
634          * qemu_chr_be_update_read_handlers().  Reset it for QIO net
635          * listener if there is.
636          */
637         qio_net_listener_set_client_func_full(s->listener, tcp_chr_accept,
638                                               chr, NULL, chr->gcontext);
639     }
640 
641     if (s->telnet_source) {
642         tcp_chr_telnet_init(CHARDEV(s));
643     }
644 
645     update_ioc_handlers(s);
646 }
647 
648 static gboolean tcp_chr_telnet_init_io(QIOChannel *ioc,
649                                        GIOCondition cond G_GNUC_UNUSED,
650                                        gpointer user_data)
651 {
652     SocketChardev *s = user_data;
653     Chardev *chr = CHARDEV(s);
654     TCPChardevTelnetInit *init = s->telnet_init;
655     ssize_t ret;
656 
657     assert(init);
658 
659     ret = qio_channel_write(ioc, init->buf, init->buflen, NULL);
660     if (ret < 0) {
661         if (ret == QIO_CHANNEL_ERR_BLOCK) {
662             ret = 0;
663         } else {
664             tcp_chr_disconnect(chr);
665             goto end;
666         }
667     }
668     init->buflen -= ret;
669 
670     if (init->buflen == 0) {
671         tcp_chr_connect(chr);
672         goto end;
673     }
674 
675     memmove(init->buf, init->buf + ret, init->buflen);
676 
677     return G_SOURCE_CONTINUE;
678 
679 end:
680     g_free(s->telnet_init);
681     s->telnet_init = NULL;
682     g_source_unref(s->telnet_source);
683     s->telnet_source = NULL;
684     return G_SOURCE_REMOVE;
685 }
686 
687 static void tcp_chr_telnet_init(Chardev *chr)
688 {
689     SocketChardev *s = SOCKET_CHARDEV(chr);
690     TCPChardevTelnetInit *init;
691     size_t n = 0;
692 
693     /* Destroy existing task */
694     tcp_chr_telnet_destroy(s);
695 
696     if (s->telnet_init) {
697         /* We are possibly during a handshake already */
698         goto cont;
699     }
700 
701     s->telnet_init = g_new0(TCPChardevTelnetInit, 1);
702     init = s->telnet_init;
703 
704 #define IACSET(x, a, b, c)                      \
705     do {                                        \
706         x[n++] = a;                             \
707         x[n++] = b;                             \
708         x[n++] = c;                             \
709     } while (0)
710 
711     if (!s->is_tn3270) {
712         init->buflen = 12;
713         /* Prep the telnet negotion to put telnet in binary,
714          * no echo, single char mode */
715         IACSET(init->buf, 0xff, 0xfb, 0x01);  /* IAC WILL ECHO */
716         IACSET(init->buf, 0xff, 0xfb, 0x03);  /* IAC WILL Suppress go ahead */
717         IACSET(init->buf, 0xff, 0xfb, 0x00);  /* IAC WILL Binary */
718         IACSET(init->buf, 0xff, 0xfd, 0x00);  /* IAC DO Binary */
719     } else {
720         init->buflen = 21;
721         /* Prep the TN3270 negotion based on RFC1576 */
722         IACSET(init->buf, 0xff, 0xfd, 0x19);  /* IAC DO EOR */
723         IACSET(init->buf, 0xff, 0xfb, 0x19);  /* IAC WILL EOR */
724         IACSET(init->buf, 0xff, 0xfd, 0x00);  /* IAC DO BINARY */
725         IACSET(init->buf, 0xff, 0xfb, 0x00);  /* IAC WILL BINARY */
726         IACSET(init->buf, 0xff, 0xfd, 0x18);  /* IAC DO TERMINAL TYPE */
727         IACSET(init->buf, 0xff, 0xfa, 0x18);  /* IAC SB TERMINAL TYPE */
728         IACSET(init->buf, 0x01, 0xff, 0xf0);  /* SEND IAC SE */
729     }
730 
731 #undef IACSET
732 
733 cont:
734     s->telnet_source = qio_channel_add_watch_source(s->ioc, G_IO_OUT,
735                                                     tcp_chr_telnet_init_io,
736                                                     s, NULL,
737                                                     chr->gcontext);
738 }
739 
740 
741 static void tcp_chr_websock_handshake(QIOTask *task, gpointer user_data)
742 {
743     Chardev *chr = user_data;
744     SocketChardev *s = user_data;
745 
746     if (qio_task_propagate_error(task, NULL)) {
747         tcp_chr_disconnect(chr);
748     } else {
749         if (s->do_telnetopt) {
750             tcp_chr_telnet_init(chr);
751         } else {
752             tcp_chr_connect(chr);
753         }
754     }
755 }
756 
757 
758 static void tcp_chr_websock_init(Chardev *chr)
759 {
760     SocketChardev *s = SOCKET_CHARDEV(chr);
761     QIOChannelWebsock *wioc = NULL;
762     gchar *name;
763 
764     wioc = qio_channel_websock_new_server(s->ioc);
765 
766     name = g_strdup_printf("chardev-websocket-server-%s", chr->label);
767     qio_channel_set_name(QIO_CHANNEL(wioc), name);
768     g_free(name);
769     object_unref(OBJECT(s->ioc));
770     s->ioc = QIO_CHANNEL(wioc);
771 
772     qio_channel_websock_handshake(wioc, tcp_chr_websock_handshake, chr, NULL);
773 }
774 
775 
776 static void tcp_chr_tls_handshake(QIOTask *task,
777                                   gpointer user_data)
778 {
779     Chardev *chr = user_data;
780     SocketChardev *s = user_data;
781 
782     if (qio_task_propagate_error(task, NULL)) {
783         tcp_chr_disconnect(chr);
784     } else {
785         if (s->is_websock) {
786             tcp_chr_websock_init(chr);
787         } else if (s->do_telnetopt) {
788             tcp_chr_telnet_init(chr);
789         } else {
790             tcp_chr_connect(chr);
791         }
792     }
793 }
794 
795 
796 static void tcp_chr_tls_init(Chardev *chr)
797 {
798     SocketChardev *s = SOCKET_CHARDEV(chr);
799     QIOChannelTLS *tioc;
800     gchar *name;
801 
802     if (s->is_listen) {
803         tioc = qio_channel_tls_new_server(
804             s->ioc, s->tls_creds,
805             s->tls_authz,
806             NULL);
807     } else {
808         tioc = qio_channel_tls_new_client(
809             s->ioc, s->tls_creds,
810             s->addr->u.inet.host,
811             NULL);
812     }
813     if (tioc == NULL) {
814         tcp_chr_disconnect(chr);
815         return;
816     }
817     name = g_strdup_printf("chardev-tls-%s-%s",
818                            s->is_listen ? "server" : "client",
819                            chr->label);
820     qio_channel_set_name(QIO_CHANNEL(tioc), name);
821     g_free(name);
822     object_unref(OBJECT(s->ioc));
823     s->ioc = QIO_CHANNEL(tioc);
824 
825     qio_channel_tls_handshake(tioc,
826                               tcp_chr_tls_handshake,
827                               chr,
828                               NULL,
829                               chr->gcontext);
830 }
831 
832 
833 static void tcp_chr_set_client_ioc_name(Chardev *chr,
834                                         QIOChannelSocket *sioc)
835 {
836     SocketChardev *s = SOCKET_CHARDEV(chr);
837     char *name;
838     name = g_strdup_printf("chardev-tcp-%s-%s",
839                            s->is_listen ? "server" : "client",
840                            chr->label);
841     qio_channel_set_name(QIO_CHANNEL(sioc), name);
842     g_free(name);
843 
844 }
845 
846 static int tcp_chr_new_client(Chardev *chr, QIOChannelSocket *sioc)
847 {
848     SocketChardev *s = SOCKET_CHARDEV(chr);
849 
850     if (s->state != TCP_CHARDEV_STATE_CONNECTING) {
851         return -1;
852     }
853 
854     s->ioc = QIO_CHANNEL(sioc);
855     object_ref(OBJECT(sioc));
856     s->sioc = sioc;
857     object_ref(OBJECT(sioc));
858 
859     qio_channel_set_blocking(s->ioc, false, NULL);
860 
861     if (s->do_nodelay) {
862         qio_channel_set_delay(s->ioc, false);
863     }
864     if (s->listener) {
865         qio_net_listener_set_client_func_full(s->listener, NULL, NULL,
866                                               NULL, chr->gcontext);
867     }
868 
869     if (s->tls_creds) {
870         tcp_chr_tls_init(chr);
871     } else if (s->is_websock) {
872         tcp_chr_websock_init(chr);
873     } else if (s->do_telnetopt) {
874         tcp_chr_telnet_init(chr);
875     } else {
876         tcp_chr_connect(chr);
877     }
878 
879     return 0;
880 }
881 
882 
883 static int tcp_chr_add_client(Chardev *chr, int fd)
884 {
885     int ret;
886     QIOChannelSocket *sioc;
887     SocketChardev *s = SOCKET_CHARDEV(chr);
888 
889     if (s->state != TCP_CHARDEV_STATE_DISCONNECTED) {
890         return -1;
891     }
892 
893     sioc = qio_channel_socket_new_fd(fd, NULL);
894     if (!sioc) {
895         return -1;
896     }
897     tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
898     tcp_chr_set_client_ioc_name(chr, sioc);
899     if (s->registered_yank) {
900         yank_register_function(CHARDEV_YANK_INSTANCE(chr->label),
901                                char_socket_yank_iochannel,
902                                QIO_CHANNEL(sioc));
903     }
904     ret = tcp_chr_new_client(chr, sioc);
905     object_unref(OBJECT(sioc));
906     return ret;
907 }
908 
909 static void tcp_chr_accept(QIONetListener *listener,
910                            QIOChannelSocket *cioc,
911                            void *opaque)
912 {
913     Chardev *chr = CHARDEV(opaque);
914     SocketChardev *s = SOCKET_CHARDEV(chr);
915 
916     tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
917     tcp_chr_set_client_ioc_name(chr, cioc);
918     if (s->registered_yank) {
919         yank_register_function(CHARDEV_YANK_INSTANCE(chr->label),
920                                char_socket_yank_iochannel,
921                                QIO_CHANNEL(cioc));
922     }
923     tcp_chr_new_client(chr, cioc);
924 }
925 
926 
927 static int tcp_chr_connect_client_sync(Chardev *chr, Error **errp)
928 {
929     SocketChardev *s = SOCKET_CHARDEV(chr);
930     QIOChannelSocket *sioc = qio_channel_socket_new();
931     tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
932     tcp_chr_set_client_ioc_name(chr, sioc);
933     if (qio_channel_socket_connect_sync(sioc, s->addr, errp) < 0) {
934         tcp_chr_change_state(s, TCP_CHARDEV_STATE_DISCONNECTED);
935         object_unref(OBJECT(sioc));
936         return -1;
937     }
938     if (s->registered_yank) {
939         yank_register_function(CHARDEV_YANK_INSTANCE(chr->label),
940                                char_socket_yank_iochannel,
941                                QIO_CHANNEL(sioc));
942     }
943     tcp_chr_new_client(chr, sioc);
944     object_unref(OBJECT(sioc));
945     return 0;
946 }
947 
948 
949 static void tcp_chr_accept_server_sync(Chardev *chr)
950 {
951     SocketChardev *s = SOCKET_CHARDEV(chr);
952     QIOChannelSocket *sioc;
953     info_report("QEMU waiting for connection on: %s",
954                 chr->filename);
955     tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
956     sioc = qio_net_listener_wait_client(s->listener);
957     tcp_chr_set_client_ioc_name(chr, sioc);
958     if (s->registered_yank) {
959         yank_register_function(CHARDEV_YANK_INSTANCE(chr->label),
960                                char_socket_yank_iochannel,
961                                QIO_CHANNEL(sioc));
962     }
963     tcp_chr_new_client(chr, sioc);
964     object_unref(OBJECT(sioc));
965 }
966 
967 
968 static int tcp_chr_wait_connected(Chardev *chr, Error **errp)
969 {
970     SocketChardev *s = SOCKET_CHARDEV(chr);
971     const char *opts[] = { "telnet", "tn3270", "websock", "tls-creds" };
972     bool optset[] = { s->is_telnet, s->is_tn3270, s->is_websock, s->tls_creds };
973     size_t i;
974 
975     QEMU_BUILD_BUG_ON(G_N_ELEMENTS(opts) != G_N_ELEMENTS(optset));
976     for (i = 0; i < G_N_ELEMENTS(opts); i++) {
977         if (optset[i]) {
978             error_setg(errp,
979                        "'%s' option is incompatible with waiting for "
980                        "connection completion", opts[i]);
981             return -1;
982         }
983     }
984 
985     tcp_chr_reconn_timer_cancel(s);
986 
987     /*
988      * We expect states to be as follows:
989      *
990      *  - server
991      *    - wait   -> CONNECTED
992      *    - nowait -> DISCONNECTED
993      *  - client
994      *    - reconnect == 0 -> CONNECTED
995      *    - reconnect != 0 -> CONNECTING
996      *
997      */
998     if (s->state == TCP_CHARDEV_STATE_CONNECTING) {
999         if (!s->connect_task) {
1000             error_setg(errp,
1001                        "Unexpected 'connecting' state without connect task "
1002                        "while waiting for connection completion");
1003             return -1;
1004         }
1005         /*
1006          * tcp_chr_wait_connected should only ever be run from the
1007          * main loop thread associated with chr->gcontext, otherwise
1008          * qio_task_wait_thread has a dangerous race condition with
1009          * free'ing of the s->connect_task object.
1010          *
1011          * Acquiring the main context doesn't 100% prove we're in
1012          * the main loop thread, but it does at least guarantee
1013          * that the main loop won't be executed by another thread
1014          * avoiding the race condition with the task idle callback.
1015          */
1016         g_main_context_acquire(chr->gcontext);
1017         qio_task_wait_thread(s->connect_task);
1018         g_main_context_release(chr->gcontext);
1019 
1020         /*
1021          * The completion callback (qemu_chr_socket_connected) for
1022          * s->connect_task should have set this to NULL by the time
1023          * qio_task_wait_thread has returned.
1024          */
1025         assert(!s->connect_task);
1026 
1027         /*
1028          * NB we are *not* guaranteed to have "s->state == ..CONNECTED"
1029          * at this point as this first connect may be failed, so
1030          * allow the next loop to run regardless.
1031          */
1032     }
1033 
1034     while (s->state != TCP_CHARDEV_STATE_CONNECTED) {
1035         if (s->is_listen) {
1036             tcp_chr_accept_server_sync(chr);
1037         } else {
1038             Error *err = NULL;
1039             if (tcp_chr_connect_client_sync(chr, &err) < 0) {
1040                 if (s->reconnect_time) {
1041                     error_free(err);
1042                     g_usleep(s->reconnect_time * 1000ULL * 1000ULL);
1043                 } else {
1044                     error_propagate(errp, err);
1045                     return -1;
1046                 }
1047             }
1048         }
1049     }
1050 
1051     return 0;
1052 }
1053 
1054 static void char_socket_finalize(Object *obj)
1055 {
1056     Chardev *chr = CHARDEV(obj);
1057     SocketChardev *s = SOCKET_CHARDEV(obj);
1058 
1059     tcp_chr_free_connection(chr);
1060     tcp_chr_reconn_timer_cancel(s);
1061     qapi_free_SocketAddress(s->addr);
1062     tcp_chr_telnet_destroy(s);
1063     g_free(s->telnet_init);
1064     if (s->listener) {
1065         qio_net_listener_set_client_func_full(s->listener, NULL, NULL,
1066                                               NULL, chr->gcontext);
1067         object_unref(OBJECT(s->listener));
1068         s->listener = NULL;
1069     }
1070     if (s->tls_creds) {
1071         object_unref(OBJECT(s->tls_creds));
1072     }
1073     g_free(s->tls_authz);
1074     if (s->registered_yank) {
1075         /*
1076          * In the chardev-change special-case, we shouldn't unregister the yank
1077          * instance, as it still may be needed.
1078          */
1079         if (!chr->handover_yank_instance) {
1080             yank_unregister_instance(CHARDEV_YANK_INSTANCE(chr->label));
1081         }
1082     }
1083 
1084     qemu_chr_be_event(chr, CHR_EVENT_CLOSED);
1085 }
1086 
1087 static void qemu_chr_socket_connected(QIOTask *task, void *opaque)
1088 {
1089     QIOChannelSocket *sioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
1090     Chardev *chr = CHARDEV(opaque);
1091     SocketChardev *s = SOCKET_CHARDEV(chr);
1092     Error *err = NULL;
1093 
1094     s->connect_task = NULL;
1095 
1096     if (qio_task_propagate_error(task, &err)) {
1097         tcp_chr_change_state(s, TCP_CHARDEV_STATE_DISCONNECTED);
1098         if (s->registered_yank) {
1099             yank_unregister_function(CHARDEV_YANK_INSTANCE(chr->label),
1100                                      char_socket_yank_iochannel,
1101                                      QIO_CHANNEL(sioc));
1102         }
1103         check_report_connect_error(chr, err);
1104         goto cleanup;
1105     }
1106 
1107     s->connect_err_reported = false;
1108     tcp_chr_new_client(chr, sioc);
1109 
1110 cleanup:
1111     object_unref(OBJECT(sioc));
1112 }
1113 
1114 
1115 static void tcp_chr_connect_client_task(QIOTask *task,
1116                                         gpointer opaque)
1117 {
1118     QIOChannelSocket *ioc = QIO_CHANNEL_SOCKET(qio_task_get_source(task));
1119     SocketAddress *addr = opaque;
1120     Error *err = NULL;
1121 
1122     qio_channel_socket_connect_sync(ioc, addr, &err);
1123 
1124     qio_task_set_error(task, err);
1125 }
1126 
1127 
1128 static void tcp_chr_connect_client_async(Chardev *chr)
1129 {
1130     SocketChardev *s = SOCKET_CHARDEV(chr);
1131     QIOChannelSocket *sioc;
1132 
1133     tcp_chr_change_state(s, TCP_CHARDEV_STATE_CONNECTING);
1134     sioc = qio_channel_socket_new();
1135     tcp_chr_set_client_ioc_name(chr, sioc);
1136     if (s->registered_yank) {
1137         yank_register_function(CHARDEV_YANK_INSTANCE(chr->label),
1138                                char_socket_yank_iochannel,
1139                                QIO_CHANNEL(sioc));
1140     }
1141     /*
1142      * Normally code would use the qio_channel_socket_connect_async
1143      * method which uses a QIOTask + qio_task_set_error internally
1144      * to avoid blocking. The tcp_chr_wait_connected method, however,
1145      * needs a way to synchronize with completion of the background
1146      * connect task which can't be done with the QIOChannelSocket
1147      * async APIs. Thus we must use QIOTask directly to implement
1148      * the non-blocking concept locally.
1149      */
1150     s->connect_task = qio_task_new(OBJECT(sioc),
1151                                    qemu_chr_socket_connected,
1152                                    object_ref(OBJECT(chr)),
1153                                    (GDestroyNotify)object_unref);
1154     qio_task_run_in_thread(s->connect_task,
1155                            tcp_chr_connect_client_task,
1156                            s->addr,
1157                            NULL,
1158                            chr->gcontext);
1159 }
1160 
1161 static gboolean socket_reconnect_timeout(gpointer opaque)
1162 {
1163     Chardev *chr = CHARDEV(opaque);
1164     SocketChardev *s = SOCKET_CHARDEV(opaque);
1165 
1166     qemu_mutex_lock(&chr->chr_write_lock);
1167     g_source_unref(s->reconnect_timer);
1168     s->reconnect_timer = NULL;
1169     qemu_mutex_unlock(&chr->chr_write_lock);
1170 
1171     if (chr->be_open) {
1172         return false;
1173     }
1174 
1175     tcp_chr_connect_client_async(chr);
1176 
1177     return false;
1178 }
1179 
1180 
1181 static int qmp_chardev_open_socket_server(Chardev *chr,
1182                                           bool is_telnet,
1183                                           bool is_waitconnect,
1184                                           Error **errp)
1185 {
1186     SocketChardev *s = SOCKET_CHARDEV(chr);
1187     char *name;
1188     if (is_telnet) {
1189         s->do_telnetopt = 1;
1190     }
1191     s->listener = qio_net_listener_new();
1192 
1193     name = g_strdup_printf("chardev-tcp-listener-%s", chr->label);
1194     qio_net_listener_set_name(s->listener, name);
1195     g_free(name);
1196 
1197     if (s->addr->type == SOCKET_ADDRESS_TYPE_FD && !*s->addr->u.fd.str) {
1198         goto skip_listen;
1199     }
1200 
1201     if (qio_net_listener_open_sync(s->listener, s->addr, 1, errp) < 0) {
1202         object_unref(OBJECT(s->listener));
1203         s->listener = NULL;
1204         return -1;
1205     }
1206 
1207     qapi_free_SocketAddress(s->addr);
1208     s->addr = socket_local_address(s->listener->sioc[0]->fd, errp);
1209 
1210 skip_listen:
1211     update_disconnected_filename(s);
1212 
1213     if (is_waitconnect) {
1214         tcp_chr_accept_server_sync(chr);
1215     } else {
1216         qio_net_listener_set_client_func_full(s->listener,
1217                                               tcp_chr_accept,
1218                                               chr, NULL,
1219                                               chr->gcontext);
1220     }
1221 
1222     return 0;
1223 }
1224 
1225 
1226 static int qmp_chardev_open_socket_client(Chardev *chr,
1227                                           int64_t reconnect,
1228                                           Error **errp)
1229 {
1230     SocketChardev *s = SOCKET_CHARDEV(chr);
1231 
1232     if (reconnect > 0) {
1233         s->reconnect_time = reconnect;
1234         tcp_chr_connect_client_async(chr);
1235         return 0;
1236     } else {
1237         return tcp_chr_connect_client_sync(chr, errp);
1238     }
1239 }
1240 
1241 
1242 static bool qmp_chardev_validate_socket(ChardevSocket *sock,
1243                                         SocketAddress *addr,
1244                                         Error **errp)
1245 {
1246     /* Validate any options which have a dependency on address type */
1247     switch (addr->type) {
1248     case SOCKET_ADDRESS_TYPE_FD:
1249         if (sock->has_reconnect) {
1250             error_setg(errp,
1251                        "'reconnect' option is incompatible with "
1252                        "'fd' address type");
1253             return false;
1254         }
1255         if (sock->tls_creds &&
1256             !(sock->has_server && sock->server)) {
1257             error_setg(errp,
1258                        "'tls_creds' option is incompatible with "
1259                        "'fd' address type as client");
1260             return false;
1261         }
1262         break;
1263 
1264     case SOCKET_ADDRESS_TYPE_UNIX:
1265         if (sock->tls_creds) {
1266             error_setg(errp,
1267                        "'tls_creds' option is incompatible with "
1268                        "'unix' address type");
1269             return false;
1270         }
1271         break;
1272 
1273     case SOCKET_ADDRESS_TYPE_INET:
1274         break;
1275 
1276     case SOCKET_ADDRESS_TYPE_VSOCK:
1277         if (sock->tls_creds) {
1278             error_setg(errp,
1279                        "'tls_creds' option is incompatible with "
1280                        "'vsock' address type");
1281             return false;
1282         }
1283 
1284     default:
1285         break;
1286     }
1287 
1288     if (sock->tls_authz && !sock->tls_creds) {
1289         error_setg(errp, "'tls_authz' option requires 'tls_creds' option");
1290         return false;
1291     }
1292 
1293     /* Validate any options which have a dependancy on client vs server */
1294     if (!sock->has_server || sock->server) {
1295         if (sock->has_reconnect) {
1296             error_setg(errp,
1297                        "'reconnect' option is incompatible with "
1298                        "socket in server listen mode");
1299             return false;
1300         }
1301     } else {
1302         if (sock->has_websocket && sock->websocket) {
1303             error_setg(errp, "%s", "Websocket client is not implemented");
1304             return false;
1305         }
1306         if (sock->has_wait) {
1307             error_setg(errp, "%s",
1308                        "'wait' option is incompatible with "
1309                        "socket in client connect mode");
1310             return false;
1311         }
1312     }
1313 
1314     return true;
1315 }
1316 
1317 
1318 static void qmp_chardev_open_socket(Chardev *chr,
1319                                     ChardevBackend *backend,
1320                                     bool *be_opened,
1321                                     Error **errp)
1322 {
1323     SocketChardev *s = SOCKET_CHARDEV(chr);
1324     ChardevSocket *sock = backend->u.socket.data;
1325     bool do_nodelay     = sock->has_nodelay ? sock->nodelay : false;
1326     bool is_listen      = sock->has_server  ? sock->server  : true;
1327     bool is_telnet      = sock->has_telnet  ? sock->telnet  : false;
1328     bool is_tn3270      = sock->has_tn3270  ? sock->tn3270  : false;
1329     bool is_waitconnect = sock->has_wait    ? sock->wait    : false;
1330     bool is_websock     = sock->has_websocket ? sock->websocket : false;
1331     int64_t reconnect   = sock->has_reconnect ? sock->reconnect : 0;
1332     SocketAddress *addr;
1333 
1334     s->is_listen = is_listen;
1335     s->is_telnet = is_telnet;
1336     s->is_tn3270 = is_tn3270;
1337     s->is_websock = is_websock;
1338     s->do_nodelay = do_nodelay;
1339     if (sock->tls_creds) {
1340         Object *creds;
1341         creds = object_resolve_path_component(
1342             object_get_objects_root(), sock->tls_creds);
1343         if (!creds) {
1344             error_setg(errp, "No TLS credentials with id '%s'",
1345                        sock->tls_creds);
1346             return;
1347         }
1348         s->tls_creds = (QCryptoTLSCreds *)
1349             object_dynamic_cast(creds,
1350                                 TYPE_QCRYPTO_TLS_CREDS);
1351         if (!s->tls_creds) {
1352             error_setg(errp, "Object with id '%s' is not TLS credentials",
1353                        sock->tls_creds);
1354             return;
1355         }
1356         object_ref(OBJECT(s->tls_creds));
1357         if (!qcrypto_tls_creds_check_endpoint(s->tls_creds,
1358                                           is_listen
1359                                           ? QCRYPTO_TLS_CREDS_ENDPOINT_SERVER
1360                                           : QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT,
1361                                           errp)) {
1362             return;
1363         }
1364     }
1365     s->tls_authz = g_strdup(sock->tls_authz);
1366 
1367     s->addr = addr = socket_address_flatten(sock->addr);
1368 
1369     if (!qmp_chardev_validate_socket(sock, addr, errp)) {
1370         return;
1371     }
1372 
1373     qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE);
1374 #ifndef _WIN32
1375     /* TODO SOCKET_ADDRESS_FD where fd has AF_UNIX */
1376     if (addr->type == SOCKET_ADDRESS_TYPE_UNIX) {
1377         qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_FD_PASS);
1378     }
1379 #endif
1380 
1381     /*
1382      * In the chardev-change special-case, we shouldn't register a new yank
1383      * instance, as there already may be one.
1384      */
1385     if (!chr->handover_yank_instance) {
1386         if (!yank_register_instance(CHARDEV_YANK_INSTANCE(chr->label), errp)) {
1387             return;
1388         }
1389     }
1390     s->registered_yank = true;
1391 
1392     /* be isn't opened until we get a connection */
1393     *be_opened = false;
1394 
1395     update_disconnected_filename(s);
1396 
1397     if (s->is_listen) {
1398         if (qmp_chardev_open_socket_server(chr, is_telnet || is_tn3270,
1399                                            is_waitconnect, errp) < 0) {
1400             return;
1401         }
1402     } else {
1403         if (qmp_chardev_open_socket_client(chr, reconnect, errp) < 0) {
1404             return;
1405         }
1406     }
1407 }
1408 
1409 static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
1410                                   Error **errp)
1411 {
1412     const char *path = qemu_opt_get(opts, "path");
1413     const char *host = qemu_opt_get(opts, "host");
1414     const char *port = qemu_opt_get(opts, "port");
1415     const char *fd = qemu_opt_get(opts, "fd");
1416 #ifdef CONFIG_LINUX
1417     bool tight = qemu_opt_get_bool(opts, "tight", true);
1418     bool abstract = qemu_opt_get_bool(opts, "abstract", false);
1419 #endif
1420     SocketAddressLegacy *addr;
1421     ChardevSocket *sock;
1422 
1423     if ((!!path + !!fd + !!host) > 1) {
1424         error_setg(errp,
1425                    "None or one of 'path', 'fd' or 'host' option required.");
1426         return;
1427     }
1428 
1429     if (host && !port) {
1430         error_setg(errp, "chardev: socket: no port given");
1431         return;
1432     }
1433 
1434     backend->type = CHARDEV_BACKEND_KIND_SOCKET;
1435     sock = backend->u.socket.data = g_new0(ChardevSocket, 1);
1436     qemu_chr_parse_common(opts, qapi_ChardevSocket_base(sock));
1437 
1438     if (qemu_opt_get(opts, "delay") && qemu_opt_get(opts, "nodelay")) {
1439         error_setg(errp, "'delay' and 'nodelay' are mutually exclusive");
1440         return;
1441     }
1442     sock->has_nodelay =
1443         qemu_opt_get(opts, "delay") ||
1444         qemu_opt_get(opts, "nodelay");
1445     sock->nodelay =
1446         !qemu_opt_get_bool(opts, "delay", true) ||
1447         qemu_opt_get_bool(opts, "nodelay", false);
1448 
1449     /*
1450      * We have different default to QMP for 'server', hence
1451      * we can't just check for existence of 'server'
1452      */
1453     sock->has_server = true;
1454     sock->server = qemu_opt_get_bool(opts, "server", false);
1455     sock->has_telnet = qemu_opt_get(opts, "telnet");
1456     sock->telnet = qemu_opt_get_bool(opts, "telnet", false);
1457     sock->has_tn3270 = qemu_opt_get(opts, "tn3270");
1458     sock->tn3270 = qemu_opt_get_bool(opts, "tn3270", false);
1459     sock->has_websocket = qemu_opt_get(opts, "websocket");
1460     sock->websocket = qemu_opt_get_bool(opts, "websocket", false);
1461     /*
1462      * We have different default to QMP for 'wait' when 'server'
1463      * is set, hence we can't just check for existence of 'wait'
1464      */
1465     sock->has_wait = qemu_opt_find(opts, "wait") || sock->server;
1466     sock->wait = qemu_opt_get_bool(opts, "wait", true);
1467     sock->has_reconnect = qemu_opt_find(opts, "reconnect");
1468     sock->reconnect = qemu_opt_get_number(opts, "reconnect", 0);
1469     sock->tls_creds = g_strdup(qemu_opt_get(opts, "tls-creds"));
1470     sock->tls_authz = g_strdup(qemu_opt_get(opts, "tls-authz"));
1471 
1472     addr = g_new0(SocketAddressLegacy, 1);
1473     if (path) {
1474         UnixSocketAddress *q_unix;
1475         addr->type = SOCKET_ADDRESS_TYPE_UNIX;
1476         q_unix = addr->u.q_unix.data = g_new0(UnixSocketAddress, 1);
1477         q_unix->path = g_strdup(path);
1478 #ifdef CONFIG_LINUX
1479         q_unix->has_tight = true;
1480         q_unix->tight = tight;
1481         q_unix->has_abstract = true;
1482         q_unix->abstract = abstract;
1483 #endif
1484     } else if (host) {
1485         addr->type = SOCKET_ADDRESS_TYPE_INET;
1486         addr->u.inet.data = g_new(InetSocketAddress, 1);
1487         *addr->u.inet.data = (InetSocketAddress) {
1488             .host = g_strdup(host),
1489             .port = g_strdup(port),
1490             .has_to = qemu_opt_get(opts, "to"),
1491             .to = qemu_opt_get_number(opts, "to", 0),
1492             .has_ipv4 = qemu_opt_get(opts, "ipv4"),
1493             .ipv4 = qemu_opt_get_bool(opts, "ipv4", 0),
1494             .has_ipv6 = qemu_opt_get(opts, "ipv6"),
1495             .ipv6 = qemu_opt_get_bool(opts, "ipv6", 0),
1496         };
1497     } else {
1498         addr->type = SOCKET_ADDRESS_TYPE_FD;
1499         addr->u.fd.data = g_new(String, 1);
1500         addr->u.fd.data->str = g_strdup(fd);
1501     }
1502     sock->addr = addr;
1503 }
1504 
1505 static void
1506 char_socket_get_addr(Object *obj, Visitor *v, const char *name,
1507                      void *opaque, Error **errp)
1508 {
1509     SocketChardev *s = SOCKET_CHARDEV(obj);
1510 
1511     visit_type_SocketAddress(v, name, &s->addr, errp);
1512 }
1513 
1514 static bool
1515 char_socket_get_connected(Object *obj, Error **errp)
1516 {
1517     SocketChardev *s = SOCKET_CHARDEV(obj);
1518 
1519     return s->state == TCP_CHARDEV_STATE_CONNECTED;
1520 }
1521 
1522 static void char_socket_class_init(ObjectClass *oc, void *data)
1523 {
1524     ChardevClass *cc = CHARDEV_CLASS(oc);
1525 
1526     cc->supports_yank = true;
1527 
1528     cc->parse = qemu_chr_parse_socket;
1529     cc->open = qmp_chardev_open_socket;
1530     cc->chr_wait_connected = tcp_chr_wait_connected;
1531     cc->chr_write = tcp_chr_write;
1532     cc->chr_sync_read = tcp_chr_sync_read;
1533     cc->chr_disconnect = tcp_chr_disconnect;
1534     cc->get_msgfds = tcp_get_msgfds;
1535     cc->set_msgfds = tcp_set_msgfds;
1536     cc->chr_add_client = tcp_chr_add_client;
1537     cc->chr_add_watch = tcp_chr_add_watch;
1538     cc->chr_update_read_handler = tcp_chr_update_read_handler;
1539 
1540     object_class_property_add(oc, "addr", "SocketAddress",
1541                               char_socket_get_addr, NULL,
1542                               NULL, NULL);
1543 
1544     object_class_property_add_bool(oc, "connected", char_socket_get_connected,
1545                                    NULL);
1546 }
1547 
1548 static const TypeInfo char_socket_type_info = {
1549     .name = TYPE_CHARDEV_SOCKET,
1550     .parent = TYPE_CHARDEV,
1551     .instance_size = sizeof(SocketChardev),
1552     .instance_finalize = char_socket_finalize,
1553     .class_init = char_socket_class_init,
1554 };
1555 
1556 static void register_types(void)
1557 {
1558     type_register_static(&char_socket_type_info);
1559 }
1560 
1561 type_init(register_types);
1562