Commit graph

171 commits

Author SHA1 Message Date
e4cdc4beec Update .gitignore, remove obsolete plan doc 2026-03-25 00:30:38 +00:00
5120b1a3b9 fix: improve reliability with typed error handling and poison recovery
- Replace string-based USB error classification with ErrorKind matching:
  nusb TransferError is now preserved through io::Error instead of being
  destroyed by format!(). Stall→ConnectionReset→EPIPE, Cancelled→
  Interrupted→ENOENT, Disconnected→ConnectionAborted→ESHUTDOWN.
- Replace fragile string matching in interrupt IN retry loop with direct
  TransferError::Cancelled pattern match.
- Replace 21 production Mutex::lock().unwrap() calls with
  .unwrap_or_else(|e| e.into_inner()) to recover from mutex poisoning
  instead of cascading panics across the server.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-25 00:21:10 +00:00
804a4910a0 fix: USB spec conformance and reliability improvements
Spec conformance:
- Add missing standard control requests for simulated devices:
  GetConfiguration, GetStatus (device/interface/endpoint),
  ClearFeature, SetFeature, SetAddress
- Replace debug_assert with truncate for path/bus_id wire format
  to prevent protocol desync in release builds

Reliability:
- server() now returns Result instead of panicking on bind failure

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-25 00:12:36 +00:00
3f4e8effce fix: security hardening and data integrity for untrusted USB/IP clients
Critical fixes:
- Validate endpoint number is 0-15 (kernel parity: stub_rx.c)
- Cap in-flight URBs at 256 to prevent DoS resource exhaustion
- Replace expect() with graceful handling on lock contention in find_ep
- Use validated transfer_buffer_length for ISO allocation instead of
  unchecked multiplication of client-supplied values

High-priority fixes:
- Validate devid matches imported device in CMD_SUBMIT and CMD_UNLINK
- Fix string descriptor bLength u8 overflow for long strings (>126 chars)
- Use saturating_add for ISO actual_length sum, capped at transfer_buffer_length
- Truncate IN response data exceeding transfer_buffer_length

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-25 00:09:20 +00:00
a3dccbca9d Update flake.nix to allow nix flake check torun tests 2026-03-25 00:07:13 +00:00
d81f5826dc feat: add fc-vsock transport for Firecracker/Cloud-Hypervisor VMs
Support connecting to guests via Firecracker/Cloud-Hypervisor's Unix
domain socket vsock proxy. The host opens the VMM's socket file,
performs a CONNECT/OK text handshake, then uses the stream for USB/IP.

New address format: fc-vsock:/path/to/socket:<port>

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 15:44:15 +00:00
4c368c02b5 feat: concurrent ISO pipelining via nusb update and &self handlers
Update nusb to c1380673 which allows multiple IsoEndpoint instances per
address, enabling concurrent URB submission from separate threads.

Change UsbInterfaceHandler trait methods from &mut self to &self and
replace Arc<Mutex<Box<dyn Handler>>> with Arc<dyn Handler>. This
removes the serialization bottleneck where the handler mutex was held
for the entire USB transfer duration, causing ISO audio to play at
~67% speed.

Handlers needing interior mutability (HID, CDC) now use Mutex on
individual fields. Passthrough handlers already used Arc<Mutex<>>
internally and need no changes.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 15:10:28 +00:00
9250c062aa fix: prevent idle disconnect by retrying interrupt IN and skipping
read timeout while URBs are in-flight

- Interrupt IN transfers now retry with 1s intervals (up to 5 min)
  instead of returning an error on timeout. Previously, nusb's 1s
  timeout caused a cancelled transfer error (-ENOENT) which told the
  kernel the endpoint was intentionally shut down, killing HID.
- Release the nusb Interface Mutex before blocking on interrupt and
  ISO transfers so other URBs on the same interface aren't starved.
- URB read timeout now skips when in-flight URBs exist (e.g. pending
  interrupt transfers), preventing false idle disconnects.
- Use appropriate timeouts per transfer type: interrupt=5min,
  isochronous=5s, control/bulk=1s.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 13:04:40 +00:00
b73b4b31f4 fix: ISO alt setting and actual_length for isochronous transfers
- Add set_alt_setting() to UsbInterfaceHandler trait so host passthrough
  handlers can update the physical USB interface's alternate setting via
  nusb::Interface::set_alt_setting() instead of raw control transfers.
  This allows nusb to find ISO endpoints after alt setting changes.
- Fix ISO OUT actual_length to equal the sum of per-packet actual_lengths
  instead of transfer_buffer_length. The kernel validates this invariant
  and disconnects the device on mismatch ("total length of iso packets
  not equal to actual length of buffer").

Enables successful USB Audio Class 2 isochronous playback through
host passthrough.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 12:45:55 +00:00
85146a12fe fix: passthrough descriptors, SET_INTERFACE forwarding, and error mapping
- Forward Configuration, BOS, and DeviceQualifier descriptors to the
  real device in passthrough mode, preserving IADs, class-specific
  descriptors (audio, HID, DFU), and endpoint companions
- Forward SET_INTERFACE to the physical device so alt settings are
  actually applied (critical for audio streaming)
- Round up interrupt IN buffers to max_packet_size (matching bulk IN)
- Map USB errors to proper Linux URB status codes: STALL→EPIPE(-32),
  cancelled→ENOENT(-2), timeout→ETIMEDOUT(-110)
- Propagate UrbResponse.status instead of hardcoding 0

Fixes USB Audio Class 2 device detection (kernel error "Audio class
v2/v3 interfaces need an interface association") and device reset
loops caused by EPROTO being returned for benign STALL/cancel errors.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 12:40:44 +00:00
02c3017679 Update README.md 2026-03-22 11:33:07 +00:00
63e9faf82d fix: align bulk IN buffers and forward unknown string descriptors
Two fixes for mass storage passthrough:

1. Bulk IN buffer allocation now rounds up to the endpoint's
   max_packet_size. nusb (and the USB spec) require IN transfers to be
   multiples of max_packet_size. Without this, SCSI INQUIRY (36 bytes on
   a 512-byte max_packet_size endpoint) was rejected, causing the kernel
   to repeatedly reset the device.

2. String descriptor requests for indices not in the local string pool
   are now forwarded to the device handler. This fixes interface and
   configuration string descriptors (e.g., iInterface=5) that exist on
   the real device but weren't populated in the synthetic string pool.

Tested: USB mass storage gadget via dummy_hcd successfully enumerates,
mounts, reads, and writes through the USBIP passthrough.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 11:13:45 +00:00
889017be3b fix: correct OUT actual_length, BCD encoding, and silent transfer failures
Three fixes for host device passthrough:

1. OUT transfers now report actual_length = transfer_buffer_length instead
   of 0. The kernel needs to know how many bytes were consumed; returning 0
   caused bulk writes (critical for mass storage) to appear as failures.

2. BCD version encoding (bcdUSB, bcdDevice) now properly reconstructs the
   full 2-byte BCD value from the Version struct's nibble fields. Previously,
   the minor field (a single nibble) was written as a full byte, corrupting
   values like USB 1.1 (0x0110 → 0x0101).

3. USB transfer handlers (nusb) now propagate errors instead of silently
   returning empty success responses. Failed control_in, bulk, and interrupt
   transfers were falling through to Ok(UrbResponse::default()), making the
   kernel think transfers succeeded with 0 bytes of data.

Also sets usb_version and dev_num from the actual device in build_usb_device.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 11:05:53 +00:00
f7236deba9 feat: add isochronous transfer support and fix host passthrough
Add IsoPacketDescriptor, UrbRequest, and UrbResponse types to the
protocol layer. Rewrite handle_urb_loop to a concurrent architecture
with pipelining for improved throughput. Replace interfaces vec with
InterfaceState to track alternate settings.

Implement isochronous transfer support in the nusb host handler with
structured ISO packet descriptor parsing and serialization. Switch to
ISO-capable nusb fork. Add IsoLoopbackHandler test fixture and ISO
transfer tests.

Fix host device passthrough: detach kernel drivers before claiming
interfaces, use real EP0 max packet size, forward SET_CONFIGURATION
to device, map nusb Speed enum to Linux kernel values, and use
extend_from_slice for OUT transfer buffers.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 10:42:08 +00:00
09b8012f8c feat: add TCP transport support
Add TCP address types, parsing, and connect/listen functions alongside
the existing vsock transport. Update all CLI commands (client listen,
host connect, test_hid connect) to support both vsock: and tcp:
address prefixes. Add hostname resolution for TCP connections.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 10:41:59 +00:00
b66cd0f7e9 fix: harden protocol handling against panics and malicious input
Replace all unimplemented!() panics with proper error returns across
URB, HID, and host handlers. Replace assert!() panics with error
returns in response serialization and string pool. Validate direction
field at runtime instead of only in debug builds.

Add allocation bounds checks to read_from_socket, read timeout to
URB loop, and clamp transfer_buffer_length to u16::MAX for control
transfers. Use correct USB/IP error status codes in submit failure
responses. Propagate control OUT transfer errors instead of swallowing
them. Fix compilation errors and doc comment copy-paste issues.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 10:41:54 +00:00
30d3c9532e feat: add usbip-rs CLI tool with vsock transport
Convert to cargo workspace with lib/ and cli/ crates. Add Nix flake
for building and development. Extract handle_urb_loop and add
read_urb_command to the library for CLI consumption.

Implement the usbip-rs CLI binary with clap subcommands:
- client listen: accept incoming connections via vhci_hcd sysfs
- host connect: passthrough real USB devices via nusb
- test_hid connect: export a simulated HID keyboard for testing

Add vsock transport layer and vhci_hcd sysfs interaction module.
Apply rustfmt formatting project-wide and add rustfmt/clippy to devShell.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-22 10:41:42 +00:00
Jiajie Chen
0878920532 Release v0.8.0 2026-01-27 13:00:46 +08:00
Jiajie Chen
a19bfe8301 Simplify documentation (fixes #59)
Remove redundant sections from README and update LICENSE copyright year.

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2025-12-25 20:58:48 +08:00
Jiajie Chen
c8cd214e1d Fix clippy warning: cloned_ref_to_slice_refs
Replace &[device.clone()] with std::slice::from_ref(&device) in
test to address clippy::cloned_ref_to_slice_refs warning.

Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
2025-12-25 20:52:45 +08:00
Jiajie Chen
48d0d9ca34 Fix bus num on non-Linux platform 2025-12-24 23:23:25 +08:00
Jiajie Chen
b83f584391 Handle upper case in hid keyboard report 2025-12-24 23:18:11 +08:00
Jiajie Chen
fdbb9574aa Fix missing max packet size handling 2025-12-24 23:17:29 +08:00
Jiajie Chen
3ff5df5c2e Migrate to nusb 0.2.1 2025-12-24 23:17:02 +08:00
Jiajie Chen
156ec45a81 Apply more cargo clippy 2025-08-01 16:38:47 +08:00
Jiajie Chen
e14858f918 Apply cargo clippy 2025-08-01 16:35:06 +08:00
Jiajie Chen
ad1537a0f8 Add more comments in descriptor handling 2025-08-01 16:05:33 +08:00
Jiajie Chen
664d9ee143 Run cargo fmt 2025-06-13 18:26:58 +08:00
Jiajie Chen
5d49acaa2e Run clippy fix 2025-06-13 17:57:50 +08:00
Jiajie Chen
a584b3713d Add stale bot 2025-06-13 17:17:59 +08:00
Jiajie Chen
c4f7a376b5 Drop broken coveralls 2025-03-24 14:15:43 +08:00
Jiajie Chen
276acbbb23 Format code 2025-03-24 14:10:35 +08:00
Jiajie Chen
bd7c558ee1 Fix clippy 2025-03-24 14:03:53 +08:00
Jiajie Chen
c622dd9763 Format code 2025-03-24 13:50:04 +08:00
Jiajie Chen
6fd28e9076 Bump dependencies 2025-03-24 13:47:39 +08:00
Jiajie Chen
22acc5b7e0 #[cfg] is required 2024-10-26 10:55:41 +08:00
Jiajie Chen
d03e34d9e4 Handle windows missing function for nusb 2024-10-26 10:53:14 +08:00
Jiajie Chen
5c00f965f9 Use nightly for coveralls 2024-10-26 10:49:05 +08:00
Jiajie Chen
0c00370aeb Improve doc 2024-10-26 10:48:13 +08:00
Jiajie Chen
94d674942f Add derive Debug, closing #17 2024-10-26 10:46:14 +08:00
Jiajie Chen
066aeb15dc Use stable in ci 2024-10-26 10:40:24 +08:00
Jiajie Chen
0e896b2cf7 Expose nusb support 2024-10-26 10:38:09 +08:00
Jiajie Chen
9c1efa1dd3 Add initial nusb support for #54 2024-10-26 10:16:04 +08:00
Jiajie Chen
af17fc2243 Fix clippy warnings 2024-10-26 09:42:51 +08:00
Jiajie Chen
8f3a72730b Expose new with raw device handles, closing pr #48 2024-10-26 09:42:44 +08:00
Jiajie Chen
7e3409d6d9 Add unset string fuctions 2024-10-26 09:35:32 +08:00
roblabla
458cc084c7 Allow not setting a string descriptor 2024-10-23 18:48:24 +02:00
Jiajie Chen
341bd0fe5e Release v0.7.1 2024-01-14 10:05:23 +08:00
Chen
0b7c03acf8
Merge pull request #42 from h7x4/fix-string-pool-panic
Fix panic on request of invalid string pool index
2024-01-14 10:03:55 +08:00
Chen
28f4f7a85e
Merge pull request #43 from h7x4/fix-usbip-cmd-unlink-behaviour
Fix `USBIP_CMD_UNLINK` behaviour
2024-01-14 10:03:43 +08:00