FreeRDP
Loading...
Searching...
No Matches
data_transfer.c
1
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25#include <winpr/sysinfo.h>
26#include <winpr/cast.h>
27
28#include <urbdrc_helpers.h>
29
30#include "urbdrc_types.h"
31#include "data_transfer.h"
32#include "msusb.h"
33
34static void usb_process_get_port_status(IUDEVICE* pdev, wStream* out)
35{
36 int bcdUSB = pdev->query_device_descriptor(pdev, BCD_USB);
37
38 switch (bcdUSB)
39 {
40 case USB_v1_0:
41 Stream_Write_UINT32(out, 0x303);
42 break;
43
44 case USB_v1_1:
45 Stream_Write_UINT32(out, 0x103);
46 break;
47
48 case USB_v2_0:
49 default:
50 Stream_Write_UINT32(out, 0x503);
51 break;
52 }
53}
54
55/* [MS-RDPEUSB] 2.2.10.1.1TS_URB_RESULT_HEADER */
56static BOOL write_urb_result_header(wStream* s, UINT16 Size, UINT32 status)
57{
58 if (!Stream_EnsureRemainingCapacity(s, 8ULL + Size))
59 return FALSE;
60 Stream_Write_UINT16(s, Size);
61 Stream_Seek_UINT16(s);
62 Stream_Write_UINT32(s, status);
63 return TRUE;
64}
65
66/* [MS-RDPEUSB] 2.2.7.2 URB Completion (URB_COMPLETION)
67 * 2.2.7.3 URB Completion No Data (URB_COMPLETION_NO_DATA)
68 */
69static wStream* create_urb_completion_message(UINT32 InterfaceId, UINT32 MessageId,
70 UINT32 RequestId, UINT32 FunctionId)
71{
72 wStream* out =
73 create_shared_message_header_with_functionid(InterfaceId, MessageId, FunctionId, 4);
74 if (!out)
75 return nullptr;
76
77 Stream_Write_UINT32(out, RequestId);
78 return out;
79}
80
81static UINT send_urb_completion_message(GENERIC_CHANNEL_CALLBACK* callback, wStream* out,
82 HRESULT hResult, UINT32 OutputSize, const void* data)
83{
84 WINPR_ASSERT(callback);
85 UINT status = ERROR_OUTOFMEMORY;
86
87 if (!Stream_EnsureRemainingCapacity(out, 8ULL + OutputSize))
88 goto fail;
89
90 Stream_Write_INT32(out, hResult);
91 Stream_Write_UINT32(out, OutputSize);
92 Stream_Write(out, data, OutputSize);
93 return stream_write_and_free(callback->plugin, callback->channel, out);
94
95fail:
96 Stream_Free(out, TRUE);
97 return status;
98}
99
100/* [MS-RDPEUSB] 2.2.7.2 and 2.2.7.3:
101 * Only a TRANSFER_IN_REQUEST that returns data carries an OutputBuffer.
102 * TRANSFER_OUT_REQUEST reports the transferred byte count in OutputBufferSize,
103 * but always uses URB_COMPLETION_NO_DATA. */
104static UINT32 urb_completion_payload_size(int transferDir, UINT32 outputBufferSize)
105{
106 return (transferDir == USBD_TRANSFER_DIRECTION_IN) ? outputBufferSize : 0;
107}
108
109static UINT urb_write_completion(WINPR_ATTR_UNUSED IUDEVICE* pdev,
110 GENERIC_CHANNEL_CALLBACK* callback, BOOL noAck, wStream* out,
111 UINT32 InterfaceId, UINT32 MessageId, UINT32 RequestId,
112 UINT32 usbd_status, UINT32 OutputBufferSize, int transferDir)
113{
114 if (!out)
115 return ERROR_INVALID_PARAMETER;
116
117 const UINT32 payloadSize = urb_completion_payload_size(transferDir, OutputBufferSize);
118 if (Stream_Capacity(out) < payloadSize + 36ULL)
119 {
120 Stream_Free(out, TRUE);
121 return ERROR_INVALID_PARAMETER;
122 }
123
124 Stream_ResetPosition(out);
125
126 const UINT32 FunctionId = (payloadSize != 0) ? URB_COMPLETION : URB_COMPLETION_NO_DATA;
127 if (!write_shared_message_header_with_functionid(out, InterfaceId, MessageId, FunctionId))
128 {
129 Stream_Free(out, TRUE);
130 return ERROR_OUTOFMEMORY;
131 }
132
133 Stream_Write_UINT32(out, RequestId);
134 Stream_Write_UINT32(out, 8);
135
136 if (!write_urb_result_header(out, 8, usbd_status))
137 {
138 Stream_Free(out, TRUE);
139 return ERROR_OUTOFMEMORY;
140 }
141
142 /* [MS-RDPEUSB] URB_COMPLETION: a failed URB must also fail the IRP, otherwise the server side
143 * (e.g. usbstor) never runs its stall recovery (SYNC_RESET_PIPE_AND_CLEAR_STALL) */
144 HRESULT hr = S_OK;
145 const UINT32 mask = 0x80000000ul;
146 const UINT32 masked = usbd_status & mask;
147 if (masked == mask)
148 hr = HRESULT_FROM_WIN32(ERROR_GEN_FAILURE);
149 Stream_Write_INT32(out, hr);
150 Stream_Write_UINT32(out, OutputBufferSize);
151 Stream_Seek(out, payloadSize);
152
153 if (!noAck)
154 return stream_write_and_free(callback->plugin, callback->channel, out);
155 else
156 Stream_Free(out, TRUE);
157
158 return ERROR_SUCCESS;
159}
160
161static wStream* urb_create_iocompletion(UINT32 InterfaceField, UINT32 MessageId, UINT32 RequestId,
162 UINT32 OutputBufferSize)
163{
164 const UINT32 InterfaceId = (STREAM_ID_PROXY << 30) | (InterfaceField & 0x3FFFFFFF);
165
166#if UINT32_MAX >= SIZE_MAX
167 if (OutputBufferSize > UINT32_MAX - 28ull)
168 return nullptr;
169#endif
170
171 wStream* out = create_shared_message_header_with_functionid(
172 InterfaceId, MessageId, IOCONTROL_COMPLETION, OutputBufferSize + 16ull);
173 if (!out)
174 return nullptr;
175
176 Stream_Write_UINT32(out, RequestId);
177 Stream_Write_UINT32(out, USBD_STATUS_SUCCESS);
178 Stream_Write_UINT32(out, OutputBufferSize);
179 Stream_Write_UINT32(out, OutputBufferSize);
180 return out;
181}
182
183/* [MS-RDPEUSB] 2.2.7.1 IO Control Completion (IOCONTROL_COMPLETION)
184 *
185 * The Information and OutputBufferSize fields describe the OutputBuffer that
186 * follows them, but urb_create_iocompletion() has to write both before the IO
187 * control handler has produced any output. Rewrite them once the payload is
188 * complete so that a handler which returns nothing does not announce a buffer
189 * it never sends.
190 */
191static BOOL urb_finalize_iocompletion(wStream* out)
192{
193 WINPR_ASSERT(out);
194
195 const size_t header = 12ULL /* SHARED_MSG_HEADER */ + 4ULL /* RequestId */;
196 const size_t offset = header + 4ULL /* HResult */;
197 const size_t fixed = offset + 4ULL /* Information */ + 4ULL /* OutputBufferSize */;
198 const size_t end = Stream_GetPosition(out);
199
200 if (end < fixed)
201 return FALSE;
202
203 const size_t OutputBufferSize = end - fixed;
204
205 if (OutputBufferSize > UINT32_MAX)
206 return FALSE;
207
208 const UINT32 size = WINPR_ASSERTING_INT_CAST(UINT32, OutputBufferSize);
209
210 if (!Stream_SetPosition(out, offset))
211 return FALSE;
212
213 Stream_Write_UINT32(out, size);
214 Stream_Write_UINT32(out, size);
215 return Stream_SetPosition(out, end);
216}
217
218static UINT urbdrc_process_register_request_callback(IUDEVICE* pdev,
219 GENERIC_CHANNEL_CALLBACK* callback, wStream* s,
220 IUDEVMAN* udevman)
221{
222 UINT32 NumRequestCompletion = 0;
223 UINT32 RequestCompletion = 0;
224
225 if (!callback || !s || !udevman || !pdev)
226 return ERROR_INVALID_PARAMETER;
227
228 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)callback->plugin;
229
230 if (!urbdrc)
231 return ERROR_INVALID_PARAMETER;
232
233 WLog_Print(urbdrc->log, WLOG_DEBUG, "urbdrc_process_register_request_callback");
234
235 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4ULL))
236 return ERROR_INVALID_DATA;
237
238 Stream_Read_UINT32(s, NumRequestCompletion);
239
240 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4ULL * NumRequestCompletion))
241 return ERROR_INVALID_DATA;
242 for (uint32_t x = 0; x < NumRequestCompletion; x++)
243 {
246 Stream_Read_UINT32(s, RequestCompletion);
247 pdev->set_ReqCompletion(pdev, RequestCompletion);
248 }
249
250 return ERROR_SUCCESS;
251}
252
253static UINT urbdrc_process_cancel_request(IUDEVICE* pdev, wStream* s, IUDEVMAN* udevman)
254{
255 UINT32 CancelId = 0;
256 URBDRC_PLUGIN* urbdrc = nullptr;
257
258 if (!s || !udevman || !pdev)
259 return ERROR_INVALID_PARAMETER;
260
261 urbdrc = (URBDRC_PLUGIN*)udevman->plugin;
262
263 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
264 return ERROR_INVALID_DATA;
265
266 Stream_Read_UINT32(s, CancelId);
267 WLog_Print(urbdrc->log, WLOG_DEBUG, "CANCEL_REQUEST: CancelId=%08" PRIx32 "", CancelId);
268
269 if (pdev->cancel_transfer_request(pdev, CancelId) < 0)
270 return ERROR_INTERNAL_ERROR;
271
272 return ERROR_SUCCESS;
273}
274
275static UINT urbdrc_process_retract_device_request(WINPR_ATTR_UNUSED IUDEVICE* pdev, wStream* s,
276 IUDEVMAN* udevman)
277{
278 UINT32 Reason = 0;
279 URBDRC_PLUGIN* urbdrc = nullptr;
280
281 if (!s || !udevman)
282 return ERROR_INVALID_PARAMETER;
283
284 urbdrc = (URBDRC_PLUGIN*)udevman->plugin;
285
286 if (!urbdrc)
287 return ERROR_INVALID_PARAMETER;
288
289 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
290 return ERROR_INVALID_DATA;
291
292 Stream_Read_UINT32(s, Reason);
293
294 switch (Reason)
295 {
296 case UsbRetractReason_BlockedByPolicy:
297 WLog_Print(urbdrc->log, WLOG_DEBUG,
298 "UsbRetractReason_BlockedByPolicy: now it is not support");
299 return ERROR_ACCESS_DENIED;
300
301 default:
302 WLog_Print(urbdrc->log, WLOG_DEBUG,
303 "urbdrc_process_retract_device_request: Unknown Reason %" PRIu32 "", Reason);
304 return ERROR_ACCESS_DENIED;
305 }
306
307 return ERROR_SUCCESS;
308}
309
310static UINT urbdrc_process_io_control(IUDEVICE* pdev, GENERIC_CHANNEL_CALLBACK* callback,
311 wStream* s, UINT32 MessageId, IUDEVMAN* udevman)
312{
313 UINT32 InterfaceId = 0;
314 UINT32 IoControlCode = 0;
315 UINT32 InputBufferSize = 0;
316 UINT32 OutputBufferSize = 0;
317 UINT32 RequestId = 0;
318 UINT32 usbd_status = USBD_STATUS_SUCCESS;
319 wStream* out = nullptr;
320 int success = 0;
321 URBDRC_PLUGIN* urbdrc = nullptr;
322
323 if (!callback || !s || !udevman || !pdev)
324 return ERROR_INVALID_PARAMETER;
325
326 urbdrc = (URBDRC_PLUGIN*)callback->plugin;
327
328 if (!urbdrc)
329 return ERROR_INVALID_PARAMETER;
330
331 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
332 return ERROR_INVALID_DATA;
333
334 Stream_Read_UINT32(s, IoControlCode);
335 Stream_Read_UINT32(s, InputBufferSize);
336
337 if (!Stream_SafeSeek(s, InputBufferSize))
338 return ERROR_INVALID_DATA;
339 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8ULL))
340 return ERROR_INVALID_DATA;
341
342 Stream_Read_UINT32(s, OutputBufferSize);
343 Stream_Read_UINT32(s, RequestId);
344
345 if (OutputBufferSize > UINT32_MAX - 4)
346 return ERROR_INVALID_DATA;
347
348 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
349 out = urb_create_iocompletion(InterfaceId, MessageId, RequestId, OutputBufferSize + 4);
350
351 if (!out)
352 return ERROR_OUTOFMEMORY;
353
354 switch (IoControlCode)
355 {
356 case IOCTL_INTERNAL_USB_SUBMIT_URB:
357 WLog_Print(urbdrc->log, WLOG_DEBUG, "ioctl: IOCTL_INTERNAL_USB_SUBMIT_URB");
358 WLog_Print(urbdrc->log, WLOG_ERROR,
359 " Function IOCTL_INTERNAL_USB_SUBMIT_URB: Unchecked");
360 break;
361
362 case IOCTL_INTERNAL_USB_RESET_PORT:
363 WLog_Print(urbdrc->log, WLOG_DEBUG, "ioctl: IOCTL_INTERNAL_USB_RESET_PORT");
364 break;
365
366 case IOCTL_INTERNAL_USB_GET_PORT_STATUS:
367 WLog_Print(urbdrc->log, WLOG_DEBUG, "ioctl: IOCTL_INTERNAL_USB_GET_PORT_STATUS");
368 success = pdev->query_device_port_status(pdev, &usbd_status, &OutputBufferSize,
369 Stream_Pointer(out));
370
371 if (success)
372 {
373 if (!Stream_SafeSeek(out, OutputBufferSize))
374 {
375 Stream_Free(out, TRUE);
376 return ERROR_INVALID_DATA;
377 }
378
379 if (pdev->isExist(pdev) == 0)
380 Stream_Write_UINT32(out, 0);
381 else
382 usb_process_get_port_status(pdev, out);
383 }
384
385 break;
386
387 case IOCTL_INTERNAL_USB_CYCLE_PORT:
388 WLog_Print(urbdrc->log, WLOG_DEBUG, "ioctl: IOCTL_INTERNAL_USB_CYCLE_PORT");
389 WLog_Print(urbdrc->log, WLOG_ERROR,
390 " Function IOCTL_INTERNAL_USB_CYCLE_PORT: Unchecked");
391 break;
392
393 case IOCTL_INTERNAL_USB_SUBMIT_IDLE_NOTIFICATION:
394 WLog_Print(urbdrc->log, WLOG_DEBUG,
395 "ioctl: IOCTL_INTERNAL_USB_SUBMIT_IDLE_NOTIFICATION");
396 WLog_Print(urbdrc->log, WLOG_ERROR,
397 " Function IOCTL_INTERNAL_USB_SUBMIT_IDLE_NOTIFICATION: Unchecked");
398 break;
399
400 default:
401 WLog_Print(urbdrc->log, WLOG_DEBUG,
402 "urbdrc_process_io_control: unknown IoControlCode 0x%" PRIX32 "",
403 IoControlCode);
404 Stream_Free(out, TRUE);
405 return ERROR_INVALID_OPERATION;
406 }
407
408 if (!urb_finalize_iocompletion(out))
409 {
410 Stream_Free(out, TRUE);
411 return ERROR_INTERNAL_ERROR;
412 }
413
414 return stream_write_and_free(callback->plugin, callback->channel, out);
415}
416
417static UINT urbdrc_process_internal_io_control(IUDEVICE* pdev, GENERIC_CHANNEL_CALLBACK* callback,
418 wStream* s, UINT32 MessageId, IUDEVMAN* udevman)
419{
420 if (!pdev || !callback || !s || !udevman)
421 return ERROR_INVALID_PARAMETER;
422
423 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)callback->plugin;
424 WINPR_ASSERT(urbdrc);
425
426 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
427 return ERROR_INVALID_DATA;
428
429 const UINT32 IoControlCode = Stream_Get_UINT32(s);
430 if (IoControlCode != IOCTL_TSUSBGD_IOCTL_USBDI_QUERY_BUS_TIME)
431 {
432 WLog_ERR(
433 TAG,
434 "Invalid [MS-RDPEUSB] 2.2.13 USB Internal IO Control Code::IoControlCode0x%08" PRIx32
435 ", must be IOCTL_TSUSBGD_IOCTL_USBDI_QUERY_BUS_TIME [0x00224000]",
436 IoControlCode);
437 return ERROR_INVALID_DATA;
438 }
439 const UINT32 InputBufferSize = Stream_Get_UINT32(s);
440
441 if (!Stream_SafeSeek(s, InputBufferSize))
442 return ERROR_INVALID_DATA;
443 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8ULL))
444 return ERROR_INVALID_DATA;
445 const UINT32 OutputBufferSize = Stream_Get_UINT32(s);
446 const UINT32 RequestId = Stream_Get_UINT32(s);
447 const UINT32 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
448 // TODO: Implement control code.
450 const UINT32 frames = GetTickCount();
451
452 if (4 > OutputBufferSize)
453 {
454 WLog_Print(urbdrc->log, WLOG_DEBUG, "out_size %" PRIu32 " > OutputBufferSize %" PRIu32, 4u,
455 OutputBufferSize);
456 return ERROR_BAD_CONFIGURATION;
457 }
458 wStream* out = urb_create_iocompletion(InterfaceId, MessageId, RequestId, 4);
459
460 if (!out)
461 return ERROR_OUTOFMEMORY;
462
463 Stream_Write_UINT32(out, frames);
464 return stream_write_and_free(callback->plugin, callback->channel, out);
465}
466
467/* [MS-RDPEUSB] 2.2.6.6 Query Device Text Response Message (QUERY_DEVICE_TEXT_RSP) */
468static UINT urbdrc_send_query_device_text_response(GENERIC_CHANNEL_CALLBACK* callback,
469 UINT32 InterfaceId, UINT32 MessageId, HRESULT hr,
470 const BYTE* text, uint8_t bytelen)
471{
472 WINPR_ASSERT(callback);
473
474 const uint8_t charlen = bytelen / sizeof(WCHAR);
475 wStream* out = create_shared_message_header_with_functionid(InterfaceId, MessageId, charlen,
476 8ULL + bytelen);
477
478 if (!out)
479 return ERROR_OUTOFMEMORY;
480
481 Stream_Write(out, text, bytelen); /* '\0' terminated unicode */
482 Stream_Write_INT32(out, hr);
483 return stream_write_and_free(callback->plugin, callback->channel, out);
484}
485
486static UINT urbdrc_process_query_device_text(IUDEVICE* pdev, GENERIC_CHANNEL_CALLBACK* callback,
487 wStream* s, UINT32 MessageId, IUDEVMAN* udevman)
488{
489 UINT32 TextType = 0;
490 UINT32 LocaleId = 0;
491 UINT8 bufferSize = 0xFF;
492 BYTE DeviceDescription[0x100] = WINPR_C_ARRAY_INIT;
493
494 if (!pdev || !callback || !s || !udevman)
495 return ERROR_INVALID_PARAMETER;
496 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
497 return ERROR_INVALID_DATA;
498
499 Stream_Read_UINT32(s, TextType);
500 Stream_Read_UINT32(s, LocaleId);
501 if (LocaleId > UINT16_MAX)
502 return ERROR_INVALID_DATA;
503
504 HRESULT hr = (HRESULT)pdev->control_query_device_text(pdev, TextType, (UINT16)LocaleId,
505 &bufferSize, DeviceDescription);
506 const UINT32 InterfaceId = ((STREAM_ID_STUB << 30) | pdev->get_UsbDevice(pdev));
507 return urbdrc_send_query_device_text_response(callback, InterfaceId, MessageId, hr,
508 DeviceDescription, bufferSize);
509}
510
511static void func_select_all_interface_for_msconfig(URBDRC_PLUGIN* urbdrc, IUDEVICE* pdev,
512 MSUSB_CONFIG_DESCRIPTOR* MsConfig)
513{
514 WINPR_ASSERT(urbdrc);
515 WINPR_ASSERT(pdev);
516 WINPR_ASSERT(MsConfig);
517
518 MSUSB_INTERFACE_DESCRIPTOR** MsInterfaces = MsConfig->MsInterfaces;
519 UINT32 NumInterfaces = MsConfig->NumInterfaces;
520
521 for (UINT32 inum = 0; inum < NumInterfaces; inum++)
522 {
523 const BYTE InterfaceNumber = MsInterfaces[inum]->InterfaceNumber;
524 const BYTE AlternateSetting = MsInterfaces[inum]->AlternateSetting;
525 const int rc = pdev->select_interface(pdev, InterfaceNumber, AlternateSetting);
526 if (rc < 0)
527 {
528 WLog_Print(urbdrc->log, WLOG_WARN,
529 "select_interface %" PRIu8 " [%" PRIu8 "] failed [%d]", InterfaceNumber,
530 AlternateSetting, rc);
531 }
532 }
533}
534
535/* [MS-RDPEUSB] 2.2.10.2 TS_URB_SELECT_CONFIGURATION_RESULT */
536static UINT send_urb_select_configuration_result(GENERIC_CHANNEL_CALLBACK* callback,
537 UINT32 InterfaceId, UINT32 MessageId,
538 UINT32 RequestId, UINT32 UrbStatus,
539 const MSUSB_CONFIG_DESCRIPTOR* MsConfig)
540{
541 wStream* out =
542 create_urb_completion_message(InterfaceId, MessageId, RequestId, URB_COMPLETION_NO_DATA);
543 if (!out)
544 return ERROR_OUTOFMEMORY;
545
546 const int size = 8 + ((MsConfig) ? MsConfig->MsOutSize : 8);
547 const uint16_t usize = WINPR_ASSERTING_INT_CAST(uint16_t, size);
548
549 if (!Stream_EnsureRemainingCapacity(out, 4))
550 goto fail;
551 Stream_Write_UINT32(out, usize); /* CbTsUrbResult */
552
553 if (!write_urb_result_header(out, usize, UrbStatus))
554 goto fail;
555
557 if (MsConfig)
558 {
559 if (!msusb_msconfig_write(MsConfig, out))
560 goto fail;
561 }
562 else
563 {
564 Stream_Write_UINT32(out, 0);
565 Stream_Write_UINT32(out, 0);
566 }
567
568 return send_urb_completion_message(callback, out, 0, 0, nullptr);
569
570fail:
571 Stream_Free(out, TRUE);
572 return ERROR_OUTOFMEMORY;
573}
574
575static UINT urb_select_configuration(IUDEVICE* pdev, GENERIC_CHANNEL_CALLBACK* callback, wStream* s,
576 UINT32 RequestField, UINT32 MessageId, IUDEVMAN* udevman,
577 int transferDir)
578{
579 MSUSB_CONFIG_DESCRIPTOR* MsConfig = nullptr;
580 UINT32 NumInterfaces = 0;
581 UINT32 usbd_status = 0;
582 BYTE ConfigurationDescriptorIsValid = 0;
583 URBDRC_PLUGIN* urbdrc = nullptr;
584 const BOOL noAck = (RequestField & 0x80000000U) != 0;
585 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
586
587 if (!callback || !s || !udevman || !pdev)
588 return ERROR_INVALID_PARAMETER;
589
590 urbdrc = (URBDRC_PLUGIN*)callback->plugin;
591
592 if (!urbdrc)
593 return ERROR_INVALID_PARAMETER;
594
595 if (transferDir == 0)
596 {
597 WLog_Print(urbdrc->log, WLOG_ERROR, "urb_select_configuration: unsupported transfer out");
598 return ERROR_INVALID_PARAMETER;
599 }
600
601 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
602 return ERROR_INVALID_DATA;
603
604 const UINT32 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
605 Stream_Read_UINT8(s, ConfigurationDescriptorIsValid);
606 Stream_Seek(s, 3); /* Padding */
607 Stream_Read_UINT32(s, NumInterfaces);
608
610 if (ConfigurationDescriptorIsValid)
611 {
612 /* parser data for struct config */
613 MsConfig = msusb_msconfig_read(s, NumInterfaces);
614
615 if (!MsConfig)
616 return ERROR_INVALID_DATA;
617
618 /* select config */
619 const int lrc = pdev->select_configuration(pdev, MsConfig->bConfigurationValue);
620 if (lrc != 0)
621 {
622 msusb_msconfig_free(MsConfig);
623 MsConfig = nullptr;
624 return ERROR_INTERNAL_ERROR;
625 }
626
627 /* select all interface */
628 func_select_all_interface_for_msconfig(urbdrc, pdev, MsConfig);
629 /* complete configuration setup */
630 if (!pdev->complete_msconfig_setup(pdev, MsConfig))
631 {
632 msusb_msconfig_free(MsConfig);
633 MsConfig = nullptr;
634 }
635 }
636
637 if (noAck)
638 return CHANNEL_RC_OK;
639 return send_urb_select_configuration_result(callback, InterfaceId, MessageId, RequestId,
640 usbd_status, MsConfig);
641}
642
643/* [MS-RDPEUSB[ 2.2.10.3 TS_URB_SELECT_INTERFACE_RESULT */
644static UINT urb_select_interface_result(GENERIC_CHANNEL_CALLBACK* callback, UINT32 RequestId,
645 UINT32 InterfaceId, UINT32 MessageId,
646 MSUSB_INTERFACE_DESCRIPTOR* MsInterface)
647{
648 WINPR_ASSERT(callback);
649 WINPR_ASSERT(MsInterface);
650
651 const uint32_t interface_size = 16U + (MsInterface->NumberOfPipes * 20U);
652 wStream* out =
653 create_urb_completion_message(InterfaceId, MessageId, RequestId, URB_COMPLETION_NO_DATA);
654
655 if (!out)
656 return ERROR_OUTOFMEMORY;
657
658 const uint32_t size = 8U + interface_size;
659 const uint16_t usize = WINPR_ASSERTING_INT_CAST(uint16_t, size);
660
661 if (!Stream_EnsureRemainingCapacity(out, 4))
662 goto fail;
663 Stream_Write_UINT32(out, usize); /* CbTsUrbResult */
664
665 if (!write_urb_result_header(out, usize, USBD_STATUS_SUCCESS))
666 goto fail;
667
668 if (!msusb_msinterface_write(MsInterface, out))
669 goto fail;
670
671 return send_urb_completion_message(callback, out, 0, 0, nullptr);
672
673fail:
674 Stream_Free(out, TRUE);
675
676 return ERROR_INTERNAL_ERROR;
677}
678
679static UINT urb_select_interface(IUDEVICE* pdev, GENERIC_CHANNEL_CALLBACK* callback, wStream* s,
680 UINT32 RequestField, UINT32 MessageId, IUDEVMAN* udevman,
681 int transferDir)
682{
683 const BOOL noAck = (RequestField & 0x80000000U) != 0;
684 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
685
686 if (!callback || !s || !udevman || !pdev)
687 return ERROR_INVALID_PARAMETER;
688
689 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)callback->plugin;
690
691 if (!urbdrc)
692 return ERROR_INVALID_PARAMETER;
693
694 if (transferDir == 0)
695 {
696 WLog_Print(urbdrc->log, WLOG_ERROR, "urb_select_interface: not support transfer out");
697 return ERROR_INVALID_PARAMETER;
698 }
699
700 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
701 return ERROR_INVALID_DATA;
702
703 const UINT32 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
704 // Currently unused, so just skip the data
705 // const UINT32 ConfigurationHandle = Stream_Get_UINT32(s);
706 Stream_Seek_UINT32(s);
707 MSUSB_INTERFACE_DESCRIPTOR* MsInterface = msusb_msinterface_read(s);
708
709 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4) || !MsInterface)
710 {
711 msusb_msinterface_free(MsInterface);
712 return ERROR_INVALID_DATA;
713 }
714
715 const UINT32 OutputBufferSize = Stream_Get_UINT32(s);
716 if (OutputBufferSize != 0)
717 {
718 WLog_Print(urbdrc->log, WLOG_ERROR,
719 "[MS-RDPEUSB] 2.2.9.3 TS_URB_SELECT_INTERFACE::OutputBufferSize must be 0, got "
720 "%" PRIu32,
721 OutputBufferSize);
722 msusb_msinterface_free(MsInterface);
723 return ERROR_INVALID_DATA;
724 }
725
726 const int lerr =
727 pdev->select_interface(pdev, MsInterface->InterfaceNumber, MsInterface->AlternateSetting);
728 if (lerr != 0)
729 {
730 msusb_msinterface_free(MsInterface);
731 return ERROR_INTERNAL_ERROR;
732 }
733
734 /* replace device's MsInterface */
735 MSUSB_CONFIG_DESCRIPTOR* MsConfig = pdev->get_MsConfig(pdev);
736 const uint8_t InterfaceNumber = MsInterface->InterfaceNumber;
737 if (!msusb_msinterface_replace(MsConfig, InterfaceNumber, MsInterface))
738 {
739 msusb_msinterface_free(MsInterface);
740 return ERROR_BAD_CONFIGURATION;
741 }
742
743 /* complete configuration setup */
744 if (!pdev->complete_msconfig_setup(pdev, MsConfig))
745 return ERROR_BAD_CONFIGURATION;
746
747 if (noAck)
748 return CHANNEL_RC_OK;
749
750 return urb_select_interface_result(callback, RequestId, InterfaceId, MessageId, MsInterface);
751}
752
753static UINT urb_control_transfer(IUDEVICE* pdev, GENERIC_CHANNEL_CALLBACK* callback, wStream* s,
754 UINT32 RequestField, UINT32 MessageId, IUDEVMAN* udevman,
755 int transferDir, int External)
756{
757 UINT32 out_size = 0;
758 UINT32 InterfaceId = 0;
759 UINT32 EndpointAddress = 0;
760 UINT32 PipeHandle = 0;
761 UINT32 TransferFlags = 0;
762 UINT32 OutputBufferSize = 0;
763 UINT32 usbd_status = 0;
764 UINT32 Timeout = 0;
765 BYTE bmRequestType = 0;
766 BYTE Request = 0;
767 UINT16 Value = 0;
768 UINT16 Index = 0;
769 UINT16 length = 0;
770 BYTE* buffer = nullptr;
771 wStream* out = nullptr;
772 URBDRC_PLUGIN* urbdrc = nullptr;
773 const BOOL noAck = (RequestField & 0x80000000U) != 0;
774 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
775
776 if (!callback || !s || !udevman || !pdev)
777 return ERROR_INVALID_PARAMETER;
778
779 urbdrc = (URBDRC_PLUGIN*)callback->plugin;
780
781 if (!urbdrc)
782 return ERROR_INVALID_PARAMETER;
783
784 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
785 return ERROR_INVALID_DATA;
786
787 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
788 Stream_Read_UINT32(s, PipeHandle);
789 Stream_Read_UINT32(s, TransferFlags);
790 EndpointAddress = (PipeHandle & 0x000000ff);
791 Timeout = 2000;
792
793 switch (External)
794 {
795 case URB_CONTROL_TRANSFER_EXTERNAL:
796 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
797 return ERROR_INVALID_DATA;
798
799 Stream_Read_UINT32(s, Timeout);
800 break;
801
802 case URB_CONTROL_TRANSFER_NONEXTERNAL:
803 break;
804 default:
805 break;
806 }
807
809 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
810 return ERROR_INVALID_DATA;
811
812 Stream_Read_UINT8(s, bmRequestType);
813 Stream_Read_UINT8(s, Request);
814 Stream_Read_UINT16(s, Value);
815 Stream_Read_UINT16(s, Index);
816 Stream_Read_UINT16(s, length);
817 Stream_Read_UINT32(s, OutputBufferSize);
818
819 if (length != OutputBufferSize)
820 {
821 WLog_Print(urbdrc->log, WLOG_ERROR, "urb_control_transfer ERROR: buf != length");
822 return ERROR_INVALID_DATA;
823 }
824
825 out_size = 36 + OutputBufferSize;
826 out = Stream_New(nullptr, out_size);
827
828 if (!out)
829 return ERROR_OUTOFMEMORY;
830
831 Stream_Seek(out, 36);
833 buffer = Stream_Pointer(out);
834
835 if (transferDir == USBD_TRANSFER_DIRECTION_OUT)
836 {
837 if (!Stream_CheckAndLogRequiredLength(TAG, s, OutputBufferSize))
838 {
839 Stream_Free(out, TRUE);
840 return ERROR_INVALID_DATA;
841 }
842 Stream_Copy(s, out, OutputBufferSize);
843 }
844
846 if (!pdev->control_transfer(pdev, RequestId, EndpointAddress, TransferFlags, bmRequestType,
847 Request, Value, Index, &usbd_status, &OutputBufferSize, buffer,
848 Timeout))
849 {
850 WLog_Print(urbdrc->log, WLOG_ERROR, "control_transfer failed");
851 Stream_Free(out, TRUE);
852 return ERROR_INTERNAL_ERROR;
853 }
854
855 return urb_write_completion(pdev, callback, noAck, out, InterfaceId, MessageId, RequestId,
856 usbd_status, OutputBufferSize, transferDir);
857}
858
859static void urb_bulk_transfer_cb(IUDEVICE* pdev, GENERIC_CHANNEL_CALLBACK* callback, wStream* out,
860 UINT32 InterfaceId, BOOL noAck, UINT32 MessageId, UINT32 RequestId,
861 WINPR_ATTR_UNUSED UINT32 NumberOfPackets, UINT32 status,
862 WINPR_ATTR_UNUSED UINT32 StartFrame,
863 WINPR_ATTR_UNUSED UINT32 ErrorCount, UINT32 OutputBufferSize,
864 int transferDir)
865{
866 if (!pdev->isChannelClosed(pdev))
867 urb_write_completion(pdev, callback, noAck, out, InterfaceId, MessageId, RequestId, status,
868 OutputBufferSize, transferDir);
869 else
870 Stream_Free(out, TRUE);
871}
872
873static UINT urb_bulk_or_interrupt_transfer(IUDEVICE* pdev, GENERIC_CHANNEL_CALLBACK* callback,
874 wStream* s, UINT32 RequestField, UINT32 MessageId,
875 IUDEVMAN* udevman, int transferDir)
876{
877 UINT32 EndpointAddress = 0;
878 UINT32 PipeHandle = 0;
879 UINT32 TransferFlags = 0;
880 UINT32 OutputBufferSize = 0;
881 const BOOL noAck = (RequestField & 0x80000000U) != 0;
882 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
883
884 if (!pdev || !callback || !s || !udevman)
885 return ERROR_INVALID_PARAMETER;
886
887 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
888 return ERROR_INVALID_DATA;
889
890 Stream_Read_UINT32(s, PipeHandle);
891 Stream_Read_UINT32(s, TransferFlags);
892 Stream_Read_UINT32(s, OutputBufferSize);
893 EndpointAddress = (PipeHandle & 0x000000ff);
894
895 if (transferDir == USBD_TRANSFER_DIRECTION_OUT)
896 {
897 if (!Stream_CheckAndLogRequiredLength(TAG, s, OutputBufferSize))
898 {
899 return ERROR_INVALID_DATA;
900 }
901 }
902
904 const int rc = pdev->bulk_or_interrupt_transfer(
905 pdev, callback, MessageId, RequestId, EndpointAddress, TransferFlags, noAck,
906 OutputBufferSize,
907 (transferDir == USBD_TRANSFER_DIRECTION_OUT) ? Stream_Pointer(s) : nullptr, transferDir,
908 urb_bulk_transfer_cb, 10000);
909
910 return (uint32_t)rc;
911}
912
913static void urb_isoch_transfer_cb(IUDEVICE* pdev, GENERIC_CHANNEL_CALLBACK* callback, wStream* out,
914 UINT32 InterfaceId, BOOL noAck, UINT32 MessageId,
915 UINT32 RequestId, UINT32 NumberOfPackets, UINT32 status,
916 UINT32 StartFrame, UINT32 ErrorCount, UINT32 OutputBufferSize,
917 int transferDir)
918{
919 /* The channel (and with it the callback) is gone, nobody to report to. */
920 if (pdev->isChannelClosed(pdev))
921 {
922 Stream_Free(out, TRUE);
923 return;
924 }
925
926 if (!noAck)
927 {
928 const UINT32 packetSize = (status == 0) ? NumberOfPackets * 12u : 0;
929 if ((NumberOfPackets > UINT32_MAX / 12) || (packetSize > UINT16_MAX - 20))
930 {
931 Stream_Free(out, TRUE);
932 return;
933 }
934
935 const UINT32 payloadSize = urb_completion_payload_size(transferDir, OutputBufferSize);
936 Stream_ResetPosition(out);
937
938 const UINT32 FunctionId = (payloadSize != 0) ? URB_COMPLETION : URB_COMPLETION_NO_DATA;
939 if (!write_shared_message_header_with_functionid(out, InterfaceId, MessageId, FunctionId))
940 {
941 Stream_Free(out, TRUE);
942 return;
943 }
944
945 Stream_Write_UINT32(out, RequestId);
946 Stream_Write_UINT32(out, 20 + packetSize);
947 if (!write_urb_result_header(out, WINPR_ASSERTING_INT_CAST(uint16_t, 20 + packetSize),
948 status))
949 {
950 Stream_Free(out, TRUE);
951 return;
952 }
953
954 Stream_Write_UINT32(out, StartFrame);
955
956 if (status == 0)
957 {
959 Stream_Write_UINT32(out, NumberOfPackets);
960 Stream_Write_UINT32(out, ErrorCount);
961 Stream_Seek(out, packetSize);
962 }
963 else
964 {
965 Stream_Write_UINT32(out, 0);
966 Stream_Write_UINT32(out, ErrorCount);
967 }
968
969 Stream_Write_UINT32(out, 0);
970 Stream_Write_UINT32(out, OutputBufferSize);
971 Stream_Seek(out, payloadSize);
972
973 const UINT rc = stream_write_and_free(callback->plugin, callback->channel, out);
974 if (rc != CHANNEL_RC_OK)
975 WLog_WARN(TAG, "stream_write_and_free failed with %" PRIu32, rc);
976 }
977}
978
979static UINT urb_isoch_transfer(IUDEVICE* pdev, GENERIC_CHANNEL_CALLBACK* callback, wStream* s,
980 UINT32 RequestField, UINT32 MessageId, IUDEVMAN* udevman,
981 int transferDir)
982{
983 int rc = 0;
984 UINT32 EndpointAddress = 0;
985 UINT32 PipeHandle = 0;
986 UINT32 TransferFlags = 0;
987 UINT32 StartFrame = 0;
988 UINT32 NumberOfPackets = 0;
989 UINT32 ErrorCount = 0;
990 UINT32 OutputBufferSize = 0;
991 BYTE* packetDescriptorData = nullptr;
992 const BOOL noAck = (RequestField & 0x80000000U) != 0;
993 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
994
995 if (!pdev || !callback || !udevman)
996 return ERROR_INVALID_PARAMETER;
997
998 if (!Stream_CheckAndLogRequiredLength(TAG, s, 20))
999 return ERROR_INVALID_DATA;
1000
1001 Stream_Read_UINT32(s, PipeHandle);
1002 EndpointAddress = (PipeHandle & 0x000000ff);
1003 Stream_Read_UINT32(s, TransferFlags);
1004 Stream_Read_UINT32(s, StartFrame);
1005 Stream_Read_UINT32(s, NumberOfPackets);
1006 Stream_Read_UINT32(s, ErrorCount);
1007
1008 if (!Stream_CheckAndLogRequiredLengthOfSize(TAG, s, NumberOfPackets, 12ull))
1009 return ERROR_INVALID_DATA;
1010
1011 if (NumberOfPackets > (UINT16_MAX - 20) / 12)
1012 {
1013 WLog_ERR(TAG,
1014 "[MS-RDPEUSB] 2.2.9.8 TS_URB_ISOCH_TRANSFER::NumberOfPackets %" PRIu32
1015 " too large, maximum allowed is %d",
1016 NumberOfPackets, (UINT16_MAX - 20) / 12);
1017 return ERROR_INVALID_DATA;
1018 }
1019
1020 packetDescriptorData = Stream_Pointer(s);
1021 Stream_Seek(s, 12ULL * NumberOfPackets);
1022
1023 if (!Stream_CheckAndLogRequiredLength(TAG, s, sizeof(UINT32)))
1024 return ERROR_INVALID_DATA;
1025 Stream_Read_UINT32(s, OutputBufferSize);
1026
1027 if (transferDir == USBD_TRANSFER_DIRECTION_OUT)
1028 {
1029 if (!Stream_CheckAndLogRequiredLength(TAG, s, OutputBufferSize))
1030 return ERROR_INVALID_DATA;
1031 }
1032
1033 rc = pdev->isoch_transfer(
1034 pdev, callback, MessageId, RequestId, EndpointAddress, TransferFlags, StartFrame,
1035 ErrorCount, noAck, packetDescriptorData, NumberOfPackets, OutputBufferSize,
1036 (transferDir == USBD_TRANSFER_DIRECTION_OUT) ? Stream_Pointer(s) : nullptr, transferDir,
1037 urb_isoch_transfer_cb, 2000);
1038
1039 if (rc < 0)
1040 return ERROR_INTERNAL_ERROR;
1041 return (UINT)rc;
1042}
1043
1044static UINT urb_control_descriptor_request(IUDEVICE* pdev, GENERIC_CHANNEL_CALLBACK* callback,
1045 wStream* s, UINT32 RequestField, UINT32 MessageId,
1046 IUDEVMAN* udevman, BYTE func_recipient, int transferDir)
1047{
1048 size_t out_size = 0;
1049 UINT32 InterfaceId = 0;
1050 UINT32 OutputBufferSize = 0;
1051 UINT32 usbd_status = 0;
1052 BYTE bmRequestType = 0;
1053 BYTE desc_index = 0;
1054 BYTE desc_type = 0;
1055 UINT16 langId = 0;
1056 wStream* out = nullptr;
1057 URBDRC_PLUGIN* urbdrc = nullptr;
1058 const BOOL noAck = (RequestField & 0x80000000U) != 0;
1059 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
1060
1061 if (!callback || !s || !udevman || !pdev)
1062 return ERROR_INVALID_PARAMETER;
1063
1064 urbdrc = (URBDRC_PLUGIN*)callback->plugin;
1065
1066 if (!urbdrc)
1067 return ERROR_INVALID_PARAMETER;
1068
1069 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
1070 return ERROR_INVALID_DATA;
1071
1072 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
1073 Stream_Read_UINT8(s, desc_index);
1074 Stream_Read_UINT8(s, desc_type);
1075 Stream_Read_UINT16(s, langId);
1076 Stream_Read_UINT32(s, OutputBufferSize);
1077 if (OutputBufferSize > UINT32_MAX - 36)
1078 return ERROR_INVALID_DATA;
1079 if (transferDir == USBD_TRANSFER_DIRECTION_OUT)
1080 {
1081 if (!Stream_CheckAndLogRequiredLength(TAG, s, OutputBufferSize))
1082 return ERROR_INVALID_DATA;
1083 }
1084
1085 out_size = 36ULL + OutputBufferSize;
1086 out = Stream_New(nullptr, out_size);
1087
1088 if (!out)
1089 return ERROR_OUTOFMEMORY;
1090
1091 Stream_Seek(out, 36);
1092 bmRequestType = func_recipient;
1093
1094 switch (transferDir)
1095 {
1096 case USBD_TRANSFER_DIRECTION_IN:
1097 bmRequestType |= 0x80;
1098 break;
1099
1100 case USBD_TRANSFER_DIRECTION_OUT:
1101 bmRequestType |= 0x00;
1102 Stream_Copy(s, out, OutputBufferSize);
1103 Stream_Rewind(out, OutputBufferSize);
1104 break;
1105
1106 default:
1107 WLog_Print(urbdrc->log, WLOG_DEBUG, "get error transferDir");
1108 OutputBufferSize = 0;
1109 usbd_status = USBD_STATUS_STALL_PID;
1110 break;
1111 }
1112
1114 if (!pdev->control_transfer(pdev, RequestId, 0, 0, bmRequestType,
1115 0x06, /* REQUEST_GET_DESCRIPTOR */
1116 WINPR_ASSERTING_INT_CAST(UINT16, ((desc_type << 8) | desc_index)),
1117 langId, &usbd_status, &OutputBufferSize, Stream_Pointer(out), 1000))
1118 {
1119 WLog_Print(urbdrc->log, WLOG_ERROR, "get_descriptor failed");
1120 Stream_Free(out, TRUE);
1121 return ERROR_INTERNAL_ERROR;
1122 }
1123
1124 return urb_write_completion(pdev, callback, noAck, out, InterfaceId, MessageId, RequestId,
1125 usbd_status, OutputBufferSize, transferDir);
1126}
1127
1128static UINT urb_control_get_status_request(IUDEVICE* pdev, GENERIC_CHANNEL_CALLBACK* callback,
1129 wStream* s, UINT32 RequestField, UINT32 MessageId,
1130 IUDEVMAN* udevman, BYTE func_recipient, int transferDir)
1131{
1132 size_t out_size = 0;
1133 UINT32 InterfaceId = 0;
1134 UINT32 OutputBufferSize = 0;
1135 UINT32 usbd_status = 0;
1136 UINT16 Index = 0;
1137 BYTE bmRequestType = 0;
1138 wStream* out = nullptr;
1139 URBDRC_PLUGIN* urbdrc = nullptr;
1140 const BOOL noAck = (RequestField & 0x80000000U) != 0;
1141 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
1142
1143 if (!callback || !s || !udevman || !pdev)
1144 return ERROR_INVALID_PARAMETER;
1145
1146 urbdrc = (URBDRC_PLUGIN*)callback->plugin;
1147
1148 if (!urbdrc)
1149 return ERROR_INVALID_PARAMETER;
1150
1151 if (transferDir == 0)
1152 {
1153 WLog_Print(urbdrc->log, WLOG_DEBUG,
1154 "urb_control_get_status_request: transfer out not supported");
1155 return ERROR_INVALID_PARAMETER;
1156 }
1157
1158 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
1159 return ERROR_INVALID_DATA;
1160
1161 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
1162 Stream_Read_UINT16(s, Index);
1163 Stream_Seek(s, 2);
1164 Stream_Read_UINT32(s, OutputBufferSize);
1165 if (OutputBufferSize > UINT32_MAX - 36)
1166 return ERROR_INVALID_DATA;
1167 out_size = 36ULL + OutputBufferSize;
1168 out = Stream_New(nullptr, out_size);
1169
1170 if (!out)
1171 return ERROR_OUTOFMEMORY;
1172
1173 Stream_Seek(out, 36);
1174 bmRequestType = func_recipient | 0x80;
1175
1176 if (!pdev->control_transfer(pdev, RequestId, 0, 0, bmRequestType, 0x00, /* REQUEST_GET_STATUS */
1177 0, Index, &usbd_status, &OutputBufferSize, Stream_Pointer(out),
1178 1000))
1179 {
1180 WLog_Print(urbdrc->log, WLOG_ERROR, "control_transfer failed");
1181 Stream_Free(out, TRUE);
1182 return ERROR_INTERNAL_ERROR;
1183 }
1184
1185 return urb_write_completion(pdev, callback, noAck, out, InterfaceId, MessageId, RequestId,
1186 usbd_status, OutputBufferSize, transferDir);
1187}
1188
1189static UINT urb_control_vendor_or_class_request(IUDEVICE* pdev, GENERIC_CHANNEL_CALLBACK* callback,
1190 wStream* s, UINT32 RequestField, UINT32 MessageId,
1191 IUDEVMAN* udevman, BYTE func_type,
1192 BYTE func_recipient, int transferDir)
1193{
1194 UINT32 out_size = 0;
1195 UINT32 InterfaceId = 0;
1196 UINT32 TransferFlags = 0;
1197 UINT32 usbd_status = 0;
1198 UINT32 OutputBufferSize = 0;
1199 BYTE ReqTypeReservedBits = 0;
1200 BYTE Request = 0;
1201 BYTE bmRequestType = 0;
1202 UINT16 Value = 0;
1203 UINT16 Index = 0;
1204 wStream* out = nullptr;
1205 URBDRC_PLUGIN* urbdrc = nullptr;
1206 const BOOL noAck = (RequestField & 0x80000000U) != 0;
1207 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
1208
1209 if (!callback || !s || !udevman || !pdev)
1210 return ERROR_INVALID_PARAMETER;
1211
1212 urbdrc = (URBDRC_PLUGIN*)callback->plugin;
1213
1214 if (!urbdrc)
1215 return ERROR_INVALID_PARAMETER;
1216
1217 if (!Stream_CheckAndLogRequiredLength(TAG, s, 16))
1218 return ERROR_INVALID_DATA;
1219
1220 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
1221 Stream_Read_UINT32(s, TransferFlags);
1222 Stream_Read_UINT8(s, ReqTypeReservedBits);
1223 Stream_Read_UINT8(s, Request);
1224 Stream_Read_UINT16(s, Value);
1225 Stream_Read_UINT16(s, Index);
1226 Stream_Seek_UINT16(s);
1227 Stream_Read_UINT32(s, OutputBufferSize);
1228 if (OutputBufferSize > UINT32_MAX - 36)
1229 return ERROR_INVALID_DATA;
1230
1231 if (transferDir == USBD_TRANSFER_DIRECTION_OUT)
1232 {
1233 if (!Stream_CheckAndLogRequiredLength(TAG, s, OutputBufferSize))
1234 return ERROR_INVALID_DATA;
1235 }
1236
1237 out_size = 36ULL + OutputBufferSize;
1238 out = Stream_New(nullptr, out_size);
1239
1240 if (!out)
1241 return ERROR_OUTOFMEMORY;
1242
1243 Stream_Seek(out, 36);
1244
1246 if (transferDir == USBD_TRANSFER_DIRECTION_OUT)
1247 {
1248 Stream_Copy(s, out, OutputBufferSize);
1249 Stream_Rewind(out, OutputBufferSize);
1250 }
1251
1253 bmRequestType = func_type | func_recipient;
1254
1255 if (TransferFlags & USBD_TRANSFER_DIRECTION)
1256 bmRequestType |= 0x80;
1257
1258 WLog_Print(urbdrc->log, WLOG_DEBUG,
1259 "RequestId 0x%" PRIx32 " TransferFlags: 0x%" PRIx32 " ReqTypeReservedBits: 0x%" PRIx8
1260 " "
1261 "Request:0x%" PRIx8 " Value: 0x%" PRIx16 " Index: 0x%" PRIx16
1262 " OutputBufferSize: 0x%" PRIx32 " bmRequestType: 0x%" PRIx8,
1263 RequestId, TransferFlags, ReqTypeReservedBits, Request, Value, Index,
1264 OutputBufferSize, bmRequestType);
1265
1266 if (!pdev->control_transfer(pdev, RequestId, 0, 0, bmRequestType, Request, Value, Index,
1267 &usbd_status, &OutputBufferSize, Stream_Pointer(out), 2000))
1268 {
1269 WLog_Print(urbdrc->log, WLOG_ERROR, "control_transfer failed");
1270 Stream_Free(out, TRUE);
1271 return ERROR_INTERNAL_ERROR;
1272 }
1273
1274 return urb_write_completion(pdev, callback, noAck, out, InterfaceId, MessageId, RequestId,
1275 usbd_status, OutputBufferSize, transferDir);
1276}
1277
1278static UINT urb_os_feature_descriptor_request(IUDEVICE* pdev, GENERIC_CHANNEL_CALLBACK* callback,
1279 wStream* s, UINT32 RequestField, UINT32 MessageId,
1280 IUDEVMAN* udevman, int transferDir)
1281{
1282 size_t out_size = 0;
1283 UINT32 InterfaceId = 0;
1284 UINT32 OutputBufferSize = 0;
1285 UINT32 usbd_status = 0;
1286 BYTE Recipient = 0;
1287 BYTE InterfaceNumber = 0;
1288 BYTE Ms_PageIndex = 0;
1289 UINT16 Ms_featureDescIndex = 0;
1290 wStream* out = nullptr;
1291 int ret = 0;
1292 URBDRC_PLUGIN* urbdrc = nullptr;
1293 const BOOL noAck = (RequestField & 0x80000000U) != 0;
1294 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
1295
1296 if (!callback || !s || !udevman || !pdev)
1297 return ERROR_INVALID_PARAMETER;
1298
1299 urbdrc = (URBDRC_PLUGIN*)callback->plugin;
1300
1301 if (!urbdrc)
1302 return ERROR_INVALID_PARAMETER;
1303
1304 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
1305 return ERROR_INVALID_DATA;
1306
1307 /* 2.2.9.15 TS_URB_OS_FEATURE_DESCRIPTOR_REQUEST */
1308 Stream_Read_UINT8(s, Recipient);
1309 Recipient = (Recipient & 0x1f); /* Mask out Padding1 */
1310 Stream_Read_UINT8(s, InterfaceNumber);
1311 Stream_Read_UINT8(s, Ms_PageIndex);
1312 Stream_Read_UINT16(s, Ms_featureDescIndex);
1313 Stream_Seek(s, 3); /* Padding 2 */
1314 Stream_Read_UINT32(s, OutputBufferSize);
1315 if (OutputBufferSize > UINT32_MAX - 36)
1316 return ERROR_INVALID_DATA;
1317
1318 switch (transferDir)
1319 {
1320 case USBD_TRANSFER_DIRECTION_OUT:
1321 if (!Stream_CheckAndLogRequiredLength(TAG, s, OutputBufferSize))
1322 return ERROR_INVALID_DATA;
1323
1324 break;
1325
1326 default:
1327 break;
1328 }
1329
1330 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
1331 out_size = 36ULL + OutputBufferSize;
1332 out = Stream_New(nullptr, out_size);
1333
1334 if (!out)
1335 return ERROR_OUTOFMEMORY;
1336
1337 Stream_Seek(out, 36);
1338
1339 switch (transferDir)
1340 {
1341 case USBD_TRANSFER_DIRECTION_OUT:
1342 Stream_Copy(s, out, OutputBufferSize);
1343 Stream_Rewind(out, OutputBufferSize);
1344 break;
1345
1346 case USBD_TRANSFER_DIRECTION_IN:
1347 break;
1348 default:
1349 break;
1350 }
1351
1352 WLog_Print(urbdrc->log, WLOG_DEBUG,
1353 "Ms descriptor arg: Recipient:0x%" PRIx8 ", "
1354 "InterfaceNumber:0x%" PRIx8 ", Ms_PageIndex:0x%" PRIx8 ", "
1355 "Ms_featureDescIndex:0x%" PRIx16 ", OutputBufferSize:0x%" PRIx32 "",
1356 Recipient, InterfaceNumber, Ms_PageIndex, Ms_featureDescIndex, OutputBufferSize);
1358 ret = pdev->os_feature_descriptor_request(pdev, RequestId, Recipient, InterfaceNumber,
1359 Ms_PageIndex, Ms_featureDescIndex, &usbd_status,
1360 &OutputBufferSize, Stream_Pointer(out), 1000);
1361
1362 if (ret < 0)
1363 {
1364 WLog_Print(urbdrc->log, WLOG_ERROR, "os_feature_descriptor_request: error num %d", ret);
1365 Stream_Free(out, TRUE);
1366 return ERROR_INTERNAL_ERROR;
1367 }
1368
1369 return urb_write_completion(pdev, callback, noAck, out, InterfaceId, MessageId, RequestId,
1370 usbd_status, OutputBufferSize, transferDir);
1371}
1372
1373static UINT urb_pipe_request(IUDEVICE* pdev, GENERIC_CHANNEL_CALLBACK* callback, wStream* s,
1374 UINT32 RequestField, UINT32 MessageId, IUDEVMAN* udevman,
1375 int transferDir, int action)
1376{
1377 UINT32 usbd_status = 0;
1378 UINT32 ret = USBD_STATUS_REQUEST_FAILED;
1379 int rc = 0;
1380 const BOOL noAck = (RequestField & 0x80000000U) != 0;
1381 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
1382
1383 if (!callback || !s || !udevman || !pdev)
1384 return ERROR_INVALID_PARAMETER;
1385
1386 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)callback->plugin;
1387
1388 if (!urbdrc)
1389 return ERROR_INVALID_PARAMETER;
1390
1391 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
1392 return ERROR_INVALID_DATA;
1393
1394 if (transferDir == 0)
1395 {
1396 WLog_Print(urbdrc->log, WLOG_DEBUG, "urb_pipe_request: not support transfer out");
1397 return ERROR_INVALID_PARAMETER;
1398 }
1399
1400 const UINT32 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
1401 const UINT32 PipeHandle = Stream_Get_UINT32(s);
1402 const UINT32 OutputBufferSize = Stream_Get_UINT32(s);
1403 const UINT32 EndpointAddress = (PipeHandle & 0x000000ff);
1404
1405 if (OutputBufferSize != 0)
1406 {
1407 WLog_Print(urbdrc->log, WLOG_DEBUG,
1408 "2.2.9.4 TS_URB_PIPE_REQUEST OutputBufferSize %" PRIu32 " != 0",
1409 OutputBufferSize);
1410 return ERROR_BAD_CONFIGURATION;
1411 }
1412
1413 switch (action)
1414 {
1415 case PIPE_CANCEL:
1416 rc = pdev->control_pipe_request(pdev, RequestId, EndpointAddress, &usbd_status,
1417 PIPE_CANCEL);
1418
1419 if (rc < 0)
1420 WLog_Print(urbdrc->log, WLOG_DEBUG, "PIPE SET HALT: error %u", ret);
1421 else
1422 ret = USBD_STATUS_SUCCESS;
1423
1424 break;
1425
1426 case PIPE_RESET:
1427 WLog_Print(urbdrc->log, WLOG_DEBUG, "urb_pipe_request: PIPE_RESET ep 0x%" PRIx32 "",
1428 EndpointAddress);
1429 rc = pdev->control_pipe_request(pdev, RequestId, EndpointAddress, &usbd_status,
1430 PIPE_RESET);
1431
1432 if (rc < 0)
1433 WLog_Print(urbdrc->log, WLOG_DEBUG, "PIPE RESET: error %u", ret);
1434 else
1435 ret = USBD_STATUS_SUCCESS;
1436
1437 break;
1438
1439 default:
1440 WLog_Print(urbdrc->log, WLOG_DEBUG, "urb_pipe_request action: %d not supported",
1441 action);
1442 ret = USBD_STATUS_INVALID_URB_FUNCTION;
1443 break;
1444 }
1445
1447
1448 wStream* out = Stream_New(nullptr, 36);
1449
1450 if (!out)
1451 return ERROR_OUTOFMEMORY;
1452
1453 return urb_write_completion(pdev, callback, noAck, out, InterfaceId, MessageId, RequestId, ret,
1454 0, transferDir);
1455}
1456/* [MS-RDPEUSB] 2.2.10.4 TS_URB_GET_CURRENT_FRAME_NUMBER_RESULT */
1457static UINT urb_send_current_frame_number_result(GENERIC_CHANNEL_CALLBACK* callback,
1458 UINT32 RequestId, UINT32 MessageId,
1459 UINT32 CompletionId, UINT32 FrameNumber)
1460{
1461 WINPR_ASSERT(callback);
1462
1463 const UINT32 InterfaceId = ((STREAM_ID_PROXY << 30) | CompletionId);
1464 wStream* out =
1465 create_urb_completion_message(InterfaceId, MessageId, RequestId, URB_COMPLETION_NO_DATA);
1466
1467 if (!out)
1468 return ERROR_OUTOFMEMORY;
1469
1470 if (!Stream_EnsureRemainingCapacity(out, 4))
1471 goto fail;
1472 Stream_Write_UINT32(out, 12);
1473 if (!write_urb_result_header(out, 12, USBD_STATUS_SUCCESS))
1474 goto fail;
1475 Stream_Write_UINT32(out, FrameNumber);
1476 return send_urb_completion_message(callback, out, 0, 0, nullptr);
1477
1478fail:
1479 Stream_Free(out, TRUE);
1480 return ERROR_OUTOFMEMORY;
1481}
1482
1483static UINT urb_get_current_frame_number(IUDEVICE* pdev, GENERIC_CHANNEL_CALLBACK* callback,
1484 wStream* s, UINT32 RequestField, UINT32 MessageId,
1485 IUDEVMAN* udevman, int transferDir)
1486{
1487 const BOOL noAck = (RequestField & 0x80000000U) != 0;
1488 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
1489
1490 if (!callback || !s || !udevman || !pdev)
1491 return ERROR_INVALID_PARAMETER;
1492
1493 URBDRC_PLUGIN* urbdrc = (URBDRC_PLUGIN*)callback->plugin;
1494
1495 if (!urbdrc)
1496 return ERROR_INVALID_PARAMETER;
1497
1498 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1499 return ERROR_INVALID_DATA;
1500
1501 if (transferDir == 0)
1502 {
1503 WLog_Print(urbdrc->log, WLOG_DEBUG,
1504 "urb_get_current_frame_number: not support transfer out");
1505 return ERROR_INVALID_PARAMETER;
1506 }
1507
1508 const UINT32 OutputBufferSize = Stream_Get_UINT32(s);
1509 if (OutputBufferSize != 0)
1510 {
1511 WLog_Print(urbdrc->log, WLOG_WARN, "OutputBufferSize=%" PRIu32 ", expected 0",
1512 OutputBufferSize);
1513 }
1515 const UINT32 dummy_frames = GetTickCount();
1516 const UINT32 CompletionId = pdev->get_ReqCompletion(pdev);
1517
1518 if (noAck)
1519 return CHANNEL_RC_OK;
1520
1521 return urb_send_current_frame_number_result(callback, RequestId, MessageId, CompletionId,
1522 dummy_frames);
1523}
1524
1525/* Unused function for current server */
1526static UINT urb_control_get_configuration_request(IUDEVICE* pdev,
1527 GENERIC_CHANNEL_CALLBACK* callback, wStream* s,
1528 UINT32 RequestField, UINT32 MessageId,
1529 IUDEVMAN* udevman, int transferDir)
1530{
1531 size_t out_size = 0;
1532 UINT32 InterfaceId = 0;
1533 UINT32 OutputBufferSize = 0;
1534 UINT32 usbd_status = 0;
1535 wStream* out = nullptr;
1536 URBDRC_PLUGIN* urbdrc = nullptr;
1537 const BOOL noAck = (RequestField & 0x80000000U) != 0;
1538 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
1539
1540 if (!callback || !s || !udevman || !pdev)
1541 return ERROR_INVALID_PARAMETER;
1542
1543 urbdrc = (URBDRC_PLUGIN*)callback->plugin;
1544
1545 if (!urbdrc)
1546 return ERROR_INVALID_PARAMETER;
1547
1548 if (transferDir == 0)
1549 {
1550 WLog_Print(urbdrc->log, WLOG_DEBUG,
1551 "urb_control_get_configuration_request:"
1552 " not support transfer out");
1553 return ERROR_INVALID_PARAMETER;
1554 }
1555
1556 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1557 return ERROR_INVALID_DATA;
1558
1559 Stream_Read_UINT32(s, OutputBufferSize);
1560 if (OutputBufferSize > UINT32_MAX - 36)
1561 return ERROR_INVALID_DATA;
1562 out_size = 36ULL + OutputBufferSize;
1563 out = Stream_New(nullptr, out_size);
1564
1565 if (!out)
1566 return ERROR_OUTOFMEMORY;
1567
1568 Stream_Seek(out, 36);
1569 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
1570
1571 if (!pdev->control_transfer(pdev, RequestId, 0, 0, 0x80 | 0x00,
1572 0x08, /* REQUEST_GET_CONFIGURATION */
1573 0, 0, &usbd_status, &OutputBufferSize, Stream_Pointer(out), 1000))
1574 {
1575 WLog_Print(urbdrc->log, WLOG_DEBUG, "control_transfer failed");
1576 Stream_Free(out, TRUE);
1577 return ERROR_INTERNAL_ERROR;
1578 }
1579
1580 return urb_write_completion(pdev, callback, noAck, out, InterfaceId, MessageId, RequestId,
1581 usbd_status, OutputBufferSize, transferDir);
1582}
1583
1584/* Unused function for current server */
1585static UINT urb_control_get_interface_request(IUDEVICE* pdev, GENERIC_CHANNEL_CALLBACK* callback,
1586 wStream* s, UINT32 RequestField, UINT32 MessageId,
1587 IUDEVMAN* udevman, int transferDir)
1588{
1589 size_t out_size = 0;
1590 UINT32 InterfaceId = 0;
1591 UINT32 OutputBufferSize = 0;
1592 UINT32 usbd_status = 0;
1593 UINT16 InterfaceNr = 0;
1594 wStream* out = nullptr;
1595 URBDRC_PLUGIN* urbdrc = nullptr;
1596 const BOOL noAck = (RequestField & 0x80000000U) != 0;
1597 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
1598
1599 if (!callback || !s || !udevman || !pdev)
1600 return ERROR_INVALID_PARAMETER;
1601
1602 urbdrc = (URBDRC_PLUGIN*)callback->plugin;
1603
1604 if (!urbdrc)
1605 return ERROR_INVALID_PARAMETER;
1606
1607 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
1608 return ERROR_INVALID_DATA;
1609
1610 if (transferDir == 0)
1611 {
1612 WLog_Print(urbdrc->log, WLOG_DEBUG,
1613 "urb_control_get_interface_request: not support transfer out");
1614 return ERROR_INVALID_PARAMETER;
1615 }
1616
1617 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
1618 Stream_Read_UINT16(s, InterfaceNr);
1619 Stream_Seek(s, 2);
1620 Stream_Read_UINT32(s, OutputBufferSize);
1621 if (OutputBufferSize > UINT32_MAX - 36)
1622 return ERROR_INVALID_DATA;
1623 out_size = 36ULL + OutputBufferSize;
1624 out = Stream_New(nullptr, out_size);
1625
1626 if (!out)
1627 return ERROR_OUTOFMEMORY;
1628
1629 Stream_Seek(out, 36);
1630
1631 if (!pdev->control_transfer(
1632 pdev, RequestId, 0, 0, 0x80 | 0x01, 0x0A, /* REQUEST_GET_INTERFACE */
1633 0, InterfaceNr, &usbd_status, &OutputBufferSize, Stream_Pointer(out), 1000))
1634 {
1635 WLog_Print(urbdrc->log, WLOG_DEBUG, "control_transfer failed");
1636 Stream_Free(out, TRUE);
1637 return ERROR_INTERNAL_ERROR;
1638 }
1639
1640 return urb_write_completion(pdev, callback, noAck, out, InterfaceId, MessageId, RequestId,
1641 usbd_status, OutputBufferSize, transferDir);
1642}
1643
1644static UINT urb_control_feature_request(IUDEVICE* pdev, GENERIC_CHANNEL_CALLBACK* callback,
1645 wStream* s, UINT32 RequestField, UINT32 MessageId,
1646 IUDEVMAN* udevman, BYTE func_recipient, BYTE command,
1647 int transferDir)
1648{
1649 UINT32 InterfaceId = 0;
1650 UINT32 OutputBufferSize = 0;
1651 UINT32 usbd_status = 0;
1652 UINT16 FeatureSelector = 0;
1653 UINT16 Index = 0;
1654 BYTE bmRequestType = 0;
1655 BYTE bmRequest = 0;
1656 wStream* out = nullptr;
1657 URBDRC_PLUGIN* urbdrc = nullptr;
1658 const BOOL noAck = (RequestField & 0x80000000U) != 0;
1659 const UINT32 RequestId = RequestField & 0x7FFFFFFF;
1660
1661 if (!callback || !s || !udevman || !pdev)
1662 return ERROR_INVALID_PARAMETER;
1663
1664 urbdrc = (URBDRC_PLUGIN*)callback->plugin;
1665
1666 if (!urbdrc)
1667 return ERROR_INVALID_PARAMETER;
1668
1669 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
1670 return ERROR_INVALID_DATA;
1671
1672 InterfaceId = ((STREAM_ID_PROXY << 30) | pdev->get_ReqCompletion(pdev));
1673 Stream_Read_UINT16(s, FeatureSelector);
1674 Stream_Read_UINT16(s, Index);
1675 Stream_Read_UINT32(s, OutputBufferSize);
1676 if (OutputBufferSize > UINT32_MAX - 36)
1677 return ERROR_INVALID_DATA;
1678 switch (transferDir)
1679 {
1680 case USBD_TRANSFER_DIRECTION_OUT:
1681 if (!Stream_CheckAndLogRequiredLength(TAG, s, OutputBufferSize))
1682 return ERROR_INVALID_DATA;
1683
1684 break;
1685
1686 default:
1687 break;
1688 }
1689
1690 out = Stream_New(nullptr, 36ULL + OutputBufferSize);
1691
1692 if (!out)
1693 return ERROR_OUTOFMEMORY;
1694
1695 Stream_Seek(out, 36);
1696 bmRequestType = func_recipient;
1697
1698 switch (transferDir)
1699 {
1700 case USBD_TRANSFER_DIRECTION_OUT:
1701 WLog_Print(urbdrc->log, WLOG_ERROR,
1702 "Function urb_control_feature_request: OUT Unchecked");
1703 Stream_Copy(s, out, OutputBufferSize);
1704 Stream_Rewind(out, OutputBufferSize);
1705 bmRequestType |= 0x00;
1706 break;
1707
1708 case USBD_TRANSFER_DIRECTION_IN:
1709 bmRequestType |= 0x80;
1710 break;
1711 default:
1712 break;
1713 }
1714
1715 switch (command)
1716 {
1717 case URB_SET_FEATURE:
1718 bmRequest = 0x03; /* REQUEST_SET_FEATURE */
1719 break;
1720
1721 case URB_CLEAR_FEATURE:
1722 bmRequest = 0x01; /* REQUEST_CLEAR_FEATURE */
1723 break;
1724
1725 default:
1726 WLog_Print(urbdrc->log, WLOG_ERROR,
1727 "urb_control_feature_request: Error Command 0x%02" PRIx8 "", command);
1728 Stream_Free(out, TRUE);
1729 return ERROR_INTERNAL_ERROR;
1730 }
1731
1732 if (!pdev->control_transfer(pdev, RequestId, 0, 0, bmRequestType, bmRequest, FeatureSelector,
1733 Index, &usbd_status, &OutputBufferSize, Stream_Pointer(out), 1000))
1734 {
1735 WLog_Print(urbdrc->log, WLOG_DEBUG, "feature control transfer failed");
1736 Stream_Free(out, TRUE);
1737 return ERROR_INTERNAL_ERROR;
1738 }
1739
1740 return urb_write_completion(pdev, callback, noAck, out, InterfaceId, MessageId, RequestId,
1741 usbd_status, OutputBufferSize, transferDir);
1742}
1743
1744static UINT urbdrc_process_transfer_request(IUDEVICE* pdev, GENERIC_CHANNEL_CALLBACK* callback,
1745 wStream* s, UINT32 MessageId, IUDEVMAN* udevman,
1746 int transferDir)
1747{
1748 UINT32 CbTsUrb = 0;
1749 UINT16 Size = 0;
1750 UINT16 URB_Function = 0;
1751 UINT32 RequestId = 0;
1752 UINT error = ERROR_INTERNAL_ERROR;
1753 URBDRC_PLUGIN* urbdrc = nullptr;
1754
1755 if (!callback || !s || !udevman || !pdev)
1756 return ERROR_INVALID_PARAMETER;
1757
1758 urbdrc = (URBDRC_PLUGIN*)callback->plugin;
1759
1760 if (!urbdrc)
1761 return ERROR_INVALID_PARAMETER;
1762
1763 if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
1764 return ERROR_INVALID_DATA;
1765
1766 Stream_Read_UINT32(s, CbTsUrb);
1767 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4ULL + CbTsUrb))
1768 return ERROR_INVALID_DATA;
1769 Stream_Read_UINT16(s, Size);
1770 if (Size != CbTsUrb)
1771 {
1772 const char* section = (transferDir == USBD_TRANSFER_DIRECTION_IN)
1773 ? "2.2.6.7 Transfer In Request (TRANSFER_IN_REQUEST)"
1774 : "2.2.6.8 Transfer Out Request (TRANSFER_OUT_REQUEST)";
1775 WLog_ERR(TAG,
1776 "[MS-RDPEUSB] 2.2.9.1.1 TS_URB_HEADER::Size 0x%04" PRIx16
1777 " != %s::CbTsUrb 0x%08" PRIx32,
1778 Size, section, CbTsUrb);
1779 return ERROR_INVALID_DATA;
1780 }
1781 Stream_Read_UINT16(s, URB_Function);
1782 Stream_Read_UINT32(s, RequestId);
1783 WLog_Print(urbdrc->log, WLOG_DEBUG, "URB %s[%" PRIu16 "]", urb_function_string(URB_Function),
1784 URB_Function);
1785
1786 switch (URB_Function)
1787 {
1788 case TS_URB_SELECT_CONFIGURATION:
1789 error = urb_select_configuration(pdev, callback, s, RequestId, MessageId, udevman,
1790 transferDir);
1791 break;
1792
1793 case TS_URB_SELECT_INTERFACE:
1794 error =
1795 urb_select_interface(pdev, callback, s, RequestId, MessageId, udevman, transferDir);
1796 break;
1797
1798 case TS_URB_PIPE_REQUEST:
1799 error = urb_pipe_request(pdev, callback, s, RequestId, MessageId, udevman, transferDir,
1800 PIPE_CANCEL);
1801 break;
1802
1803 case TS_URB_TAKE_FRAME_LENGTH_CONTROL:
1807 break;
1808
1809 case TS_URB_RELEASE_FRAME_LENGTH_CONTROL:
1813 break;
1814
1815 case TS_URB_GET_FRAME_LENGTH:
1819 break;
1820
1821 case TS_URB_SET_FRAME_LENGTH:
1825 break;
1826
1827 case TS_URB_GET_CURRENT_FRAME_NUMBER:
1828 error = urb_get_current_frame_number(pdev, callback, s, RequestId, MessageId, udevman,
1829 transferDir);
1830 break;
1831
1832 case TS_URB_CONTROL_TRANSFER:
1833 error = urb_control_transfer(pdev, callback, s, RequestId, MessageId, udevman,
1834 transferDir, URB_CONTROL_TRANSFER_NONEXTERNAL);
1835 break;
1836
1837 case TS_URB_BULK_OR_INTERRUPT_TRANSFER:
1838 error = urb_bulk_or_interrupt_transfer(pdev, callback, s, RequestId, MessageId, udevman,
1839 transferDir);
1840 break;
1841
1842 case TS_URB_ISOCH_TRANSFER:
1843 error =
1844 urb_isoch_transfer(pdev, callback, s, RequestId, MessageId, udevman, transferDir);
1845 break;
1846
1847 case TS_URB_GET_DESCRIPTOR_FROM_DEVICE:
1848 error = urb_control_descriptor_request(pdev, callback, s, RequestId, MessageId, udevman,
1849 0x00, transferDir);
1850 break;
1851
1852 case TS_URB_SET_DESCRIPTOR_TO_DEVICE:
1853 error = urb_control_descriptor_request(pdev, callback, s, RequestId, MessageId, udevman,
1854 0x00, transferDir);
1855 break;
1856
1857 case TS_URB_SET_FEATURE_TO_DEVICE:
1858 error = urb_control_feature_request(pdev, callback, s, RequestId, MessageId, udevman,
1859 0x00, URB_SET_FEATURE, transferDir);
1860 break;
1861
1862 case TS_URB_SET_FEATURE_TO_INTERFACE:
1863 error = urb_control_feature_request(pdev, callback, s, RequestId, MessageId, udevman,
1864 0x01, URB_SET_FEATURE, transferDir);
1865 break;
1866
1867 case TS_URB_SET_FEATURE_TO_ENDPOINT:
1868 error = urb_control_feature_request(pdev, callback, s, RequestId, MessageId, udevman,
1869 0x02, URB_SET_FEATURE, transferDir);
1870 break;
1871
1872 case TS_URB_CLEAR_FEATURE_TO_DEVICE:
1873 error = urb_control_feature_request(pdev, callback, s, RequestId, MessageId, udevman,
1874 0x00, URB_CLEAR_FEATURE, transferDir);
1875 break;
1876
1877 case TS_URB_CLEAR_FEATURE_TO_INTERFACE:
1878 error = urb_control_feature_request(pdev, callback, s, RequestId, MessageId, udevman,
1879 0x01, URB_CLEAR_FEATURE, transferDir);
1880 break;
1881
1882 case TS_URB_CLEAR_FEATURE_TO_ENDPOINT:
1883 error = urb_control_feature_request(pdev, callback, s, RequestId, MessageId, udevman,
1884 0x02, URB_CLEAR_FEATURE, transferDir);
1885 break;
1886
1887 case TS_URB_GET_STATUS_FROM_DEVICE:
1888 error = urb_control_get_status_request(pdev, callback, s, RequestId, MessageId, udevman,
1889 0x00, transferDir);
1890 break;
1891
1892 case TS_URB_GET_STATUS_FROM_INTERFACE:
1893 error = urb_control_get_status_request(pdev, callback, s, RequestId, MessageId, udevman,
1894 0x01, transferDir);
1895 break;
1896
1897 case TS_URB_GET_STATUS_FROM_ENDPOINT:
1898 error = urb_control_get_status_request(pdev, callback, s, RequestId, MessageId, udevman,
1899 0x02, transferDir);
1900 break;
1901
1902 case TS_URB_RESERVED_0X0016:
1903 break;
1904
1905 case TS_URB_VENDOR_DEVICE:
1906 error = urb_control_vendor_or_class_request(pdev, callback, s, RequestId, MessageId,
1907 udevman, (0x02u << 5), /* vendor type */
1908 0x00, transferDir);
1909 break;
1910
1911 case TS_URB_VENDOR_INTERFACE:
1912 error = urb_control_vendor_or_class_request(pdev, callback, s, RequestId, MessageId,
1913 udevman, (0x02u << 5), /* vendor type */
1914 0x01, transferDir);
1915 break;
1916
1917 case TS_URB_VENDOR_ENDPOINT:
1918 error = urb_control_vendor_or_class_request(pdev, callback, s, RequestId, MessageId,
1919 udevman, (0x02u << 5), /* vendor type */
1920 0x02, transferDir);
1921 break;
1922
1923 case TS_URB_CLASS_DEVICE:
1924 error = urb_control_vendor_or_class_request(pdev, callback, s, RequestId, MessageId,
1925 udevman, (0x01u << 5), /* class type */
1926 0x00, transferDir);
1927 break;
1928
1929 case TS_URB_CLASS_INTERFACE:
1930 error = urb_control_vendor_or_class_request(pdev, callback, s, RequestId, MessageId,
1931 udevman, (0x01u << 5), /* class type */
1932 0x01, transferDir);
1933 break;
1934
1935 case TS_URB_CLASS_ENDPOINT:
1936 error = urb_control_vendor_or_class_request(pdev, callback, s, RequestId, MessageId,
1937 udevman, (0x01u << 5), /* class type */
1938 0x02, transferDir);
1939 break;
1940
1941 case TS_URB_RESERVE_0X001D:
1942 break;
1943
1944 case TS_URB_SYNC_RESET_PIPE_AND_CLEAR_STALL:
1945 error = urb_pipe_request(pdev, callback, s, RequestId, MessageId, udevman, transferDir,
1946 PIPE_RESET);
1947 break;
1948
1949 case TS_URB_CLASS_OTHER:
1950 error = urb_control_vendor_or_class_request(pdev, callback, s, RequestId, MessageId,
1951 udevman, (0x01u << 5), /* class type */
1952 0x03, transferDir);
1953 break;
1954
1955 case TS_URB_VENDOR_OTHER:
1956 error = urb_control_vendor_or_class_request(pdev, callback, s, RequestId, MessageId,
1957 udevman, (0x02u << 5), /* vendor type */
1958 0x03, transferDir);
1959 break;
1960
1961 case TS_URB_GET_STATUS_FROM_OTHER:
1962 error = urb_control_get_status_request(pdev, callback, s, RequestId, MessageId, udevman,
1963 0x03, transferDir);
1964 break;
1965
1966 case TS_URB_CLEAR_FEATURE_TO_OTHER:
1967 error = urb_control_feature_request(pdev, callback, s, RequestId, MessageId, udevman,
1968 0x03, URB_CLEAR_FEATURE, transferDir);
1969 break;
1970
1971 case TS_URB_SET_FEATURE_TO_OTHER:
1972 error = urb_control_feature_request(pdev, callback, s, RequestId, MessageId, udevman,
1973 0x03, URB_SET_FEATURE, transferDir);
1974 break;
1975
1976 case TS_URB_GET_DESCRIPTOR_FROM_ENDPOINT:
1977 error = urb_control_descriptor_request(pdev, callback, s, RequestId, MessageId, udevman,
1978 0x02, transferDir);
1979 break;
1980
1981 case TS_URB_SET_DESCRIPTOR_TO_ENDPOINT:
1982 error = urb_control_descriptor_request(pdev, callback, s, RequestId, MessageId, udevman,
1983 0x02, transferDir);
1984 break;
1985
1986 case TS_URB_CONTROL_GET_CONFIGURATION_REQUEST:
1987 error = urb_control_get_configuration_request(pdev, callback, s, RequestId, MessageId,
1988 udevman, transferDir);
1989 break;
1990
1991 case TS_URB_CONTROL_GET_INTERFACE_REQUEST:
1992 error = urb_control_get_interface_request(pdev, callback, s, RequestId, MessageId,
1993 udevman, transferDir);
1994 break;
1995
1996 case TS_URB_GET_DESCRIPTOR_FROM_INTERFACE:
1997 error = urb_control_descriptor_request(pdev, callback, s, RequestId, MessageId, udevman,
1998 0x01, transferDir);
1999 break;
2000
2001 case TS_URB_SET_DESCRIPTOR_TO_INTERFACE:
2002 error = urb_control_descriptor_request(pdev, callback, s, RequestId, MessageId, udevman,
2003 0x01, transferDir);
2004 break;
2005
2006 case TS_URB_GET_OS_FEATURE_DESCRIPTOR_REQUEST:
2007 error = urb_os_feature_descriptor_request(pdev, callback, s, RequestId, MessageId,
2008 udevman, transferDir);
2009 break;
2010
2011 case TS_URB_RESERVE_0X002B:
2012 case TS_URB_RESERVE_0X002C:
2013 case TS_URB_RESERVE_0X002D:
2014 case TS_URB_RESERVE_0X002E:
2015 case TS_URB_RESERVE_0X002F:
2016 break;
2017
2019 case TS_URB_SYNC_RESET_PIPE:
2020 error = urb_pipe_request(pdev, callback, s, RequestId, MessageId, udevman, transferDir,
2021 PIPE_RESET);
2022 break;
2023
2024 case TS_URB_SYNC_CLEAR_STALL:
2025 urb_pipe_request(pdev, callback, s, RequestId, MessageId, udevman, transferDir,
2026 PIPE_RESET);
2027 break;
2028
2029 case TS_URB_CONTROL_TRANSFER_EX:
2030 error = urb_control_transfer(pdev, callback, s, RequestId, MessageId, udevman,
2031 transferDir, URB_CONTROL_TRANSFER_EXTERNAL);
2032 break;
2033
2034 default:
2035 WLog_Print(urbdrc->log, WLOG_DEBUG, "URB_Func: %" PRIx16 " is not found!",
2036 URB_Function);
2037 break;
2038 }
2039
2040 if (error)
2041 {
2042 WLog_Print(urbdrc->log, WLOG_WARN,
2043 "USB transfer request URB Function '%s' [0x%08x] failed with %08" PRIx32,
2044 urb_function_string(URB_Function), URB_Function, error);
2045 }
2046
2047 return error;
2048}
2049
2050UINT urbdrc_process_udev_data_transfer(GENERIC_CHANNEL_CALLBACK* callback, URBDRC_PLUGIN* urbdrc,
2051 IUDEVMAN* udevman, wStream* data)
2052{
2053 UINT32 InterfaceId = 0;
2054 UINT32 MessageId = 0;
2055 UINT32 FunctionId = 0;
2056 IUDEVICE* pdev = nullptr;
2057 UINT error = ERROR_INTERNAL_ERROR;
2058
2059 if (!urbdrc || !data || !callback || !udevman)
2060 goto fail;
2061
2062 if (!Stream_CheckAndLogRequiredLength(TAG, data, 8))
2063 goto fail;
2064
2065 Stream_Rewind_UINT32(data);
2066
2067 Stream_Read_UINT32(data, InterfaceId);
2068 Stream_Read_UINT32(data, MessageId);
2069 Stream_Read_UINT32(data, FunctionId);
2070
2071 pdev = udevman->get_udevice_by_UsbDevice(udevman, InterfaceId);
2072
2073 /* Device does not exist, ignore this request. */
2074 if (pdev == nullptr)
2075 {
2076 error = ERROR_SUCCESS;
2077 goto fail;
2078 }
2079
2080 /* Device has been removed, ignore this request. */
2081 if (pdev->isChannelClosed(pdev))
2082 {
2083 error = ERROR_SUCCESS;
2084 goto fail;
2085 }
2086
2087 /* Whatever channel the request arrived on, answer on the channel the device is bound to: a
2088 * pending transfer keeps the callback of that channel and closing it drains the device before
2089 * the callback is freed. Any other channel may go away while the transfer is still pending. */
2090 callback = pdev->get_channelCallback(pdev);
2091
2092 /* Device has not been announced yet, ignore this request. */
2093 if (callback == nullptr)
2094 {
2095 error = ERROR_SUCCESS;
2096 goto fail;
2097 }
2098
2099 /* USB kernel driver detach!! */
2100 if (!pdev->detach_kernel_driver(pdev))
2101 {
2102 error = ERROR_SUCCESS;
2103 goto fail;
2104 }
2105
2106 switch (FunctionId)
2107 {
2108 case CANCEL_REQUEST:
2109 error = urbdrc_process_cancel_request(pdev, data, udevman);
2110 break;
2111
2112 case REGISTER_REQUEST_CALLBACK:
2113 error = urbdrc_process_register_request_callback(pdev, callback, data, udevman);
2114 break;
2115
2116 case IO_CONTROL:
2117 error = urbdrc_process_io_control(pdev, callback, data, MessageId, udevman);
2118 break;
2119
2120 case INTERNAL_IO_CONTROL:
2121 error = urbdrc_process_internal_io_control(pdev, callback, data, MessageId, udevman);
2122 break;
2123
2124 case QUERY_DEVICE_TEXT:
2125 error = urbdrc_process_query_device_text(pdev, callback, data, MessageId, udevman);
2126 break;
2127
2128 case TRANSFER_IN_REQUEST:
2129 error = urbdrc_process_transfer_request(pdev, callback, data, MessageId, udevman,
2130 USBD_TRANSFER_DIRECTION_IN);
2131 break;
2132
2133 case TRANSFER_OUT_REQUEST:
2134 error = urbdrc_process_transfer_request(pdev, callback, data, MessageId, udevman,
2135 USBD_TRANSFER_DIRECTION_OUT);
2136 break;
2137
2138 case RETRACT_DEVICE:
2139 error = urbdrc_process_retract_device_request(pdev, data, udevman);
2140 break;
2141
2142 default:
2143 WLog_Print(urbdrc->log, WLOG_WARN,
2144 "urbdrc_process_udev_data_transfer:"
2145 " unknown FunctionId 0x%" PRIX32 "",
2146 FunctionId);
2147 break;
2148 }
2149
2150fail:
2151 if (error)
2152 {
2153 WLog_WARN(TAG, "USB request failed with %08" PRIx32, error);
2154 }
2155
2156 return error;
2157}