VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/VBoxUSB/win/mon/VBoxUsbFlt.cpp@ 38488

Last change on this file since 38488 was 38488, checked in by vboxsync, 13 years ago

usb/win: usbhub filter drivers support (more correct dealing with usbhub device & driver objects)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 53.1 KB
Line 
1/* $Id: VBoxUsbFlt.cpp 38488 2011-08-18 10:26:06Z vboxsync $ */
2/** @file
3 * VBox USB Monitor Device Filtering functionality
4 */
5/*
6 * Copyright (C) 2011 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 */
16
17
18/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21#include "VBoxUsbMon.h"
22#include "../cmn/VBoxUsbTool.h"
23
24#include <VBox/cdefs.h>
25#include <VBox/types.h>
26#include <iprt/process.h>
27#include <iprt/assert.h>
28#include <VBox/err.h>
29//#include <VBox/sup.h>
30
31#include <iprt/assert.h>
32#include <stdio.h>
33
34#pragma warning(disable : 4200)
35#include "usbdi.h"
36#pragma warning(default : 4200)
37#include "usbdlib.h"
38#include "VBoxUSBFilterMgr.h"
39#include <VBox/usblib.h>
40#include <devguid.h>
41
42/*
43 * Note: Must match the VID & PID in the USB driver .inf file!!
44 */
45/*
46 BusQueryDeviceID USB\Vid_80EE&Pid_CAFE
47 BusQueryInstanceID 2
48 BusQueryHardwareIDs USB\Vid_80EE&Pid_CAFE&Rev_0100
49 BusQueryHardwareIDs USB\Vid_80EE&Pid_CAFE
50 BusQueryCompatibleIDs USB\Class_ff&SubClass_00&Prot_00
51 BusQueryCompatibleIDs USB\Class_ff&SubClass_00
52 BusQueryCompatibleIDs USB\Class_ff
53*/
54
55#define szBusQueryDeviceId L"USB\\Vid_80EE&Pid_CAFE"
56#define szBusQueryHardwareIDs L"USB\\Vid_80EE&Pid_CAFE&Rev_0100\0USB\\Vid_80EE&Pid_CAFE\0\0"
57#define szBusQueryCompatibleIDs L"USB\\Class_ff&SubClass_00&Prot_00\0USB\\Class_ff&SubClass_00\0USB\\Class_ff\0\0"
58
59#define szDeviceTextDescription L"VirtualBox USB"
60
61/* Possible USB bus driver names. */
62static LPWSTR lpszStandardControllerName[1] =
63{
64 L"\\Driver\\usbhub",
65};
66
67/*
68 * state transitions:
69 *
70 * (we are not filtering this device )
71 * ADDED --> UNCAPTURED ------------------------------->-
72 * | |
73 * | (we are filtering this device, | (the device is being
74 * | waiting for our device driver | re-plugged to perform
75 * | to pick it up) | capture-uncapture transition)
76 * |-> CAPTURING -------------------------------->|---> REPLUGGING -----
77 * ^ | (device driver picked | |
78 * | | up the device) | (remove cased | (device is removed
79 * | ->---> CAPTURED ---------------------->| by "real" removal | the device info is removed form the list)
80 * | | |------------------->->--> REMOVED
81 * | | |
82 * |-----------<->---> USED_BY_GUEST ------->|
83 * | |
84 * |------------------------<-
85 *
86 * NOTE: the order of enums DOES MATTER!!
87 * Do not blindly modify!! as the code assumes the state is ordered this way.
88 */
89typedef enum
90{
91 VBOXUSBFLT_DEVSTATE_UNKNOWN = 0,
92 VBOXUSBFLT_DEVSTATE_REMOVED,
93 VBOXUSBFLT_DEVSTATE_REPLUGGING,
94 VBOXUSBFLT_DEVSTATE_ADDED,
95 VBOXUSBFLT_DEVSTATE_UNCAPTURED,
96 VBOXUSBFLT_DEVSTATE_CAPTURING,
97 VBOXUSBFLT_DEVSTATE_CAPTURED,
98 VBOXUSBFLT_DEVSTATE_USED_BY_GUEST,
99 VBOXUSBFLT_DEVSTATE_32BIT_HACK = 0x7fffffff
100} VBOXUSBFLT_DEVSTATE;
101
102typedef struct VBOXUSBFLT_DEVICE
103{
104 LIST_ENTRY GlobalLe;
105 /* auxiliary list to be used for gathering devices to be re-plugged
106 * only thread that puts the device to the REPLUGGING state can use this list */
107 LIST_ENTRY RepluggingLe;
108 /* Owning session. Each matched device has an owning session. */
109 struct VBOXUSBFLTCTX *pOwner;
110 /* filter id - if NULL AND device has an owner - the filter is destroyed */
111 uintptr_t uFltId;
112 /* true iff device is filtered with a one-shot filter */
113 bool fIsFilterOneShot;
114 /* The device state. If the non-owner session is requesting the state while the device is grabbed,
115 * the USBDEVICESTATE_USED_BY_HOST is returned. */
116 VBOXUSBFLT_DEVSTATE enmState;
117 volatile uint32_t cRefs;
118 PDEVICE_OBJECT Pdo;
119 uint16_t idVendor;
120 uint16_t idProduct;
121 uint16_t bcdDevice;
122 uint8_t bClass;
123 uint8_t bSubClass;
124 uint8_t bProtocol;
125 char szSerial[MAX_USB_SERIAL_STRING];
126 char szMfgName[MAX_USB_SERIAL_STRING];
127 char szProduct[MAX_USB_SERIAL_STRING];
128#if 0
129 char szDrvKeyName[512];
130 BOOLEAN fHighSpeed;
131#endif
132} VBOXUSBFLT_DEVICE, *PVBOXUSBFLT_DEVICE;
133
134#define PVBOXUSBFLT_DEVICE_FROM_LE(_pLe) ( (PVBOXUSBFLT_DEVICE)( ((uint8_t*)(_pLe)) - RT_OFFSETOF(VBOXUSBFLT_DEVICE, GlobalLe) ) )
135#define PVBOXUSBFLT_DEVICE_FROM_REPLUGGINGLE(_pLe) ( (PVBOXUSBFLT_DEVICE)( ((uint8_t*)(_pLe)) - RT_OFFSETOF(VBOXUSBFLT_DEVICE, RepluggingLe) ) )
136#define PVBOXUSBFLTCTX_FROM_LE(_pLe) ( (PVBOXUSBFLTCTX)( ((uint8_t*)(_pLe)) - RT_OFFSETOF(VBOXUSBFLTCTX, ListEntry) ) )
137
138typedef struct VBOXUSBFLT_LOCK
139{
140 KSPIN_LOCK Lock;
141 KIRQL OldIrql;
142} VBOXUSBFLT_LOCK, *PVBOXUSBFLT_LOCK;
143
144#define VBOXUSBFLT_LOCK_INIT() \
145 KeInitializeSpinLock(&g_VBoxUsbFltGlobals.Lock.Lock)
146#define VBOXUSBFLT_LOCK_TERM() do { } while (0)
147#define VBOXUSBFLT_LOCK_ACQUIRE() \
148 KeAcquireSpinLock(&g_VBoxUsbFltGlobals.Lock.Lock, &g_VBoxUsbFltGlobals.Lock.OldIrql);
149#define VBOXUSBFLT_LOCK_RELEASE() \
150 KeReleaseSpinLock(&g_VBoxUsbFltGlobals.Lock.Lock, g_VBoxUsbFltGlobals.Lock.OldIrql);
151
152
153typedef struct VBOXUSBFLT_BLDEV
154{
155 LIST_ENTRY ListEntry;
156 uint16_t idVendor;
157 uint16_t idProduct;
158 uint16_t bcdDevice;
159} VBOXUSBFLT_BLDEV, *PVBOXUSBFLT_BLDEV;
160
161#define PVBOXUSBFLT_BLDEV_FROM_LE(_pLe) ( (PVBOXUSBFLT_BLDEV)( ((uint8_t*)(_pLe)) - RT_OFFSETOF(VBOXUSBFLT_BLDEV, ListEntry) ) )
162
163typedef struct VBOXUSBFLTGLOBALS
164{
165 LIST_ENTRY DeviceList;
166 LIST_ENTRY ContextList;
167 /* devices known to misbehave */
168 LIST_ENTRY BlackDeviceList;
169 VBOXUSBFLT_LOCK Lock;
170} VBOXUSBFLTGLOBALS, *PVBOXUSBFLTGLOBALS;
171static VBOXUSBFLTGLOBALS g_VBoxUsbFltGlobals;
172
173static bool vboxUsbFltBlDevMatchLocked(uint16_t idVendor, uint16_t idProduct, uint16_t bcdDevice)
174{
175 for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.BlackDeviceList.Flink;
176 pEntry != &g_VBoxUsbFltGlobals.BlackDeviceList;
177 pEntry = pEntry->Flink)
178 {
179 PVBOXUSBFLT_BLDEV pDev = PVBOXUSBFLT_BLDEV_FROM_LE(pEntry);
180 if (pDev->idVendor != idVendor)
181 continue;
182 if (pDev->idProduct != idProduct)
183 continue;
184 if (pDev->bcdDevice != bcdDevice)
185 continue;
186
187 return true;
188 }
189 return false;
190}
191
192static NTSTATUS vboxUsbFltBlDevAddLocked(uint16_t idVendor, uint16_t idProduct, uint16_t bcdDevice)
193{
194 if (vboxUsbFltBlDevMatchLocked(idVendor, idProduct, bcdDevice))
195 return STATUS_SUCCESS;
196 PVBOXUSBFLT_BLDEV pDev = (PVBOXUSBFLT_BLDEV)VBoxUsbMonMemAllocZ(sizeof (*pDev));
197 if (!pDev)
198 {
199 AssertFailed();
200 return STATUS_INSUFFICIENT_RESOURCES;
201 }
202
203 pDev->idVendor = idVendor;
204 pDev->idProduct = idProduct;
205 pDev->bcdDevice = bcdDevice;
206 InsertHeadList(&g_VBoxUsbFltGlobals.BlackDeviceList, &pDev->ListEntry);
207 return STATUS_SUCCESS;
208}
209
210static void vboxUsbFltBlDevClearLocked()
211{
212 PLIST_ENTRY pNext;
213 for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.BlackDeviceList.Flink;
214 pEntry != &g_VBoxUsbFltGlobals.BlackDeviceList;
215 pEntry = pNext)
216 {
217 pNext = pEntry->Flink;
218 VBoxUsbMonMemFree(pEntry);
219 }
220}
221
222static void vboxUsbFltBlDevPopulateWithKnownLocked()
223{
224 /* this one halts when trying to get string descriptors from it */
225 vboxUsbFltBlDevAddLocked(0x5ac, 0x921c, 0x115);
226}
227
228
229DECLINLINE(void) vboxUsbFltDevRetain(PVBOXUSBFLT_DEVICE pDevice)
230{
231 Assert(pDevice->cRefs);
232 ASMAtomicIncU32(&pDevice->cRefs);
233}
234
235static void vboxUsbFltDevDestroy(PVBOXUSBFLT_DEVICE pDevice)
236{
237 Assert(!pDevice->cRefs);
238 Assert(pDevice->enmState == VBOXUSBFLT_DEVSTATE_REMOVED);
239 VBoxUsbMonMemFree(pDevice);
240}
241
242DECLINLINE(void) vboxUsbFltDevRelease(PVBOXUSBFLT_DEVICE pDevice)
243{
244 uint32_t cRefs = ASMAtomicDecU32(&pDevice->cRefs);
245 Assert(cRefs < UINT32_MAX/2);
246 if (!cRefs)
247 {
248 vboxUsbFltDevDestroy(pDevice);
249 }
250}
251
252static void vboxUsbFltDevOwnerSetLocked(PVBOXUSBFLT_DEVICE pDevice, PVBOXUSBFLTCTX pContext, uintptr_t uFltId, bool fIsOneShot)
253{
254 ASSERT_WARN(!pDevice->pOwner, ("device 0x%p has an owner(0x%p)", pDevice, pDevice->pOwner));
255 ++pContext->cActiveFilters;
256 pDevice->pOwner = pContext;
257 pDevice->uFltId = uFltId;
258 pDevice->fIsFilterOneShot = fIsOneShot;
259}
260
261static void vboxUsbFltDevOwnerClearLocked(PVBOXUSBFLT_DEVICE pDevice)
262{
263 ASSERT_WARN(pDevice->pOwner, ("no owner for device 0x%p", pDevice));
264 --pDevice->pOwner->cActiveFilters;
265 ASSERT_WARN(pDevice->pOwner->cActiveFilters < UINT32_MAX/2, ("cActiveFilters (%d)", pDevice->pOwner->cActiveFilters));
266 pDevice->pOwner = NULL;
267 pDevice->uFltId = 0;
268}
269
270static void vboxUsbFltDevOwnerUpdateLocked(PVBOXUSBFLT_DEVICE pDevice, PVBOXUSBFLTCTX pContext, uintptr_t uFltId, bool fIsOneShot)
271{
272 if (pDevice->pOwner != pContext)
273 {
274 if (pDevice->pOwner)
275 vboxUsbFltDevOwnerClearLocked(pDevice);
276 if (pContext)
277 vboxUsbFltDevOwnerSetLocked(pDevice, pContext, uFltId, fIsOneShot);
278 }
279 else if (pContext)
280 {
281 pDevice->uFltId = uFltId;
282 pDevice->fIsFilterOneShot = fIsOneShot;
283 }
284}
285
286static PVBOXUSBFLT_DEVICE vboxUsbFltDevGetLocked(PDEVICE_OBJECT pPdo)
287{
288#ifdef VBOX_USB_WITH_VERBOSE_LOGGING
289 for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
290 pEntry != &g_VBoxUsbFltGlobals.DeviceList;
291 pEntry = pEntry->Flink)
292 {
293 PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
294 for (PLIST_ENTRY pEntry2 = pEntry->Flink;
295 pEntry2 != &g_VBoxUsbFltGlobals.DeviceList;
296 pEntry2 = pEntry2->Flink)
297 {
298 PVBOXUSBFLT_DEVICE pDevice2 = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry2);
299 ASSERT_WARN( pDevice->idVendor != pDevice2->idVendor
300 || pDevice->idProduct != pDevice2->idProduct
301 || pDevice->bcdDevice != pDevice2->bcdDevice, ("duplicate devices in a list!!"));
302 }
303 }
304#endif
305 for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
306 pEntry != &g_VBoxUsbFltGlobals.DeviceList;
307 pEntry = pEntry->Flink)
308 {
309 PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
310 ASSERT_WARN( pDevice->enmState == VBOXUSBFLT_DEVSTATE_REPLUGGING
311 || pDevice->enmState == VBOXUSBFLT_DEVSTATE_UNCAPTURED
312 || pDevice->enmState == VBOXUSBFLT_DEVSTATE_CAPTURING
313 || pDevice->enmState == VBOXUSBFLT_DEVSTATE_CAPTURED
314 || pDevice->enmState == VBOXUSBFLT_DEVSTATE_USED_BY_GUEST,
315 ("Invalid device state(%d) for device(0x%p) PDO(0x%p)", pDevice->enmState, pDevice, pDevice->Pdo));
316 if (pDevice->Pdo == pPdo)
317 return pDevice;
318 }
319 return NULL;
320}
321
322PVBOXUSBFLT_DEVICE vboxUsbFltDevGet(PDEVICE_OBJECT pPdo)
323{
324 PVBOXUSBFLT_DEVICE pDevice;
325
326 VBOXUSBFLT_LOCK_ACQUIRE();
327 pDevice = vboxUsbFltDevGetLocked(pPdo);
328 if (pDevice->enmState > VBOXUSBFLT_DEVSTATE_ADDED)
329 {
330 vboxUsbFltDevRetain(pDevice);
331 LOG(("found device (0x%p), state(%d) for PDO(0x%p)", pDevice, pDevice->enmState, pPdo));
332 }
333 else
334 {
335 LOG(("found replugging device (0x%p), state(%d) for PDO(0x%p)", pDevice, pDevice->enmState, pPdo));
336 pDevice = NULL;
337 }
338 VBOXUSBFLT_LOCK_RELEASE();
339
340 return pDevice;
341}
342
343static NTSTATUS vboxUsbFltPdoReplug(PDEVICE_OBJECT pDo)
344{
345 LOG(("Replugging PDO(0x%p)", pDo));
346 NTSTATUS Status = VBoxUsbToolIoInternalCtlSendSync(pDo, IOCTL_INTERNAL_USB_CYCLE_PORT, NULL, NULL);
347 ASSERT_WARN(Status == STATUS_SUCCESS, ("replugging PDO(0x%p) failed Status(0x%x)", pDo, Status));
348 LOG(("Replugging PDO(0x%p) done with Status(0x%x)", pDo, Status));
349 return Status;
350}
351
352static PVBOXUSBFLTCTX vboxUsbFltDevMatchLocked(PVBOXUSBFLT_DEVICE pDevice, uintptr_t *puId, bool fRemoveFltIfOneShot, bool *pfFilter, bool *pfIsOneShot)
353{
354 USBFILTER DevFlt;
355 USBFilterInit(&DevFlt, USBFILTERTYPE_CAPTURE);
356 USBFilterSetNumExact(&DevFlt, USBFILTERIDX_VENDOR_ID, pDevice->idVendor, true);
357 USBFilterSetNumExact(&DevFlt, USBFILTERIDX_PRODUCT_ID, pDevice->idProduct, true);
358 USBFilterSetNumExact(&DevFlt, USBFILTERIDX_DEVICE_REV, pDevice->bcdDevice, true);
359 USBFilterSetNumExact(&DevFlt, USBFILTERIDX_DEVICE_CLASS, pDevice->bClass, true);
360 USBFilterSetNumExact(&DevFlt, USBFILTERIDX_DEVICE_SUB_CLASS, pDevice->bSubClass, true);
361 USBFilterSetNumExact(&DevFlt, USBFILTERIDX_DEVICE_PROTOCOL, pDevice->bProtocol, true);
362 USBFilterSetStringExact(&DevFlt, USBFILTERIDX_MANUFACTURER_STR, pDevice->szMfgName, true);
363 USBFilterSetStringExact(&DevFlt, USBFILTERIDX_PRODUCT_STR, pDevice->szProduct, true);
364 USBFilterSetStringExact(&DevFlt, USBFILTERIDX_SERIAL_NUMBER_STR, pDevice->szSerial, true);
365
366 /* Run filters on the thing. */
367 *puId = 0;
368 *pfFilter = false;
369 *pfIsOneShot = false;
370 PVBOXUSBFLTCTX pOwner = VBoxUSBFilterMatchEx(&DevFlt, puId, fRemoveFltIfOneShot, pfFilter, pfIsOneShot);
371 USBFilterDelete(&DevFlt);
372 return pOwner;
373}
374
375static void vboxUsbFltDevStateMarkReplugLocked(PVBOXUSBFLT_DEVICE pDevice)
376{
377 vboxUsbFltDevOwnerUpdateLocked(pDevice, NULL, 0, false);
378 pDevice->enmState = VBOXUSBFLT_DEVSTATE_REPLUGGING;
379}
380
381static bool vboxUsbFltDevStateIsNotFiltered(PVBOXUSBFLT_DEVICE pDevice)
382{
383 return pDevice->enmState == VBOXUSBFLT_DEVSTATE_UNCAPTURED;
384}
385
386static bool vboxUsbFltDevStateIsFiltered(PVBOXUSBFLT_DEVICE pDevice)
387{
388 return pDevice->enmState >= VBOXUSBFLT_DEVSTATE_CAPTURING;
389}
390
391#define VBOXUSBMON_POPULATE_REQUEST_TIMEOUT_MS 10000
392
393static NTSTATUS vboxUsbFltDevPopulate(PVBOXUSBFLT_DEVICE pDevice, PDEVICE_OBJECT pDo /*, BOOLEAN bPopulateNonFilterProps*/)
394{
395 NTSTATUS Status;
396 PUSB_DEVICE_DESCRIPTOR pDevDr = 0;
397
398 pDevice->Pdo = pDo;
399
400 LOG(("Populating Device(0x%p) for PDO(0x%p)", pDevice, pDo));
401
402 pDevDr = (PUSB_DEVICE_DESCRIPTOR)VBoxUsbMonMemAllocZ(sizeof(*pDevDr));
403 if (pDevDr == NULL)
404 {
405 WARN(("Failed to alloc mem for urb"));
406 return STATUS_INSUFFICIENT_RESOURCES;
407 }
408
409 do
410 {
411 Status = VBoxUsbToolGetDescriptor(pDo, pDevDr, sizeof(*pDevDr), USB_DEVICE_DESCRIPTOR_TYPE, 0, 0, VBOXUSBMON_POPULATE_REQUEST_TIMEOUT_MS);
412 if (!NT_SUCCESS(Status))
413 {
414 WARN(("getting device descriptor failed, Status (0x%x)", Status));
415 break;
416 }
417
418 if (vboxUsbFltBlDevMatchLocked(pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice))
419 {
420 WARN(("found a known black list device, vid(0x%x), pid(0x%x), rev(0x%x)", pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice));
421 Status = STATUS_UNSUCCESSFUL;
422 break;
423 }
424
425 LOG(("Device pid=%x vid=%x rev=%x", pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice));
426 pDevice->idVendor = pDevDr->idVendor;
427 pDevice->idProduct = pDevDr->idProduct;
428 pDevice->bcdDevice = pDevDr->bcdDevice;
429 pDevice->bClass = pDevDr->bDeviceClass;
430 pDevice->bSubClass = pDevDr->bDeviceSubClass;
431 pDevice->bProtocol = pDevDr->bDeviceProtocol;
432 pDevice->szSerial[0] = 0;
433 pDevice->szMfgName[0] = 0;
434 pDevice->szProduct[0] = 0;
435
436 /* If there are no strings, don't even try to get any string descriptors. */
437 if (pDevDr->iSerialNumber || pDevDr->iManufacturer || pDevDr->iProduct)
438 {
439 int langId;
440
441 Status = VBoxUsbToolGetLangID(pDo, &langId, VBOXUSBMON_POPULATE_REQUEST_TIMEOUT_MS);
442 if (!NT_SUCCESS(Status))
443 {
444 WARN(("reading language ID failed"));
445 if (Status == STATUS_CANCELLED)
446 {
447 WARN(("found a new black list device, vid(0x%x), pid(0x%x), rev(0x%x)", pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice));
448 vboxUsbFltBlDevAddLocked(pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice);
449 Status = STATUS_UNSUCCESSFUL;
450 }
451 break;
452 }
453
454 if (pDevDr->iSerialNumber)
455 {
456 Status = VBoxUsbToolGetStringDescriptorA(pDo, pDevice->szSerial, sizeof (pDevice->szSerial), pDevDr->iSerialNumber, langId, VBOXUSBMON_POPULATE_REQUEST_TIMEOUT_MS);
457 if (!NT_SUCCESS(Status))
458 {
459 WARN(("reading serial number failed"));
460 ASSERT_WARN(pDevice->szSerial[0] == '\0', ("serial is not zero!!"));
461 if (Status == STATUS_CANCELLED)
462 {
463 WARN(("found a new black list device, vid(0x%x), pid(0x%x), rev(0x%x)", pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice));
464 vboxUsbFltBlDevAddLocked(pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice);
465 Status = STATUS_UNSUCCESSFUL;
466 break;
467 }
468 LOG(("pretending success.."));
469 Status = STATUS_SUCCESS;
470 }
471 }
472
473 if (pDevDr->iManufacturer)
474 {
475 Status = VBoxUsbToolGetStringDescriptorA(pDo, pDevice->szMfgName, sizeof (pDevice->szMfgName), pDevDr->iManufacturer, langId, VBOXUSBMON_POPULATE_REQUEST_TIMEOUT_MS);
476 if (!NT_SUCCESS(Status))
477 {
478 WARN(("reading manufacturer name failed"));
479 ASSERT_WARN(pDevice->szMfgName[0] == '\0', ("szMfgName is not zero!!"));
480 if (Status == STATUS_CANCELLED)
481 {
482 WARN(("found a new black list device, vid(0x%x), pid(0x%x), rev(0x%x)", pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice));
483 vboxUsbFltBlDevAddLocked(pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice);
484 Status = STATUS_UNSUCCESSFUL;
485 break;
486 }
487 LOG(("pretending success.."));
488 Status = STATUS_SUCCESS;
489 }
490 }
491
492 if (pDevDr->iProduct)
493 {
494 Status = VBoxUsbToolGetStringDescriptorA(pDo, pDevice->szProduct, sizeof (pDevice->szProduct), pDevDr->iProduct, langId, VBOXUSBMON_POPULATE_REQUEST_TIMEOUT_MS);
495 if (!NT_SUCCESS(Status))
496 {
497 WARN(("reading product name failed"));
498 ASSERT_WARN(pDevice->szProduct[0] == '\0', ("szProduct is not zero!!"));
499 if (Status == STATUS_CANCELLED)
500 {
501 WARN(("found a new black list device, vid(0x%x), pid(0x%x), rev(0x%x)", pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice));
502 vboxUsbFltBlDevAddLocked(pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice);
503 Status = STATUS_UNSUCCESSFUL;
504 break;
505 }
506 LOG(("pretending success.."));
507 Status = STATUS_SUCCESS;
508 }
509 }
510
511#if 0
512 if (bPopulateNonFilterProps)
513 {
514 WCHAR RegKeyBuf[512];
515 ULONG cbRegKeyBuf = sizeof (RegKeyBuf);
516 Status = IoGetDeviceProperty(pDo,
517 DevicePropertyDriverKeyName,
518 cbRegKeyBuf,
519 RegKeyBuf,
520 &cbRegKeyBuf);
521 if (!NT_SUCCESS(Status))
522 {
523 AssertMsgFailed((__FUNCTION__": IoGetDeviceProperty failed Status (0x%x)", Status));
524 break;
525 }
526
527 ANSI_STRING Ansi;
528 UNICODE_STRING Unicode;
529 Ansi.Buffer = pDevice->szDrvKeyName;
530 Ansi.Length = 0;
531 Ansi.MaximumLength = sizeof(pDevice->szDrvKeyName);
532 RtlInitUnicodeString(&Unicode, RegKeyBuf);
533
534 Status = RtlUnicodeStringToAnsiString(&Ansi, &Unicode, FALSE /* do not allocate */);
535 if (!NT_SUCCESS(Status))
536 {
537 AssertMsgFailed((__FUNCTION__": RtlUnicodeStringToAnsiString failed Status (0x%x)", Status));
538 break;
539 }
540
541 pDevice->fHighSpend = FALSE;
542 Status = VBoxUsbToolGetDeviceSpeed(pDo, &pDevice->fHighSpend);
543 if (!NT_SUCCESS(Status))
544 {
545 AssertMsgFailed((__FUNCTION__": VBoxUsbToolGetDeviceSpeed failed Status (0x%x)", Status));
546 break;
547 }
548 }
549#endif
550 LOG((": strings: '%s':'%s':'%s' (lang ID %x)",
551 pDevice->szMfgName, pDevice->szProduct, pDevice->szSerial, langId));
552 }
553
554 LOG(("Populating Device(0x%p) for PDO(0x%p) Succeeded", pDevice, pDo));
555 Status = STATUS_SUCCESS;
556 } while (0);
557
558 VBoxUsbMonMemFree(pDevDr);
559 LOG(("Populating Device(0x%p) for PDO(0x%p) Done, Status (0x%x)", pDevice, pDo, Status));
560 return Status;
561}
562
563static void vboxUsbFltSignalChangeLocked()
564{
565 for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.ContextList.Flink;
566 pEntry != &g_VBoxUsbFltGlobals.ContextList;
567 pEntry = pEntry->Flink)
568 {
569 PVBOXUSBFLTCTX pCtx = PVBOXUSBFLTCTX_FROM_LE(pEntry);
570 /* the removed context can not be in a list */
571 Assert(!pCtx->bRemoved);
572 if (pCtx->pChangeEvent)
573 {
574 KeSetEvent(pCtx->pChangeEvent,
575 0, /* increment*/
576 FALSE /* wait */);
577 }
578 }
579}
580
581static bool vboxUsbFltDevCheckReplugLocked(PVBOXUSBFLT_DEVICE pDevice, PVBOXUSBFLTCTX pContext)
582{
583 ASSERT_WARN(pContext, ("context is NULL!"));
584
585 LOG(("Current context is (0x%p)", pContext));
586 LOG(("Current Device owner is (0x%p)", pDevice->pOwner));
587
588 /* check if device is already replugging */
589 if (pDevice->enmState <= VBOXUSBFLT_DEVSTATE_ADDED)
590 {
591 LOG(("Device (0x%p) is already replugging, return..", pDevice));
592 /* it is, do nothing */
593 ASSERT_WARN(pDevice->enmState == VBOXUSBFLT_DEVSTATE_REPLUGGING,
594 ("Device (0x%p) state is NOT REPLUGGING (%d)", pDevice, pDevice->enmState));
595 return false;
596 }
597
598 if (pDevice->pOwner && pContext != pDevice->pOwner)
599 {
600 LOG(("Device (0x%p) is owned by another context(0x%p), current is(0x%p)", pDevice, pDevice->pOwner, pContext));
601 /* this device is owned by another context, we're not allowed to do anything */
602 return false;
603 }
604
605 uintptr_t uId = 0;
606 bool bNeedReplug = false;
607 bool fFilter = false;
608 bool fIsOneShot = false;
609 PVBOXUSBFLTCTX pNewOwner = vboxUsbFltDevMatchLocked(pDevice, &uId,
610 false, /* do not remove a one-shot filter */
611 &fFilter, &fIsOneShot);
612 LOG(("Matching Info: Filter (0x%p), NewOwner(0x%p), fFilter(%d), fIsOneShot(%d)", uId, pNewOwner, (int)fFilter, (int)fIsOneShot));
613 if (pDevice->pOwner && pNewOwner && pDevice->pOwner != pNewOwner)
614 {
615 LOG(("Matching: Device (0x%p) is requested another owner(0x%p), current is(0x%p)", pDevice, pNewOwner, pDevice->pOwner));
616 /* the device is owned by another owner, we can not change the owner here */
617 return false;
618 }
619
620 if (!fFilter)
621 {
622 LOG(("Matching: Device (0x%p) should NOT be filtered", pDevice));
623 /* the device should NOT be filtered, check the current state */
624 if (vboxUsbFltDevStateIsNotFiltered(pDevice))
625 {
626 LOG(("Device (0x%p) is NOT filtered", pDevice));
627 /* no changes */
628 if (fIsOneShot)
629 {
630 ASSERT_WARN(pNewOwner, ("no new owner"));
631 LOG(("Matching: This is a one-shot filter (0x%p), removing..", uId));
632 /* remove a one-shot filter and keep the original filter data */
633 int tmpRc = VBoxUSBFilterRemove(pNewOwner, uId);
634 ASSERT_WARN(RT_SUCCESS(tmpRc), ("remove filter failed, rc (%d)", tmpRc));
635 if (!pDevice->pOwner)
636 {
637 LOG(("Matching: updating the one-shot owner to (0x%p), fltId(0x%p)", pNewOwner, uId));
638 /* update owner for one-shot if the owner is changed (i.e. assigned) */
639 vboxUsbFltDevOwnerUpdateLocked(pDevice, pNewOwner, uId, true);
640 }
641 else
642 {
643 LOG(("Matching: device already has owner (0x%p) assigned", pDevice->pOwner));
644 }
645 }
646 else
647 {
648 LOG(("Matching: This is NOT a one-shot filter (0x%p), newOwner(0x%p)", uId, pNewOwner));
649 if (pNewOwner)
650 {
651 vboxUsbFltDevOwnerUpdateLocked(pDevice, pNewOwner, uId, false);
652 }
653 }
654 }
655 else
656 {
657 LOG(("Device (0x%p) IS filtered", pDevice));
658 /* the device is currently filtered, we should release it only if
659 * 1. device does not have an owner
660 * or
661 * 2. it should be released bue to a one-shot filter
662 * or
663 * 3. it is NOT grabbed by a one-shot filter */
664 if (!pDevice->pOwner || fIsOneShot || !pDevice->fIsFilterOneShot)
665 {
666 LOG(("Matching: Need replug"));
667 bNeedReplug = true;
668 }
669 }
670 }
671 else
672 {
673 LOG(("Matching: Device (0x%p) SHOULD be filtered", pDevice));
674 /* the device should be filtered, check the current state */
675 ASSERT_WARN(uId, ("zero uid"));
676 ASSERT_WARN(pNewOwner, ("zero pNewOwner"));
677 if (vboxUsbFltDevStateIsFiltered(pDevice))
678 {
679 LOG(("Device (0x%p) IS filtered", pDevice));
680 /* the device is filtered */
681 if (pNewOwner == pDevice->pOwner)
682 {
683 LOG(("Device owner match"));
684 /* no changes */
685 if (fIsOneShot)
686 {
687 LOG(("Matching: This is a one-shot filter (0x%p), removing..", uId));
688 /* remove a one-shot filter and keep the original filter data */
689 int tmpRc = VBoxUSBFilterRemove(pNewOwner, uId);
690 ASSERT_WARN(RT_SUCCESS(tmpRc), ("remove filter failed, rc (%d)", tmpRc));
691 }
692 else
693 {
694 LOG(("Matching: This is NOT a one-shot filter (0x%p), Owner(0x%p)", uId, pDevice->pOwner));
695 vboxUsbFltDevOwnerUpdateLocked(pDevice, pDevice->pOwner, uId, false);
696 }
697 }
698 else
699 {
700 ASSERT_WARN(!pDevice->pOwner, ("device should NOT have owner"));
701 LOG(("Matching: Need replug"));
702 /* the device needs to be filtered, but the owner changes, replug needed */
703 bNeedReplug = true;
704 }
705 }
706 else
707 {
708 /* the device is currently NOT filtered,
709 * we should replug it only if
710 * 1. device does not have an owner
711 * or
712 * 2. it should be captured due to a one-shot filter
713 * or
714 * 3. it is NOT released by a one-shot filter */
715 if (!pDevice->pOwner || fIsOneShot || !pDevice->fIsFilterOneShot)
716 {
717 bNeedReplug = true;
718 LOG(("Matching: Need replug"));
719 }
720 }
721 }
722
723 if (bNeedReplug)
724 {
725 LOG(("Matching: Device needs replugging, marking as such"));
726 vboxUsbFltDevStateMarkReplugLocked(pDevice);
727 }
728 else
729 {
730 LOG(("Matching: Device does NOT need replugging"));
731 }
732
733 return bNeedReplug;
734}
735
736static void vboxUsbFltReplugList(PLIST_ENTRY pList)
737{
738 PLIST_ENTRY pNext;
739 for (PLIST_ENTRY pEntry = pList->Flink;
740 pEntry != pList;
741 pEntry = pNext)
742 {
743 pNext = pEntry->Flink;
744 PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_REPLUGGINGLE(pEntry);
745 LOG(("replugging matched PDO(0x%p), pDevice(0x%p)", pDevice->Pdo, pDevice));
746 ASSERT_WARN(pDevice->enmState == VBOXUSBFLT_DEVSTATE_REPLUGGING
747 || pDevice->enmState == VBOXUSBFLT_DEVSTATE_REMOVED,
748 ("invalid state(0x%x) for device(0x%p)", pDevice->enmState, pDevice));
749
750 vboxUsbFltPdoReplug(pDevice->Pdo);
751 ObDereferenceObject(pDevice->Pdo);
752 vboxUsbFltDevRelease(pDevice);
753 }
754}
755
756typedef struct VBOXUSBFLTCHECKWALKER
757{
758 PVBOXUSBFLTCTX pContext;
759} VBOXUSBFLTCHECKWALKER, *PVBOXUSBFLTCHECKWALKER;
760
761static DECLCALLBACK(BOOLEAN) vboxUsbFltFilterCheckWalker(PFILE_OBJECT pFile, PDEVICE_OBJECT pTopDo, PDEVICE_OBJECT pHubDo, PVOID pvContext)
762{
763 PVBOXUSBFLTCHECKWALKER pData = (PVBOXUSBFLTCHECKWALKER)pvContext;
764 PVBOXUSBFLTCTX pContext = pData->pContext;
765
766 LOG(("Visiting pFile(0x%p), pTopDo(0x%p), pHubDo(0x%p), oContext(0x%p)", pFile, pTopDo, pHubDo, pContext));
767 KIRQL Irql = KeGetCurrentIrql();
768 ASSERT_WARN(Irql == PASSIVE_LEVEL, ("unexpected IRQL (%d)", Irql));
769
770
771
772 PDEVICE_RELATIONS pDevRelations = NULL;
773
774 NTSTATUS Status = VBoxUsbMonQueryBusRelations(pTopDo, pFile, &pDevRelations);
775 if (Status == STATUS_SUCCESS && pDevRelations)
776 {
777 ULONG cReplugPdos = pDevRelations->Count;
778 LIST_ENTRY ReplugDevList;
779 InitializeListHead(&ReplugDevList);
780 for (ULONG k = 0; k < pDevRelations->Count; ++k)
781 {
782 PDEVICE_OBJECT pDevObj = pDevRelations->Objects[k];
783
784 LOG(("Found existing USB PDO 0x%p", pDevObj));
785 VBOXUSBFLT_LOCK_ACQUIRE();
786 PVBOXUSBFLT_DEVICE pDevice = vboxUsbFltDevGetLocked(pDevObj);
787 if (pDevice)
788 {
789 LOG(("Found existing device info (0x%p) for PDO 0x%p", pDevice, pDevObj));
790 bool bReplug = vboxUsbFltDevCheckReplugLocked(pDevice, pContext);
791 if (bReplug)
792 {
793 LOG(("Replug needed for device (0x%p)", pDevice));
794 InsertHeadList(&ReplugDevList, &pDevice->RepluggingLe);
795 vboxUsbFltDevRetain(pDevice);
796 /* do not dereference object since we will use it later */
797 }
798 else
799 {
800 LOG(("Replug NOT needed for device (0x%p)", pDevice));
801 ObDereferenceObject(pDevObj);
802 }
803
804 VBOXUSBFLT_LOCK_RELEASE();
805
806 pDevRelations->Objects[k] = NULL;
807 --cReplugPdos;
808 ASSERT_WARN((uint32_t)cReplugPdos < UINT32_MAX/2, ("cReplugPdos(%d) state broken", cReplugPdos));
809 continue;
810 }
811 VBOXUSBFLT_LOCK_RELEASE();
812
813 LOG(("NO device info found for PDO 0x%p", pDevObj));
814 VBOXUSBFLT_DEVICE Device;
815 Status = vboxUsbFltDevPopulate(&Device, pDevObj /*, FALSE /* only need filter properties */);
816 if (NT_SUCCESS(Status))
817 {
818 uintptr_t uId = 0;
819 bool fFilter = false;
820 bool fIsOneShot = false;
821 VBOXUSBFLT_LOCK_ACQUIRE();
822 PVBOXUSBFLTCTX pCtx = vboxUsbFltDevMatchLocked(&Device, &uId,
823 false, /* do not remove a one-shot filter */
824 &fFilter, &fIsOneShot);
825 VBOXUSBFLT_LOCK_RELEASE();
826 LOG(("Matching Info: Filter (0x%p), pCtx(0x%p), fFilter(%d), fIsOneShot(%d)", uId, pCtx, (int)fFilter, (int)fIsOneShot));
827 if (fFilter)
828 {
829 LOG(("Matching: This device SHOULD be filtered"));
830 /* this device needs to be filtered, but it's not,
831 * leave the PDO in array to issue a replug request for it
832 * later on */
833 continue;
834 }
835 }
836 else
837 {
838 WARN(("vboxUsbFltDevPopulate for PDO 0x%p failed with Status 0x%x", pDevObj, Status));
839 }
840
841 LOG(("Matching: This device should NOT be filtered"));
842 /* this device should not be filtered, and it's not */
843 ObDereferenceObject(pDevObj);
844 pDevRelations->Objects[k] = NULL;
845 --cReplugPdos;
846 ASSERT_WARN((uint32_t)cReplugPdos < UINT32_MAX/2, ("cReplugPdos is %d", cReplugPdos));
847 }
848
849 LOG(("(%d) non-matched PDOs to be replugged", cReplugPdos));
850
851 if (cReplugPdos)
852 {
853 for (ULONG k = 0; k < pDevRelations->Count; ++k)
854 {
855 if (!pDevRelations->Objects[k])
856 continue;
857
858 Status = vboxUsbFltPdoReplug(pDevRelations->Objects[k]);
859 ASSERT_WARN(Status == STATUS_SUCCESS, ("vboxUsbFltPdoReplug ailed Status(0x%x)", Status));
860 ObDereferenceObject(pDevRelations->Objects[k]);
861 if (!--cReplugPdos)
862 break;
863 }
864
865 ASSERT_WARN(!cReplugPdos, ("cReplugPdosreached zero!"));
866 }
867
868 vboxUsbFltReplugList(&ReplugDevList);
869
870 ExFreePool(pDevRelations);
871 }
872 else
873 {
874 WARN(("VBoxUsbMonQueryBusRelations failed for DO(0x%p), Status(0x%x), pDevRelations(0x%p)",
875 pTopDo, Status, pDevRelations));
876 }
877
878 LOG(("Done Visiting pFile(0x%p), pTopDo(0x%p), pHubDo(0x%p), oContext(0x%p)", pFile, pTopDo, pHubDo, pContext));
879
880 return TRUE;
881}
882
883NTSTATUS VBoxUsbFltFilterCheck(PVBOXUSBFLTCTX pContext)
884{
885 KIRQL Irql = KeGetCurrentIrql();
886 ASSERT_WARN(Irql == PASSIVE_LEVEL, ("unexpected IRQL (%d)", Irql));
887
888 LOG(("Running filters, Context (0x%p)..", pContext));
889
890 VBOXUSBFLTCHECKWALKER Data;
891 Data.pContext = pContext;
892 vboxUsbMonHubDevWalk(vboxUsbFltFilterCheckWalker, &Data, VBOXUSBMONHUBWALK_F_FDO);
893
894 LOG(("DONE Running filters, Context (0x%p)", pContext));
895
896 return STATUS_SUCCESS;
897}
898
899NTSTATUS VBoxUsbFltClose(PVBOXUSBFLTCTX pContext)
900{
901 LOG(("Closing context(0x%p)", pContext));
902 LIST_ENTRY ReplugDevList;
903 InitializeListHead(&ReplugDevList);
904
905 ASSERT_WARN(pContext, ("null context"));
906
907 KIRQL Irql = KeGetCurrentIrql();
908 ASSERT_WARN(Irql == PASSIVE_LEVEL, ("irql==(%d)", Irql));
909
910 VBOXUSBFLT_LOCK_ACQUIRE();
911 uint32_t cActiveFilters = pContext->cActiveFilters;
912 pContext->bRemoved = TRUE;
913 if (pContext->pChangeEvent)
914 {
915 LOG(("seting & closing change event (0x%p)", pContext->pChangeEvent));
916 KeSetEvent(pContext->pChangeEvent,
917 0, /* increment*/
918 FALSE /* wait */);
919 ObDereferenceObject(pContext->pChangeEvent);
920 pContext->pChangeEvent = NULL;
921 }
922 else
923 {
924 LOG(("no change event"));
925 }
926 RemoveEntryList(&pContext->ListEntry);
927
928 LOG(("removing owner filters"));
929 /* now re-arrange the filters */
930 /* 1. remove filters */
931 VBoxUSBFilterRemoveOwner(pContext);
932
933 LOG(("enumerating devices.."));
934 /* 2. check if there are devices owned */
935 for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
936 pEntry != &g_VBoxUsbFltGlobals.DeviceList;
937 pEntry = pEntry->Flink)
938 {
939 PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
940 if (pDevice->pOwner != pContext)
941 continue;
942
943 LOG(("found device(0x%p), pdo(0x%p), state(%d), filter id(0x%p), oneshot(%d)",
944 pDevice, pDevice->Pdo, pDevice->enmState, pDevice->uFltId, (int)pDevice->fIsFilterOneShot));
945 ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_ADDED, ("VBOXUSBFLT_DEVSTATE_ADDED state for device(0x%p)", pDevice));
946 ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_REMOVED, ("VBOXUSBFLT_DEVSTATE_REMOVED state for device(0x%p)", pDevice));
947
948 vboxUsbFltDevOwnerClearLocked(pDevice);
949
950 if (vboxUsbFltDevCheckReplugLocked(pDevice, pContext))
951 {
952 LOG(("device needs replug"));
953 InsertHeadList(&ReplugDevList, &pDevice->RepluggingLe);
954 /* retain to ensure the device is not removed before we issue a replug */
955 vboxUsbFltDevRetain(pDevice);
956 /* keep the PDO alive */
957 ObReferenceObject(pDevice->Pdo);
958 }
959 else
960 {
961 LOG(("device does NOT need replug"));
962 }
963 }
964 VBOXUSBFLT_LOCK_RELEASE();
965
966 /* this should replug all devices that were either skipped or grabbed due to the context's */
967 vboxUsbFltReplugList(&ReplugDevList);
968
969 LOG(("SUCCESS done context(0x%p)", pContext));
970 return STATUS_SUCCESS;
971}
972
973NTSTATUS VBoxUsbFltCreate(PVBOXUSBFLTCTX pContext)
974{
975 LOG(("Creating context(0x%p)", pContext));
976 memset(pContext, 0, sizeof (*pContext));
977 pContext->Process = RTProcSelf();
978 VBOXUSBFLT_LOCK_ACQUIRE();
979 InsertHeadList(&g_VBoxUsbFltGlobals.ContextList, &pContext->ListEntry);
980 VBOXUSBFLT_LOCK_RELEASE();
981 LOG(("SUCCESS context(0x%p)", pContext));
982 return STATUS_SUCCESS;
983}
984
985int VBoxUsbFltAdd(PVBOXUSBFLTCTX pContext, PUSBFILTER pFilter, uintptr_t *pId)
986{
987 LOG(("adding filter, Context (0x%p)..", pContext));
988 *pId = 0;
989 /* LOG the filter details. */
990 LOG((__FUNCTION__": %s %s %s",
991 USBFilterGetString(pFilter, USBFILTERIDX_MANUFACTURER_STR) ? USBFilterGetString(pFilter, USBFILTERIDX_MANUFACTURER_STR) : "<null>",
992 USBFilterGetString(pFilter, USBFILTERIDX_PRODUCT_STR) ? USBFilterGetString(pFilter, USBFILTERIDX_PRODUCT_STR) : "<null>",
993 USBFilterGetString(pFilter, USBFILTERIDX_SERIAL_NUMBER_STR) ? USBFilterGetString(pFilter, USBFILTERIDX_SERIAL_NUMBER_STR) : "<null>"));
994#ifdef VBOX_USB_WITH_VERBOSE_LOGGING
995 LOG(("VBoxUSBClient::addFilter: idVendor=%#x idProduct=%#x bcdDevice=%#x bDeviceClass=%#x bDeviceSubClass=%#x bDeviceProtocol=%#x bBus=%#x bPort=%#x Type%#x",
996 USBFilterGetNum(pFilter, USBFILTERIDX_VENDOR_ID),
997 USBFilterGetNum(pFilter, USBFILTERIDX_PRODUCT_ID),
998 USBFilterGetNum(pFilter, USBFILTERIDX_DEVICE_REV),
999 USBFilterGetNum(pFilter, USBFILTERIDX_DEVICE_CLASS),
1000 USBFilterGetNum(pFilter, USBFILTERIDX_DEVICE_SUB_CLASS),
1001 USBFilterGetNum(pFilter, USBFILTERIDX_DEVICE_PROTOCOL),
1002 USBFilterGetNum(pFilter, USBFILTERIDX_BUS),
1003 USBFilterGetNum(pFilter, USBFILTERIDX_PORT),
1004 USBFilterGetFilterType(pFilter)));
1005#endif
1006
1007 /* We can't get the bus/port numbers. Ignore them while matching. */
1008 USBFilterSetMustBePresent(pFilter, USBFILTERIDX_BUS, false);
1009 USBFilterSetMustBePresent(pFilter, USBFILTERIDX_PORT, false);
1010
1011 uintptr_t uId = 0;
1012 VBOXUSBFLT_LOCK_ACQUIRE();
1013 /* Add the filter. */
1014 int rc = VBoxUSBFilterAdd(pFilter, pContext, &uId);
1015 VBOXUSBFLT_LOCK_RELEASE();
1016 if (RT_SUCCESS(rc))
1017 {
1018 LOG(("ADDED filer id 0x%p", uId));
1019 ASSERT_WARN(uId, ("uid is NULL"));
1020#ifdef VBOX_USBMON_WITH_FILTER_AUTOAPPLY
1021 VBoxUsbFltFilterCheck();
1022#endif
1023 }
1024 else
1025 {
1026 WARN(("VBoxUSBFilterAdd failed rc (%d)", rc));
1027 ASSERT_WARN(!uId, ("uid is not NULL"));
1028 }
1029
1030 *pId = uId;
1031 return rc;
1032}
1033
1034int VBoxUsbFltRemove(PVBOXUSBFLTCTX pContext, uintptr_t uId)
1035{
1036 LOG(("removing filter id(0x%p), Context (0x%p)..", pContext, uId));
1037 Assert(uId);
1038
1039 VBOXUSBFLT_LOCK_ACQUIRE();
1040 int rc = VBoxUSBFilterRemove(pContext, uId);
1041 if (!RT_SUCCESS(rc))
1042 {
1043 WARN(("VBoxUSBFilterRemove failed rc (%d)", rc));
1044 VBOXUSBFLT_LOCK_RELEASE();
1045 return rc;
1046 }
1047
1048 LOG(("enumerating devices.."));
1049 for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
1050 pEntry != &g_VBoxUsbFltGlobals.DeviceList;
1051 pEntry = pEntry->Flink)
1052 {
1053 PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
1054 if (pDevice->fIsFilterOneShot)
1055 {
1056 ASSERT_WARN(!pDevice->uFltId, ("oneshot filter on device(0x%p): unexpected uFltId(%d)", pDevice, pDevice->uFltId));
1057 }
1058
1059 if (pDevice->uFltId != uId)
1060 continue;
1061
1062 ASSERT_WARN(pDevice->pOwner == pContext, ("Device(0x%p) owner(0x%p) not match to (0x%p)", pDevice, pDevice->pOwner, pContext));
1063 if (pDevice->pOwner != pContext)
1064 continue;
1065
1066 LOG(("found device(0x%p), pdo(0x%p), state(%d), filter id(0x%p), oneshot(%d)",
1067 pDevice, pDevice->Pdo, pDevice->enmState, pDevice->uFltId, (int)pDevice->fIsFilterOneShot));
1068 ASSERT_WARN(!pDevice->fIsFilterOneShot, ("device(0x%p) is filtered with a oneshot filter", pDevice));
1069 pDevice->uFltId = 0;
1070 /* clear the fIsFilterOneShot flag to ensure the device is replugged on the next VBoxUsbFltFilterCheck call */
1071 pDevice->fIsFilterOneShot = false;
1072 }
1073 VBOXUSBFLT_LOCK_RELEASE();
1074
1075 LOG(("done enumerating devices"));
1076
1077 if (RT_SUCCESS(rc))
1078 {
1079#ifdef VBOX_USBMON_WITH_FILTER_AUTOAPPLY
1080 VBoxUsbFltFilterCheck();
1081#endif
1082 }
1083 return rc;
1084}
1085
1086NTSTATUS VBoxUsbFltSetNotifyEvent(PVBOXUSBFLTCTX pContext, HANDLE hEvent)
1087{
1088 NTSTATUS Status = STATUS_SUCCESS;
1089 PKEVENT pEvent = NULL;
1090 PKEVENT pOldEvent = NULL;
1091 if (hEvent)
1092 {
1093 Status = ObReferenceObjectByHandle(hEvent,
1094 EVENT_MODIFY_STATE,
1095 *ExEventObjectType, UserMode,
1096 (PVOID*)&pEvent,
1097 NULL);
1098 Assert(Status == STATUS_SUCCESS);
1099 if (!NT_SUCCESS(Status))
1100 return Status;
1101 }
1102
1103 VBOXUSBFLT_LOCK_ACQUIRE();
1104 pOldEvent = pContext->pChangeEvent;
1105 pContext->pChangeEvent = pEvent;
1106 VBOXUSBFLT_LOCK_RELEASE();
1107
1108 if (pOldEvent)
1109 {
1110 ObDereferenceObject(pOldEvent);
1111 }
1112
1113 return STATUS_SUCCESS;
1114}
1115
1116static USBDEVICESTATE vboxUsbDevGetUserState(PVBOXUSBFLTCTX pContext, PVBOXUSBFLT_DEVICE pDevice)
1117{
1118 if (vboxUsbFltDevStateIsNotFiltered(pDevice))
1119 return USBDEVICESTATE_USED_BY_HOST_CAPTURABLE;
1120
1121 /* the device is filtered, or replugging */
1122 if (pDevice->enmState == VBOXUSBFLT_DEVSTATE_REPLUGGING)
1123 {
1124 ASSERT_WARN(!pDevice->pOwner, ("replugging device(0x%p) still has an owner(0x%p)", pDevice, pDevice->pOwner));
1125 ASSERT_WARN(!pDevice->uFltId, ("replugging device(0x%p) still has filter(0x%p)", pDevice, pDevice->uFltId));
1126 /* no user state for this, we should not return it tu the user */
1127 return USBDEVICESTATE_USED_BY_HOST;
1128 }
1129
1130 /* the device is filtered, if owner differs from the context, return as USED_BY_HOST */
1131 ASSERT_WARN(pDevice->pOwner, ("device(0x%p) has noowner", pDevice));
1132 /* the id can be null if a filter is removed */
1133// Assert(pDevice->uFltId);
1134
1135 if (pDevice->pOwner != pContext)
1136 {
1137 LOG(("Device owner differs from the current context, returning used by host"));
1138 return USBDEVICESTATE_USED_BY_HOST;
1139 }
1140
1141 switch (pDevice->enmState)
1142 {
1143 case VBOXUSBFLT_DEVSTATE_UNCAPTURED:
1144 case VBOXUSBFLT_DEVSTATE_CAPTURING:
1145 return USBDEVICESTATE_USED_BY_HOST_CAPTURABLE;
1146 case VBOXUSBFLT_DEVSTATE_CAPTURED:
1147 return USBDEVICESTATE_HELD_BY_PROXY;
1148 case VBOXUSBFLT_DEVSTATE_USED_BY_GUEST:
1149 return USBDEVICESTATE_USED_BY_GUEST;
1150 default:
1151 WARN(("unexpected device state(%d) for device(0x%p)", pDevice->enmState, pDevice));
1152 return USBDEVICESTATE_UNSUPPORTED;
1153 }
1154}
1155
1156static void vboxUsbDevToUserInfo(PVBOXUSBFLTCTX pContext, PVBOXUSBFLT_DEVICE pDevice, PUSBSUP_DEVINFO pDevInfo)
1157{
1158#if 0
1159 pDevInfo->usVendorId = pDevice->idVendor;
1160 pDevInfo->usProductId = pDevice->idProduct;
1161 pDevInfo->usRevision = pDevice->bcdDevice;
1162 pDevInfo->enmState = vboxUsbDevGetUserState(pContext, pDevice);
1163
1164 strcpy(pDevInfo->szDrvKeyName, pDevice->szDrvKeyName);
1165 if (pDevInfo->enmState == USBDEVICESTATE_HELD_BY_PROXY
1166 || pDevInfo->enmState == USBDEVICESTATE_USED_BY_GUEST)
1167 {
1168 /* this is the only case where we return the obj name to the client */
1169 strcpy(pDevInfo->szObjName, pDevice->szObjName);
1170 }
1171 pDevInfo->fHighSpeed = pDevice->fHighSpeed;
1172#endif
1173}
1174
1175NTSTATUS VBoxUsbFltGetDevice(PVBOXUSBFLTCTX pContext, HVBOXUSBDEVUSR hDevice, PUSBSUP_GETDEV_MON pInfo)
1176{
1177 Assert(hDevice);
1178
1179 memset (pInfo, 0, sizeof (*pInfo));
1180 VBOXUSBFLT_LOCK_ACQUIRE();
1181 for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
1182 pEntry != &g_VBoxUsbFltGlobals.DeviceList;
1183 pEntry = pEntry->Flink)
1184 {
1185 PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
1186 Assert(pDevice->enmState != VBOXUSBFLT_DEVSTATE_REMOVED);
1187 Assert(pDevice->enmState != VBOXUSBFLT_DEVSTATE_ADDED);
1188
1189 if (pDevice != hDevice)
1190 continue;
1191
1192 USBDEVICESTATE enmUsrState = vboxUsbDevGetUserState(pContext, pDevice);
1193 pInfo->enmState = enmUsrState;
1194 VBOXUSBFLT_LOCK_RELEASE();
1195 return STATUS_SUCCESS;
1196 }
1197
1198 VBOXUSBFLT_LOCK_RELEASE();
1199
1200 /* this should not occur */
1201 AssertFailed();
1202
1203 return STATUS_INVALID_PARAMETER;
1204}
1205
1206NTSTATUS VBoxUsbFltPdoAdd(PDEVICE_OBJECT pPdo, BOOLEAN *pbFiltered)
1207{
1208 *pbFiltered = FALSE;
1209 PVBOXUSBFLT_DEVICE pDevice;
1210
1211 /* first check if device is in the a already */
1212 VBOXUSBFLT_LOCK_ACQUIRE();
1213 pDevice = vboxUsbFltDevGetLocked(pPdo);
1214 if (pDevice)
1215 {
1216 LOG(("found device (0x%p), state(%d) for PDO(0x%p)", pDevice, pDevice->enmState, pPdo));
1217 ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_ADDED, ("VBOXUSBFLT_DEVSTATE_ADDED state for device(0x%p)", pDevice));
1218 ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_REMOVED, ("VBOXUSBFLT_DEVSTATE_REMOVED state for device(0x%p)", pDevice));
1219 *pbFiltered = pDevice->enmState >= VBOXUSBFLT_DEVSTATE_CAPTURING;
1220 VBOXUSBFLT_LOCK_RELEASE();
1221 return STATUS_SUCCESS;
1222 }
1223 VBOXUSBFLT_LOCK_RELEASE();
1224 pDevice = (PVBOXUSBFLT_DEVICE)VBoxUsbMonMemAllocZ(sizeof (*pDevice));
1225 if (!pDevice)
1226 {
1227 WARN(("VBoxUsbMonMemAllocZ failed"));
1228 return STATUS_NO_MEMORY;
1229 }
1230
1231 pDevice->enmState = VBOXUSBFLT_DEVSTATE_ADDED;
1232 pDevice->cRefs = 1;
1233 NTSTATUS Status = vboxUsbFltDevPopulate(pDevice, pPdo /* , TRUE /* need all props */);
1234 if (!NT_SUCCESS(Status))
1235 {
1236 WARN(("vboxUsbFltDevPopulate failed, Status 0x%x", Status));
1237 VBoxUsbMonMemFree(pDevice);
1238 return Status;
1239 }
1240
1241 uintptr_t uId;
1242 bool fFilter = false;
1243 bool fIsOneShot = false;
1244 PVBOXUSBFLTCTX pCtx;
1245 PVBOXUSBFLT_DEVICE pTmpDev;
1246 VBOXUSBFLT_LOCK_ACQUIRE();
1247 /* (paranoia) re-check the device is still not here */
1248 pTmpDev = vboxUsbFltDevGetLocked(pPdo);
1249 if (pTmpDev)
1250 {
1251 LOG(("second try: found device (0x%p), state(%d) for PDO(0x%p)", pDevice, pDevice->enmState, pPdo));
1252 ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_ADDED, ("second try: VBOXUSBFLT_DEVSTATE_ADDED state for device(0x%p)", pDevice));
1253 ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_REMOVED, ("second try: VBOXUSBFLT_DEVSTATE_REMOVED state for device(0x%p)", pDevice));
1254 *pbFiltered = pTmpDev->enmState >= VBOXUSBFLT_DEVSTATE_CAPTURING;
1255 VBOXUSBFLT_LOCK_RELEASE();
1256 VBoxUsbMonMemFree(pDevice);
1257 return STATUS_SUCCESS;
1258 }
1259
1260 LOG(("Created Device 0x%p for PDO 0x%p", pDevice, pPdo));
1261
1262 pCtx = vboxUsbFltDevMatchLocked(pDevice, &uId,
1263 true, /* remove a one-shot filter */
1264 &fFilter, &fIsOneShot);
1265 LOG(("Matching Info: Filter (0x%p), pCtx(0x%p), fFilter(%d), fIsOneShot(%d)", uId, pCtx, (int)fFilter, (int)fIsOneShot));
1266 if (fFilter)
1267 {
1268 LOG(("Created Device 0x%p should be filtered", pDevice));
1269 ASSERT_WARN(pCtx, ("zero ctx"));
1270 ASSERT_WARN(uId, ("zero uId"));
1271 pDevice->enmState = VBOXUSBFLT_DEVSTATE_CAPTURING;
1272 }
1273 else
1274 {
1275 LOG(("Created Device 0x%p should NOT be filtered", pDevice));
1276 ASSERT_WARN(!uId == !pCtx, ("invalid uid(0x%p) - ctx(0x%p) pair", uId, pCtx)); /* either both zero or both not */
1277 pDevice->enmState = VBOXUSBFLT_DEVSTATE_UNCAPTURED;
1278 }
1279
1280 if (pCtx)
1281 vboxUsbFltDevOwnerSetLocked(pDevice, pCtx, fIsOneShot ? 0 : uId, fIsOneShot);
1282
1283 InsertHeadList(&g_VBoxUsbFltGlobals.DeviceList, &pDevice->GlobalLe);
1284
1285 /* do not need to signal anything here -
1286 * going to do that once the proxy device object starts */
1287 VBOXUSBFLT_LOCK_RELEASE();
1288
1289 *pbFiltered = fFilter;
1290
1291 return STATUS_SUCCESS;
1292}
1293
1294NTSTATUS VBoxUsbFltPdoAddCompleted(PDEVICE_OBJECT pPdo)
1295{
1296 VBOXUSBFLT_LOCK_ACQUIRE();
1297 vboxUsbFltSignalChangeLocked();
1298 VBOXUSBFLT_LOCK_RELEASE();
1299 return STATUS_SUCCESS;
1300}
1301
1302BOOLEAN VBoxUsbFltPdoIsFiltered(PDEVICE_OBJECT pPdo)
1303{
1304 VBOXUSBFLT_DEVSTATE enmState = VBOXUSBFLT_DEVSTATE_REMOVED;
1305 VBOXUSBFLT_LOCK_ACQUIRE();
1306 PVBOXUSBFLT_DEVICE pDevice = vboxUsbFltDevGetLocked(pPdo);
1307 if (pDevice)
1308 {
1309 enmState = pDevice->enmState;
1310 }
1311 VBOXUSBFLT_LOCK_RELEASE();
1312
1313 return enmState >= VBOXUSBFLT_DEVSTATE_CAPTURING;
1314}
1315
1316NTSTATUS VBoxUsbFltPdoRemove(PDEVICE_OBJECT pPdo)
1317{
1318 PVBOXUSBFLT_DEVICE pDevice;
1319 VBOXUSBFLT_DEVSTATE enmOldState;
1320
1321 VBOXUSBFLT_LOCK_ACQUIRE();
1322 pDevice = vboxUsbFltDevGetLocked(pPdo);
1323 if (pDevice)
1324 {
1325 RemoveEntryList(&pDevice->GlobalLe);
1326 enmOldState = pDevice->enmState;
1327 pDevice->enmState = VBOXUSBFLT_DEVSTATE_REMOVED;
1328 if (enmOldState != VBOXUSBFLT_DEVSTATE_REPLUGGING)
1329 {
1330 vboxUsbFltSignalChangeLocked();
1331 }
1332 else
1333 {
1334 /* the device *should* reappear, do signlling on re-appear only
1335 * to avoid extra signaling. still there might be a situation
1336 * when the device will not re-appear if it gets physically removed
1337 * before it re-appears
1338 * @todo: set a timer callback to do a notification from it */
1339 }
1340 }
1341 VBOXUSBFLT_LOCK_RELEASE();
1342 if (pDevice)
1343 vboxUsbFltDevRelease(pDevice);
1344 return STATUS_SUCCESS;
1345}
1346
1347HVBOXUSBFLTDEV VBoxUsbFltProxyStarted(PDEVICE_OBJECT pPdo)
1348{
1349 PVBOXUSBFLT_DEVICE pDevice;
1350 VBOXUSBFLT_LOCK_ACQUIRE();
1351 pDevice = vboxUsbFltDevGetLocked(pPdo);
1352 if (pDevice->enmState = VBOXUSBFLT_DEVSTATE_CAPTURING)
1353 {
1354 pDevice->enmState = VBOXUSBFLT_DEVSTATE_CAPTURED;
1355 LOG(("The proxy notified proxy start for the captured device 0x%x", pDevice));
1356 vboxUsbFltDevRetain(pDevice);
1357 vboxUsbFltSignalChangeLocked();
1358 }
1359 else
1360 {
1361 WARN(("invalid state, %d", pDevice->enmState));
1362 pDevice = NULL;
1363 }
1364 VBOXUSBFLT_LOCK_RELEASE();
1365 return pDevice;
1366}
1367
1368void VBoxUsbFltProxyStopped(HVBOXUSBFLTDEV hDev)
1369{
1370 PVBOXUSBFLT_DEVICE pDevice = (PVBOXUSBFLT_DEVICE)hDev;
1371 VBOXUSBFLT_LOCK_ACQUIRE();
1372 if (pDevice->enmState == VBOXUSBFLT_DEVSTATE_CAPTURED
1373 || pDevice->enmState == VBOXUSBFLT_DEVSTATE_USED_BY_GUEST)
1374 {
1375 /* this is due to devie was physically removed */
1376 LOG(("The proxy notified proxy stop for the captured device 0x%x, current state %d", pDevice, pDevice->enmState));
1377 pDevice->enmState = VBOXUSBFLT_DEVSTATE_CAPTURING;
1378 vboxUsbFltSignalChangeLocked();
1379 }
1380 else
1381 {
1382 if(pDevice->enmState != VBOXUSBFLT_DEVSTATE_REPLUGGING)
1383 {
1384 WARN(("invalid state, %d", pDevice->enmState));
1385 }
1386 }
1387 VBOXUSBFLT_LOCK_RELEASE();
1388
1389 vboxUsbFltDevRelease(pDevice);
1390}
1391
1392NTSTATUS VBoxUsbFltInit()
1393{
1394 int rc = VBoxUSBFilterInit();
1395 if (RT_FAILURE(rc))
1396 {
1397 WARN(("VBoxUSBFilterInit failed, rc (%d)", rc));
1398 return STATUS_UNSUCCESSFUL;
1399 }
1400
1401 memset(&g_VBoxUsbFltGlobals, 0, sizeof (g_VBoxUsbFltGlobals));
1402 InitializeListHead(&g_VBoxUsbFltGlobals.DeviceList);
1403 InitializeListHead(&g_VBoxUsbFltGlobals.ContextList);
1404 InitializeListHead(&g_VBoxUsbFltGlobals.BlackDeviceList);
1405 vboxUsbFltBlDevPopulateWithKnownLocked();
1406 VBOXUSBFLT_LOCK_INIT();
1407 return STATUS_SUCCESS;
1408}
1409
1410NTSTATUS VBoxUsbFltTerm()
1411{
1412 bool bBusy = false;
1413 VBOXUSBFLT_LOCK_ACQUIRE();
1414 do
1415 {
1416 if (!IsListEmpty(&g_VBoxUsbFltGlobals.ContextList))
1417 {
1418 AssertFailed();
1419 bBusy = true;
1420 break;
1421 }
1422
1423 PLIST_ENTRY pNext = NULL;
1424 for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
1425 pEntry != &g_VBoxUsbFltGlobals.DeviceList;
1426 pEntry = pNext)
1427 {
1428 pNext = pEntry->Flink;
1429 PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
1430 Assert(!pDevice->uFltId);
1431 Assert(!pDevice->pOwner);
1432 if (pDevice->cRefs != 1)
1433 {
1434 AssertFailed();
1435 bBusy = true;
1436 break;
1437 }
1438 }
1439 } while (0);
1440
1441 VBOXUSBFLT_LOCK_RELEASE()
1442
1443 if (bBusy)
1444 {
1445 return STATUS_DEVICE_BUSY;
1446 }
1447
1448 for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
1449 pEntry != &g_VBoxUsbFltGlobals.DeviceList;
1450 pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink)
1451 {
1452 RemoveEntryList(pEntry);
1453 PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
1454 pDevice->enmState = VBOXUSBFLT_DEVSTATE_REMOVED;
1455 vboxUsbFltDevRelease(pDevice);
1456 }
1457
1458 vboxUsbFltBlDevClearLocked();
1459
1460 VBOXUSBFLT_LOCK_TERM();
1461
1462 VBoxUSBFilterTerm();
1463
1464 return STATUS_SUCCESS;
1465}
1466
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette