13e230dd2SCorentin Chary /* 23e230dd2SCorentin Chary * QEMU VNC display driver 33e230dd2SCorentin Chary * 43e230dd2SCorentin Chary * Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws> 53e230dd2SCorentin Chary * Copyright (C) 2006 Fabrice Bellard 63e230dd2SCorentin Chary * Copyright (C) 2009 Red Hat, Inc 73e230dd2SCorentin Chary * 83e230dd2SCorentin Chary * Permission is hereby granted, free of charge, to any person obtaining a copy 93e230dd2SCorentin Chary * of this software and associated documentation files (the "Software"), to deal 103e230dd2SCorentin Chary * in the Software without restriction, including without limitation the rights 113e230dd2SCorentin Chary * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 123e230dd2SCorentin Chary * copies of the Software, and to permit persons to whom the Software is 133e230dd2SCorentin Chary * furnished to do so, subject to the following conditions: 143e230dd2SCorentin Chary * 153e230dd2SCorentin Chary * The above copyright notice and this permission notice shall be included in 163e230dd2SCorentin Chary * all copies or substantial portions of the Software. 173e230dd2SCorentin Chary * 183e230dd2SCorentin Chary * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 193e230dd2SCorentin Chary * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 203e230dd2SCorentin Chary * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 213e230dd2SCorentin Chary * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 223e230dd2SCorentin Chary * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 233e230dd2SCorentin Chary * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 243e230dd2SCorentin Chary * THE SOFTWARE. 253e230dd2SCorentin Chary */ 263e230dd2SCorentin Chary 27e16f4c87SPeter Maydell #include "qemu/osdep.h" 283e230dd2SCorentin Chary #include "vnc.h" 29bd023f95SCorentin Chary #include "vnc-jobs.h" 3040066175SGerd Hoffmann #include "trace.h" 3113d4ff07SMarkus Armbruster #include "hw/qdev-core.h" 329c17d615SPaolo Bonzini #include "sysemu/sysemu.h" 33d49b6836SMarkus Armbruster #include "qemu/error-report.h" 34db725815SMarkus Armbruster #include "qemu/main-loop.h" 350b8fa32fSMarkus Armbruster #include "qemu/module.h" 36922a01a0SMarkus Armbruster #include "qemu/option.h" 371de7afc9SPaolo Bonzini #include "qemu/sockets.h" 381de7afc9SPaolo Bonzini #include "qemu/timer.h" 39b76806d4SDaniel P. Berrange #include "authz/list.h" 404db14629SGerd Hoffmann #include "qemu/config-file.h" 415d75648bSMarkus Armbruster #include "qapi/qapi-emit-events.h" 425d75648bSMarkus Armbruster #include "qapi/qapi-events-ui.h" 43e688df6bSMarkus Armbruster #include "qapi/error.h" 449af23989SMarkus Armbruster #include "qapi/qapi-commands-ui.h" 458d447d10SGerd Hoffmann #include "ui/input.h" 468e9b0d24SDaniel P. Berrange #include "crypto/hash.h" 473e305e4aSDaniel P. Berrange #include "crypto/tlscredsanon.h" 483e305e4aSDaniel P. Berrange #include "crypto/tlscredsx509.h" 49f7b2502cSRichard Henderson #include "crypto/random.h" 503e305e4aSDaniel P. Berrange #include "qom/object_interfaces.h" 51f348b6d1SVeronia Bahaa #include "qemu/cutils.h" 5257a6d6d5SDaniel P. Berrange #include "io/dns-resolver.h" 533e230dd2SCorentin Chary 540f7b2864SGerd Hoffmann #define VNC_REFRESH_INTERVAL_BASE GUI_REFRESH_INTERVAL_DEFAULT 553e230dd2SCorentin Chary #define VNC_REFRESH_INTERVAL_INC 50 560f7b2864SGerd Hoffmann #define VNC_REFRESH_INTERVAL_MAX GUI_REFRESH_INTERVAL_IDLE 57999342a0SCorentin Chary static const struct timeval VNC_REFRESH_STATS = { 0, 500000 }; 58999342a0SCorentin Chary static const struct timeval VNC_REFRESH_LOSSY = { 2, 0 }; 593e230dd2SCorentin Chary 603e230dd2SCorentin Chary #include "vnc_keysym.h" 61800567a6SDaniel P. Berrange #include "crypto/cipher.h" 623e230dd2SCorentin Chary 63d616ccc5SGerd Hoffmann static QTAILQ_HEAD(, VncDisplay) vnc_displays = 64d616ccc5SGerd Hoffmann QTAILQ_HEAD_INITIALIZER(vnc_displays); 653e230dd2SCorentin Chary 663e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs); 67e2b72cb6SDaniel P. Berrange static void vnc_update_throttle_offset(VncState *vs); 683e230dd2SCorentin Chary 698cf36489SGerd Hoffmann static void vnc_set_share_mode(VncState *vs, VncShareMode mode) 708cf36489SGerd Hoffmann { 718cf36489SGerd Hoffmann #ifdef _VNC_DEBUG 728cf36489SGerd Hoffmann static const char *mn[] = { 738cf36489SGerd Hoffmann [0] = "undefined", 748cf36489SGerd Hoffmann [VNC_SHARE_MODE_CONNECTING] = "connecting", 758cf36489SGerd Hoffmann [VNC_SHARE_MODE_SHARED] = "shared", 768cf36489SGerd Hoffmann [VNC_SHARE_MODE_EXCLUSIVE] = "exclusive", 778cf36489SGerd Hoffmann [VNC_SHARE_MODE_DISCONNECTED] = "disconnected", 788cf36489SGerd Hoffmann }; 7904d2529dSDaniel P. Berrange fprintf(stderr, "%s/%p: %s -> %s\n", __func__, 8004d2529dSDaniel P. Berrange vs->ioc, mn[vs->share_mode], mn[mode]); 818cf36489SGerd Hoffmann #endif 828cf36489SGerd Hoffmann 83e5f34cddSGerd Hoffmann switch (vs->share_mode) { 84e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_CONNECTING: 85e5f34cddSGerd Hoffmann vs->vd->num_connecting--; 86e5f34cddSGerd Hoffmann break; 87e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_SHARED: 88e5f34cddSGerd Hoffmann vs->vd->num_shared--; 89e5f34cddSGerd Hoffmann break; 90e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_EXCLUSIVE: 918cf36489SGerd Hoffmann vs->vd->num_exclusive--; 92e5f34cddSGerd Hoffmann break; 93e5f34cddSGerd Hoffmann default: 94e5f34cddSGerd Hoffmann break; 958cf36489SGerd Hoffmann } 96e5f34cddSGerd Hoffmann 978cf36489SGerd Hoffmann vs->share_mode = mode; 98e5f34cddSGerd Hoffmann 99e5f34cddSGerd Hoffmann switch (vs->share_mode) { 100e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_CONNECTING: 101e5f34cddSGerd Hoffmann vs->vd->num_connecting++; 102e5f34cddSGerd Hoffmann break; 103e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_SHARED: 104e5f34cddSGerd Hoffmann vs->vd->num_shared++; 105e5f34cddSGerd Hoffmann break; 106e5f34cddSGerd Hoffmann case VNC_SHARE_MODE_EXCLUSIVE: 1078cf36489SGerd Hoffmann vs->vd->num_exclusive++; 108e5f34cddSGerd Hoffmann break; 109e5f34cddSGerd Hoffmann default: 110e5f34cddSGerd Hoffmann break; 1118cf36489SGerd Hoffmann } 1128cf36489SGerd Hoffmann } 1138cf36489SGerd Hoffmann 1143e230dd2SCorentin Chary 115bd269ebcSMarkus Armbruster static void vnc_init_basic_info(SocketAddress *addr, 11698481bfcSEric Blake VncBasicInfo *info, 11798481bfcSEric Blake Error **errp) 1183e230dd2SCorentin Chary { 11904d2529dSDaniel P. Berrange switch (addr->type) { 120bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_INET: 121bd269ebcSMarkus Armbruster info->host = g_strdup(addr->u.inet.host); 122bd269ebcSMarkus Armbruster info->service = g_strdup(addr->u.inet.port); 123bd269ebcSMarkus Armbruster if (addr->u.inet.ipv6) { 12404d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_IPV6; 12504d2529dSDaniel P. Berrange } else { 12604d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_IPV4; 12704d2529dSDaniel P. Berrange } 12804d2529dSDaniel P. Berrange break; 1293e230dd2SCorentin Chary 130bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_UNIX: 13104d2529dSDaniel P. Berrange info->host = g_strdup(""); 132bd269ebcSMarkus Armbruster info->service = g_strdup(addr->u.q_unix.path); 13304d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_UNIX; 13404d2529dSDaniel P. Berrange break; 13504d2529dSDaniel P. Berrange 136bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_VSOCK: 137bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_FD: 138a6c76285SMarkus Armbruster error_setg(errp, "Unsupported socket address type %s", 139977c736fSMarkus Armbruster SocketAddressType_str(addr->type)); 14004d2529dSDaniel P. Berrange break; 141a6c76285SMarkus Armbruster default: 142a6c76285SMarkus Armbruster abort(); 14304d2529dSDaniel P. Berrange } 14404d2529dSDaniel P. Berrange 14598481bfcSEric Blake return; 1463e230dd2SCorentin Chary } 1473e230dd2SCorentin Chary 14804d2529dSDaniel P. Berrange static void vnc_init_basic_info_from_server_addr(QIOChannelSocket *ioc, 14904d2529dSDaniel P. Berrange VncBasicInfo *info, 15098481bfcSEric Blake Error **errp) 1513e230dd2SCorentin Chary { 152bd269ebcSMarkus Armbruster SocketAddress *addr = NULL; 1533e230dd2SCorentin Chary 154624cdd46SDaniel P. Berrange if (!ioc) { 155624cdd46SDaniel P. Berrange error_setg(errp, "No listener socket available"); 156624cdd46SDaniel P. Berrange return; 157624cdd46SDaniel P. Berrange } 158624cdd46SDaniel P. Berrange 15904d2529dSDaniel P. Berrange addr = qio_channel_socket_get_local_address(ioc, errp); 16004d2529dSDaniel P. Berrange if (!addr) { 16198481bfcSEric Blake return; 1623e230dd2SCorentin Chary } 1633e230dd2SCorentin Chary 16404d2529dSDaniel P. Berrange vnc_init_basic_info(addr, info, errp); 165bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 1663e230dd2SCorentin Chary } 1673e230dd2SCorentin Chary 16804d2529dSDaniel P. Berrange static void vnc_init_basic_info_from_remote_addr(QIOChannelSocket *ioc, 16904d2529dSDaniel P. Berrange VncBasicInfo *info, 17098481bfcSEric Blake Error **errp) 1713e230dd2SCorentin Chary { 172bd269ebcSMarkus Armbruster SocketAddress *addr = NULL; 1733e230dd2SCorentin Chary 17404d2529dSDaniel P. Berrange addr = qio_channel_socket_get_remote_address(ioc, errp); 17504d2529dSDaniel P. Berrange if (!addr) { 17698481bfcSEric Blake return; 1773e230dd2SCorentin Chary } 1783e230dd2SCorentin Chary 17904d2529dSDaniel P. Berrange vnc_init_basic_info(addr, info, errp); 180bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 1813e230dd2SCorentin Chary } 1823e230dd2SCorentin Chary 1833e230dd2SCorentin Chary static const char *vnc_auth_name(VncDisplay *vd) { 1843e230dd2SCorentin Chary switch (vd->auth) { 1853e230dd2SCorentin Chary case VNC_AUTH_INVALID: 1863e230dd2SCorentin Chary return "invalid"; 1873e230dd2SCorentin Chary case VNC_AUTH_NONE: 1883e230dd2SCorentin Chary return "none"; 1893e230dd2SCorentin Chary case VNC_AUTH_VNC: 1903e230dd2SCorentin Chary return "vnc"; 1913e230dd2SCorentin Chary case VNC_AUTH_RA2: 1923e230dd2SCorentin Chary return "ra2"; 1933e230dd2SCorentin Chary case VNC_AUTH_RA2NE: 1943e230dd2SCorentin Chary return "ra2ne"; 1953e230dd2SCorentin Chary case VNC_AUTH_TIGHT: 1963e230dd2SCorentin Chary return "tight"; 1973e230dd2SCorentin Chary case VNC_AUTH_ULTRA: 1983e230dd2SCorentin Chary return "ultra"; 1993e230dd2SCorentin Chary case VNC_AUTH_TLS: 2003e230dd2SCorentin Chary return "tls"; 2013e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT: 2023e230dd2SCorentin Chary switch (vd->subauth) { 2033e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_PLAIN: 2043e230dd2SCorentin Chary return "vencrypt+plain"; 2053e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSNONE: 2063e230dd2SCorentin Chary return "vencrypt+tls+none"; 2073e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSVNC: 2083e230dd2SCorentin Chary return "vencrypt+tls+vnc"; 2093e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSPLAIN: 2103e230dd2SCorentin Chary return "vencrypt+tls+plain"; 2113e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509NONE: 2123e230dd2SCorentin Chary return "vencrypt+x509+none"; 2133e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509VNC: 2143e230dd2SCorentin Chary return "vencrypt+x509+vnc"; 2153e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509PLAIN: 2163e230dd2SCorentin Chary return "vencrypt+x509+plain"; 2173e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_TLSSASL: 2183e230dd2SCorentin Chary return "vencrypt+tls+sasl"; 2193e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT_X509SASL: 2203e230dd2SCorentin Chary return "vencrypt+x509+sasl"; 2213e230dd2SCorentin Chary default: 2223e230dd2SCorentin Chary return "vencrypt"; 2233e230dd2SCorentin Chary } 2243e230dd2SCorentin Chary case VNC_AUTH_SASL: 2253e230dd2SCorentin Chary return "sasl"; 2263e230dd2SCorentin Chary } 2273e230dd2SCorentin Chary return "unknown"; 2283e230dd2SCorentin Chary } 2293e230dd2SCorentin Chary 230d616ccc5SGerd Hoffmann static VncServerInfo *vnc_server_info_get(VncDisplay *vd) 2313e230dd2SCorentin Chary { 232fb6ba0d5SWenchao Xia VncServerInfo *info; 23398481bfcSEric Blake Error *err = NULL; 2343e230dd2SCorentin Chary 23513e1d0e7SDaniel P. Berrange if (!vd->listener || !vd->listener->nsioc) { 2364ee74fa7SDaniel P. Berrange return NULL; 2374ee74fa7SDaniel P. Berrange } 2384ee74fa7SDaniel P. Berrange 2393e7f136dSDaniel P. Berrange info = g_malloc0(sizeof(*info)); 24013e1d0e7SDaniel P. Berrange vnc_init_basic_info_from_server_addr(vd->listener->sioc[0], 241ddf21908SEric Blake qapi_VncServerInfo_base(info), &err); 242fb6ba0d5SWenchao Xia info->has_auth = true; 243d616ccc5SGerd Hoffmann info->auth = g_strdup(vnc_auth_name(vd)); 24498481bfcSEric Blake if (err) { 24598481bfcSEric Blake qapi_free_VncServerInfo(info); 24698481bfcSEric Blake info = NULL; 24798481bfcSEric Blake error_free(err); 24898481bfcSEric Blake } 249fb6ba0d5SWenchao Xia return info; 2503e230dd2SCorentin Chary } 2513e230dd2SCorentin Chary 2523e230dd2SCorentin Chary static void vnc_client_cache_auth(VncState *client) 2533e230dd2SCorentin Chary { 2543e230dd2SCorentin Chary if (!client->info) { 2553e230dd2SCorentin Chary return; 2563e230dd2SCorentin Chary } 2573e230dd2SCorentin Chary 2583e305e4aSDaniel P. Berrange if (client->tls) { 2593e305e4aSDaniel P. Berrange client->info->x509_dname = 2603e305e4aSDaniel P. Berrange qcrypto_tls_session_get_peer_name(client->tls); 2613e305e4aSDaniel P. Berrange client->info->has_x509_dname = 2623e305e4aSDaniel P. Berrange client->info->x509_dname != NULL; 2633e230dd2SCorentin Chary } 2643e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 2653e230dd2SCorentin Chary if (client->sasl.conn && 2663e230dd2SCorentin Chary client->sasl.username) { 267fb6ba0d5SWenchao Xia client->info->has_sasl_username = true; 268fb6ba0d5SWenchao Xia client->info->sasl_username = g_strdup(client->sasl.username); 2693e230dd2SCorentin Chary } 2703e230dd2SCorentin Chary #endif 2713e230dd2SCorentin Chary } 2723e230dd2SCorentin Chary 2733e230dd2SCorentin Chary static void vnc_client_cache_addr(VncState *client) 2743e230dd2SCorentin Chary { 27598481bfcSEric Blake Error *err = NULL; 2763e230dd2SCorentin Chary 277fb6ba0d5SWenchao Xia client->info = g_malloc0(sizeof(*client->info)); 27804d2529dSDaniel P. Berrange vnc_init_basic_info_from_remote_addr(client->sioc, 279ddf21908SEric Blake qapi_VncClientInfo_base(client->info), 28098481bfcSEric Blake &err); 281e1b3d477SGerd Hoffmann client->info->websocket = client->websocket; 28298481bfcSEric Blake if (err) { 28398481bfcSEric Blake qapi_free_VncClientInfo(client->info); 28498481bfcSEric Blake client->info = NULL; 28598481bfcSEric Blake error_free(err); 286fb6ba0d5SWenchao Xia } 2873e230dd2SCorentin Chary } 2883e230dd2SCorentin Chary 289fb6ba0d5SWenchao Xia static void vnc_qmp_event(VncState *vs, QAPIEvent event) 2903e230dd2SCorentin Chary { 291fb6ba0d5SWenchao Xia VncServerInfo *si; 2923e230dd2SCorentin Chary 2933e230dd2SCorentin Chary if (!vs->info) { 2943e230dd2SCorentin Chary return; 2953e230dd2SCorentin Chary } 2963e230dd2SCorentin Chary 297d616ccc5SGerd Hoffmann si = vnc_server_info_get(vs->vd); 298fb6ba0d5SWenchao Xia if (!si) { 2993e230dd2SCorentin Chary return; 3003e230dd2SCorentin Chary } 3013e230dd2SCorentin Chary 302fb6ba0d5SWenchao Xia switch (event) { 303fb6ba0d5SWenchao Xia case QAPI_EVENT_VNC_CONNECTED: 3043ab72385SPeter Xu qapi_event_send_vnc_connected(si, qapi_VncClientInfo_base(vs->info)); 305fb6ba0d5SWenchao Xia break; 306fb6ba0d5SWenchao Xia case QAPI_EVENT_VNC_INITIALIZED: 3073ab72385SPeter Xu qapi_event_send_vnc_initialized(si, vs->info); 308fb6ba0d5SWenchao Xia break; 309fb6ba0d5SWenchao Xia case QAPI_EVENT_VNC_DISCONNECTED: 3103ab72385SPeter Xu qapi_event_send_vnc_disconnected(si, vs->info); 311fb6ba0d5SWenchao Xia break; 312fb6ba0d5SWenchao Xia default: 313fb6ba0d5SWenchao Xia break; 314fb6ba0d5SWenchao Xia } 3153e230dd2SCorentin Chary 316fb6ba0d5SWenchao Xia qapi_free_VncServerInfo(si); 3173e230dd2SCorentin Chary } 3183e230dd2SCorentin Chary 3192b54aa87SLuiz Capitulino static VncClientInfo *qmp_query_vnc_client(const VncState *client) 3203e230dd2SCorentin Chary { 3212b54aa87SLuiz Capitulino VncClientInfo *info; 32204d2529dSDaniel P. Berrange Error *err = NULL; 3232b54aa87SLuiz Capitulino 3242b54aa87SLuiz Capitulino info = g_malloc0(sizeof(*info)); 32504d2529dSDaniel P. Berrange 32604d2529dSDaniel P. Berrange vnc_init_basic_info_from_remote_addr(client->sioc, 32704d2529dSDaniel P. Berrange qapi_VncClientInfo_base(info), 32804d2529dSDaniel P. Berrange &err); 32904d2529dSDaniel P. Berrange if (err) { 33004d2529dSDaniel P. Berrange error_free(err); 33104d2529dSDaniel P. Berrange qapi_free_VncClientInfo(info); 33204d2529dSDaniel P. Berrange return NULL; 33304d2529dSDaniel P. Berrange } 33404d2529dSDaniel P. Berrange 335ddf21908SEric Blake info->websocket = client->websocket; 3363e230dd2SCorentin Chary 3373e305e4aSDaniel P. Berrange if (client->tls) { 3383e305e4aSDaniel P. Berrange info->x509_dname = qcrypto_tls_session_get_peer_name(client->tls); 3393e305e4aSDaniel P. Berrange info->has_x509_dname = info->x509_dname != NULL; 3402b54aa87SLuiz Capitulino } 3413e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 3422b54aa87SLuiz Capitulino if (client->sasl.conn && client->sasl.username) { 3432b54aa87SLuiz Capitulino info->has_sasl_username = true; 3442b54aa87SLuiz Capitulino info->sasl_username = g_strdup(client->sasl.username); 3452b54aa87SLuiz Capitulino } 3463e230dd2SCorentin Chary #endif 3472b54aa87SLuiz Capitulino 3482b54aa87SLuiz Capitulino return info; 3493e230dd2SCorentin Chary } 3503e230dd2SCorentin Chary 351d616ccc5SGerd Hoffmann static VncDisplay *vnc_display_find(const char *id) 352d616ccc5SGerd Hoffmann { 353d616ccc5SGerd Hoffmann VncDisplay *vd; 354d616ccc5SGerd Hoffmann 355d616ccc5SGerd Hoffmann if (id == NULL) { 356d616ccc5SGerd Hoffmann return QTAILQ_FIRST(&vnc_displays); 357d616ccc5SGerd Hoffmann } 358d616ccc5SGerd Hoffmann QTAILQ_FOREACH(vd, &vnc_displays, next) { 359d616ccc5SGerd Hoffmann if (strcmp(id, vd->id) == 0) { 360d616ccc5SGerd Hoffmann return vd; 361d616ccc5SGerd Hoffmann } 362d616ccc5SGerd Hoffmann } 363d616ccc5SGerd Hoffmann return NULL; 364d616ccc5SGerd Hoffmann } 365d616ccc5SGerd Hoffmann 3662d29a436SGerd Hoffmann static VncClientInfoList *qmp_query_client_list(VncDisplay *vd) 3672d29a436SGerd Hoffmann { 3682d29a436SGerd Hoffmann VncClientInfoList *cinfo, *prev = NULL; 3692d29a436SGerd Hoffmann VncState *client; 3702d29a436SGerd Hoffmann 3712d29a436SGerd Hoffmann QTAILQ_FOREACH(client, &vd->clients, next) { 3722d29a436SGerd Hoffmann cinfo = g_new0(VncClientInfoList, 1); 3732d29a436SGerd Hoffmann cinfo->value = qmp_query_vnc_client(client); 3742d29a436SGerd Hoffmann cinfo->next = prev; 3752d29a436SGerd Hoffmann prev = cinfo; 3762d29a436SGerd Hoffmann } 3772d29a436SGerd Hoffmann return prev; 3782d29a436SGerd Hoffmann } 3792d29a436SGerd Hoffmann 3802b54aa87SLuiz Capitulino VncInfo *qmp_query_vnc(Error **errp) 3813e230dd2SCorentin Chary { 3822b54aa87SLuiz Capitulino VncInfo *info = g_malloc0(sizeof(*info)); 383d616ccc5SGerd Hoffmann VncDisplay *vd = vnc_display_find(NULL); 384bd269ebcSMarkus Armbruster SocketAddress *addr = NULL; 3853e230dd2SCorentin Chary 38613e1d0e7SDaniel P. Berrange if (vd == NULL || !vd->listener || !vd->listener->nsioc) { 3872b54aa87SLuiz Capitulino info->enabled = false; 3883e230dd2SCorentin Chary } else { 3892b54aa87SLuiz Capitulino info->enabled = true; 3902b54aa87SLuiz Capitulino 3912b54aa87SLuiz Capitulino /* for compatibility with the original command */ 3922b54aa87SLuiz Capitulino info->has_clients = true; 3932d29a436SGerd Hoffmann info->clients = qmp_query_client_list(vd); 3943e230dd2SCorentin Chary 39513e1d0e7SDaniel P. Berrange addr = qio_channel_socket_get_local_address(vd->listener->sioc[0], 39613e1d0e7SDaniel P. Berrange errp); 39704d2529dSDaniel P. Berrange if (!addr) { 3982b54aa87SLuiz Capitulino goto out_error; 3992b54aa87SLuiz Capitulino } 4003e230dd2SCorentin Chary 40104d2529dSDaniel P. Berrange switch (addr->type) { 402bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_INET: 403bd269ebcSMarkus Armbruster info->host = g_strdup(addr->u.inet.host); 404bd269ebcSMarkus Armbruster info->service = g_strdup(addr->u.inet.port); 405bd269ebcSMarkus Armbruster if (addr->u.inet.ipv6) { 40604d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_IPV6; 40704d2529dSDaniel P. Berrange } else { 40804d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_IPV4; 40904d2529dSDaniel P. Berrange } 41004d2529dSDaniel P. Berrange break; 41104d2529dSDaniel P. Berrange 412bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_UNIX: 41304d2529dSDaniel P. Berrange info->host = g_strdup(""); 414bd269ebcSMarkus Armbruster info->service = g_strdup(addr->u.q_unix.path); 41504d2529dSDaniel P. Berrange info->family = NETWORK_ADDRESS_FAMILY_UNIX; 41604d2529dSDaniel P. Berrange break; 41704d2529dSDaniel P. Berrange 418bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_VSOCK: 419bd269ebcSMarkus Armbruster case SOCKET_ADDRESS_TYPE_FD: 420a6c76285SMarkus Armbruster error_setg(errp, "Unsupported socket address type %s", 421977c736fSMarkus Armbruster SocketAddressType_str(addr->type)); 4222b54aa87SLuiz Capitulino goto out_error; 423a6c76285SMarkus Armbruster default: 424a6c76285SMarkus Armbruster abort(); 4253e230dd2SCorentin Chary } 4262b54aa87SLuiz Capitulino 4272b54aa87SLuiz Capitulino info->has_host = true; 4282b54aa87SLuiz Capitulino info->has_service = true; 4292b54aa87SLuiz Capitulino info->has_family = true; 4302b54aa87SLuiz Capitulino 4312b54aa87SLuiz Capitulino info->has_auth = true; 432d616ccc5SGerd Hoffmann info->auth = g_strdup(vnc_auth_name(vd)); 4333e230dd2SCorentin Chary } 4342b54aa87SLuiz Capitulino 435bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 4362b54aa87SLuiz Capitulino return info; 4372b54aa87SLuiz Capitulino 4382b54aa87SLuiz Capitulino out_error: 439bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 4402b54aa87SLuiz Capitulino qapi_free_VncInfo(info); 4412b54aa87SLuiz Capitulino return NULL; 4423e230dd2SCorentin Chary } 4433e230dd2SCorentin Chary 4442a7e6857SDaniel P. Berrange 4452a7e6857SDaniel P. Berrange static void qmp_query_auth(int auth, int subauth, 4462a7e6857SDaniel P. Berrange VncPrimaryAuth *qmp_auth, 4472a7e6857SDaniel P. Berrange VncVencryptSubAuth *qmp_vencrypt, 4482a7e6857SDaniel P. Berrange bool *qmp_has_vencrypt); 4492a7e6857SDaniel P. Berrange 4502a7e6857SDaniel P. Berrange static VncServerInfo2List *qmp_query_server_entry(QIOChannelSocket *ioc, 4514478aa76SGerd Hoffmann bool websocket, 4522a7e6857SDaniel P. Berrange int auth, 4532a7e6857SDaniel P. Berrange int subauth, 4542a7e6857SDaniel P. Berrange VncServerInfo2List *prev) 455df887684SGerd Hoffmann { 4562a7e6857SDaniel P. Berrange VncServerInfo2List *list; 4572a7e6857SDaniel P. Berrange VncServerInfo2 *info; 45804d2529dSDaniel P. Berrange Error *err = NULL; 459bd269ebcSMarkus Armbruster SocketAddress *addr; 460df887684SGerd Hoffmann 46104d2529dSDaniel P. Berrange addr = qio_channel_socket_get_local_address(ioc, &err); 46204d2529dSDaniel P. Berrange if (!addr) { 46304d2529dSDaniel P. Berrange error_free(err); 464df887684SGerd Hoffmann return prev; 465df887684SGerd Hoffmann } 466df887684SGerd Hoffmann 4672a7e6857SDaniel P. Berrange info = g_new0(VncServerInfo2, 1); 4682a7e6857SDaniel P. Berrange vnc_init_basic_info(addr, qapi_VncServerInfo2_base(info), &err); 469bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 47004d2529dSDaniel P. Berrange if (err) { 4712a7e6857SDaniel P. Berrange qapi_free_VncServerInfo2(info); 47204d2529dSDaniel P. Berrange error_free(err); 47304d2529dSDaniel P. Berrange return prev; 47404d2529dSDaniel P. Berrange } 4754478aa76SGerd Hoffmann info->websocket = websocket; 476df887684SGerd Hoffmann 4772a7e6857SDaniel P. Berrange qmp_query_auth(auth, subauth, &info->auth, 4782a7e6857SDaniel P. Berrange &info->vencrypt, &info->has_vencrypt); 4792a7e6857SDaniel P. Berrange 4802a7e6857SDaniel P. Berrange list = g_new0(VncServerInfo2List, 1); 481df887684SGerd Hoffmann list->value = info; 482df887684SGerd Hoffmann list->next = prev; 483df887684SGerd Hoffmann return list; 484df887684SGerd Hoffmann } 485df887684SGerd Hoffmann 4862a7e6857SDaniel P. Berrange static void qmp_query_auth(int auth, int subauth, 4872a7e6857SDaniel P. Berrange VncPrimaryAuth *qmp_auth, 4882a7e6857SDaniel P. Berrange VncVencryptSubAuth *qmp_vencrypt, 4892a7e6857SDaniel P. Berrange bool *qmp_has_vencrypt) 490df887684SGerd Hoffmann { 4912a7e6857SDaniel P. Berrange switch (auth) { 492df887684SGerd Hoffmann case VNC_AUTH_VNC: 4932a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_VNC; 494df887684SGerd Hoffmann break; 495df887684SGerd Hoffmann case VNC_AUTH_RA2: 4962a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_RA2; 497df887684SGerd Hoffmann break; 498df887684SGerd Hoffmann case VNC_AUTH_RA2NE: 4992a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_RA2NE; 500df887684SGerd Hoffmann break; 501df887684SGerd Hoffmann case VNC_AUTH_TIGHT: 5022a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_TIGHT; 503df887684SGerd Hoffmann break; 504df887684SGerd Hoffmann case VNC_AUTH_ULTRA: 5052a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_ULTRA; 506df887684SGerd Hoffmann break; 507df887684SGerd Hoffmann case VNC_AUTH_TLS: 5082a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_TLS; 509df887684SGerd Hoffmann break; 510df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT: 5112a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_VENCRYPT; 5122a7e6857SDaniel P. Berrange *qmp_has_vencrypt = true; 5132a7e6857SDaniel P. Berrange switch (subauth) { 514df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_PLAIN: 5152a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_PLAIN; 516df887684SGerd Hoffmann break; 517df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_TLSNONE: 5182a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_NONE; 519df887684SGerd Hoffmann break; 520df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_TLSVNC: 5212a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_VNC; 522df887684SGerd Hoffmann break; 523df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_TLSPLAIN: 5242a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_PLAIN; 525df887684SGerd Hoffmann break; 526df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_X509NONE: 5272a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_NONE; 528df887684SGerd Hoffmann break; 529df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_X509VNC: 5302a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_VNC; 531df887684SGerd Hoffmann break; 532df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_X509PLAIN: 5332a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_PLAIN; 534df887684SGerd Hoffmann break; 535df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_TLSSASL: 5362a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_TLS_SASL; 537df887684SGerd Hoffmann break; 538df887684SGerd Hoffmann case VNC_AUTH_VENCRYPT_X509SASL: 5392a7e6857SDaniel P. Berrange *qmp_vencrypt = VNC_VENCRYPT_SUB_AUTH_X509_SASL; 540df887684SGerd Hoffmann break; 541df887684SGerd Hoffmann default: 5422a7e6857SDaniel P. Berrange *qmp_has_vencrypt = false; 543df887684SGerd Hoffmann break; 544df887684SGerd Hoffmann } 545df887684SGerd Hoffmann break; 546df887684SGerd Hoffmann case VNC_AUTH_SASL: 5472a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_SASL; 548df887684SGerd Hoffmann break; 549df887684SGerd Hoffmann case VNC_AUTH_NONE: 550df887684SGerd Hoffmann default: 5512a7e6857SDaniel P. Berrange *qmp_auth = VNC_PRIMARY_AUTH_NONE; 552df887684SGerd Hoffmann break; 553df887684SGerd Hoffmann } 554df887684SGerd Hoffmann } 555df887684SGerd Hoffmann 556df887684SGerd Hoffmann VncInfo2List *qmp_query_vnc_servers(Error **errp) 557df887684SGerd Hoffmann { 558df887684SGerd Hoffmann VncInfo2List *item, *prev = NULL; 559df887684SGerd Hoffmann VncInfo2 *info; 560df887684SGerd Hoffmann VncDisplay *vd; 561df887684SGerd Hoffmann DeviceState *dev; 5624ee74fa7SDaniel P. Berrange size_t i; 563df887684SGerd Hoffmann 564df887684SGerd Hoffmann QTAILQ_FOREACH(vd, &vnc_displays, next) { 565df887684SGerd Hoffmann info = g_new0(VncInfo2, 1); 566df887684SGerd Hoffmann info->id = g_strdup(vd->id); 567df887684SGerd Hoffmann info->clients = qmp_query_client_list(vd); 5682a7e6857SDaniel P. Berrange qmp_query_auth(vd->auth, vd->subauth, &info->auth, 5692a7e6857SDaniel P. Berrange &info->vencrypt, &info->has_vencrypt); 570df887684SGerd Hoffmann if (vd->dcl.con) { 571df887684SGerd Hoffmann dev = DEVICE(object_property_get_link(OBJECT(vd->dcl.con), 572df887684SGerd Hoffmann "device", NULL)); 573df887684SGerd Hoffmann info->has_display = true; 574df887684SGerd Hoffmann info->display = g_strdup(dev->id); 575df887684SGerd Hoffmann } 57613e1d0e7SDaniel P. Berrange for (i = 0; vd->listener != NULL && i < vd->listener->nsioc; i++) { 57704d2529dSDaniel P. Berrange info->server = qmp_query_server_entry( 57813e1d0e7SDaniel P. Berrange vd->listener->sioc[i], false, vd->auth, vd->subauth, 57913e1d0e7SDaniel P. Berrange info->server); 580df887684SGerd Hoffmann } 58113e1d0e7SDaniel P. Berrange for (i = 0; vd->wslistener != NULL && i < vd->wslistener->nsioc; i++) { 58204d2529dSDaniel P. Berrange info->server = qmp_query_server_entry( 58313e1d0e7SDaniel P. Berrange vd->wslistener->sioc[i], true, vd->ws_auth, 5844ee74fa7SDaniel P. Berrange vd->ws_subauth, info->server); 585df887684SGerd Hoffmann } 586df887684SGerd Hoffmann 587df887684SGerd Hoffmann item = g_new0(VncInfo2List, 1); 588df887684SGerd Hoffmann item->value = info; 589df887684SGerd Hoffmann item->next = prev; 590df887684SGerd Hoffmann prev = item; 591df887684SGerd Hoffmann } 592df887684SGerd Hoffmann return prev; 593df887684SGerd Hoffmann } 594df887684SGerd Hoffmann 5953e230dd2SCorentin Chary /* TODO 5963e230dd2SCorentin Chary 1) Get the queue working for IO. 5973e230dd2SCorentin Chary 2) there is some weirdness when using the -S option (the screen is grey 5983e230dd2SCorentin Chary and not totally invalidated 5993e230dd2SCorentin Chary 3) resolutions > 1024 6003e230dd2SCorentin Chary */ 6013e230dd2SCorentin Chary 6026af998dbSDaniel P. Berrange static int vnc_update_client(VncState *vs, int has_dirty); 6033e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs); 6043e230dd2SCorentin Chary 6053e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs); 6063e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental, 6073e230dd2SCorentin Chary int x_position, int y_position, 6083e230dd2SCorentin Chary int w, int h); 6090f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl); 6103e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd); 6113e230dd2SCorentin Chary 612d05959c2SGerd Hoffmann static int vnc_width(VncDisplay *vd) 613d05959c2SGerd Hoffmann { 614d05959c2SGerd Hoffmann return MIN(VNC_MAX_WIDTH, ROUND_UP(surface_width(vd->ds), 615d05959c2SGerd Hoffmann VNC_DIRTY_PIXELS_PER_BIT)); 616d05959c2SGerd Hoffmann } 617d05959c2SGerd Hoffmann 618d05959c2SGerd Hoffmann static int vnc_height(VncDisplay *vd) 619d05959c2SGerd Hoffmann { 620d05959c2SGerd Hoffmann return MIN(VNC_MAX_HEIGHT, surface_height(vd->ds)); 621d05959c2SGerd Hoffmann } 622d05959c2SGerd Hoffmann 623bea60dd7SPeter Lieven static void vnc_set_area_dirty(DECLARE_BITMAP(dirty[VNC_MAX_HEIGHT], 624bea60dd7SPeter Lieven VNC_MAX_WIDTH / VNC_DIRTY_PIXELS_PER_BIT), 625f7b3d68cSGerd Hoffmann VncDisplay *vd, 626f7b3d68cSGerd Hoffmann int x, int y, int w, int h) 627f7b3d68cSGerd Hoffmann { 628f7b3d68cSGerd Hoffmann int width = vnc_width(vd); 629f7b3d68cSGerd Hoffmann int height = vnc_height(vd); 630f7b3d68cSGerd Hoffmann 63191937225SPeter Lieven /* this is needed this to ensure we updated all affected 63291937225SPeter Lieven * blocks if x % VNC_DIRTY_PIXELS_PER_BIT != 0 */ 633b4c85ddcSPeter Lieven w += (x % VNC_DIRTY_PIXELS_PER_BIT); 634b4c85ddcSPeter Lieven x -= (x % VNC_DIRTY_PIXELS_PER_BIT); 6353e230dd2SCorentin Chary 6369f64916dSGerd Hoffmann x = MIN(x, width); 6379f64916dSGerd Hoffmann y = MIN(y, height); 6389f64916dSGerd Hoffmann w = MIN(x + w, width) - x; 63991937225SPeter Lieven h = MIN(y + h, height); 6403e230dd2SCorentin Chary 641b4c85ddcSPeter Lieven for (; y < h; y++) { 642bea60dd7SPeter Lieven bitmap_set(dirty[y], x / VNC_DIRTY_PIXELS_PER_BIT, 64391937225SPeter Lieven DIV_ROUND_UP(w, VNC_DIRTY_PIXELS_PER_BIT)); 644b4c85ddcSPeter Lieven } 6453e230dd2SCorentin Chary } 6463e230dd2SCorentin Chary 647bea60dd7SPeter Lieven static void vnc_dpy_update(DisplayChangeListener *dcl, 648bea60dd7SPeter Lieven int x, int y, int w, int h) 649bea60dd7SPeter Lieven { 650bea60dd7SPeter Lieven VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 651bea60dd7SPeter Lieven struct VncSurface *s = &vd->guest; 652bea60dd7SPeter Lieven 653f7b3d68cSGerd Hoffmann vnc_set_area_dirty(s->dirty, vd, x, y, w, h); 654bea60dd7SPeter Lieven } 655bea60dd7SPeter Lieven 6563e230dd2SCorentin Chary void vnc_framebuffer_update(VncState *vs, int x, int y, int w, int h, 6573e230dd2SCorentin Chary int32_t encoding) 6583e230dd2SCorentin Chary { 6593e230dd2SCorentin Chary vnc_write_u16(vs, x); 6603e230dd2SCorentin Chary vnc_write_u16(vs, y); 6613e230dd2SCorentin Chary vnc_write_u16(vs, w); 6623e230dd2SCorentin Chary vnc_write_u16(vs, h); 6633e230dd2SCorentin Chary 6643e230dd2SCorentin Chary vnc_write_s32(vs, encoding); 6653e230dd2SCorentin Chary } 6663e230dd2SCorentin Chary 66732ed2680STim Hardeck 6683e230dd2SCorentin Chary static void vnc_desktop_resize(VncState *vs) 6693e230dd2SCorentin Chary { 67004d2529dSDaniel P. Berrange if (vs->ioc == NULL || !vnc_has_feature(vs, VNC_FEATURE_RESIZE)) { 6713e230dd2SCorentin Chary return; 6723e230dd2SCorentin Chary } 673bea60dd7SPeter Lieven if (vs->client_width == pixman_image_get_width(vs->vd->server) && 674bea60dd7SPeter Lieven vs->client_height == pixman_image_get_height(vs->vd->server)) { 6753e230dd2SCorentin Chary return; 6763e230dd2SCorentin Chary } 6774c956bd8SDaniel P. Berrange 6784c956bd8SDaniel P. Berrange assert(pixman_image_get_width(vs->vd->server) < 65536 && 6794c956bd8SDaniel P. Berrange pixman_image_get_width(vs->vd->server) >= 0); 6804c956bd8SDaniel P. Berrange assert(pixman_image_get_height(vs->vd->server) < 65536 && 6814c956bd8SDaniel P. Berrange pixman_image_get_height(vs->vd->server) >= 0); 682bea60dd7SPeter Lieven vs->client_width = pixman_image_get_width(vs->vd->server); 683bea60dd7SPeter Lieven vs->client_height = pixman_image_get_height(vs->vd->server); 684bd023f95SCorentin Chary vnc_lock_output(vs); 6853e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 6863e230dd2SCorentin Chary vnc_write_u8(vs, 0); 6873e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 6883e230dd2SCorentin Chary vnc_framebuffer_update(vs, 0, 0, vs->client_width, vs->client_height, 6893e230dd2SCorentin Chary VNC_ENCODING_DESKTOPRESIZE); 690bd023f95SCorentin Chary vnc_unlock_output(vs); 6913e230dd2SCorentin Chary vnc_flush(vs); 6923e230dd2SCorentin Chary } 6933e230dd2SCorentin Chary 694bd023f95SCorentin Chary static void vnc_abort_display_jobs(VncDisplay *vd) 695bd023f95SCorentin Chary { 696bd023f95SCorentin Chary VncState *vs; 697bd023f95SCorentin Chary 698bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 699bd023f95SCorentin Chary vnc_lock_output(vs); 700bd023f95SCorentin Chary vs->abort = true; 701bd023f95SCorentin Chary vnc_unlock_output(vs); 702bd023f95SCorentin Chary } 703bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 704bd023f95SCorentin Chary vnc_jobs_join(vs); 705bd023f95SCorentin Chary } 706bd023f95SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 707bd023f95SCorentin Chary vnc_lock_output(vs); 708bbcdeb62SGerd Hoffmann if (vs->update == VNC_STATE_UPDATE_NONE && 709bbcdeb62SGerd Hoffmann vs->job_update != VNC_STATE_UPDATE_NONE) { 710bbcdeb62SGerd Hoffmann /* job aborted before completion */ 711bbcdeb62SGerd Hoffmann vs->update = vs->job_update; 712bbcdeb62SGerd Hoffmann vs->job_update = VNC_STATE_UPDATE_NONE; 713bbcdeb62SGerd Hoffmann } 714bd023f95SCorentin Chary vs->abort = false; 715bd023f95SCorentin Chary vnc_unlock_output(vs); 716bd023f95SCorentin Chary } 717bd023f95SCorentin Chary } 718bd023f95SCorentin Chary 7199f64916dSGerd Hoffmann int vnc_server_fb_stride(VncDisplay *vd) 7209f64916dSGerd Hoffmann { 7219f64916dSGerd Hoffmann return pixman_image_get_stride(vd->server); 7229f64916dSGerd Hoffmann } 7239f64916dSGerd Hoffmann 7249f64916dSGerd Hoffmann void *vnc_server_fb_ptr(VncDisplay *vd, int x, int y) 7259f64916dSGerd Hoffmann { 7269f64916dSGerd Hoffmann uint8_t *ptr; 7279f64916dSGerd Hoffmann 7289f64916dSGerd Hoffmann ptr = (uint8_t *)pixman_image_get_data(vd->server); 7299f64916dSGerd Hoffmann ptr += y * vnc_server_fb_stride(vd); 7309f64916dSGerd Hoffmann ptr += x * VNC_SERVER_FB_BYTES; 7319f64916dSGerd Hoffmann return ptr; 7329f64916dSGerd Hoffmann } 7339f64916dSGerd Hoffmann 734453f842bSGerd Hoffmann static void vnc_update_server_surface(VncDisplay *vd) 735453f842bSGerd Hoffmann { 736b69a553bSDaniel P. Berrange int width, height; 737b69a553bSDaniel P. Berrange 738453f842bSGerd Hoffmann qemu_pixman_image_unref(vd->server); 739453f842bSGerd Hoffmann vd->server = NULL; 740453f842bSGerd Hoffmann 741c7628bffSGerd Hoffmann if (QTAILQ_EMPTY(&vd->clients)) { 742c7628bffSGerd Hoffmann return; 743c7628bffSGerd Hoffmann } 744c7628bffSGerd Hoffmann 745b69a553bSDaniel P. Berrange width = vnc_width(vd); 746b69a553bSDaniel P. Berrange height = vnc_height(vd); 747453f842bSGerd Hoffmann vd->server = pixman_image_create_bits(VNC_SERVER_FB_FORMAT, 748b69a553bSDaniel P. Berrange width, height, 749453f842bSGerd Hoffmann NULL, 0); 750b69a553bSDaniel P. Berrange 751b69a553bSDaniel P. Berrange memset(vd->guest.dirty, 0x00, sizeof(vd->guest.dirty)); 752b69a553bSDaniel P. Berrange vnc_set_area_dirty(vd->guest.dirty, vd, 0, 0, 753b69a553bSDaniel P. Berrange width, height); 754453f842bSGerd Hoffmann } 755453f842bSGerd Hoffmann 75661e77a5fSGerd Hoffmann static bool vnc_check_pageflip(DisplaySurface *s1, 75761e77a5fSGerd Hoffmann DisplaySurface *s2) 75861e77a5fSGerd Hoffmann { 75961e77a5fSGerd Hoffmann return (s1 != NULL && 76061e77a5fSGerd Hoffmann s2 != NULL && 76161e77a5fSGerd Hoffmann surface_width(s1) == surface_width(s2) && 76261e77a5fSGerd Hoffmann surface_height(s1) == surface_height(s2) && 76361e77a5fSGerd Hoffmann surface_format(s1) == surface_format(s2)); 76461e77a5fSGerd Hoffmann 76561e77a5fSGerd Hoffmann } 76661e77a5fSGerd Hoffmann 767c12aeb86SGerd Hoffmann static void vnc_dpy_switch(DisplayChangeListener *dcl, 768c12aeb86SGerd Hoffmann DisplaySurface *surface) 7693e230dd2SCorentin Chary { 7702e5567c9SGerd Hoffmann static const char placeholder_msg[] = 7712e5567c9SGerd Hoffmann "Display output is not active."; 7722e5567c9SGerd Hoffmann static DisplaySurface *placeholder; 77321ef45d7SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 77461e77a5fSGerd Hoffmann bool pageflip = vnc_check_pageflip(vd->ds, surface); 7753e230dd2SCorentin Chary VncState *vs; 7763e230dd2SCorentin Chary 7772e5567c9SGerd Hoffmann if (surface == NULL) { 7782e5567c9SGerd Hoffmann if (placeholder == NULL) { 7792e5567c9SGerd Hoffmann placeholder = qemu_create_message_surface(640, 480, placeholder_msg); 7802e5567c9SGerd Hoffmann } 7812e5567c9SGerd Hoffmann surface = placeholder; 7822e5567c9SGerd Hoffmann } 7832e5567c9SGerd Hoffmann 784bd023f95SCorentin Chary vnc_abort_display_jobs(vd); 785453f842bSGerd Hoffmann vd->ds = surface; 786bd023f95SCorentin Chary 7873e230dd2SCorentin Chary /* guest surface */ 7889f64916dSGerd Hoffmann qemu_pixman_image_unref(vd->guest.fb); 789d39fa6d8SGerd Hoffmann vd->guest.fb = pixman_image_ref(surface->image); 790d39fa6d8SGerd Hoffmann vd->guest.format = surface->format; 7913e230dd2SCorentin Chary 79261e77a5fSGerd Hoffmann if (pageflip) { 79361e77a5fSGerd Hoffmann vnc_set_area_dirty(vd->guest.dirty, vd, 0, 0, 79461e77a5fSGerd Hoffmann surface_width(surface), 79561e77a5fSGerd Hoffmann surface_height(surface)); 79661e77a5fSGerd Hoffmann return; 79761e77a5fSGerd Hoffmann } 79861e77a5fSGerd Hoffmann 79961e77a5fSGerd Hoffmann /* server surface */ 80061e77a5fSGerd Hoffmann vnc_update_server_surface(vd); 80161e77a5fSGerd Hoffmann 8023e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 8033e230dd2SCorentin Chary vnc_colordepth(vs); 8043e230dd2SCorentin Chary vnc_desktop_resize(vs); 8053e230dd2SCorentin Chary if (vs->vd->cursor) { 8063e230dd2SCorentin Chary vnc_cursor_define(vs); 8073e230dd2SCorentin Chary } 808bea60dd7SPeter Lieven memset(vs->dirty, 0x00, sizeof(vs->dirty)); 809f7b3d68cSGerd Hoffmann vnc_set_area_dirty(vs->dirty, vd, 0, 0, 810b69a553bSDaniel P. Berrange vnc_width(vd), 811b69a553bSDaniel P. Berrange vnc_height(vd)); 812e2b72cb6SDaniel P. Berrange vnc_update_throttle_offset(vs); 8133e230dd2SCorentin Chary } 8143e230dd2SCorentin Chary } 8153e230dd2SCorentin Chary 8163e230dd2SCorentin Chary /* fastest code */ 8179f64916dSGerd Hoffmann static void vnc_write_pixels_copy(VncState *vs, 8183e230dd2SCorentin Chary void *pixels, int size) 8193e230dd2SCorentin Chary { 8203e230dd2SCorentin Chary vnc_write(vs, pixels, size); 8213e230dd2SCorentin Chary } 8223e230dd2SCorentin Chary 8233e230dd2SCorentin Chary /* slowest but generic code. */ 8243e230dd2SCorentin Chary void vnc_convert_pixel(VncState *vs, uint8_t *buf, uint32_t v) 8253e230dd2SCorentin Chary { 8263e230dd2SCorentin Chary uint8_t r, g, b; 8273e230dd2SCorentin Chary 8289f64916dSGerd Hoffmann #if VNC_SERVER_FB_FORMAT == PIXMAN_FORMAT(32, PIXMAN_TYPE_ARGB, 0, 8, 8, 8) 8299f64916dSGerd Hoffmann r = (((v & 0x00ff0000) >> 16) << vs->client_pf.rbits) >> 8; 8309f64916dSGerd Hoffmann g = (((v & 0x0000ff00) >> 8) << vs->client_pf.gbits) >> 8; 8319f64916dSGerd Hoffmann b = (((v & 0x000000ff) >> 0) << vs->client_pf.bbits) >> 8; 8329f64916dSGerd Hoffmann #else 8339f64916dSGerd Hoffmann # error need some bits here if you change VNC_SERVER_FB_FORMAT 8349f64916dSGerd Hoffmann #endif 8359f64916dSGerd Hoffmann v = (r << vs->client_pf.rshift) | 8369f64916dSGerd Hoffmann (g << vs->client_pf.gshift) | 8379f64916dSGerd Hoffmann (b << vs->client_pf.bshift); 8389f64916dSGerd Hoffmann switch (vs->client_pf.bytes_per_pixel) { 8393e230dd2SCorentin Chary case 1: 8403e230dd2SCorentin Chary buf[0] = v; 8413e230dd2SCorentin Chary break; 8423e230dd2SCorentin Chary case 2: 8439f64916dSGerd Hoffmann if (vs->client_be) { 8443e230dd2SCorentin Chary buf[0] = v >> 8; 8453e230dd2SCorentin Chary buf[1] = v; 8463e230dd2SCorentin Chary } else { 8473e230dd2SCorentin Chary buf[1] = v >> 8; 8483e230dd2SCorentin Chary buf[0] = v; 8493e230dd2SCorentin Chary } 8503e230dd2SCorentin Chary break; 8513e230dd2SCorentin Chary default: 8523e230dd2SCorentin Chary case 4: 8539f64916dSGerd Hoffmann if (vs->client_be) { 8543e230dd2SCorentin Chary buf[0] = v >> 24; 8553e230dd2SCorentin Chary buf[1] = v >> 16; 8563e230dd2SCorentin Chary buf[2] = v >> 8; 8573e230dd2SCorentin Chary buf[3] = v; 8583e230dd2SCorentin Chary } else { 8593e230dd2SCorentin Chary buf[3] = v >> 24; 8603e230dd2SCorentin Chary buf[2] = v >> 16; 8613e230dd2SCorentin Chary buf[1] = v >> 8; 8623e230dd2SCorentin Chary buf[0] = v; 8633e230dd2SCorentin Chary } 8643e230dd2SCorentin Chary break; 8653e230dd2SCorentin Chary } 8663e230dd2SCorentin Chary } 8673e230dd2SCorentin Chary 8689f64916dSGerd Hoffmann static void vnc_write_pixels_generic(VncState *vs, 8693e230dd2SCorentin Chary void *pixels1, int size) 8703e230dd2SCorentin Chary { 8713e230dd2SCorentin Chary uint8_t buf[4]; 8723e230dd2SCorentin Chary 8739f64916dSGerd Hoffmann if (VNC_SERVER_FB_BYTES == 4) { 8743e230dd2SCorentin Chary uint32_t *pixels = pixels1; 8753e230dd2SCorentin Chary int n, i; 8763e230dd2SCorentin Chary n = size >> 2; 8773e230dd2SCorentin Chary for (i = 0; i < n; i++) { 8783e230dd2SCorentin Chary vnc_convert_pixel(vs, buf, pixels[i]); 8799f64916dSGerd Hoffmann vnc_write(vs, buf, vs->client_pf.bytes_per_pixel); 8803e230dd2SCorentin Chary } 8813e230dd2SCorentin Chary } 8823e230dd2SCorentin Chary } 8833e230dd2SCorentin Chary 8843e230dd2SCorentin Chary int vnc_raw_send_framebuffer_update(VncState *vs, int x, int y, int w, int h) 8853e230dd2SCorentin Chary { 8863e230dd2SCorentin Chary int i; 8873e230dd2SCorentin Chary uint8_t *row; 8883e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 8893e230dd2SCorentin Chary 8909f64916dSGerd Hoffmann row = vnc_server_fb_ptr(vd, x, y); 8913e230dd2SCorentin Chary for (i = 0; i < h; i++) { 8929f64916dSGerd Hoffmann vs->write_pixels(vs, row, w * VNC_SERVER_FB_BYTES); 8939f64916dSGerd Hoffmann row += vnc_server_fb_stride(vd); 8943e230dd2SCorentin Chary } 8953e230dd2SCorentin Chary return 1; 8963e230dd2SCorentin Chary } 8973e230dd2SCorentin Chary 898bd023f95SCorentin Chary int vnc_send_framebuffer_update(VncState *vs, int x, int y, int w, int h) 8993e230dd2SCorentin Chary { 9003e230dd2SCorentin Chary int n = 0; 9013e230dd2SCorentin Chary 9023e230dd2SCorentin Chary switch(vs->vnc_encoding) { 9033e230dd2SCorentin Chary case VNC_ENCODING_ZLIB: 9043e230dd2SCorentin Chary n = vnc_zlib_send_framebuffer_update(vs, x, y, w, h); 9053e230dd2SCorentin Chary break; 9063e230dd2SCorentin Chary case VNC_ENCODING_HEXTILE: 9073e230dd2SCorentin Chary vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_HEXTILE); 9083e230dd2SCorentin Chary n = vnc_hextile_send_framebuffer_update(vs, x, y, w, h); 9093e230dd2SCorentin Chary break; 9103e230dd2SCorentin Chary case VNC_ENCODING_TIGHT: 9113e230dd2SCorentin Chary n = vnc_tight_send_framebuffer_update(vs, x, y, w, h); 9123e230dd2SCorentin Chary break; 913efe556adSCorentin Chary case VNC_ENCODING_TIGHT_PNG: 914efe556adSCorentin Chary n = vnc_tight_png_send_framebuffer_update(vs, x, y, w, h); 915efe556adSCorentin Chary break; 916148954faSCorentin Chary case VNC_ENCODING_ZRLE: 917148954faSCorentin Chary n = vnc_zrle_send_framebuffer_update(vs, x, y, w, h); 918148954faSCorentin Chary break; 919148954faSCorentin Chary case VNC_ENCODING_ZYWRLE: 920148954faSCorentin Chary n = vnc_zywrle_send_framebuffer_update(vs, x, y, w, h); 921148954faSCorentin Chary break; 9223e230dd2SCorentin Chary default: 923de3f7de7SPeter Lieven vnc_framebuffer_update(vs, x, y, w, h, VNC_ENCODING_RAW); 924de3f7de7SPeter Lieven n = vnc_raw_send_framebuffer_update(vs, x, y, w, h); 925*0780ec7bSGerd Hoffmann break; 926de3f7de7SPeter Lieven } 9273e230dd2SCorentin Chary return n; 9283e230dd2SCorentin Chary } 9293e230dd2SCorentin Chary 9307c20b4a3SGerd Hoffmann static void vnc_mouse_set(DisplayChangeListener *dcl, 9317c20b4a3SGerd Hoffmann int x, int y, int visible) 9323e230dd2SCorentin Chary { 9333e230dd2SCorentin Chary /* can we ask the client(s) to move the pointer ??? */ 9343e230dd2SCorentin Chary } 9353e230dd2SCorentin Chary 9363e230dd2SCorentin Chary static int vnc_cursor_define(VncState *vs) 9373e230dd2SCorentin Chary { 9383e230dd2SCorentin Chary QEMUCursor *c = vs->vd->cursor; 9393e230dd2SCorentin Chary int isize; 9403e230dd2SCorentin Chary 9413e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_RICH_CURSOR)) { 942d01f9595SCorentin Chary vnc_lock_output(vs); 9433e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 9443e230dd2SCorentin Chary vnc_write_u8(vs, 0); /* padding */ 9453e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* # of rects */ 9463e230dd2SCorentin Chary vnc_framebuffer_update(vs, c->hot_x, c->hot_y, c->width, c->height, 9473e230dd2SCorentin Chary VNC_ENCODING_RICH_CURSOR); 9489f64916dSGerd Hoffmann isize = c->width * c->height * vs->client_pf.bytes_per_pixel; 9499f64916dSGerd Hoffmann vnc_write_pixels_generic(vs, c->data, isize); 9503e230dd2SCorentin Chary vnc_write(vs, vs->vd->cursor_mask, vs->vd->cursor_msize); 951d01f9595SCorentin Chary vnc_unlock_output(vs); 9523e230dd2SCorentin Chary return 0; 9533e230dd2SCorentin Chary } 9543e230dd2SCorentin Chary return -1; 9553e230dd2SCorentin Chary } 9563e230dd2SCorentin Chary 9577c20b4a3SGerd Hoffmann static void vnc_dpy_cursor_define(DisplayChangeListener *dcl, 9587c20b4a3SGerd Hoffmann QEMUCursor *c) 9593e230dd2SCorentin Chary { 960d616ccc5SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 9613e230dd2SCorentin Chary VncState *vs; 9623e230dd2SCorentin Chary 9633e230dd2SCorentin Chary cursor_put(vd->cursor); 9647267c094SAnthony Liguori g_free(vd->cursor_mask); 9653e230dd2SCorentin Chary 9663e230dd2SCorentin Chary vd->cursor = c; 9673e230dd2SCorentin Chary cursor_get(vd->cursor); 9683e230dd2SCorentin Chary vd->cursor_msize = cursor_get_mono_bpl(c) * c->height; 9697267c094SAnthony Liguori vd->cursor_mask = g_malloc0(vd->cursor_msize); 9703e230dd2SCorentin Chary cursor_get_mono_mask(c, 0, vd->cursor_mask); 9713e230dd2SCorentin Chary 9723e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 9733e230dd2SCorentin Chary vnc_cursor_define(vs); 9743e230dd2SCorentin Chary } 9753e230dd2SCorentin Chary } 9763e230dd2SCorentin Chary 9774769a881SChih-Min Chao static int find_and_clear_dirty_height(VncState *vs, 9786c71a539SCorentin Chary int y, int last_x, int x, int height) 9793e230dd2SCorentin Chary { 9803e230dd2SCorentin Chary int h; 9813e230dd2SCorentin Chary 9826c71a539SCorentin Chary for (h = 1; h < (height - y); h++) { 983bc2429b9SCorentin Chary if (!test_bit(last_x, vs->dirty[y + h])) { 9843e230dd2SCorentin Chary break; 985bc2429b9SCorentin Chary } 986863d7c91SPeter Lieven bitmap_clear(vs->dirty[y + h], last_x, x - last_x); 9873e230dd2SCorentin Chary } 9883e230dd2SCorentin Chary 9893e230dd2SCorentin Chary return h; 9903e230dd2SCorentin Chary } 9913e230dd2SCorentin Chary 992e2b72cb6SDaniel P. Berrange /* 993e2b72cb6SDaniel P. Berrange * Figure out how much pending data we should allow in the output 994e2b72cb6SDaniel P. Berrange * buffer before we throttle incremental display updates, and/or 995e2b72cb6SDaniel P. Berrange * drop audio samples. 996e2b72cb6SDaniel P. Berrange * 997e2b72cb6SDaniel P. Berrange * We allow for equiv of 1 full display's worth of FB updates, 998e2b72cb6SDaniel P. Berrange * and 1 second of audio samples. If audio backlog was larger 999e2b72cb6SDaniel P. Berrange * than that the client would already suffering awful audio 1000e2b72cb6SDaniel P. Berrange * glitches, so dropping samples is no worse really). 1001e2b72cb6SDaniel P. Berrange */ 1002e2b72cb6SDaniel P. Berrange static void vnc_update_throttle_offset(VncState *vs) 1003e2b72cb6SDaniel P. Berrange { 1004e2b72cb6SDaniel P. Berrange size_t offset = 1005e2b72cb6SDaniel P. Berrange vs->client_width * vs->client_height * vs->client_pf.bytes_per_pixel; 1006e2b72cb6SDaniel P. Berrange 1007e2b72cb6SDaniel P. Berrange if (vs->audio_cap) { 1008e2b72cb6SDaniel P. Berrange int bps; 1009e2b72cb6SDaniel P. Berrange switch (vs->as.fmt) { 1010e2b72cb6SDaniel P. Berrange default: 101185bc5852SKővágó, Zoltán case AUDIO_FORMAT_U8: 101285bc5852SKővágó, Zoltán case AUDIO_FORMAT_S8: 1013e2b72cb6SDaniel P. Berrange bps = 1; 1014e2b72cb6SDaniel P. Berrange break; 101585bc5852SKővágó, Zoltán case AUDIO_FORMAT_U16: 101685bc5852SKővágó, Zoltán case AUDIO_FORMAT_S16: 1017e2b72cb6SDaniel P. Berrange bps = 2; 1018e2b72cb6SDaniel P. Berrange break; 101985bc5852SKővágó, Zoltán case AUDIO_FORMAT_U32: 102085bc5852SKővágó, Zoltán case AUDIO_FORMAT_S32: 1021e2b72cb6SDaniel P. Berrange bps = 4; 1022e2b72cb6SDaniel P. Berrange break; 1023e2b72cb6SDaniel P. Berrange } 1024cf070658SDaniel P. Berrangé offset += vs->as.freq * bps * vs->as.nchannels; 1025e2b72cb6SDaniel P. Berrange } 1026e2b72cb6SDaniel P. Berrange 1027e2b72cb6SDaniel P. Berrange /* Put a floor of 1MB on offset, so that if we have a large pending 1028e2b72cb6SDaniel P. Berrange * buffer and the display is resized to a small size & back again 1029e2b72cb6SDaniel P. Berrange * we don't suddenly apply a tiny send limit 1030e2b72cb6SDaniel P. Berrange */ 1031e2b72cb6SDaniel P. Berrange offset = MAX(offset, 1024 * 1024); 1032e2b72cb6SDaniel P. Berrange 10336aa22a29SDaniel P. Berrange if (vs->throttle_output_offset != offset) { 10346aa22a29SDaniel P. Berrange trace_vnc_client_throttle_threshold( 10356aa22a29SDaniel P. Berrange vs, vs->ioc, vs->throttle_output_offset, offset, vs->client_width, 10366aa22a29SDaniel P. Berrange vs->client_height, vs->client_pf.bytes_per_pixel, vs->audio_cap); 10376aa22a29SDaniel P. Berrange } 10386aa22a29SDaniel P. Berrange 1039e2b72cb6SDaniel P. Berrange vs->throttle_output_offset = offset; 1040e2b72cb6SDaniel P. Berrange } 1041e2b72cb6SDaniel P. Berrange 10420bad8342SDaniel P. Berrange static bool vnc_should_update(VncState *vs) 10430bad8342SDaniel P. Berrange { 10440bad8342SDaniel P. Berrange switch (vs->update) { 10450bad8342SDaniel P. Berrange case VNC_STATE_UPDATE_NONE: 10460bad8342SDaniel P. Berrange break; 10470bad8342SDaniel P. Berrange case VNC_STATE_UPDATE_INCREMENTAL: 1048e2b72cb6SDaniel P. Berrange /* Only allow incremental updates if the pending send queue 1049ada8d2e4SDaniel P. Berrange * is less than the permitted threshold, and the job worker 1050ada8d2e4SDaniel P. Berrange * is completely idle. 10510bad8342SDaniel P. Berrange */ 1052ada8d2e4SDaniel P. Berrange if (vs->output.offset < vs->throttle_output_offset && 1053ada8d2e4SDaniel P. Berrange vs->job_update == VNC_STATE_UPDATE_NONE) { 10540bad8342SDaniel P. Berrange return true; 10550bad8342SDaniel P. Berrange } 10566aa22a29SDaniel P. Berrange trace_vnc_client_throttle_incremental( 10576aa22a29SDaniel P. Berrange vs, vs->ioc, vs->job_update, vs->output.offset); 10580bad8342SDaniel P. Berrange break; 10590bad8342SDaniel P. Berrange case VNC_STATE_UPDATE_FORCE: 1060ada8d2e4SDaniel P. Berrange /* Only allow forced updates if the pending send queue 1061ada8d2e4SDaniel P. Berrange * does not contain a previous forced update, and the 1062ada8d2e4SDaniel P. Berrange * job worker is completely idle. 1063ada8d2e4SDaniel P. Berrange * 1064ada8d2e4SDaniel P. Berrange * Note this means we'll queue a forced update, even if 1065ada8d2e4SDaniel P. Berrange * the output buffer size is otherwise over the throttle 1066ada8d2e4SDaniel P. Berrange * output limit. 1067ada8d2e4SDaniel P. Berrange */ 1068ada8d2e4SDaniel P. Berrange if (vs->force_update_offset == 0 && 1069ada8d2e4SDaniel P. Berrange vs->job_update == VNC_STATE_UPDATE_NONE) { 10700bad8342SDaniel P. Berrange return true; 10710bad8342SDaniel P. Berrange } 10726aa22a29SDaniel P. Berrange trace_vnc_client_throttle_forced( 10736aa22a29SDaniel P. Berrange vs, vs->ioc, vs->job_update, vs->force_update_offset); 1074ada8d2e4SDaniel P. Berrange break; 1075ada8d2e4SDaniel P. Berrange } 10760bad8342SDaniel P. Berrange return false; 10770bad8342SDaniel P. Berrange } 10780bad8342SDaniel P. Berrange 10796af998dbSDaniel P. Berrange static int vnc_update_client(VncState *vs, int has_dirty) 10803e230dd2SCorentin Chary { 10813e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 1082bd023f95SCorentin Chary VncJob *job; 10833e230dd2SCorentin Chary int y; 10842f487a3dSPeter Lieven int height, width; 1085bd023f95SCorentin Chary int n = 0; 1086bd023f95SCorentin Chary 1087b939eb89SDaniel P. Berrange if (vs->disconnecting) { 1088b939eb89SDaniel P. Berrange vnc_disconnect_finish(vs); 1089b939eb89SDaniel P. Berrange return 0; 1090b939eb89SDaniel P. Berrange } 1091b939eb89SDaniel P. Berrange 1092b939eb89SDaniel P. Berrange vs->has_dirty += has_dirty; 10930bad8342SDaniel P. Berrange if (!vnc_should_update(vs)) { 10943e230dd2SCorentin Chary return 0; 1095b939eb89SDaniel P. Berrange } 10963e230dd2SCorentin Chary 1097fef1bbadSDaniel P. Berrange if (!vs->has_dirty && vs->update != VNC_STATE_UPDATE_FORCE) { 10983e230dd2SCorentin Chary return 0; 1099b939eb89SDaniel P. Berrange } 11003e230dd2SCorentin Chary 11013e230dd2SCorentin Chary /* 11023e230dd2SCorentin Chary * Send screen updates to the vnc client using the server 11033e230dd2SCorentin Chary * surface and server dirty map. guest surface updates 11043e230dd2SCorentin Chary * happening in parallel don't disturb us, the next pass will 11053e230dd2SCorentin Chary * send them to the client. 11063e230dd2SCorentin Chary */ 1107bd023f95SCorentin Chary job = vnc_job_new(vs); 11083e230dd2SCorentin Chary 1109bea60dd7SPeter Lieven height = pixman_image_get_height(vd->server); 1110bea60dd7SPeter Lieven width = pixman_image_get_width(vd->server); 11113e230dd2SCorentin Chary 111212b316d4SPeter Lieven y = 0; 111312b316d4SPeter Lieven for (;;) { 111412b316d4SPeter Lieven int x, h; 111512b316d4SPeter Lieven unsigned long x2; 111612b316d4SPeter Lieven unsigned long offset = find_next_bit((unsigned long *) &vs->dirty, 111712b316d4SPeter Lieven height * VNC_DIRTY_BPL(vs), 111812b316d4SPeter Lieven y * VNC_DIRTY_BPL(vs)); 111912b316d4SPeter Lieven if (offset == height * VNC_DIRTY_BPL(vs)) { 112012b316d4SPeter Lieven /* no more dirty bits */ 112112b316d4SPeter Lieven break; 11223e230dd2SCorentin Chary } 112312b316d4SPeter Lieven y = offset / VNC_DIRTY_BPL(vs); 112412b316d4SPeter Lieven x = offset % VNC_DIRTY_BPL(vs); 112512b316d4SPeter Lieven x2 = find_next_zero_bit((unsigned long *) &vs->dirty[y], 112612b316d4SPeter Lieven VNC_DIRTY_BPL(vs), x); 112712b316d4SPeter Lieven bitmap_clear(vs->dirty[y], x, x2 - x); 112812b316d4SPeter Lieven h = find_and_clear_dirty_height(vs, y, x, x2, height); 11292f487a3dSPeter Lieven x2 = MIN(x2, width / VNC_DIRTY_PIXELS_PER_BIT); 11302f487a3dSPeter Lieven if (x2 > x) { 113112b316d4SPeter Lieven n += vnc_job_add_rect(job, x * VNC_DIRTY_PIXELS_PER_BIT, y, 113212b316d4SPeter Lieven (x2 - x) * VNC_DIRTY_PIXELS_PER_BIT, h); 11333e230dd2SCorentin Chary } 11340e7d6f60SPeter Lieven if (!x && x2 == width / VNC_DIRTY_PIXELS_PER_BIT) { 11350e7d6f60SPeter Lieven y += h; 11360e7d6f60SPeter Lieven if (y == height) { 11370e7d6f60SPeter Lieven break; 11380e7d6f60SPeter Lieven } 11390e7d6f60SPeter Lieven } 11402f487a3dSPeter Lieven } 1141bd023f95SCorentin Chary 1142ada8d2e4SDaniel P. Berrange vs->job_update = vs->update; 1143728a7ac9SDaniel P. Berrange vs->update = VNC_STATE_UPDATE_NONE; 1144ada8d2e4SDaniel P. Berrange vnc_job_push(job); 114563658280SGerd Hoffmann vs->has_dirty = 0; 1146bd023f95SCorentin Chary return n; 11473e230dd2SCorentin Chary } 11483e230dd2SCorentin Chary 11493e230dd2SCorentin Chary /* audio */ 11503e230dd2SCorentin Chary static void audio_capture_notify(void *opaque, audcnotification_e cmd) 11513e230dd2SCorentin Chary { 11523e230dd2SCorentin Chary VncState *vs = opaque; 11533e230dd2SCorentin Chary 1154f31f9c10SGerd Hoffmann assert(vs->magic == VNC_MAGIC); 11553e230dd2SCorentin Chary switch (cmd) { 11563e230dd2SCorentin Chary case AUD_CNOTIFY_DISABLE: 1157bd023f95SCorentin Chary vnc_lock_output(vs); 11583e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 11593e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 11603e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_END); 1161bd023f95SCorentin Chary vnc_unlock_output(vs); 11623e230dd2SCorentin Chary vnc_flush(vs); 11633e230dd2SCorentin Chary break; 11643e230dd2SCorentin Chary 11653e230dd2SCorentin Chary case AUD_CNOTIFY_ENABLE: 1166bd023f95SCorentin Chary vnc_lock_output(vs); 11673e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 11683e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 11693e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_BEGIN); 1170bd023f95SCorentin Chary vnc_unlock_output(vs); 11713e230dd2SCorentin Chary vnc_flush(vs); 11723e230dd2SCorentin Chary break; 11733e230dd2SCorentin Chary } 11743e230dd2SCorentin Chary } 11753e230dd2SCorentin Chary 11763e230dd2SCorentin Chary static void audio_capture_destroy(void *opaque) 11773e230dd2SCorentin Chary { 11783e230dd2SCorentin Chary } 11793e230dd2SCorentin Chary 11803e230dd2SCorentin Chary static void audio_capture(void *opaque, void *buf, int size) 11813e230dd2SCorentin Chary { 11823e230dd2SCorentin Chary VncState *vs = opaque; 11833e230dd2SCorentin Chary 1184f31f9c10SGerd Hoffmann assert(vs->magic == VNC_MAGIC); 1185bd023f95SCorentin Chary vnc_lock_output(vs); 1186e2b72cb6SDaniel P. Berrange if (vs->output.offset < vs->throttle_output_offset) { 11873e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU); 11883e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_QEMU_AUDIO); 11893e230dd2SCorentin Chary vnc_write_u16(vs, VNC_MSG_SERVER_QEMU_AUDIO_DATA); 11903e230dd2SCorentin Chary vnc_write_u32(vs, size); 11913e230dd2SCorentin Chary vnc_write(vs, buf, size); 11926aa22a29SDaniel P. Berrange } else { 11936aa22a29SDaniel P. Berrange trace_vnc_client_throttle_audio(vs, vs->ioc, vs->output.offset); 1194e2b72cb6SDaniel P. Berrange } 1195bd023f95SCorentin Chary vnc_unlock_output(vs); 11963e230dd2SCorentin Chary vnc_flush(vs); 11973e230dd2SCorentin Chary } 11983e230dd2SCorentin Chary 11993e230dd2SCorentin Chary static void audio_add(VncState *vs) 12003e230dd2SCorentin Chary { 12013e230dd2SCorentin Chary struct audio_capture_ops ops; 12023e230dd2SCorentin Chary 12033e230dd2SCorentin Chary if (vs->audio_cap) { 1204027a79c3SCole Robinson error_report("audio already running"); 12053e230dd2SCorentin Chary return; 12063e230dd2SCorentin Chary } 12073e230dd2SCorentin Chary 12083e230dd2SCorentin Chary ops.notify = audio_capture_notify; 12093e230dd2SCorentin Chary ops.destroy = audio_capture_destroy; 12103e230dd2SCorentin Chary ops.capture = audio_capture; 12113e230dd2SCorentin Chary 1212f0b9f36dSKővágó, Zoltán vs->audio_cap = AUD_add_capture(vs->vd->audio_state, &vs->as, &ops, vs); 12133e230dd2SCorentin Chary if (!vs->audio_cap) { 1214027a79c3SCole Robinson error_report("Failed to add audio capture"); 12153e230dd2SCorentin Chary } 12163e230dd2SCorentin Chary } 12173e230dd2SCorentin Chary 12183e230dd2SCorentin Chary static void audio_del(VncState *vs) 12193e230dd2SCorentin Chary { 12203e230dd2SCorentin Chary if (vs->audio_cap) { 12213e230dd2SCorentin Chary AUD_del_capture(vs->audio_cap, vs); 12223e230dd2SCorentin Chary vs->audio_cap = NULL; 12233e230dd2SCorentin Chary } 12243e230dd2SCorentin Chary } 12253e230dd2SCorentin Chary 12263e230dd2SCorentin Chary static void vnc_disconnect_start(VncState *vs) 12273e230dd2SCorentin Chary { 122804d2529dSDaniel P. Berrange if (vs->disconnecting) { 12293e230dd2SCorentin Chary return; 123004d2529dSDaniel P. Berrange } 1231ad6374c4SDaniel P. Berrange trace_vnc_client_disconnect_start(vs, vs->ioc); 12328cf36489SGerd Hoffmann vnc_set_share_mode(vs, VNC_SHARE_MODE_DISCONNECTED); 123304d2529dSDaniel P. Berrange if (vs->ioc_tag) { 123404d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 1235a75d6f07SBrandon Carpenter vs->ioc_tag = 0; 123604d2529dSDaniel P. Berrange } 123704d2529dSDaniel P. Berrange qio_channel_close(vs->ioc, NULL); 123804d2529dSDaniel P. Berrange vs->disconnecting = TRUE; 12393e230dd2SCorentin Chary } 12403e230dd2SCorentin Chary 12417536ee4bSTim Hardeck void vnc_disconnect_finish(VncState *vs) 12423e230dd2SCorentin Chary { 12437d964c9dSCorentin Chary int i; 12447d964c9dSCorentin Chary 1245ad6374c4SDaniel P. Berrange trace_vnc_client_disconnect_finish(vs, vs->ioc); 1246ad6374c4SDaniel P. Berrange 1247bd023f95SCorentin Chary vnc_jobs_join(vs); /* Wait encoding jobs */ 1248bd023f95SCorentin Chary 1249bd023f95SCorentin Chary vnc_lock_output(vs); 1250fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_DISCONNECTED); 12513e230dd2SCorentin Chary 12523e230dd2SCorentin Chary buffer_free(&vs->input); 12533e230dd2SCorentin Chary buffer_free(&vs->output); 12543e230dd2SCorentin Chary 1255fb6ba0d5SWenchao Xia qapi_free_VncClientInfo(vs->info); 12563e230dd2SCorentin Chary 12573e230dd2SCorentin Chary vnc_zlib_clear(vs); 12583e230dd2SCorentin Chary vnc_tight_clear(vs); 1259148954faSCorentin Chary vnc_zrle_clear(vs); 12603e230dd2SCorentin Chary 12613e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 12623e230dd2SCorentin Chary vnc_sasl_client_cleanup(vs); 12633e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 12643e230dd2SCorentin Chary audio_del(vs); 1265c2f2ba49SGerd Hoffmann qkbd_state_lift_all_keys(vs->vd->kbd); 12663e230dd2SCorentin Chary 126790cd03a3SDaniel P. Berrange if (vs->mouse_mode_notifier.notify != NULL) { 12686fd8e79aSTim Hardeck qemu_remove_mouse_mode_change_notifier(&vs->mouse_mode_notifier); 126990cd03a3SDaniel P. Berrange } 127090cd03a3SDaniel P. Berrange QTAILQ_REMOVE(&vs->vd->clients, vs, next); 1271c7628bffSGerd Hoffmann if (QTAILQ_EMPTY(&vs->vd->clients)) { 1272c7628bffSGerd Hoffmann /* last client gone */ 1273c7628bffSGerd Hoffmann vnc_update_server_surface(vs->vd); 1274c7628bffSGerd Hoffmann } 12753e230dd2SCorentin Chary 1276bd023f95SCorentin Chary vnc_unlock_output(vs); 1277bd023f95SCorentin Chary 1278bd023f95SCorentin Chary qemu_mutex_destroy(&vs->output_mutex); 12796fd8e79aSTim Hardeck if (vs->bh != NULL) { 1280175b2a6eSCorentin Chary qemu_bh_delete(vs->bh); 12816fd8e79aSTim Hardeck } 1282175b2a6eSCorentin Chary buffer_free(&vs->jobs_buffer); 1283175b2a6eSCorentin Chary 12847d964c9dSCorentin Chary for (i = 0; i < VNC_STAT_ROWS; ++i) { 12857267c094SAnthony Liguori g_free(vs->lossy_rect[i]); 12867d964c9dSCorentin Chary } 12877267c094SAnthony Liguori g_free(vs->lossy_rect); 128804d2529dSDaniel P. Berrange 128904d2529dSDaniel P. Berrange object_unref(OBJECT(vs->ioc)); 129004d2529dSDaniel P. Berrange vs->ioc = NULL; 129104d2529dSDaniel P. Berrange object_unref(OBJECT(vs->sioc)); 129204d2529dSDaniel P. Berrange vs->sioc = NULL; 1293f31f9c10SGerd Hoffmann vs->magic = 0; 12946bf21f3dSLi Qiang g_free(vs->zrle); 12956bf21f3dSLi Qiang g_free(vs->tight); 12967267c094SAnthony Liguori g_free(vs); 12973e230dd2SCorentin Chary } 12983e230dd2SCorentin Chary 129934ab29c2SVladimir Sementsov-Ogievskiy size_t vnc_client_io_error(VncState *vs, ssize_t ret, Error *err) 13003e230dd2SCorentin Chary { 130104d2529dSDaniel P. Berrange if (ret <= 0) { 130204d2529dSDaniel P. Berrange if (ret == 0) { 1303ad6374c4SDaniel P. Berrange trace_vnc_client_eof(vs, vs->ioc); 1304537848eeSMichael Tokarev vnc_disconnect_start(vs); 130504d2529dSDaniel P. Berrange } else if (ret != QIO_CHANNEL_ERR_BLOCK) { 1306ad6374c4SDaniel P. Berrange trace_vnc_client_io_error(vs, vs->ioc, 130734ab29c2SVladimir Sementsov-Ogievskiy err ? error_get_pretty(err) : "Unknown"); 1308537848eeSMichael Tokarev vnc_disconnect_start(vs); 13093e230dd2SCorentin Chary } 13103e230dd2SCorentin Chary 131134ab29c2SVladimir Sementsov-Ogievskiy error_free(err); 13123e230dd2SCorentin Chary return 0; 13133e230dd2SCorentin Chary } 13143e230dd2SCorentin Chary return ret; 13153e230dd2SCorentin Chary } 13163e230dd2SCorentin Chary 13173e230dd2SCorentin Chary 13183e230dd2SCorentin Chary void vnc_client_error(VncState *vs) 13193e230dd2SCorentin Chary { 13203e230dd2SCorentin Chary VNC_DEBUG("Closing down client sock: protocol error\n"); 13213e230dd2SCorentin Chary vnc_disconnect_start(vs); 13223e230dd2SCorentin Chary } 13233e230dd2SCorentin Chary 13243e305e4aSDaniel P. Berrange 13253e230dd2SCorentin Chary /* 13263e230dd2SCorentin Chary * Called to write a chunk of data to the client socket. The data may 13273e230dd2SCorentin Chary * be the raw data, or may have already been encoded by SASL. 13283e230dd2SCorentin Chary * The data will be written either straight onto the socket, or 13293e230dd2SCorentin Chary * written via the GNUTLS wrappers, if TLS/SSL encryption is enabled 13303e230dd2SCorentin Chary * 13313e230dd2SCorentin Chary * NB, it is theoretically possible to have 2 layers of encryption, 13323e230dd2SCorentin Chary * both SASL, and this TLS layer. It is highly unlikely in practice 13333e230dd2SCorentin Chary * though, since SASL encryption will typically be a no-op if TLS 13343e230dd2SCorentin Chary * is active 13353e230dd2SCorentin Chary * 13363e230dd2SCorentin Chary * Returns the number of bytes written, which may be less than 13373e230dd2SCorentin Chary * the requested 'datalen' if the socket would block. Returns 133830b80fd5SDaniel P. Berrange * 0 on I/O error, and disconnects the client socket. 13393e230dd2SCorentin Chary */ 134030b80fd5SDaniel P. Berrange size_t vnc_client_write_buf(VncState *vs, const uint8_t *data, size_t datalen) 13413e230dd2SCorentin Chary { 134204d2529dSDaniel P. Berrange Error *err = NULL; 1343fdd1ab6aSDaniel P. Berrange ssize_t ret; 134434ab29c2SVladimir Sementsov-Ogievskiy ret = qio_channel_write(vs->ioc, (const char *)data, datalen, &err); 13453e230dd2SCorentin Chary VNC_DEBUG("Wrote wire %p %zd -> %ld\n", data, datalen, ret); 134634ab29c2SVladimir Sementsov-Ogievskiy return vnc_client_io_error(vs, ret, err); 13473e230dd2SCorentin Chary } 13483e230dd2SCorentin Chary 13493e230dd2SCorentin Chary 13503e230dd2SCorentin Chary /* 13513e230dd2SCorentin Chary * Called to write buffered data to the client socket, when not 13523e230dd2SCorentin Chary * using any SASL SSF encryption layers. Will write as much data 13533e230dd2SCorentin Chary * as possible without blocking. If all buffered data is written, 13543e230dd2SCorentin Chary * will switch the FD poll() handler back to read monitoring. 13553e230dd2SCorentin Chary * 13563e230dd2SCorentin Chary * Returns the number of bytes written, which may be less than 13573e230dd2SCorentin Chary * the buffered output data if the socket would block. Returns 135830b80fd5SDaniel P. Berrange * 0 on I/O error, and disconnects the client socket. 13593e230dd2SCorentin Chary */ 136030b80fd5SDaniel P. Berrange static size_t vnc_client_write_plain(VncState *vs) 13613e230dd2SCorentin Chary { 13626aa22a29SDaniel P. Berrange size_t offset; 136330b80fd5SDaniel P. Berrange size_t ret; 13643e230dd2SCorentin Chary 13653e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 13663e230dd2SCorentin Chary VNC_DEBUG("Write Plain: Pending output %p size %zd offset %zd. Wait SSF %d\n", 13673e230dd2SCorentin Chary vs->output.buffer, vs->output.capacity, vs->output.offset, 13683e230dd2SCorentin Chary vs->sasl.waitWriteSSF); 13693e230dd2SCorentin Chary 13703e230dd2SCorentin Chary if (vs->sasl.conn && 13713e230dd2SCorentin Chary vs->sasl.runSSF && 13723e230dd2SCorentin Chary vs->sasl.waitWriteSSF) { 13733e230dd2SCorentin Chary ret = vnc_client_write_buf(vs, vs->output.buffer, vs->sasl.waitWriteSSF); 13743e230dd2SCorentin Chary if (ret) 13753e230dd2SCorentin Chary vs->sasl.waitWriteSSF -= ret; 13763e230dd2SCorentin Chary } else 13773e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 13783e230dd2SCorentin Chary ret = vnc_client_write_buf(vs, vs->output.buffer, vs->output.offset); 13793e230dd2SCorentin Chary if (!ret) 13803e230dd2SCorentin Chary return 0; 13813e230dd2SCorentin Chary 1382ada8d2e4SDaniel P. Berrange if (ret >= vs->force_update_offset) { 13836aa22a29SDaniel P. Berrange if (vs->force_update_offset != 0) { 13846aa22a29SDaniel P. Berrange trace_vnc_client_unthrottle_forced(vs, vs->ioc); 13856aa22a29SDaniel P. Berrange } 1386ada8d2e4SDaniel P. Berrange vs->force_update_offset = 0; 1387ada8d2e4SDaniel P. Berrange } else { 1388ada8d2e4SDaniel P. Berrange vs->force_update_offset -= ret; 1389ada8d2e4SDaniel P. Berrange } 13906aa22a29SDaniel P. Berrange offset = vs->output.offset; 139132ed2680STim Hardeck buffer_advance(&vs->output, ret); 13926aa22a29SDaniel P. Berrange if (offset >= vs->throttle_output_offset && 13936aa22a29SDaniel P. Berrange vs->output.offset < vs->throttle_output_offset) { 13946aa22a29SDaniel P. Berrange trace_vnc_client_unthrottle_incremental(vs, vs->ioc, vs->output.offset); 13956aa22a29SDaniel P. Berrange } 13963e230dd2SCorentin Chary 13973e230dd2SCorentin Chary if (vs->output.offset == 0) { 139804d2529dSDaniel P. Berrange if (vs->ioc_tag) { 139904d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 140004d2529dSDaniel P. Berrange } 140104d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 140204d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vnc_client_io, vs, NULL); 14033e230dd2SCorentin Chary } 14043e230dd2SCorentin Chary 14053e230dd2SCorentin Chary return ret; 14063e230dd2SCorentin Chary } 14073e230dd2SCorentin Chary 14083e230dd2SCorentin Chary 14093e230dd2SCorentin Chary /* 14103e230dd2SCorentin Chary * First function called whenever there is data to be written to 14113e230dd2SCorentin Chary * the client socket. Will delegate actual work according to whether 14123e230dd2SCorentin Chary * SASL SSF layers are enabled (thus requiring encryption calls) 14133e230dd2SCorentin Chary */ 141404d2529dSDaniel P. Berrange static void vnc_client_write_locked(VncState *vs) 14153e230dd2SCorentin Chary { 14163e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 14173e230dd2SCorentin Chary if (vs->sasl.conn && 14183e230dd2SCorentin Chary vs->sasl.runSSF && 14193e230dd2SCorentin Chary !vs->sasl.waitWriteSSF) { 14203e230dd2SCorentin Chary vnc_client_write_sasl(vs); 14213e230dd2SCorentin Chary } else 14223e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 14237536ee4bSTim Hardeck { 14243e230dd2SCorentin Chary vnc_client_write_plain(vs); 14253e230dd2SCorentin Chary } 14267536ee4bSTim Hardeck } 14273e230dd2SCorentin Chary 142804d2529dSDaniel P. Berrange static void vnc_client_write(VncState *vs) 1429bd023f95SCorentin Chary { 1430f31f9c10SGerd Hoffmann assert(vs->magic == VNC_MAGIC); 1431bd023f95SCorentin Chary vnc_lock_output(vs); 1432d5f04223SDaniel P. Berrange if (vs->output.offset) { 143304d2529dSDaniel P. Berrange vnc_client_write_locked(vs); 143404d2529dSDaniel P. Berrange } else if (vs->ioc != NULL) { 143504d2529dSDaniel P. Berrange if (vs->ioc_tag) { 143604d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 143704d2529dSDaniel P. Berrange } 143804d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 143904d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vnc_client_io, vs, NULL); 1440bd023f95SCorentin Chary } 1441bd023f95SCorentin Chary vnc_unlock_output(vs); 1442bd023f95SCorentin Chary } 1443bd023f95SCorentin Chary 14443e230dd2SCorentin Chary void vnc_read_when(VncState *vs, VncReadEvent *func, size_t expecting) 14453e230dd2SCorentin Chary { 14463e230dd2SCorentin Chary vs->read_handler = func; 14473e230dd2SCorentin Chary vs->read_handler_expect = expecting; 14483e230dd2SCorentin Chary } 14493e230dd2SCorentin Chary 14503e230dd2SCorentin Chary 14513e230dd2SCorentin Chary /* 14523e230dd2SCorentin Chary * Called to read a chunk of data from the client socket. The data may 14533e230dd2SCorentin Chary * be the raw data, or may need to be further decoded by SASL. 14543e230dd2SCorentin Chary * The data will be read either straight from to the socket, or 14553e230dd2SCorentin Chary * read via the GNUTLS wrappers, if TLS/SSL encryption is enabled 14563e230dd2SCorentin Chary * 14573e230dd2SCorentin Chary * NB, it is theoretically possible to have 2 layers of encryption, 14583e230dd2SCorentin Chary * both SASL, and this TLS layer. It is highly unlikely in practice 14593e230dd2SCorentin Chary * though, since SASL encryption will typically be a no-op if TLS 14603e230dd2SCorentin Chary * is active 14613e230dd2SCorentin Chary * 14623e230dd2SCorentin Chary * Returns the number of bytes read, which may be less than 14633e230dd2SCorentin Chary * the requested 'datalen' if the socket would block. Returns 146430b80fd5SDaniel P. Berrange * 0 on I/O error or EOF, and disconnects the client socket. 14653e230dd2SCorentin Chary */ 146630b80fd5SDaniel P. Berrange size_t vnc_client_read_buf(VncState *vs, uint8_t *data, size_t datalen) 14673e230dd2SCorentin Chary { 1468fdd1ab6aSDaniel P. Berrange ssize_t ret; 146904d2529dSDaniel P. Berrange Error *err = NULL; 147034ab29c2SVladimir Sementsov-Ogievskiy ret = qio_channel_read(vs->ioc, (char *)data, datalen, &err); 14713e230dd2SCorentin Chary VNC_DEBUG("Read wire %p %zd -> %ld\n", data, datalen, ret); 147234ab29c2SVladimir Sementsov-Ogievskiy return vnc_client_io_error(vs, ret, err); 14733e230dd2SCorentin Chary } 14743e230dd2SCorentin Chary 14753e230dd2SCorentin Chary 14763e230dd2SCorentin Chary /* 14773e230dd2SCorentin Chary * Called to read data from the client socket to the input buffer, 14783e230dd2SCorentin Chary * when not using any SASL SSF encryption layers. Will read as much 14793e230dd2SCorentin Chary * data as possible without blocking. 14803e230dd2SCorentin Chary * 148130b80fd5SDaniel P. Berrange * Returns the number of bytes read, which may be less than 148230b80fd5SDaniel P. Berrange * the requested 'datalen' if the socket would block. Returns 148330b80fd5SDaniel P. Berrange * 0 on I/O error or EOF, and disconnects the client socket. 14843e230dd2SCorentin Chary */ 148530b80fd5SDaniel P. Berrange static size_t vnc_client_read_plain(VncState *vs) 14863e230dd2SCorentin Chary { 148730b80fd5SDaniel P. Berrange size_t ret; 14883e230dd2SCorentin Chary VNC_DEBUG("Read plain %p size %zd offset %zd\n", 14893e230dd2SCorentin Chary vs->input.buffer, vs->input.capacity, vs->input.offset); 14903e230dd2SCorentin Chary buffer_reserve(&vs->input, 4096); 14913e230dd2SCorentin Chary ret = vnc_client_read_buf(vs, buffer_end(&vs->input), 4096); 14923e230dd2SCorentin Chary if (!ret) 14933e230dd2SCorentin Chary return 0; 14943e230dd2SCorentin Chary vs->input.offset += ret; 14953e230dd2SCorentin Chary return ret; 14963e230dd2SCorentin Chary } 14973e230dd2SCorentin Chary 1498175b2a6eSCorentin Chary static void vnc_jobs_bh(void *opaque) 1499175b2a6eSCorentin Chary { 1500175b2a6eSCorentin Chary VncState *vs = opaque; 1501175b2a6eSCorentin Chary 1502f31f9c10SGerd Hoffmann assert(vs->magic == VNC_MAGIC); 1503175b2a6eSCorentin Chary vnc_jobs_consume_buffer(vs); 1504175b2a6eSCorentin Chary } 15053e230dd2SCorentin Chary 15063e230dd2SCorentin Chary /* 15073e230dd2SCorentin Chary * First function called whenever there is more data to be read from 15083e230dd2SCorentin Chary * the client socket. Will delegate actual work according to whether 15093e230dd2SCorentin Chary * SASL SSF layers are enabled (thus requiring decryption calls) 1510ea697449SDaniel P. Berrange * Returns 0 on success, -1 if client disconnected 15113e230dd2SCorentin Chary */ 1512ea697449SDaniel P. Berrange static int vnc_client_read(VncState *vs) 15133e230dd2SCorentin Chary { 151430b80fd5SDaniel P. Berrange size_t ret; 15153e230dd2SCorentin Chary 15163e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 15173e230dd2SCorentin Chary if (vs->sasl.conn && vs->sasl.runSSF) 15183e230dd2SCorentin Chary ret = vnc_client_read_sasl(vs); 15193e230dd2SCorentin Chary else 15203e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 15213e230dd2SCorentin Chary ret = vnc_client_read_plain(vs); 15223e230dd2SCorentin Chary if (!ret) { 152304d2529dSDaniel P. Berrange if (vs->disconnecting) { 15243e230dd2SCorentin Chary vnc_disconnect_finish(vs); 1525ea697449SDaniel P. Berrange return -1; 152604d2529dSDaniel P. Berrange } 1527ea697449SDaniel P. Berrange return 0; 15283e230dd2SCorentin Chary } 15293e230dd2SCorentin Chary 15303e230dd2SCorentin Chary while (vs->read_handler && vs->input.offset >= vs->read_handler_expect) { 15313e230dd2SCorentin Chary size_t len = vs->read_handler_expect; 15323e230dd2SCorentin Chary int ret; 15333e230dd2SCorentin Chary 15343e230dd2SCorentin Chary ret = vs->read_handler(vs, vs->input.buffer, len); 153504d2529dSDaniel P. Berrange if (vs->disconnecting) { 15363e230dd2SCorentin Chary vnc_disconnect_finish(vs); 1537ea697449SDaniel P. Berrange return -1; 15383e230dd2SCorentin Chary } 15393e230dd2SCorentin Chary 15403e230dd2SCorentin Chary if (!ret) { 154132ed2680STim Hardeck buffer_advance(&vs->input, len); 15423e230dd2SCorentin Chary } else { 15433e230dd2SCorentin Chary vs->read_handler_expect = ret; 15443e230dd2SCorentin Chary } 15453e230dd2SCorentin Chary } 1546ea697449SDaniel P. Berrange return 0; 15473e230dd2SCorentin Chary } 15483e230dd2SCorentin Chary 154904d2529dSDaniel P. Berrange gboolean vnc_client_io(QIOChannel *ioc G_GNUC_UNUSED, 155004d2529dSDaniel P. Berrange GIOCondition condition, void *opaque) 155104d2529dSDaniel P. Berrange { 155204d2529dSDaniel P. Berrange VncState *vs = opaque; 1553f31f9c10SGerd Hoffmann 1554f31f9c10SGerd Hoffmann assert(vs->magic == VNC_MAGIC); 155504d2529dSDaniel P. Berrange if (condition & G_IO_IN) { 1556ea697449SDaniel P. Berrange if (vnc_client_read(vs) < 0) { 15571bc3117aSGerd Hoffmann /* vs is free()ed here */ 15581bc3117aSGerd Hoffmann return TRUE; 1559ea697449SDaniel P. Berrange } 156004d2529dSDaniel P. Berrange } 156104d2529dSDaniel P. Berrange if (condition & G_IO_OUT) { 156204d2529dSDaniel P. Berrange vnc_client_write(vs); 156304d2529dSDaniel P. Berrange } 15641bc3117aSGerd Hoffmann 1565d49b87f0SKlim Kireev if (vs->disconnecting) { 1566d49b87f0SKlim Kireev if (vs->ioc_tag != 0) { 1567d49b87f0SKlim Kireev g_source_remove(vs->ioc_tag); 1568d49b87f0SKlim Kireev } 1569d49b87f0SKlim Kireev vs->ioc_tag = 0; 1570d49b87f0SKlim Kireev } 157104d2529dSDaniel P. Berrange return TRUE; 157204d2529dSDaniel P. Berrange } 157304d2529dSDaniel P. Berrange 157404d2529dSDaniel P. Berrange 1575f887cf16SDaniel P. Berrange /* 1576f887cf16SDaniel P. Berrange * Scale factor to apply to vs->throttle_output_offset when checking for 1577f887cf16SDaniel P. Berrange * hard limit. Worst case normal usage could be x2, if we have a complete 1578f887cf16SDaniel P. Berrange * incremental update and complete forced update in the output buffer. 1579f887cf16SDaniel P. Berrange * So x3 should be good enough, but we pick x5 to be conservative and thus 1580f887cf16SDaniel P. Berrange * (hopefully) never trigger incorrectly. 1581f887cf16SDaniel P. Berrange */ 1582f887cf16SDaniel P. Berrange #define VNC_THROTTLE_OUTPUT_LIMIT_SCALE 5 1583f887cf16SDaniel P. Berrange 15843e230dd2SCorentin Chary void vnc_write(VncState *vs, const void *data, size_t len) 15853e230dd2SCorentin Chary { 1586f31f9c10SGerd Hoffmann assert(vs->magic == VNC_MAGIC); 1587f887cf16SDaniel P. Berrange if (vs->disconnecting) { 1588f887cf16SDaniel P. Berrange return; 1589f887cf16SDaniel P. Berrange } 1590f887cf16SDaniel P. Berrange /* Protection against malicious client/guest to prevent our output 1591f887cf16SDaniel P. Berrange * buffer growing without bound if client stops reading data. This 1592f887cf16SDaniel P. Berrange * should rarely trigger, because we have earlier throttling code 1593f887cf16SDaniel P. Berrange * which stops issuing framebuffer updates and drops audio data 1594f887cf16SDaniel P. Berrange * if the throttle_output_offset value is exceeded. So we only reach 1595f887cf16SDaniel P. Berrange * this higher level if a huge number of pseudo-encodings get 1596f887cf16SDaniel P. Berrange * triggered while data can't be sent on the socket. 1597f887cf16SDaniel P. Berrange * 1598f887cf16SDaniel P. Berrange * NB throttle_output_offset can be zero during early protocol 1599f887cf16SDaniel P. Berrange * handshake, or from the job thread's VncState clone 1600f887cf16SDaniel P. Berrange */ 1601f887cf16SDaniel P. Berrange if (vs->throttle_output_offset != 0 && 1602dffa1de0SDaniel P. Berrangé (vs->output.offset / VNC_THROTTLE_OUTPUT_LIMIT_SCALE) > 1603dffa1de0SDaniel P. Berrangé vs->throttle_output_offset) { 16046aa22a29SDaniel P. Berrange trace_vnc_client_output_limit(vs, vs->ioc, vs->output.offset, 16056aa22a29SDaniel P. Berrange vs->throttle_output_offset); 1606f887cf16SDaniel P. Berrange vnc_disconnect_start(vs); 1607f887cf16SDaniel P. Berrange return; 1608f887cf16SDaniel P. Berrange } 16093e230dd2SCorentin Chary buffer_reserve(&vs->output, len); 16103e230dd2SCorentin Chary 161104d2529dSDaniel P. Berrange if (vs->ioc != NULL && buffer_empty(&vs->output)) { 161204d2529dSDaniel P. Berrange if (vs->ioc_tag) { 161304d2529dSDaniel P. Berrange g_source_remove(vs->ioc_tag); 161404d2529dSDaniel P. Berrange } 161504d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 161604d2529dSDaniel P. Berrange vs->ioc, G_IO_IN | G_IO_OUT, vnc_client_io, vs, NULL); 16173e230dd2SCorentin Chary } 16183e230dd2SCorentin Chary 16193e230dd2SCorentin Chary buffer_append(&vs->output, data, len); 16203e230dd2SCorentin Chary } 16213e230dd2SCorentin Chary 16223e230dd2SCorentin Chary void vnc_write_s32(VncState *vs, int32_t value) 16233e230dd2SCorentin Chary { 16243e230dd2SCorentin Chary vnc_write_u32(vs, *(uint32_t *)&value); 16253e230dd2SCorentin Chary } 16263e230dd2SCorentin Chary 16273e230dd2SCorentin Chary void vnc_write_u32(VncState *vs, uint32_t value) 16283e230dd2SCorentin Chary { 16293e230dd2SCorentin Chary uint8_t buf[4]; 16303e230dd2SCorentin Chary 16313e230dd2SCorentin Chary buf[0] = (value >> 24) & 0xFF; 16323e230dd2SCorentin Chary buf[1] = (value >> 16) & 0xFF; 16333e230dd2SCorentin Chary buf[2] = (value >> 8) & 0xFF; 16343e230dd2SCorentin Chary buf[3] = value & 0xFF; 16353e230dd2SCorentin Chary 16363e230dd2SCorentin Chary vnc_write(vs, buf, 4); 16373e230dd2SCorentin Chary } 16383e230dd2SCorentin Chary 16393e230dd2SCorentin Chary void vnc_write_u16(VncState *vs, uint16_t value) 16403e230dd2SCorentin Chary { 16413e230dd2SCorentin Chary uint8_t buf[2]; 16423e230dd2SCorentin Chary 16433e230dd2SCorentin Chary buf[0] = (value >> 8) & 0xFF; 16443e230dd2SCorentin Chary buf[1] = value & 0xFF; 16453e230dd2SCorentin Chary 16463e230dd2SCorentin Chary vnc_write(vs, buf, 2); 16473e230dd2SCorentin Chary } 16483e230dd2SCorentin Chary 16493e230dd2SCorentin Chary void vnc_write_u8(VncState *vs, uint8_t value) 16503e230dd2SCorentin Chary { 16513e230dd2SCorentin Chary vnc_write(vs, (char *)&value, 1); 16523e230dd2SCorentin Chary } 16533e230dd2SCorentin Chary 16543e230dd2SCorentin Chary void vnc_flush(VncState *vs) 16553e230dd2SCorentin Chary { 1656bd023f95SCorentin Chary vnc_lock_output(vs); 1657d5f04223SDaniel P. Berrange if (vs->ioc != NULL && vs->output.offset) { 1658bd023f95SCorentin Chary vnc_client_write_locked(vs); 1659bd023f95SCorentin Chary } 1660d49b87f0SKlim Kireev if (vs->disconnecting) { 1661d49b87f0SKlim Kireev if (vs->ioc_tag != 0) { 1662d49b87f0SKlim Kireev g_source_remove(vs->ioc_tag); 1663d49b87f0SKlim Kireev } 1664d49b87f0SKlim Kireev vs->ioc_tag = 0; 1665d49b87f0SKlim Kireev } 1666bd023f95SCorentin Chary vnc_unlock_output(vs); 16673e230dd2SCorentin Chary } 16683e230dd2SCorentin Chary 166971a8cdecSBlue Swirl static uint8_t read_u8(uint8_t *data, size_t offset) 16703e230dd2SCorentin Chary { 16713e230dd2SCorentin Chary return data[offset]; 16723e230dd2SCorentin Chary } 16733e230dd2SCorentin Chary 167471a8cdecSBlue Swirl static uint16_t read_u16(uint8_t *data, size_t offset) 16753e230dd2SCorentin Chary { 16763e230dd2SCorentin Chary return ((data[offset] & 0xFF) << 8) | (data[offset + 1] & 0xFF); 16773e230dd2SCorentin Chary } 16783e230dd2SCorentin Chary 167971a8cdecSBlue Swirl static int32_t read_s32(uint8_t *data, size_t offset) 16803e230dd2SCorentin Chary { 16813e230dd2SCorentin Chary return (int32_t)((data[offset] << 24) | (data[offset + 1] << 16) | 16823e230dd2SCorentin Chary (data[offset + 2] << 8) | data[offset + 3]); 16833e230dd2SCorentin Chary } 16843e230dd2SCorentin Chary 16853e230dd2SCorentin Chary uint32_t read_u32(uint8_t *data, size_t offset) 16863e230dd2SCorentin Chary { 16873e230dd2SCorentin Chary return ((data[offset] << 24) | (data[offset + 1] << 16) | 16883e230dd2SCorentin Chary (data[offset + 2] << 8) | data[offset + 3]); 16893e230dd2SCorentin Chary } 16903e230dd2SCorentin Chary 16913e230dd2SCorentin Chary static void client_cut_text(VncState *vs, size_t len, uint8_t *text) 16923e230dd2SCorentin Chary { 16933e230dd2SCorentin Chary } 16943e230dd2SCorentin Chary 16959e8dd451SJan Kiszka static void check_pointer_type_change(Notifier *notifier, void *data) 16963e230dd2SCorentin Chary { 16973e230dd2SCorentin Chary VncState *vs = container_of(notifier, VncState, mouse_mode_notifier); 169814768ebaSGerd Hoffmann int absolute = qemu_input_is_absolute(); 16993e230dd2SCorentin Chary 17003e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE) && vs->absolute != absolute) { 1701bd023f95SCorentin Chary vnc_lock_output(vs); 17023e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 17033e230dd2SCorentin Chary vnc_write_u8(vs, 0); 17043e230dd2SCorentin Chary vnc_write_u16(vs, 1); 17053e230dd2SCorentin Chary vnc_framebuffer_update(vs, absolute, 0, 1706bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 1707bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 17083e230dd2SCorentin Chary VNC_ENCODING_POINTER_TYPE_CHANGE); 1709bd023f95SCorentin Chary vnc_unlock_output(vs); 17103e230dd2SCorentin Chary vnc_flush(vs); 17113e230dd2SCorentin Chary } 17123e230dd2SCorentin Chary vs->absolute = absolute; 17133e230dd2SCorentin Chary } 17143e230dd2SCorentin Chary 17153e230dd2SCorentin Chary static void pointer_event(VncState *vs, int button_mask, int x, int y) 17163e230dd2SCorentin Chary { 17177fb1cf16SEric Blake static uint32_t bmap[INPUT_BUTTON__MAX] = { 171814768ebaSGerd Hoffmann [INPUT_BUTTON_LEFT] = 0x01, 171914768ebaSGerd Hoffmann [INPUT_BUTTON_MIDDLE] = 0x02, 172014768ebaSGerd Hoffmann [INPUT_BUTTON_RIGHT] = 0x04, 1721f22d0af0SGerd Hoffmann [INPUT_BUTTON_WHEEL_UP] = 0x08, 1722f22d0af0SGerd Hoffmann [INPUT_BUTTON_WHEEL_DOWN] = 0x10, 172314768ebaSGerd Hoffmann }; 172414768ebaSGerd Hoffmann QemuConsole *con = vs->vd->dcl.con; 1725bea60dd7SPeter Lieven int width = pixman_image_get_width(vs->vd->server); 1726bea60dd7SPeter Lieven int height = pixman_image_get_height(vs->vd->server); 17273e230dd2SCorentin Chary 172814768ebaSGerd Hoffmann if (vs->last_bmask != button_mask) { 172914768ebaSGerd Hoffmann qemu_input_update_buttons(con, bmap, vs->last_bmask, button_mask); 173014768ebaSGerd Hoffmann vs->last_bmask = button_mask; 173114768ebaSGerd Hoffmann } 17323e230dd2SCorentin Chary 17333e230dd2SCorentin Chary if (vs->absolute) { 17349cfa7ab9SPhilippe Voinov qemu_input_queue_abs(con, INPUT_AXIS_X, x, 0, width); 17359cfa7ab9SPhilippe Voinov qemu_input_queue_abs(con, INPUT_AXIS_Y, y, 0, height); 17363e230dd2SCorentin Chary } else if (vnc_has_feature(vs, VNC_FEATURE_POINTER_TYPE_CHANGE)) { 173714768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_X, x - 0x7FFF); 173814768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_Y, y - 0x7FFF); 17393e230dd2SCorentin Chary } else { 174014768ebaSGerd Hoffmann if (vs->last_x != -1) { 174114768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_X, x - vs->last_x); 174214768ebaSGerd Hoffmann qemu_input_queue_rel(con, INPUT_AXIS_Y, y - vs->last_y); 174314768ebaSGerd Hoffmann } 17443e230dd2SCorentin Chary vs->last_x = x; 17453e230dd2SCorentin Chary vs->last_y = y; 17463e230dd2SCorentin Chary } 174714768ebaSGerd Hoffmann qemu_input_event_sync(); 17483e230dd2SCorentin Chary } 17493e230dd2SCorentin Chary 1750c2f2ba49SGerd Hoffmann static void press_key(VncState *vs, QKeyCode qcode) 17513e230dd2SCorentin Chary { 1752c2f2ba49SGerd Hoffmann qkbd_state_key_event(vs->vd->kbd, qcode, true); 1753c2f2ba49SGerd Hoffmann qkbd_state_key_event(vs->vd->kbd, qcode, false); 17543e230dd2SCorentin Chary } 17553e230dd2SCorentin Chary 1756ab99e5c1SLei Li static void vnc_led_state_change(VncState *vs) 1757ab99e5c1SLei Li { 1758ab99e5c1SLei Li if (!vnc_has_feature(vs, VNC_FEATURE_LED_STATE)) { 1759ab99e5c1SLei Li return; 1760ab99e5c1SLei Li } 1761ab99e5c1SLei Li 1762ab99e5c1SLei Li vnc_lock_output(vs); 1763ab99e5c1SLei Li vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 1764ab99e5c1SLei Li vnc_write_u8(vs, 0); 1765ab99e5c1SLei Li vnc_write_u16(vs, 1); 1766ab99e5c1SLei Li vnc_framebuffer_update(vs, 0, 0, 1, 1, VNC_ENCODING_LED_STATE); 1767a54f0d2bSPierre Ossman vnc_write_u8(vs, vs->vd->ledstate); 1768ab99e5c1SLei Li vnc_unlock_output(vs); 1769ab99e5c1SLei Li vnc_flush(vs); 1770ab99e5c1SLei Li } 1771ab99e5c1SLei Li 17723e230dd2SCorentin Chary static void kbd_leds(void *opaque, int ledstate) 17733e230dd2SCorentin Chary { 1774a54f0d2bSPierre Ossman VncDisplay *vd = opaque; 1775a54f0d2bSPierre Ossman VncState *client; 17763e230dd2SCorentin Chary 177740066175SGerd Hoffmann trace_vnc_key_guest_leds((ledstate & QEMU_CAPS_LOCK_LED), 177840066175SGerd Hoffmann (ledstate & QEMU_NUM_LOCK_LED), 177940066175SGerd Hoffmann (ledstate & QEMU_SCROLL_LOCK_LED)); 178040066175SGerd Hoffmann 1781a54f0d2bSPierre Ossman if (ledstate == vd->ledstate) { 1782a54f0d2bSPierre Ossman return; 178396f3d174SLei Li } 1784ab99e5c1SLei Li 1785a54f0d2bSPierre Ossman vd->ledstate = ledstate; 1786a54f0d2bSPierre Ossman 1787a54f0d2bSPierre Ossman QTAILQ_FOREACH(client, &vd->clients, next) { 1788a54f0d2bSPierre Ossman vnc_led_state_change(client); 1789ab99e5c1SLei Li } 17903e230dd2SCorentin Chary } 17913e230dd2SCorentin Chary 17923e230dd2SCorentin Chary static void do_key_event(VncState *vs, int down, int keycode, int sym) 17933e230dd2SCorentin Chary { 1794c2f2ba49SGerd Hoffmann QKeyCode qcode = qemu_input_key_number_to_qcode(keycode); 1795c2f2ba49SGerd Hoffmann 17963e230dd2SCorentin Chary /* QEMU console switch */ 1797c2f2ba49SGerd Hoffmann switch (qcode) { 1798c2f2ba49SGerd Hoffmann case Q_KEY_CODE_1 ... Q_KEY_CODE_9: /* '1' to '9' keys */ 1799c2f2ba49SGerd Hoffmann if (vs->vd->dcl.con == NULL && down && 1800c2f2ba49SGerd Hoffmann qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_CTRL) && 1801c2f2ba49SGerd Hoffmann qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_ALT)) { 18023e230dd2SCorentin Chary /* Reset the modifiers sent to the current console */ 1803c2f2ba49SGerd Hoffmann qkbd_state_lift_all_keys(vs->vd->kbd); 1804c2f2ba49SGerd Hoffmann console_select(qcode - Q_KEY_CODE_1); 18053e230dd2SCorentin Chary return; 18063e230dd2SCorentin Chary } 1807c2f2ba49SGerd Hoffmann default: 18083e230dd2SCorentin Chary break; 18093e230dd2SCorentin Chary } 18103e230dd2SCorentin Chary 1811e7b2aaccSLei Li /* Turn off the lock state sync logic if the client support the led 1812e7b2aaccSLei Li state extension. 1813e7b2aaccSLei Li */ 18149892088bSGerd Hoffmann if (down && vs->vd->lock_key_sync && 1815e7b2aaccSLei Li !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) && 18163e230dd2SCorentin Chary keycode_is_keypad(vs->vd->kbd_layout, keycode)) { 18173e230dd2SCorentin Chary /* If the numlock state needs to change then simulate an additional 18183e230dd2SCorentin Chary keypress before sending this one. This will happen if the user 18193e230dd2SCorentin Chary toggles numlock away from the VNC window. 18203e230dd2SCorentin Chary */ 18213e230dd2SCorentin Chary if (keysym_is_numlock(vs->vd->kbd_layout, sym & 0xFFFF)) { 1822c2f2ba49SGerd Hoffmann if (!qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_NUMLOCK)) { 182340066175SGerd Hoffmann trace_vnc_key_sync_numlock(true); 1824c2f2ba49SGerd Hoffmann press_key(vs, Q_KEY_CODE_NUM_LOCK); 18253e230dd2SCorentin Chary } 18263e230dd2SCorentin Chary } else { 1827c2f2ba49SGerd Hoffmann if (qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_NUMLOCK)) { 182840066175SGerd Hoffmann trace_vnc_key_sync_numlock(false); 1829c2f2ba49SGerd Hoffmann press_key(vs, Q_KEY_CODE_NUM_LOCK); 18303e230dd2SCorentin Chary } 18313e230dd2SCorentin Chary } 18323e230dd2SCorentin Chary } 18333e230dd2SCorentin Chary 18349892088bSGerd Hoffmann if (down && vs->vd->lock_key_sync && 1835e7b2aaccSLei Li !vnc_has_feature(vs, VNC_FEATURE_LED_STATE) && 18363e230dd2SCorentin Chary ((sym >= 'A' && sym <= 'Z') || (sym >= 'a' && sym <= 'z'))) { 18373e230dd2SCorentin Chary /* If the capslock state needs to change then simulate an additional 18383e230dd2SCorentin Chary keypress before sending this one. This will happen if the user 18393e230dd2SCorentin Chary toggles capslock away from the VNC window. 18403e230dd2SCorentin Chary */ 18413e230dd2SCorentin Chary int uppercase = !!(sym >= 'A' && sym <= 'Z'); 1842c2f2ba49SGerd Hoffmann bool shift = qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_SHIFT); 1843c2f2ba49SGerd Hoffmann bool capslock = qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_CAPSLOCK); 18443e230dd2SCorentin Chary if (capslock) { 18453e230dd2SCorentin Chary if (uppercase == shift) { 184640066175SGerd Hoffmann trace_vnc_key_sync_capslock(false); 1847c2f2ba49SGerd Hoffmann press_key(vs, Q_KEY_CODE_CAPS_LOCK); 18483e230dd2SCorentin Chary } 18493e230dd2SCorentin Chary } else { 18503e230dd2SCorentin Chary if (uppercase != shift) { 185140066175SGerd Hoffmann trace_vnc_key_sync_capslock(true); 1852c2f2ba49SGerd Hoffmann press_key(vs, Q_KEY_CODE_CAPS_LOCK); 18533e230dd2SCorentin Chary } 18543e230dd2SCorentin Chary } 18553e230dd2SCorentin Chary } 18563e230dd2SCorentin Chary 1857c2f2ba49SGerd Hoffmann qkbd_state_key_event(vs->vd->kbd, qcode, down); 1858c2f2ba49SGerd Hoffmann if (!qemu_console_is_graphic(NULL)) { 1859c2f2ba49SGerd Hoffmann bool numlock = qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_NUMLOCK); 1860c2f2ba49SGerd Hoffmann bool control = qkbd_state_modifier_get(vs->vd->kbd, QKBD_MOD_CTRL); 18613e230dd2SCorentin Chary /* QEMU console emulation */ 18623e230dd2SCorentin Chary if (down) { 18633e230dd2SCorentin Chary switch (keycode) { 18643e230dd2SCorentin Chary case 0x2a: /* Left Shift */ 18653e230dd2SCorentin Chary case 0x36: /* Right Shift */ 18663e230dd2SCorentin Chary case 0x1d: /* Left CTRL */ 18673e230dd2SCorentin Chary case 0x9d: /* Right CTRL */ 18683e230dd2SCorentin Chary case 0x38: /* Left ALT */ 18693e230dd2SCorentin Chary case 0xb8: /* Right ALT */ 18703e230dd2SCorentin Chary break; 18713e230dd2SCorentin Chary case 0xc8: 18723e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_UP); 18733e230dd2SCorentin Chary break; 18743e230dd2SCorentin Chary case 0xd0: 18753e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_DOWN); 18763e230dd2SCorentin Chary break; 18773e230dd2SCorentin Chary case 0xcb: 18783e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_LEFT); 18793e230dd2SCorentin Chary break; 18803e230dd2SCorentin Chary case 0xcd: 18813e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_RIGHT); 18823e230dd2SCorentin Chary break; 18833e230dd2SCorentin Chary case 0xd3: 18843e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_DELETE); 18853e230dd2SCorentin Chary break; 18863e230dd2SCorentin Chary case 0xc7: 18873e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_HOME); 18883e230dd2SCorentin Chary break; 18893e230dd2SCorentin Chary case 0xcf: 18903e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_END); 18913e230dd2SCorentin Chary break; 18923e230dd2SCorentin Chary case 0xc9: 18933e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_PAGEUP); 18943e230dd2SCorentin Chary break; 18953e230dd2SCorentin Chary case 0xd1: 18963e230dd2SCorentin Chary kbd_put_keysym(QEMU_KEY_PAGEDOWN); 18973e230dd2SCorentin Chary break; 18983e230dd2SCorentin Chary 18993e230dd2SCorentin Chary case 0x47: 19003e230dd2SCorentin Chary kbd_put_keysym(numlock ? '7' : QEMU_KEY_HOME); 19013e230dd2SCorentin Chary break; 19023e230dd2SCorentin Chary case 0x48: 19033e230dd2SCorentin Chary kbd_put_keysym(numlock ? '8' : QEMU_KEY_UP); 19043e230dd2SCorentin Chary break; 19053e230dd2SCorentin Chary case 0x49: 19063e230dd2SCorentin Chary kbd_put_keysym(numlock ? '9' : QEMU_KEY_PAGEUP); 19073e230dd2SCorentin Chary break; 19083e230dd2SCorentin Chary case 0x4b: 19093e230dd2SCorentin Chary kbd_put_keysym(numlock ? '4' : QEMU_KEY_LEFT); 19103e230dd2SCorentin Chary break; 19113e230dd2SCorentin Chary case 0x4c: 19123e230dd2SCorentin Chary kbd_put_keysym('5'); 19133e230dd2SCorentin Chary break; 19143e230dd2SCorentin Chary case 0x4d: 19153e230dd2SCorentin Chary kbd_put_keysym(numlock ? '6' : QEMU_KEY_RIGHT); 19163e230dd2SCorentin Chary break; 19173e230dd2SCorentin Chary case 0x4f: 19183e230dd2SCorentin Chary kbd_put_keysym(numlock ? '1' : QEMU_KEY_END); 19193e230dd2SCorentin Chary break; 19203e230dd2SCorentin Chary case 0x50: 19213e230dd2SCorentin Chary kbd_put_keysym(numlock ? '2' : QEMU_KEY_DOWN); 19223e230dd2SCorentin Chary break; 19233e230dd2SCorentin Chary case 0x51: 19243e230dd2SCorentin Chary kbd_put_keysym(numlock ? '3' : QEMU_KEY_PAGEDOWN); 19253e230dd2SCorentin Chary break; 19263e230dd2SCorentin Chary case 0x52: 19273e230dd2SCorentin Chary kbd_put_keysym('0'); 19283e230dd2SCorentin Chary break; 19293e230dd2SCorentin Chary case 0x53: 19303e230dd2SCorentin Chary kbd_put_keysym(numlock ? '.' : QEMU_KEY_DELETE); 19313e230dd2SCorentin Chary break; 19323e230dd2SCorentin Chary 19333e230dd2SCorentin Chary case 0xb5: 19343e230dd2SCorentin Chary kbd_put_keysym('/'); 19353e230dd2SCorentin Chary break; 19363e230dd2SCorentin Chary case 0x37: 19373e230dd2SCorentin Chary kbd_put_keysym('*'); 19383e230dd2SCorentin Chary break; 19393e230dd2SCorentin Chary case 0x4a: 19403e230dd2SCorentin Chary kbd_put_keysym('-'); 19413e230dd2SCorentin Chary break; 19423e230dd2SCorentin Chary case 0x4e: 19433e230dd2SCorentin Chary kbd_put_keysym('+'); 19443e230dd2SCorentin Chary break; 19453e230dd2SCorentin Chary case 0x9c: 19463e230dd2SCorentin Chary kbd_put_keysym('\n'); 19473e230dd2SCorentin Chary break; 19483e230dd2SCorentin Chary 19493e230dd2SCorentin Chary default: 1950e26437c2SGerd Hoffmann if (control) { 1951e26437c2SGerd Hoffmann kbd_put_keysym(sym & 0x1f); 1952e26437c2SGerd Hoffmann } else { 19533e230dd2SCorentin Chary kbd_put_keysym(sym); 1954e26437c2SGerd Hoffmann } 19553e230dd2SCorentin Chary break; 19563e230dd2SCorentin Chary } 19573e230dd2SCorentin Chary } 19583e230dd2SCorentin Chary } 19593e230dd2SCorentin Chary } 19603e230dd2SCorentin Chary 196140066175SGerd Hoffmann static const char *code2name(int keycode) 196240066175SGerd Hoffmann { 1963977c736fSMarkus Armbruster return QKeyCode_str(qemu_input_key_number_to_qcode(keycode)); 196440066175SGerd Hoffmann } 196540066175SGerd Hoffmann 19663e230dd2SCorentin Chary static void key_event(VncState *vs, int down, uint32_t sym) 19673e230dd2SCorentin Chary { 19683e230dd2SCorentin Chary int keycode; 19693e230dd2SCorentin Chary int lsym = sym; 19703e230dd2SCorentin Chary 197181c0d5a6SGerd Hoffmann if (lsym >= 'A' && lsym <= 'Z' && qemu_console_is_graphic(NULL)) { 19723e230dd2SCorentin Chary lsym = lsym - 'A' + 'a'; 19733e230dd2SCorentin Chary } 19743e230dd2SCorentin Chary 1975abb4f2c9SGerd Hoffmann keycode = keysym2scancode(vs->vd->kbd_layout, lsym & 0xFFFF, 197619c1b9fdSGerd Hoffmann vs->vd->kbd, down) & SCANCODE_KEYMASK; 197740066175SGerd Hoffmann trace_vnc_key_event_map(down, sym, keycode, code2name(keycode)); 19783e230dd2SCorentin Chary do_key_event(vs, down, keycode, sym); 19793e230dd2SCorentin Chary } 19803e230dd2SCorentin Chary 19813e230dd2SCorentin Chary static void ext_key_event(VncState *vs, int down, 19823e230dd2SCorentin Chary uint32_t sym, uint16_t keycode) 19833e230dd2SCorentin Chary { 19843e230dd2SCorentin Chary /* if the user specifies a keyboard layout, always use it */ 198540066175SGerd Hoffmann if (keyboard_layout) { 19863e230dd2SCorentin Chary key_event(vs, down, sym); 198740066175SGerd Hoffmann } else { 198840066175SGerd Hoffmann trace_vnc_key_event_ext(down, sym, keycode, code2name(keycode)); 19893e230dd2SCorentin Chary do_key_event(vs, down, keycode, sym); 19903e230dd2SCorentin Chary } 199140066175SGerd Hoffmann } 19923e230dd2SCorentin Chary 19933e230dd2SCorentin Chary static void framebuffer_update_request(VncState *vs, int incremental, 1994bea60dd7SPeter Lieven int x, int y, int w, int h) 19953e230dd2SCorentin Chary { 1996bea60dd7SPeter Lieven if (incremental) { 1997fef1bbadSDaniel P. Berrange if (vs->update != VNC_STATE_UPDATE_FORCE) { 1998fef1bbadSDaniel P. Berrange vs->update = VNC_STATE_UPDATE_INCREMENTAL; 19993e230dd2SCorentin Chary } 2000fef1bbadSDaniel P. Berrange } else { 2001fef1bbadSDaniel P. Berrange vs->update = VNC_STATE_UPDATE_FORCE; 2002f7b3d68cSGerd Hoffmann vnc_set_area_dirty(vs->dirty, vs->vd, x, y, w, h); 20033e230dd2SCorentin Chary } 2004fef1bbadSDaniel P. Berrange } 20053e230dd2SCorentin Chary 20063e230dd2SCorentin Chary static void send_ext_key_event_ack(VncState *vs) 20073e230dd2SCorentin Chary { 2008bd023f95SCorentin Chary vnc_lock_output(vs); 20093e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 20103e230dd2SCorentin Chary vnc_write_u8(vs, 0); 20113e230dd2SCorentin Chary vnc_write_u16(vs, 1); 2012d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 2013bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 2014bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 20153e230dd2SCorentin Chary VNC_ENCODING_EXT_KEY_EVENT); 2016bd023f95SCorentin Chary vnc_unlock_output(vs); 20173e230dd2SCorentin Chary vnc_flush(vs); 20183e230dd2SCorentin Chary } 20193e230dd2SCorentin Chary 20203e230dd2SCorentin Chary static void send_ext_audio_ack(VncState *vs) 20213e230dd2SCorentin Chary { 2022bd023f95SCorentin Chary vnc_lock_output(vs); 20233e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 20243e230dd2SCorentin Chary vnc_write_u8(vs, 0); 20253e230dd2SCorentin Chary vnc_write_u16(vs, 1); 2026d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 2027bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 2028bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 20293e230dd2SCorentin Chary VNC_ENCODING_AUDIO); 2030bd023f95SCorentin Chary vnc_unlock_output(vs); 20313e230dd2SCorentin Chary vnc_flush(vs); 20323e230dd2SCorentin Chary } 20333e230dd2SCorentin Chary 20343e230dd2SCorentin Chary static void set_encodings(VncState *vs, int32_t *encodings, size_t n_encodings) 20353e230dd2SCorentin Chary { 20363e230dd2SCorentin Chary int i; 20373e230dd2SCorentin Chary unsigned int enc = 0; 20383e230dd2SCorentin Chary 20393e230dd2SCorentin Chary vs->features = 0; 20403e230dd2SCorentin Chary vs->vnc_encoding = 0; 20416bf21f3dSLi Qiang vs->tight->compression = 9; 20426bf21f3dSLi Qiang vs->tight->quality = -1; /* Lossless by default */ 20433e230dd2SCorentin Chary vs->absolute = -1; 20443e230dd2SCorentin Chary 20453e230dd2SCorentin Chary /* 20463e230dd2SCorentin Chary * Start from the end because the encodings are sent in order of preference. 2047e5bed759SDong Xu Wang * This way the preferred encoding (first encoding defined in the array) 20483e230dd2SCorentin Chary * will be set at the end of the loop. 20493e230dd2SCorentin Chary */ 20503e230dd2SCorentin Chary for (i = n_encodings - 1; i >= 0; i--) { 20513e230dd2SCorentin Chary enc = encodings[i]; 20523e230dd2SCorentin Chary switch (enc) { 20533e230dd2SCorentin Chary case VNC_ENCODING_RAW: 20543e230dd2SCorentin Chary vs->vnc_encoding = enc; 20553e230dd2SCorentin Chary break; 20563e230dd2SCorentin Chary case VNC_ENCODING_COPYRECT: 20573e230dd2SCorentin Chary vs->features |= VNC_FEATURE_COPYRECT_MASK; 20583e230dd2SCorentin Chary break; 20593e230dd2SCorentin Chary case VNC_ENCODING_HEXTILE: 20603e230dd2SCorentin Chary vs->features |= VNC_FEATURE_HEXTILE_MASK; 20613e230dd2SCorentin Chary vs->vnc_encoding = enc; 20623e230dd2SCorentin Chary break; 20633e230dd2SCorentin Chary case VNC_ENCODING_TIGHT: 20643e230dd2SCorentin Chary vs->features |= VNC_FEATURE_TIGHT_MASK; 20653e230dd2SCorentin Chary vs->vnc_encoding = enc; 20663e230dd2SCorentin Chary break; 2067fe3e7f2dSJoel Martin #ifdef CONFIG_VNC_PNG 2068efe556adSCorentin Chary case VNC_ENCODING_TIGHT_PNG: 2069efe556adSCorentin Chary vs->features |= VNC_FEATURE_TIGHT_PNG_MASK; 2070efe556adSCorentin Chary vs->vnc_encoding = enc; 2071efe556adSCorentin Chary break; 2072fe3e7f2dSJoel Martin #endif 20733e230dd2SCorentin Chary case VNC_ENCODING_ZLIB: 20743e230dd2SCorentin Chary vs->features |= VNC_FEATURE_ZLIB_MASK; 20753e230dd2SCorentin Chary vs->vnc_encoding = enc; 20763e230dd2SCorentin Chary break; 2077148954faSCorentin Chary case VNC_ENCODING_ZRLE: 2078148954faSCorentin Chary vs->features |= VNC_FEATURE_ZRLE_MASK; 2079148954faSCorentin Chary vs->vnc_encoding = enc; 2080148954faSCorentin Chary break; 2081148954faSCorentin Chary case VNC_ENCODING_ZYWRLE: 2082148954faSCorentin Chary vs->features |= VNC_FEATURE_ZYWRLE_MASK; 2083148954faSCorentin Chary vs->vnc_encoding = enc; 2084148954faSCorentin Chary break; 20853e230dd2SCorentin Chary case VNC_ENCODING_DESKTOPRESIZE: 20863e230dd2SCorentin Chary vs->features |= VNC_FEATURE_RESIZE_MASK; 20873e230dd2SCorentin Chary break; 20883e230dd2SCorentin Chary case VNC_ENCODING_POINTER_TYPE_CHANGE: 20893e230dd2SCorentin Chary vs->features |= VNC_FEATURE_POINTER_TYPE_CHANGE_MASK; 20903e230dd2SCorentin Chary break; 20913e230dd2SCorentin Chary case VNC_ENCODING_RICH_CURSOR: 20923e230dd2SCorentin Chary vs->features |= VNC_FEATURE_RICH_CURSOR_MASK; 209391ec41dcSFrediano Ziglio if (vs->vd->cursor) { 209491ec41dcSFrediano Ziglio vnc_cursor_define(vs); 209591ec41dcSFrediano Ziglio } 20963e230dd2SCorentin Chary break; 20973e230dd2SCorentin Chary case VNC_ENCODING_EXT_KEY_EVENT: 20983e230dd2SCorentin Chary send_ext_key_event_ack(vs); 20993e230dd2SCorentin Chary break; 21003e230dd2SCorentin Chary case VNC_ENCODING_AUDIO: 21013e230dd2SCorentin Chary send_ext_audio_ack(vs); 21023e230dd2SCorentin Chary break; 21033e230dd2SCorentin Chary case VNC_ENCODING_WMVi: 21043e230dd2SCorentin Chary vs->features |= VNC_FEATURE_WMVI_MASK; 21053e230dd2SCorentin Chary break; 2106ab99e5c1SLei Li case VNC_ENCODING_LED_STATE: 2107ab99e5c1SLei Li vs->features |= VNC_FEATURE_LED_STATE_MASK; 2108ab99e5c1SLei Li break; 21093e230dd2SCorentin Chary case VNC_ENCODING_COMPRESSLEVEL0 ... VNC_ENCODING_COMPRESSLEVEL0 + 9: 21106bf21f3dSLi Qiang vs->tight->compression = (enc & 0x0F); 21113e230dd2SCorentin Chary break; 21123e230dd2SCorentin Chary case VNC_ENCODING_QUALITYLEVEL0 ... VNC_ENCODING_QUALITYLEVEL0 + 9: 2113b31f519eSCorentin Chary if (vs->vd->lossy) { 21146bf21f3dSLi Qiang vs->tight->quality = (enc & 0x0F); 2115b31f519eSCorentin Chary } 21163e230dd2SCorentin Chary break; 21173e230dd2SCorentin Chary default: 21183e230dd2SCorentin Chary VNC_DEBUG("Unknown encoding: %d (0x%.8x): %d\n", i, enc, enc); 21193e230dd2SCorentin Chary break; 21203e230dd2SCorentin Chary } 21213e230dd2SCorentin Chary } 21223e230dd2SCorentin Chary vnc_desktop_resize(vs); 21239e8dd451SJan Kiszka check_pointer_type_change(&vs->mouse_mode_notifier, NULL); 2124ab99e5c1SLei Li vnc_led_state_change(vs); 21253e230dd2SCorentin Chary } 21263e230dd2SCorentin Chary 21273e230dd2SCorentin Chary static void set_pixel_conversion(VncState *vs) 21283e230dd2SCorentin Chary { 21299f64916dSGerd Hoffmann pixman_format_code_t fmt = qemu_pixman_get_format(&vs->client_pf); 21309f64916dSGerd Hoffmann 21319f64916dSGerd Hoffmann if (fmt == VNC_SERVER_FB_FORMAT) { 21323e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_copy; 21333e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 0); 21343e230dd2SCorentin Chary } else { 21353e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_generic; 21363e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 1); 21373e230dd2SCorentin Chary } 21383e230dd2SCorentin Chary } 21393e230dd2SCorentin Chary 21400c426e45SAlexander Graf static void send_color_map(VncState *vs) 21410c426e45SAlexander Graf { 21420c426e45SAlexander Graf int i; 21430c426e45SAlexander Graf 21440c426e45SAlexander Graf vnc_write_u8(vs, VNC_MSG_SERVER_SET_COLOUR_MAP_ENTRIES); 21450c426e45SAlexander Graf vnc_write_u8(vs, 0); /* padding */ 21460c426e45SAlexander Graf vnc_write_u16(vs, 0); /* first color */ 21470c426e45SAlexander Graf vnc_write_u16(vs, 256); /* # of colors */ 21480c426e45SAlexander Graf 21490c426e45SAlexander Graf for (i = 0; i < 256; i++) { 21500c426e45SAlexander Graf PixelFormat *pf = &vs->client_pf; 21510c426e45SAlexander Graf 21520c426e45SAlexander Graf vnc_write_u16(vs, (((i >> pf->rshift) & pf->rmax) << (16 - pf->rbits))); 21530c426e45SAlexander Graf vnc_write_u16(vs, (((i >> pf->gshift) & pf->gmax) << (16 - pf->gbits))); 21540c426e45SAlexander Graf vnc_write_u16(vs, (((i >> pf->bshift) & pf->bmax) << (16 - pf->bbits))); 21550c426e45SAlexander Graf } 21560c426e45SAlexander Graf } 21570c426e45SAlexander Graf 2158ec9fb41aSGerd Hoffmann static void set_pixel_format(VncState *vs, int bits_per_pixel, 21593e230dd2SCorentin Chary int big_endian_flag, int true_color_flag, 21603e230dd2SCorentin Chary int red_max, int green_max, int blue_max, 21613e230dd2SCorentin Chary int red_shift, int green_shift, int blue_shift) 21623e230dd2SCorentin Chary { 21633e230dd2SCorentin Chary if (!true_color_flag) { 21640c426e45SAlexander Graf /* Expose a reasonable default 256 color map */ 21650c426e45SAlexander Graf bits_per_pixel = 8; 21660c426e45SAlexander Graf red_max = 7; 21670c426e45SAlexander Graf green_max = 7; 21680c426e45SAlexander Graf blue_max = 3; 21690c426e45SAlexander Graf red_shift = 0; 21700c426e45SAlexander Graf green_shift = 3; 21710c426e45SAlexander Graf blue_shift = 6; 21723e230dd2SCorentin Chary } 21733e230dd2SCorentin Chary 2174e6908bfeSPetr Matousek switch (bits_per_pixel) { 2175e6908bfeSPetr Matousek case 8: 2176e6908bfeSPetr Matousek case 16: 2177e6908bfeSPetr Matousek case 32: 2178e6908bfeSPetr Matousek break; 2179e6908bfeSPetr Matousek default: 2180e6908bfeSPetr Matousek vnc_client_error(vs); 2181e6908bfeSPetr Matousek return; 2182e6908bfeSPetr Matousek } 2183e6908bfeSPetr Matousek 21844c65fed8SPrasad J Pandit vs->client_pf.rmax = red_max ? red_max : 0xFF; 21857c9209e7SCédric Le Goater vs->client_pf.rbits = ctpopl(red_max); 21869f64916dSGerd Hoffmann vs->client_pf.rshift = red_shift; 21879f64916dSGerd Hoffmann vs->client_pf.rmask = red_max << red_shift; 21884c65fed8SPrasad J Pandit vs->client_pf.gmax = green_max ? green_max : 0xFF; 21897c9209e7SCédric Le Goater vs->client_pf.gbits = ctpopl(green_max); 21909f64916dSGerd Hoffmann vs->client_pf.gshift = green_shift; 21919f64916dSGerd Hoffmann vs->client_pf.gmask = green_max << green_shift; 21924c65fed8SPrasad J Pandit vs->client_pf.bmax = blue_max ? blue_max : 0xFF; 21937c9209e7SCédric Le Goater vs->client_pf.bbits = ctpopl(blue_max); 21949f64916dSGerd Hoffmann vs->client_pf.bshift = blue_shift; 21959f64916dSGerd Hoffmann vs->client_pf.bmask = blue_max << blue_shift; 21969f64916dSGerd Hoffmann vs->client_pf.bits_per_pixel = bits_per_pixel; 21979f64916dSGerd Hoffmann vs->client_pf.bytes_per_pixel = bits_per_pixel / 8; 21989f64916dSGerd Hoffmann vs->client_pf.depth = bits_per_pixel == 32 ? 24 : bits_per_pixel; 21999f64916dSGerd Hoffmann vs->client_be = big_endian_flag; 22003e230dd2SCorentin Chary 22010c426e45SAlexander Graf if (!true_color_flag) { 22020c426e45SAlexander Graf send_color_map(vs); 22030c426e45SAlexander Graf } 22040c426e45SAlexander Graf 22053e230dd2SCorentin Chary set_pixel_conversion(vs); 22063e230dd2SCorentin Chary 22071d0d59feSGerd Hoffmann graphic_hw_invalidate(vs->vd->dcl.con); 22081d0d59feSGerd Hoffmann graphic_hw_update(vs->vd->dcl.con); 22093e230dd2SCorentin Chary } 22103e230dd2SCorentin Chary 22113e230dd2SCorentin Chary static void pixel_format_message (VncState *vs) { 22123e230dd2SCorentin Chary char pad[3] = { 0, 0, 0 }; 22133e230dd2SCorentin Chary 22149f64916dSGerd Hoffmann vs->client_pf = qemu_default_pixelformat(32); 22159f64916dSGerd Hoffmann 22169f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.bits_per_pixel); /* bits-per-pixel */ 22179f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.depth); /* depth */ 22183e230dd2SCorentin Chary 22193e230dd2SCorentin Chary #ifdef HOST_WORDS_BIGENDIAN 22203e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* big-endian-flag */ 22213e230dd2SCorentin Chary #else 22223e230dd2SCorentin Chary vnc_write_u8(vs, 0); /* big-endian-flag */ 22233e230dd2SCorentin Chary #endif 22243e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* true-color-flag */ 22259f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.rmax); /* red-max */ 22269f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.gmax); /* green-max */ 22279f64916dSGerd Hoffmann vnc_write_u16(vs, vs->client_pf.bmax); /* blue-max */ 22289f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.rshift); /* red-shift */ 22299f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.gshift); /* green-shift */ 22309f64916dSGerd Hoffmann vnc_write_u8(vs, vs->client_pf.bshift); /* blue-shift */ 22319f64916dSGerd Hoffmann vnc_write(vs, pad, 3); /* padding */ 22323e230dd2SCorentin Chary 22333e230dd2SCorentin Chary vnc_hextile_set_pixel_conversion(vs, 0); 22343e230dd2SCorentin Chary vs->write_pixels = vnc_write_pixels_copy; 22353e230dd2SCorentin Chary } 22363e230dd2SCorentin Chary 22373e230dd2SCorentin Chary static void vnc_colordepth(VncState *vs) 22383e230dd2SCorentin Chary { 22393e230dd2SCorentin Chary if (vnc_has_feature(vs, VNC_FEATURE_WMVI)) { 22403e230dd2SCorentin Chary /* Sending a WMVi message to notify the client*/ 2241bd023f95SCorentin Chary vnc_lock_output(vs); 22423e230dd2SCorentin Chary vnc_write_u8(vs, VNC_MSG_SERVER_FRAMEBUFFER_UPDATE); 22433e230dd2SCorentin Chary vnc_write_u8(vs, 0); 22443e230dd2SCorentin Chary vnc_write_u16(vs, 1); /* number of rects */ 2245d39fa6d8SGerd Hoffmann vnc_framebuffer_update(vs, 0, 0, 2246bea60dd7SPeter Lieven pixman_image_get_width(vs->vd->server), 2247bea60dd7SPeter Lieven pixman_image_get_height(vs->vd->server), 2248d39fa6d8SGerd Hoffmann VNC_ENCODING_WMVi); 22493e230dd2SCorentin Chary pixel_format_message(vs); 2250bd023f95SCorentin Chary vnc_unlock_output(vs); 22513e230dd2SCorentin Chary vnc_flush(vs); 22523e230dd2SCorentin Chary } else { 22533e230dd2SCorentin Chary set_pixel_conversion(vs); 22543e230dd2SCorentin Chary } 22553e230dd2SCorentin Chary } 22563e230dd2SCorentin Chary 22573e230dd2SCorentin Chary static int protocol_client_msg(VncState *vs, uint8_t *data, size_t len) 22583e230dd2SCorentin Chary { 22593e230dd2SCorentin Chary int i; 22603e230dd2SCorentin Chary uint16_t limit; 2261cf070658SDaniel P. Berrangé uint32_t freq; 22623e230dd2SCorentin Chary VncDisplay *vd = vs->vd; 22633e230dd2SCorentin Chary 22643e230dd2SCorentin Chary if (data[0] > 3) { 22650f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 22663e230dd2SCorentin Chary } 22673e230dd2SCorentin Chary 22683e230dd2SCorentin Chary switch (data[0]) { 22693e230dd2SCorentin Chary case VNC_MSG_CLIENT_SET_PIXEL_FORMAT: 22703e230dd2SCorentin Chary if (len == 1) 22713e230dd2SCorentin Chary return 20; 22723e230dd2SCorentin Chary 2273ec9fb41aSGerd Hoffmann set_pixel_format(vs, read_u8(data, 4), 22743e230dd2SCorentin Chary read_u8(data, 6), read_u8(data, 7), 22753e230dd2SCorentin Chary read_u16(data, 8), read_u16(data, 10), 22763e230dd2SCorentin Chary read_u16(data, 12), read_u8(data, 14), 22773e230dd2SCorentin Chary read_u8(data, 15), read_u8(data, 16)); 22783e230dd2SCorentin Chary break; 22793e230dd2SCorentin Chary case VNC_MSG_CLIENT_SET_ENCODINGS: 22803e230dd2SCorentin Chary if (len == 1) 22813e230dd2SCorentin Chary return 4; 22823e230dd2SCorentin Chary 22833e230dd2SCorentin Chary if (len == 4) { 22843e230dd2SCorentin Chary limit = read_u16(data, 2); 22853e230dd2SCorentin Chary if (limit > 0) 22863e230dd2SCorentin Chary return 4 + (limit * 4); 22873e230dd2SCorentin Chary } else 22883e230dd2SCorentin Chary limit = read_u16(data, 2); 22893e230dd2SCorentin Chary 22903e230dd2SCorentin Chary for (i = 0; i < limit; i++) { 22913e230dd2SCorentin Chary int32_t val = read_s32(data, 4 + (i * 4)); 22923e230dd2SCorentin Chary memcpy(data + 4 + (i * 4), &val, sizeof(val)); 22933e230dd2SCorentin Chary } 22943e230dd2SCorentin Chary 22953e230dd2SCorentin Chary set_encodings(vs, (int32_t *)(data + 4), limit); 22963e230dd2SCorentin Chary break; 22973e230dd2SCorentin Chary case VNC_MSG_CLIENT_FRAMEBUFFER_UPDATE_REQUEST: 22983e230dd2SCorentin Chary if (len == 1) 22993e230dd2SCorentin Chary return 10; 23003e230dd2SCorentin Chary 23013e230dd2SCorentin Chary framebuffer_update_request(vs, 23023e230dd2SCorentin Chary read_u8(data, 1), read_u16(data, 2), read_u16(data, 4), 23033e230dd2SCorentin Chary read_u16(data, 6), read_u16(data, 8)); 23043e230dd2SCorentin Chary break; 23053e230dd2SCorentin Chary case VNC_MSG_CLIENT_KEY_EVENT: 23063e230dd2SCorentin Chary if (len == 1) 23073e230dd2SCorentin Chary return 8; 23083e230dd2SCorentin Chary 23093e230dd2SCorentin Chary key_event(vs, read_u8(data, 1), read_u32(data, 4)); 23103e230dd2SCorentin Chary break; 23113e230dd2SCorentin Chary case VNC_MSG_CLIENT_POINTER_EVENT: 23123e230dd2SCorentin Chary if (len == 1) 23133e230dd2SCorentin Chary return 6; 23143e230dd2SCorentin Chary 23153e230dd2SCorentin Chary pointer_event(vs, read_u8(data, 1), read_u16(data, 2), read_u16(data, 4)); 23163e230dd2SCorentin Chary break; 23173e230dd2SCorentin Chary case VNC_MSG_CLIENT_CUT_TEXT: 2318f9a70e79SPeter Lieven if (len == 1) { 23193e230dd2SCorentin Chary return 8; 2320f9a70e79SPeter Lieven } 23213e230dd2SCorentin Chary if (len == 8) { 23223e230dd2SCorentin Chary uint32_t dlen = read_u32(data, 4); 2323f9a70e79SPeter Lieven if (dlen > (1 << 20)) { 2324f9a70e79SPeter Lieven error_report("vnc: client_cut_text msg payload has %u bytes" 2325f9a70e79SPeter Lieven " which exceeds our limit of 1MB.", dlen); 2326f9a70e79SPeter Lieven vnc_client_error(vs); 2327f9a70e79SPeter Lieven break; 2328f9a70e79SPeter Lieven } 2329f9a70e79SPeter Lieven if (dlen > 0) { 23303e230dd2SCorentin Chary return 8 + dlen; 23313e230dd2SCorentin Chary } 2332f9a70e79SPeter Lieven } 23333e230dd2SCorentin Chary 23343e230dd2SCorentin Chary client_cut_text(vs, read_u32(data, 4), data + 8); 23353e230dd2SCorentin Chary break; 23363e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU: 23373e230dd2SCorentin Chary if (len == 1) 23383e230dd2SCorentin Chary return 2; 23393e230dd2SCorentin Chary 23403e230dd2SCorentin Chary switch (read_u8(data, 1)) { 23413e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_EXT_KEY_EVENT: 23423e230dd2SCorentin Chary if (len == 2) 23433e230dd2SCorentin Chary return 12; 23443e230dd2SCorentin Chary 23453e230dd2SCorentin Chary ext_key_event(vs, read_u16(data, 2), 23463e230dd2SCorentin Chary read_u32(data, 4), read_u32(data, 8)); 23473e230dd2SCorentin Chary break; 23483e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO: 23493e230dd2SCorentin Chary if (len == 2) 23503e230dd2SCorentin Chary return 4; 23513e230dd2SCorentin Chary 23523e230dd2SCorentin Chary switch (read_u16 (data, 2)) { 23533e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_ENABLE: 23543e230dd2SCorentin Chary audio_add(vs); 23553e230dd2SCorentin Chary break; 23563e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_DISABLE: 23573e230dd2SCorentin Chary audio_del(vs); 23583e230dd2SCorentin Chary break; 23593e230dd2SCorentin Chary case VNC_MSG_CLIENT_QEMU_AUDIO_SET_FORMAT: 23603e230dd2SCorentin Chary if (len == 4) 23613e230dd2SCorentin Chary return 10; 23623e230dd2SCorentin Chary switch (read_u8(data, 4)) { 236385bc5852SKővágó, Zoltán case 0: vs->as.fmt = AUDIO_FORMAT_U8; break; 236485bc5852SKővágó, Zoltán case 1: vs->as.fmt = AUDIO_FORMAT_S8; break; 236585bc5852SKővágó, Zoltán case 2: vs->as.fmt = AUDIO_FORMAT_U16; break; 236685bc5852SKővágó, Zoltán case 3: vs->as.fmt = AUDIO_FORMAT_S16; break; 236785bc5852SKővágó, Zoltán case 4: vs->as.fmt = AUDIO_FORMAT_U32; break; 236885bc5852SKővágó, Zoltán case 5: vs->as.fmt = AUDIO_FORMAT_S32; break; 23693e230dd2SCorentin Chary default: 2370153130cdSDaniel P. Berrange VNC_DEBUG("Invalid audio format %d\n", read_u8(data, 4)); 23713e230dd2SCorentin Chary vnc_client_error(vs); 23723e230dd2SCorentin Chary break; 23733e230dd2SCorentin Chary } 23743e230dd2SCorentin Chary vs->as.nchannels = read_u8(data, 5); 23753e230dd2SCorentin Chary if (vs->as.nchannels != 1 && vs->as.nchannels != 2) { 2376090fdc83SMarc-André Lureau VNC_DEBUG("Invalid audio channel count %d\n", 23773e230dd2SCorentin Chary read_u8(data, 5)); 23783e230dd2SCorentin Chary vnc_client_error(vs); 23793e230dd2SCorentin Chary break; 23803e230dd2SCorentin Chary } 2381cf070658SDaniel P. Berrangé freq = read_u32(data, 6); 2382cf070658SDaniel P. Berrangé /* No official limit for protocol, but 48khz is a sensible 2383cf070658SDaniel P. Berrangé * upper bound for trustworthy clients, and this limit 2384cf070658SDaniel P. Berrangé * protects calculations involving 'vs->as.freq' later. 2385cf070658SDaniel P. Berrangé */ 2386cf070658SDaniel P. Berrangé if (freq > 48000) { 2387cf070658SDaniel P. Berrangé VNC_DEBUG("Invalid audio frequency %u > 48000", freq); 2388cf070658SDaniel P. Berrangé vnc_client_error(vs); 2389cf070658SDaniel P. Berrangé break; 2390cf070658SDaniel P. Berrangé } 2391cf070658SDaniel P. Berrangé vs->as.freq = freq; 23923e230dd2SCorentin Chary break; 23933e230dd2SCorentin Chary default: 2394153130cdSDaniel P. Berrange VNC_DEBUG("Invalid audio message %d\n", read_u8(data, 4)); 23953e230dd2SCorentin Chary vnc_client_error(vs); 23963e230dd2SCorentin Chary break; 23973e230dd2SCorentin Chary } 23983e230dd2SCorentin Chary break; 23993e230dd2SCorentin Chary 24003e230dd2SCorentin Chary default: 2401153130cdSDaniel P. Berrange VNC_DEBUG("Msg: %d\n", read_u16(data, 0)); 24023e230dd2SCorentin Chary vnc_client_error(vs); 24033e230dd2SCorentin Chary break; 24043e230dd2SCorentin Chary } 24053e230dd2SCorentin Chary break; 24063e230dd2SCorentin Chary default: 2407153130cdSDaniel P. Berrange VNC_DEBUG("Msg: %d\n", data[0]); 24083e230dd2SCorentin Chary vnc_client_error(vs); 24093e230dd2SCorentin Chary break; 24103e230dd2SCorentin Chary } 24113e230dd2SCorentin Chary 2412e2b72cb6SDaniel P. Berrange vnc_update_throttle_offset(vs); 24133e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_msg, 1); 24143e230dd2SCorentin Chary return 0; 24153e230dd2SCorentin Chary } 24163e230dd2SCorentin Chary 24173e230dd2SCorentin Chary static int protocol_client_init(VncState *vs, uint8_t *data, size_t len) 24183e230dd2SCorentin Chary { 24193e230dd2SCorentin Chary char buf[1024]; 24208cf36489SGerd Hoffmann VncShareMode mode; 24213e230dd2SCorentin Chary int size; 24223e230dd2SCorentin Chary 24238cf36489SGerd Hoffmann mode = data[0] ? VNC_SHARE_MODE_SHARED : VNC_SHARE_MODE_EXCLUSIVE; 24248cf36489SGerd Hoffmann switch (vs->vd->share_policy) { 24258cf36489SGerd Hoffmann case VNC_SHARE_POLICY_IGNORE: 24268cf36489SGerd Hoffmann /* 24278cf36489SGerd Hoffmann * Ignore the shared flag. Nothing to do here. 24288cf36489SGerd Hoffmann * 24298cf36489SGerd Hoffmann * Doesn't conform to the rfb spec but is traditional qemu 24308cf36489SGerd Hoffmann * behavior, thus left here as option for compatibility 24318cf36489SGerd Hoffmann * reasons. 24328cf36489SGerd Hoffmann */ 24338cf36489SGerd Hoffmann break; 24348cf36489SGerd Hoffmann case VNC_SHARE_POLICY_ALLOW_EXCLUSIVE: 24358cf36489SGerd Hoffmann /* 24368cf36489SGerd Hoffmann * Policy: Allow clients ask for exclusive access. 24378cf36489SGerd Hoffmann * 24388cf36489SGerd Hoffmann * Implementation: When a client asks for exclusive access, 24398cf36489SGerd Hoffmann * disconnect all others. Shared connects are allowed as long 24408cf36489SGerd Hoffmann * as no exclusive connection exists. 24418cf36489SGerd Hoffmann * 24428cf36489SGerd Hoffmann * This is how the rfb spec suggests to handle the shared flag. 24438cf36489SGerd Hoffmann */ 24448cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_EXCLUSIVE) { 24458cf36489SGerd Hoffmann VncState *client; 24468cf36489SGerd Hoffmann QTAILQ_FOREACH(client, &vs->vd->clients, next) { 24478cf36489SGerd Hoffmann if (vs == client) { 24488cf36489SGerd Hoffmann continue; 24498cf36489SGerd Hoffmann } 24508cf36489SGerd Hoffmann if (client->share_mode != VNC_SHARE_MODE_EXCLUSIVE && 24518cf36489SGerd Hoffmann client->share_mode != VNC_SHARE_MODE_SHARED) { 24528cf36489SGerd Hoffmann continue; 24538cf36489SGerd Hoffmann } 24548cf36489SGerd Hoffmann vnc_disconnect_start(client); 24558cf36489SGerd Hoffmann } 24568cf36489SGerd Hoffmann } 24578cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_SHARED) { 24588cf36489SGerd Hoffmann if (vs->vd->num_exclusive > 0) { 24598cf36489SGerd Hoffmann vnc_disconnect_start(vs); 24608cf36489SGerd Hoffmann return 0; 24618cf36489SGerd Hoffmann } 24628cf36489SGerd Hoffmann } 24638cf36489SGerd Hoffmann break; 24648cf36489SGerd Hoffmann case VNC_SHARE_POLICY_FORCE_SHARED: 24658cf36489SGerd Hoffmann /* 24668cf36489SGerd Hoffmann * Policy: Shared connects only. 24678cf36489SGerd Hoffmann * Implementation: Disallow clients asking for exclusive access. 24688cf36489SGerd Hoffmann * 24698cf36489SGerd Hoffmann * Useful for shared desktop sessions where you don't want 24708cf36489SGerd Hoffmann * someone forgetting to say -shared when running the vnc 24718cf36489SGerd Hoffmann * client disconnect everybody else. 24728cf36489SGerd Hoffmann */ 24738cf36489SGerd Hoffmann if (mode == VNC_SHARE_MODE_EXCLUSIVE) { 24748cf36489SGerd Hoffmann vnc_disconnect_start(vs); 24758cf36489SGerd Hoffmann return 0; 24768cf36489SGerd Hoffmann } 24778cf36489SGerd Hoffmann break; 24788cf36489SGerd Hoffmann } 24798cf36489SGerd Hoffmann vnc_set_share_mode(vs, mode); 24808cf36489SGerd Hoffmann 2481e5f34cddSGerd Hoffmann if (vs->vd->num_shared > vs->vd->connections_limit) { 2482e5f34cddSGerd Hoffmann vnc_disconnect_start(vs); 2483e5f34cddSGerd Hoffmann return 0; 2484e5f34cddSGerd Hoffmann } 2485e5f34cddSGerd Hoffmann 24864c956bd8SDaniel P. Berrange assert(pixman_image_get_width(vs->vd->server) < 65536 && 24874c956bd8SDaniel P. Berrange pixman_image_get_width(vs->vd->server) >= 0); 24884c956bd8SDaniel P. Berrange assert(pixman_image_get_height(vs->vd->server) < 65536 && 24894c956bd8SDaniel P. Berrange pixman_image_get_height(vs->vd->server) >= 0); 2490bea60dd7SPeter Lieven vs->client_width = pixman_image_get_width(vs->vd->server); 2491bea60dd7SPeter Lieven vs->client_height = pixman_image_get_height(vs->vd->server); 24923e230dd2SCorentin Chary vnc_write_u16(vs, vs->client_width); 24933e230dd2SCorentin Chary vnc_write_u16(vs, vs->client_height); 24943e230dd2SCorentin Chary 24953e230dd2SCorentin Chary pixel_format_message(vs); 24963e230dd2SCorentin Chary 249797efe4f9SThomas Huth if (qemu_name) { 24983e230dd2SCorentin Chary size = snprintf(buf, sizeof(buf), "QEMU (%s)", qemu_name); 249997efe4f9SThomas Huth if (size > sizeof(buf)) { 250097efe4f9SThomas Huth size = sizeof(buf); 250197efe4f9SThomas Huth } 250297efe4f9SThomas Huth } else { 25033e230dd2SCorentin Chary size = snprintf(buf, sizeof(buf), "QEMU"); 250497efe4f9SThomas Huth } 25053e230dd2SCorentin Chary 25063e230dd2SCorentin Chary vnc_write_u32(vs, size); 25073e230dd2SCorentin Chary vnc_write(vs, buf, size); 25083e230dd2SCorentin Chary vnc_flush(vs); 25093e230dd2SCorentin Chary 25103e230dd2SCorentin Chary vnc_client_cache_auth(vs); 2511fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_INITIALIZED); 25123e230dd2SCorentin Chary 25133e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_msg, 1); 25143e230dd2SCorentin Chary 25153e230dd2SCorentin Chary return 0; 25163e230dd2SCorentin Chary } 25173e230dd2SCorentin Chary 25183e230dd2SCorentin Chary void start_client_init(VncState *vs) 25193e230dd2SCorentin Chary { 25203e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_init, 1); 25213e230dd2SCorentin Chary } 25223e230dd2SCorentin Chary 25234347e638SRichard Henderson static void authentication_failed(VncState *vs) 25244347e638SRichard Henderson { 25254347e638SRichard Henderson vnc_write_u32(vs, 1); /* Reject auth */ 25264347e638SRichard Henderson if (vs->minor >= 8) { 25274347e638SRichard Henderson static const char err[] = "Authentication failed"; 25284347e638SRichard Henderson vnc_write_u32(vs, sizeof(err)); 25294347e638SRichard Henderson vnc_write(vs, err, sizeof(err)); 25304347e638SRichard Henderson } 25314347e638SRichard Henderson vnc_flush(vs); 25324347e638SRichard Henderson vnc_client_error(vs); 25334347e638SRichard Henderson } 25344347e638SRichard Henderson 25353e230dd2SCorentin Chary static int protocol_client_auth_vnc(VncState *vs, uint8_t *data, size_t len) 25363e230dd2SCorentin Chary { 25373e230dd2SCorentin Chary unsigned char response[VNC_AUTH_CHALLENGE_SIZE]; 2538800567a6SDaniel P. Berrange size_t i, pwlen; 25393e230dd2SCorentin Chary unsigned char key[8]; 25403c9405a0SGerd Hoffmann time_t now = time(NULL); 254160928458SGonglei QCryptoCipher *cipher = NULL; 2542800567a6SDaniel P. Berrange Error *err = NULL; 25433e230dd2SCorentin Chary 25441cd20f8bSAnthony Liguori if (!vs->vd->password) { 25457364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "password is not set", ""); 25466bffdf0fSGerd Hoffmann goto reject; 25473e230dd2SCorentin Chary } 25483c9405a0SGerd Hoffmann if (vs->vd->expires < now) { 25497364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "password is expired", ""); 25503c9405a0SGerd Hoffmann goto reject; 25513c9405a0SGerd Hoffmann } 25523e230dd2SCorentin Chary 25533e230dd2SCorentin Chary memcpy(response, vs->challenge, VNC_AUTH_CHALLENGE_SIZE); 25543e230dd2SCorentin Chary 25553e230dd2SCorentin Chary /* Calculate the expected challenge response */ 25563e230dd2SCorentin Chary pwlen = strlen(vs->vd->password); 25573e230dd2SCorentin Chary for (i=0; i<sizeof(key); i++) 25583e230dd2SCorentin Chary key[i] = i<pwlen ? vs->vd->password[i] : 0; 2559800567a6SDaniel P. Berrange 2560800567a6SDaniel P. Berrange cipher = qcrypto_cipher_new( 2561800567a6SDaniel P. Berrange QCRYPTO_CIPHER_ALG_DES_RFB, 2562800567a6SDaniel P. Berrange QCRYPTO_CIPHER_MODE_ECB, 2563800567a6SDaniel P. Berrange key, G_N_ELEMENTS(key), 2564800567a6SDaniel P. Berrange &err); 2565800567a6SDaniel P. Berrange if (!cipher) { 25667364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "cannot create cipher", 2567800567a6SDaniel P. Berrange error_get_pretty(err)); 2568800567a6SDaniel P. Berrange error_free(err); 2569800567a6SDaniel P. Berrange goto reject; 2570800567a6SDaniel P. Berrange } 2571800567a6SDaniel P. Berrange 2572a1695137SWolfgang Bumiller if (qcrypto_cipher_encrypt(cipher, 2573800567a6SDaniel P. Berrange vs->challenge, 2574800567a6SDaniel P. Berrange response, 2575800567a6SDaniel P. Berrange VNC_AUTH_CHALLENGE_SIZE, 2576800567a6SDaniel P. Berrange &err) < 0) { 25777364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "cannot encrypt challenge response", 2578800567a6SDaniel P. Berrange error_get_pretty(err)); 2579800567a6SDaniel P. Berrange error_free(err); 2580800567a6SDaniel P. Berrange goto reject; 2581800567a6SDaniel P. Berrange } 25823e230dd2SCorentin Chary 25833e230dd2SCorentin Chary /* Compare expected vs actual challenge response */ 25843e230dd2SCorentin Chary if (memcmp(response, data, VNC_AUTH_CHALLENGE_SIZE) != 0) { 25857364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "mis-matched challenge response", ""); 25866bffdf0fSGerd Hoffmann goto reject; 25876bffdf0fSGerd Hoffmann } else { 25887364dbdaSDaniel P. Berrange trace_vnc_auth_pass(vs, vs->auth); 25896bffdf0fSGerd Hoffmann vnc_write_u32(vs, 0); /* Accept auth */ 25906bffdf0fSGerd Hoffmann vnc_flush(vs); 25916bffdf0fSGerd Hoffmann 25926bffdf0fSGerd Hoffmann start_client_init(vs); 25936bffdf0fSGerd Hoffmann } 259460928458SGonglei 259560928458SGonglei qcrypto_cipher_free(cipher); 25966bffdf0fSGerd Hoffmann return 0; 25976bffdf0fSGerd Hoffmann 25986bffdf0fSGerd Hoffmann reject: 25994347e638SRichard Henderson authentication_failed(vs); 260060928458SGonglei qcrypto_cipher_free(cipher); 26013e230dd2SCorentin Chary return 0; 26023e230dd2SCorentin Chary } 26033e230dd2SCorentin Chary 26043e230dd2SCorentin Chary void start_auth_vnc(VncState *vs) 26053e230dd2SCorentin Chary { 2606f7b2502cSRichard Henderson Error *err = NULL; 2607f7b2502cSRichard Henderson 2608f7b2502cSRichard Henderson if (qcrypto_random_bytes(vs->challenge, sizeof(vs->challenge), &err)) { 2609f7b2502cSRichard Henderson trace_vnc_auth_fail(vs, vs->auth, "cannot get random bytes", 2610f7b2502cSRichard Henderson error_get_pretty(err)); 2611f7b2502cSRichard Henderson error_free(err); 2612f7b2502cSRichard Henderson authentication_failed(vs); 2613f7b2502cSRichard Henderson return; 2614f7b2502cSRichard Henderson } 2615f7b2502cSRichard Henderson 26163e230dd2SCorentin Chary /* Send client a 'random' challenge */ 26173e230dd2SCorentin Chary vnc_write(vs, vs->challenge, sizeof(vs->challenge)); 26183e230dd2SCorentin Chary vnc_flush(vs); 26193e230dd2SCorentin Chary 26203e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_auth_vnc, sizeof(vs->challenge)); 26213e230dd2SCorentin Chary } 26223e230dd2SCorentin Chary 26233e230dd2SCorentin Chary 26243e230dd2SCorentin Chary static int protocol_client_auth(VncState *vs, uint8_t *data, size_t len) 26253e230dd2SCorentin Chary { 26263e230dd2SCorentin Chary /* We only advertise 1 auth scheme at a time, so client 26273e230dd2SCorentin Chary * must pick the one we sent. Verify this */ 26287e7e2ebcSDaniel P. Berrange if (data[0] != vs->auth) { /* Reject auth */ 26297364dbdaSDaniel P. Berrange trace_vnc_auth_reject(vs, vs->auth, (int)data[0]); 26304347e638SRichard Henderson authentication_failed(vs); 26313e230dd2SCorentin Chary } else { /* Accept requested auth */ 26327364dbdaSDaniel P. Berrange trace_vnc_auth_start(vs, vs->auth); 26337e7e2ebcSDaniel P. Berrange switch (vs->auth) { 26343e230dd2SCorentin Chary case VNC_AUTH_NONE: 26353e230dd2SCorentin Chary if (vs->minor >= 8) { 26363e230dd2SCorentin Chary vnc_write_u32(vs, 0); /* Accept auth completion */ 26373e230dd2SCorentin Chary vnc_flush(vs); 26383e230dd2SCorentin Chary } 26397364dbdaSDaniel P. Berrange trace_vnc_auth_pass(vs, vs->auth); 26403e230dd2SCorentin Chary start_client_init(vs); 26413e230dd2SCorentin Chary break; 26423e230dd2SCorentin Chary 26433e230dd2SCorentin Chary case VNC_AUTH_VNC: 26443e230dd2SCorentin Chary start_auth_vnc(vs); 26453e230dd2SCorentin Chary break; 26463e230dd2SCorentin Chary 26473e230dd2SCorentin Chary case VNC_AUTH_VENCRYPT: 26483e230dd2SCorentin Chary start_auth_vencrypt(vs); 26493e230dd2SCorentin Chary break; 26503e230dd2SCorentin Chary 26513e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 26523e230dd2SCorentin Chary case VNC_AUTH_SASL: 26533e230dd2SCorentin Chary start_auth_sasl(vs); 26543e230dd2SCorentin Chary break; 26553e230dd2SCorentin Chary #endif /* CONFIG_VNC_SASL */ 26563e230dd2SCorentin Chary 26573e230dd2SCorentin Chary default: /* Should not be possible, but just in case */ 26587364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, "Unhandled auth method", ""); 26594347e638SRichard Henderson authentication_failed(vs); 26603e230dd2SCorentin Chary } 26613e230dd2SCorentin Chary } 26623e230dd2SCorentin Chary return 0; 26633e230dd2SCorentin Chary } 26643e230dd2SCorentin Chary 26653e230dd2SCorentin Chary static int protocol_version(VncState *vs, uint8_t *version, size_t len) 26663e230dd2SCorentin Chary { 26673e230dd2SCorentin Chary char local[13]; 26683e230dd2SCorentin Chary 26693e230dd2SCorentin Chary memcpy(local, version, 12); 26703e230dd2SCorentin Chary local[12] = 0; 26713e230dd2SCorentin Chary 26723e230dd2SCorentin Chary if (sscanf(local, "RFB %03d.%03d\n", &vs->major, &vs->minor) != 2) { 26733e230dd2SCorentin Chary VNC_DEBUG("Malformed protocol version %s\n", local); 26743e230dd2SCorentin Chary vnc_client_error(vs); 26753e230dd2SCorentin Chary return 0; 26763e230dd2SCorentin Chary } 26773e230dd2SCorentin Chary VNC_DEBUG("Client request protocol version %d.%d\n", vs->major, vs->minor); 26783e230dd2SCorentin Chary if (vs->major != 3 || 26793e230dd2SCorentin Chary (vs->minor != 3 && 26803e230dd2SCorentin Chary vs->minor != 4 && 26813e230dd2SCorentin Chary vs->minor != 5 && 26823e230dd2SCorentin Chary vs->minor != 7 && 26833e230dd2SCorentin Chary vs->minor != 8)) { 26843e230dd2SCorentin Chary VNC_DEBUG("Unsupported client version\n"); 26853e230dd2SCorentin Chary vnc_write_u32(vs, VNC_AUTH_INVALID); 26863e230dd2SCorentin Chary vnc_flush(vs); 26873e230dd2SCorentin Chary vnc_client_error(vs); 26883e230dd2SCorentin Chary return 0; 26893e230dd2SCorentin Chary } 26903e230dd2SCorentin Chary /* Some broken clients report v3.4 or v3.5, which spec requires to be treated 26913e230dd2SCorentin Chary * as equivalent to v3.3 by servers 26923e230dd2SCorentin Chary */ 26933e230dd2SCorentin Chary if (vs->minor == 4 || vs->minor == 5) 26943e230dd2SCorentin Chary vs->minor = 3; 26953e230dd2SCorentin Chary 26963e230dd2SCorentin Chary if (vs->minor == 3) { 26977364dbdaSDaniel P. Berrange trace_vnc_auth_start(vs, vs->auth); 26987e7e2ebcSDaniel P. Berrange if (vs->auth == VNC_AUTH_NONE) { 26997e7e2ebcSDaniel P. Berrange vnc_write_u32(vs, vs->auth); 27003e230dd2SCorentin Chary vnc_flush(vs); 27017364dbdaSDaniel P. Berrange trace_vnc_auth_pass(vs, vs->auth); 27023e230dd2SCorentin Chary start_client_init(vs); 27037e7e2ebcSDaniel P. Berrange } else if (vs->auth == VNC_AUTH_VNC) { 27043e230dd2SCorentin Chary VNC_DEBUG("Tell client VNC auth\n"); 27057e7e2ebcSDaniel P. Berrange vnc_write_u32(vs, vs->auth); 27063e230dd2SCorentin Chary vnc_flush(vs); 27073e230dd2SCorentin Chary start_auth_vnc(vs); 27083e230dd2SCorentin Chary } else { 27097364dbdaSDaniel P. Berrange trace_vnc_auth_fail(vs, vs->auth, 27107364dbdaSDaniel P. Berrange "Unsupported auth method for v3.3", ""); 27113e230dd2SCorentin Chary vnc_write_u32(vs, VNC_AUTH_INVALID); 27123e230dd2SCorentin Chary vnc_flush(vs); 27133e230dd2SCorentin Chary vnc_client_error(vs); 27143e230dd2SCorentin Chary } 27153e230dd2SCorentin Chary } else { 27163e230dd2SCorentin Chary vnc_write_u8(vs, 1); /* num auth */ 27177e7e2ebcSDaniel P. Berrange vnc_write_u8(vs, vs->auth); 27183e230dd2SCorentin Chary vnc_read_when(vs, protocol_client_auth, 1); 27193e230dd2SCorentin Chary vnc_flush(vs); 27203e230dd2SCorentin Chary } 27213e230dd2SCorentin Chary 27223e230dd2SCorentin Chary return 0; 27233e230dd2SCorentin Chary } 27243e230dd2SCorentin Chary 2725999342a0SCorentin Chary static VncRectStat *vnc_stat_rect(VncDisplay *vd, int x, int y) 2726999342a0SCorentin Chary { 2727999342a0SCorentin Chary struct VncSurface *vs = &vd->guest; 2728999342a0SCorentin Chary 2729999342a0SCorentin Chary return &vs->stats[y / VNC_STAT_RECT][x / VNC_STAT_RECT]; 2730999342a0SCorentin Chary } 2731999342a0SCorentin Chary 27327d964c9dSCorentin Chary void vnc_sent_lossy_rect(VncState *vs, int x, int y, int w, int h) 27337d964c9dSCorentin Chary { 27347d964c9dSCorentin Chary int i, j; 27357d964c9dSCorentin Chary 27367d964c9dSCorentin Chary w = (x + w) / VNC_STAT_RECT; 27377d964c9dSCorentin Chary h = (y + h) / VNC_STAT_RECT; 27387d964c9dSCorentin Chary x /= VNC_STAT_RECT; 27397d964c9dSCorentin Chary y /= VNC_STAT_RECT; 27407d964c9dSCorentin Chary 2741207f328aSCorentin Chary for (j = y; j <= h; j++) { 2742207f328aSCorentin Chary for (i = x; i <= w; i++) { 27437d964c9dSCorentin Chary vs->lossy_rect[j][i] = 1; 27447d964c9dSCorentin Chary } 27457d964c9dSCorentin Chary } 27467d964c9dSCorentin Chary } 27477d964c9dSCorentin Chary 27487d964c9dSCorentin Chary static int vnc_refresh_lossy_rect(VncDisplay *vd, int x, int y) 27497d964c9dSCorentin Chary { 27507d964c9dSCorentin Chary VncState *vs; 27517d964c9dSCorentin Chary int sty = y / VNC_STAT_RECT; 27527d964c9dSCorentin Chary int stx = x / VNC_STAT_RECT; 27537d964c9dSCorentin Chary int has_dirty = 0; 27547d964c9dSCorentin Chary 27555a3804dbSMarc-André Lureau y = QEMU_ALIGN_DOWN(y, VNC_STAT_RECT); 27565a3804dbSMarc-André Lureau x = QEMU_ALIGN_DOWN(x, VNC_STAT_RECT); 27577d964c9dSCorentin Chary 27587d964c9dSCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 2759bc2429b9SCorentin Chary int j; 27607d964c9dSCorentin Chary 27617d964c9dSCorentin Chary /* kernel send buffers are full -> refresh later */ 27627d964c9dSCorentin Chary if (vs->output.offset) { 27637d964c9dSCorentin Chary continue; 27647d964c9dSCorentin Chary } 27657d964c9dSCorentin Chary 27667d964c9dSCorentin Chary if (!vs->lossy_rect[sty][stx]) { 27677d964c9dSCorentin Chary continue; 27687d964c9dSCorentin Chary } 2769207f328aSCorentin Chary 27707d964c9dSCorentin Chary vs->lossy_rect[sty][stx] = 0; 27717d964c9dSCorentin Chary for (j = 0; j < VNC_STAT_RECT; ++j) { 2772b4c85ddcSPeter Lieven bitmap_set(vs->dirty[y + j], 2773b4c85ddcSPeter Lieven x / VNC_DIRTY_PIXELS_PER_BIT, 2774b4c85ddcSPeter Lieven VNC_STAT_RECT / VNC_DIRTY_PIXELS_PER_BIT); 27757d964c9dSCorentin Chary } 27767d964c9dSCorentin Chary has_dirty++; 27777d964c9dSCorentin Chary } 2778207f328aSCorentin Chary 27797d964c9dSCorentin Chary return has_dirty; 27807d964c9dSCorentin Chary } 27817d964c9dSCorentin Chary 27827d964c9dSCorentin Chary static int vnc_update_stats(VncDisplay *vd, struct timeval * tv) 2783999342a0SCorentin Chary { 2784eebe0b79SGerd Hoffmann int width = MIN(pixman_image_get_width(vd->guest.fb), 2785eebe0b79SGerd Hoffmann pixman_image_get_width(vd->server)); 2786eebe0b79SGerd Hoffmann int height = MIN(pixman_image_get_height(vd->guest.fb), 2787eebe0b79SGerd Hoffmann pixman_image_get_height(vd->server)); 2788999342a0SCorentin Chary int x, y; 2789999342a0SCorentin Chary struct timeval res; 27907d964c9dSCorentin Chary int has_dirty = 0; 2791999342a0SCorentin Chary 27929f64916dSGerd Hoffmann for (y = 0; y < height; y += VNC_STAT_RECT) { 27939f64916dSGerd Hoffmann for (x = 0; x < width; x += VNC_STAT_RECT) { 2794999342a0SCorentin Chary VncRectStat *rect = vnc_stat_rect(vd, x, y); 2795999342a0SCorentin Chary 2796999342a0SCorentin Chary rect->updated = false; 2797999342a0SCorentin Chary } 2798999342a0SCorentin Chary } 2799999342a0SCorentin Chary 2800ad620c29SBlue Swirl qemu_timersub(tv, &VNC_REFRESH_STATS, &res); 2801999342a0SCorentin Chary 2802999342a0SCorentin Chary if (timercmp(&vd->guest.last_freq_check, &res, >)) { 28037d964c9dSCorentin Chary return has_dirty; 2804999342a0SCorentin Chary } 2805999342a0SCorentin Chary vd->guest.last_freq_check = *tv; 2806999342a0SCorentin Chary 28079f64916dSGerd Hoffmann for (y = 0; y < height; y += VNC_STAT_RECT) { 28089f64916dSGerd Hoffmann for (x = 0; x < width; x += VNC_STAT_RECT) { 2809999342a0SCorentin Chary VncRectStat *rect= vnc_stat_rect(vd, x, y); 2810999342a0SCorentin Chary int count = ARRAY_SIZE(rect->times); 2811999342a0SCorentin Chary struct timeval min, max; 2812999342a0SCorentin Chary 2813999342a0SCorentin Chary if (!timerisset(&rect->times[count - 1])) { 2814999342a0SCorentin Chary continue ; 2815999342a0SCorentin Chary } 2816999342a0SCorentin Chary 2817999342a0SCorentin Chary max = rect->times[(rect->idx + count - 1) % count]; 2818ad620c29SBlue Swirl qemu_timersub(tv, &max, &res); 2819999342a0SCorentin Chary 2820999342a0SCorentin Chary if (timercmp(&res, &VNC_REFRESH_LOSSY, >)) { 2821999342a0SCorentin Chary rect->freq = 0; 28227d964c9dSCorentin Chary has_dirty += vnc_refresh_lossy_rect(vd, x, y); 2823999342a0SCorentin Chary memset(rect->times, 0, sizeof (rect->times)); 2824999342a0SCorentin Chary continue ; 2825999342a0SCorentin Chary } 2826999342a0SCorentin Chary 2827999342a0SCorentin Chary min = rect->times[rect->idx]; 2828999342a0SCorentin Chary max = rect->times[(rect->idx + count - 1) % count]; 2829ad620c29SBlue Swirl qemu_timersub(&max, &min, &res); 2830999342a0SCorentin Chary 2831999342a0SCorentin Chary rect->freq = res.tv_sec + res.tv_usec / 1000000.; 2832999342a0SCorentin Chary rect->freq /= count; 2833999342a0SCorentin Chary rect->freq = 1. / rect->freq; 2834999342a0SCorentin Chary } 2835999342a0SCorentin Chary } 28367d964c9dSCorentin Chary return has_dirty; 2837999342a0SCorentin Chary } 2838999342a0SCorentin Chary 2839999342a0SCorentin Chary double vnc_update_freq(VncState *vs, int x, int y, int w, int h) 2840999342a0SCorentin Chary { 2841999342a0SCorentin Chary int i, j; 2842999342a0SCorentin Chary double total = 0; 2843999342a0SCorentin Chary int num = 0; 2844999342a0SCorentin Chary 28455a3804dbSMarc-André Lureau x = QEMU_ALIGN_DOWN(x, VNC_STAT_RECT); 28465a3804dbSMarc-André Lureau y = QEMU_ALIGN_DOWN(y, VNC_STAT_RECT); 2847999342a0SCorentin Chary 2848999342a0SCorentin Chary for (j = y; j <= y + h; j += VNC_STAT_RECT) { 2849999342a0SCorentin Chary for (i = x; i <= x + w; i += VNC_STAT_RECT) { 2850999342a0SCorentin Chary total += vnc_stat_rect(vs->vd, i, j)->freq; 2851999342a0SCorentin Chary num++; 2852999342a0SCorentin Chary } 2853999342a0SCorentin Chary } 2854999342a0SCorentin Chary 2855999342a0SCorentin Chary if (num) { 2856999342a0SCorentin Chary return total / num; 2857999342a0SCorentin Chary } else { 2858999342a0SCorentin Chary return 0; 2859999342a0SCorentin Chary } 2860999342a0SCorentin Chary } 2861999342a0SCorentin Chary 2862999342a0SCorentin Chary static void vnc_rect_updated(VncDisplay *vd, int x, int y, struct timeval * tv) 2863999342a0SCorentin Chary { 2864999342a0SCorentin Chary VncRectStat *rect; 2865999342a0SCorentin Chary 2866999342a0SCorentin Chary rect = vnc_stat_rect(vd, x, y); 2867999342a0SCorentin Chary if (rect->updated) { 2868999342a0SCorentin Chary return ; 2869999342a0SCorentin Chary } 2870999342a0SCorentin Chary rect->times[rect->idx] = *tv; 2871999342a0SCorentin Chary rect->idx = (rect->idx + 1) % ARRAY_SIZE(rect->times); 2872999342a0SCorentin Chary rect->updated = true; 2873999342a0SCorentin Chary } 2874999342a0SCorentin Chary 28753e230dd2SCorentin Chary static int vnc_refresh_server_surface(VncDisplay *vd) 28763e230dd2SCorentin Chary { 2877bea60dd7SPeter Lieven int width = MIN(pixman_image_get_width(vd->guest.fb), 2878bea60dd7SPeter Lieven pixman_image_get_width(vd->server)); 2879bea60dd7SPeter Lieven int height = MIN(pixman_image_get_height(vd->guest.fb), 2880bea60dd7SPeter Lieven pixman_image_get_height(vd->server)); 2881eb8934b0SGerd Hoffmann int cmp_bytes, server_stride, line_bytes, guest_ll, guest_stride, y = 0; 288212b316d4SPeter Lieven uint8_t *guest_row0 = NULL, *server_row0; 28833e230dd2SCorentin Chary VncState *vs; 28843e230dd2SCorentin Chary int has_dirty = 0; 28859f64916dSGerd Hoffmann pixman_image_t *tmpbuf = NULL; 28863e230dd2SCorentin Chary 288780e0c8c3SCorentin Chary struct timeval tv = { 0, 0 }; 2888999342a0SCorentin Chary 288980e0c8c3SCorentin Chary if (!vd->non_adaptive) { 2890999342a0SCorentin Chary gettimeofday(&tv, NULL); 28917d964c9dSCorentin Chary has_dirty = vnc_update_stats(vd, &tv); 289280e0c8c3SCorentin Chary } 2893999342a0SCorentin Chary 28943e230dd2SCorentin Chary /* 28953e230dd2SCorentin Chary * Walk through the guest dirty map. 28963e230dd2SCorentin Chary * Check and copy modified bits from guest to server surface. 28973e230dd2SCorentin Chary * Update server dirty map. 28983e230dd2SCorentin Chary */ 2899bea60dd7SPeter Lieven server_row0 = (uint8_t *)pixman_image_get_data(vd->server); 2900eb8934b0SGerd Hoffmann server_stride = guest_stride = guest_ll = 2901eb8934b0SGerd Hoffmann pixman_image_get_stride(vd->server); 2902bea60dd7SPeter Lieven cmp_bytes = MIN(VNC_DIRTY_PIXELS_PER_BIT * VNC_SERVER_FB_BYTES, 2903bea60dd7SPeter Lieven server_stride); 29049f64916dSGerd Hoffmann if (vd->guest.format != VNC_SERVER_FB_FORMAT) { 29059f64916dSGerd Hoffmann int width = pixman_image_get_width(vd->server); 29069f64916dSGerd Hoffmann tmpbuf = qemu_pixman_linebuf_create(VNC_SERVER_FB_FORMAT, width); 290712b316d4SPeter Lieven } else { 2908eb8934b0SGerd Hoffmann int guest_bpp = 2909eb8934b0SGerd Hoffmann PIXMAN_FORMAT_BPP(pixman_image_get_format(vd->guest.fb)); 291012b316d4SPeter Lieven guest_row0 = (uint8_t *)pixman_image_get_data(vd->guest.fb); 291112b316d4SPeter Lieven guest_stride = pixman_image_get_stride(vd->guest.fb); 2912949ed4c2SPhilippe Mathieu-Daudé guest_ll = pixman_image_get_width(vd->guest.fb) 2913949ed4c2SPhilippe Mathieu-Daudé * DIV_ROUND_UP(guest_bpp, 8); 29149f64916dSGerd Hoffmann } 2915eb8934b0SGerd Hoffmann line_bytes = MIN(server_stride, guest_ll); 291612b316d4SPeter Lieven 291712b316d4SPeter Lieven for (;;) { 29183e230dd2SCorentin Chary int x; 291912b316d4SPeter Lieven uint8_t *guest_ptr, *server_ptr; 292012b316d4SPeter Lieven unsigned long offset = find_next_bit((unsigned long *) &vd->guest.dirty, 292112b316d4SPeter Lieven height * VNC_DIRTY_BPL(&vd->guest), 292212b316d4SPeter Lieven y * VNC_DIRTY_BPL(&vd->guest)); 292312b316d4SPeter Lieven if (offset == height * VNC_DIRTY_BPL(&vd->guest)) { 292412b316d4SPeter Lieven /* no more dirty bits */ 292512b316d4SPeter Lieven break; 292612b316d4SPeter Lieven } 292712b316d4SPeter Lieven y = offset / VNC_DIRTY_BPL(&vd->guest); 292812b316d4SPeter Lieven x = offset % VNC_DIRTY_BPL(&vd->guest); 292912b316d4SPeter Lieven 293012b316d4SPeter Lieven server_ptr = server_row0 + y * server_stride + x * cmp_bytes; 29313e230dd2SCorentin Chary 29329f64916dSGerd Hoffmann if (vd->guest.format != VNC_SERVER_FB_FORMAT) { 2933bc210eb1SGerd Hoffmann qemu_pixman_linebuf_fill(tmpbuf, vd->guest.fb, width, 0, y); 29349f64916dSGerd Hoffmann guest_ptr = (uint8_t *)pixman_image_get_data(tmpbuf); 29359f64916dSGerd Hoffmann } else { 293612b316d4SPeter Lieven guest_ptr = guest_row0 + y * guest_stride; 29379f64916dSGerd Hoffmann } 293812b316d4SPeter Lieven guest_ptr += x * cmp_bytes; 29393e230dd2SCorentin Chary 294012b316d4SPeter Lieven for (; x < DIV_ROUND_UP(width, VNC_DIRTY_PIXELS_PER_BIT); 294112b316d4SPeter Lieven x++, guest_ptr += cmp_bytes, server_ptr += cmp_bytes) { 2942bea60dd7SPeter Lieven int _cmp_bytes = cmp_bytes; 294312b316d4SPeter Lieven if (!test_and_clear_bit(x, vd->guest.dirty[y])) { 29443e230dd2SCorentin Chary continue; 2945b4c85ddcSPeter Lieven } 2946eb8934b0SGerd Hoffmann if ((x + 1) * cmp_bytes > line_bytes) { 2947eb8934b0SGerd Hoffmann _cmp_bytes = line_bytes - x * cmp_bytes; 2948bea60dd7SPeter Lieven } 2949eb8934b0SGerd Hoffmann assert(_cmp_bytes >= 0); 2950bea60dd7SPeter Lieven if (memcmp(server_ptr, guest_ptr, _cmp_bytes) == 0) { 29513e230dd2SCorentin Chary continue; 2952b4c85ddcSPeter Lieven } 2953bea60dd7SPeter Lieven memcpy(server_ptr, guest_ptr, _cmp_bytes); 295412b316d4SPeter Lieven if (!vd->non_adaptive) { 295512b316d4SPeter Lieven vnc_rect_updated(vd, x * VNC_DIRTY_PIXELS_PER_BIT, 295612b316d4SPeter Lieven y, &tv); 295712b316d4SPeter Lieven } 29583e230dd2SCorentin Chary QTAILQ_FOREACH(vs, &vd->clients, next) { 295912b316d4SPeter Lieven set_bit(x, vs->dirty[y]); 29603e230dd2SCorentin Chary } 29613e230dd2SCorentin Chary has_dirty++; 29623e230dd2SCorentin Chary } 296312b316d4SPeter Lieven 296412b316d4SPeter Lieven y++; 29653e230dd2SCorentin Chary } 29669f64916dSGerd Hoffmann qemu_pixman_image_unref(tmpbuf); 29673e230dd2SCorentin Chary return has_dirty; 29683e230dd2SCorentin Chary } 29693e230dd2SCorentin Chary 29700f7b2864SGerd Hoffmann static void vnc_refresh(DisplayChangeListener *dcl) 29713e230dd2SCorentin Chary { 29720f7b2864SGerd Hoffmann VncDisplay *vd = container_of(dcl, VncDisplay, dcl); 29733e230dd2SCorentin Chary VncState *vs, *vn; 29743e230dd2SCorentin Chary int has_dirty, rects = 0; 29753e230dd2SCorentin Chary 29769d6b2070SChenLiang if (QTAILQ_EMPTY(&vd->clients)) { 29779d6b2070SChenLiang update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_MAX); 29789d6b2070SChenLiang return; 29799d6b2070SChenLiang } 29809d6b2070SChenLiang 29811d0d59feSGerd Hoffmann graphic_hw_update(vd->dcl.con); 29823e230dd2SCorentin Chary 2983bd023f95SCorentin Chary if (vnc_trylock_display(vd)) { 29840f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 2985bd023f95SCorentin Chary return; 2986bd023f95SCorentin Chary } 2987bd023f95SCorentin Chary 29883e230dd2SCorentin Chary has_dirty = vnc_refresh_server_surface(vd); 2989bd023f95SCorentin Chary vnc_unlock_display(vd); 29903e230dd2SCorentin Chary 29913e230dd2SCorentin Chary QTAILQ_FOREACH_SAFE(vs, &vd->clients, next, vn) { 29926af998dbSDaniel P. Berrange rects += vnc_update_client(vs, has_dirty); 29933e230dd2SCorentin Chary /* vs might be free()ed here */ 29943e230dd2SCorentin Chary } 2995bd023f95SCorentin Chary 29963e230dd2SCorentin Chary if (has_dirty && rects) { 29970f7b2864SGerd Hoffmann vd->dcl.update_interval /= 2; 29980f7b2864SGerd Hoffmann if (vd->dcl.update_interval < VNC_REFRESH_INTERVAL_BASE) { 29990f7b2864SGerd Hoffmann vd->dcl.update_interval = VNC_REFRESH_INTERVAL_BASE; 30000f7b2864SGerd Hoffmann } 30013e230dd2SCorentin Chary } else { 30020f7b2864SGerd Hoffmann vd->dcl.update_interval += VNC_REFRESH_INTERVAL_INC; 30030f7b2864SGerd Hoffmann if (vd->dcl.update_interval > VNC_REFRESH_INTERVAL_MAX) { 30040f7b2864SGerd Hoffmann vd->dcl.update_interval = VNC_REFRESH_INTERVAL_MAX; 30053e230dd2SCorentin Chary } 30063e230dd2SCorentin Chary } 30073e230dd2SCorentin Chary } 30083e230dd2SCorentin Chary 300904d2529dSDaniel P. Berrange static void vnc_connect(VncDisplay *vd, QIOChannelSocket *sioc, 30102c8cf549SMichael Tokarev bool skipauth, bool websocket) 30113e230dd2SCorentin Chary { 3012fedf0d35SMarkus Armbruster VncState *vs = g_new0(VncState, 1); 301390cd03a3SDaniel P. Berrange bool first_client = QTAILQ_EMPTY(&vd->clients); 30147d964c9dSCorentin Chary int i; 30157d964c9dSCorentin Chary 3016ad6374c4SDaniel P. Berrange trace_vnc_client_connect(vs, sioc); 30176bf21f3dSLi Qiang vs->zrle = g_new0(VncZrle, 1); 30186bf21f3dSLi Qiang vs->tight = g_new0(VncTight, 1); 3019f31f9c10SGerd Hoffmann vs->magic = VNC_MAGIC; 302004d2529dSDaniel P. Berrange vs->sioc = sioc; 302104d2529dSDaniel P. Berrange object_ref(OBJECT(vs->sioc)); 302204d2529dSDaniel P. Berrange vs->ioc = QIO_CHANNEL(sioc); 302304d2529dSDaniel P. Berrange object_ref(OBJECT(vs->ioc)); 3024d616ccc5SGerd Hoffmann vs->vd = vd; 30257e7e2ebcSDaniel P. Berrange 302604d2529dSDaniel P. Berrange buffer_init(&vs->input, "vnc-input/%p", sioc); 302704d2529dSDaniel P. Berrange buffer_init(&vs->output, "vnc-output/%p", sioc); 302804d2529dSDaniel P. Berrange buffer_init(&vs->jobs_buffer, "vnc-jobs_buffer/%p", sioc); 3029543b9580SGerd Hoffmann 30306bf21f3dSLi Qiang buffer_init(&vs->tight->tight, "vnc-tight/%p", sioc); 30316bf21f3dSLi Qiang buffer_init(&vs->tight->zlib, "vnc-tight-zlib/%p", sioc); 30326bf21f3dSLi Qiang buffer_init(&vs->tight->gradient, "vnc-tight-gradient/%p", sioc); 3033543b9580SGerd Hoffmann #ifdef CONFIG_VNC_JPEG 30346bf21f3dSLi Qiang buffer_init(&vs->tight->jpeg, "vnc-tight-jpeg/%p", sioc); 3035543b9580SGerd Hoffmann #endif 3036543b9580SGerd Hoffmann #ifdef CONFIG_VNC_PNG 30376bf21f3dSLi Qiang buffer_init(&vs->tight->png, "vnc-tight-png/%p", sioc); 3038543b9580SGerd Hoffmann #endif 303904d2529dSDaniel P. Berrange buffer_init(&vs->zlib.zlib, "vnc-zlib/%p", sioc); 30406bf21f3dSLi Qiang buffer_init(&vs->zrle->zrle, "vnc-zrle/%p", sioc); 30416bf21f3dSLi Qiang buffer_init(&vs->zrle->fb, "vnc-zrle-fb/%p", sioc); 30426bf21f3dSLi Qiang buffer_init(&vs->zrle->zlib, "vnc-zrle-zlib/%p", sioc); 3043543b9580SGerd Hoffmann 30447e7e2ebcSDaniel P. Berrange if (skipauth) { 30457e7e2ebcSDaniel P. Berrange vs->auth = VNC_AUTH_NONE; 30467e7e2ebcSDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 30477e7e2ebcSDaniel P. Berrange } else { 3048f9148c8aSDaniel P. Berrange if (websocket) { 3049f9148c8aSDaniel P. Berrange vs->auth = vd->ws_auth; 3050f9148c8aSDaniel P. Berrange vs->subauth = VNC_AUTH_INVALID; 3051f9148c8aSDaniel P. Berrange } else { 30527e7e2ebcSDaniel P. Berrange vs->auth = vd->auth; 30537e7e2ebcSDaniel P. Berrange vs->subauth = vd->subauth; 30547e7e2ebcSDaniel P. Berrange } 3055f9148c8aSDaniel P. Berrange } 305604d2529dSDaniel P. Berrange VNC_DEBUG("Client sioc=%p ws=%d auth=%d subauth=%d\n", 305704d2529dSDaniel P. Berrange sioc, websocket, vs->auth, vs->subauth); 30587e7e2ebcSDaniel P. Berrange 30597267c094SAnthony Liguori vs->lossy_rect = g_malloc0(VNC_STAT_ROWS * sizeof (*vs->lossy_rect)); 30607d964c9dSCorentin Chary for (i = 0; i < VNC_STAT_ROWS; ++i) { 3061fedf0d35SMarkus Armbruster vs->lossy_rect[i] = g_new0(uint8_t, VNC_STAT_COLS); 30627d964c9dSCorentin Chary } 30633e230dd2SCorentin Chary 306404d2529dSDaniel P. Berrange VNC_DEBUG("New client on socket %p\n", vs->sioc); 30650f7b2864SGerd Hoffmann update_displaychangelistener(&vd->dcl, VNC_REFRESH_INTERVAL_BASE); 306604d2529dSDaniel P. Berrange qio_channel_set_blocking(vs->ioc, false, NULL); 3067a75d6f07SBrandon Carpenter if (vs->ioc_tag) { 3068a75d6f07SBrandon Carpenter g_source_remove(vs->ioc_tag); 3069a75d6f07SBrandon Carpenter } 30707536ee4bSTim Hardeck if (websocket) { 30717536ee4bSTim Hardeck vs->websocket = 1; 307238e5756aSDaniel P. Berrange if (vd->tlscreds) { 307304d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 307404d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vncws_tls_handshake_io, vs, NULL); 30753e305e4aSDaniel P. Berrange } else { 307604d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 307704d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vncws_handshake_io, vs, NULL); 30780057a0d5STim Hardeck } 307904d2529dSDaniel P. Berrange } else { 308004d2529dSDaniel P. Berrange vs->ioc_tag = qio_channel_add_watch( 308104d2529dSDaniel P. Berrange vs->ioc, G_IO_IN, vnc_client_io, vs, NULL); 30827536ee4bSTim Hardeck } 30833e230dd2SCorentin Chary 30843e230dd2SCorentin Chary vnc_client_cache_addr(vs); 3085fb6ba0d5SWenchao Xia vnc_qmp_event(vs, QAPI_EVENT_VNC_CONNECTED); 30868cf36489SGerd Hoffmann vnc_set_share_mode(vs, VNC_SHARE_MODE_CONNECTING); 30873e230dd2SCorentin Chary 30883e230dd2SCorentin Chary vs->last_x = -1; 30893e230dd2SCorentin Chary vs->last_y = -1; 30903e230dd2SCorentin Chary 30913e230dd2SCorentin Chary vs->as.freq = 44100; 30923e230dd2SCorentin Chary vs->as.nchannels = 2; 309385bc5852SKővágó, Zoltán vs->as.fmt = AUDIO_FORMAT_S16; 30943e230dd2SCorentin Chary vs->as.endianness = 0; 30953e230dd2SCorentin Chary 3096bd023f95SCorentin Chary qemu_mutex_init(&vs->output_mutex); 3097175b2a6eSCorentin Chary vs->bh = qemu_bh_new(vnc_jobs_bh, vs); 3098bd023f95SCorentin Chary 3099e5f34cddSGerd Hoffmann QTAILQ_INSERT_TAIL(&vd->clients, vs, next); 3100c7628bffSGerd Hoffmann if (first_client) { 3101c7628bffSGerd Hoffmann vnc_update_server_surface(vd); 3102c7628bffSGerd Hoffmann } 31033e230dd2SCorentin Chary 31041d0d59feSGerd Hoffmann graphic_hw_update(vd->dcl.con); 31053e230dd2SCorentin Chary 310690cd03a3SDaniel P. Berrange if (!vs->websocket) { 3107dbee9897SDaniel P. Berrange vnc_start_protocol(vs); 310890cd03a3SDaniel P. Berrange } 310990cd03a3SDaniel P. Berrange 311090cd03a3SDaniel P. Berrange if (vd->num_connecting > vd->connections_limit) { 311190cd03a3SDaniel P. Berrange QTAILQ_FOREACH(vs, &vd->clients, next) { 311290cd03a3SDaniel P. Berrange if (vs->share_mode == VNC_SHARE_MODE_CONNECTING) { 311390cd03a3SDaniel P. Berrange vnc_disconnect_start(vs); 311490cd03a3SDaniel P. Berrange return; 311590cd03a3SDaniel P. Berrange } 311690cd03a3SDaniel P. Berrange } 311790cd03a3SDaniel P. Berrange } 311890cd03a3SDaniel P. Berrange } 311990cd03a3SDaniel P. Berrange 3120dbee9897SDaniel P. Berrange void vnc_start_protocol(VncState *vs) 312190cd03a3SDaniel P. Berrange { 31223e230dd2SCorentin Chary vnc_write(vs, "RFB 003.008\n", 12); 31233e230dd2SCorentin Chary vnc_flush(vs); 31243e230dd2SCorentin Chary vnc_read_when(vs, protocol_version, 12); 31253e230dd2SCorentin Chary 31263e230dd2SCorentin Chary vs->mouse_mode_notifier.notify = check_pointer_type_change; 31273e230dd2SCorentin Chary qemu_add_mouse_mode_change_notifier(&vs->mouse_mode_notifier); 31283e230dd2SCorentin Chary } 31293e230dd2SCorentin Chary 313013e1d0e7SDaniel P. Berrange static void vnc_listen_io(QIONetListener *listener, 313113e1d0e7SDaniel P. Berrange QIOChannelSocket *cioc, 313204d2529dSDaniel P. Berrange void *opaque) 31333e230dd2SCorentin Chary { 3134bf01c179SDaniel P. Berrange VncDisplay *vd = opaque; 313513e1d0e7SDaniel P. Berrange bool isWebsock = listener == vd->wslistener; 31364ee74fa7SDaniel P. Berrange 313713e1d0e7SDaniel P. Berrange qio_channel_set_name(QIO_CHANNEL(cioc), 31384ee74fa7SDaniel P. Berrange isWebsock ? "vnc-ws-server" : "vnc-server"); 313913e1d0e7SDaniel P. Berrange qio_channel_set_delay(QIO_CHANNEL(cioc), false); 314013e1d0e7SDaniel P. Berrange vnc_connect(vd, cioc, false, isWebsock); 31417536ee4bSTim Hardeck } 31427536ee4bSTim Hardeck 31437c20b4a3SGerd Hoffmann static const DisplayChangeListenerOps dcl_ops = { 31447c20b4a3SGerd Hoffmann .dpy_name = "vnc", 31450f7b2864SGerd Hoffmann .dpy_refresh = vnc_refresh, 31467c20b4a3SGerd Hoffmann .dpy_gfx_update = vnc_dpy_update, 3147c12aeb86SGerd Hoffmann .dpy_gfx_switch = vnc_dpy_switch, 314834da30afSBenjamin Herrenschmidt .dpy_gfx_check_format = qemu_pixman_check_format, 31497c20b4a3SGerd Hoffmann .dpy_mouse_set = vnc_mouse_set, 31507c20b4a3SGerd Hoffmann .dpy_cursor_define = vnc_dpy_cursor_define, 31517c20b4a3SGerd Hoffmann }; 31527c20b4a3SGerd Hoffmann 3153ab4f931eSFei Li void vnc_display_init(const char *id, Error **errp) 31543e230dd2SCorentin Chary { 3155bf01c179SDaniel P. Berrange VncDisplay *vd; 31564db14629SGerd Hoffmann 31574db14629SGerd Hoffmann if (vnc_display_find(id) != NULL) { 31584db14629SGerd Hoffmann return; 31594db14629SGerd Hoffmann } 3160bf01c179SDaniel P. Berrange vd = g_malloc0(sizeof(*vd)); 31613e230dd2SCorentin Chary 3162bf01c179SDaniel P. Berrange vd->id = strdup(id); 3163bf01c179SDaniel P. Berrange QTAILQ_INSERT_TAIL(&vnc_displays, vd, next); 31643e230dd2SCorentin Chary 3165bf01c179SDaniel P. Berrange QTAILQ_INIT(&vd->clients); 3166bf01c179SDaniel P. Berrange vd->expires = TIME_MAX; 31673e230dd2SCorentin Chary 316840066175SGerd Hoffmann if (keyboard_layout) { 316940066175SGerd Hoffmann trace_vnc_key_map_init(keyboard_layout); 3170ab4f931eSFei Li vd->kbd_layout = init_keyboard_layout(name2keysym, 3171ab4f931eSFei Li keyboard_layout, errp); 317240066175SGerd Hoffmann } else { 3173ab4f931eSFei Li vd->kbd_layout = init_keyboard_layout(name2keysym, "en-us", errp); 317440066175SGerd Hoffmann } 31753e230dd2SCorentin Chary 3176bf01c179SDaniel P. Berrange if (!vd->kbd_layout) { 3177ab4f931eSFei Li return; 3178bf01c179SDaniel P. Berrange } 31793e230dd2SCorentin Chary 3180bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 3181bf01c179SDaniel P. Berrange vd->connections_limit = 32; 318212e29b16SDaniel P. Berrange 3183bf01c179SDaniel P. Berrange qemu_mutex_init(&vd->mutex); 3184bd023f95SCorentin Chary vnc_start_worker_thread(); 3185bd023f95SCorentin Chary 3186bf01c179SDaniel P. Berrange vd->dcl.ops = &dcl_ops; 3187bf01c179SDaniel P. Berrange register_displaychangelistener(&vd->dcl); 3188c2f2ba49SGerd Hoffmann vd->kbd = qkbd_state_init(vd->dcl.con); 31893e230dd2SCorentin Chary } 31903e230dd2SCorentin Chary 31913e230dd2SCorentin Chary 3192bf01c179SDaniel P. Berrange static void vnc_display_close(VncDisplay *vd) 31933e230dd2SCorentin Chary { 3194bf01c179SDaniel P. Berrange if (!vd) { 31953e230dd2SCorentin Chary return; 319604d2529dSDaniel P. Berrange } 3197bf01c179SDaniel P. Berrange vd->is_unix = false; 31984ee74fa7SDaniel P. Berrange 319913e1d0e7SDaniel P. Berrange if (vd->listener) { 320013e1d0e7SDaniel P. Berrange qio_net_listener_disconnect(vd->listener); 320113e1d0e7SDaniel P. Berrange object_unref(OBJECT(vd->listener)); 320204d2529dSDaniel P. Berrange } 320313e1d0e7SDaniel P. Berrange vd->listener = NULL; 320413e1d0e7SDaniel P. Berrange 320513e1d0e7SDaniel P. Berrange if (vd->wslistener) { 320613e1d0e7SDaniel P. Berrange qio_net_listener_disconnect(vd->wslistener); 320713e1d0e7SDaniel P. Berrange object_unref(OBJECT(vd->wslistener)); 32087536ee4bSTim Hardeck } 320913e1d0e7SDaniel P. Berrange vd->wslistener = NULL; 32104ee74fa7SDaniel P. Berrange 3211bf01c179SDaniel P. Berrange vd->auth = VNC_AUTH_INVALID; 3212bf01c179SDaniel P. Berrange vd->subauth = VNC_AUTH_INVALID; 3213bf01c179SDaniel P. Berrange if (vd->tlscreds) { 3214bf01c179SDaniel P. Berrange object_unparent(OBJECT(vd->tlscreds)); 3215bf01c179SDaniel P. Berrange vd->tlscreds = NULL; 3216bf01c179SDaniel P. Berrange } 3217b76806d4SDaniel P. Berrange if (vd->tlsauthz) { 3218b76806d4SDaniel P. Berrange object_unparent(OBJECT(vd->tlsauthz)); 3219b76806d4SDaniel P. Berrange vd->tlsauthz = NULL; 3220b76806d4SDaniel P. Berrange } 3221b76806d4SDaniel P. Berrange g_free(vd->tlsauthzid); 3222b76806d4SDaniel P. Berrange vd->tlsauthzid = NULL; 3223a54f0d2bSPierre Ossman if (vd->lock_key_sync) { 3224a54f0d2bSPierre Ossman qemu_remove_led_event_handler(vd->led); 32252dc120beSGerd Hoffmann vd->led = NULL; 3226a54f0d2bSPierre Ossman } 3227b76806d4SDaniel P. Berrange #ifdef CONFIG_VNC_SASL 3228b76806d4SDaniel P. Berrange if (vd->sasl.authz) { 3229b76806d4SDaniel P. Berrange object_unparent(OBJECT(vd->sasl.authz)); 3230b76806d4SDaniel P. Berrange vd->sasl.authz = NULL; 3231b76806d4SDaniel P. Berrange } 3232b76806d4SDaniel P. Berrange g_free(vd->sasl.authzid); 3233b76806d4SDaniel P. Berrange vd->sasl.authzid = NULL; 3234b76806d4SDaniel P. Berrange #endif 32353e230dd2SCorentin Chary } 32363e230dd2SCorentin Chary 323714f7143eSGerd Hoffmann int vnc_display_password(const char *id, const char *password) 32381cd20f8bSAnthony Liguori { 3239bf01c179SDaniel P. Berrange VncDisplay *vd = vnc_display_find(id); 32401cd20f8bSAnthony Liguori 3241bf01c179SDaniel P. Berrange if (!vd) { 3242a6aa9d3eSLuiz Capitulino return -EINVAL; 32431cd20f8bSAnthony Liguori } 3244bf01c179SDaniel P. Berrange if (vd->auth == VNC_AUTH_NONE) { 3245cf864569SGerd Hoffmann error_printf_unless_qmp("If you want use passwords please enable " 32467ea7d36eSMarkus Armbruster "password auth using '-vnc ${dpy},password'.\n"); 3247cf864569SGerd Hoffmann return -EINVAL; 32481cd20f8bSAnthony Liguori } 32491cd20f8bSAnthony Liguori 3250bf01c179SDaniel P. Berrange g_free(vd->password); 3251bf01c179SDaniel P. Berrange vd->password = g_strdup(password); 3252a6aa9d3eSLuiz Capitulino 3253a6aa9d3eSLuiz Capitulino return 0; 32543e230dd2SCorentin Chary } 32553e230dd2SCorentin Chary 325614f7143eSGerd Hoffmann int vnc_display_pw_expire(const char *id, time_t expires) 32573c9405a0SGerd Hoffmann { 3258bf01c179SDaniel P. Berrange VncDisplay *vd = vnc_display_find(id); 32593c9405a0SGerd Hoffmann 3260bf01c179SDaniel P. Berrange if (!vd) { 32611643f2b2SGerd Hoffmann return -EINVAL; 32621643f2b2SGerd Hoffmann } 32631643f2b2SGerd Hoffmann 3264bf01c179SDaniel P. Berrange vd->expires = expires; 32653c9405a0SGerd Hoffmann return 0; 32663c9405a0SGerd Hoffmann } 32673c9405a0SGerd Hoffmann 3268bf01c179SDaniel P. Berrange static void vnc_display_print_local_addr(VncDisplay *vd) 32693e230dd2SCorentin Chary { 3270bd269ebcSMarkus Armbruster SocketAddress *addr; 327104d2529dSDaniel P. Berrange Error *err = NULL; 32723e230dd2SCorentin Chary 327313e1d0e7SDaniel P. Berrange if (!vd->listener || !vd->listener->nsioc) { 32744ee74fa7SDaniel P. Berrange return; 32754ee74fa7SDaniel P. Berrange } 32764ee74fa7SDaniel P. Berrange 327713e1d0e7SDaniel P. Berrange addr = qio_channel_socket_get_local_address(vd->listener->sioc[0], &err); 327804d2529dSDaniel P. Berrange if (!addr) { 327933df7bf3SPaolo Bonzini return; 328004d2529dSDaniel P. Berrange } 328104d2529dSDaniel P. Berrange 3282bd269ebcSMarkus Armbruster if (addr->type != SOCKET_ADDRESS_TYPE_INET) { 3283bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 328433df7bf3SPaolo Bonzini return; 328504d2529dSDaniel P. Berrange } 328633df7bf3SPaolo Bonzini error_printf_unless_qmp("VNC server running on %s:%s\n", 3287bd269ebcSMarkus Armbruster addr->u.inet.host, 3288bd269ebcSMarkus Armbruster addr->u.inet.port); 3289bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 32903e230dd2SCorentin Chary } 32913e230dd2SCorentin Chary 32924db14629SGerd Hoffmann static QemuOptsList qemu_vnc_opts = { 32934db14629SGerd Hoffmann .name = "vnc", 32944db14629SGerd Hoffmann .head = QTAILQ_HEAD_INITIALIZER(qemu_vnc_opts.head), 32954db14629SGerd Hoffmann .implied_opt_name = "vnc", 32964db14629SGerd Hoffmann .desc = { 32974db14629SGerd Hoffmann { 32984db14629SGerd Hoffmann .name = "vnc", 32994db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 33004db14629SGerd Hoffmann },{ 33014db14629SGerd Hoffmann .name = "websocket", 33024db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 33034db14629SGerd Hoffmann },{ 33043e305e4aSDaniel P. Berrange .name = "tls-creds", 33053e305e4aSDaniel P. Berrange .type = QEMU_OPT_STRING, 33063e305e4aSDaniel P. Berrange },{ 33074db14629SGerd Hoffmann .name = "share", 33084db14629SGerd Hoffmann .type = QEMU_OPT_STRING, 33094db14629SGerd Hoffmann },{ 33101d0d59feSGerd Hoffmann .name = "display", 33111d0d59feSGerd Hoffmann .type = QEMU_OPT_STRING, 33121d0d59feSGerd Hoffmann },{ 33131d0d59feSGerd Hoffmann .name = "head", 33141d0d59feSGerd Hoffmann .type = QEMU_OPT_NUMBER, 33151d0d59feSGerd Hoffmann },{ 3316e5f34cddSGerd Hoffmann .name = "connections", 3317e5f34cddSGerd Hoffmann .type = QEMU_OPT_NUMBER, 3318e5f34cddSGerd Hoffmann },{ 331988428b7aSGonglei .name = "to", 332088428b7aSGonglei .type = QEMU_OPT_NUMBER, 332188428b7aSGonglei },{ 332288428b7aSGonglei .name = "ipv4", 332388428b7aSGonglei .type = QEMU_OPT_BOOL, 332488428b7aSGonglei },{ 332588428b7aSGonglei .name = "ipv6", 332688428b7aSGonglei .type = QEMU_OPT_BOOL, 332788428b7aSGonglei },{ 33284db14629SGerd Hoffmann .name = "password", 33294db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 33304db14629SGerd Hoffmann },{ 33314db14629SGerd Hoffmann .name = "reverse", 33324db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 33334db14629SGerd Hoffmann },{ 33344db14629SGerd Hoffmann .name = "lock-key-sync", 33354db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 33364db14629SGerd Hoffmann },{ 3337c5ce8333SGerd Hoffmann .name = "key-delay-ms", 3338c5ce8333SGerd Hoffmann .type = QEMU_OPT_NUMBER, 3339c5ce8333SGerd Hoffmann },{ 33404db14629SGerd Hoffmann .name = "sasl", 33414db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 33424db14629SGerd Hoffmann },{ 33434db14629SGerd Hoffmann .name = "acl", 33444db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 33454db14629SGerd Hoffmann },{ 334655cf09a0SDaniel P. Berrange .name = "tls-authz", 334755cf09a0SDaniel P. Berrange .type = QEMU_OPT_STRING, 334855cf09a0SDaniel P. Berrange },{ 334955cf09a0SDaniel P. Berrange .name = "sasl-authz", 335055cf09a0SDaniel P. Berrange .type = QEMU_OPT_STRING, 335155cf09a0SDaniel P. Berrange },{ 33524db14629SGerd Hoffmann .name = "lossy", 33534db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 33544db14629SGerd Hoffmann },{ 33554db14629SGerd Hoffmann .name = "non-adaptive", 33564db14629SGerd Hoffmann .type = QEMU_OPT_BOOL, 3357f0b9f36dSKővágó, Zoltán },{ 3358f0b9f36dSKővágó, Zoltán .name = "audiodev", 3359f0b9f36dSKővágó, Zoltán .type = QEMU_OPT_STRING, 33604db14629SGerd Hoffmann }, 33614db14629SGerd Hoffmann { /* end of list */ } 33624db14629SGerd Hoffmann }, 33634db14629SGerd Hoffmann }; 33644db14629SGerd Hoffmann 33650dd72e15SDaniel P. Berrange 33663e305e4aSDaniel P. Berrange static int 3367eda24e18SDaniel P. Berrange vnc_display_setup_auth(int *auth, 3368eda24e18SDaniel P. Berrange int *subauth, 3369eda24e18SDaniel P. Berrange QCryptoTLSCreds *tlscreds, 33700dd72e15SDaniel P. Berrange bool password, 33710dd72e15SDaniel P. Berrange bool sasl, 33723e305e4aSDaniel P. Berrange bool websocket, 33733e305e4aSDaniel P. Berrange Error **errp) 33740dd72e15SDaniel P. Berrange { 33750dd72e15SDaniel P. Berrange /* 33760dd72e15SDaniel P. Berrange * We have a choice of 3 authentication options 33770dd72e15SDaniel P. Berrange * 33780dd72e15SDaniel P. Berrange * 1. none 33790dd72e15SDaniel P. Berrange * 2. vnc 33800dd72e15SDaniel P. Berrange * 3. sasl 33810dd72e15SDaniel P. Berrange * 33820dd72e15SDaniel P. Berrange * The channel can be run in 2 modes 33830dd72e15SDaniel P. Berrange * 33840dd72e15SDaniel P. Berrange * 1. clear 33850dd72e15SDaniel P. Berrange * 2. tls 33860dd72e15SDaniel P. Berrange * 33870dd72e15SDaniel P. Berrange * And TLS can use 2 types of credentials 33880dd72e15SDaniel P. Berrange * 33890dd72e15SDaniel P. Berrange * 1. anon 33900dd72e15SDaniel P. Berrange * 2. x509 33910dd72e15SDaniel P. Berrange * 33920dd72e15SDaniel P. Berrange * We thus have 9 possible logical combinations 33930dd72e15SDaniel P. Berrange * 33940dd72e15SDaniel P. Berrange * 1. clear + none 33950dd72e15SDaniel P. Berrange * 2. clear + vnc 33960dd72e15SDaniel P. Berrange * 3. clear + sasl 33970dd72e15SDaniel P. Berrange * 4. tls + anon + none 33980dd72e15SDaniel P. Berrange * 5. tls + anon + vnc 33990dd72e15SDaniel P. Berrange * 6. tls + anon + sasl 34000dd72e15SDaniel P. Berrange * 7. tls + x509 + none 34010dd72e15SDaniel P. Berrange * 8. tls + x509 + vnc 34020dd72e15SDaniel P. Berrange * 9. tls + x509 + sasl 34030dd72e15SDaniel P. Berrange * 34040dd72e15SDaniel P. Berrange * These need to be mapped into the VNC auth schemes 34050dd72e15SDaniel P. Berrange * in an appropriate manner. In regular VNC, all the 34060dd72e15SDaniel P. Berrange * TLS options get mapped into VNC_AUTH_VENCRYPT 34070dd72e15SDaniel P. Berrange * sub-auth types. 3408f9148c8aSDaniel P. Berrange * 3409f9148c8aSDaniel P. Berrange * In websockets, the https:// protocol already provides 3410f9148c8aSDaniel P. Berrange * TLS support, so there is no need to make use of the 3411f9148c8aSDaniel P. Berrange * VeNCrypt extension. Furthermore, websockets browser 3412f9148c8aSDaniel P. Berrange * clients could not use VeNCrypt even if they wanted to, 3413f9148c8aSDaniel P. Berrange * as they cannot control when the TLS handshake takes 3414f9148c8aSDaniel P. Berrange * place. Thus there is no option but to rely on https://, 3415f9148c8aSDaniel P. Berrange * meaning combinations 4->6 and 7->9 will be mapped to 3416f9148c8aSDaniel P. Berrange * VNC auth schemes in the same way as combos 1->3. 3417f9148c8aSDaniel P. Berrange * 3418f9148c8aSDaniel P. Berrange * Regardless of fact that we have a different mapping to 3419f9148c8aSDaniel P. Berrange * VNC auth mechs for plain VNC vs websockets VNC, the end 3420f9148c8aSDaniel P. Berrange * result has the same security characteristics. 34210dd72e15SDaniel P. Berrange */ 3422eda24e18SDaniel P. Berrange if (websocket || !tlscreds) { 34230dd72e15SDaniel P. Berrange if (password) { 34240dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with password auth\n"); 3425eda24e18SDaniel P. Berrange *auth = VNC_AUTH_VNC; 34260dd72e15SDaniel P. Berrange } else if (sasl) { 34270dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with SASL auth\n"); 3428eda24e18SDaniel P. Berrange *auth = VNC_AUTH_SASL; 34290dd72e15SDaniel P. Berrange } else { 34300dd72e15SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with no auth\n"); 3431eda24e18SDaniel P. Berrange *auth = VNC_AUTH_NONE; 34320dd72e15SDaniel P. Berrange } 3433eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_INVALID; 3434f9148c8aSDaniel P. Berrange } else { 3435eda24e18SDaniel P. Berrange bool is_x509 = object_dynamic_cast(OBJECT(tlscreds), 3436eda24e18SDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS_X509) != NULL; 3437eda24e18SDaniel P. Berrange bool is_anon = object_dynamic_cast(OBJECT(tlscreds), 3438eda24e18SDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS_ANON) != NULL; 3439eda24e18SDaniel P. Berrange 3440eda24e18SDaniel P. Berrange if (!is_x509 && !is_anon) { 3441eda24e18SDaniel P. Berrange error_setg(errp, 3442eda24e18SDaniel P. Berrange "Unsupported TLS cred type %s", 3443eda24e18SDaniel P. Berrange object_get_typename(OBJECT(tlscreds))); 3444eda24e18SDaniel P. Berrange return -1; 3445eda24e18SDaniel P. Berrange } 3446eda24e18SDaniel P. Berrange *auth = VNC_AUTH_VENCRYPT; 3447eda24e18SDaniel P. Berrange if (password) { 3448eda24e18SDaniel P. Berrange if (is_x509) { 3449eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with x509 password auth\n"); 3450eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_X509VNC; 3451eda24e18SDaniel P. Berrange } else { 3452eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with TLS password auth\n"); 3453eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_TLSVNC; 3454eda24e18SDaniel P. Berrange } 3455eda24e18SDaniel P. Berrange 3456eda24e18SDaniel P. Berrange } else if (sasl) { 3457eda24e18SDaniel P. Berrange if (is_x509) { 3458eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with x509 SASL auth\n"); 3459eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_X509SASL; 3460eda24e18SDaniel P. Berrange } else { 3461eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with TLS SASL auth\n"); 3462eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_TLSSASL; 3463eda24e18SDaniel P. Berrange } 3464eda24e18SDaniel P. Berrange } else { 3465eda24e18SDaniel P. Berrange if (is_x509) { 3466eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with x509 no auth\n"); 3467eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_X509NONE; 3468eda24e18SDaniel P. Berrange } else { 3469eda24e18SDaniel P. Berrange VNC_DEBUG("Initializing VNC server with TLS no auth\n"); 3470eda24e18SDaniel P. Berrange *subauth = VNC_AUTH_VENCRYPT_TLSNONE; 3471eda24e18SDaniel P. Berrange } 3472f9148c8aSDaniel P. Berrange } 34730dd72e15SDaniel P. Berrange } 34743e305e4aSDaniel P. Berrange return 0; 34750dd72e15SDaniel P. Berrange } 34760dd72e15SDaniel P. Berrange 34773e305e4aSDaniel P. Berrange 3478275e0d61SDaniel P. Berrange static int vnc_display_get_address(const char *addrstr, 3479275e0d61SDaniel P. Berrange bool websocket, 3480e5766eb4SGerd Hoffmann bool reverse, 3481275e0d61SDaniel P. Berrange int displaynum, 3482275e0d61SDaniel P. Berrange int to, 3483275e0d61SDaniel P. Berrange bool has_ipv4, 3484275e0d61SDaniel P. Berrange bool has_ipv6, 3485275e0d61SDaniel P. Berrange bool ipv4, 3486275e0d61SDaniel P. Berrange bool ipv6, 3487bd269ebcSMarkus Armbruster SocketAddress **retaddr, 3488275e0d61SDaniel P. Berrange Error **errp) 3489275e0d61SDaniel P. Berrange { 3490275e0d61SDaniel P. Berrange int ret = -1; 3491bd269ebcSMarkus Armbruster SocketAddress *addr = NULL; 3492275e0d61SDaniel P. Berrange 3493bd269ebcSMarkus Armbruster addr = g_new0(SocketAddress, 1); 3494275e0d61SDaniel P. Berrange 3495275e0d61SDaniel P. Berrange if (strncmp(addrstr, "unix:", 5) == 0) { 3496bd269ebcSMarkus Armbruster addr->type = SOCKET_ADDRESS_TYPE_UNIX; 3497bd269ebcSMarkus Armbruster addr->u.q_unix.path = g_strdup(addrstr + 5); 3498275e0d61SDaniel P. Berrange 3499275e0d61SDaniel P. Berrange if (websocket) { 3500275e0d61SDaniel P. Berrange error_setg(errp, "UNIX sockets not supported with websock"); 3501275e0d61SDaniel P. Berrange goto cleanup; 3502275e0d61SDaniel P. Berrange } 3503275e0d61SDaniel P. Berrange 3504275e0d61SDaniel P. Berrange if (to) { 3505275e0d61SDaniel P. Berrange error_setg(errp, "Port range not support with UNIX socket"); 3506275e0d61SDaniel P. Berrange goto cleanup; 3507275e0d61SDaniel P. Berrange } 3508275e0d61SDaniel P. Berrange ret = 0; 3509275e0d61SDaniel P. Berrange } else { 3510275e0d61SDaniel P. Berrange const char *port; 3511275e0d61SDaniel P. Berrange size_t hostlen; 3512275e0d61SDaniel P. Berrange unsigned long long baseport = 0; 3513275e0d61SDaniel P. Berrange InetSocketAddress *inet; 3514275e0d61SDaniel P. Berrange 3515275e0d61SDaniel P. Berrange port = strrchr(addrstr, ':'); 3516275e0d61SDaniel P. Berrange if (!port) { 3517275e0d61SDaniel P. Berrange if (websocket) { 3518275e0d61SDaniel P. Berrange hostlen = 0; 3519275e0d61SDaniel P. Berrange port = addrstr; 3520275e0d61SDaniel P. Berrange } else { 3521275e0d61SDaniel P. Berrange error_setg(errp, "no vnc port specified"); 3522275e0d61SDaniel P. Berrange goto cleanup; 3523275e0d61SDaniel P. Berrange } 3524275e0d61SDaniel P. Berrange } else { 3525275e0d61SDaniel P. Berrange hostlen = port - addrstr; 3526275e0d61SDaniel P. Berrange port++; 3527275e0d61SDaniel P. Berrange if (*port == '\0') { 3528275e0d61SDaniel P. Berrange error_setg(errp, "vnc port cannot be empty"); 3529275e0d61SDaniel P. Berrange goto cleanup; 3530275e0d61SDaniel P. Berrange } 3531275e0d61SDaniel P. Berrange } 3532275e0d61SDaniel P. Berrange 3533bd269ebcSMarkus Armbruster addr->type = SOCKET_ADDRESS_TYPE_INET; 3534bd269ebcSMarkus Armbruster inet = &addr->u.inet; 3535275e0d61SDaniel P. Berrange if (addrstr[0] == '[' && addrstr[hostlen - 1] == ']') { 3536275e0d61SDaniel P. Berrange inet->host = g_strndup(addrstr + 1, hostlen - 2); 3537275e0d61SDaniel P. Berrange } else { 3538275e0d61SDaniel P. Berrange inet->host = g_strndup(addrstr, hostlen); 3539275e0d61SDaniel P. Berrange } 3540275e0d61SDaniel P. Berrange /* plain VNC port is just an offset, for websocket 3541275e0d61SDaniel P. Berrange * port is absolute */ 3542275e0d61SDaniel P. Berrange if (websocket) { 3543275e0d61SDaniel P. Berrange if (g_str_equal(addrstr, "") || 3544275e0d61SDaniel P. Berrange g_str_equal(addrstr, "on")) { 3545396f935aSDaniel P. Berrange if (displaynum == -1) { 3546396f935aSDaniel P. Berrange error_setg(errp, "explicit websocket port is required"); 3547396f935aSDaniel P. Berrange goto cleanup; 3548396f935aSDaniel P. Berrange } 3549275e0d61SDaniel P. Berrange inet->port = g_strdup_printf( 3550275e0d61SDaniel P. Berrange "%d", displaynum + 5700); 3551275e0d61SDaniel P. Berrange if (to) { 3552275e0d61SDaniel P. Berrange inet->has_to = true; 3553275e0d61SDaniel P. Berrange inet->to = to + 5700; 3554275e0d61SDaniel P. Berrange } 3555275e0d61SDaniel P. Berrange } else { 3556275e0d61SDaniel P. Berrange inet->port = g_strdup(port); 3557275e0d61SDaniel P. Berrange } 3558275e0d61SDaniel P. Berrange } else { 3559e5766eb4SGerd Hoffmann int offset = reverse ? 0 : 5900; 3560275e0d61SDaniel P. Berrange if (parse_uint_full(port, &baseport, 10) < 0) { 3561275e0d61SDaniel P. Berrange error_setg(errp, "can't convert to a number: %s", port); 3562275e0d61SDaniel P. Berrange goto cleanup; 3563275e0d61SDaniel P. Berrange } 3564275e0d61SDaniel P. Berrange if (baseport > 65535 || 3565e5766eb4SGerd Hoffmann baseport + offset > 65535) { 3566275e0d61SDaniel P. Berrange error_setg(errp, "port %s out of range", port); 3567275e0d61SDaniel P. Berrange goto cleanup; 3568275e0d61SDaniel P. Berrange } 3569275e0d61SDaniel P. Berrange inet->port = g_strdup_printf( 3570e5766eb4SGerd Hoffmann "%d", (int)baseport + offset); 3571275e0d61SDaniel P. Berrange 3572275e0d61SDaniel P. Berrange if (to) { 3573275e0d61SDaniel P. Berrange inet->has_to = true; 3574e5766eb4SGerd Hoffmann inet->to = to + offset; 3575275e0d61SDaniel P. Berrange } 3576275e0d61SDaniel P. Berrange } 3577275e0d61SDaniel P. Berrange 3578275e0d61SDaniel P. Berrange inet->ipv4 = ipv4; 3579275e0d61SDaniel P. Berrange inet->has_ipv4 = has_ipv4; 3580275e0d61SDaniel P. Berrange inet->ipv6 = ipv6; 3581275e0d61SDaniel P. Berrange inet->has_ipv6 = has_ipv6; 3582275e0d61SDaniel P. Berrange 3583275e0d61SDaniel P. Berrange ret = baseport; 3584275e0d61SDaniel P. Berrange } 3585275e0d61SDaniel P. Berrange 3586275e0d61SDaniel P. Berrange *retaddr = addr; 3587275e0d61SDaniel P. Berrange 3588275e0d61SDaniel P. Berrange cleanup: 3589275e0d61SDaniel P. Berrange if (ret < 0) { 3590bd269ebcSMarkus Armbruster qapi_free_SocketAddress(addr); 3591275e0d61SDaniel P. Berrange } 3592275e0d61SDaniel P. Berrange return ret; 3593275e0d61SDaniel P. Berrange } 3594275e0d61SDaniel P. Berrange 35959f26f325SPhilippe Mathieu-Daudé static void vnc_free_addresses(SocketAddress ***retsaddr, 35969f26f325SPhilippe Mathieu-Daudé size_t *retnsaddr) 35979f26f325SPhilippe Mathieu-Daudé { 35989f26f325SPhilippe Mathieu-Daudé size_t i; 35999f26f325SPhilippe Mathieu-Daudé 36009f26f325SPhilippe Mathieu-Daudé for (i = 0; i < *retnsaddr; i++) { 36019f26f325SPhilippe Mathieu-Daudé qapi_free_SocketAddress((*retsaddr)[i]); 36029f26f325SPhilippe Mathieu-Daudé } 36039f26f325SPhilippe Mathieu-Daudé g_free(*retsaddr); 36049f26f325SPhilippe Mathieu-Daudé 36059f26f325SPhilippe Mathieu-Daudé *retsaddr = NULL; 36069f26f325SPhilippe Mathieu-Daudé *retnsaddr = 0; 36079f26f325SPhilippe Mathieu-Daudé } 36089f26f325SPhilippe Mathieu-Daudé 3609275e0d61SDaniel P. Berrange static int vnc_display_get_addresses(QemuOpts *opts, 3610e5766eb4SGerd Hoffmann bool reverse, 3611bd269ebcSMarkus Armbruster SocketAddress ***retsaddr, 3612396f935aSDaniel P. Berrange size_t *retnsaddr, 3613bd269ebcSMarkus Armbruster SocketAddress ***retwsaddr, 3614396f935aSDaniel P. Berrange size_t *retnwsaddr, 3615275e0d61SDaniel P. Berrange Error **errp) 3616275e0d61SDaniel P. Berrange { 3617bd269ebcSMarkus Armbruster SocketAddress *saddr = NULL; 3618bd269ebcSMarkus Armbruster SocketAddress *wsaddr = NULL; 3619396f935aSDaniel P. Berrange QemuOptsIter addriter; 3620396f935aSDaniel P. Berrange const char *addr; 3621275e0d61SDaniel P. Berrange int to = qemu_opt_get_number(opts, "to", 0); 3622275e0d61SDaniel P. Berrange bool has_ipv4 = qemu_opt_get(opts, "ipv4"); 3623275e0d61SDaniel P. Berrange bool has_ipv6 = qemu_opt_get(opts, "ipv6"); 3624275e0d61SDaniel P. Berrange bool ipv4 = qemu_opt_get_bool(opts, "ipv4", false); 3625275e0d61SDaniel P. Berrange bool ipv6 = qemu_opt_get_bool(opts, "ipv6", false); 3626396f935aSDaniel P. Berrange int displaynum = -1; 3627396f935aSDaniel P. Berrange int ret = -1; 3628275e0d61SDaniel P. Berrange 3629275e0d61SDaniel P. Berrange *retsaddr = NULL; 3630396f935aSDaniel P. Berrange *retnsaddr = 0; 3631275e0d61SDaniel P. Berrange *retwsaddr = NULL; 3632396f935aSDaniel P. Berrange *retnwsaddr = 0; 3633275e0d61SDaniel P. Berrange 3634396f935aSDaniel P. Berrange addr = qemu_opt_get(opts, "vnc"); 3635396f935aSDaniel P. Berrange if (addr == NULL || g_str_equal(addr, "none")) { 3636396f935aSDaniel P. Berrange ret = 0; 3637396f935aSDaniel P. Berrange goto cleanup; 3638396f935aSDaniel P. Berrange } 3639396f935aSDaniel P. Berrange if (qemu_opt_get(opts, "websocket") && 3640275e0d61SDaniel P. Berrange !qcrypto_hash_supports(QCRYPTO_HASH_ALG_SHA1)) { 3641275e0d61SDaniel P. Berrange error_setg(errp, 3642275e0d61SDaniel P. Berrange "SHA1 hash support is required for websockets"); 3643396f935aSDaniel P. Berrange goto cleanup; 3644275e0d61SDaniel P. Berrange } 3645275e0d61SDaniel P. Berrange 3646396f935aSDaniel P. Berrange qemu_opt_iter_init(&addriter, opts, "vnc"); 3647396f935aSDaniel P. Berrange while ((addr = qemu_opt_iter_next(&addriter)) != NULL) { 3648396f935aSDaniel P. Berrange int rv; 3649e5766eb4SGerd Hoffmann rv = vnc_display_get_address(addr, false, reverse, 0, to, 3650275e0d61SDaniel P. Berrange has_ipv4, has_ipv6, 3651275e0d61SDaniel P. Berrange ipv4, ipv6, 3652275e0d61SDaniel P. Berrange &saddr, errp); 3653396f935aSDaniel P. Berrange if (rv < 0) { 3654396f935aSDaniel P. Berrange goto cleanup; 3655275e0d61SDaniel P. Berrange } 3656396f935aSDaniel P. Berrange /* Historical compat - first listen address can be used 3657396f935aSDaniel P. Berrange * to set the default websocket port 3658396f935aSDaniel P. Berrange */ 3659396f935aSDaniel P. Berrange if (displaynum == -1) { 3660396f935aSDaniel P. Berrange displaynum = rv; 3661396f935aSDaniel P. Berrange } 3662bd269ebcSMarkus Armbruster *retsaddr = g_renew(SocketAddress *, *retsaddr, *retnsaddr + 1); 3663396f935aSDaniel P. Berrange (*retsaddr)[(*retnsaddr)++] = saddr; 3664396f935aSDaniel P. Berrange } 3665396f935aSDaniel P. Berrange 3666396f935aSDaniel P. Berrange /* If we had multiple primary displays, we don't do defaults 3667396f935aSDaniel P. Berrange * for websocket, and require explicit config instead. */ 3668396f935aSDaniel P. Berrange if (*retnsaddr > 1) { 3669396f935aSDaniel P. Berrange displaynum = -1; 3670396f935aSDaniel P. Berrange } 3671396f935aSDaniel P. Berrange 3672396f935aSDaniel P. Berrange qemu_opt_iter_init(&addriter, opts, "websocket"); 3673396f935aSDaniel P. Berrange while ((addr = qemu_opt_iter_next(&addriter)) != NULL) { 3674e5766eb4SGerd Hoffmann if (vnc_display_get_address(addr, true, reverse, displaynum, to, 3675275e0d61SDaniel P. Berrange has_ipv4, has_ipv6, 3676275e0d61SDaniel P. Berrange ipv4, ipv6, 3677275e0d61SDaniel P. Berrange &wsaddr, errp) < 0) { 3678396f935aSDaniel P. Berrange goto cleanup; 3679275e0d61SDaniel P. Berrange } 3680396f935aSDaniel P. Berrange 3681396f935aSDaniel P. Berrange /* Historical compat - if only a single listen address was 3682396f935aSDaniel P. Berrange * provided, then this is used to set the default listen 3683396f935aSDaniel P. Berrange * address for websocket too 3684396f935aSDaniel P. Berrange */ 3685396f935aSDaniel P. Berrange if (*retnsaddr == 1 && 3686bd269ebcSMarkus Armbruster (*retsaddr)[0]->type == SOCKET_ADDRESS_TYPE_INET && 3687bd269ebcSMarkus Armbruster wsaddr->type == SOCKET_ADDRESS_TYPE_INET && 3688bd269ebcSMarkus Armbruster g_str_equal(wsaddr->u.inet.host, "") && 3689bd269ebcSMarkus Armbruster !g_str_equal((*retsaddr)[0]->u.inet.host, "")) { 3690bd269ebcSMarkus Armbruster g_free(wsaddr->u.inet.host); 3691bd269ebcSMarkus Armbruster wsaddr->u.inet.host = g_strdup((*retsaddr)[0]->u.inet.host); 3692275e0d61SDaniel P. Berrange } 3693275e0d61SDaniel P. Berrange 3694bd269ebcSMarkus Armbruster *retwsaddr = g_renew(SocketAddress *, *retwsaddr, *retnwsaddr + 1); 3695396f935aSDaniel P. Berrange (*retwsaddr)[(*retnwsaddr)++] = wsaddr; 3696396f935aSDaniel P. Berrange } 3697396f935aSDaniel P. Berrange 3698396f935aSDaniel P. Berrange ret = 0; 3699396f935aSDaniel P. Berrange cleanup: 3700396f935aSDaniel P. Berrange if (ret < 0) { 37019f26f325SPhilippe Mathieu-Daudé vnc_free_addresses(retsaddr, retnsaddr); 37029f26f325SPhilippe Mathieu-Daudé vnc_free_addresses(retwsaddr, retnwsaddr); 3703396f935aSDaniel P. Berrange } 3704396f935aSDaniel P. Berrange return ret; 3705275e0d61SDaniel P. Berrange } 3706275e0d61SDaniel P. Berrange 37078bd22f47SDaniel P. Berrange static int vnc_display_connect(VncDisplay *vd, 3708bd269ebcSMarkus Armbruster SocketAddress **saddr, 3709396f935aSDaniel P. Berrange size_t nsaddr, 3710bd269ebcSMarkus Armbruster SocketAddress **wsaddr, 3711396f935aSDaniel P. Berrange size_t nwsaddr, 37128bd22f47SDaniel P. Berrange Error **errp) 37138bd22f47SDaniel P. Berrange { 37148bd22f47SDaniel P. Berrange /* connect to viewer */ 37158bd22f47SDaniel P. Berrange QIOChannelSocket *sioc = NULL; 3716396f935aSDaniel P. Berrange if (nwsaddr != 0) { 37178bd22f47SDaniel P. Berrange error_setg(errp, "Cannot use websockets in reverse mode"); 37188bd22f47SDaniel P. Berrange return -1; 37198bd22f47SDaniel P. Berrange } 3720396f935aSDaniel P. Berrange if (nsaddr != 1) { 3721396f935aSDaniel P. Berrange error_setg(errp, "Expected a single address in reverse mode"); 3722396f935aSDaniel P. Berrange return -1; 3723396f935aSDaniel P. Berrange } 3724bd269ebcSMarkus Armbruster /* TODO SOCKET_ADDRESS_TYPE_FD when fd has AF_UNIX */ 3725bd269ebcSMarkus Armbruster vd->is_unix = saddr[0]->type == SOCKET_ADDRESS_TYPE_UNIX; 37268bd22f47SDaniel P. Berrange sioc = qio_channel_socket_new(); 37278bd22f47SDaniel P. Berrange qio_channel_set_name(QIO_CHANNEL(sioc), "vnc-reverse"); 3728396f935aSDaniel P. Berrange if (qio_channel_socket_connect_sync(sioc, saddr[0], errp) < 0) { 37298bd22f47SDaniel P. Berrange return -1; 37308bd22f47SDaniel P. Berrange } 37318bd22f47SDaniel P. Berrange vnc_connect(vd, sioc, false, false); 37328bd22f47SDaniel P. Berrange object_unref(OBJECT(sioc)); 37338bd22f47SDaniel P. Berrange return 0; 37348bd22f47SDaniel P. Berrange } 37358bd22f47SDaniel P. Berrange 37368bd22f47SDaniel P. Berrange 37378bd22f47SDaniel P. Berrange static int vnc_display_listen(VncDisplay *vd, 3738bd269ebcSMarkus Armbruster SocketAddress **saddr, 3739396f935aSDaniel P. Berrange size_t nsaddr, 3740bd269ebcSMarkus Armbruster SocketAddress **wsaddr, 3741396f935aSDaniel P. Berrange size_t nwsaddr, 37428bd22f47SDaniel P. Berrange Error **errp) 37438bd22f47SDaniel P. Berrange { 3744396f935aSDaniel P. Berrange size_t i; 37458bd22f47SDaniel P. Berrange 374613e1d0e7SDaniel P. Berrange if (nsaddr) { 374713e1d0e7SDaniel P. Berrange vd->listener = qio_net_listener_new(); 374813e1d0e7SDaniel P. Berrange qio_net_listener_set_name(vd->listener, "vnc-listen"); 3749396f935aSDaniel P. Berrange for (i = 0; i < nsaddr; i++) { 375013e1d0e7SDaniel P. Berrange if (qio_net_listener_open_sync(vd->listener, 3751fc8135c6SJuan Quintela saddr[i], 1, 37528bd22f47SDaniel P. Berrange errp) < 0) { 37538bd22f47SDaniel P. Berrange return -1; 37548bd22f47SDaniel P. Berrange } 3755396f935aSDaniel P. Berrange } 375613e1d0e7SDaniel P. Berrange 375713e1d0e7SDaniel P. Berrange qio_net_listener_set_client_func(vd->listener, 375813e1d0e7SDaniel P. Berrange vnc_listen_io, vd, NULL); 375913e1d0e7SDaniel P. Berrange } 376013e1d0e7SDaniel P. Berrange 376113e1d0e7SDaniel P. Berrange if (nwsaddr) { 376213e1d0e7SDaniel P. Berrange vd->wslistener = qio_net_listener_new(); 376313e1d0e7SDaniel P. Berrange qio_net_listener_set_name(vd->wslistener, "vnc-ws-listen"); 3764396f935aSDaniel P. Berrange for (i = 0; i < nwsaddr; i++) { 376513e1d0e7SDaniel P. Berrange if (qio_net_listener_open_sync(vd->wslistener, 3766fc8135c6SJuan Quintela wsaddr[i], 1, 37678bd22f47SDaniel P. Berrange errp) < 0) { 37688bd22f47SDaniel P. Berrange return -1; 37698bd22f47SDaniel P. Berrange } 3770396f935aSDaniel P. Berrange } 37718bd22f47SDaniel P. Berrange 377213e1d0e7SDaniel P. Berrange qio_net_listener_set_client_func(vd->wslistener, 377313e1d0e7SDaniel P. Berrange vnc_listen_io, vd, NULL); 377413e1d0e7SDaniel P. Berrange } 377513e1d0e7SDaniel P. Berrange 37768bd22f47SDaniel P. Berrange return 0; 37778bd22f47SDaniel P. Berrange } 37788bd22f47SDaniel P. Berrange 37798bd22f47SDaniel P. Berrange 37804db14629SGerd Hoffmann void vnc_display_open(const char *id, Error **errp) 37813e230dd2SCorentin Chary { 3782bf01c179SDaniel P. Berrange VncDisplay *vd = vnc_display_find(id); 37834db14629SGerd Hoffmann QemuOpts *opts = qemu_opts_find(&qemu_vnc_opts, id); 3784bd269ebcSMarkus Armbruster SocketAddress **saddr = NULL, **wsaddr = NULL; 3785396f935aSDaniel P. Berrange size_t nsaddr, nwsaddr; 3786e2a11d9dSGonglei const char *share, *device_id; 37871d0d59feSGerd Hoffmann QemuConsole *con; 3788a2c72de0SGonglei bool password = false; 3789a2c72de0SGonglei bool reverse = false; 37903e305e4aSDaniel P. Berrange const char *credid; 3791a2c72de0SGonglei bool sasl = false; 37923e230dd2SCorentin Chary int acl = 0; 379355cf09a0SDaniel P. Berrange const char *tlsauthz; 379455cf09a0SDaniel P. Berrange const char *saslauthz; 37953e230dd2SCorentin Chary int lock_key_sync = 1; 3796c5ce8333SGerd Hoffmann int key_delay_ms; 3797f0b9f36dSKővágó, Zoltán const char *audiodev; 37983e230dd2SCorentin Chary 3799bf01c179SDaniel P. Berrange if (!vd) { 38002d55f0e8SPaolo Bonzini error_setg(errp, "VNC display not active"); 38012d55f0e8SPaolo Bonzini return; 38022d55f0e8SPaolo Bonzini } 3803bf01c179SDaniel P. Berrange vnc_display_close(vd); 38044db14629SGerd Hoffmann 38054db14629SGerd Hoffmann if (!opts) { 38062d55f0e8SPaolo Bonzini return; 38074db14629SGerd Hoffmann } 3808275e0d61SDaniel P. Berrange 3809e5766eb4SGerd Hoffmann reverse = qemu_opt_get_bool(opts, "reverse", false); 3810e5766eb4SGerd Hoffmann if (vnc_display_get_addresses(opts, reverse, &saddr, &nsaddr, 3811396f935aSDaniel P. Berrange &wsaddr, &nwsaddr, errp) < 0) { 3812275e0d61SDaniel P. Berrange goto fail; 3813275e0d61SDaniel P. Berrange } 3814275e0d61SDaniel P. Berrange 38154db14629SGerd Hoffmann password = qemu_opt_get_bool(opts, "password", false); 3816800567a6SDaniel P. Berrange if (password) { 3817800567a6SDaniel P. Berrange if (fips_get_state()) { 38182d55f0e8SPaolo Bonzini error_setg(errp, 38190f66998fSPaul Moore "VNC password auth disabled due to FIPS mode, " 38200f66998fSPaul Moore "consider using the VeNCrypt or SASL authentication " 38212d55f0e8SPaolo Bonzini "methods as an alternative"); 38221ce52c78SPaolo Bonzini goto fail; 38230f66998fSPaul Moore } 3824800567a6SDaniel P. Berrange if (!qcrypto_cipher_supports( 3825f844836dSGonglei QCRYPTO_CIPHER_ALG_DES_RFB, QCRYPTO_CIPHER_MODE_ECB)) { 3826800567a6SDaniel P. Berrange error_setg(errp, 3827800567a6SDaniel P. Berrange "Cipher backend does not support DES RFB algorithm"); 3828800567a6SDaniel P. Berrange goto fail; 3829800567a6SDaniel P. Berrange } 3830800567a6SDaniel P. Berrange } 38314db14629SGerd Hoffmann 38324db14629SGerd Hoffmann lock_key_sync = qemu_opt_get_bool(opts, "lock-key-sync", true); 3833d3b0db6dSAlexander Graf key_delay_ms = qemu_opt_get_number(opts, "key-delay-ms", 10); 38344db14629SGerd Hoffmann sasl = qemu_opt_get_bool(opts, "sasl", false); 3835d169f04bSDaniel P. Berrange #ifndef CONFIG_VNC_SASL 3836d169f04bSDaniel P. Berrange if (sasl) { 3837d169f04bSDaniel P. Berrange error_setg(errp, "VNC SASL auth requires cyrus-sasl support"); 3838d169f04bSDaniel P. Berrange goto fail; 3839d169f04bSDaniel P. Berrange } 3840d169f04bSDaniel P. Berrange #endif /* CONFIG_VNC_SASL */ 38413e305e4aSDaniel P. Berrange credid = qemu_opt_get(opts, "tls-creds"); 38423e305e4aSDaniel P. Berrange if (credid) { 38433e305e4aSDaniel P. Berrange Object *creds; 38443e305e4aSDaniel P. Berrange creds = object_resolve_path_component( 38453e305e4aSDaniel P. Berrange object_get_objects_root(), credid); 38463e305e4aSDaniel P. Berrange if (!creds) { 38473e305e4aSDaniel P. Berrange error_setg(errp, "No TLS credentials with id '%s'", 38483e305e4aSDaniel P. Berrange credid); 38493e305e4aSDaniel P. Berrange goto fail; 38503e305e4aSDaniel P. Berrange } 3851bf01c179SDaniel P. Berrange vd->tlscreds = (QCryptoTLSCreds *) 38523e305e4aSDaniel P. Berrange object_dynamic_cast(creds, 38533e305e4aSDaniel P. Berrange TYPE_QCRYPTO_TLS_CREDS); 3854bf01c179SDaniel P. Berrange if (!vd->tlscreds) { 38553e305e4aSDaniel P. Berrange error_setg(errp, "Object with id '%s' is not TLS credentials", 38563e305e4aSDaniel P. Berrange credid); 38573e305e4aSDaniel P. Berrange goto fail; 38583e305e4aSDaniel P. Berrange } 3859bf01c179SDaniel P. Berrange object_ref(OBJECT(vd->tlscreds)); 38603e305e4aSDaniel P. Berrange 3861bf01c179SDaniel P. Berrange if (vd->tlscreds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) { 38623e305e4aSDaniel P. Berrange error_setg(errp, 38633e305e4aSDaniel P. Berrange "Expecting TLS credentials with a server endpoint"); 38643e305e4aSDaniel P. Berrange goto fail; 38653e305e4aSDaniel P. Berrange } 3866d169f04bSDaniel P. Berrange } 386755cf09a0SDaniel P. Berrange if (qemu_opt_get(opts, "acl")) { 386855cf09a0SDaniel P. Berrange error_report("The 'acl' option to -vnc is deprecated. " 386955cf09a0SDaniel P. Berrange "Please use the 'tls-authz' and 'sasl-authz' " 387055cf09a0SDaniel P. Berrange "options instead"); 387155cf09a0SDaniel P. Berrange } 38724db14629SGerd Hoffmann acl = qemu_opt_get_bool(opts, "acl", false); 387355cf09a0SDaniel P. Berrange tlsauthz = qemu_opt_get(opts, "tls-authz"); 387455cf09a0SDaniel P. Berrange if (acl && tlsauthz) { 387555cf09a0SDaniel P. Berrange error_setg(errp, "'acl' option is mutually exclusive with the " 387655cf09a0SDaniel P. Berrange "'tls-authz' option"); 387755cf09a0SDaniel P. Berrange goto fail; 387855cf09a0SDaniel P. Berrange } 387955cf09a0SDaniel P. Berrange if (tlsauthz && !vd->tlscreds) { 388055cf09a0SDaniel P. Berrange error_setg(errp, "'tls-authz' provided but TLS is not enabled"); 388155cf09a0SDaniel P. Berrange goto fail; 388255cf09a0SDaniel P. Berrange } 388355cf09a0SDaniel P. Berrange 388455cf09a0SDaniel P. Berrange saslauthz = qemu_opt_get(opts, "sasl-authz"); 388555cf09a0SDaniel P. Berrange if (acl && saslauthz) { 388655cf09a0SDaniel P. Berrange error_setg(errp, "'acl' option is mutually exclusive with the " 388755cf09a0SDaniel P. Berrange "'sasl-authz' option"); 388855cf09a0SDaniel P. Berrange goto fail; 388955cf09a0SDaniel P. Berrange } 389055cf09a0SDaniel P. Berrange if (saslauthz && !sasl) { 389155cf09a0SDaniel P. Berrange error_setg(errp, "'sasl-authz' provided but SASL auth is not enabled"); 389255cf09a0SDaniel P. Berrange goto fail; 389355cf09a0SDaniel P. Berrange } 38944db14629SGerd Hoffmann 38954db14629SGerd Hoffmann share = qemu_opt_get(opts, "share"); 38964db14629SGerd Hoffmann if (share) { 38974db14629SGerd Hoffmann if (strcmp(share, "ignore") == 0) { 3898bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_IGNORE; 38994db14629SGerd Hoffmann } else if (strcmp(share, "allow-exclusive") == 0) { 3900bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 39014db14629SGerd Hoffmann } else if (strcmp(share, "force-shared") == 0) { 3902bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_FORCE_SHARED; 39038cf36489SGerd Hoffmann } else { 39042d55f0e8SPaolo Bonzini error_setg(errp, "unknown vnc share= option"); 39051ce52c78SPaolo Bonzini goto fail; 39068cf36489SGerd Hoffmann } 39074db14629SGerd Hoffmann } else { 3908bf01c179SDaniel P. Berrange vd->share_policy = VNC_SHARE_POLICY_ALLOW_EXCLUSIVE; 39093e230dd2SCorentin Chary } 3910bf01c179SDaniel P. Berrange vd->connections_limit = qemu_opt_get_number(opts, "connections", 32); 39113e230dd2SCorentin Chary 39124db14629SGerd Hoffmann #ifdef CONFIG_VNC_JPEG 3913bf01c179SDaniel P. Berrange vd->lossy = qemu_opt_get_bool(opts, "lossy", false); 39144db14629SGerd Hoffmann #endif 3915bf01c179SDaniel P. Berrange vd->non_adaptive = qemu_opt_get_bool(opts, "non-adaptive", false); 3916e22492d3SPeter Lieven /* adaptive updates are only used with tight encoding and 3917e22492d3SPeter Lieven * if lossy updates are enabled so we can disable all the 3918e22492d3SPeter Lieven * calculations otherwise */ 3919bf01c179SDaniel P. Berrange if (!vd->lossy) { 3920bf01c179SDaniel P. Berrange vd->non_adaptive = true; 3921e22492d3SPeter Lieven } 3922e22492d3SPeter Lieven 392355cf09a0SDaniel P. Berrange if (tlsauthz) { 392455cf09a0SDaniel P. Berrange vd->tlsauthzid = g_strdup(tlsauthz); 392555cf09a0SDaniel P. Berrange } else if (acl) { 3926bf01c179SDaniel P. Berrange if (strcmp(vd->id, "default") == 0) { 3927b76806d4SDaniel P. Berrange vd->tlsauthzid = g_strdup("vnc.x509dname"); 3928c8496408SGerd Hoffmann } else { 3929b76806d4SDaniel P. Berrange vd->tlsauthzid = g_strdup_printf("vnc.%s.x509dname", vd->id); 3930c8496408SGerd Hoffmann } 3931b76806d4SDaniel P. Berrange vd->tlsauthz = QAUTHZ(qauthz_list_new(vd->tlsauthzid, 3932b76806d4SDaniel P. Berrange QAUTHZ_LIST_POLICY_DENY, 3933b76806d4SDaniel P. Berrange &error_abort)); 39343e230dd2SCorentin Chary } 39353e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 393655cf09a0SDaniel P. Berrange if (sasl) { 393755cf09a0SDaniel P. Berrange if (saslauthz) { 393855cf09a0SDaniel P. Berrange vd->sasl.authzid = g_strdup(saslauthz); 393955cf09a0SDaniel P. Berrange } else if (acl) { 3940bf01c179SDaniel P. Berrange if (strcmp(vd->id, "default") == 0) { 3941b76806d4SDaniel P. Berrange vd->sasl.authzid = g_strdup("vnc.username"); 3942c8496408SGerd Hoffmann } else { 3943b76806d4SDaniel P. Berrange vd->sasl.authzid = g_strdup_printf("vnc.%s.username", vd->id); 3944c8496408SGerd Hoffmann } 3945b76806d4SDaniel P. Berrange vd->sasl.authz = QAUTHZ(qauthz_list_new(vd->sasl.authzid, 3946b76806d4SDaniel P. Berrange QAUTHZ_LIST_POLICY_DENY, 3947b76806d4SDaniel P. Berrange &error_abort)); 39483e230dd2SCorentin Chary } 394955cf09a0SDaniel P. Berrange } 39503e230dd2SCorentin Chary #endif 39513e230dd2SCorentin Chary 3952eda24e18SDaniel P. Berrange if (vnc_display_setup_auth(&vd->auth, &vd->subauth, 3953eda24e18SDaniel P. Berrange vd->tlscreds, password, 3954eda24e18SDaniel P. Berrange sasl, false, errp) < 0) { 3955eda24e18SDaniel P. Berrange goto fail; 3956eda24e18SDaniel P. Berrange } 39577364dbdaSDaniel P. Berrange trace_vnc_auth_init(vd, 0, vd->auth, vd->subauth); 3958eda24e18SDaniel P. Berrange 3959eda24e18SDaniel P. Berrange if (vnc_display_setup_auth(&vd->ws_auth, &vd->ws_subauth, 3960eda24e18SDaniel P. Berrange vd->tlscreds, password, 3961eda24e18SDaniel P. Berrange sasl, true, errp) < 0) { 39623e305e4aSDaniel P. Berrange goto fail; 39633e305e4aSDaniel P. Berrange } 39647364dbdaSDaniel P. Berrange trace_vnc_auth_init(vd, 1, vd->ws_auth, vd->ws_subauth); 39653e230dd2SCorentin Chary 39663e230dd2SCorentin Chary #ifdef CONFIG_VNC_SASL 3967b5dc0d7dSMarc-André Lureau if (sasl) { 3968b5dc0d7dSMarc-André Lureau int saslErr = sasl_server_init(NULL, "qemu"); 3969b5dc0d7dSMarc-André Lureau 3970b5dc0d7dSMarc-André Lureau if (saslErr != SASL_OK) { 39712d55f0e8SPaolo Bonzini error_setg(errp, "Failed to initialize SASL auth: %s", 39723e230dd2SCorentin Chary sasl_errstring(saslErr, NULL, NULL)); 39731ce52c78SPaolo Bonzini goto fail; 39743e230dd2SCorentin Chary } 3975b5dc0d7dSMarc-André Lureau } 39763e230dd2SCorentin Chary #endif 3977bf01c179SDaniel P. Berrange vd->lock_key_sync = lock_key_sync; 3978a54f0d2bSPierre Ossman if (lock_key_sync) { 3979a54f0d2bSPierre Ossman vd->led = qemu_add_led_event_handler(kbd_leds, vd); 3980a54f0d2bSPierre Ossman } 3981a54f0d2bSPierre Ossman vd->ledstate = 0; 39823e230dd2SCorentin Chary 3983f0b9f36dSKővágó, Zoltán audiodev = qemu_opt_get(opts, "audiodev"); 3984f0b9f36dSKővágó, Zoltán if (audiodev) { 3985f0b9f36dSKővágó, Zoltán vd->audio_state = audio_state_by_name(audiodev); 3986f0b9f36dSKővágó, Zoltán if (!vd->audio_state) { 3987f0b9f36dSKővágó, Zoltán error_setg(errp, "Audiodev '%s' not found", audiodev); 3988f0b9f36dSKővágó, Zoltán goto fail; 3989f0b9f36dSKővágó, Zoltán } 3990f0b9f36dSKővágó, Zoltán } 3991f0b9f36dSKővágó, Zoltán 39921d0d59feSGerd Hoffmann device_id = qemu_opt_get(opts, "display"); 39931d0d59feSGerd Hoffmann if (device_id) { 39941d0d59feSGerd Hoffmann int head = qemu_opt_get_number(opts, "head", 0); 3995f2c1d54cSGerd Hoffmann Error *err = NULL; 39961d0d59feSGerd Hoffmann 3997f2c1d54cSGerd Hoffmann con = qemu_console_lookup_by_device_name(device_id, head, &err); 3998f2c1d54cSGerd Hoffmann if (err) { 3999f2c1d54cSGerd Hoffmann error_propagate(errp, err); 40001d0d59feSGerd Hoffmann goto fail; 40011d0d59feSGerd Hoffmann } 40021d0d59feSGerd Hoffmann } else { 40031d0d59feSGerd Hoffmann con = NULL; 40041d0d59feSGerd Hoffmann } 40051d0d59feSGerd Hoffmann 4006bf01c179SDaniel P. Berrange if (con != vd->dcl.con) { 4007c2f2ba49SGerd Hoffmann qkbd_state_free(vd->kbd); 4008bf01c179SDaniel P. Berrange unregister_displaychangelistener(&vd->dcl); 4009bf01c179SDaniel P. Berrange vd->dcl.con = con; 4010bf01c179SDaniel P. Berrange register_displaychangelistener(&vd->dcl); 4011c2f2ba49SGerd Hoffmann vd->kbd = qkbd_state_init(vd->dcl.con); 40121d0d59feSGerd Hoffmann } 4013c2f2ba49SGerd Hoffmann qkbd_state_set_delay(vd->kbd, key_delay_ms); 40141d0d59feSGerd Hoffmann 4015fa03cb7fSMarc-André Lureau if (saddr == NULL) { 4016fa03cb7fSMarc-André Lureau goto cleanup; 4017fa03cb7fSMarc-André Lureau } 4018fa03cb7fSMarc-André Lureau 40193e230dd2SCorentin Chary if (reverse) { 4020396f935aSDaniel P. Berrange if (vnc_display_connect(vd, saddr, nsaddr, wsaddr, nwsaddr, errp) < 0) { 4021e0d03b8cSDaniel P. Berrange goto fail; 40223e230dd2SCorentin Chary } 40233e230dd2SCorentin Chary } else { 4024396f935aSDaniel P. Berrange if (vnc_display_listen(vd, saddr, nsaddr, wsaddr, nwsaddr, errp) < 0) { 40253d00ac1aSCole Robinson goto fail; 40263d00ac1aSCole Robinson } 40273e230dd2SCorentin Chary } 4028e0d03b8cSDaniel P. Berrange 4029275e0d61SDaniel P. Berrange if (qemu_opt_get(opts, "to")) { 4030bf01c179SDaniel P. Berrange vnc_display_print_local_addr(vd); 403133df7bf3SPaolo Bonzini } 403233df7bf3SPaolo Bonzini 4033396f935aSDaniel P. Berrange cleanup: 40349f26f325SPhilippe Mathieu-Daudé vnc_free_addresses(&saddr, &nsaddr); 40359f26f325SPhilippe Mathieu-Daudé vnc_free_addresses(&wsaddr, &nwsaddr); 40362d55f0e8SPaolo Bonzini return; 40371ce52c78SPaolo Bonzini 40381ce52c78SPaolo Bonzini fail: 40394ee74fa7SDaniel P. Berrange vnc_display_close(vd); 4040396f935aSDaniel P. Berrange goto cleanup; 40413e230dd2SCorentin Chary } 404213661089SDaniel P. Berrange 404314f7143eSGerd Hoffmann void vnc_display_add_client(const char *id, int csock, bool skipauth) 404413661089SDaniel P. Berrange { 4045bf01c179SDaniel P. Berrange VncDisplay *vd = vnc_display_find(id); 404604d2529dSDaniel P. Berrange QIOChannelSocket *sioc; 404713661089SDaniel P. Berrange 4048bf01c179SDaniel P. Berrange if (!vd) { 4049d616ccc5SGerd Hoffmann return; 4050d616ccc5SGerd Hoffmann } 405104d2529dSDaniel P. Berrange 405204d2529dSDaniel P. Berrange sioc = qio_channel_socket_new_fd(csock, NULL); 405304d2529dSDaniel P. Berrange if (sioc) { 405410bcfe58SDaniel P. Berrange qio_channel_set_name(QIO_CHANNEL(sioc), "vnc-server"); 4055bf01c179SDaniel P. Berrange vnc_connect(vd, sioc, skipauth, false); 405604d2529dSDaniel P. Berrange object_unref(OBJECT(sioc)); 405704d2529dSDaniel P. Berrange } 405813661089SDaniel P. Berrange } 40594db14629SGerd Hoffmann 40609634f4e3SGerd Hoffmann static void vnc_auto_assign_id(QemuOptsList *olist, QemuOpts *opts) 40612779672fSGonglei { 40622779672fSGonglei int i = 2; 40632779672fSGonglei char *id; 40642779672fSGonglei 40652779672fSGonglei id = g_strdup("default"); 40662779672fSGonglei while (qemu_opts_find(olist, id)) { 40672779672fSGonglei g_free(id); 40682779672fSGonglei id = g_strdup_printf("vnc%d", i++); 40692779672fSGonglei } 40702779672fSGonglei qemu_opts_set_id(opts, id); 40712779672fSGonglei } 40722779672fSGonglei 407370b94331SMarkus Armbruster QemuOpts *vnc_parse(const char *str, Error **errp) 40744db14629SGerd Hoffmann { 40754db14629SGerd Hoffmann QemuOptsList *olist = qemu_find_opts("vnc"); 407670b94331SMarkus Armbruster QemuOpts *opts = qemu_opts_parse(olist, str, true, errp); 407781607cbfSGonglei const char *id; 40784db14629SGerd Hoffmann 407981607cbfSGonglei if (!opts) { 408081607cbfSGonglei return NULL; 408181607cbfSGonglei } 408281607cbfSGonglei 408381607cbfSGonglei id = qemu_opts_id(opts); 40844db14629SGerd Hoffmann if (!id) { 40854db14629SGerd Hoffmann /* auto-assign id if not present */ 40862779672fSGonglei vnc_auto_assign_id(olist, opts); 40879634f4e3SGerd Hoffmann } 40889634f4e3SGerd Hoffmann return opts; 40894db14629SGerd Hoffmann } 40904db14629SGerd Hoffmann 409128d0de7aSMarkus Armbruster int vnc_init_func(void *opaque, QemuOpts *opts, Error **errp) 40929634f4e3SGerd Hoffmann { 40939634f4e3SGerd Hoffmann Error *local_err = NULL; 40949634f4e3SGerd Hoffmann char *id = (char *)qemu_opts_id(opts); 40959634f4e3SGerd Hoffmann 40969634f4e3SGerd Hoffmann assert(id); 4097ab4f931eSFei Li vnc_display_init(id, &local_err); 4098ab4f931eSFei Li if (local_err) { 4099612aea20SMarkus Armbruster error_propagate(errp, local_err); 4100612aea20SMarkus Armbruster return -1; 4101ab4f931eSFei Li } 41024db14629SGerd Hoffmann vnc_display_open(id, &local_err); 41034db14629SGerd Hoffmann if (local_err != NULL) { 4104612aea20SMarkus Armbruster error_propagate(errp, local_err); 4105612aea20SMarkus Armbruster return -1; 41064db14629SGerd Hoffmann } 41074db14629SGerd Hoffmann return 0; 41084db14629SGerd Hoffmann } 41094db14629SGerd Hoffmann 41104db14629SGerd Hoffmann static void vnc_register_config(void) 41114db14629SGerd Hoffmann { 41124db14629SGerd Hoffmann qemu_add_opts(&qemu_vnc_opts); 41134db14629SGerd Hoffmann } 411434294e2fSEduardo Habkost opts_init(vnc_register_config); 4115