The HID failure came down to the endpoint type map being indexed by endpoint number without the direction bit. A composite device can have endpoint 1 as both interrupt IN (0x81) and bulk OUT (0x01); the last one read won, so interrupt URBs were submitted as bulk and the kernel rejected them. The device attached and stayed silent. Endpoint data now comes from the raw descriptors read from /dev/bus/usb rather than sysfs, which only ever exposes the active alternate setting — a webcam's isochronous endpoints are invisible there because they only exist after SET_INTERFACE. Two sysfs parsing bugs fell out of that too: the numeric endpoint attributes are hex without a prefix (wMaxPacketSize "0040" was read as 40, not 64), and bInterval was never read at all. Reliability: three places could freeze the whole process. The share path fed io.Pipe from the WebSocket read loop, so one slow USB transfer stalled every tunnel and the keepalives with them. The relay wrote to client sockets while holding the hub lock, so one peer that stopped reading blocked routing and registration for everyone. Control transfers ran inline in the protocol loop behind a 5s timeout. Also fixed: a use-after- free where a discarded URB's memory could be collected while the kernel still owned it, a reap loop that spun at 100% CPU on ioctl errors, a missing attach timeout, a double close(done) panic, and Hash[:8] in the relay's log line, which let a client with a short hash take the server down. Adds mode "both", so one client can offer and consume devices at once. The tunnel and client-left callbacks became multicast for it: as plain fields the second manager to register silently unhooked the first. Tunnel traffic is now AES-256-GCM end to end, on the relay path as well as directly. The key is derived from the three tokens, not from the group hash — the relay is told the hash, so a key derived from it would protect nothing from the one party in the middle. Group IDs are unchanged, so existing setups keep working; only clients configured without the tokens drop to unencrypted, relay-only operation. Peers now try to connect directly, with the relay supplying the public address neither side can determine for itself. Candidates are raced because an unreachable address hangs until timeout rather than refusing. Falling back to the relay is not an error. Platform reach: cross-compiled targets for ARM, MIPS and RISC-V (the Linux client needed no code changes — usbdevfs is not architecture specific), multi-arch Docker images, an Android bridge that accepts devices over SCM_RIGHTS because apps cannot open /dev/bus/usb, and macOS builds via system_profiler enumeration. Adds a Windows KMDF filter driver under driver/windows with its Go side. UNTESTED: it has never been compiled or run, needs the WDK to build and an EV certificate to distribute. Treat it as a starting point. Adds "usb-client diag": says per machine whether sharing and using are possible, what stands in the way, and what fixes it. Reports can be uploaded to a relay to get them off machines that are awkward to copy from. 96 tests, all green under -race. Builds for linux, windows and darwin on amd64 and arm64. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
93 lines
3.0 KiB
C
93 lines
3.0 KiB
C
/*
|
|
* usbshare - intercepting the class driver while the device is claimed
|
|
*
|
|
* This is what makes the filter approach worth the trouble. While no client
|
|
* holds the device, every request is forwarded untouched and the device
|
|
* behaves exactly as if this driver were not installed. Only once a client
|
|
* claims it do the class driver's requests get swallowed, so the two do not
|
|
* fight over the same endpoints.
|
|
*/
|
|
|
|
#include "usbshare.h"
|
|
|
|
/*
|
|
* Forwards a request to the driver below unchanged.
|
|
*
|
|
* Send-and-forget is right here: we have no interest in the answer, and not
|
|
* setting a completion routine avoids holding a reference on a request that
|
|
* may outlive our interest in it.
|
|
*/
|
|
static VOID
|
|
UsbShareForward(
|
|
_In_ WDFDEVICE Device,
|
|
_In_ WDFREQUEST Request
|
|
)
|
|
{
|
|
WDF_REQUEST_SEND_OPTIONS options;
|
|
|
|
WDF_REQUEST_SEND_OPTIONS_INIT(&options, WDF_REQUEST_SEND_OPTION_SEND_AND_FORGET);
|
|
WdfRequestFormatRequestUsingCurrentType(Request);
|
|
|
|
if (!WdfRequestSend(Request, WdfDeviceGetIoTarget(Device), &options)) {
|
|
WdfRequestComplete(Request, WdfRequestGetStatus(Request));
|
|
}
|
|
}
|
|
|
|
VOID
|
|
UsbShareEvtIoDefault(
|
|
_In_ WDFQUEUE Queue,
|
|
_In_ WDFREQUEST Request
|
|
)
|
|
{
|
|
WDFDEVICE device = WdfIoQueueGetDevice(Queue);
|
|
|
|
/*
|
|
* Reads and writes are not intercepted even while claimed. They come from
|
|
* user mode against the class driver's own interface, and failing them
|
|
* would surface as application errors rather than a device that is simply
|
|
* busy elsewhere.
|
|
*/
|
|
UsbShareForward(device, Request);
|
|
}
|
|
|
|
VOID
|
|
UsbShareEvtIoInternalDeviceControl(
|
|
_In_ WDFQUEUE Queue,
|
|
_In_ WDFREQUEST Request,
|
|
_In_ size_t OutputBufferLength,
|
|
_In_ size_t InputBufferLength,
|
|
_In_ ULONG IoControlCode
|
|
)
|
|
{
|
|
WDFDEVICE device = WdfIoQueueGetDevice(Queue);
|
|
PDEVICE_CONTEXT context = GetDeviceContext(device);
|
|
|
|
UNREFERENCED_PARAMETER(OutputBufferLength);
|
|
UNREFERENCED_PARAMETER(InputBufferLength);
|
|
|
|
/*
|
|
* IOCTL_INTERNAL_USB_SUBMIT_URB is how the class driver above us talks to
|
|
* the USB stack. Letting those through while a client holds the device
|
|
* would mean two parties submitting to the same endpoints: transfers
|
|
* would be answered to whoever asked last, and a keyboard would appear to
|
|
* type on both machines at once.
|
|
*/
|
|
if (IoControlCode == IOCTL_INTERNAL_USB_SUBMIT_URB && UsbShareIsClaimed(context)) {
|
|
/*
|
|
* STATUS_DEVICE_NOT_CONNECTED rather than STATUS_DEVICE_BUSY: class
|
|
* drivers treat "busy" as a reason to retry in a tight loop, whereas
|
|
* "not connected" makes them stand down until PnP says otherwise —
|
|
* which is exactly the state the device is in from their point of view.
|
|
*/
|
|
WdfRequestComplete(Request, STATUS_DEVICE_NOT_CONNECTED);
|
|
return;
|
|
}
|
|
|
|
/*
|
|
* Everything else — PnP queries, port status, idle notifications — is
|
|
* forwarded even while claimed. Blocking those would confuse the stack
|
|
* about the device's existence, and it does still exist.
|
|
*/
|
|
UsbShareForward(device, Request);
|
|
}
|