1 | /* $Id: VBoxUsbFlt.cpp 38436 2011-08-12 14:10:45Z 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. */
|
---|
62 | static 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 | */
|
---|
89 | typedef 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 |
|
---|
102 | typedef 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 |
|
---|
138 | typedef 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 |
|
---|
153 | typedef 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 |
|
---|
163 | typedef 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;
|
---|
171 | static VBOXUSBFLTGLOBALS g_VBoxUsbFltGlobals;
|
---|
172 |
|
---|
173 | static 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 |
|
---|
192 | static 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 |
|
---|
210 | static 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 |
|
---|
222 | static void vboxUsbFltBlDevPopulateWithKnownLocked()
|
---|
223 | {
|
---|
224 | /* this one halts when trying to get string descriptors from it */
|
---|
225 | vboxUsbFltBlDevAddLocked(0x5ac, 0x921c, 0x115);
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | DECLINLINE(void) vboxUsbFltDevRetain(PVBOXUSBFLT_DEVICE pDevice)
|
---|
230 | {
|
---|
231 | Assert(pDevice->cRefs);
|
---|
232 | ASMAtomicIncU32(&pDevice->cRefs);
|
---|
233 | }
|
---|
234 |
|
---|
235 | static void vboxUsbFltDevDestroy(PVBOXUSBFLT_DEVICE pDevice)
|
---|
236 | {
|
---|
237 | Assert(!pDevice->cRefs);
|
---|
238 | Assert(pDevice->enmState == VBOXUSBFLT_DEVSTATE_REMOVED);
|
---|
239 | VBoxUsbMonMemFree(pDevice);
|
---|
240 | }
|
---|
241 |
|
---|
242 | DECLINLINE(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 |
|
---|
252 | static 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 |
|
---|
261 | static 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 |
|
---|
270 | static 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 |
|
---|
286 | static 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 |
|
---|
322 | PVBOXUSBFLT_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 |
|
---|
343 | static 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 |
|
---|
352 | static 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 |
|
---|
375 | static void vboxUsbFltDevStateMarkReplugLocked(PVBOXUSBFLT_DEVICE pDevice)
|
---|
376 | {
|
---|
377 | vboxUsbFltDevOwnerUpdateLocked(pDevice, NULL, 0, false);
|
---|
378 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_REPLUGGING;
|
---|
379 | }
|
---|
380 |
|
---|
381 | static bool vboxUsbFltDevStateIsNotFiltered(PVBOXUSBFLT_DEVICE pDevice)
|
---|
382 | {
|
---|
383 | return pDevice->enmState == VBOXUSBFLT_DEVSTATE_UNCAPTURED;
|
---|
384 | }
|
---|
385 |
|
---|
386 | static bool vboxUsbFltDevStateIsFiltered(PVBOXUSBFLT_DEVICE pDevice)
|
---|
387 | {
|
---|
388 | return pDevice->enmState >= VBOXUSBFLT_DEVSTATE_CAPTURING;
|
---|
389 | }
|
---|
390 |
|
---|
391 | #define VBOXUSBMON_POPULATE_REQUEST_TIMEOUT_MS 10000
|
---|
392 |
|
---|
393 | static 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\n"));
|
---|
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)\n", 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)\n", pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice));
|
---|
421 | Status = STATUS_UNSUCCESSFUL;
|
---|
422 | break;
|
---|
423 | }
|
---|
424 |
|
---|
425 | LOG(("Device pid=%x vid=%x rev=%x\n", 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\n"));
|
---|
445 | if (Status == STATUS_CANCELLED)
|
---|
446 | {
|
---|
447 | WARN(("found a new black list device, vid(0x%x), pid(0x%x), rev(0x%x)\n", 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\n"));
|
---|
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)\n", 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\n"));
|
---|
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)\n", 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\n"));
|
---|
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)\n", 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)\n", 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)\n", 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)\n", Status));
|
---|
546 | break;
|
---|
547 | }
|
---|
548 | }
|
---|
549 | #endif
|
---|
550 | LOG((": strings: '%s':'%s':'%s' (lang ID %x)\n",
|
---|
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 |
|
---|
563 | static 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 |
|
---|
581 | static 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 |
|
---|
736 | static 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 |
|
---|
756 | NTSTATUS VBoxUsbFltFilterCheck(PVBOXUSBFLTCTX pContext)
|
---|
757 | {
|
---|
758 | NTSTATUS Status;
|
---|
759 | UNICODE_STRING szStandardControllerName[RT_ELEMENTS(lpszStandardControllerName)];
|
---|
760 | KIRQL Irql = KeGetCurrentIrql();
|
---|
761 | Assert(Irql == PASSIVE_LEVEL);
|
---|
762 |
|
---|
763 | LOG(("Running filters, Context (0x%p)..", pContext));
|
---|
764 |
|
---|
765 | for (int i=0;i<RT_ELEMENTS(lpszStandardControllerName);i++)
|
---|
766 | {
|
---|
767 | szStandardControllerName[i].Length = 0;
|
---|
768 | szStandardControllerName[i].MaximumLength = 0;
|
---|
769 | szStandardControllerName[i].Buffer = 0;
|
---|
770 |
|
---|
771 | RtlInitUnicodeString(&szStandardControllerName[i], lpszStandardControllerName[i]);
|
---|
772 | }
|
---|
773 |
|
---|
774 | for (int i = 0; i < 16; i++)
|
---|
775 | {
|
---|
776 | char szHubName[32];
|
---|
777 | WCHAR szwHubName[32];
|
---|
778 | UNICODE_STRING UnicodeName;
|
---|
779 | ANSI_STRING AnsiName;
|
---|
780 | PDEVICE_OBJECT pHubDevObj;
|
---|
781 | PFILE_OBJECT pHubFileObj;
|
---|
782 |
|
---|
783 | sprintf(szHubName, "\\Device\\USBPDO-%d", i);
|
---|
784 |
|
---|
785 | UnicodeName.Length = 0;
|
---|
786 | UnicodeName.MaximumLength = sizeof (szwHubName);
|
---|
787 | UnicodeName.Buffer = szwHubName;
|
---|
788 |
|
---|
789 | RtlInitAnsiString(&AnsiName, szHubName);
|
---|
790 | RtlAnsiStringToUnicodeString(&UnicodeName, &AnsiName, FALSE);
|
---|
791 |
|
---|
792 | Status = IoGetDeviceObjectPointer(&UnicodeName, FILE_READ_DATA, &pHubFileObj, &pHubDevObj);
|
---|
793 | if (Status == STATUS_SUCCESS)
|
---|
794 | {
|
---|
795 | LOG(("IoGetDeviceObjectPointer for %s returned %p %p\n", szHubName, pHubDevObj, pHubFileObj));
|
---|
796 |
|
---|
797 | if (pHubDevObj->DriverObject
|
---|
798 | && pHubDevObj->DriverObject->DriverName.Buffer
|
---|
799 | && pHubDevObj->DriverObject->DriverName.Length
|
---|
800 | )
|
---|
801 | {
|
---|
802 | for (int j = 0; j < RT_ELEMENTS(lpszStandardControllerName); ++j)
|
---|
803 | {
|
---|
804 | if (!RtlCompareUnicodeString(&szStandardControllerName[j], &pHubDevObj->DriverObject->DriverName, TRUE /* case insensitive */))
|
---|
805 | {
|
---|
806 | PDEVICE_RELATIONS pDevRelations = NULL;
|
---|
807 |
|
---|
808 | LOG(("Flt Associated driver "));
|
---|
809 | LOG_USTR(&pHubDevObj->DriverObject->DriverName);
|
---|
810 | LOG((" -> related dev obj=0x%p", pHubDevObj));
|
---|
811 |
|
---|
812 | Status = VBoxUsbMonQueryBusRelations(pHubDevObj, pHubFileObj, &pDevRelations);
|
---|
813 | if (Status == STATUS_SUCCESS && pDevRelations)
|
---|
814 | {
|
---|
815 | ULONG cReplugPdos = pDevRelations->Count;
|
---|
816 | LIST_ENTRY ReplugDevList;
|
---|
817 | InitializeListHead(&ReplugDevList);
|
---|
818 | for (ULONG k = 0; k < pDevRelations->Count; ++k)
|
---|
819 | {
|
---|
820 | PDEVICE_OBJECT pDevObj = pDevRelations->Objects[k];
|
---|
821 |
|
---|
822 | LOG(("Found existing USB PDO 0x%p", pDevObj));
|
---|
823 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
824 | PVBOXUSBFLT_DEVICE pDevice = vboxUsbFltDevGetLocked(pDevObj);
|
---|
825 | if (pDevice)
|
---|
826 | {
|
---|
827 | LOG(("Found existing device info (0x%p) for PDO 0x%p", pDevice, pDevObj));
|
---|
828 | bool bReplug = vboxUsbFltDevCheckReplugLocked(pDevice, pContext);
|
---|
829 | if (bReplug)
|
---|
830 | {
|
---|
831 | LOG(("Replug needed for device (0x%p)", pDevice));
|
---|
832 | InsertHeadList(&ReplugDevList, &pDevice->RepluggingLe);
|
---|
833 | vboxUsbFltDevRetain(pDevice);
|
---|
834 | /* do not dereference object since we will use it later */
|
---|
835 | }
|
---|
836 | else
|
---|
837 | {
|
---|
838 | LOG(("Replug NOT needed for device (0x%p)", pDevice));
|
---|
839 | ObDereferenceObject(pDevObj);
|
---|
840 | }
|
---|
841 |
|
---|
842 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
843 |
|
---|
844 | pDevRelations->Objects[k] = NULL;
|
---|
845 | --cReplugPdos;
|
---|
846 | ASSERT_WARN((uint32_t)cReplugPdos < UINT32_MAX/2, ("cReplugPdos(%d) state broken", cReplugPdos));
|
---|
847 | continue;
|
---|
848 | }
|
---|
849 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
850 |
|
---|
851 | LOG(("NO device info found for PDO 0x%p", pDevObj));
|
---|
852 | VBOXUSBFLT_DEVICE Device;
|
---|
853 | Status = vboxUsbFltDevPopulate(&Device, pDevObj /*, FALSE /* only need filter properties */);
|
---|
854 | if (NT_SUCCESS(Status))
|
---|
855 | {
|
---|
856 | uintptr_t uId = 0;
|
---|
857 | bool fFilter = false;
|
---|
858 | bool fIsOneShot = false;
|
---|
859 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
860 | PVBOXUSBFLTCTX pCtx = vboxUsbFltDevMatchLocked(&Device, &uId,
|
---|
861 | false, /* do not remove a one-shot filter */
|
---|
862 | &fFilter, &fIsOneShot);
|
---|
863 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
864 | LOG(("Matching Info: Filter (0x%p), pCtx(0x%p), fFilter(%d), fIsOneShot(%d)", uId, pCtx, (int)fFilter, (int)fIsOneShot));
|
---|
865 | if (fFilter)
|
---|
866 | {
|
---|
867 | LOG(("Matching: This device SHOULD be filtered"));
|
---|
868 | /* this device needs to be filtered, but it's not,
|
---|
869 | * leave the PDO in array to issue a replug request for it
|
---|
870 | * later on */
|
---|
871 | continue;
|
---|
872 | }
|
---|
873 | }
|
---|
874 | else
|
---|
875 | {
|
---|
876 | WARN(("vboxUsbFltDevPopulate for PDO 0x%p failed with Status 0x%x", pDevObj, Status));
|
---|
877 | }
|
---|
878 |
|
---|
879 | LOG(("Matching: This device should NOT be filtered"));
|
---|
880 | /* this device should not be filtered, and it's not */
|
---|
881 | ObDereferenceObject(pDevObj);
|
---|
882 | pDevRelations->Objects[k] = NULL;
|
---|
883 | --cReplugPdos;
|
---|
884 | ASSERT_WARN((uint32_t)cReplugPdos < UINT32_MAX/2, ("cReplugPdos is %d", cReplugPdos));
|
---|
885 | }
|
---|
886 |
|
---|
887 | LOG(("(%d) non-matched PDOs to be replugged", cReplugPdos));
|
---|
888 |
|
---|
889 | if (cReplugPdos)
|
---|
890 | {
|
---|
891 | for (ULONG k = 0; k < pDevRelations->Count; ++k)
|
---|
892 | {
|
---|
893 | if (!pDevRelations->Objects[k])
|
---|
894 | continue;
|
---|
895 |
|
---|
896 | Status = vboxUsbFltPdoReplug(pDevRelations->Objects[k]);
|
---|
897 | ASSERT_WARN(Status == STATUS_SUCCESS, ("vboxUsbFltPdoReplug ailed Status(0x%x)", Status));
|
---|
898 | ObDereferenceObject(pDevRelations->Objects[k]);
|
---|
899 | if (!--cReplugPdos)
|
---|
900 | break;
|
---|
901 | }
|
---|
902 |
|
---|
903 | ASSERT_WARN(!cReplugPdos, ("cReplugPdosreached zero!"));
|
---|
904 | }
|
---|
905 |
|
---|
906 | vboxUsbFltReplugList(&ReplugDevList);
|
---|
907 |
|
---|
908 | ExFreePool(pDevRelations);
|
---|
909 | }
|
---|
910 | else
|
---|
911 | {
|
---|
912 | WARN(("VBoxUsbMonQueryBusRelations failed for DO(0x%p), Status(0x%x), pDevRelations(0x%p)",
|
---|
913 | pHubDevObj, Status, pDevRelations));
|
---|
914 | }
|
---|
915 | }
|
---|
916 | else
|
---|
917 | {
|
---|
918 | LOG(("driver name not match, was:"));
|
---|
919 | LOG_USTR(&pHubDevObj->DriverObject->DriverName);
|
---|
920 | LOG(("but expected:"));
|
---|
921 | LOG_USTR(&szStandardControllerName[j]);
|
---|
922 | }
|
---|
923 | }
|
---|
924 | }
|
---|
925 | else
|
---|
926 | {
|
---|
927 | LOG(("null driver object (0x%p) or name buffer (0x%p), length(%d)", pHubDevObj->DriverObject,
|
---|
928 | pHubDevObj->DriverObject ? pHubDevObj->DriverObject->DriverName.Buffer : NULL,
|
---|
929 | pHubDevObj->DriverObject ? pHubDevObj->DriverObject->DriverName.Length : 0));
|
---|
930 | }
|
---|
931 | ObDereferenceObject(pHubFileObj);
|
---|
932 | }
|
---|
933 | }
|
---|
934 |
|
---|
935 | LOG(("DONE Running filters, Context (0x%p)", pContext));
|
---|
936 |
|
---|
937 | return STATUS_SUCCESS;
|
---|
938 | }
|
---|
939 |
|
---|
940 | NTSTATUS VBoxUsbFltClose(PVBOXUSBFLTCTX pContext)
|
---|
941 | {
|
---|
942 | LOG(("Closing context(0x%p)", pContext));
|
---|
943 | LIST_ENTRY ReplugDevList;
|
---|
944 | InitializeListHead(&ReplugDevList);
|
---|
945 |
|
---|
946 | ASSERT_WARN(pContext, ("null context"));
|
---|
947 |
|
---|
948 | KIRQL Irql = KeGetCurrentIrql();
|
---|
949 | ASSERT_WARN(Irql == PASSIVE_LEVEL, ("irql==(%d)", Irql));
|
---|
950 |
|
---|
951 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
952 | uint32_t cActiveFilters = pContext->cActiveFilters;
|
---|
953 | pContext->bRemoved = TRUE;
|
---|
954 | if (pContext->pChangeEvent)
|
---|
955 | {
|
---|
956 | LOG(("seting & closing change event (0x%p)", pContext->pChangeEvent));
|
---|
957 | KeSetEvent(pContext->pChangeEvent,
|
---|
958 | 0, /* increment*/
|
---|
959 | FALSE /* wait */);
|
---|
960 | ObDereferenceObject(pContext->pChangeEvent);
|
---|
961 | pContext->pChangeEvent = NULL;
|
---|
962 | }
|
---|
963 | else
|
---|
964 | {
|
---|
965 | LOG(("no change event"));
|
---|
966 | }
|
---|
967 | RemoveEntryList(&pContext->ListEntry);
|
---|
968 |
|
---|
969 | LOG(("removing owner filters"));
|
---|
970 | /* now re-arrange the filters */
|
---|
971 | /* 1. remove filters */
|
---|
972 | VBoxUSBFilterRemoveOwner(pContext);
|
---|
973 |
|
---|
974 | LOG(("enumerating devices.."));
|
---|
975 | /* 2. check if there are devices owned */
|
---|
976 | for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
|
---|
977 | pEntry != &g_VBoxUsbFltGlobals.DeviceList;
|
---|
978 | pEntry = pEntry->Flink)
|
---|
979 | {
|
---|
980 | PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
|
---|
981 | if (pDevice->pOwner != pContext)
|
---|
982 | continue;
|
---|
983 |
|
---|
984 | LOG(("found device(0x%p), pdo(0x%p), state(%d), filter id(0x%p), oneshot(%d)",
|
---|
985 | pDevice, pDevice->Pdo, pDevice->enmState, pDevice->uFltId, (int)pDevice->fIsFilterOneShot));
|
---|
986 | ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_ADDED, ("VBOXUSBFLT_DEVSTATE_ADDED state for device(0x%p)", pDevice));
|
---|
987 | ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_REMOVED, ("VBOXUSBFLT_DEVSTATE_REMOVED state for device(0x%p)", pDevice));
|
---|
988 |
|
---|
989 | vboxUsbFltDevOwnerClearLocked(pDevice);
|
---|
990 |
|
---|
991 | if (vboxUsbFltDevCheckReplugLocked(pDevice, pContext))
|
---|
992 | {
|
---|
993 | LOG(("device needs replug"));
|
---|
994 | InsertHeadList(&ReplugDevList, &pDevice->RepluggingLe);
|
---|
995 | /* retain to ensure the device is not removed before we issue a replug */
|
---|
996 | vboxUsbFltDevRetain(pDevice);
|
---|
997 | /* keep the PDO alive */
|
---|
998 | ObReferenceObject(pDevice->Pdo);
|
---|
999 | }
|
---|
1000 | else
|
---|
1001 | {
|
---|
1002 | LOG(("device does NOT need replug"));
|
---|
1003 | }
|
---|
1004 | }
|
---|
1005 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1006 |
|
---|
1007 | /* this should replug all devices that were either skipped or grabbed due to the context's */
|
---|
1008 | vboxUsbFltReplugList(&ReplugDevList);
|
---|
1009 |
|
---|
1010 | LOG(("SUCCESS done context(0x%p)", pContext));
|
---|
1011 | return STATUS_SUCCESS;
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | NTSTATUS VBoxUsbFltCreate(PVBOXUSBFLTCTX pContext)
|
---|
1015 | {
|
---|
1016 | LOG(("Creating context(0x%p)", pContext));
|
---|
1017 | memset(pContext, 0, sizeof (*pContext));
|
---|
1018 | pContext->Process = RTProcSelf();
|
---|
1019 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1020 | InsertHeadList(&g_VBoxUsbFltGlobals.ContextList, &pContext->ListEntry);
|
---|
1021 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1022 | LOG(("SUCCESS context(0x%p)", pContext));
|
---|
1023 | return STATUS_SUCCESS;
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | int VBoxUsbFltAdd(PVBOXUSBFLTCTX pContext, PUSBFILTER pFilter, uintptr_t *pId)
|
---|
1027 | {
|
---|
1028 | LOG(("adding filter, Context (0x%p)..", pContext));
|
---|
1029 | *pId = 0;
|
---|
1030 | /* LOG the filter details. */
|
---|
1031 | LOG((__FUNCTION__": %s %s %s\n",
|
---|
1032 | USBFilterGetString(pFilter, USBFILTERIDX_MANUFACTURER_STR) ? USBFilterGetString(pFilter, USBFILTERIDX_MANUFACTURER_STR) : "<null>",
|
---|
1033 | USBFilterGetString(pFilter, USBFILTERIDX_PRODUCT_STR) ? USBFilterGetString(pFilter, USBFILTERIDX_PRODUCT_STR) : "<null>",
|
---|
1034 | USBFilterGetString(pFilter, USBFILTERIDX_SERIAL_NUMBER_STR) ? USBFilterGetString(pFilter, USBFILTERIDX_SERIAL_NUMBER_STR) : "<null>"));
|
---|
1035 | #ifdef VBOX_USB_WITH_VERBOSE_LOGGING
|
---|
1036 | LOG(("VBoxUSBClient::addFilter: idVendor=%#x idProduct=%#x bcdDevice=%#x bDeviceClass=%#x bDeviceSubClass=%#x bDeviceProtocol=%#x bBus=%#x bPort=%#x Type%#x\n",
|
---|
1037 | USBFilterGetNum(pFilter, USBFILTERIDX_VENDOR_ID),
|
---|
1038 | USBFilterGetNum(pFilter, USBFILTERIDX_PRODUCT_ID),
|
---|
1039 | USBFilterGetNum(pFilter, USBFILTERIDX_DEVICE_REV),
|
---|
1040 | USBFilterGetNum(pFilter, USBFILTERIDX_DEVICE_CLASS),
|
---|
1041 | USBFilterGetNum(pFilter, USBFILTERIDX_DEVICE_SUB_CLASS),
|
---|
1042 | USBFilterGetNum(pFilter, USBFILTERIDX_DEVICE_PROTOCOL),
|
---|
1043 | USBFilterGetNum(pFilter, USBFILTERIDX_BUS),
|
---|
1044 | USBFilterGetNum(pFilter, USBFILTERIDX_PORT),
|
---|
1045 | USBFilterGetFilterType(pFilter)));
|
---|
1046 | #endif
|
---|
1047 |
|
---|
1048 | /* We can't get the bus/port numbers. Ignore them while matching. */
|
---|
1049 | USBFilterSetMustBePresent(pFilter, USBFILTERIDX_BUS, false);
|
---|
1050 | USBFilterSetMustBePresent(pFilter, USBFILTERIDX_PORT, false);
|
---|
1051 |
|
---|
1052 | uintptr_t uId = 0;
|
---|
1053 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1054 | /* Add the filter. */
|
---|
1055 | int rc = VBoxUSBFilterAdd(pFilter, pContext, &uId);
|
---|
1056 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1057 | if (RT_SUCCESS(rc))
|
---|
1058 | {
|
---|
1059 | LOG(("ADDED filer id 0x%p", uId));
|
---|
1060 | ASSERT_WARN(uId, ("uid is NULL"));
|
---|
1061 | #ifdef VBOX_USBMON_WITH_FILTER_AUTOAPPLY
|
---|
1062 | VBoxUsbFltFilterCheck();
|
---|
1063 | #endif
|
---|
1064 | }
|
---|
1065 | else
|
---|
1066 | {
|
---|
1067 | WARN(("VBoxUSBFilterAdd failed rc (%d)", rc));
|
---|
1068 | ASSERT_WARN(!uId, ("uid is not NULL"));
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | *pId = uId;
|
---|
1072 | return rc;
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | int VBoxUsbFltRemove(PVBOXUSBFLTCTX pContext, uintptr_t uId)
|
---|
1076 | {
|
---|
1077 | LOG(("removing filter id(0x%p), Context (0x%p)..", pContext, uId));
|
---|
1078 | Assert(uId);
|
---|
1079 |
|
---|
1080 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1081 | int rc = VBoxUSBFilterRemove(pContext, uId);
|
---|
1082 | if (!RT_SUCCESS(rc))
|
---|
1083 | {
|
---|
1084 | WARN(("VBoxUSBFilterRemove failed rc (%d)", rc));
|
---|
1085 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1086 | return rc;
|
---|
1087 | }
|
---|
1088 |
|
---|
1089 | LOG(("enumerating devices.."));
|
---|
1090 | for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
|
---|
1091 | pEntry != &g_VBoxUsbFltGlobals.DeviceList;
|
---|
1092 | pEntry = pEntry->Flink)
|
---|
1093 | {
|
---|
1094 | PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
|
---|
1095 | if (pDevice->fIsFilterOneShot)
|
---|
1096 | {
|
---|
1097 | ASSERT_WARN(!pDevice->uFltId, ("oneshot filter on device(0x%p): unexpected uFltId(%d)", pDevice, pDevice->uFltId));
|
---|
1098 | }
|
---|
1099 |
|
---|
1100 | if (pDevice->uFltId != uId)
|
---|
1101 | continue;
|
---|
1102 |
|
---|
1103 | ASSERT_WARN(pDevice->pOwner == pContext, ("Device(0x%p) owner(0x%p) not match to (0x%p)", pDevice, pDevice->pOwner, pContext));
|
---|
1104 | if (pDevice->pOwner != pContext)
|
---|
1105 | continue;
|
---|
1106 |
|
---|
1107 | LOG(("found device(0x%p), pdo(0x%p), state(%d), filter id(0x%p), oneshot(%d)",
|
---|
1108 | pDevice, pDevice->Pdo, pDevice->enmState, pDevice->uFltId, (int)pDevice->fIsFilterOneShot));
|
---|
1109 | ASSERT_WARN(!pDevice->fIsFilterOneShot, ("device(0x%p) is filtered with a oneshot filter", pDevice));
|
---|
1110 | pDevice->uFltId = 0;
|
---|
1111 | /* clear the fIsFilterOneShot flag to ensure the device is replugged on the next VBoxUsbFltFilterCheck call */
|
---|
1112 | pDevice->fIsFilterOneShot = false;
|
---|
1113 | }
|
---|
1114 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1115 |
|
---|
1116 | LOG(("done enumerating devices"));
|
---|
1117 |
|
---|
1118 | if (RT_SUCCESS(rc))
|
---|
1119 | {
|
---|
1120 | #ifdef VBOX_USBMON_WITH_FILTER_AUTOAPPLY
|
---|
1121 | VBoxUsbFltFilterCheck();
|
---|
1122 | #endif
|
---|
1123 | }
|
---|
1124 | return rc;
|
---|
1125 | }
|
---|
1126 |
|
---|
1127 | NTSTATUS VBoxUsbFltSetNotifyEvent(PVBOXUSBFLTCTX pContext, HANDLE hEvent)
|
---|
1128 | {
|
---|
1129 | NTSTATUS Status = STATUS_SUCCESS;
|
---|
1130 | PKEVENT pEvent = NULL;
|
---|
1131 | PKEVENT pOldEvent = NULL;
|
---|
1132 | if (hEvent)
|
---|
1133 | {
|
---|
1134 | Status = ObReferenceObjectByHandle(hEvent,
|
---|
1135 | EVENT_MODIFY_STATE,
|
---|
1136 | *ExEventObjectType, UserMode,
|
---|
1137 | (PVOID*)&pEvent,
|
---|
1138 | NULL);
|
---|
1139 | Assert(Status == STATUS_SUCCESS);
|
---|
1140 | if (!NT_SUCCESS(Status))
|
---|
1141 | return Status;
|
---|
1142 | }
|
---|
1143 |
|
---|
1144 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1145 | pOldEvent = pContext->pChangeEvent;
|
---|
1146 | pContext->pChangeEvent = pEvent;
|
---|
1147 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1148 |
|
---|
1149 | if (pOldEvent)
|
---|
1150 | {
|
---|
1151 | ObDereferenceObject(pOldEvent);
|
---|
1152 | }
|
---|
1153 |
|
---|
1154 | return STATUS_SUCCESS;
|
---|
1155 | }
|
---|
1156 |
|
---|
1157 | static USBDEVICESTATE vboxUsbDevGetUserState(PVBOXUSBFLTCTX pContext, PVBOXUSBFLT_DEVICE pDevice)
|
---|
1158 | {
|
---|
1159 | if (vboxUsbFltDevStateIsNotFiltered(pDevice))
|
---|
1160 | return USBDEVICESTATE_USED_BY_HOST_CAPTURABLE;
|
---|
1161 |
|
---|
1162 | /* the device is filtered, or replugging */
|
---|
1163 | if (pDevice->enmState == VBOXUSBFLT_DEVSTATE_REPLUGGING)
|
---|
1164 | {
|
---|
1165 | ASSERT_WARN(!pDevice->pOwner, ("replugging device(0x%p) still has an owner(0x%p)", pDevice, pDevice->pOwner));
|
---|
1166 | ASSERT_WARN(!pDevice->uFltId, ("replugging device(0x%p) still has filter(0x%p)", pDevice, pDevice->uFltId));
|
---|
1167 | /* no user state for this, we should not return it tu the user */
|
---|
1168 | return USBDEVICESTATE_USED_BY_HOST;
|
---|
1169 | }
|
---|
1170 |
|
---|
1171 | /* the device is filtered, if owner differs from the context, return as USED_BY_HOST */
|
---|
1172 | ASSERT_WARN(pDevice->pOwner, ("device(0x%p) has noowner", pDevice));
|
---|
1173 | /* the id can be null if a filter is removed */
|
---|
1174 | // Assert(pDevice->uFltId);
|
---|
1175 |
|
---|
1176 | if (pDevice->pOwner != pContext)
|
---|
1177 | {
|
---|
1178 | LOG(("Device owner differs from the current context, returning used by host"));
|
---|
1179 | return USBDEVICESTATE_USED_BY_HOST;
|
---|
1180 | }
|
---|
1181 |
|
---|
1182 | switch (pDevice->enmState)
|
---|
1183 | {
|
---|
1184 | case VBOXUSBFLT_DEVSTATE_UNCAPTURED:
|
---|
1185 | case VBOXUSBFLT_DEVSTATE_CAPTURING:
|
---|
1186 | return USBDEVICESTATE_USED_BY_HOST_CAPTURABLE;
|
---|
1187 | case VBOXUSBFLT_DEVSTATE_CAPTURED:
|
---|
1188 | return USBDEVICESTATE_HELD_BY_PROXY;
|
---|
1189 | case VBOXUSBFLT_DEVSTATE_USED_BY_GUEST:
|
---|
1190 | return USBDEVICESTATE_USED_BY_GUEST;
|
---|
1191 | default:
|
---|
1192 | WARN(("unexpected device state(%d) for device(0x%p)", pDevice->enmState, pDevice));
|
---|
1193 | return USBDEVICESTATE_UNSUPPORTED;
|
---|
1194 | }
|
---|
1195 | }
|
---|
1196 |
|
---|
1197 | static void vboxUsbDevToUserInfo(PVBOXUSBFLTCTX pContext, PVBOXUSBFLT_DEVICE pDevice, PUSBSUP_DEVINFO pDevInfo)
|
---|
1198 | {
|
---|
1199 | #if 0
|
---|
1200 | pDevInfo->usVendorId = pDevice->idVendor;
|
---|
1201 | pDevInfo->usProductId = pDevice->idProduct;
|
---|
1202 | pDevInfo->usRevision = pDevice->bcdDevice;
|
---|
1203 | pDevInfo->enmState = vboxUsbDevGetUserState(pContext, pDevice);
|
---|
1204 |
|
---|
1205 | strcpy(pDevInfo->szDrvKeyName, pDevice->szDrvKeyName);
|
---|
1206 | if (pDevInfo->enmState == USBDEVICESTATE_HELD_BY_PROXY
|
---|
1207 | || pDevInfo->enmState == USBDEVICESTATE_USED_BY_GUEST)
|
---|
1208 | {
|
---|
1209 | /* this is the only case where we return the obj name to the client */
|
---|
1210 | strcpy(pDevInfo->szObjName, pDevice->szObjName);
|
---|
1211 | }
|
---|
1212 | pDevInfo->fHighSpeed = pDevice->fHighSpeed;
|
---|
1213 | #endif
|
---|
1214 | }
|
---|
1215 |
|
---|
1216 | NTSTATUS VBoxUsbFltGetDevice(PVBOXUSBFLTCTX pContext, HVBOXUSBDEVUSR hDevice, PUSBSUP_GETDEV_MON pInfo)
|
---|
1217 | {
|
---|
1218 | Assert(hDevice);
|
---|
1219 |
|
---|
1220 | memset (pInfo, 0, sizeof (*pInfo));
|
---|
1221 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1222 | for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
|
---|
1223 | pEntry != &g_VBoxUsbFltGlobals.DeviceList;
|
---|
1224 | pEntry = pEntry->Flink)
|
---|
1225 | {
|
---|
1226 | PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
|
---|
1227 | Assert(pDevice->enmState != VBOXUSBFLT_DEVSTATE_REMOVED);
|
---|
1228 | Assert(pDevice->enmState != VBOXUSBFLT_DEVSTATE_ADDED);
|
---|
1229 |
|
---|
1230 | if (pDevice != hDevice)
|
---|
1231 | continue;
|
---|
1232 |
|
---|
1233 | USBDEVICESTATE enmUsrState = vboxUsbDevGetUserState(pContext, pDevice);
|
---|
1234 | pInfo->enmState = enmUsrState;
|
---|
1235 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1236 | return STATUS_SUCCESS;
|
---|
1237 | }
|
---|
1238 |
|
---|
1239 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1240 |
|
---|
1241 | /* this should not occur */
|
---|
1242 | AssertFailed();
|
---|
1243 |
|
---|
1244 | return STATUS_INVALID_PARAMETER;
|
---|
1245 | }
|
---|
1246 |
|
---|
1247 | NTSTATUS VBoxUsbFltPdoAdd(PDEVICE_OBJECT pPdo, BOOLEAN *pbFiltered)
|
---|
1248 | {
|
---|
1249 | *pbFiltered = FALSE;
|
---|
1250 | PVBOXUSBFLT_DEVICE pDevice;
|
---|
1251 |
|
---|
1252 | /* first check if device is in the a already */
|
---|
1253 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1254 | pDevice = vboxUsbFltDevGetLocked(pPdo);
|
---|
1255 | if (pDevice)
|
---|
1256 | {
|
---|
1257 | LOG(("found device (0x%p), state(%d) for PDO(0x%p)", pDevice, pDevice->enmState, pPdo));
|
---|
1258 | ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_ADDED, ("VBOXUSBFLT_DEVSTATE_ADDED state for device(0x%p)", pDevice));
|
---|
1259 | ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_REMOVED, ("VBOXUSBFLT_DEVSTATE_REMOVED state for device(0x%p)", pDevice));
|
---|
1260 | *pbFiltered = pDevice->enmState >= VBOXUSBFLT_DEVSTATE_CAPTURING;
|
---|
1261 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1262 | return STATUS_SUCCESS;
|
---|
1263 | }
|
---|
1264 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1265 | pDevice = (PVBOXUSBFLT_DEVICE)VBoxUsbMonMemAllocZ(sizeof (*pDevice));
|
---|
1266 | if (!pDevice)
|
---|
1267 | {
|
---|
1268 | WARN(("VBoxUsbMonMemAllocZ failed"));
|
---|
1269 | return STATUS_NO_MEMORY;
|
---|
1270 | }
|
---|
1271 |
|
---|
1272 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_ADDED;
|
---|
1273 | pDevice->cRefs = 1;
|
---|
1274 | NTSTATUS Status = vboxUsbFltDevPopulate(pDevice, pPdo /* , TRUE /* need all props */);
|
---|
1275 | if (!NT_SUCCESS(Status))
|
---|
1276 | {
|
---|
1277 | WARN(("vboxUsbFltDevPopulate failed, Status 0x%x", Status));
|
---|
1278 | VBoxUsbMonMemFree(pDevice);
|
---|
1279 | return Status;
|
---|
1280 | }
|
---|
1281 |
|
---|
1282 | uintptr_t uId;
|
---|
1283 | bool fFilter = false;
|
---|
1284 | bool fIsOneShot = false;
|
---|
1285 | PVBOXUSBFLTCTX pCtx;
|
---|
1286 | PVBOXUSBFLT_DEVICE pTmpDev;
|
---|
1287 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1288 | /* (paranoia) re-check the device is still not here */
|
---|
1289 | pTmpDev = vboxUsbFltDevGetLocked(pPdo);
|
---|
1290 | if (pTmpDev)
|
---|
1291 | {
|
---|
1292 | LOG(("second try: found device (0x%p), state(%d) for PDO(0x%p)", pDevice, pDevice->enmState, pPdo));
|
---|
1293 | ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_ADDED, ("second try: VBOXUSBFLT_DEVSTATE_ADDED state for device(0x%p)", pDevice));
|
---|
1294 | ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_REMOVED, ("second try: VBOXUSBFLT_DEVSTATE_REMOVED state for device(0x%p)", pDevice));
|
---|
1295 | *pbFiltered = pTmpDev->enmState >= VBOXUSBFLT_DEVSTATE_CAPTURING;
|
---|
1296 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1297 | VBoxUsbMonMemFree(pDevice);
|
---|
1298 | return STATUS_SUCCESS;
|
---|
1299 | }
|
---|
1300 |
|
---|
1301 | LOG(("Created Device 0x%p for PDO 0x%p", pDevice, pPdo));
|
---|
1302 |
|
---|
1303 | pCtx = vboxUsbFltDevMatchLocked(pDevice, &uId,
|
---|
1304 | true, /* remove a one-shot filter */
|
---|
1305 | &fFilter, &fIsOneShot);
|
---|
1306 | LOG(("Matching Info: Filter (0x%p), pCtx(0x%p), fFilter(%d), fIsOneShot(%d)", uId, pCtx, (int)fFilter, (int)fIsOneShot));
|
---|
1307 | if (fFilter)
|
---|
1308 | {
|
---|
1309 | LOG(("Created Device 0x%p should be filtered", pDevice));
|
---|
1310 | ASSERT_WARN(pCtx, ("zero ctx"));
|
---|
1311 | ASSERT_WARN(uId, ("zero uId"));
|
---|
1312 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_CAPTURING;
|
---|
1313 | }
|
---|
1314 | else
|
---|
1315 | {
|
---|
1316 | LOG(("Created Device 0x%p should NOT be filtered", pDevice));
|
---|
1317 | ASSERT_WARN(!uId == !pCtx, ("invalid uid(0x%p) - ctx(0x%p) pair", uId, pCtx)); /* either both zero or both not */
|
---|
1318 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_UNCAPTURED;
|
---|
1319 | }
|
---|
1320 |
|
---|
1321 | if (pCtx)
|
---|
1322 | vboxUsbFltDevOwnerSetLocked(pDevice, pCtx, fIsOneShot ? 0 : uId, fIsOneShot);
|
---|
1323 |
|
---|
1324 | InsertHeadList(&g_VBoxUsbFltGlobals.DeviceList, &pDevice->GlobalLe);
|
---|
1325 |
|
---|
1326 | /* do not need to signal anything here -
|
---|
1327 | * going to do that once the proxy device object starts */
|
---|
1328 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1329 |
|
---|
1330 | *pbFiltered = fFilter;
|
---|
1331 |
|
---|
1332 | return STATUS_SUCCESS;
|
---|
1333 | }
|
---|
1334 |
|
---|
1335 | NTSTATUS VBoxUsbFltPdoAddCompleted(PDEVICE_OBJECT pPdo)
|
---|
1336 | {
|
---|
1337 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1338 | vboxUsbFltSignalChangeLocked();
|
---|
1339 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1340 | return STATUS_SUCCESS;
|
---|
1341 | }
|
---|
1342 |
|
---|
1343 | BOOLEAN VBoxUsbFltPdoIsFiltered(PDEVICE_OBJECT pPdo)
|
---|
1344 | {
|
---|
1345 | VBOXUSBFLT_DEVSTATE enmState = VBOXUSBFLT_DEVSTATE_REMOVED;
|
---|
1346 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1347 | PVBOXUSBFLT_DEVICE pDevice = vboxUsbFltDevGetLocked(pPdo);
|
---|
1348 | if (pDevice)
|
---|
1349 | {
|
---|
1350 | enmState = pDevice->enmState;
|
---|
1351 | }
|
---|
1352 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1353 |
|
---|
1354 | return enmState >= VBOXUSBFLT_DEVSTATE_CAPTURING;
|
---|
1355 | }
|
---|
1356 |
|
---|
1357 | NTSTATUS VBoxUsbFltPdoRemove(PDEVICE_OBJECT pPdo)
|
---|
1358 | {
|
---|
1359 | PVBOXUSBFLT_DEVICE pDevice;
|
---|
1360 | VBOXUSBFLT_DEVSTATE enmOldState;
|
---|
1361 |
|
---|
1362 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1363 | pDevice = vboxUsbFltDevGetLocked(pPdo);
|
---|
1364 | if (pDevice)
|
---|
1365 | {
|
---|
1366 | RemoveEntryList(&pDevice->GlobalLe);
|
---|
1367 | enmOldState = pDevice->enmState;
|
---|
1368 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_REMOVED;
|
---|
1369 | if (enmOldState != VBOXUSBFLT_DEVSTATE_REPLUGGING)
|
---|
1370 | {
|
---|
1371 | vboxUsbFltSignalChangeLocked();
|
---|
1372 | }
|
---|
1373 | else
|
---|
1374 | {
|
---|
1375 | /* the device *should* reappear, do signlling on re-appear only
|
---|
1376 | * to avoid extra signaling. still there might be a situation
|
---|
1377 | * when the device will not re-appear if it gets physically removed
|
---|
1378 | * before it re-appears
|
---|
1379 | * @todo: set a timer callback to do a notification from it */
|
---|
1380 | }
|
---|
1381 | }
|
---|
1382 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1383 | if (pDevice)
|
---|
1384 | vboxUsbFltDevRelease(pDevice);
|
---|
1385 | return STATUS_SUCCESS;
|
---|
1386 | }
|
---|
1387 |
|
---|
1388 | HVBOXUSBFLTDEV VBoxUsbFltProxyStarted(PDEVICE_OBJECT pPdo)
|
---|
1389 | {
|
---|
1390 | PVBOXUSBFLT_DEVICE pDevice;
|
---|
1391 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1392 | pDevice = vboxUsbFltDevGetLocked(pPdo);
|
---|
1393 | if (pDevice->enmState = VBOXUSBFLT_DEVSTATE_CAPTURING)
|
---|
1394 | {
|
---|
1395 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_CAPTURED;
|
---|
1396 | LOG(("The proxy notified proxy start for the captured device 0x%x\n", pDevice));
|
---|
1397 | vboxUsbFltDevRetain(pDevice);
|
---|
1398 | vboxUsbFltSignalChangeLocked();
|
---|
1399 | }
|
---|
1400 | else
|
---|
1401 | {
|
---|
1402 | WARN(("invalid state, %d", pDevice->enmState));
|
---|
1403 | pDevice = NULL;
|
---|
1404 | }
|
---|
1405 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1406 | return pDevice;
|
---|
1407 | }
|
---|
1408 |
|
---|
1409 | void VBoxUsbFltProxyStopped(HVBOXUSBFLTDEV hDev)
|
---|
1410 | {
|
---|
1411 | PVBOXUSBFLT_DEVICE pDevice = (PVBOXUSBFLT_DEVICE)hDev;
|
---|
1412 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1413 | if (pDevice->enmState == VBOXUSBFLT_DEVSTATE_CAPTURED
|
---|
1414 | || pDevice->enmState == VBOXUSBFLT_DEVSTATE_USED_BY_GUEST)
|
---|
1415 | {
|
---|
1416 | /* this is due to devie was physically removed */
|
---|
1417 | LOG(("The proxy notified proxy stop for the captured device 0x%x, current state %d\n", pDevice, pDevice->enmState));
|
---|
1418 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_CAPTURING;
|
---|
1419 | vboxUsbFltSignalChangeLocked();
|
---|
1420 | }
|
---|
1421 | else
|
---|
1422 | {
|
---|
1423 | if(pDevice->enmState != VBOXUSBFLT_DEVSTATE_REPLUGGING)
|
---|
1424 | {
|
---|
1425 | WARN(("invalid state, %d", pDevice->enmState));
|
---|
1426 | }
|
---|
1427 | }
|
---|
1428 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1429 |
|
---|
1430 | vboxUsbFltDevRelease(pDevice);
|
---|
1431 | }
|
---|
1432 |
|
---|
1433 | NTSTATUS VBoxUsbFltInit()
|
---|
1434 | {
|
---|
1435 | int rc = VBoxUSBFilterInit();
|
---|
1436 | if (RT_FAILURE(rc))
|
---|
1437 | {
|
---|
1438 | WARN(("VBoxUSBFilterInit failed, rc (%d)", rc));
|
---|
1439 | return STATUS_UNSUCCESSFUL;
|
---|
1440 | }
|
---|
1441 |
|
---|
1442 | memset(&g_VBoxUsbFltGlobals, 0, sizeof (g_VBoxUsbFltGlobals));
|
---|
1443 | InitializeListHead(&g_VBoxUsbFltGlobals.DeviceList);
|
---|
1444 | InitializeListHead(&g_VBoxUsbFltGlobals.ContextList);
|
---|
1445 | InitializeListHead(&g_VBoxUsbFltGlobals.BlackDeviceList);
|
---|
1446 | vboxUsbFltBlDevPopulateWithKnownLocked();
|
---|
1447 | VBOXUSBFLT_LOCK_INIT();
|
---|
1448 | return STATUS_SUCCESS;
|
---|
1449 | }
|
---|
1450 |
|
---|
1451 | NTSTATUS VBoxUsbFltTerm()
|
---|
1452 | {
|
---|
1453 | bool bBusy = false;
|
---|
1454 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1455 | do
|
---|
1456 | {
|
---|
1457 | if (!IsListEmpty(&g_VBoxUsbFltGlobals.ContextList))
|
---|
1458 | {
|
---|
1459 | AssertFailed();
|
---|
1460 | bBusy = true;
|
---|
1461 | break;
|
---|
1462 | }
|
---|
1463 |
|
---|
1464 | PLIST_ENTRY pNext = NULL;
|
---|
1465 | for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
|
---|
1466 | pEntry != &g_VBoxUsbFltGlobals.DeviceList;
|
---|
1467 | pEntry = pNext)
|
---|
1468 | {
|
---|
1469 | pNext = pEntry->Flink;
|
---|
1470 | PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
|
---|
1471 | Assert(!pDevice->uFltId);
|
---|
1472 | Assert(!pDevice->pOwner);
|
---|
1473 | if (pDevice->cRefs != 1)
|
---|
1474 | {
|
---|
1475 | AssertFailed();
|
---|
1476 | bBusy = true;
|
---|
1477 | break;
|
---|
1478 | }
|
---|
1479 | }
|
---|
1480 | } while (0);
|
---|
1481 |
|
---|
1482 | VBOXUSBFLT_LOCK_RELEASE()
|
---|
1483 |
|
---|
1484 | if (bBusy)
|
---|
1485 | {
|
---|
1486 | return STATUS_DEVICE_BUSY;
|
---|
1487 | }
|
---|
1488 |
|
---|
1489 | for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
|
---|
1490 | pEntry != &g_VBoxUsbFltGlobals.DeviceList;
|
---|
1491 | pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink)
|
---|
1492 | {
|
---|
1493 | RemoveEntryList(pEntry);
|
---|
1494 | PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
|
---|
1495 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_REMOVED;
|
---|
1496 | vboxUsbFltDevRelease(pDevice);
|
---|
1497 | }
|
---|
1498 |
|
---|
1499 | vboxUsbFltBlDevClearLocked();
|
---|
1500 |
|
---|
1501 | VBOXUSBFLT_LOCK_TERM();
|
---|
1502 |
|
---|
1503 | VBoxUSBFilterTerm();
|
---|
1504 |
|
---|
1505 | return STATUS_SUCCESS;
|
---|
1506 | }
|
---|
1507 |
|
---|