1 | /* $Id: VBoxUsbFlt.cpp 48874 2013-10-04 05:07:40Z 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 | /*
|
---|
329 | * Prevent a host crash when vboxUsbFltDevGetLocked fails to locate the matching PDO
|
---|
330 | * in g_VBoxUsbFltGlobals.DeviceList (see @bugref{6509}).
|
---|
331 | */
|
---|
332 | if (pDevice == NULL)
|
---|
333 | {
|
---|
334 | WARN(("failed to get device for PDO(0x%p)", pPdo));
|
---|
335 | }
|
---|
336 | else if (pDevice->enmState > VBOXUSBFLT_DEVSTATE_ADDED)
|
---|
337 | {
|
---|
338 | vboxUsbFltDevRetain(pDevice);
|
---|
339 | LOG(("found device (0x%p), state(%d) for PDO(0x%p)", pDevice, pDevice->enmState, pPdo));
|
---|
340 | }
|
---|
341 | else
|
---|
342 | {
|
---|
343 | LOG(("found replugging device (0x%p), state(%d) for PDO(0x%p)", pDevice, pDevice->enmState, pPdo));
|
---|
344 | pDevice = NULL;
|
---|
345 | }
|
---|
346 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
347 |
|
---|
348 | return pDevice;
|
---|
349 | }
|
---|
350 |
|
---|
351 | static NTSTATUS vboxUsbFltPdoReplug(PDEVICE_OBJECT pDo)
|
---|
352 | {
|
---|
353 | LOG(("Replugging PDO(0x%p)", pDo));
|
---|
354 | NTSTATUS Status = VBoxUsbToolIoInternalCtlSendSync(pDo, IOCTL_INTERNAL_USB_CYCLE_PORT, NULL, NULL);
|
---|
355 | ASSERT_WARN(Status == STATUS_SUCCESS, ("replugging PDO(0x%p) failed Status(0x%x)", pDo, Status));
|
---|
356 | LOG(("Replugging PDO(0x%p) done with Status(0x%x)", pDo, Status));
|
---|
357 | return Status;
|
---|
358 | }
|
---|
359 |
|
---|
360 | static bool vboxUsbFltDevCanBeCaptured(PVBOXUSBFLT_DEVICE pDevice)
|
---|
361 | {
|
---|
362 | if (pDevice->bClass == USB_DEVICE_CLASS_HUB)
|
---|
363 | {
|
---|
364 | LOG(("device (0x%p), pdo (0x%p) is a hub, can not be captured", pDevice, pDevice->Pdo));
|
---|
365 | return false;
|
---|
366 | }
|
---|
367 | return true;
|
---|
368 | }
|
---|
369 |
|
---|
370 | static PVBOXUSBFLTCTX vboxUsbFltDevMatchLocked(PVBOXUSBFLT_DEVICE pDevice, uintptr_t *puId, bool fRemoveFltIfOneShot, bool *pfFilter, bool *pfIsOneShot)
|
---|
371 | {
|
---|
372 | *puId = 0;
|
---|
373 | *pfFilter = false;
|
---|
374 | *pfIsOneShot = false;
|
---|
375 | if (!vboxUsbFltDevCanBeCaptured(pDevice))
|
---|
376 | {
|
---|
377 | LOG(("vboxUsbFltDevCanBeCaptured returned false"));
|
---|
378 | return NULL;
|
---|
379 | }
|
---|
380 |
|
---|
381 | USBFILTER DevFlt;
|
---|
382 | USBFilterInit(&DevFlt, USBFILTERTYPE_CAPTURE);
|
---|
383 | USBFilterSetNumExact(&DevFlt, USBFILTERIDX_VENDOR_ID, pDevice->idVendor, true);
|
---|
384 | USBFilterSetNumExact(&DevFlt, USBFILTERIDX_PRODUCT_ID, pDevice->idProduct, true);
|
---|
385 | USBFilterSetNumExact(&DevFlt, USBFILTERIDX_DEVICE_REV, pDevice->bcdDevice, true);
|
---|
386 | USBFilterSetNumExact(&DevFlt, USBFILTERIDX_DEVICE_CLASS, pDevice->bClass, true);
|
---|
387 | USBFilterSetNumExact(&DevFlt, USBFILTERIDX_DEVICE_SUB_CLASS, pDevice->bSubClass, true);
|
---|
388 | USBFilterSetNumExact(&DevFlt, USBFILTERIDX_DEVICE_PROTOCOL, pDevice->bProtocol, true);
|
---|
389 | USBFilterSetStringExact(&DevFlt, USBFILTERIDX_MANUFACTURER_STR, pDevice->szMfgName, true);
|
---|
390 | USBFilterSetStringExact(&DevFlt, USBFILTERIDX_PRODUCT_STR, pDevice->szProduct, true);
|
---|
391 | USBFilterSetStringExact(&DevFlt, USBFILTERIDX_SERIAL_NUMBER_STR, pDevice->szSerial, true);
|
---|
392 |
|
---|
393 | /* Run filters on the thing. */
|
---|
394 | PVBOXUSBFLTCTX pOwner = VBoxUSBFilterMatchEx(&DevFlt, puId, fRemoveFltIfOneShot, pfFilter, pfIsOneShot);
|
---|
395 | USBFilterDelete(&DevFlt);
|
---|
396 | return pOwner;
|
---|
397 | }
|
---|
398 |
|
---|
399 | static void vboxUsbFltDevStateMarkReplugLocked(PVBOXUSBFLT_DEVICE pDevice)
|
---|
400 | {
|
---|
401 | vboxUsbFltDevOwnerUpdateLocked(pDevice, NULL, 0, false);
|
---|
402 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_REPLUGGING;
|
---|
403 | }
|
---|
404 |
|
---|
405 | static bool vboxUsbFltDevStateIsNotFiltered(PVBOXUSBFLT_DEVICE pDevice)
|
---|
406 | {
|
---|
407 | return pDevice->enmState == VBOXUSBFLT_DEVSTATE_UNCAPTURED;
|
---|
408 | }
|
---|
409 |
|
---|
410 | static bool vboxUsbFltDevStateIsFiltered(PVBOXUSBFLT_DEVICE pDevice)
|
---|
411 | {
|
---|
412 | return pDevice->enmState >= VBOXUSBFLT_DEVSTATE_CAPTURING;
|
---|
413 | }
|
---|
414 |
|
---|
415 | #define VBOXUSBMON_POPULATE_REQUEST_TIMEOUT_MS 10000
|
---|
416 |
|
---|
417 | static NTSTATUS vboxUsbFltDevPopulate(PVBOXUSBFLT_DEVICE pDevice, PDEVICE_OBJECT pDo /*, BOOLEAN bPopulateNonFilterProps*/)
|
---|
418 | {
|
---|
419 | NTSTATUS Status;
|
---|
420 | PUSB_DEVICE_DESCRIPTOR pDevDr = 0;
|
---|
421 |
|
---|
422 | pDevice->Pdo = pDo;
|
---|
423 |
|
---|
424 | LOG(("Populating Device(0x%p) for PDO(0x%p)", pDevice, pDo));
|
---|
425 |
|
---|
426 | pDevDr = (PUSB_DEVICE_DESCRIPTOR)VBoxUsbMonMemAllocZ(sizeof(*pDevDr));
|
---|
427 | if (pDevDr == NULL)
|
---|
428 | {
|
---|
429 | WARN(("Failed to alloc mem for urb"));
|
---|
430 | return STATUS_INSUFFICIENT_RESOURCES;
|
---|
431 | }
|
---|
432 |
|
---|
433 | do
|
---|
434 | {
|
---|
435 | Status = VBoxUsbToolGetDescriptor(pDo, pDevDr, sizeof(*pDevDr), USB_DEVICE_DESCRIPTOR_TYPE, 0, 0, VBOXUSBMON_POPULATE_REQUEST_TIMEOUT_MS);
|
---|
436 | if (!NT_SUCCESS(Status))
|
---|
437 | {
|
---|
438 | WARN(("getting device descriptor failed, Status (0x%x)", Status));
|
---|
439 | break;
|
---|
440 | }
|
---|
441 |
|
---|
442 | if (vboxUsbFltBlDevMatchLocked(pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice))
|
---|
443 | {
|
---|
444 | WARN(("found a known black list device, vid(0x%x), pid(0x%x), rev(0x%x)", pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice));
|
---|
445 | Status = STATUS_UNSUCCESSFUL;
|
---|
446 | break;
|
---|
447 | }
|
---|
448 |
|
---|
449 | LOG(("Device pid=%x vid=%x rev=%x", pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice));
|
---|
450 | pDevice->idVendor = pDevDr->idVendor;
|
---|
451 | pDevice->idProduct = pDevDr->idProduct;
|
---|
452 | pDevice->bcdDevice = pDevDr->bcdDevice;
|
---|
453 | pDevice->bClass = pDevDr->bDeviceClass;
|
---|
454 | pDevice->bSubClass = pDevDr->bDeviceSubClass;
|
---|
455 | pDevice->bProtocol = pDevDr->bDeviceProtocol;
|
---|
456 | pDevice->szSerial[0] = 0;
|
---|
457 | pDevice->szMfgName[0] = 0;
|
---|
458 | pDevice->szProduct[0] = 0;
|
---|
459 |
|
---|
460 | /* If there are no strings, don't even try to get any string descriptors. */
|
---|
461 | if (pDevDr->iSerialNumber || pDevDr->iManufacturer || pDevDr->iProduct)
|
---|
462 | {
|
---|
463 | int langId;
|
---|
464 |
|
---|
465 | Status = VBoxUsbToolGetLangID(pDo, &langId, VBOXUSBMON_POPULATE_REQUEST_TIMEOUT_MS);
|
---|
466 | if (!NT_SUCCESS(Status))
|
---|
467 | {
|
---|
468 | WARN(("reading language ID failed"));
|
---|
469 | if (Status == STATUS_CANCELLED)
|
---|
470 | {
|
---|
471 | WARN(("found a new black list device, vid(0x%x), pid(0x%x), rev(0x%x)", pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice));
|
---|
472 | vboxUsbFltBlDevAddLocked(pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice);
|
---|
473 | Status = STATUS_UNSUCCESSFUL;
|
---|
474 | }
|
---|
475 | break;
|
---|
476 | }
|
---|
477 |
|
---|
478 | if (pDevDr->iSerialNumber)
|
---|
479 | {
|
---|
480 | Status = VBoxUsbToolGetStringDescriptorA(pDo, pDevice->szSerial, sizeof (pDevice->szSerial), pDevDr->iSerialNumber, langId, VBOXUSBMON_POPULATE_REQUEST_TIMEOUT_MS);
|
---|
481 | if (!NT_SUCCESS(Status))
|
---|
482 | {
|
---|
483 | WARN(("reading serial number failed"));
|
---|
484 | ASSERT_WARN(pDevice->szSerial[0] == '\0', ("serial is not zero!!"));
|
---|
485 | if (Status == STATUS_CANCELLED)
|
---|
486 | {
|
---|
487 | WARN(("found a new black list device, vid(0x%x), pid(0x%x), rev(0x%x)", pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice));
|
---|
488 | vboxUsbFltBlDevAddLocked(pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice);
|
---|
489 | Status = STATUS_UNSUCCESSFUL;
|
---|
490 | break;
|
---|
491 | }
|
---|
492 | LOG(("pretending success.."));
|
---|
493 | Status = STATUS_SUCCESS;
|
---|
494 | }
|
---|
495 | }
|
---|
496 |
|
---|
497 | if (pDevDr->iManufacturer)
|
---|
498 | {
|
---|
499 | Status = VBoxUsbToolGetStringDescriptorA(pDo, pDevice->szMfgName, sizeof (pDevice->szMfgName), pDevDr->iManufacturer, langId, VBOXUSBMON_POPULATE_REQUEST_TIMEOUT_MS);
|
---|
500 | if (!NT_SUCCESS(Status))
|
---|
501 | {
|
---|
502 | WARN(("reading manufacturer name failed"));
|
---|
503 | ASSERT_WARN(pDevice->szMfgName[0] == '\0', ("szMfgName is not zero!!"));
|
---|
504 | if (Status == STATUS_CANCELLED)
|
---|
505 | {
|
---|
506 | WARN(("found a new black list device, vid(0x%x), pid(0x%x), rev(0x%x)", pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice));
|
---|
507 | vboxUsbFltBlDevAddLocked(pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice);
|
---|
508 | Status = STATUS_UNSUCCESSFUL;
|
---|
509 | break;
|
---|
510 | }
|
---|
511 | LOG(("pretending success.."));
|
---|
512 | Status = STATUS_SUCCESS;
|
---|
513 | }
|
---|
514 | }
|
---|
515 |
|
---|
516 | if (pDevDr->iProduct)
|
---|
517 | {
|
---|
518 | Status = VBoxUsbToolGetStringDescriptorA(pDo, pDevice->szProduct, sizeof (pDevice->szProduct), pDevDr->iProduct, langId, VBOXUSBMON_POPULATE_REQUEST_TIMEOUT_MS);
|
---|
519 | if (!NT_SUCCESS(Status))
|
---|
520 | {
|
---|
521 | WARN(("reading product name failed"));
|
---|
522 | ASSERT_WARN(pDevice->szProduct[0] == '\0', ("szProduct is not zero!!"));
|
---|
523 | if (Status == STATUS_CANCELLED)
|
---|
524 | {
|
---|
525 | WARN(("found a new black list device, vid(0x%x), pid(0x%x), rev(0x%x)", pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice));
|
---|
526 | vboxUsbFltBlDevAddLocked(pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice);
|
---|
527 | Status = STATUS_UNSUCCESSFUL;
|
---|
528 | break;
|
---|
529 | }
|
---|
530 | LOG(("pretending success.."));
|
---|
531 | Status = STATUS_SUCCESS;
|
---|
532 | }
|
---|
533 | }
|
---|
534 |
|
---|
535 | #if 0
|
---|
536 | if (bPopulateNonFilterProps)
|
---|
537 | {
|
---|
538 | WCHAR RegKeyBuf[512];
|
---|
539 | ULONG cbRegKeyBuf = sizeof (RegKeyBuf);
|
---|
540 | Status = IoGetDeviceProperty(pDo,
|
---|
541 | DevicePropertyDriverKeyName,
|
---|
542 | cbRegKeyBuf,
|
---|
543 | RegKeyBuf,
|
---|
544 | &cbRegKeyBuf);
|
---|
545 | if (!NT_SUCCESS(Status))
|
---|
546 | {
|
---|
547 | AssertMsgFailed((__FUNCTION__": IoGetDeviceProperty failed Status (0x%x)", Status));
|
---|
548 | break;
|
---|
549 | }
|
---|
550 |
|
---|
551 | ANSI_STRING Ansi;
|
---|
552 | UNICODE_STRING Unicode;
|
---|
553 | Ansi.Buffer = pDevice->szDrvKeyName;
|
---|
554 | Ansi.Length = 0;
|
---|
555 | Ansi.MaximumLength = sizeof(pDevice->szDrvKeyName);
|
---|
556 | RtlInitUnicodeString(&Unicode, RegKeyBuf);
|
---|
557 |
|
---|
558 | Status = RtlUnicodeStringToAnsiString(&Ansi, &Unicode, FALSE /* do not allocate */);
|
---|
559 | if (!NT_SUCCESS(Status))
|
---|
560 | {
|
---|
561 | AssertMsgFailed((__FUNCTION__": RtlUnicodeStringToAnsiString failed Status (0x%x)", Status));
|
---|
562 | break;
|
---|
563 | }
|
---|
564 |
|
---|
565 | pDevice->fHighSpend = FALSE;
|
---|
566 | Status = VBoxUsbToolGetDeviceSpeed(pDo, &pDevice->fHighSpend);
|
---|
567 | if (!NT_SUCCESS(Status))
|
---|
568 | {
|
---|
569 | AssertMsgFailed((__FUNCTION__": VBoxUsbToolGetDeviceSpeed failed Status (0x%x)", Status));
|
---|
570 | break;
|
---|
571 | }
|
---|
572 | }
|
---|
573 | #endif
|
---|
574 | LOG((": strings: '%s':'%s':'%s' (lang ID %x)",
|
---|
575 | pDevice->szMfgName, pDevice->szProduct, pDevice->szSerial, langId));
|
---|
576 | }
|
---|
577 |
|
---|
578 | LOG(("Populating Device(0x%p) for PDO(0x%p) Succeeded", pDevice, pDo));
|
---|
579 | Status = STATUS_SUCCESS;
|
---|
580 | } while (0);
|
---|
581 |
|
---|
582 | VBoxUsbMonMemFree(pDevDr);
|
---|
583 | LOG(("Populating Device(0x%p) for PDO(0x%p) Done, Status (0x%x)", pDevice, pDo, Status));
|
---|
584 | return Status;
|
---|
585 | }
|
---|
586 |
|
---|
587 | static void vboxUsbFltSignalChangeLocked()
|
---|
588 | {
|
---|
589 | for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.ContextList.Flink;
|
---|
590 | pEntry != &g_VBoxUsbFltGlobals.ContextList;
|
---|
591 | pEntry = pEntry->Flink)
|
---|
592 | {
|
---|
593 | PVBOXUSBFLTCTX pCtx = PVBOXUSBFLTCTX_FROM_LE(pEntry);
|
---|
594 | /* the removed context can not be in a list */
|
---|
595 | Assert(!pCtx->bRemoved);
|
---|
596 | if (pCtx->pChangeEvent)
|
---|
597 | {
|
---|
598 | KeSetEvent(pCtx->pChangeEvent,
|
---|
599 | 0, /* increment*/
|
---|
600 | FALSE /* wait */);
|
---|
601 | }
|
---|
602 | }
|
---|
603 | }
|
---|
604 |
|
---|
605 | static bool vboxUsbFltDevCheckReplugLocked(PVBOXUSBFLT_DEVICE pDevice, PVBOXUSBFLTCTX pContext)
|
---|
606 | {
|
---|
607 | ASSERT_WARN(pContext, ("context is NULL!"));
|
---|
608 |
|
---|
609 | LOG(("Current context is (0x%p)", pContext));
|
---|
610 | LOG(("Current Device owner is (0x%p)", pDevice->pOwner));
|
---|
611 |
|
---|
612 | /* check if device is already replugging */
|
---|
613 | if (pDevice->enmState <= VBOXUSBFLT_DEVSTATE_ADDED)
|
---|
614 | {
|
---|
615 | LOG(("Device (0x%p) is already replugging, return..", pDevice));
|
---|
616 | /* it is, do nothing */
|
---|
617 | ASSERT_WARN(pDevice->enmState == VBOXUSBFLT_DEVSTATE_REPLUGGING,
|
---|
618 | ("Device (0x%p) state is NOT REPLUGGING (%d)", pDevice, pDevice->enmState));
|
---|
619 | return false;
|
---|
620 | }
|
---|
621 |
|
---|
622 | if (pDevice->pOwner && pContext != pDevice->pOwner)
|
---|
623 | {
|
---|
624 | LOG(("Device (0x%p) is owned by another context(0x%p), current is(0x%p)", pDevice, pDevice->pOwner, pContext));
|
---|
625 | /* this device is owned by another context, we're not allowed to do anything */
|
---|
626 | return false;
|
---|
627 | }
|
---|
628 |
|
---|
629 | uintptr_t uId = 0;
|
---|
630 | bool bNeedReplug = false;
|
---|
631 | bool fFilter = false;
|
---|
632 | bool fIsOneShot = false;
|
---|
633 | PVBOXUSBFLTCTX pNewOwner = vboxUsbFltDevMatchLocked(pDevice, &uId,
|
---|
634 | false, /* do not remove a one-shot filter */
|
---|
635 | &fFilter, &fIsOneShot);
|
---|
636 | LOG(("Matching Info: Filter (0x%p), NewOwner(0x%p), fFilter(%d), fIsOneShot(%d)", uId, pNewOwner, (int)fFilter, (int)fIsOneShot));
|
---|
637 | if (pDevice->pOwner && pNewOwner && pDevice->pOwner != pNewOwner)
|
---|
638 | {
|
---|
639 | LOG(("Matching: Device (0x%p) is requested another owner(0x%p), current is(0x%p)", pDevice, pNewOwner, pDevice->pOwner));
|
---|
640 | /* the device is owned by another owner, we can not change the owner here */
|
---|
641 | return false;
|
---|
642 | }
|
---|
643 |
|
---|
644 | if (!fFilter)
|
---|
645 | {
|
---|
646 | LOG(("Matching: Device (0x%p) should NOT be filtered", pDevice));
|
---|
647 | /* the device should NOT be filtered, check the current state */
|
---|
648 | if (vboxUsbFltDevStateIsNotFiltered(pDevice))
|
---|
649 | {
|
---|
650 | LOG(("Device (0x%p) is NOT filtered", pDevice));
|
---|
651 | /* no changes */
|
---|
652 | if (fIsOneShot)
|
---|
653 | {
|
---|
654 | ASSERT_WARN(pNewOwner, ("no new owner"));
|
---|
655 | LOG(("Matching: This is a one-shot filter (0x%p), removing..", uId));
|
---|
656 | /* remove a one-shot filter and keep the original filter data */
|
---|
657 | int tmpRc = VBoxUSBFilterRemove(pNewOwner, uId);
|
---|
658 | ASSERT_WARN(RT_SUCCESS(tmpRc), ("remove filter failed, rc (%d)", tmpRc));
|
---|
659 | if (!pDevice->pOwner)
|
---|
660 | {
|
---|
661 | LOG(("Matching: updating the one-shot owner to (0x%p), fltId(0x%p)", pNewOwner, uId));
|
---|
662 | /* update owner for one-shot if the owner is changed (i.e. assigned) */
|
---|
663 | vboxUsbFltDevOwnerUpdateLocked(pDevice, pNewOwner, uId, true);
|
---|
664 | }
|
---|
665 | else
|
---|
666 | {
|
---|
667 | LOG(("Matching: device already has owner (0x%p) assigned", pDevice->pOwner));
|
---|
668 | }
|
---|
669 | }
|
---|
670 | else
|
---|
671 | {
|
---|
672 | LOG(("Matching: This is NOT a one-shot filter (0x%p), newOwner(0x%p)", uId, pNewOwner));
|
---|
673 | if (pNewOwner)
|
---|
674 | {
|
---|
675 | vboxUsbFltDevOwnerUpdateLocked(pDevice, pNewOwner, uId, false);
|
---|
676 | }
|
---|
677 | }
|
---|
678 | }
|
---|
679 | else
|
---|
680 | {
|
---|
681 | LOG(("Device (0x%p) IS filtered", pDevice));
|
---|
682 | /* the device is currently filtered, we should release it only if
|
---|
683 | * 1. device does not have an owner
|
---|
684 | * or
|
---|
685 | * 2. it should be released bue to a one-shot filter
|
---|
686 | * or
|
---|
687 | * 3. it is NOT grabbed by a one-shot filter */
|
---|
688 | if (!pDevice->pOwner || fIsOneShot || !pDevice->fIsFilterOneShot)
|
---|
689 | {
|
---|
690 | LOG(("Matching: Need replug"));
|
---|
691 | bNeedReplug = true;
|
---|
692 | }
|
---|
693 | }
|
---|
694 | }
|
---|
695 | else
|
---|
696 | {
|
---|
697 | LOG(("Matching: Device (0x%p) SHOULD be filtered", pDevice));
|
---|
698 | /* the device should be filtered, check the current state */
|
---|
699 | ASSERT_WARN(uId, ("zero uid"));
|
---|
700 | ASSERT_WARN(pNewOwner, ("zero pNewOwner"));
|
---|
701 | if (vboxUsbFltDevStateIsFiltered(pDevice))
|
---|
702 | {
|
---|
703 | LOG(("Device (0x%p) IS filtered", pDevice));
|
---|
704 | /* the device is filtered */
|
---|
705 | if (pNewOwner == pDevice->pOwner)
|
---|
706 | {
|
---|
707 | LOG(("Device owner match"));
|
---|
708 | /* no changes */
|
---|
709 | if (fIsOneShot)
|
---|
710 | {
|
---|
711 | LOG(("Matching: This is a one-shot filter (0x%p), removing..", uId));
|
---|
712 | /* remove a one-shot filter and keep the original filter data */
|
---|
713 | int tmpRc = VBoxUSBFilterRemove(pNewOwner, uId);
|
---|
714 | ASSERT_WARN(RT_SUCCESS(tmpRc), ("remove filter failed, rc (%d)", tmpRc));
|
---|
715 | }
|
---|
716 | else
|
---|
717 | {
|
---|
718 | LOG(("Matching: This is NOT a one-shot filter (0x%p), Owner(0x%p)", uId, pDevice->pOwner));
|
---|
719 | vboxUsbFltDevOwnerUpdateLocked(pDevice, pDevice->pOwner, uId, false);
|
---|
720 | }
|
---|
721 | }
|
---|
722 | else
|
---|
723 | {
|
---|
724 | ASSERT_WARN(!pDevice->pOwner, ("device should NOT have owner"));
|
---|
725 | LOG(("Matching: Need replug"));
|
---|
726 | /* the device needs to be filtered, but the owner changes, replug needed */
|
---|
727 | bNeedReplug = true;
|
---|
728 | }
|
---|
729 | }
|
---|
730 | else
|
---|
731 | {
|
---|
732 | /* the device is currently NOT filtered,
|
---|
733 | * we should replug it only if
|
---|
734 | * 1. device does not have an owner
|
---|
735 | * or
|
---|
736 | * 2. it should be captured due to a one-shot filter
|
---|
737 | * or
|
---|
738 | * 3. it is NOT released by a one-shot filter */
|
---|
739 | if (!pDevice->pOwner || fIsOneShot || !pDevice->fIsFilterOneShot)
|
---|
740 | {
|
---|
741 | bNeedReplug = true;
|
---|
742 | LOG(("Matching: Need replug"));
|
---|
743 | }
|
---|
744 | }
|
---|
745 | }
|
---|
746 |
|
---|
747 | if (bNeedReplug)
|
---|
748 | {
|
---|
749 | LOG(("Matching: Device needs replugging, marking as such"));
|
---|
750 | vboxUsbFltDevStateMarkReplugLocked(pDevice);
|
---|
751 | }
|
---|
752 | else
|
---|
753 | {
|
---|
754 | LOG(("Matching: Device does NOT need replugging"));
|
---|
755 | }
|
---|
756 |
|
---|
757 | return bNeedReplug;
|
---|
758 | }
|
---|
759 |
|
---|
760 | static void vboxUsbFltReplugList(PLIST_ENTRY pList)
|
---|
761 | {
|
---|
762 | PLIST_ENTRY pNext;
|
---|
763 | for (PLIST_ENTRY pEntry = pList->Flink;
|
---|
764 | pEntry != pList;
|
---|
765 | pEntry = pNext)
|
---|
766 | {
|
---|
767 | pNext = pEntry->Flink;
|
---|
768 | PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_REPLUGGINGLE(pEntry);
|
---|
769 | LOG(("replugging matched PDO(0x%p), pDevice(0x%p)", pDevice->Pdo, pDevice));
|
---|
770 | ASSERT_WARN(pDevice->enmState == VBOXUSBFLT_DEVSTATE_REPLUGGING
|
---|
771 | || pDevice->enmState == VBOXUSBFLT_DEVSTATE_REMOVED,
|
---|
772 | ("invalid state(0x%x) for device(0x%p)", pDevice->enmState, pDevice));
|
---|
773 |
|
---|
774 | vboxUsbFltPdoReplug(pDevice->Pdo);
|
---|
775 | ObDereferenceObject(pDevice->Pdo);
|
---|
776 | vboxUsbFltDevRelease(pDevice);
|
---|
777 | }
|
---|
778 | }
|
---|
779 |
|
---|
780 | typedef struct VBOXUSBFLTCHECKWALKER
|
---|
781 | {
|
---|
782 | PVBOXUSBFLTCTX pContext;
|
---|
783 | } VBOXUSBFLTCHECKWALKER, *PVBOXUSBFLTCHECKWALKER;
|
---|
784 |
|
---|
785 | static DECLCALLBACK(BOOLEAN) vboxUsbFltFilterCheckWalker(PFILE_OBJECT pFile, PDEVICE_OBJECT pTopDo, PDEVICE_OBJECT pHubDo, PVOID pvContext)
|
---|
786 | {
|
---|
787 | PVBOXUSBFLTCHECKWALKER pData = (PVBOXUSBFLTCHECKWALKER)pvContext;
|
---|
788 | PVBOXUSBFLTCTX pContext = pData->pContext;
|
---|
789 |
|
---|
790 | LOG(("Visiting pFile(0x%p), pTopDo(0x%p), pHubDo(0x%p), oContext(0x%p)", pFile, pTopDo, pHubDo, pContext));
|
---|
791 | KIRQL Irql = KeGetCurrentIrql();
|
---|
792 | ASSERT_WARN(Irql == PASSIVE_LEVEL, ("unexpected IRQL (%d)", Irql));
|
---|
793 |
|
---|
794 | PDEVICE_RELATIONS pDevRelations = NULL;
|
---|
795 |
|
---|
796 | NTSTATUS Status = VBoxUsbMonQueryBusRelations(pTopDo, pFile, &pDevRelations);
|
---|
797 | if (Status == STATUS_SUCCESS && pDevRelations)
|
---|
798 | {
|
---|
799 | ULONG cReplugPdos = pDevRelations->Count;
|
---|
800 | LIST_ENTRY ReplugDevList;
|
---|
801 | InitializeListHead(&ReplugDevList);
|
---|
802 | for (ULONG k = 0; k < pDevRelations->Count; ++k)
|
---|
803 | {
|
---|
804 | PDEVICE_OBJECT pDevObj = pDevRelations->Objects[k];
|
---|
805 |
|
---|
806 | LOG(("Found existing USB PDO 0x%p", pDevObj));
|
---|
807 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
808 | PVBOXUSBFLT_DEVICE pDevice = vboxUsbFltDevGetLocked(pDevObj);
|
---|
809 | if (pDevice)
|
---|
810 | {
|
---|
811 | LOG(("Found existing device info (0x%p) for PDO 0x%p", pDevice, pDevObj));
|
---|
812 | bool bReplug = vboxUsbFltDevCheckReplugLocked(pDevice, pContext);
|
---|
813 | if (bReplug)
|
---|
814 | {
|
---|
815 | LOG(("Replug needed for device (0x%p)", pDevice));
|
---|
816 | InsertHeadList(&ReplugDevList, &pDevice->RepluggingLe);
|
---|
817 | vboxUsbFltDevRetain(pDevice);
|
---|
818 | /* do not dereference object since we will use it later */
|
---|
819 | }
|
---|
820 | else
|
---|
821 | {
|
---|
822 | LOG(("Replug NOT needed for device (0x%p)", pDevice));
|
---|
823 | ObDereferenceObject(pDevObj);
|
---|
824 | }
|
---|
825 |
|
---|
826 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
827 |
|
---|
828 | pDevRelations->Objects[k] = NULL;
|
---|
829 | --cReplugPdos;
|
---|
830 | ASSERT_WARN((uint32_t)cReplugPdos < UINT32_MAX/2, ("cReplugPdos(%d) state broken", cReplugPdos));
|
---|
831 | continue;
|
---|
832 | }
|
---|
833 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
834 |
|
---|
835 | LOG(("NO device info found for PDO 0x%p", pDevObj));
|
---|
836 | VBOXUSBFLT_DEVICE Device;
|
---|
837 | Status = vboxUsbFltDevPopulate(&Device, pDevObj /*, FALSE /* only need filter properties */);
|
---|
838 | if (NT_SUCCESS(Status))
|
---|
839 | {
|
---|
840 | uintptr_t uId = 0;
|
---|
841 | bool fFilter = false;
|
---|
842 | bool fIsOneShot = false;
|
---|
843 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
844 | PVBOXUSBFLTCTX pCtx = vboxUsbFltDevMatchLocked(&Device, &uId,
|
---|
845 | false, /* do not remove a one-shot filter */
|
---|
846 | &fFilter, &fIsOneShot);
|
---|
847 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
848 | LOG(("Matching Info: Filter (0x%p), pCtx(0x%p), fFilter(%d), fIsOneShot(%d)", uId, pCtx, (int)fFilter, (int)fIsOneShot));
|
---|
849 | if (fFilter)
|
---|
850 | {
|
---|
851 | LOG(("Matching: This device SHOULD be filtered"));
|
---|
852 | /* this device needs to be filtered, but it's not,
|
---|
853 | * leave the PDO in array to issue a replug request for it
|
---|
854 | * later on */
|
---|
855 | continue;
|
---|
856 | }
|
---|
857 | }
|
---|
858 | else
|
---|
859 | {
|
---|
860 | WARN(("vboxUsbFltDevPopulate for PDO 0x%p failed with Status 0x%x", pDevObj, Status));
|
---|
861 | }
|
---|
862 |
|
---|
863 | LOG(("Matching: This device should NOT be filtered"));
|
---|
864 | /* this device should not be filtered, and it's not */
|
---|
865 | ObDereferenceObject(pDevObj);
|
---|
866 | pDevRelations->Objects[k] = NULL;
|
---|
867 | --cReplugPdos;
|
---|
868 | ASSERT_WARN((uint32_t)cReplugPdos < UINT32_MAX/2, ("cReplugPdos is %d", cReplugPdos));
|
---|
869 | }
|
---|
870 |
|
---|
871 | LOG(("(%d) non-matched PDOs to be replugged", cReplugPdos));
|
---|
872 |
|
---|
873 | if (cReplugPdos)
|
---|
874 | {
|
---|
875 | for (ULONG k = 0; k < pDevRelations->Count; ++k)
|
---|
876 | {
|
---|
877 | if (!pDevRelations->Objects[k])
|
---|
878 | continue;
|
---|
879 |
|
---|
880 | Status = vboxUsbFltPdoReplug(pDevRelations->Objects[k]);
|
---|
881 | ASSERT_WARN(Status == STATUS_SUCCESS, ("vboxUsbFltPdoReplug ailed Status(0x%x)", Status));
|
---|
882 | ObDereferenceObject(pDevRelations->Objects[k]);
|
---|
883 | if (!--cReplugPdos)
|
---|
884 | break;
|
---|
885 | }
|
---|
886 |
|
---|
887 | ASSERT_WARN(!cReplugPdos, ("cReplugPdosreached zero!"));
|
---|
888 | }
|
---|
889 |
|
---|
890 | vboxUsbFltReplugList(&ReplugDevList);
|
---|
891 |
|
---|
892 | ExFreePool(pDevRelations);
|
---|
893 | }
|
---|
894 | else
|
---|
895 | {
|
---|
896 | WARN(("VBoxUsbMonQueryBusRelations failed for DO(0x%p), Status(0x%x), pDevRelations(0x%p)",
|
---|
897 | pTopDo, Status, pDevRelations));
|
---|
898 | }
|
---|
899 |
|
---|
900 | LOG(("Done Visiting pFile(0x%p), pTopDo(0x%p), pHubDo(0x%p), oContext(0x%p)", pFile, pTopDo, pHubDo, pContext));
|
---|
901 |
|
---|
902 | return TRUE;
|
---|
903 | }
|
---|
904 |
|
---|
905 | NTSTATUS VBoxUsbFltFilterCheck(PVBOXUSBFLTCTX pContext)
|
---|
906 | {
|
---|
907 | KIRQL Irql = KeGetCurrentIrql();
|
---|
908 | ASSERT_WARN(Irql == PASSIVE_LEVEL, ("unexpected IRQL (%d)", Irql));
|
---|
909 |
|
---|
910 | LOG(("Running filters, Context (0x%p)..", pContext));
|
---|
911 |
|
---|
912 | VBOXUSBFLTCHECKWALKER Data;
|
---|
913 | Data.pContext = pContext;
|
---|
914 | vboxUsbMonHubDevWalk(vboxUsbFltFilterCheckWalker, &Data, VBOXUSBMONHUBWALK_F_FDO);
|
---|
915 |
|
---|
916 | LOG(("DONE Running filters, Context (0x%p)", pContext));
|
---|
917 |
|
---|
918 | return STATUS_SUCCESS;
|
---|
919 | }
|
---|
920 |
|
---|
921 | NTSTATUS VBoxUsbFltClose(PVBOXUSBFLTCTX pContext)
|
---|
922 | {
|
---|
923 | LOG(("Closing context(0x%p)", pContext));
|
---|
924 | LIST_ENTRY ReplugDevList;
|
---|
925 | InitializeListHead(&ReplugDevList);
|
---|
926 |
|
---|
927 | ASSERT_WARN(pContext, ("null context"));
|
---|
928 |
|
---|
929 | KIRQL Irql = KeGetCurrentIrql();
|
---|
930 | ASSERT_WARN(Irql == PASSIVE_LEVEL, ("irql==(%d)", Irql));
|
---|
931 |
|
---|
932 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
933 | uint32_t cActiveFilters = pContext->cActiveFilters;
|
---|
934 | pContext->bRemoved = TRUE;
|
---|
935 | if (pContext->pChangeEvent)
|
---|
936 | {
|
---|
937 | LOG(("seting & closing change event (0x%p)", pContext->pChangeEvent));
|
---|
938 | KeSetEvent(pContext->pChangeEvent,
|
---|
939 | 0, /* increment*/
|
---|
940 | FALSE /* wait */);
|
---|
941 | ObDereferenceObject(pContext->pChangeEvent);
|
---|
942 | pContext->pChangeEvent = NULL;
|
---|
943 | }
|
---|
944 | else
|
---|
945 | {
|
---|
946 | LOG(("no change event"));
|
---|
947 | }
|
---|
948 | RemoveEntryList(&pContext->ListEntry);
|
---|
949 |
|
---|
950 | LOG(("removing owner filters"));
|
---|
951 | /* now re-arrange the filters */
|
---|
952 | /* 1. remove filters */
|
---|
953 | VBoxUSBFilterRemoveOwner(pContext);
|
---|
954 |
|
---|
955 | LOG(("enumerating devices.."));
|
---|
956 | /* 2. check if there are devices owned */
|
---|
957 | for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
|
---|
958 | pEntry != &g_VBoxUsbFltGlobals.DeviceList;
|
---|
959 | pEntry = pEntry->Flink)
|
---|
960 | {
|
---|
961 | PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
|
---|
962 | if (pDevice->pOwner != pContext)
|
---|
963 | continue;
|
---|
964 |
|
---|
965 | LOG(("found device(0x%p), pdo(0x%p), state(%d), filter id(0x%p), oneshot(%d)",
|
---|
966 | pDevice, pDevice->Pdo, pDevice->enmState, pDevice->uFltId, (int)pDevice->fIsFilterOneShot));
|
---|
967 | ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_ADDED, ("VBOXUSBFLT_DEVSTATE_ADDED state for device(0x%p)", pDevice));
|
---|
968 | ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_REMOVED, ("VBOXUSBFLT_DEVSTATE_REMOVED state for device(0x%p)", pDevice));
|
---|
969 |
|
---|
970 | vboxUsbFltDevOwnerClearLocked(pDevice);
|
---|
971 |
|
---|
972 | if (vboxUsbFltDevCheckReplugLocked(pDevice, pContext))
|
---|
973 | {
|
---|
974 | LOG(("device needs replug"));
|
---|
975 | InsertHeadList(&ReplugDevList, &pDevice->RepluggingLe);
|
---|
976 | /* retain to ensure the device is not removed before we issue a replug */
|
---|
977 | vboxUsbFltDevRetain(pDevice);
|
---|
978 | /* keep the PDO alive */
|
---|
979 | ObReferenceObject(pDevice->Pdo);
|
---|
980 | }
|
---|
981 | else
|
---|
982 | {
|
---|
983 | LOG(("device does NOT need replug"));
|
---|
984 | }
|
---|
985 | }
|
---|
986 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
987 |
|
---|
988 | /* this should replug all devices that were either skipped or grabbed due to the context's */
|
---|
989 | vboxUsbFltReplugList(&ReplugDevList);
|
---|
990 |
|
---|
991 | LOG(("SUCCESS done context(0x%p)", pContext));
|
---|
992 | return STATUS_SUCCESS;
|
---|
993 | }
|
---|
994 |
|
---|
995 | NTSTATUS VBoxUsbFltCreate(PVBOXUSBFLTCTX pContext)
|
---|
996 | {
|
---|
997 | LOG(("Creating context(0x%p)", pContext));
|
---|
998 | memset(pContext, 0, sizeof (*pContext));
|
---|
999 | pContext->Process = RTProcSelf();
|
---|
1000 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1001 | InsertHeadList(&g_VBoxUsbFltGlobals.ContextList, &pContext->ListEntry);
|
---|
1002 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1003 | LOG(("SUCCESS context(0x%p)", pContext));
|
---|
1004 | return STATUS_SUCCESS;
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | int VBoxUsbFltAdd(PVBOXUSBFLTCTX pContext, PUSBFILTER pFilter, uintptr_t *pId)
|
---|
1008 | {
|
---|
1009 | LOG(("adding filter, Context (0x%p)..", pContext));
|
---|
1010 | *pId = 0;
|
---|
1011 | /* LOG the filter details. */
|
---|
1012 | LOG((__FUNCTION__": %s %s %s",
|
---|
1013 | USBFilterGetString(pFilter, USBFILTERIDX_MANUFACTURER_STR) ? USBFilterGetString(pFilter, USBFILTERIDX_MANUFACTURER_STR) : "<null>",
|
---|
1014 | USBFilterGetString(pFilter, USBFILTERIDX_PRODUCT_STR) ? USBFilterGetString(pFilter, USBFILTERIDX_PRODUCT_STR) : "<null>",
|
---|
1015 | USBFilterGetString(pFilter, USBFILTERIDX_SERIAL_NUMBER_STR) ? USBFilterGetString(pFilter, USBFILTERIDX_SERIAL_NUMBER_STR) : "<null>"));
|
---|
1016 | #ifdef VBOX_USB_WITH_VERBOSE_LOGGING
|
---|
1017 | LOG(("VBoxUSBClient::addFilter: idVendor=%#x idProduct=%#x bcdDevice=%#x bDeviceClass=%#x bDeviceSubClass=%#x bDeviceProtocol=%#x bBus=%#x bPort=%#x Type%#x",
|
---|
1018 | USBFilterGetNum(pFilter, USBFILTERIDX_VENDOR_ID),
|
---|
1019 | USBFilterGetNum(pFilter, USBFILTERIDX_PRODUCT_ID),
|
---|
1020 | USBFilterGetNum(pFilter, USBFILTERIDX_DEVICE_REV),
|
---|
1021 | USBFilterGetNum(pFilter, USBFILTERIDX_DEVICE_CLASS),
|
---|
1022 | USBFilterGetNum(pFilter, USBFILTERIDX_DEVICE_SUB_CLASS),
|
---|
1023 | USBFilterGetNum(pFilter, USBFILTERIDX_DEVICE_PROTOCOL),
|
---|
1024 | USBFilterGetNum(pFilter, USBFILTERIDX_BUS),
|
---|
1025 | USBFilterGetNum(pFilter, USBFILTERIDX_PORT),
|
---|
1026 | USBFilterGetFilterType(pFilter)));
|
---|
1027 | #endif
|
---|
1028 |
|
---|
1029 | /* We can't get the bus/port numbers. Ignore them while matching. */
|
---|
1030 | USBFilterSetMustBePresent(pFilter, USBFILTERIDX_BUS, false);
|
---|
1031 | USBFilterSetMustBePresent(pFilter, USBFILTERIDX_PORT, false);
|
---|
1032 |
|
---|
1033 | uintptr_t uId = 0;
|
---|
1034 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1035 | /* Add the filter. */
|
---|
1036 | int rc = VBoxUSBFilterAdd(pFilter, pContext, &uId);
|
---|
1037 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1038 | if (RT_SUCCESS(rc))
|
---|
1039 | {
|
---|
1040 | LOG(("ADDED filer id 0x%p", uId));
|
---|
1041 | ASSERT_WARN(uId, ("uid is NULL"));
|
---|
1042 | #ifdef VBOX_USBMON_WITH_FILTER_AUTOAPPLY
|
---|
1043 | VBoxUsbFltFilterCheck();
|
---|
1044 | #endif
|
---|
1045 | }
|
---|
1046 | else
|
---|
1047 | {
|
---|
1048 | WARN(("VBoxUSBFilterAdd failed rc (%d)", rc));
|
---|
1049 | ASSERT_WARN(!uId, ("uid is not NULL"));
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | *pId = uId;
|
---|
1053 | return rc;
|
---|
1054 | }
|
---|
1055 |
|
---|
1056 | int VBoxUsbFltRemove(PVBOXUSBFLTCTX pContext, uintptr_t uId)
|
---|
1057 | {
|
---|
1058 | LOG(("removing filter id(0x%p), Context (0x%p)..", pContext, uId));
|
---|
1059 | Assert(uId);
|
---|
1060 |
|
---|
1061 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1062 | int rc = VBoxUSBFilterRemove(pContext, uId);
|
---|
1063 | if (!RT_SUCCESS(rc))
|
---|
1064 | {
|
---|
1065 | WARN(("VBoxUSBFilterRemove failed rc (%d)", rc));
|
---|
1066 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1067 | return rc;
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | LOG(("enumerating devices.."));
|
---|
1071 | for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
|
---|
1072 | pEntry != &g_VBoxUsbFltGlobals.DeviceList;
|
---|
1073 | pEntry = pEntry->Flink)
|
---|
1074 | {
|
---|
1075 | PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
|
---|
1076 | if (pDevice->fIsFilterOneShot)
|
---|
1077 | {
|
---|
1078 | ASSERT_WARN(!pDevice->uFltId, ("oneshot filter on device(0x%p): unexpected uFltId(%d)", pDevice, pDevice->uFltId));
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | if (pDevice->uFltId != uId)
|
---|
1082 | continue;
|
---|
1083 |
|
---|
1084 | ASSERT_WARN(pDevice->pOwner == pContext, ("Device(0x%p) owner(0x%p) not match to (0x%p)", pDevice, pDevice->pOwner, pContext));
|
---|
1085 | if (pDevice->pOwner != pContext)
|
---|
1086 | continue;
|
---|
1087 |
|
---|
1088 | LOG(("found device(0x%p), pdo(0x%p), state(%d), filter id(0x%p), oneshot(%d)",
|
---|
1089 | pDevice, pDevice->Pdo, pDevice->enmState, pDevice->uFltId, (int)pDevice->fIsFilterOneShot));
|
---|
1090 | ASSERT_WARN(!pDevice->fIsFilterOneShot, ("device(0x%p) is filtered with a oneshot filter", pDevice));
|
---|
1091 | pDevice->uFltId = 0;
|
---|
1092 | /* clear the fIsFilterOneShot flag to ensure the device is replugged on the next VBoxUsbFltFilterCheck call */
|
---|
1093 | pDevice->fIsFilterOneShot = false;
|
---|
1094 | }
|
---|
1095 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1096 |
|
---|
1097 | LOG(("done enumerating devices"));
|
---|
1098 |
|
---|
1099 | if (RT_SUCCESS(rc))
|
---|
1100 | {
|
---|
1101 | #ifdef VBOX_USBMON_WITH_FILTER_AUTOAPPLY
|
---|
1102 | VBoxUsbFltFilterCheck();
|
---|
1103 | #endif
|
---|
1104 | }
|
---|
1105 | return rc;
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | NTSTATUS VBoxUsbFltSetNotifyEvent(PVBOXUSBFLTCTX pContext, HANDLE hEvent)
|
---|
1109 | {
|
---|
1110 | NTSTATUS Status = STATUS_SUCCESS;
|
---|
1111 | PKEVENT pEvent = NULL;
|
---|
1112 | PKEVENT pOldEvent = NULL;
|
---|
1113 | if (hEvent)
|
---|
1114 | {
|
---|
1115 | Status = ObReferenceObjectByHandle(hEvent,
|
---|
1116 | EVENT_MODIFY_STATE,
|
---|
1117 | *ExEventObjectType, UserMode,
|
---|
1118 | (PVOID*)&pEvent,
|
---|
1119 | NULL);
|
---|
1120 | Assert(Status == STATUS_SUCCESS);
|
---|
1121 | if (!NT_SUCCESS(Status))
|
---|
1122 | return Status;
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1126 | pOldEvent = pContext->pChangeEvent;
|
---|
1127 | pContext->pChangeEvent = pEvent;
|
---|
1128 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1129 |
|
---|
1130 | if (pOldEvent)
|
---|
1131 | {
|
---|
1132 | ObDereferenceObject(pOldEvent);
|
---|
1133 | }
|
---|
1134 |
|
---|
1135 | return STATUS_SUCCESS;
|
---|
1136 | }
|
---|
1137 |
|
---|
1138 | static USBDEVICESTATE vboxUsbDevGetUserState(PVBOXUSBFLTCTX pContext, PVBOXUSBFLT_DEVICE pDevice)
|
---|
1139 | {
|
---|
1140 | if (vboxUsbFltDevStateIsNotFiltered(pDevice))
|
---|
1141 | return USBDEVICESTATE_USED_BY_HOST_CAPTURABLE;
|
---|
1142 |
|
---|
1143 | /* the device is filtered, or replugging */
|
---|
1144 | if (pDevice->enmState == VBOXUSBFLT_DEVSTATE_REPLUGGING)
|
---|
1145 | {
|
---|
1146 | ASSERT_WARN(!pDevice->pOwner, ("replugging device(0x%p) still has an owner(0x%p)", pDevice, pDevice->pOwner));
|
---|
1147 | ASSERT_WARN(!pDevice->uFltId, ("replugging device(0x%p) still has filter(0x%p)", pDevice, pDevice->uFltId));
|
---|
1148 | /* no user state for this, we should not return it tu the user */
|
---|
1149 | return USBDEVICESTATE_USED_BY_HOST;
|
---|
1150 | }
|
---|
1151 |
|
---|
1152 | /* the device is filtered, if owner differs from the context, return as USED_BY_HOST */
|
---|
1153 | ASSERT_WARN(pDevice->pOwner, ("device(0x%p) has noowner", pDevice));
|
---|
1154 | /* the id can be null if a filter is removed */
|
---|
1155 | // Assert(pDevice->uFltId);
|
---|
1156 |
|
---|
1157 | if (pDevice->pOwner != pContext)
|
---|
1158 | {
|
---|
1159 | LOG(("Device owner differs from the current context, returning used by host"));
|
---|
1160 | return USBDEVICESTATE_USED_BY_HOST;
|
---|
1161 | }
|
---|
1162 |
|
---|
1163 | switch (pDevice->enmState)
|
---|
1164 | {
|
---|
1165 | case VBOXUSBFLT_DEVSTATE_UNCAPTURED:
|
---|
1166 | case VBOXUSBFLT_DEVSTATE_CAPTURING:
|
---|
1167 | return USBDEVICESTATE_USED_BY_HOST_CAPTURABLE;
|
---|
1168 | case VBOXUSBFLT_DEVSTATE_CAPTURED:
|
---|
1169 | return USBDEVICESTATE_HELD_BY_PROXY;
|
---|
1170 | case VBOXUSBFLT_DEVSTATE_USED_BY_GUEST:
|
---|
1171 | return USBDEVICESTATE_USED_BY_GUEST;
|
---|
1172 | default:
|
---|
1173 | WARN(("unexpected device state(%d) for device(0x%p)", pDevice->enmState, pDevice));
|
---|
1174 | return USBDEVICESTATE_UNSUPPORTED;
|
---|
1175 | }
|
---|
1176 | }
|
---|
1177 |
|
---|
1178 | static void vboxUsbDevToUserInfo(PVBOXUSBFLTCTX pContext, PVBOXUSBFLT_DEVICE pDevice, PUSBSUP_DEVINFO pDevInfo)
|
---|
1179 | {
|
---|
1180 | #if 0
|
---|
1181 | pDevInfo->usVendorId = pDevice->idVendor;
|
---|
1182 | pDevInfo->usProductId = pDevice->idProduct;
|
---|
1183 | pDevInfo->usRevision = pDevice->bcdDevice;
|
---|
1184 | pDevInfo->enmState = vboxUsbDevGetUserState(pContext, pDevice);
|
---|
1185 |
|
---|
1186 | strcpy(pDevInfo->szDrvKeyName, pDevice->szDrvKeyName);
|
---|
1187 | if (pDevInfo->enmState == USBDEVICESTATE_HELD_BY_PROXY
|
---|
1188 | || pDevInfo->enmState == USBDEVICESTATE_USED_BY_GUEST)
|
---|
1189 | {
|
---|
1190 | /* this is the only case where we return the obj name to the client */
|
---|
1191 | strcpy(pDevInfo->szObjName, pDevice->szObjName);
|
---|
1192 | }
|
---|
1193 | pDevInfo->fHighSpeed = pDevice->fHighSpeed;
|
---|
1194 | #endif
|
---|
1195 | }
|
---|
1196 |
|
---|
1197 | NTSTATUS VBoxUsbFltGetDevice(PVBOXUSBFLTCTX pContext, HVBOXUSBDEVUSR hDevice, PUSBSUP_GETDEV_MON pInfo)
|
---|
1198 | {
|
---|
1199 | Assert(hDevice);
|
---|
1200 |
|
---|
1201 | memset (pInfo, 0, sizeof (*pInfo));
|
---|
1202 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1203 | for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
|
---|
1204 | pEntry != &g_VBoxUsbFltGlobals.DeviceList;
|
---|
1205 | pEntry = pEntry->Flink)
|
---|
1206 | {
|
---|
1207 | PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
|
---|
1208 | Assert(pDevice->enmState != VBOXUSBFLT_DEVSTATE_REMOVED);
|
---|
1209 | Assert(pDevice->enmState != VBOXUSBFLT_DEVSTATE_ADDED);
|
---|
1210 |
|
---|
1211 | if (pDevice != hDevice)
|
---|
1212 | continue;
|
---|
1213 |
|
---|
1214 | USBDEVICESTATE enmUsrState = vboxUsbDevGetUserState(pContext, pDevice);
|
---|
1215 | pInfo->enmState = enmUsrState;
|
---|
1216 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1217 | return STATUS_SUCCESS;
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1221 |
|
---|
1222 | /* this should not occur */
|
---|
1223 | AssertFailed();
|
---|
1224 |
|
---|
1225 | return STATUS_INVALID_PARAMETER;
|
---|
1226 | }
|
---|
1227 |
|
---|
1228 | NTSTATUS VBoxUsbFltPdoAdd(PDEVICE_OBJECT pPdo, BOOLEAN *pbFiltered)
|
---|
1229 | {
|
---|
1230 | *pbFiltered = FALSE;
|
---|
1231 | PVBOXUSBFLT_DEVICE pDevice;
|
---|
1232 |
|
---|
1233 | /* first check if device is in the a already */
|
---|
1234 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1235 | pDevice = vboxUsbFltDevGetLocked(pPdo);
|
---|
1236 | if (pDevice)
|
---|
1237 | {
|
---|
1238 | LOG(("found device (0x%p), state(%d) for PDO(0x%p)", pDevice, pDevice->enmState, pPdo));
|
---|
1239 | ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_ADDED, ("VBOXUSBFLT_DEVSTATE_ADDED state for device(0x%p)", pDevice));
|
---|
1240 | ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_REMOVED, ("VBOXUSBFLT_DEVSTATE_REMOVED state for device(0x%p)", pDevice));
|
---|
1241 | *pbFiltered = pDevice->enmState >= VBOXUSBFLT_DEVSTATE_CAPTURING;
|
---|
1242 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1243 | return STATUS_SUCCESS;
|
---|
1244 | }
|
---|
1245 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1246 | pDevice = (PVBOXUSBFLT_DEVICE)VBoxUsbMonMemAllocZ(sizeof (*pDevice));
|
---|
1247 | if (!pDevice)
|
---|
1248 | {
|
---|
1249 | WARN(("VBoxUsbMonMemAllocZ failed"));
|
---|
1250 | return STATUS_NO_MEMORY;
|
---|
1251 | }
|
---|
1252 |
|
---|
1253 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_ADDED;
|
---|
1254 | pDevice->cRefs = 1;
|
---|
1255 | NTSTATUS Status = vboxUsbFltDevPopulate(pDevice, pPdo /* , TRUE /* need all props */);
|
---|
1256 | if (!NT_SUCCESS(Status))
|
---|
1257 | {
|
---|
1258 | WARN(("vboxUsbFltDevPopulate failed, Status 0x%x", Status));
|
---|
1259 | VBoxUsbMonMemFree(pDevice);
|
---|
1260 | return Status;
|
---|
1261 | }
|
---|
1262 |
|
---|
1263 | uintptr_t uId;
|
---|
1264 | bool fFilter = false;
|
---|
1265 | bool fIsOneShot = false;
|
---|
1266 | PVBOXUSBFLTCTX pCtx;
|
---|
1267 | PVBOXUSBFLT_DEVICE pTmpDev;
|
---|
1268 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1269 | /* (paranoia) re-check the device is still not here */
|
---|
1270 | pTmpDev = vboxUsbFltDevGetLocked(pPdo);
|
---|
1271 | if (pTmpDev)
|
---|
1272 | {
|
---|
1273 | LOG(("second try: found device (0x%p), state(%d) for PDO(0x%p)", pDevice, pDevice->enmState, pPdo));
|
---|
1274 | ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_ADDED, ("second try: VBOXUSBFLT_DEVSTATE_ADDED state for device(0x%p)", pDevice));
|
---|
1275 | ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_REMOVED, ("second try: VBOXUSBFLT_DEVSTATE_REMOVED state for device(0x%p)", pDevice));
|
---|
1276 | *pbFiltered = pTmpDev->enmState >= VBOXUSBFLT_DEVSTATE_CAPTURING;
|
---|
1277 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1278 | VBoxUsbMonMemFree(pDevice);
|
---|
1279 | return STATUS_SUCCESS;
|
---|
1280 | }
|
---|
1281 |
|
---|
1282 | LOG(("Created Device 0x%p for PDO 0x%p", pDevice, pPdo));
|
---|
1283 |
|
---|
1284 | pCtx = vboxUsbFltDevMatchLocked(pDevice, &uId,
|
---|
1285 | true, /* remove a one-shot filter */
|
---|
1286 | &fFilter, &fIsOneShot);
|
---|
1287 | LOG(("Matching Info: Filter (0x%p), pCtx(0x%p), fFilter(%d), fIsOneShot(%d)", uId, pCtx, (int)fFilter, (int)fIsOneShot));
|
---|
1288 | if (fFilter)
|
---|
1289 | {
|
---|
1290 | LOG(("Created Device 0x%p should be filtered", pDevice));
|
---|
1291 | ASSERT_WARN(pCtx, ("zero ctx"));
|
---|
1292 | ASSERT_WARN(uId, ("zero uId"));
|
---|
1293 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_CAPTURING;
|
---|
1294 | }
|
---|
1295 | else
|
---|
1296 | {
|
---|
1297 | LOG(("Created Device 0x%p should NOT be filtered", pDevice));
|
---|
1298 | ASSERT_WARN(!uId == !pCtx, ("invalid uid(0x%p) - ctx(0x%p) pair", uId, pCtx)); /* either both zero or both not */
|
---|
1299 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_UNCAPTURED;
|
---|
1300 | }
|
---|
1301 |
|
---|
1302 | if (pCtx)
|
---|
1303 | vboxUsbFltDevOwnerSetLocked(pDevice, pCtx, fIsOneShot ? 0 : uId, fIsOneShot);
|
---|
1304 |
|
---|
1305 | InsertHeadList(&g_VBoxUsbFltGlobals.DeviceList, &pDevice->GlobalLe);
|
---|
1306 |
|
---|
1307 | /* do not need to signal anything here -
|
---|
1308 | * going to do that once the proxy device object starts */
|
---|
1309 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1310 |
|
---|
1311 | *pbFiltered = fFilter;
|
---|
1312 |
|
---|
1313 | return STATUS_SUCCESS;
|
---|
1314 | }
|
---|
1315 |
|
---|
1316 | NTSTATUS VBoxUsbFltPdoAddCompleted(PDEVICE_OBJECT pPdo)
|
---|
1317 | {
|
---|
1318 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1319 | vboxUsbFltSignalChangeLocked();
|
---|
1320 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1321 | return STATUS_SUCCESS;
|
---|
1322 | }
|
---|
1323 |
|
---|
1324 | BOOLEAN VBoxUsbFltPdoIsFiltered(PDEVICE_OBJECT pPdo)
|
---|
1325 | {
|
---|
1326 | VBOXUSBFLT_DEVSTATE enmState = VBOXUSBFLT_DEVSTATE_REMOVED;
|
---|
1327 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1328 | PVBOXUSBFLT_DEVICE pDevice = vboxUsbFltDevGetLocked(pPdo);
|
---|
1329 | if (pDevice)
|
---|
1330 | {
|
---|
1331 | enmState = pDevice->enmState;
|
---|
1332 | }
|
---|
1333 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1334 |
|
---|
1335 | return enmState >= VBOXUSBFLT_DEVSTATE_CAPTURING;
|
---|
1336 | }
|
---|
1337 |
|
---|
1338 | NTSTATUS VBoxUsbFltPdoRemove(PDEVICE_OBJECT pPdo)
|
---|
1339 | {
|
---|
1340 | PVBOXUSBFLT_DEVICE pDevice;
|
---|
1341 | VBOXUSBFLT_DEVSTATE enmOldState;
|
---|
1342 |
|
---|
1343 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1344 | pDevice = vboxUsbFltDevGetLocked(pPdo);
|
---|
1345 | if (pDevice)
|
---|
1346 | {
|
---|
1347 | RemoveEntryList(&pDevice->GlobalLe);
|
---|
1348 | enmOldState = pDevice->enmState;
|
---|
1349 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_REMOVED;
|
---|
1350 | if (enmOldState != VBOXUSBFLT_DEVSTATE_REPLUGGING)
|
---|
1351 | {
|
---|
1352 | vboxUsbFltSignalChangeLocked();
|
---|
1353 | }
|
---|
1354 | else
|
---|
1355 | {
|
---|
1356 | /* the device *should* reappear, do signlling on re-appear only
|
---|
1357 | * to avoid extra signaling. still there might be a situation
|
---|
1358 | * when the device will not re-appear if it gets physically removed
|
---|
1359 | * before it re-appears
|
---|
1360 | * @todo: set a timer callback to do a notification from it */
|
---|
1361 | }
|
---|
1362 | }
|
---|
1363 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1364 | if (pDevice)
|
---|
1365 | vboxUsbFltDevRelease(pDevice);
|
---|
1366 | return STATUS_SUCCESS;
|
---|
1367 | }
|
---|
1368 |
|
---|
1369 | HVBOXUSBFLTDEV VBoxUsbFltProxyStarted(PDEVICE_OBJECT pPdo)
|
---|
1370 | {
|
---|
1371 | PVBOXUSBFLT_DEVICE pDevice;
|
---|
1372 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1373 | pDevice = vboxUsbFltDevGetLocked(pPdo);
|
---|
1374 | /*
|
---|
1375 | * Prevent a host crash when vboxUsbFltDevGetLocked fails to locate the matching PDO
|
---|
1376 | * in g_VBoxUsbFltGlobals.DeviceList (see @bugref{6509}).
|
---|
1377 | */
|
---|
1378 | if (pDevice == NULL)
|
---|
1379 | {
|
---|
1380 | WARN(("failed to get device for PDO(0x%p)", pPdo));
|
---|
1381 | }
|
---|
1382 | else if (pDevice->enmState = VBOXUSBFLT_DEVSTATE_CAPTURING)
|
---|
1383 | {
|
---|
1384 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_CAPTURED;
|
---|
1385 | LOG(("The proxy notified proxy start for the captured device 0x%x", pDevice));
|
---|
1386 | vboxUsbFltDevRetain(pDevice);
|
---|
1387 | vboxUsbFltSignalChangeLocked();
|
---|
1388 | }
|
---|
1389 | else
|
---|
1390 | {
|
---|
1391 | WARN(("invalid state, %d", pDevice->enmState));
|
---|
1392 | pDevice = NULL;
|
---|
1393 | }
|
---|
1394 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1395 | return pDevice;
|
---|
1396 | }
|
---|
1397 |
|
---|
1398 | void VBoxUsbFltProxyStopped(HVBOXUSBFLTDEV hDev)
|
---|
1399 | {
|
---|
1400 | PVBOXUSBFLT_DEVICE pDevice = (PVBOXUSBFLT_DEVICE)hDev;
|
---|
1401 | /*
|
---|
1402 | * Prevent a host crash when VBoxUsbFltProxyStarted fails, returning NULL.
|
---|
1403 | * See @bugref{6509}.
|
---|
1404 | */
|
---|
1405 | if (pDevice == NULL)
|
---|
1406 | {
|
---|
1407 | WARN(("VBoxUsbFltProxyStopped called with NULL device pointer"));
|
---|
1408 | return;
|
---|
1409 | }
|
---|
1410 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1411 | if (pDevice->enmState == VBOXUSBFLT_DEVSTATE_CAPTURED
|
---|
1412 | || pDevice->enmState == VBOXUSBFLT_DEVSTATE_USED_BY_GUEST)
|
---|
1413 | {
|
---|
1414 | /* this is due to devie was physically removed */
|
---|
1415 | LOG(("The proxy notified proxy stop for the captured device 0x%x, current state %d", pDevice, pDevice->enmState));
|
---|
1416 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_CAPTURING;
|
---|
1417 | vboxUsbFltSignalChangeLocked();
|
---|
1418 | }
|
---|
1419 | else
|
---|
1420 | {
|
---|
1421 | if(pDevice->enmState != VBOXUSBFLT_DEVSTATE_REPLUGGING)
|
---|
1422 | {
|
---|
1423 | WARN(("invalid state, %d", pDevice->enmState));
|
---|
1424 | }
|
---|
1425 | }
|
---|
1426 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1427 |
|
---|
1428 | vboxUsbFltDevRelease(pDevice);
|
---|
1429 | }
|
---|
1430 |
|
---|
1431 | NTSTATUS VBoxUsbFltInit()
|
---|
1432 | {
|
---|
1433 | int rc = VBoxUSBFilterInit();
|
---|
1434 | if (RT_FAILURE(rc))
|
---|
1435 | {
|
---|
1436 | WARN(("VBoxUSBFilterInit failed, rc (%d)", rc));
|
---|
1437 | return STATUS_UNSUCCESSFUL;
|
---|
1438 | }
|
---|
1439 |
|
---|
1440 | memset(&g_VBoxUsbFltGlobals, 0, sizeof (g_VBoxUsbFltGlobals));
|
---|
1441 | InitializeListHead(&g_VBoxUsbFltGlobals.DeviceList);
|
---|
1442 | InitializeListHead(&g_VBoxUsbFltGlobals.ContextList);
|
---|
1443 | InitializeListHead(&g_VBoxUsbFltGlobals.BlackDeviceList);
|
---|
1444 | vboxUsbFltBlDevPopulateWithKnownLocked();
|
---|
1445 | VBOXUSBFLT_LOCK_INIT();
|
---|
1446 | return STATUS_SUCCESS;
|
---|
1447 | }
|
---|
1448 |
|
---|
1449 | NTSTATUS VBoxUsbFltTerm()
|
---|
1450 | {
|
---|
1451 | bool bBusy = false;
|
---|
1452 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1453 | do
|
---|
1454 | {
|
---|
1455 | if (!IsListEmpty(&g_VBoxUsbFltGlobals.ContextList))
|
---|
1456 | {
|
---|
1457 | AssertFailed();
|
---|
1458 | bBusy = true;
|
---|
1459 | break;
|
---|
1460 | }
|
---|
1461 |
|
---|
1462 | PLIST_ENTRY pNext = NULL;
|
---|
1463 | for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
|
---|
1464 | pEntry != &g_VBoxUsbFltGlobals.DeviceList;
|
---|
1465 | pEntry = pNext)
|
---|
1466 | {
|
---|
1467 | pNext = pEntry->Flink;
|
---|
1468 | PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
|
---|
1469 | Assert(!pDevice->uFltId);
|
---|
1470 | Assert(!pDevice->pOwner);
|
---|
1471 | if (pDevice->cRefs != 1)
|
---|
1472 | {
|
---|
1473 | AssertFailed();
|
---|
1474 | bBusy = true;
|
---|
1475 | break;
|
---|
1476 | }
|
---|
1477 | }
|
---|
1478 | } while (0);
|
---|
1479 |
|
---|
1480 | VBOXUSBFLT_LOCK_RELEASE()
|
---|
1481 |
|
---|
1482 | if (bBusy)
|
---|
1483 | {
|
---|
1484 | return STATUS_DEVICE_BUSY;
|
---|
1485 | }
|
---|
1486 |
|
---|
1487 | for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
|
---|
1488 | pEntry != &g_VBoxUsbFltGlobals.DeviceList;
|
---|
1489 | pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink)
|
---|
1490 | {
|
---|
1491 | RemoveEntryList(pEntry);
|
---|
1492 | PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
|
---|
1493 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_REMOVED;
|
---|
1494 | vboxUsbFltDevRelease(pDevice);
|
---|
1495 | }
|
---|
1496 |
|
---|
1497 | vboxUsbFltBlDevClearLocked();
|
---|
1498 |
|
---|
1499 | VBOXUSBFLT_LOCK_TERM();
|
---|
1500 |
|
---|
1501 | VBoxUSBFilterTerm();
|
---|
1502 |
|
---|
1503 | return STATUS_SUCCESS;
|
---|
1504 | }
|
---|
1505 |
|
---|