Coverage Report

Created: 2025-07-23 07:03

/src/spice-usbredir/usbredirparser/usbredirparser.c
Line
Count
Source (jump to first uncovered line)
1
/* usbredirparser.c usb redirection protocol parser
2
3
   Copyright 2010-2012 Red Hat, Inc.
4
5
   Red Hat Authors:
6
   Hans de Goede <hdegoede@redhat.com>
7
8
   This library is free software; you can redistribute it and/or
9
   modify it under the terms of the GNU Lesser General Public
10
   License as published by the Free Software Foundation; either
11
   version 2.1 of the License, or (at your option) any later version.
12
13
   This library is distributed in the hope that it will be useful,
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
   Lesser General Public License for more details.
17
18
   You should have received a copy of the GNU Lesser General Public
19
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
20
*/
21
#include "config.h"
22
23
#include <assert.h>
24
#include <stdbool.h>
25
#include <stddef.h>
26
#include <stdio.h>
27
#include <stdlib.h>
28
#include <stdarg.h>
29
#include <string.h>
30
#include "usbredirproto-compat.h"
31
#include "usbredirparser.h"
32
#include "usbredirfilter.h"
33
34
/* Put *some* upper limit on bulk transfer sizes */
35
216k
#define MAX_BULK_TRANSFER_SIZE (128u * 1024u * 1024u)
36
37
/* Upper limit for accepted packet sizes including headers; makes the assumption
38
 * that no header is longer than 1kB
39
 */
40
214k
#define MAX_PACKET_SIZE (1024u + MAX_BULK_TRANSFER_SIZE)
41
42
/* Locking convenience macros */
43
#define LOCK(parser) \
44
90.5k
    do { \
45
90.5k
        if ((parser)->lock) \
46
90.5k
            (parser)->callb.lock_func((parser)->lock); \
47
90.5k
    } while (0)
48
49
#define UNLOCK(parser) \
50
90.5k
    do { \
51
90.5k
        if ((parser)->lock) \
52
90.5k
            (parser)->callb.unlock_func((parser)->lock); \
53
90.5k
    } while (0)
54
55
struct usbredirparser_buf {
56
    uint8_t *buf;
57
    int pos;
58
    int len;
59
60
    struct usbredirparser_buf *next;
61
};
62
63
struct usbredirparser_priv {
64
    struct usbredirparser callb;
65
    int flags;
66
67
    int have_peer_caps;
68
    uint32_t our_caps[USB_REDIR_CAPS_SIZE];
69
    uint32_t peer_caps[USB_REDIR_CAPS_SIZE];
70
71
    void *lock;
72
73
    union {
74
        struct usb_redir_header header;
75
        struct usb_redir_header_32bit_id header_32bit_id;
76
    };
77
    uint8_t type_header[288];
78
    int header_read;
79
    int type_header_len;
80
    int type_header_read;
81
    uint8_t *data;
82
    int data_len;
83
    int data_read;
84
    int to_skip;
85
    int write_buf_count;
86
    struct usbredirparser_buf *write_buf;
87
    uint64_t write_buf_total_size;
88
};
89
90
static void
91
#if defined __MINGW_PRINTF_FORMAT
92
__attribute__((format(__MINGW_PRINTF_FORMAT, 3, 4)))
93
#elif defined __GNUC__
94
__attribute__((format(printf, 3, 4)))
95
#endif
96
va_log(struct usbredirparser_priv *parser, int verbose, const char *fmt, ...)
97
829k
{
98
829k
    char buf[512];
99
829k
    va_list ap;
100
829k
    int n;
101
102
829k
    n = sprintf(buf, "usbredirparser: ");
103
829k
    va_start(ap, fmt);
104
829k
    vsnprintf(buf + n, sizeof(buf) - n, fmt, ap);
105
829k
    va_end(ap);
106
107
829k
    parser->callb.log_func(parser->callb.priv, verbose, buf);
108
829k
}
109
110
130k
#define ERROR(...)   va_log(parser, usbredirparser_error, __VA_ARGS__)
111
8
#define WARNING(...) va_log(parser, usbredirparser_warning, __VA_ARGS__)
112
158
#define INFO(...)    va_log(parser, usbredirparser_info, __VA_ARGS__)
113
698k
#define DEBUG(...)    va_log(parser, usbredirparser_debug, __VA_ARGS__)
114
115
static inline void
116
usbredirparser_assert_invariants(const struct usbredirparser_priv *parser)
117
257k
{
118
257k
#ifdef ENABLE_EXTRA_CHECKS
119
257k
    assert(parser != NULL);
120
257k
    assert(parser->header_read >= 0);
121
257k
    assert(parser->header_read <= sizeof(parser->header));
122
257k
    assert(parser->type_header_read >= 0);
123
257k
    assert(parser->type_header_len <= sizeof(parser->type_header));
124
257k
    assert(parser->type_header_read <= parser->type_header_len);
125
257k
    assert(parser->data_len >= 0);
126
257k
    assert(parser->data_len <= MAX_PACKET_SIZE);
127
257k
    assert(parser->data_read >= 0);
128
257k
    assert(parser->data_read <= parser->data_len);
129
257k
    assert((parser->data_len != 0) ^ (parser->data == NULL));
130
131
257k
    int write_buf_count = 0;
132
257k
    uint64_t total_size = 0;
133
257k
    const struct usbredirparser_buf *write_buf = parser->write_buf;
134
349k
    for (; write_buf != NULL ; write_buf = write_buf->next) {
135
91.4k
        assert(write_buf->pos >= 0);
136
91.4k
        assert(write_buf->len >= 0);
137
91.4k
        assert(write_buf->pos <= write_buf->len);
138
91.4k
        assert(write_buf->len == 0 || write_buf->buf != NULL);
139
91.4k
        write_buf_count++;
140
91.4k
        total_size += write_buf->len;
141
91.4k
    }
142
257k
    assert(parser->write_buf_count == write_buf_count);
143
257k
    assert(parser->write_buf_total_size == total_size);
144
257k
#endif
145
257k
}
146
147
#if 0 /* Can be enabled and called from random place to test serialization */
148
static void serialize_test(struct usbredirparser *parser_pub)
149
{
150
    struct usbredirparser_priv *parser =
151
        (struct usbredirparser_priv *)parser_pub;
152
    struct usbredirparser_buf *wbuf, *next_wbuf;
153
    uint8_t *data;
154
    int len;
155
156
    if (usbredirparser_serialize(parser_pub, &data, &len))
157
        return;
158
159
    wbuf = parser->write_buf;
160
    while (wbuf) {
161
        next_wbuf = wbuf->next;
162
        free(wbuf->buf);
163
        free(wbuf);
164
        wbuf = next_wbuf;
165
    }
166
    parser->write_buf = NULL;
167
    parser->write_buf_count = 0;
168
169
    free(parser->data);
170
    parser->data = NULL;
171
172
    parser->type_header_len = parser->data_len = parser->have_peer_caps = 0;
173
174
    usbredirparser_unserialize(parser_pub, data, len);
175
    free(data);
176
}
177
#endif
178
179
static void usbredirparser_queue(struct usbredirparser *parser, uint32_t type,
180
    uint64_t id, void *type_header_in, uint8_t *data_in, int data_len);
181
static int usbredirparser_caps_get_cap(struct usbredirparser_priv *parser,
182
    uint32_t *caps, int cap);
183
184
USBREDIR_VISIBLE
185
struct usbredirparser *usbredirparser_create(void)
186
3.10k
{
187
3.10k
    return calloc(1, sizeof(struct usbredirparser_priv));
188
3.10k
}
189
190
static void usbredirparser_verify_caps(struct usbredirparser_priv *parser,
191
    uint32_t *caps, const char *desc)
192
3.18k
{
193
3.18k
    if (usbredirparser_caps_get_cap(parser, caps,
194
3.18k
                                    usb_redir_cap_bulk_streams) &&
195
3.18k
        !usbredirparser_caps_get_cap(parser, caps,
196
1.29k
                                     usb_redir_cap_ep_info_max_packet_size)) {
197
426
        ERROR("error %s caps contains cap_bulk_streams without"
198
426
              "cap_ep_info_max_packet_size", desc);
199
426
        caps[0] &= ~(1 << usb_redir_cap_bulk_streams);
200
426
    }
201
3.18k
}
202
203
USBREDIR_VISIBLE
204
void usbredirparser_init(struct usbredirparser *parser_pub,
205
    const char *version, uint32_t *caps, int caps_len, int flags)
206
3.10k
{
207
3.10k
    struct usbredirparser_priv *parser =
208
3.10k
        (struct usbredirparser_priv *)parser_pub;
209
3.10k
    struct usb_redir_hello_header hello = { { 0 }, };
210
211
3.10k
    parser->flags = (flags & ~usbredirparser_fl_no_hello);
212
3.10k
    if (parser->callb.alloc_lock_func) {
213
3.10k
        parser->lock = parser->callb.alloc_lock_func();
214
3.10k
    }
215
216
3.10k
    snprintf(hello.version, sizeof(hello.version), "%s", version);
217
3.10k
    if (caps_len > USB_REDIR_CAPS_SIZE) {
218
0
        caps_len = USB_REDIR_CAPS_SIZE;
219
0
    }
220
3.10k
    memcpy(parser->our_caps, caps, caps_len * sizeof(uint32_t));
221
    /* libusbredirparser handles sending the ack internally */
222
3.10k
    if (!(flags & usbredirparser_fl_usb_host))
223
1.73k
        usbredirparser_caps_set_cap(parser->our_caps,
224
1.73k
                                    usb_redir_cap_device_disconnect_ack);
225
3.10k
    usbredirparser_verify_caps(parser, parser->our_caps, "our");
226
3.10k
    if (!(flags & usbredirparser_fl_no_hello))
227
1.33k
        usbredirparser_queue(parser_pub, usb_redir_hello, 0, &hello,
228
1.33k
                             (uint8_t *)parser->our_caps,
229
1.33k
                             USB_REDIR_CAPS_SIZE * sizeof(uint32_t));
230
3.10k
}
231
232
USBREDIR_VISIBLE
233
void usbredirparser_destroy(struct usbredirparser *parser_pub)
234
3.10k
{
235
3.10k
    struct usbredirparser_priv *parser =
236
3.10k
        (struct usbredirparser_priv *)parser_pub;
237
3.10k
    struct usbredirparser_buf *wbuf, *next_wbuf;
238
239
3.10k
    free(parser->data);
240
3.10k
    parser->data = NULL;
241
242
3.10k
    wbuf = parser->write_buf;
243
8.46k
    while (wbuf) {
244
5.36k
        next_wbuf = wbuf->next;
245
5.36k
        free(wbuf->buf);
246
5.36k
        free(wbuf);
247
5.36k
        wbuf = next_wbuf;
248
5.36k
    }
249
250
3.10k
    if (parser->lock)
251
0
        parser->callb.free_lock_func(parser->lock);
252
253
3.10k
    free(parser);
254
3.10k
}
255
256
USBREDIR_VISIBLE
257
uint64_t usbredirparser_get_bufferered_output_size(struct usbredirparser *parser_pub)
258
0
{
259
0
    struct usbredirparser_priv *parser =
260
0
        (struct usbredirparser_priv *)parser_pub;
261
0
    uint64_t size;
262
263
0
    LOCK(parser);
264
0
    size = parser->write_buf_total_size;
265
0
    UNLOCK(parser);
266
0
    return size;
267
0
}
268
269
static int usbredirparser_caps_get_cap(struct usbredirparser_priv *parser,
270
    uint32_t *caps, int cap)
271
1.04M
{
272
1.04M
    if (cap / 32 >= USB_REDIR_CAPS_SIZE) {
273
0
        ERROR("error request for out of bounds cap: %d", cap);
274
0
        return 0;
275
0
    }
276
1.04M
    if (caps[cap / 32] & (1 << (cap % 32))) {
277
825k
        return 1;
278
825k
    } else {
279
223k
        return 0;
280
223k
    }
281
1.04M
}
282
283
USBREDIR_VISIBLE
284
void usbredirparser_caps_set_cap(uint32_t *caps, int cap)
285
1.73k
{
286
1.73k
    caps[cap / 32] |= 1 << (cap % 32);
287
1.73k
}
288
289
USBREDIR_VISIBLE
290
int usbredirparser_have_peer_caps(struct usbredirparser *parser_pub)
291
0
{
292
0
    struct usbredirparser_priv *parser =
293
0
        (struct usbredirparser_priv *)parser_pub;
294
295
0
    return parser->have_peer_caps;
296
0
}
297
298
USBREDIR_VISIBLE
299
int usbredirparser_peer_has_cap(struct usbredirparser *parser_pub, int cap)
300
519k
{
301
519k
    struct usbredirparser_priv *parser =
302
519k
        (struct usbredirparser_priv *)parser_pub;
303
519k
    return usbredirparser_caps_get_cap(parser, parser->peer_caps, cap);
304
519k
}
305
306
USBREDIR_VISIBLE
307
int usbredirparser_have_cap(struct usbredirparser *parser_pub, int cap)
308
524k
{
309
524k
    struct usbredirparser_priv *parser =
310
524k
        (struct usbredirparser_priv *)parser_pub;
311
524k
    return usbredirparser_caps_get_cap(parser, parser->our_caps, cap);
312
524k
}
313
314
static int usbredirparser_using_32bits_ids(struct usbredirparser *parser_pub)
315
510k
{
316
510k
    return !usbredirparser_have_cap(parser_pub, usb_redir_cap_64bits_ids) ||
317
510k
           !usbredirparser_peer_has_cap(parser_pub, usb_redir_cap_64bits_ids);
318
510k
}
319
320
static void usbredirparser_handle_hello(struct usbredirparser *parser_pub,
321
    struct usb_redir_hello_header *hello, uint8_t *data, int data_len)
322
1.14k
{
323
1.14k
    struct usbredirparser_priv *parser =
324
1.14k
        (struct usbredirparser_priv *)parser_pub;
325
1.14k
    uint32_t *peer_caps = (uint32_t *)data;
326
1.14k
    char buf[64];
327
1.14k
    int i;
328
329
1.14k
    if (parser->have_peer_caps) {
330
1.07k
        ERROR("Received second hello message, ignoring");
331
1.07k
        return;
332
1.07k
    }
333
334
    /* In case hello->version is not 0 terminated (which would be a protocol
335
       violation)_ */
336
79
    strncpy(buf, hello->version, sizeof(buf));
337
79
    buf[sizeof(buf)-1] = '\0';
338
339
79
    memset(parser->peer_caps, 0, sizeof(parser->peer_caps));
340
79
    if (data_len > sizeof(parser->peer_caps)) {
341
33
        data_len = sizeof(parser->peer_caps);
342
33
    }
343
138
    for (i = 0; i < data_len / sizeof(uint32_t); i++) {
344
59
        parser->peer_caps[i] = peer_caps[i];
345
59
    }
346
79
    usbredirparser_verify_caps(parser, parser->peer_caps, "peer");
347
79
    parser->have_peer_caps = 1;
348
349
79
    INFO("Peer version: %s, using %d-bits ids", buf,
350
79
         usbredirparser_using_32bits_ids(parser_pub) ? 32 : 64);
351
352
    /* Added in 0.3.2, so no guarantee it is there */
353
79
    if (parser->callb.hello_func)
354
79
        parser->callb.hello_func(parser->callb.priv, hello);
355
79
}
356
357
static int usbredirparser_get_header_len(struct usbredirparser *parser_pub)
358
319k
{
359
319k
    if (usbredirparser_using_32bits_ids(parser_pub))
360
169k
        return sizeof(struct usb_redir_header_32bit_id);
361
149k
    else
362
149k
        return sizeof(struct usb_redir_header);
363
319k
}
364
365
static int usbredirparser_get_type_header_len(
366
    struct usbredirparser *parser_pub, int32_t type, int send)
367
317k
{
368
317k
    struct usbredirparser_priv *parser =
369
317k
        (struct usbredirparser_priv *)parser_pub;
370
317k
    int command_for_host = 0;
371
372
317k
    if (parser->flags & usbredirparser_fl_usb_host) {
373
22.4k
        command_for_host = 1;
374
22.4k
    }
375
317k
    if (send) {
376
89.3k
        command_for_host = !command_for_host;
377
89.3k
    }
378
379
317k
    switch (type) {
380
100k
    case usb_redir_hello:
381
100k
        return sizeof(struct usb_redir_hello_header);
382
1.21k
    case usb_redir_device_connect:
383
1.21k
        if (!command_for_host) {
384
908
            if (usbredirparser_have_cap(parser_pub,
385
908
                                    usb_redir_cap_connect_device_version) &&
386
908
                usbredirparser_peer_has_cap(parser_pub,
387
638
                                    usb_redir_cap_connect_device_version)) {
388
213
                return sizeof(struct usb_redir_device_connect_header);
389
695
            } else {
390
695
                return sizeof(struct usb_redir_device_connect_header_no_device_version);
391
695
            }
392
908
        } else {
393
309
            return -1;
394
309
        }
395
88.8k
    case usb_redir_device_disconnect:
396
88.8k
        if (!command_for_host) {
397
88.4k
            return 0;
398
88.4k
        } else {
399
435
            return -1;
400
435
        }
401
471
    case usb_redir_reset:
402
471
        if (command_for_host) {
403
251
            return 0; /* No packet type specific header */
404
251
        } else {
405
220
            return -1;
406
220
        }
407
883
    case usb_redir_interface_info:
408
883
        if (!command_for_host) {
409
689
            return sizeof(struct usb_redir_interface_info_header);
410
689
        } else {
411
194
            return -1;
412
194
        }
413
1.57k
    case usb_redir_ep_info:
414
1.57k
        if (!command_for_host) {
415
1.22k
            if (usbredirparser_have_cap(parser_pub,
416
1.22k
                                    usb_redir_cap_bulk_streams) &&
417
1.22k
                usbredirparser_peer_has_cap(parser_pub,
418
459
                                    usb_redir_cap_bulk_streams)) {
419
223
                return sizeof(struct usb_redir_ep_info_header);
420
998
            } else if (usbredirparser_have_cap(parser_pub,
421
998
                                    usb_redir_cap_ep_info_max_packet_size) &&
422
998
                       usbredirparser_peer_has_cap(parser_pub,
423
545
                                    usb_redir_cap_ep_info_max_packet_size)) {
424
250
                return sizeof(struct usb_redir_ep_info_header_no_max_streams);
425
748
            } else {
426
748
                return sizeof(struct usb_redir_ep_info_header_no_max_pktsz);
427
748
            }
428
1.22k
        } else {
429
355
            return -1;
430
355
        }
431
606
    case usb_redir_set_configuration:
432
606
        if (command_for_host) {
433
398
            return sizeof(struct usb_redir_set_configuration_header);
434
398
        } else {
435
208
            return -1; /* Should never be send to a guest */
436
208
        }
437
500
    case usb_redir_get_configuration:
438
500
        if (command_for_host) {
439
303
            return 0; /* No packet type specific header */
440
303
        } else {
441
197
            return -1;
442
197
        }
443
628
    case usb_redir_configuration_status:
444
628
        if (!command_for_host) {
445
399
            return sizeof(struct usb_redir_configuration_status_header);
446
399
        } else {
447
229
            return -1;
448
229
        }
449
599
    case usb_redir_set_alt_setting:
450
599
        if (command_for_host) {
451
404
            return sizeof(struct usb_redir_set_alt_setting_header);
452
404
        } else {
453
195
            return -1;
454
195
        }
455
1.22k
    case usb_redir_get_alt_setting:
456
1.22k
        if (command_for_host) {
457
1.02k
            return sizeof(struct usb_redir_get_alt_setting_header);
458
1.02k
        } else {
459
199
            return -1;
460
199
        }
461
600
    case usb_redir_alt_setting_status:
462
600
        if (!command_for_host) {
463
403
            return sizeof(struct usb_redir_alt_setting_status_header);
464
403
        } else {
465
197
            return -1;
466
197
        }
467
774
    case usb_redir_start_iso_stream:
468
774
        if (command_for_host) {
469
543
            return sizeof(struct usb_redir_start_iso_stream_header);
470
543
        } else {
471
231
            return -1;
472
231
        }
473
714
    case usb_redir_stop_iso_stream:
474
714
        if (command_for_host) {
475
390
            return sizeof(struct usb_redir_stop_iso_stream_header);
476
390
        } else {
477
324
            return -1;
478
324
        }
479
597
    case usb_redir_iso_stream_status:
480
597
        if (!command_for_host) {
481
402
            return sizeof(struct usb_redir_iso_stream_status_header);
482
402
        } else {
483
195
            return -1;
484
195
        }
485
1.04k
    case usb_redir_start_interrupt_receiving:
486
1.04k
        if (command_for_host) {
487
833
            return sizeof(struct usb_redir_start_interrupt_receiving_header);
488
833
        } else {
489
211
            return -1;
490
211
        }
491
1.18k
    case usb_redir_stop_interrupt_receiving:
492
1.18k
        if (command_for_host) {
493
990
            return sizeof(struct usb_redir_stop_interrupt_receiving_header);
494
990
        } else {
495
199
            return -1;
496
199
        }
497
835
    case usb_redir_interrupt_receiving_status:
498
835
        if (!command_for_host) {
499
636
            return sizeof(struct usb_redir_interrupt_receiving_status_header);
500
636
        } else {
501
199
            return -1;
502
199
        }
503
718
    case usb_redir_alloc_bulk_streams:
504
718
        if (command_for_host) {
505
393
            return sizeof(struct usb_redir_alloc_bulk_streams_header);
506
393
        } else {
507
325
            return -1;
508
325
        }
509
783
    case usb_redir_free_bulk_streams:
510
783
        if (command_for_host) {
511
585
            return sizeof(struct usb_redir_free_bulk_streams_header);
512
585
        } else {
513
198
            return -1;
514
198
        }
515
590
    case usb_redir_bulk_streams_status:
516
590
        if (!command_for_host) {
517
396
            return sizeof(struct usb_redir_bulk_streams_status_header);
518
396
        } else {
519
194
            return -1;
520
194
        }
521
548
    case usb_redir_cancel_data_packet:
522
548
        if (command_for_host) {
523
351
            return 0; /* No packet type specific header */
524
351
        } else {
525
197
            return -1;
526
197
        }
527
765
    case usb_redir_filter_reject:
528
765
        if (command_for_host) {
529
569
            return 0;
530
569
        } else {
531
196
            return -1;
532
196
        }
533
4.14k
    case usb_redir_filter_filter:
534
4.14k
        return 0;
535
89.1k
    case usb_redir_device_disconnect_ack:
536
89.1k
        if (command_for_host) {
537
88.9k
            return 0;
538
88.9k
        } else {
539
197
            return -1;
540
197
        }
541
1.28k
    case usb_redir_start_bulk_receiving:
542
1.28k
        if (command_for_host) {
543
1.05k
            return sizeof(struct usb_redir_start_bulk_receiving_header);
544
1.05k
        } else {
545
227
            return -1;
546
227
        }
547
980
    case usb_redir_stop_bulk_receiving:
548
980
        if (command_for_host) {
549
786
            return sizeof(struct usb_redir_stop_bulk_receiving_header);
550
786
        } else {
551
194
            return -1;
552
194
        }
553
1.11k
    case usb_redir_bulk_receiving_status:
554
1.11k
        if (!command_for_host) {
555
906
            return sizeof(struct usb_redir_bulk_receiving_status_header);
556
906
        } else {
557
206
            return -1;
558
206
        }
559
783
    case usb_redir_control_packet:
560
783
        return sizeof(struct usb_redir_control_packet_header);
561
1.72k
    case usb_redir_bulk_packet:
562
1.72k
        if (usbredirparser_have_cap(parser_pub,
563
1.72k
                                usb_redir_cap_32bits_bulk_length) &&
564
1.72k
            usbredirparser_peer_has_cap(parser_pub,
565
1.20k
                                usb_redir_cap_32bits_bulk_length)) {
566
593
            return sizeof(struct usb_redir_bulk_packet_header);
567
1.13k
        } else {
568
1.13k
            return sizeof(struct usb_redir_bulk_packet_header_16bit_length);
569
1.13k
        }
570
1.11k
    case usb_redir_iso_packet:
571
1.11k
        return sizeof(struct usb_redir_iso_packet_header);
572
1.31k
    case usb_redir_interrupt_packet:
573
1.31k
        return sizeof(struct usb_redir_interrupt_packet_header);
574
1.45k
    case usb_redir_buffered_bulk_packet:
575
1.45k
        if (!command_for_host) {
576
1.25k
            return sizeof(struct usb_redir_buffered_bulk_packet_header);
577
1.25k
        } else {
578
198
            return -1;
579
198
        }
580
8.67k
    default:
581
8.67k
        return -1;
582
317k
    }
583
317k
}
584
585
/* Note this function only checks if extra data is allowed for the
586
   packet type being read at all, a check if it is actually allowed
587
   given the direction of the packet + ep is done in _verify_type_header */
588
static int usbredirparser_expect_extra_data(struct usbredirparser_priv *parser)
589
7.28k
{
590
7.28k
    switch (parser->header.type) {
591
1.16k
    case usb_redir_hello: /* For the variable length capabilities array */
592
4.87k
    case usb_redir_filter_filter:
593
5.21k
    case usb_redir_control_packet:
594
5.51k
    case usb_redir_bulk_packet:
595
5.83k
    case usb_redir_iso_packet:
596
6.30k
    case usb_redir_interrupt_packet:
597
6.78k
    case usb_redir_buffered_bulk_packet:
598
6.78k
        return 1;
599
503
    default:
600
503
        return 0;
601
7.28k
    }
602
7.28k
}
603
604
static int usbredirparser_verify_bulk_recv_cap(
605
    struct usbredirparser *parser_pub, int send)
606
3.20k
{
607
3.20k
    struct usbredirparser_priv *parser =
608
3.20k
        (struct usbredirparser_priv *)parser_pub;
609
610
3.20k
    if ((send && !usbredirparser_peer_has_cap(parser_pub,
611
0
                                              usb_redir_cap_bulk_receiving)) ||
612
3.20k
        (!send && !usbredirparser_have_cap(parser_pub,
613
3.20k
                                           usb_redir_cap_bulk_receiving))) {
614
850
        ERROR("error bulk_receiving without cap_bulk_receiving");
615
850
        return 0;
616
850
    }
617
2.35k
    return 1; /* Verify ok */
618
3.20k
}
619
620
static int usbredirparser_verify_type_header(
621
    struct usbredirparser *parser_pub,
622
    int32_t type, void *header, uint8_t *data, int data_len, int send)
623
197k
{
624
197k
    struct usbredirparser_priv *parser =
625
197k
        (struct usbredirparser_priv *)parser_pub;
626
197k
    int command_for_host = 0, expect_extra_data = 0;
627
197k
    uint32_t length = 0;
628
197k
    int ep = -1;
629
630
197k
    if (parser->flags & usbredirparser_fl_usb_host) {
631
12.0k
        command_for_host = 1;
632
12.0k
    }
633
197k
    if (send) {
634
89.3k
        command_for_host = !command_for_host;
635
89.3k
    }
636
637
197k
    switch (type) {
638
445
    case usb_redir_interface_info: {
639
445
        struct usb_redir_interface_info_header *intf_info = header;
640
641
445
        if (intf_info->interface_count > 32) {
642
241
            ERROR("error interface_count > 32");
643
241
            return 0;
644
241
        }
645
204
        break;
646
445
    }
647
798
    case usb_redir_start_interrupt_receiving: {
648
798
        struct usb_redir_start_interrupt_receiving_header *start_int = header;
649
650
798
        if (!(start_int->endpoint & 0x80)) {
651
195
            ERROR("start int receiving on non input ep %02x",
652
195
                  start_int->endpoint);
653
195
            return 0;
654
195
        }
655
603
        break;
656
798
    }
657
967
    case usb_redir_stop_interrupt_receiving: {
658
967
        struct usb_redir_stop_interrupt_receiving_header *stop_int = header;
659
660
967
        if (!(stop_int->endpoint & 0x80)) {
661
553
            ERROR("stop int receiving on non input ep %02x",
662
553
                  stop_int->endpoint);
663
553
            return 0;
664
553
        }
665
414
        break;
666
967
    }
667
414
    case usb_redir_interrupt_receiving_status: {
668
402
        struct usb_redir_interrupt_receiving_status_header *int_status = header;
669
670
402
        if (!(int_status->endpoint & 0x80)) {
671
203
            ERROR("int receiving status for non input ep %02x",
672
203
                  int_status->endpoint);
673
203
            return 0;
674
203
        }
675
199
        break;
676
402
    }
677
502
    case usb_redir_filter_reject:
678
502
        if ((send && !usbredirparser_peer_has_cap(parser_pub,
679
0
                                             usb_redir_cap_filter)) ||
680
502
            (!send && !usbredirparser_have_cap(parser_pub,
681
502
                                             usb_redir_cap_filter))) {
682
302
            ERROR("error filter_reject without cap_filter");
683
302
            return 0;
684
302
        }
685
200
        break;
686
3.94k
    case usb_redir_filter_filter:
687
3.94k
        if ((send && !usbredirparser_peer_has_cap(parser_pub,
688
0
                                             usb_redir_cap_filter)) ||
689
3.94k
            (!send && !usbredirparser_have_cap(parser_pub,
690
3.94k
                                             usb_redir_cap_filter))) {
691
1.05k
            ERROR("error filter_filter without cap_filter");
692
1.05k
            return 0;
693
1.05k
        }
694
2.89k
        if (data_len < 1) {
695
196
            ERROR("error filter_filter without data");
696
196
            return 0;
697
196
        }
698
2.69k
        if (data[data_len - 1] != 0) {
699
259
            ERROR("error non 0 terminated filter_filter data");
700
259
            return 0;
701
259
        }
702
2.43k
        break;
703
88.6k
    case usb_redir_device_disconnect_ack:
704
88.6k
        if ((send && !usbredirparser_peer_has_cap(parser_pub,
705
88.0k
                                     usb_redir_cap_device_disconnect_ack)) ||
706
88.6k
            (!send && !usbredirparser_have_cap(parser_pub,
707
672
                                     usb_redir_cap_device_disconnect_ack))) {
708
195
            ERROR("error device_disconnect_ack without cap_device_disconnect_ack");
709
195
            return 0;
710
195
        }
711
88.5k
        break;
712
88.5k
    case usb_redir_start_bulk_receiving: {
713
907
        struct usb_redir_start_bulk_receiving_header *start_bulk = header;
714
715
907
        if (!usbredirparser_verify_bulk_recv_cap(parser_pub, send)) {
716
212
            return 0;
717
212
        }
718
695
        if (start_bulk->bytes_per_transfer > MAX_BULK_TRANSFER_SIZE) {
719
227
            ERROR("start bulk receiving length exceeds limits %u > %u",
720
227
                  start_bulk->bytes_per_transfer, MAX_BULK_TRANSFER_SIZE);
721
227
            return 0;
722
227
        }
723
468
        if (!(start_bulk->endpoint & 0x80)) {
724
254
            ERROR("start bulk receiving on non input ep %02x",
725
254
                  start_bulk->endpoint);
726
254
            return 0;
727
254
        }
728
214
        break;
729
468
    }
730
591
    case usb_redir_stop_bulk_receiving: {
731
591
        struct usb_redir_stop_bulk_receiving_header *stop_bulk = header;
732
733
591
        if (!usbredirparser_verify_bulk_recv_cap(parser_pub, send)) {
734
194
            return 0;
735
194
        }
736
397
        if (!(stop_bulk->endpoint & 0x80)) {
737
197
            ERROR("stop bulk receiving on non input ep %02x",
738
197
                  stop_bulk->endpoint);
739
197
            return 0;
740
197
        }
741
200
        break;
742
397
    }
743
681
    case usb_redir_bulk_receiving_status: {
744
681
        struct usb_redir_bulk_receiving_status_header *bulk_status = header;
745
746
681
        if (!usbredirparser_verify_bulk_recv_cap(parser_pub, send)) {
747
249
            return 0;
748
249
        }
749
432
        if (!(bulk_status->endpoint & 0x80)) {
750
218
            ERROR("bulk receiving status for non input ep %02x",
751
218
                  bulk_status->endpoint);
752
218
            return 0;
753
218
        }
754
214
        break;
755
432
    }
756
579
    case usb_redir_control_packet:
757
579
        length = ((struct usb_redir_control_packet_header *)header)->length;
758
579
        ep = ((struct usb_redir_control_packet_header *)header)->endpoint;
759
579
        break;
760
1.07k
    case usb_redir_bulk_packet: {
761
1.07k
        struct usb_redir_bulk_packet_header *bulk_packet = header;
762
1.07k
        if (usbredirparser_have_cap(parser_pub,
763
1.07k
                                usb_redir_cap_32bits_bulk_length) &&
764
1.07k
            usbredirparser_peer_has_cap(parser_pub,
765
779
                                usb_redir_cap_32bits_bulk_length)) {
766
402
            length = (((uint32_t)bulk_packet->length_high) << 16) | bulk_packet->length;
767
675
        } else {
768
675
            length = bulk_packet->length;
769
675
            if (!send)
770
675
                bulk_packet->length_high = 0;
771
675
        }
772
1.07k
        if (length > MAX_BULK_TRANSFER_SIZE) {
773
321
            ERROR("bulk transfer length exceeds limits %u > %u",
774
321
                  (uint32_t)length, MAX_BULK_TRANSFER_SIZE);
775
321
            return 0;
776
321
        }
777
756
        ep = bulk_packet->endpoint;
778
756
        break;
779
1.07k
    }
780
894
    case usb_redir_iso_packet:
781
894
        length = ((struct usb_redir_iso_packet_header *)header)->length;
782
894
        ep = ((struct usb_redir_iso_packet_header *)header)->endpoint;
783
894
        break;
784
1.22k
    case usb_redir_interrupt_packet:
785
1.22k
        length = ((struct usb_redir_interrupt_packet_header *)header)->length;
786
1.22k
        ep = ((struct usb_redir_interrupt_packet_header *)header)->endpoint;
787
1.22k
        break;
788
1.02k
    case usb_redir_buffered_bulk_packet: {
789
1.02k
        struct usb_redir_buffered_bulk_packet_header *buf_bulk_pkt = header;
790
1.02k
        length = buf_bulk_pkt->length;
791
1.02k
        if (!usbredirparser_verify_bulk_recv_cap(parser_pub, send)) {
792
195
            return 0;
793
195
        }
794
829
        if ((uint32_t)length > MAX_BULK_TRANSFER_SIZE) {
795
206
            ERROR("buffered bulk transfer length exceeds limits %u > %u",
796
206
                  (uint32_t)length, MAX_BULK_TRANSFER_SIZE);
797
206
            return 0;
798
206
        }
799
623
        ep = buf_bulk_pkt->endpoint;
800
623
        break;
801
829
    }
802
197k
    }
803
804
192k
    if (ep != -1) {
805
4.07k
        if (((ep & 0x80) && !command_for_host) ||
806
4.07k
            (!(ep & 0x80) && command_for_host)) {
807
2.03k
            expect_extra_data = 1;
808
2.03k
        }
809
4.07k
        if (expect_extra_data) {
810
2.03k
            if (data_len != length) {
811
503
                ERROR("error data len %d != header len %d ep %02X",
812
503
                      data_len, length, ep);
813
503
                return 0;
814
503
            }
815
2.03k
        } else {
816
2.03k
            if (data || data_len) {
817
426
                ERROR("error unexpected extra data ep %02X", ep);
818
426
                return 0;
819
426
            }
820
1.60k
            switch (type) {
821
194
            case usb_redir_iso_packet:
822
194
                ERROR("error iso packet send in wrong direction");
823
194
                return 0;
824
519
            case usb_redir_interrupt_packet:
825
519
                if (command_for_host) {
826
195
                    ERROR("error interrupt packet send in wrong direction");
827
195
                    return 0;
828
195
                }
829
324
                break;
830
324
            case usb_redir_buffered_bulk_packet:
831
205
                ERROR("error buffered bulk packet send in wrong direction");
832
205
                return 0;
833
1.60k
            }
834
1.60k
        }
835
4.07k
    }
836
837
190k
    return 1; /* Verify ok */
838
192k
}
839
840
static void usbredirparser_call_type_func(struct usbredirparser *parser_pub,
841
    bool *data_ownership_transferred)
842
101k
{
843
101k
    struct usbredirparser_priv *parser =
844
101k
        (struct usbredirparser_priv *)parser_pub;
845
101k
    uint64_t id;
846
847
101k
    if (usbredirparser_using_32bits_ids(parser_pub))
848
28.1k
        id = parser->header_32bit_id.id;
849
73.3k
    else
850
73.3k
        id = parser->header.id;
851
852
101k
    switch (parser->header.type) {
853
1.14k
    case usb_redir_hello:
854
1.14k
        usbredirparser_handle_hello(parser_pub,
855
1.14k
            (struct usb_redir_hello_header *)parser->type_header,
856
1.14k
            parser->data, parser->data_len);
857
1.14k
        break;
858
220
    case usb_redir_device_connect:
859
220
        parser->callb.device_connect_func(parser->callb.priv,
860
220
            (struct usb_redir_device_connect_header *)parser->type_header);
861
220
        break;
862
88.4k
    case usb_redir_device_disconnect:
863
88.4k
        parser->callb.device_disconnect_func(parser->callb.priv);
864
88.4k
        if (usbredirparser_peer_has_cap(parser_pub,
865
88.4k
                                        usb_redir_cap_device_disconnect_ack))
866
88.0k
            usbredirparser_queue(parser_pub, usb_redir_device_disconnect_ack,
867
88.0k
                                 0, NULL, NULL, 0);
868
88.4k
        break;
869
250
    case usb_redir_reset:
870
250
        parser->callb.reset_func(parser->callb.priv);
871
250
        break;
872
204
    case usb_redir_interface_info:
873
204
        parser->callb.interface_info_func(parser->callb.priv,
874
204
            (struct usb_redir_interface_info_header *)parser->type_header);
875
204
        break;
876
242
    case usb_redir_ep_info:
877
242
        parser->callb.ep_info_func(parser->callb.priv,
878
242
            (struct usb_redir_ep_info_header *)parser->type_header);
879
242
        break;
880
336
    case usb_redir_set_configuration:
881
336
        parser->callb.set_configuration_func(parser->callb.priv, id,
882
336
            (struct usb_redir_set_configuration_header *)parser->type_header);
883
336
        break;
884
195
    case usb_redir_get_configuration:
885
195
        parser->callb.get_configuration_func(parser->callb.priv, id);
886
195
        break;
887
227
    case usb_redir_configuration_status:
888
227
        parser->callb.configuration_status_func(parser->callb.priv, id,
889
227
          (struct usb_redir_configuration_status_header *)parser->type_header);
890
227
        break;
891
195
    case usb_redir_set_alt_setting:
892
195
        parser->callb.set_alt_setting_func(parser->callb.priv, id,
893
195
            (struct usb_redir_set_alt_setting_header *)parser->type_header);
894
195
        break;
895
709
    case usb_redir_get_alt_setting:
896
709
        parser->callb.get_alt_setting_func(parser->callb.priv, id,
897
709
            (struct usb_redir_get_alt_setting_header *)parser->type_header);
898
709
        break;
899
199
    case usb_redir_alt_setting_status:
900
199
        parser->callb.alt_setting_status_func(parser->callb.priv, id,
901
199
            (struct usb_redir_alt_setting_status_header *)parser->type_header);
902
199
        break;
903
346
    case usb_redir_start_iso_stream:
904
346
        parser->callb.start_iso_stream_func(parser->callb.priv, id,
905
346
            (struct usb_redir_start_iso_stream_header *)parser->type_header);
906
346
        break;
907
194
    case usb_redir_stop_iso_stream:
908
194
        parser->callb.stop_iso_stream_func(parser->callb.priv, id,
909
194
            (struct usb_redir_stop_iso_stream_header *)parser->type_header);
910
194
        break;
911
200
    case usb_redir_iso_stream_status:
912
200
        parser->callb.iso_stream_status_func(parser->callb.priv, id,
913
200
            (struct usb_redir_iso_stream_status_header *)parser->type_header);
914
200
        break;
915
603
    case usb_redir_start_interrupt_receiving:
916
603
        parser->callb.start_interrupt_receiving_func(parser->callb.priv, id,
917
603
            (struct usb_redir_start_interrupt_receiving_header *)
918
603
            parser->type_header);
919
603
        break;
920
414
    case usb_redir_stop_interrupt_receiving:
921
414
        parser->callb.stop_interrupt_receiving_func(parser->callb.priv, id,
922
414
            (struct usb_redir_stop_interrupt_receiving_header *)
923
414
            parser->type_header);
924
414
        break;
925
199
    case usb_redir_interrupt_receiving_status:
926
199
        parser->callb.interrupt_receiving_status_func(parser->callb.priv, id,
927
199
            (struct usb_redir_interrupt_receiving_status_header *)
928
199
            parser->type_header);
929
199
        break;
930
194
    case usb_redir_alloc_bulk_streams:
931
194
        parser->callb.alloc_bulk_streams_func(parser->callb.priv, id,
932
194
            (struct usb_redir_alloc_bulk_streams_header *)parser->type_header);
933
194
        break;
934
194
    case usb_redir_free_bulk_streams:
935
194
        parser->callb.free_bulk_streams_func(parser->callb.priv, id,
936
194
            (struct usb_redir_free_bulk_streams_header *)parser->type_header);
937
194
        break;
938
194
    case usb_redir_bulk_streams_status:
939
194
        parser->callb.bulk_streams_status_func(parser->callb.priv, id,
940
194
          (struct usb_redir_bulk_streams_status_header *)parser->type_header);
941
194
        break;
942
323
    case usb_redir_cancel_data_packet:
943
323
        parser->callb.cancel_data_packet_func(parser->callb.priv, id);
944
323
        break;
945
200
    case usb_redir_filter_reject:
946
200
        parser->callb.filter_reject_func(parser->callb.priv);
947
200
        break;
948
2.43k
    case usb_redir_filter_filter: {
949
2.43k
        struct usbredirfilter_rule *rules;
950
2.43k
        int r, count;
951
952
2.43k
        r = usbredirfilter_string_to_rules((char *)parser->data, ",", "|",
953
2.43k
                                           &rules, &count);
954
2.43k
        if (r) {
955
1.95k
            ERROR("error parsing filter (%d), ignoring filter message", r);
956
1.95k
            break;
957
1.95k
        }
958
482
        parser->callb.filter_filter_func(parser->callb.priv, rules, count);
959
482
        break;
960
2.43k
    }
961
477
    case usb_redir_device_disconnect_ack:
962
477
        parser->callb.device_disconnect_ack_func(parser->callb.priv);
963
477
        break;
964
214
    case usb_redir_start_bulk_receiving:
965
214
        parser->callb.start_bulk_receiving_func(parser->callb.priv, id,
966
214
            (struct usb_redir_start_bulk_receiving_header *)
967
214
            parser->type_header);
968
214
        break;
969
200
    case usb_redir_stop_bulk_receiving:
970
200
        parser->callb.stop_bulk_receiving_func(parser->callb.priv, id,
971
200
            (struct usb_redir_stop_bulk_receiving_header *)
972
200
            parser->type_header);
973
200
        break;
974
214
    case usb_redir_bulk_receiving_status:
975
214
        parser->callb.bulk_receiving_status_func(parser->callb.priv, id,
976
214
            (struct usb_redir_bulk_receiving_status_header *)
977
214
            parser->type_header);
978
214
        break;
979
575
    case usb_redir_control_packet:
980
575
        *data_ownership_transferred = true;
981
575
        parser->callb.control_packet_func(parser->callb.priv, id,
982
575
            (struct usb_redir_control_packet_header *)parser->type_header,
983
575
            parser->data, parser->data_len);
984
575
        break;
985
521
    case usb_redir_bulk_packet:
986
521
        *data_ownership_transferred = true;
987
521
        parser->callb.bulk_packet_func(parser->callb.priv, id,
988
521
            (struct usb_redir_bulk_packet_header *)parser->type_header,
989
521
            parser->data, parser->data_len);
990
521
        break;
991
487
    case usb_redir_iso_packet:
992
487
        *data_ownership_transferred = true;
993
487
        parser->callb.iso_packet_func(parser->callb.priv, id,
994
487
            (struct usb_redir_iso_packet_header *)parser->type_header,
995
487
            parser->data, parser->data_len);
996
487
        break;
997
589
    case usb_redir_interrupt_packet:
998
589
        *data_ownership_transferred = true;
999
589
        parser->callb.interrupt_packet_func(parser->callb.priv, id,
1000
589
            (struct usb_redir_interrupt_packet_header *)parser->type_header,
1001
589
            parser->data, parser->data_len);
1002
589
        break;
1003
379
    case usb_redir_buffered_bulk_packet:
1004
379
        *data_ownership_transferred = true;
1005
379
        parser->callb.buffered_bulk_packet_func(parser->callb.priv, id,
1006
379
          (struct usb_redir_buffered_bulk_packet_header *)parser->type_header,
1007
379
          parser->data, parser->data_len);
1008
379
        break;
1009
101k
    }
1010
101k
}
1011
1012
USBREDIR_VISIBLE
1013
int usbredirparser_do_read(struct usbredirparser *parser_pub)
1014
127k
{
1015
127k
    struct usbredirparser_priv *parser =
1016
127k
        (struct usbredirparser_priv *)parser_pub;
1017
127k
    int r, header_len, type_header_len, data_len;
1018
127k
    bool data_ownership_transferred;
1019
127k
    uint8_t *dest;
1020
1021
127k
    header_len = usbredirparser_get_header_len(parser_pub);
1022
1023
127k
    usbredirparser_assert_invariants(parser);
1024
    /* Skip forward to next packet (only used in error conditions) */
1025
750k
    while (parser->to_skip > 0) {
1026
622k
        uint8_t buf[65536];
1027
622k
        r = (parser->to_skip > sizeof(buf)) ? sizeof(buf) : parser->to_skip;
1028
622k
        r = parser->callb.read_func(parser->callb.priv, buf, r);
1029
622k
        if (r <= 0) {
1030
145
            usbredirparser_assert_invariants(parser);
1031
145
            return r;
1032
145
        }
1033
622k
        parser->to_skip -= r;
1034
622k
    }
1035
1036
    /* Consume data until read would block or returns an error */
1037
1.28M
    while (1) {
1038
1.28M
        if (parser->header_read < header_len) {
1039
1.14M
            r = header_len - parser->header_read;
1040
1.14M
            dest = (uint8_t *)&parser->header + parser->header_read;
1041
1.14M
        } else if (parser->type_header_read < parser->type_header_len) {
1042
19.3k
            r = parser->type_header_len - parser->type_header_read;
1043
19.3k
            dest = parser->type_header + parser->type_header_read;
1044
117k
        } else {
1045
117k
            r = parser->data_len - parser->data_read;
1046
117k
            dest = parser->data + parser->data_read;
1047
117k
        }
1048
1049
1.28M
        if (r > 0) {
1050
1.18M
            r = parser->callb.read_func(parser->callb.priv, dest, r);
1051
1.18M
            if (r <= 0) {
1052
1.20k
                usbredirparser_assert_invariants(parser);
1053
1.20k
                return r;
1054
1.20k
            }
1055
1.18M
        }
1056
1057
1.28M
        if (parser->header_read < header_len) {
1058
1.14M
            parser->header_read += r;
1059
1.14M
            if (parser->header_read == header_len) {
1060
228k
                type_header_len =
1061
228k
                    usbredirparser_get_type_header_len(parser_pub,
1062
228k
                                                       parser->header.type, 0);
1063
228k
                if (type_header_len < 0) {
1064
14.9k
                    ERROR("error invalid usb-redir packet type: %u",
1065
14.9k
                          parser->header.type);
1066
14.9k
                    parser->to_skip = parser->header.length;
1067
14.9k
                    parser->header_read = 0;
1068
14.9k
                    usbredirparser_assert_invariants(parser);
1069
14.9k
                    return usbredirparser_read_parse_error;
1070
14.9k
                }
1071
                /* This should never happen */
1072
213k
                if (type_header_len > sizeof(parser->type_header)) {
1073
0
                    ERROR("error type specific header buffer too small, please report!!");
1074
0
                    parser->to_skip = parser->header.length;
1075
0
                    parser->header_read = 0;
1076
0
                    usbredirparser_assert_invariants(parser);
1077
0
                    return usbredirparser_read_parse_error;
1078
0
                }
1079
213k
                if (parser->header.length > MAX_PACKET_SIZE) {
1080
5.80k
                    ERROR("packet length of %d larger than permitted %d bytes",
1081
5.80k
                          parser->header.length, MAX_PACKET_SIZE);
1082
5.80k
                    parser->to_skip = parser->header.length;
1083
5.80k
                    parser->header_read = 0;
1084
5.80k
                    usbredirparser_assert_invariants(parser);
1085
5.80k
                    return usbredirparser_read_parse_error;
1086
5.80k
                }
1087
207k
                if ((int)parser->header.length < type_header_len ||
1088
207k
                    ((int)parser->header.length > type_header_len &&
1089
109k
                     !usbredirparser_expect_extra_data(parser))) {
1090
98.8k
                    ERROR("error invalid packet type %u length: %u",
1091
98.8k
                          parser->header.type, parser->header.length);
1092
98.8k
                    parser->to_skip = parser->header.length;
1093
98.8k
                    parser->header_read = 0;
1094
98.8k
                    usbredirparser_assert_invariants(parser);
1095
98.8k
                    return usbredirparser_read_parse_error;
1096
98.8k
                }
1097
108k
                data_len = parser->header.length - type_header_len;
1098
108k
                if (data_len) {
1099
6.69k
                    parser->data = malloc(data_len);
1100
6.69k
                    if (!parser->data) {
1101
0
                        ERROR("Out of memory allocating data buffer");
1102
0
                        parser->to_skip = parser->header.length;
1103
0
                        parser->header_read = 0;
1104
0
                        usbredirparser_assert_invariants(parser);
1105
0
                        return usbredirparser_read_parse_error;
1106
0
                    }
1107
6.69k
                }
1108
108k
                parser->type_header_len = type_header_len;
1109
108k
                parser->data_len = data_len;
1110
108k
            }
1111
1.14M
        } else if (parser->type_header_read < parser->type_header_len) {
1112
19.1k
            parser->type_header_read += r;
1113
117k
        } else {
1114
117k
            parser->data_read += r;
1115
117k
            if (parser->data_read == parser->data_len) {
1116
108k
                r = usbredirparser_verify_type_header(parser_pub,
1117
108k
                         parser->header.type, parser->type_header,
1118
108k
                         parser->data, parser->data_len, 0);
1119
108k
                data_ownership_transferred = false;
1120
108k
                if (r) {
1121
101k
                    usbredirparser_call_type_func(parser_pub,
1122
101k
                                                  &data_ownership_transferred);
1123
101k
                }
1124
108k
                if (!data_ownership_transferred) {
1125
105k
                    free(parser->data);
1126
105k
                }
1127
108k
                parser->header_read = 0;
1128
108k
                parser->type_header_len  = 0;
1129
108k
                parser->type_header_read = 0;
1130
108k
                parser->data_len  = 0;
1131
108k
                parser->data_read = 0;
1132
108k
                parser->data = NULL;
1133
108k
                if (!r) {
1134
6.99k
                    usbredirparser_assert_invariants(parser);
1135
6.99k
                    return usbredirparser_read_parse_error;
1136
6.99k
                }
1137
                /* header len may change if this was an hello packet */
1138
101k
                header_len = usbredirparser_get_header_len(parser_pub);
1139
101k
            }
1140
117k
        }
1141
1.28M
    }
1142
127k
}
1143
1144
USBREDIR_VISIBLE
1145
int usbredirparser_has_data_to_write(struct usbredirparser *parser_pub)
1146
128k
{
1147
128k
    struct usbredirparser_priv *parser =
1148
128k
        (struct usbredirparser_priv *)parser_pub;
1149
128k
    return parser->write_buf_count;
1150
128k
}
1151
1152
USBREDIR_VISIBLE
1153
int usbredirparser_do_write(struct usbredirparser *parser_pub)
1154
1.22k
{
1155
1.22k
    struct usbredirparser_priv *parser =
1156
1.22k
        (struct usbredirparser_priv *)parser_pub;
1157
1.22k
    struct usbredirparser_buf* wbuf;
1158
1.22k
    int w, ret = 0;
1159
1160
1.22k
    LOCK(parser);
1161
1.22k
    assert((parser->write_buf_count != 0) ^ (parser->write_buf == NULL));
1162
1163
1.67M
    for (;;) {
1164
1.67M
        wbuf = parser->write_buf;
1165
1.67M
        if (!wbuf)
1166
1.22k
            break;
1167
1168
1.67M
        w = wbuf->len - wbuf->pos;
1169
1.67M
        w = parser->callb.write_func(parser->callb.priv,
1170
1.67M
                                     wbuf->buf + wbuf->pos, w);
1171
1.67M
        if (w <= 0) {
1172
0
            ret = w;
1173
0
            break;
1174
0
        }
1175
1176
        /* See usbredirparser_write documentation */
1177
1.67M
        if ((parser->flags & usbredirparser_fl_write_cb_owns_buffer) &&
1178
1.67M
                w != wbuf->len)
1179
0
            abort();
1180
1181
1.67M
        wbuf->pos += w;
1182
1.67M
        if (wbuf->pos == wbuf->len) {
1183
84.3k
            parser->write_buf = wbuf->next;
1184
84.3k
            if (!(parser->flags & usbredirparser_fl_write_cb_owns_buffer))
1185
84.3k
                free(wbuf->buf);
1186
1187
84.3k
            parser->write_buf_total_size -= wbuf->len;
1188
84.3k
            parser->write_buf_count--;
1189
84.3k
            free(wbuf);
1190
84.3k
        }
1191
1.67M
    }
1192
1.22k
    UNLOCK(parser);
1193
1.22k
    return ret;
1194
1.22k
}
1195
1196
USBREDIR_VISIBLE
1197
void usbredirparser_free_write_buffer(struct usbredirparser *parser,
1198
    uint8_t *data)
1199
0
{
1200
0
    free(data);
1201
0
}
1202
1203
USBREDIR_VISIBLE
1204
void usbredirparser_free_packet_data(struct usbredirparser *parser,
1205
    uint8_t *data)
1206
2.55k
{
1207
2.55k
    free(data);
1208
2.55k
}
1209
1210
static void usbredirparser_queue(struct usbredirparser *parser_pub,
1211
    uint32_t type, uint64_t id, void *type_header_in,
1212
    uint8_t *data_in, int data_len)
1213
89.3k
{
1214
89.3k
    struct usbredirparser_priv *parser =
1215
89.3k
        (struct usbredirparser_priv *)parser_pub;
1216
89.3k
    uint8_t *buf, *type_header_out, *data_out;
1217
89.3k
    struct usb_redir_header *header;
1218
89.3k
    struct usbredirparser_buf *wbuf, *new_wbuf;
1219
89.3k
    int header_len, type_header_len, total_size;
1220
1221
89.3k
    header_len = usbredirparser_get_header_len(parser_pub);
1222
89.3k
    type_header_len = usbredirparser_get_type_header_len(parser_pub, type, 1);
1223
89.3k
    if (type_header_len < 0) { /* This should never happen */
1224
0
        ERROR("error packet type unknown with internal call, please report!!");
1225
0
        return;
1226
0
    }
1227
1228
89.3k
    if (!usbredirparser_verify_type_header(parser_pub, type, type_header_in,
1229
89.3k
                                           data_in, data_len, 1)) {
1230
0
        ERROR("error usbredirparser_send_* call invalid params, please report!!");
1231
0
        return;
1232
0
    }
1233
1234
89.3k
    total_size = header_len + type_header_len + data_len;
1235
89.3k
    new_wbuf = calloc(1, sizeof(*new_wbuf));
1236
89.3k
    buf = malloc(total_size);
1237
89.3k
    if (!new_wbuf || !buf) {
1238
0
        ERROR("Out of memory allocating buffer to send packet, dropping!");
1239
0
        free(new_wbuf); free(buf);
1240
0
        return;
1241
0
    }
1242
1243
89.3k
    new_wbuf->buf = buf;
1244
89.3k
    new_wbuf->len = total_size;
1245
1246
89.3k
    header = (struct usb_redir_header *)buf;
1247
89.3k
    type_header_out = buf + header_len;
1248
89.3k
    data_out = type_header_out + type_header_len;
1249
1250
89.3k
    header->type   = type;
1251
89.3k
    header->length = type_header_len + data_len;
1252
89.3k
    if (usbredirparser_using_32bits_ids(parser_pub))
1253
16.1k
        ((struct usb_redir_header_32bit_id *)header)->id = id;
1254
73.1k
    else
1255
73.1k
        header->id = id;
1256
89.3k
    memcpy(type_header_out, type_header_in, type_header_len);
1257
89.3k
    memcpy(data_out, data_in, data_len);
1258
1259
89.3k
    LOCK(parser);
1260
89.3k
    if (!parser->write_buf) {
1261
2.20k
        parser->write_buf = new_wbuf;
1262
87.1k
    } else {
1263
        /* limiting the write_buf's stack depth is our users responsibility */
1264
87.1k
        wbuf = parser->write_buf;
1265
586M
        while (wbuf->next)
1266
586M
            wbuf = wbuf->next;
1267
1268
87.1k
        wbuf->next = new_wbuf;
1269
87.1k
    }
1270
89.3k
    parser->write_buf_total_size += total_size;
1271
89.3k
    parser->write_buf_count++;
1272
89.3k
    UNLOCK(parser);
1273
89.3k
}
1274
1275
USBREDIR_VISIBLE
1276
void usbredirparser_send_device_connect(struct usbredirparser *parser,
1277
    struct usb_redir_device_connect_header *device_connect)
1278
0
{
1279
0
    usbredirparser_queue(parser, usb_redir_device_connect, 0, device_connect,
1280
0
                         NULL, 0);
1281
0
}
1282
1283
USBREDIR_VISIBLE
1284
void usbredirparser_send_device_disconnect(struct usbredirparser *parser)
1285
0
{
1286
0
    usbredirparser_queue(parser, usb_redir_device_disconnect, 0, NULL,
1287
0
                         NULL, 0);
1288
0
}
1289
1290
USBREDIR_VISIBLE
1291
void usbredirparser_send_reset(struct usbredirparser *parser)
1292
0
{
1293
0
    usbredirparser_queue(parser, usb_redir_reset, 0, NULL, NULL, 0);
1294
0
}
1295
1296
USBREDIR_VISIBLE
1297
void usbredirparser_send_interface_info(struct usbredirparser *parser,
1298
    struct usb_redir_interface_info_header *interface_info)
1299
0
{
1300
0
    usbredirparser_queue(parser, usb_redir_interface_info, 0, interface_info,
1301
0
                         NULL, 0);
1302
0
}
1303
1304
USBREDIR_VISIBLE
1305
void usbredirparser_send_ep_info(struct usbredirparser *parser,
1306
    struct usb_redir_ep_info_header *ep_info)
1307
0
{
1308
0
    usbredirparser_queue(parser, usb_redir_ep_info, 0, ep_info, NULL, 0);
1309
0
}
1310
1311
USBREDIR_VISIBLE
1312
void usbredirparser_send_set_configuration(struct usbredirparser *parser,
1313
    uint64_t id,
1314
    struct usb_redir_set_configuration_header *set_configuration)
1315
0
{
1316
0
    usbredirparser_queue(parser, usb_redir_set_configuration, id,
1317
0
                         set_configuration, NULL, 0);
1318
0
}
1319
1320
USBREDIR_VISIBLE
1321
void usbredirparser_send_get_configuration(struct usbredirparser *parser,
1322
    uint64_t id)
1323
0
{
1324
0
    usbredirparser_queue(parser, usb_redir_get_configuration, id,
1325
0
                         NULL, NULL, 0);
1326
0
}
1327
1328
USBREDIR_VISIBLE
1329
void usbredirparser_send_configuration_status(struct usbredirparser *parser,
1330
    uint64_t id,
1331
    struct usb_redir_configuration_status_header *configuration_status)
1332
0
{
1333
0
    usbredirparser_queue(parser, usb_redir_configuration_status, id,
1334
0
                         configuration_status, NULL, 0);
1335
0
}
1336
1337
USBREDIR_VISIBLE
1338
void usbredirparser_send_set_alt_setting(struct usbredirparser *parser,
1339
    uint64_t id,
1340
    struct usb_redir_set_alt_setting_header *set_alt_setting)
1341
0
{
1342
0
    usbredirparser_queue(parser, usb_redir_set_alt_setting, id,
1343
0
                         set_alt_setting, NULL, 0);
1344
0
}
1345
1346
USBREDIR_VISIBLE
1347
void usbredirparser_send_get_alt_setting(struct usbredirparser *parser,
1348
    uint64_t id,
1349
    struct usb_redir_get_alt_setting_header *get_alt_setting)
1350
0
{
1351
0
    usbredirparser_queue(parser, usb_redir_get_alt_setting, id,
1352
0
                         get_alt_setting, NULL, 0);
1353
0
}
1354
1355
USBREDIR_VISIBLE
1356
void usbredirparser_send_alt_setting_status(struct usbredirparser *parser,
1357
    uint64_t id,
1358
    struct usb_redir_alt_setting_status_header *alt_setting_status)
1359
0
{
1360
0
    usbredirparser_queue(parser, usb_redir_alt_setting_status, id,
1361
0
                         alt_setting_status, NULL, 0);
1362
0
}
1363
1364
USBREDIR_VISIBLE
1365
void usbredirparser_send_start_iso_stream(struct usbredirparser *parser,
1366
    uint64_t id,
1367
    struct usb_redir_start_iso_stream_header *start_iso_stream)
1368
0
{
1369
0
    usbredirparser_queue(parser, usb_redir_start_iso_stream, id,
1370
0
                         start_iso_stream, NULL, 0);
1371
0
}
1372
1373
USBREDIR_VISIBLE
1374
void usbredirparser_send_stop_iso_stream(struct usbredirparser *parser,
1375
    uint64_t id,
1376
    struct usb_redir_stop_iso_stream_header *stop_iso_stream)
1377
0
{
1378
0
    usbredirparser_queue(parser, usb_redir_stop_iso_stream, id,
1379
0
                         stop_iso_stream, NULL, 0);
1380
0
}
1381
1382
USBREDIR_VISIBLE
1383
void usbredirparser_send_iso_stream_status(struct usbredirparser *parser,
1384
    uint64_t id,
1385
    struct usb_redir_iso_stream_status_header *iso_stream_status)
1386
0
{
1387
0
    usbredirparser_queue(parser, usb_redir_iso_stream_status, id,
1388
0
                         iso_stream_status, NULL, 0);
1389
0
}
1390
1391
USBREDIR_VISIBLE
1392
void usbredirparser_send_start_interrupt_receiving(struct usbredirparser *parser,
1393
    uint64_t id,
1394
    struct usb_redir_start_interrupt_receiving_header *start_interrupt_receiving)
1395
0
{
1396
0
    usbredirparser_queue(parser, usb_redir_start_interrupt_receiving, id,
1397
0
                         start_interrupt_receiving, NULL, 0);
1398
0
}
1399
1400
USBREDIR_VISIBLE
1401
void usbredirparser_send_stop_interrupt_receiving(struct usbredirparser *parser,
1402
    uint64_t id,
1403
    struct usb_redir_stop_interrupt_receiving_header *stop_interrupt_receiving)
1404
0
{
1405
0
    usbredirparser_queue(parser, usb_redir_stop_interrupt_receiving, id,
1406
0
                         stop_interrupt_receiving, NULL, 0);
1407
0
}
1408
1409
USBREDIR_VISIBLE
1410
void usbredirparser_send_interrupt_receiving_status(struct usbredirparser *parser,
1411
    uint64_t id,
1412
    struct usb_redir_interrupt_receiving_status_header *interrupt_receiving_status)
1413
0
{
1414
0
    usbredirparser_queue(parser, usb_redir_interrupt_receiving_status, id,
1415
0
                         interrupt_receiving_status, NULL, 0);
1416
0
}
1417
1418
USBREDIR_VISIBLE
1419
void usbredirparser_send_alloc_bulk_streams(struct usbredirparser *parser,
1420
    uint64_t id,
1421
    struct usb_redir_alloc_bulk_streams_header *alloc_bulk_streams)
1422
0
{
1423
0
    usbredirparser_queue(parser, usb_redir_alloc_bulk_streams, id,
1424
0
                         alloc_bulk_streams, NULL, 0);
1425
0
}
1426
1427
USBREDIR_VISIBLE
1428
void usbredirparser_send_free_bulk_streams(struct usbredirparser *parser,
1429
    uint64_t id,
1430
    struct usb_redir_free_bulk_streams_header *free_bulk_streams)
1431
0
{
1432
0
    usbredirparser_queue(parser, usb_redir_free_bulk_streams, id,
1433
0
                         free_bulk_streams, NULL, 0);
1434
0
}
1435
1436
USBREDIR_VISIBLE
1437
void usbredirparser_send_bulk_streams_status(struct usbredirparser *parser,
1438
    uint64_t id,
1439
    struct usb_redir_bulk_streams_status_header *bulk_streams_status)
1440
0
{
1441
0
    usbredirparser_queue(parser, usb_redir_bulk_streams_status, id,
1442
0
                         bulk_streams_status, NULL, 0);
1443
0
}
1444
1445
USBREDIR_VISIBLE
1446
void usbredirparser_send_cancel_data_packet(struct usbredirparser *parser,
1447
    uint64_t id)
1448
0
{
1449
0
    usbredirparser_queue(parser, usb_redir_cancel_data_packet, id,
1450
0
                         NULL, NULL, 0);
1451
0
}
1452
1453
USBREDIR_VISIBLE
1454
void usbredirparser_send_filter_reject(struct usbredirparser *parser)
1455
0
{
1456
0
    if (!usbredirparser_peer_has_cap(parser, usb_redir_cap_filter))
1457
0
        return;
1458
1459
0
    usbredirparser_queue(parser, usb_redir_filter_reject, 0, NULL, NULL, 0);
1460
0
}
1461
1462
USBREDIR_VISIBLE
1463
void usbredirparser_send_filter_filter(struct usbredirparser *parser_pub,
1464
    const struct usbredirfilter_rule *rules, int rules_count)
1465
0
{
1466
0
    struct usbredirparser_priv *parser =
1467
0
        (struct usbredirparser_priv *)parser_pub;
1468
0
    char *str;
1469
1470
0
    if (!usbredirparser_peer_has_cap(parser_pub, usb_redir_cap_filter))
1471
0
        return;
1472
1473
0
    str = usbredirfilter_rules_to_string(rules, rules_count, ",", "|");
1474
0
    if (!str) {
1475
0
        ERROR("error creating filter string, not sending filter");
1476
0
        return;
1477
0
    }
1478
0
    usbredirparser_queue(parser_pub, usb_redir_filter_filter, 0, NULL,
1479
0
                         (uint8_t *)str, strlen(str) + 1);
1480
0
    free(str);
1481
0
}
1482
1483
USBREDIR_VISIBLE
1484
void usbredirparser_send_start_bulk_receiving(struct usbredirparser *parser,
1485
    uint64_t id,
1486
    struct usb_redir_start_bulk_receiving_header *start_bulk_receiving)
1487
0
{
1488
0
    usbredirparser_queue(parser, usb_redir_start_bulk_receiving, id,
1489
0
                         start_bulk_receiving, NULL, 0);
1490
0
}
1491
1492
USBREDIR_VISIBLE
1493
void usbredirparser_send_stop_bulk_receiving(struct usbredirparser *parser,
1494
    uint64_t id,
1495
    struct usb_redir_stop_bulk_receiving_header *stop_bulk_receiving)
1496
0
{
1497
0
    usbredirparser_queue(parser, usb_redir_stop_bulk_receiving, id,
1498
0
                         stop_bulk_receiving, NULL, 0);
1499
0
}
1500
1501
USBREDIR_VISIBLE
1502
void usbredirparser_send_bulk_receiving_status(struct usbredirparser *parser,
1503
    uint64_t id,
1504
    struct usb_redir_bulk_receiving_status_header *bulk_receiving_status)
1505
0
{
1506
0
    usbredirparser_queue(parser, usb_redir_bulk_receiving_status, id,
1507
0
                         bulk_receiving_status, NULL, 0);
1508
0
}
1509
1510
/* Data packets: */
1511
USBREDIR_VISIBLE
1512
void usbredirparser_send_control_packet(struct usbredirparser *parser,
1513
    uint64_t id,
1514
    struct usb_redir_control_packet_header *control_header,
1515
    uint8_t *data, int data_len)
1516
0
{
1517
0
    usbredirparser_queue(parser, usb_redir_control_packet, id, control_header,
1518
0
                         data, data_len);
1519
0
}
1520
1521
USBREDIR_VISIBLE
1522
void usbredirparser_send_bulk_packet(struct usbredirparser *parser,
1523
    uint64_t id,
1524
    struct usb_redir_bulk_packet_header *bulk_header,
1525
    uint8_t *data, int data_len)
1526
0
{
1527
0
    usbredirparser_queue(parser, usb_redir_bulk_packet, id, bulk_header,
1528
0
                         data, data_len);
1529
0
}
1530
1531
USBREDIR_VISIBLE
1532
void usbredirparser_send_iso_packet(struct usbredirparser *parser,
1533
    uint64_t id,
1534
    struct usb_redir_iso_packet_header *iso_header,
1535
    uint8_t *data, int data_len)
1536
0
{
1537
0
    usbredirparser_queue(parser, usb_redir_iso_packet, id, iso_header,
1538
0
                         data, data_len);
1539
0
}
1540
1541
USBREDIR_VISIBLE
1542
void usbredirparser_send_interrupt_packet(struct usbredirparser *parser,
1543
    uint64_t id,
1544
    struct usb_redir_interrupt_packet_header *interrupt_header,
1545
    uint8_t *data, int data_len)
1546
0
{
1547
0
    usbredirparser_queue(parser, usb_redir_interrupt_packet, id,
1548
0
                         interrupt_header, data, data_len);
1549
0
}
1550
1551
USBREDIR_VISIBLE
1552
void usbredirparser_send_buffered_bulk_packet(struct usbredirparser *parser,
1553
    uint64_t id,
1554
    struct usb_redir_buffered_bulk_packet_header *buffered_bulk_header,
1555
    uint8_t *data, int data_len)
1556
0
{
1557
0
    usbredirparser_queue(parser, usb_redir_buffered_bulk_packet, id,
1558
0
                         buffered_bulk_header, data, data_len);
1559
0
}
1560
1561
/****** Serialization support ******/
1562
1563
122k
#define USBREDIRPARSER_SERIALIZE_BUF_SIZE     65536
1564
1565
/* Serialization format, send and receiving endian are expected to be the same!
1566
    uint32 MAGIC: 0x55525031 ascii: URP1 (UsbRedirParser version 1)
1567
    uint32 len: length of the entire serialized state, including MAGIC
1568
    uint32 our_caps_len
1569
    uint32 our_caps[our_caps_len]
1570
    uint32 peer_caps_len
1571
    uint32 peer_caps[peer_caps_len]
1572
    uint32 to_skip
1573
    uint32 header_read
1574
    uint8  header[header_read]
1575
    uint32 type_header_read
1576
    uint8  type_header[type_header_read]
1577
    uint32 data_read
1578
    uint8  data[data_read]
1579
    uint32 write_buf_count: followed by write_buf_count times:
1580
        uint32 write_buf_len
1581
        uint8  write_buf_data[write_buf_len]
1582
*/
1583
1584
static int serialize_alloc(struct usbredirparser_priv *parser,
1585
                           uint8_t **state, uint8_t **pos,
1586
                           uint32_t *remain, uint32_t needed)
1587
622k
{
1588
622k
    uint8_t *old_state = *state;
1589
622k
    uint32_t used, size;
1590
1591
622k
    if (*remain >= needed)
1592
560k
        return 0;
1593
1594
61.4k
    used = *pos - *state;
1595
61.4k
    size = (used + needed + USBREDIRPARSER_SERIALIZE_BUF_SIZE - 1) &
1596
61.4k
           ~(USBREDIRPARSER_SERIALIZE_BUF_SIZE - 1);
1597
1598
61.4k
    *state = realloc(*state, size);
1599
61.4k
    if (!*state) {
1600
0
        free(old_state);
1601
0
        ERROR("Out of memory allocating serialization buffer");
1602
0
        return -1;
1603
0
    }
1604
1605
61.4k
    *pos = *state + used;
1606
61.4k
    *remain = size - used;
1607
1608
61.4k
    return 0;
1609
61.4k
}
1610
1611
static int serialize_int(struct usbredirparser_priv *parser,
1612
                         uint8_t **state, uint8_t **pos, uint32_t *remain,
1613
                         uint32_t val, const char *desc)
1614
298k
{
1615
298k
    DEBUG("serializing int %08x : %s", val, desc);
1616
1617
298k
    if (serialize_alloc(parser, state, pos, remain, sizeof(uint32_t)))
1618
0
        return -1;
1619
1620
298k
    memcpy(*pos, &val, sizeof(uint32_t));
1621
298k
    *pos += sizeof(uint32_t);
1622
298k
    *remain -= sizeof(uint32_t);
1623
1624
298k
    return 0;
1625
298k
}
1626
1627
static int unserialize_int(struct usbredirparser_priv *parser,
1628
                           uint8_t **pos, uint32_t *remain, uint32_t *val,
1629
                           const char *desc)
1630
2.79k
{
1631
2.79k
    if (*remain < sizeof(uint32_t)) {
1632
157
        ERROR("error buffer underrun while unserializing state");
1633
157
        return -1;
1634
157
    }
1635
2.63k
    memcpy(val, *pos, sizeof(uint32_t));
1636
2.63k
    *pos += sizeof(uint32_t);
1637
2.63k
    *remain -= sizeof(uint32_t);
1638
1639
2.63k
    DEBUG("unserialized int %08x : %s", *val, desc);
1640
1641
2.63k
    return 0;
1642
2.79k
}
1643
1644
static int serialize_data(struct usbredirparser_priv *parser,
1645
                          uint8_t **state, uint8_t **pos, uint32_t *remain,
1646
                          uint8_t *data, uint32_t len, const char *desc)
1647
324k
{
1648
324k
    DEBUG("serializing %d bytes of %s data", len, desc);
1649
324k
    if (len >= 8)
1650
69.4k
        DEBUG("First 8 bytes of %s: %02x %02x %02x %02x %02x %02x %02x %02x",
1651
324k
              desc, data[0], data[1], data[2], data[3],
1652
324k
                    data[4], data[5], data[6], data[7]);
1653
1654
324k
    if (serialize_alloc(parser, state, pos, remain, sizeof(uint32_t) + len))
1655
0
        return -1;
1656
1657
324k
    memcpy(*pos, &len, sizeof(uint32_t));
1658
324k
    *pos += sizeof(uint32_t);
1659
324k
    *remain -= sizeof(uint32_t);
1660
1661
324k
    memcpy(*pos, data, len);
1662
324k
    *pos += len;
1663
324k
    *remain -= len;
1664
1665
324k
    return 0;
1666
324k
}
1667
1668
/* If *data == NULL, allocs buffer dynamically, else len_in_out must contain
1669
   the length of the passed in buffer. */
1670
static int unserialize_data(struct usbredirparser_priv *parser,
1671
                            uint8_t **pos, uint32_t *remain,
1672
                            uint8_t **data, uint32_t *len_in_out,
1673
                            const char *desc)
1674
3.75k
{
1675
3.75k
    uint32_t len;
1676
1677
3.75k
    if (*remain < sizeof(uint32_t)) {
1678
212
        ERROR("error buffer underrun while unserializing state");
1679
212
        return -1;
1680
212
    }
1681
3.54k
    memcpy(&len, *pos, sizeof(uint32_t));
1682
3.54k
    *pos += sizeof(uint32_t);
1683
3.54k
    *remain -= sizeof(uint32_t);
1684
1685
3.54k
    if (*remain < len) {
1686
69
        ERROR("error buffer underrun while unserializing state");
1687
69
        return -1;
1688
69
    }
1689
3.47k
    if (*data == NULL && len > 0) {
1690
837
        *data = malloc(len);
1691
837
        if (!*data) {
1692
0
            ERROR("Out of memory allocating unserialize buffer");
1693
0
            return -1;
1694
0
        }
1695
2.63k
    } else {
1696
2.63k
        if (*len_in_out < len) {
1697
2
            ERROR("error buffer overrun while unserializing state");
1698
2
            return -1;
1699
2
        }
1700
2.63k
    }
1701
1702
3.46k
    memcpy(*data, *pos, len);
1703
3.46k
    *pos += len;
1704
3.46k
    *remain -= len;
1705
3.46k
    *len_in_out = len;
1706
1707
3.46k
    DEBUG("unserialized %d bytes of %s data", len, desc);
1708
3.46k
    if (len >= 8)
1709
689
        DEBUG("First 8 bytes of %s: %02x %02x %02x %02x %02x %02x %02x %02x",
1710
3.46k
              desc, (*data)[0], (*data)[1], (*data)[2], (*data)[3],
1711
3.46k
              (*data)[4], (*data)[5], (*data)[6], (*data)[7]);
1712
1713
3.46k
    return 0;
1714
3.47k
}
1715
1716
USBREDIR_VISIBLE
1717
int usbredirparser_serialize(struct usbredirparser *parser_pub,
1718
                             uint8_t **state_dest, int *state_len)
1719
61.4k
{
1720
61.4k
    struct usbredirparser_priv *parser =
1721
61.4k
        (struct usbredirparser_priv *)parser_pub;
1722
61.4k
    struct usbredirparser_buf *wbuf;
1723
61.4k
    uint8_t *state = NULL, *pos = NULL;
1724
61.4k
    uint32_t write_buf_count = 0, len, remain = 0;
1725
61.4k
    ptrdiff_t write_buf_count_pos;
1726
1727
61.4k
    *state_dest = NULL;
1728
61.4k
    *state_len = 0;
1729
1730
61.4k
    if (serialize_int(parser, &state, &pos, &remain,
1731
61.4k
                                   USBREDIRPARSER_SERIALIZE_MAGIC, "magic"))
1732
0
        return -1;
1733
1734
    /* To be replaced with length later */
1735
61.4k
    if (serialize_int(parser, &state, &pos, &remain, 0, "length"))
1736
0
        return -1;
1737
1738
61.4k
    if (serialize_data(parser, &state, &pos, &remain,
1739
61.4k
                       (uint8_t *)parser->our_caps,
1740
61.4k
                       USB_REDIR_CAPS_SIZE * sizeof(int32_t), "our_caps"))
1741
0
        return -1;
1742
1743
61.4k
    if (parser->have_peer_caps) {
1744
8.79k
        if (serialize_data(parser, &state, &pos, &remain,
1745
8.79k
                           (uint8_t *)parser->peer_caps,
1746
8.79k
                           USB_REDIR_CAPS_SIZE * sizeof(int32_t), "peer_caps"))
1747
0
            return -1;
1748
52.6k
    } else {
1749
52.6k
        if (serialize_int(parser, &state, &pos, &remain, 0, "peer_caps_len"))
1750
0
            return -1;
1751
52.6k
    }
1752
1753
61.4k
    if (serialize_int(parser, &state, &pos, &remain, parser->to_skip, "skip"))
1754
0
        return -1;
1755
1756
61.4k
    if (serialize_data(parser, &state, &pos, &remain,
1757
61.4k
                       (uint8_t *)&parser->header, parser->header_read,
1758
61.4k
                       "header"))
1759
0
        return -1;
1760
1761
61.4k
    if (serialize_data(parser, &state, &pos, &remain,
1762
61.4k
                       parser->type_header, parser->type_header_read,
1763
61.4k
                       "type_header"))
1764
0
        return -1;
1765
1766
61.4k
    if (serialize_data(parser, &state, &pos, &remain,
1767
61.4k
                       parser->data, parser->data_read, "packet-data"))
1768
0
        return -1;
1769
1770
61.4k
    write_buf_count_pos = pos - state;
1771
    /* To be replaced with write_buf_count later */
1772
61.4k
    if (serialize_int(parser, &state, &pos, &remain, 0, "write_buf_count"))
1773
0
        return -1;
1774
1775
61.4k
    wbuf = parser->write_buf;
1776
131k
    while (wbuf) {
1777
69.6k
        if (serialize_data(parser, &state, &pos, &remain,
1778
69.6k
                           wbuf->buf + wbuf->pos, wbuf->len - wbuf->pos,
1779
69.6k
                           "write-buf"))
1780
0
            return -1;
1781
69.6k
        write_buf_count++;
1782
69.6k
        wbuf = wbuf->next;
1783
69.6k
    }
1784
    /* Patch in write_buf_count */
1785
61.4k
    memcpy(state + write_buf_count_pos, &write_buf_count, sizeof(int32_t));
1786
1787
    /* Patch in length */
1788
61.4k
    len = pos - state;
1789
61.4k
    memcpy(state + sizeof(int32_t), &len, sizeof(int32_t));
1790
1791
61.4k
    *state_dest = state;
1792
61.4k
    *state_len = len;
1793
1794
61.4k
    return 0;
1795
61.4k
}
1796
1797
USBREDIR_VISIBLE
1798
int usbredirparser_unserialize(struct usbredirparser *parser_pub,
1799
                               uint8_t *state, int len)
1800
915
{
1801
915
    struct usbredirparser_priv *parser =
1802
915
        (struct usbredirparser_priv *)parser_pub;
1803
915
    struct usbredirparser_buf *wbuf, **next;
1804
915
    uint32_t orig_caps[USB_REDIR_CAPS_SIZE];
1805
915
    uint8_t *data;
1806
915
    uint32_t i, l, header_len, remain = len;
1807
1808
915
    usbredirparser_assert_invariants(parser);
1809
915
    if (unserialize_int(parser, &state, &remain, &i, "magic")) {
1810
29
        usbredirparser_assert_invariants(parser);
1811
29
        return -1;
1812
29
    }
1813
886
    if (i != USBREDIRPARSER_SERIALIZE_MAGIC) {
1814
0
        ERROR("error unserialize magic mismatch");
1815
0
        usbredirparser_assert_invariants(parser);
1816
0
        return -1;
1817
0
    }
1818
1819
886
    if (!(parser->data == NULL && parser->header_read == 0 &&
1820
886
          parser->type_header_read == 0 && parser->data_read == 0)) {
1821
0
        ERROR("unserialization must use a pristine parser");
1822
0
        usbredirparser_assert_invariants(parser);
1823
0
        return -1;
1824
0
    }
1825
1826
886
    {
1827
        /* We need to reset parser's state to receive unserialized
1828
         * data. */
1829
886
        struct usbredirparser_buf *wbuf = parser->write_buf;
1830
1.34k
        while (wbuf) {
1831
459
            struct usbredirparser_buf *next_wbuf = wbuf->next;
1832
459
            free(wbuf->buf);
1833
459
            free(wbuf);
1834
459
            wbuf = next_wbuf;
1835
459
        }
1836
886
        parser->write_buf = NULL;
1837
886
        parser->write_buf_count = 0;
1838
886
        parser->write_buf_total_size = 0;
1839
886
    }
1840
1841
886
    if (unserialize_int(parser, &state, &remain, &i, "length")) {
1842
65
        usbredirparser_assert_invariants(parser);
1843
65
        return -1;
1844
65
    }
1845
821
    if (i != len) {
1846
97
        ERROR("error unserialize length mismatch");
1847
97
        usbredirparser_assert_invariants(parser);
1848
97
        return -1;
1849
97
    }
1850
1851
724
    data = (uint8_t *)parser->our_caps;
1852
724
    i = USB_REDIR_CAPS_SIZE * sizeof(int32_t);
1853
724
    memcpy(orig_caps, parser->our_caps, i);
1854
724
    if (unserialize_data(parser, &state, &remain, &data, &i, "our_caps")) {
1855
55
        usbredirparser_assert_invariants(parser);
1856
55
        return -1;
1857
55
    }
1858
1.25k
    for (i =0; i < USB_REDIR_CAPS_SIZE; i++) {
1859
669
        if (parser->our_caps[i] != orig_caps[i]) {
1860
            /* orig_caps is our original settings
1861
             * parser->our_caps is off the wire.
1862
             * We want to allow reception from an older
1863
             * usbredir that doesn't have all our features.
1864
             */
1865
90
            if (parser->our_caps[i] & ~orig_caps[i]) {
1866
                /* Source has a cap we don't */
1867
82
                ERROR("error unserialize caps mismatch ours: %x recv: %x",
1868
82
                      orig_caps[i], parser->our_caps[i]);
1869
82
                usbredirparser_assert_invariants(parser);
1870
82
                return -1;
1871
82
            } else {
1872
                /* We've got a cap the source doesn't - that's OK */
1873
8
                WARNING("unserialize missing some caps; ours: %x recv: %x",
1874
8
                      orig_caps[i], parser->our_caps[i]);
1875
8
            }
1876
90
        }
1877
669
    }
1878
1879
587
    data = (uint8_t *)parser->peer_caps;
1880
587
    i = USB_REDIR_CAPS_SIZE * sizeof(int32_t);
1881
587
    if (unserialize_data(parser, &state, &remain, &data, &i, "peer_caps")) {
1882
4
        usbredirparser_assert_invariants(parser);
1883
4
        return -1;
1884
4
    }
1885
583
    if (i)
1886
207
        parser->have_peer_caps = 1;
1887
1888
583
    if (unserialize_int(parser, &state, &remain, &i, "skip")) {
1889
4
        usbredirparser_assert_invariants(parser);
1890
4
        return -1;
1891
4
    }
1892
579
    parser->to_skip = i;
1893
1894
579
    header_len = usbredirparser_get_header_len(parser_pub);
1895
579
    data = (uint8_t *)&parser->header;
1896
579
    i = header_len;
1897
579
    memset(&parser->header, 0, sizeof(parser->header));
1898
579
    if (unserialize_data(parser, &state, &remain, &data, &i, "header")) {
1899
3
        usbredirparser_assert_invariants(parser);
1900
3
        return -1;
1901
3
    }
1902
576
    if (parser->header.length > MAX_PACKET_SIZE) {
1903
15
        ERROR("packet length of %d larger than permitted %d bytes",
1904
15
              parser->header.length, MAX_PACKET_SIZE);
1905
15
        usbredirparser_assert_invariants(parser);
1906
15
        return -1;
1907
15
    }
1908
561
    parser->header_read = i;
1909
561
    parser->type_header_len = 0;
1910
1911
    /* Set various length field from the header (if any) */
1912
561
    if (parser->header_read == header_len) {
1913
106
        int type_header_len =
1914
106
            usbredirparser_get_type_header_len(parser_pub,
1915
106
                                               parser->header.type, 0);
1916
106
        if (type_header_len < 0 ||
1917
106
            type_header_len > sizeof(parser->type_header) ||
1918
106
            parser->header.length < type_header_len ||
1919
106
            (parser->header.length > type_header_len &&
1920
103
             !usbredirparser_expect_extra_data(parser))) {
1921
15
            ERROR("error unserialize packet header invalid");
1922
15
            usbredirparser_assert_invariants(parser);
1923
15
            return -1;
1924
15
        }
1925
91
        parser->type_header_len = type_header_len;
1926
91
    }
1927
1928
546
    data = parser->type_header;
1929
546
    i = parser->type_header_len;
1930
546
    if (unserialize_data(parser, &state, &remain, &data, &i, "type_header")) {
1931
87
        usbredirparser_assert_invariants(parser);
1932
87
        return -1;
1933
87
    }
1934
459
    if (parser->header_read == header_len) {
1935
53
        parser->type_header_read = i;
1936
53
    }
1937
1938
459
    if (parser->type_header_read == parser->type_header_len) {
1939
435
        parser->data_len = parser->header.length - parser->type_header_len;
1940
435
        if (parser->data_len) {
1941
145
            parser->data = malloc(parser->data_len);
1942
145
            if (!parser->data) {
1943
0
                ERROR("Out of memory allocating unserialize buffer");
1944
0
                usbredirparser_assert_invariants(parser);
1945
0
                return -1;
1946
0
            }
1947
145
        }
1948
435
    }
1949
459
    i = parser->data_len;
1950
459
    if (unserialize_data(parser, &state, &remain, &parser->data, &i, "data")) {
1951
53
        free(parser->data);
1952
53
        parser->data = NULL;
1953
53
        parser->data_len = 0;
1954
53
        usbredirparser_assert_invariants(parser);
1955
53
        return -1;
1956
53
    }
1957
406
    if (parser->header_read == header_len &&
1958
406
        parser->type_header_read == parser->type_header_len &&
1959
406
        parser->data_len > 0) {
1960
27
        parser->data_read = i;
1961
379
    } else if (parser->data != NULL) {
1962
149
        free(parser->data);
1963
149
        parser->data = NULL;
1964
149
        parser->data_len = 0;
1965
149
    }
1966
1967
    /* Get the write buffer count and the write buffers */
1968
406
    if (unserialize_int(parser, &state, &remain, &i, "write_buf_count")) {
1969
59
        usbredirparser_assert_invariants(parser);
1970
59
        return -1;
1971
59
    }
1972
347
    next = &parser->write_buf;
1973
347
    usbredirparser_assert_invariants(parser);
1974
1.11k
    while (i) {
1975
857
        uint8_t *buf = NULL;
1976
1977
857
        l = 0;
1978
857
        if (unserialize_data(parser, &state, &remain, &buf, &l, "wbuf")) {
1979
81
            usbredirparser_assert_invariants(parser);
1980
81
            return -1;
1981
81
        }
1982
1983
776
        if (l == 0) {
1984
13
            free(buf);
1985
13
            ERROR("write buffer %d is empty", i);
1986
13
            usbredirparser_assert_invariants(parser);
1987
13
            return -1;
1988
13
        }
1989
1990
763
        wbuf = calloc(1, sizeof(*wbuf));
1991
763
        if (!wbuf) {
1992
0
            free(buf);
1993
0
            ERROR("Out of memory allocating unserialize buffer");
1994
0
            usbredirparser_assert_invariants(parser);
1995
0
            return -1;
1996
0
        }
1997
763
        wbuf->buf = buf;
1998
763
        wbuf->len = l;
1999
763
        *next = wbuf;
2000
763
        next = &wbuf->next;
2001
763
        parser->write_buf_total_size += wbuf->len;
2002
763
        parser->write_buf_count++;
2003
763
        i--;
2004
763
    }
2005
2006
253
    if (remain) {
2007
22
        ERROR("error unserialize %d bytes of extraneous state data", remain);
2008
22
        usbredirparser_assert_invariants(parser);
2009
22
        return -1;
2010
22
    }
2011
2012
231
    usbredirparser_assert_invariants(parser);
2013
231
    return 0;
2014
253
}