Update websocket code to more fully support the RFC
PATCH v3 2/7 io: Small updates in preparation for websocket changes
In reply to Brandon CarpenterGets rid of unnecessary bit shifting and performs proper EOF checking to avoid a large number of repeated calls to recvmsg() when a client abruptly terminates a connection (bug fix).
--- io/channel-websock.c | 62 +++++++++++++++------------------------------------- 1 file changed, 18 insertions(+), 44 deletions(-)
io/channel-websock.c +18 -44
@@ -86,8 +86,6 @@#define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE 0x0f#define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK 0x80#define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN 0x7f -#define QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_FIN 7 -#define QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_HAS_MASK 7typedef struct QIOChannelWebsockHeader QIOChannelWebsockHeader ;
@@ -492,7 +490,7 @@ static void qio_channel_websock_encode(QIOChannelWebsock *ioc)return ; } - header.ws.b0 = (1 <<QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_FIN ) | + header.ws.b0 =QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN | (QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME &QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE ); if (ioc->rawoutput.offset <
@@ -519,8 +517,8 @@ static void qio_channel_websock_encode(QIOChannelWebsock *ioc) } -static ssize_t qio_channel_websock_decode_header (QIOChannelWebsock *ioc, -Error **errp) +static int qio_channel_websock_decode_header (QIOChannelWebsock *ioc, +Error **errp) {unsigned char opcode, fin, has_mask;size_t header_size;
@@ -539,11 +537,9 @@ static ssize_t qio_channel_websock_decode_header(QIOChannelWebsock *ioc,return QIO_CHANNEL_ERR_BLOCK ; } - fin = (header->b0 &QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN ) >> -QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_FIN ; + fin = header->b0 &QIO_CHANNEL_WEBSOCK_HEADER_FIELD_FIN ; opcode = header->b0 &QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE ; - has_mask = (header->b1 &QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK ) >> -QIO_CHANNEL_WEBSOCK_HEADER_SHIFT_HAS_MASK ; + has_mask = header->b1 &QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK ; payload_len = header->b1 &QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN ;if (opcode ==QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE ) {
@@ -561,7 +557,7 @@ static ssize_t qio_channel_websock_decode_header(QIOChannelWebsock *ioc,return -1 ; }if (!has_mask) { -error_setg (errp,"websocket frames must be masked" ); +error_setg (errp,"client websocket frames must be masked" );return -1 ; }if (opcode !=QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME ) {
@@ -593,8 +589,8 @@ static ssize_t qio_channel_websock_decode_header(QIOChannelWebsock *ioc, } -static ssize_t qio_channel_websock_decode_payload (QIOChannelWebsock *ioc, -Error **errp) +static int qio_channel_websock_decode_payload (QIOChannelWebsock *ioc, +Error **errp) {size_t i;size_t payload_len;
@@ -635,7 +631,7 @@ static ssize_t qio_channel_websock_decode_payload(QIOChannelWebsock *ioc,buffer_reserve (&ioc->rawinput, payload_len);buffer_append (&ioc->rawinput, ioc->encinput.buffer, payload_len);buffer_advance (&ioc->encinput, payload_len); -return payload_len; +return 0 ; }
@@ -715,8 +711,8 @@ static ssize_t qio_channel_websock_read_wire(QIOChannelWebsock *ioc,if (ret <0 ) {return ret; } -if (ret ==0 && - ioc->encinput.offset ==0 ) { +if (ret ==0 && ioc->encinput.offset ==0 ) { + ioc->io_eof = TRUE;return 0 ; } ioc->encinput.offset += ret;
@@ -728,10 +724,6 @@ static ssize_t qio_channel_websock_read_wire(QIOChannelWebsock *ioc,if (ret <0 ) {return ret; } -if (ret ==0 ) { - ioc->io_eof = TRUE; -break ; - } } ret =qio_channel_websock_decode_payload (ioc, errp);
@@ -996,14 +988,12 @@ struct QIOChannelWebsockSource { };static gboolean -qio_channel_websock_source_prepare (GSource *source, -gint *timeout) +qio_channel_websock_source_check (GSource *source) {QIOChannelWebsockSource *wsource = (QIOChannelWebsockSource *)source;GIOCondition cond =0 ; - *timeout =-1 ; -if (wsource->wioc->rawinput.offset) { +if (wsource->wioc->rawinput.offset || wsource->wioc->io_eof) { cond |=G_IO_IN ; }if (wsource->wioc->rawoutput.offset <QIO_CHANNEL_WEBSOCK_MAX_BUFFER ) {
@@ -1014,19 +1004,11 @@ qio_channel_websock_source_prepare(GSource *source, }static gboolean -qio_channel_websock_source_check (GSource *source) +qio_channel_websock_source_prepare (GSource *source, +gint *timeout) { -QIOChannelWebsockSource *wsource = (QIOChannelWebsockSource *)source; -GIOCondition cond =0 ; - -if (wsource->wioc->rawinput.offset) { - cond |=G_IO_IN ; - } -if (wsource->wioc->rawoutput.offset <QIO_CHANNEL_WEBSOCK_MAX_BUFFER ) { - cond |=G_IO_OUT ; - } - -return cond & wsource->condition; + *timeout =-1 ; +return qio_channel_websock_source_check (source); } static gboolean
@@ -1036,17 +1018,9 @@ qio_channel_websock_source_dispatch(GSource *source, {QIOChannelFunc func = (QIOChannelFunc )callback;QIOChannelWebsockSource *wsource = (QIOChannelWebsockSource *)source; -GIOCondition cond =0 ; - -if (wsource->wioc->rawinput.offset) { - cond |=G_IO_IN ; - } -if (wsource->wioc->rawoutput.offset <QIO_CHANNEL_WEBSOCK_MAX_BUFFER ) { - cond |=G_IO_OUT ; - }return (*func)(QIO_CHANNEL (wsource->wioc), - (cond & wsource->condition), +qio_channel_websock_source_check (source), user_data); }
Signature · 12 lines
2.14.1 -- CONFIDENTIALITY NOTICE: This e-mail message, including any attachments, is for the sole use of the intended recipient(s) and may contain proprietary, confidential or privileged information or otherwise be protected by law. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please notify the sender and destroy all copies and the original message.
PATCH v3 3/7 io: Add support for fragmented websocket binary frames
In reply to Brandon CarpenterAllows fragmented binary frames by saving the previous opcode. Handles the case where an intermediary (i.e., web proxy) fragments frames originally sent unfragmented by the client.
--- include/io/channel-websock.h | 1 + io/channel-websock.c | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-)
include/io/channel-websock.h +1 -0
@@ -65,6 +65,7 @@ struct QIOChannelWebsock {guint io_tag;Error *io_err;gboolean io_eof; +uint8_t opcode; };/**
io/channel-websock.c +18 -8
@@ -542,28 +542,38 @@ static int qio_channel_websock_decode_header(QIOChannelWebsock *ioc, has_mask = header->b1 &QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK ; payload_len = header->b1 &QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN ; +/* Save or restore opcode. */ +if (opcode) { + ioc->opcode = opcode; + }else { + opcode = ioc->opcode; + } +if (opcode ==QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE ) {/* disconnect */ return 0 ; }/* Websocket frame sanity check: -* * Websocket fragmentation is not supported. -* * All websockets frames sent by a client have to be masked. +* * Fragmentation is only supported for binary frames. +* * All frames sent by a client MUST be masked. * * Only binary encoding is supported. */ if (!fin) { -error_setg (errp,"websocket fragmentation is not supported" ); -return -1 ; +if (opcode !=QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME ) { +error_setg (errp,"only binary websocket frames may be fragmented" ); +return -1 ; + } + }else { +if (opcode !=QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME ) { +error_setg (errp,"only binary websocket frames are supported" ); +return -1 ; + } }if (!has_mask) {error_setg (errp,"client websocket frames must be masked" );return -1 ; } -if (opcode !=QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME ) { -error_setg (errp,"only binary websocket frames are supported" ); -return -1 ; - }if (payload_len <QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT ) { ioc->payload_remain = payload_len;
Signature · 12 lines
2.14.1 -- CONFIDENTIALITY NOTICE: This e-mail message, including any attachments, is for the sole use of the intended recipient(s) and may contain proprietary, confidential or privileged information or otherwise be protected by law. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please notify the sender and destroy all copies and the original message.
PATCH v3 1/7 io: Always remove an old channel watch before adding a new one
In reply to Brandon CarpenterAlso set saved handle to zero when removing without adding a new watch.
--- ui/vnc-auth-vencrypt.c | 3 +++ ui/vnc-ws.c | 6 ++++++ ui/vnc.c | 4 ++++ 3 files changed, 13 insertions(+)
ui/vnc-auth-vencrypt.c +3 -0
@@ -77,6 +77,9 @@ static void vnc_tls_handshake_done(QIOTask *task,vnc_client_error (vs);error_free (err); }else { +if (vs->ioc_tag) { +g_source_remove (vs->ioc_tag); + } vs->ioc_tag =qio_channel_add_watch ( vs->ioc,G_IO_IN |G_IO_OUT , vnc_client_io, vs,NULL );start_auth_vencrypt_subauth (vs);
ui/vnc-ws.c +6 -0
@@ -36,6 +36,9 @@ static void vncws_tls_handshake_done(QIOTask *task,error_free (err); }else {VNC_DEBUG ("TLS handshake complete, starting websocket handshake\n" ); +if (vs->ioc_tag) { +g_source_remove (vs->ioc_tag); + } vs->ioc_tag =qio_channel_add_watch (QIO_CHANNEL (vs->ioc),G_IO_IN , vncws_handshake_io, vs,NULL ); }
@@ -97,6 +100,9 @@ static void vncws_handshake_done(QIOTask *task, }else {VNC_DEBUG ("Websock handshake complete, starting VNC protocol\n" );vnc_start_protocol (vs); +if (vs->ioc_tag) { +g_source_remove (vs->ioc_tag); + } vs->ioc_tag =qio_channel_add_watch ( vs->ioc,G_IO_IN , vnc_client_io, vs,NULL ); }
ui/vnc.c +4 -0
@@ -1121,6 +1121,7 @@ static void vnc_disconnect_start(VncState *vs)vnc_set_share_mode (vs,VNC_SHARE_MODE_DISCONNECTED );if (vs->ioc_tag) {g_source_remove (vs->ioc_tag); + vs->ioc_tag =0 ; }qio_channel_close (vs->ioc,NULL ); vs->disconnecting = TRUE;
@@ -2931,6 +2932,9 @@ static void vnc_connect(VncDisplay *vd, QIOChannelSocket *sioc,VNC_DEBUG ("New client on socket %p\n" , vs->sioc);update_displaychangelistener (&vd->dcl,VNC_REFRESH_INTERVAL_BASE );qio_channel_set_blocking (vs->ioc, false,NULL ); +if (vs->ioc_tag) { +g_source_remove (vs->ioc_tag); + }if (websocket) { vs->websocket =1 ;if (vd->tlscreds) {
Signature · 12 lines
2.14.1 -- CONFIDENTIALITY NOTICE: This e-mail message, including any attachments, is for the sole use of the intended recipient(s) and may contain proprietary, confidential or privileged information or otherwise be protected by law. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please notify the sender and destroy all copies and the original message.
v3 cover Update websocket code to more fully support the RFC
We've been experiencing issues where the qemu websocket server closes connections from noVNC clients for no apparent reason. Debugging shows that certain web browsers are injecting ping and pong frames when the connection becomes idle. Some browsers send those frames without a payload, which also is causing closure. This patch series addresses these issues by making the websocket server more conformant to RFC 6455 - The WebSocket Protocol.
Remembering the opcode is sufficient for handling fragmented frames from the client, which may be introduced by an intermediary server/proxy. Respond to pings and ignore pongs rather than close the connection as many browsers use ping/pong to test an idle connection. Close connections according to the RFC, including providing a reason code and message to aid debugging of unexpected disconnects. Empty payloads should not cause a disconnect.
While updating the websocket code, several other bugs were discovered for which patches are also included early in the set.
Brandon Carpenter (7):
io: Always remove an old channel watch before adding a new one
io: Small updates in preparation for websocket changes
io: Add support for fragmented websocket binary frames
io: Allow empty websocket payload
io: Ignore websocket PING and PONG frames
io: Reply to ping frames
io: Attempt to send websocket close messages to client
include/io/channel-websock.h | 2 + io/channel-websock.c | 282 +++++++++++++++++++++++++++---------------- ui/vnc-auth-vencrypt.c | 3 + ui/vnc-ws.c | 6 + ui/vnc.c | 4 + 5 files changed, 194 insertions(+), 103 deletions(-)
Signature · 12 lines
2.14.1 -- CONFIDENTIALITY NOTICE: This e-mail message, including any attachments, is for the sole use of the intended recipient(s) and may contain proprietary, confidential or privileged information or otherwise be protected by law. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please notify the sender and destroy all copies and the original message.
PATCH v3 4/7 io: Allow empty websocket payload
Some browsers send pings/pongs with no payload, so allow empty payloads instead of closing the connection.
--- io/channel-websock.c | 62 +++++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 32 deletions(-)
io/channel-websock.c +30 -32
@@ -603,44 +603,42 @@ static int qio_channel_websock_decode_payload(QIOChannelWebsock *ioc, Error **errp) {size_t i; -size_t payload_len; +size_t payload_len =0 ;uint32_t *payload32; -if (!ioc->payload_remain) { -error_setg (errp, -"Decoding payload but no bytes of payload remain" ); -return -1 ; - } - -/* If we aren't at the end of the payload, then drop -* off the last bytes, so we're always multiple of 4 -* for purpose of unmasking, except at end of payload -*/ -if (ioc->encinput.offset < ioc->payload_remain) { - payload_len = ioc->encinput.offset - (ioc->encinput.offset %4 ); - }else { - payload_len = ioc->payload_remain; - } -if (payload_len ==0 ) { -return QIO_CHANNEL_ERR_BLOCK ; - } +if (ioc->payload_remain) { +/* If we aren't at the end of the payload, then drop +* off the last bytes, so we're always multiple of 4 +* for purpose of unmasking, except at end of payload +*/ +if (ioc->encinput.offset < ioc->payload_remain) { + payload_len = ioc->encinput.offset - (ioc->encinput.offset %4 ); + }else { + payload_len = ioc->payload_remain; + } +if (payload_len ==0 ) { +return QIO_CHANNEL_ERR_BLOCK ; + } - ioc->payload_remain -= payload_len; + ioc->payload_remain -= payload_len; -/* unmask frame */ -/* process 1 frame (32 bit op) */ - payload32 = (uint32_t *)ioc->encinput.buffer; -for (i =0 ; i < payload_len /4 ; i++) { - payload32[i] ^= ioc->mask.u; - } -/* process the remaining bytes (if any) */ -for (i *=4 ; i < payload_len; i++) { - ioc->encinput.buffer[i] ^= ioc->mask.c[i %4 ]; +/* unmask frame */ +/* process 1 frame (32 bit op) */ + payload32 = (uint32_t *)ioc->encinput.buffer; +for (i =0 ; i < payload_len /4 ; i++) { + payload32[i] ^= ioc->mask.u; + } +/* process the remaining bytes (if any) */ +for (i *=4 ; i < payload_len; i++) { + ioc->encinput.buffer[i] ^= ioc->mask.c[i %4 ]; + } } -buffer_reserve (&ioc->rawinput, payload_len); -buffer_append (&ioc->rawinput, ioc->encinput.buffer, payload_len); -buffer_advance (&ioc->encinput, payload_len); +if (payload_len) { +buffer_reserve (&ioc->rawinput, payload_len); +buffer_append (&ioc->rawinput, ioc->encinput.buffer, payload_len); +buffer_advance (&ioc->encinput, payload_len); + }return 0 ; }
Signature · 12 lines
2.14.1 -- CONFIDENTIALITY NOTICE: This e-mail message, including any attachments, is for the sole use of the intended recipient(s) and may contain proprietary, confidential or privileged information or otherwise be protected by law. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please notify the sender and destroy all copies and the original message.
PATCH v3 7/7 io: Attempt to send websocket close messages to client
In reply to Brandon CarpenterMake a best effort attempt to close websocket connections according to the RFC. Sends the close message, as room permits in the socket buffer, and immediately closes the socket.
--- io/channel-websock.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 3 deletions(-)
io/channel-websock.c +62 -3
@@ -122,6 +122,15 @@ enum {QIO_CHANNEL_WEBSOCK_OPCODE_PONG =0xA }; +enum { +QIO_CHANNEL_WEBSOCK_STATUS_NORMAL =1000 , +QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR =1002 , +QIO_CHANNEL_WEBSOCK_STATUS_INVALID_DATA =1003 , +QIO_CHANNEL_WEBSOCK_STATUS_POLICY =1008 , +QIO_CHANNEL_WEBSOCK_STATUS_TOO_LARGE =1009 , +QIO_CHANNEL_WEBSOCK_STATUS_SERVER_ERR =1011 , +}; + static size_t qio_channel_websock_extract_headers(char *buffer, QIOChannelWebsockHTTPHeader *hdrs,
@@ -523,6 +532,26 @@ static void qio_channel_websock_encode(QIOChannelWebsock *ioc) } +static ssize_t qio_channel_websock_write_wire (QIOChannelWebsock *,Error **); + + +static void qio_channel_websock_write_close (QIOChannelWebsock *ioc, +uint16_t code,const char *reason) +{ +buffer_reserve (&ioc->rawoutput,2 + (reason ?strlen (reason) :0 )); + *(uint16_t *)(ioc->rawoutput.buffer + ioc->rawoutput.offset) =cpu_to_be16 (code); + ioc->rawoutput.offset +=2 ; +if (reason) { +buffer_append (&ioc->rawoutput, reason,strlen (reason)); + } +qio_channel_websock_encode_buffer (&ioc->encoutput, +QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE , &ioc->rawoutput); +buffer_reset (&ioc->rawoutput); +qio_channel_websock_write_wire (ioc,NULL ); +qio_channel_shutdown (ioc->master,QIO_CHANNEL_SHUTDOWN_BOTH ,NULL ); +} + +static int qio_channel_websock_decode_header (QIOChannelWebsock *ioc,Error **errp) {
@@ -536,6 +565,8 @@ static int qio_channel_websock_decode_header(QIOChannelWebsock *ioc,error_setg (errp,"Decoding header but %zu bytes of payload remain" , ioc->payload_remain); +qio_channel_websock_write_close (ioc, +QIO_CHANNEL_WEBSOCK_STATUS_SERVER_ERR ,"internal server error" );return -1 ; }if (ioc->encinput.offset <QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT ) {
@@ -568,19 +599,29 @@ static int qio_channel_websock_decode_header(QIOChannelWebsock *ioc,if (!fin) {if (opcode !=QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME ) {error_setg (errp,"only binary websocket frames may be fragmented" ); +qio_channel_websock_write_close (ioc, +QIO_CHANNEL_WEBSOCK_STATUS_POLICY , +"only binary frames may be fragmented" );return -1 ; } }else {if (opcode !=QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME && + opcode !=QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE && opcode !=QIO_CHANNEL_WEBSOCK_OPCODE_PING && opcode !=QIO_CHANNEL_WEBSOCK_OPCODE_PONG ) { -error_setg (errp,"unsupported opcode: %#04x; only binary, ping, " -"and pong websocket frames are supported" , opcode); +error_setg (errp,"unsupported opcode: %#04x; only binary, close, " +"ping, and pong websocket frames are supported" , opcode); +qio_channel_websock_write_close (ioc, +QIO_CHANNEL_WEBSOCK_STATUS_INVALID_DATA , +"only binary, close, ping, and pong frames are supported" );return -1 ; } }if (!has_mask) {error_setg (errp,"client websocket frames must be masked" ); +qio_channel_websock_write_close (ioc, +QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR , +"client frames must be masked" );return -1 ; }
@@ -590,6 +631,9 @@ static int qio_channel_websock_decode_header(QIOChannelWebsock *ioc, ioc->mask = header->u.m; }else if (opcode &QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK ) {error_setg (errp,"websocket control frame is too large" ); +qio_channel_websock_write_close (ioc, +QIO_CHANNEL_WEBSOCK_STATUS_PROTOCOL_ERR , +"control frame is too large" );return -1 ; }else if (payload_len ==QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT && ioc->encinput.offset >=QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT ) {
@@ -607,7 +651,7 @@ static int qio_channel_websock_decode_header(QIOChannelWebsock *ioc, }buffer_advance (&ioc->encinput, header_size); -return 1 ; +return 0 ; }
@@ -657,6 +701,21 @@ static int qio_channel_websock_decode_payload(QIOChannelWebsock *ioc,buffer_reserve (&ioc->rawinput, payload_len);buffer_append (&ioc->rawinput, ioc->encinput.buffer, payload_len); } + }else if (ioc->opcode ==QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE ) { +/* close frames are echoed back */ +error_setg (errp,"websocket closed by peer" ); +if (payload_len) { +/* echo client status */ +qio_channel_websock_encode_buffer (&ioc->encoutput, +QIO_CHANNEL_WEBSOCK_OPCODE_CLOSE , &ioc->encinput); +qio_channel_websock_write_wire (ioc,NULL ); +qio_channel_shutdown (ioc->master,QIO_CHANNEL_SHUTDOWN_BOTH ,NULL ); + }else { +/* send our own status */ +qio_channel_websock_write_close (ioc, +QIO_CHANNEL_WEBSOCK_STATUS_NORMAL ,"peer requested close" ); + } +return -1 ; }else if (ioc->opcode ==QIO_CHANNEL_WEBSOCK_OPCODE_PING ) {/* ping frames produce an immediate reply */ buffer_reset (&ioc->ping_reply);
Signature · 12 lines
2.14.1 -- CONFIDENTIALITY NOTICE: This e-mail message, including any attachments, is for the sole use of the intended recipient(s) and may contain proprietary, confidential or privileged information or otherwise be protected by law. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please notify the sender and destroy all copies and the original message.
PATCH v3 5/7 io: Ignore websocket PING and PONG frames
In reply to Brandon CarpenterKeep pings and gratuitous pongs generated by web browsers from killing websocket connections.
--- io/channel-websock.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-)
io/channel-websock.c +17 -4
@@ -86,6 +86,7 @@#define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_OPCODE 0x0f#define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_HAS_MASK 0x80#define QIO_CHANNEL_WEBSOCK_HEADER_FIELD_PAYLOAD_LEN 0x7f +#define QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK 0x8typedef struct QIOChannelWebsockHeader QIOChannelWebsockHeader ;
@@ -565,8 +566,11 @@ static int qio_channel_websock_decode_header(QIOChannelWebsock *ioc,return -1 ; } }else { -if (opcode !=QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME ) { -error_setg (errp,"only binary websocket frames are supported" ); +if (opcode !=QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME && + opcode !=QIO_CHANNEL_WEBSOCK_OPCODE_PING && + opcode !=QIO_CHANNEL_WEBSOCK_OPCODE_PONG ) { +error_setg (errp,"unsupported opcode: %#04x; only binary, ping, " +"and pong websocket frames are supported" , opcode);return -1 ; } }
@@ -579,6 +583,9 @@ static int qio_channel_websock_decode_header(QIOChannelWebsock *ioc, ioc->payload_remain = payload_len; header_size =QIO_CHANNEL_WEBSOCK_HEADER_LEN_7_BIT ; ioc->mask = header->u.m; + }else if (opcode &QIO_CHANNEL_WEBSOCK_CONTROL_OPCODE_MASK ) { +error_setg (errp,"websocket control frame is too large" ); +return -1 ; }else if (payload_len ==QIO_CHANNEL_WEBSOCK_PAYLOAD_LEN_MAGIC_16_BIT && ioc->encinput.offset >=QIO_CHANNEL_WEBSOCK_HEADER_LEN_16_BIT ) { ioc->payload_remain =be16_to_cpu (header->u.s16.l16);
@@ -634,9 +641,15 @@ static int qio_channel_websock_decode_payload(QIOChannelWebsock *ioc, } } +/* Drop the payload of ping/pong packets */ +if (ioc->opcode ==QIO_CHANNEL_WEBSOCK_OPCODE_BINARY_FRAME ) { +if (payload_len) { +buffer_reserve (&ioc->rawinput, payload_len); +buffer_append (&ioc->rawinput, ioc->encinput.buffer, payload_len); + } + } +if (payload_len) { -buffer_reserve (&ioc->rawinput, payload_len); -buffer_append (&ioc->rawinput, ioc->encinput.buffer, payload_len);buffer_advance (&ioc->encinput, payload_len); }return 0 ;
Signature · 12 lines
2.14.1 -- CONFIDENTIALITY NOTICE: This e-mail message, including any attachments, is for the sole use of the intended recipient(s) and may contain proprietary, confidential or privileged information or otherwise be protected by law. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please notify the sender and destroy all copies and the original message.
PATCH v3 6/7 io: Reply to ping frames
In reply to Brandon CarpenterAdd an immediate ping reply (pong) to the outgoing stream when a ping is received. Unsolicited pongs are ignored.
--- include/io/channel-websock.h | 1 + io/channel-websock.c | 64 +++++++++++++++++++++++++++++--------------- 2 files changed, 44 insertions(+), 21 deletions(-)
include/io/channel-websock.h +1 -0
@@ -60,6 +60,7 @@ struct QIOChannelWebsock {Buffer encoutput;Buffer rawinput;Buffer rawoutput; +Buffer ping_reply;size_t payload_remain;QIOChannelWebsockMask mask;guint io_tag;
io/channel-websock.c +43 -21
129 lines not on this page · raw message
Signature · 12 lines
2.14.1 -- CONFIDENTIALITY NOTICE: This e-mail message, including any attachments, is for the sole use of the intended recipient(s) and may contain proprietary, confidential or privileged information or otherwise be protected by law. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please notify the sender and destroy all copies and the original message.
Quoted 28 lines · Brandon Carpenter
We've been experiencing issues where the qemu websocket server closes connections from noVNC clients for no apparent reason. Debugging shows that certain web browsers are injecting ping and pong frames when the connection becomes idle. Some browsers send those frames without a payload, which also is causing closure. This patch series addresses these issues by making the websocket server more conformant to RFC 6455 - The WebSocket Protocol.
Remembering the opcode is sufficient for handling fragmented frames from the client, which may be introduced by an intermediary server/proxy. Respond to pings and ignore pongs rather than close the connection as many browsers use ping/pong to test an idle connection. Close connections according to the RFC, including providing a reason code and message to aid debugging of unexpected disconnects. Empty payloads should not cause a disconnect.
While updating the websocket code, several other bugs were discovered for which patches are also included early in the set.
Brandon Carpenter (7):
io: Always remove an old channel watch before adding a new one
io: Small updates in preparation for websocket changes
io: Add support for fragmented websocket binary frames
io: Allow empty websocket payload
io: Ignore websocket PING and PONG frames
io: Reply to ping frames
io: Attempt to send websocket close messages to client
Quoted 9 lines
include/io/channel-websock.h | 2 + io/channel-websock.c | 282 +++++++++++++++++++++++++++---------------- ui/vnc-auth-vencrypt.c | 3 + ui/vnc-ws.c | 6 + ui/vnc.c | 4 + 5 files changed, 194 insertions(+), 103 deletions(-)Signature · 1 line
2.14.1
Hi Brandon,
Thanks for the patch series! Please CC the relevant maintainer on future patches. I've CCed Daniel Berrange on this email to bring your work to his attention.
$ scripts/get_maintainer.pl -f io/channel-websock.c "Daniel P. Berrange" <[email protected]> (maintainer:I/O Channels) [email protected] (open list:All patches CC here)
Stefan
Quoted 12 lines
Signature · 9 lines
-- CONFIDENTIALITY NOTICE: This e-mail message, including any attachments, is for the sole use of the intended recipient(s) and may contain proprietary, confidential or privileged information or otherwise be protected by law. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please notify the sender and destroy all copies and the original message.
How are things looking, Daniel. I believe every comment from the previous version of the series was addressed.
I also wanted to mention that I put together a Python script, which acts as a proxy between noVNC and qemu, and can inject various frame types, fragment frames, and shutdown the socket in various ways to help exercise the different code paths. I would be happy to post it here to help test the changes.
Thanks,
--
Brandon Carpenter | Software Engineer
Cypherpath, Inc.
400 Columbia Point Drive Ste 101 | Richland, Washington USA
Office: (650) 713-3060
Quoted 39 lines · Brandon Carpenter
We've been experiencing issues where the qemu websocket server closes connections from noVNC clients for no apparent reason. Debugging shows that certain web browsers are injecting ping and pong frames when the connection becomes idle. Some browsers send those frames without a payload, which also is causing closure. This patch series addresses these issues by making the websocket server more conformant to RFC 6455 - The WebSocket Protocol.
Remembering the opcode is sufficient for handling fragmented frames from the client, which may be introduced by an intermediary server/proxy. Respond to pings and ignore pongs rather than close the connection as many browsers use ping/pong to test an idle connection. Close connections according to the RFC, including providing a reason code and message to aid debugging of unexpected disconnects. Empty payloads should not cause a disconnect.
While updating the websocket code, several other bugs were discovered for which patches are also included early in the set.
Brandon Carpenter (7):
io: Always remove an old channel watch before adding a new one
io: Small updates in preparation for websocket changes
io: Add support for fragmented websocket binary frames
io: Allow empty websocket payload
io: Ignore websocket PING and PONG frames
io: Reply to ping frames
io: Attempt to send websocket close messages to clientinclude/io/channel-websock.h | 2 + io/channel-websock.c | 282 +++++++++++++++++++++++++++---------------- ui/vnc-auth-vencrypt.c | 3 + ui/vnc-ws.c | 6 + ui/vnc.c | 4 + 5 files changed, 194 insertions(+), 103 deletions(-)--
2.14.1
Signature · 1 line
CONFIDENTIALITY NOTICE: This e-mail message, including any attachments, is for the sole use of the intended recipient(s) and may contain proprietary, confidential or privileged information or otherwise be protected by law. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please notify the sender and destroy all copies and the original message.
Quoted 8 lines · Brandon Carpenter
How are things looking, Daniel. I believe every comment from the previous version of the series was addressed.
I also wanted to mention that I put together a Python script, which acts as a proxy between noVNC and qemu, and can inject various frame types, fragment frames, and shutdown the socket in various ways to help exercise the different code paths. I would be happy to post it here to help test the changes.
Sorry for the delay - I've reviewed this now and it looks good. I've made a few whitespace changes in places, but I've queued it for my next pull request now.
Regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Cool. Thanks for helping me through the process and for the great feedback.
--
Brandon Carpenter | Software Engineer
Cypherpath, Inc.
400 Columbia Point Drive Ste 101 | Richland, Washington USA
Office: (650) 713-3060
Quoted 24 lines · Daniel P. Berrange
Quoted 4 lines · Brandon Carpenter
How are things looking, Daniel. I believe every comment from the
previous
version of the series was addressed.Quoted 8 lines
I also wanted to mention that I put together a Python script, which
acts as
a proxy between noVNC and qemu, and can inject various frame types,
fragment
frames, and shutdown the socket in various ways to help exercise the different code paths. I would be happy to post it here to help test
the
changes.Sorry for the delay - I've reviewed this now and it looks good. I've made a few whitespace changes in places, but I've queued it for my next pull request now.
Regards,
Daniel
--|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Signature · 1 line
CONFIDENTIALITY NOTICE: This e-mail message, including any attachments, is for the sole use of the intended recipient(s) and may contain proprietary, confidential or privileged information or otherwise be protected by law. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please notify the sender and destroy all copies and the original message.