1 | /* $Id: VBoxUsbFlt.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox USB Monitor Device Filtering functionality
|
---|
4 | */
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2011-2022 Oracle and/or its affiliates.
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox base platform packages, as
|
---|
9 | * available from https://www.virtualbox.org.
|
---|
10 | *
|
---|
11 | * This program is free software; you can redistribute it and/or
|
---|
12 | * modify it under the terms of the GNU General Public License
|
---|
13 | * as published by the Free Software Foundation, in version 3 of the
|
---|
14 | * License.
|
---|
15 | *
|
---|
16 | * This program is distributed in the hope that it will be useful, but
|
---|
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
19 | * General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
23 | *
|
---|
24 | * The contents of this file may alternatively be used under the terms
|
---|
25 | * of the Common Development and Distribution License Version 1.0
|
---|
26 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
27 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
28 | * CDDL are applicable instead of those of the GPL.
|
---|
29 | *
|
---|
30 | * You may elect to license modified versions of this file under the
|
---|
31 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
32 | *
|
---|
33 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
34 | */
|
---|
35 |
|
---|
36 |
|
---|
37 | /*********************************************************************************************************************************
|
---|
38 | * Header Files *
|
---|
39 | *********************************************************************************************************************************/
|
---|
40 | #include "VBoxUsbMon.h"
|
---|
41 | #include "../cmn/VBoxUsbTool.h"
|
---|
42 |
|
---|
43 | #include <VBox/cdefs.h>
|
---|
44 | #include <VBox/types.h>
|
---|
45 | #include <iprt/process.h>
|
---|
46 | #include <iprt/assert.h>
|
---|
47 | #include <iprt/errcore.h>
|
---|
48 |
|
---|
49 | #include <iprt/assert.h>
|
---|
50 |
|
---|
51 | #pragma warning(disable : 4200)
|
---|
52 | #include "usbdi.h"
|
---|
53 | #pragma warning(default : 4200)
|
---|
54 | #include "usbdlib.h"
|
---|
55 | #include "VBoxUSBFilterMgr.h"
|
---|
56 | #include <VBox/usblib.h>
|
---|
57 | #include <devguid.h>
|
---|
58 | #include <devpkey.h>
|
---|
59 |
|
---|
60 |
|
---|
61 | /* We should be including ntifs.h but that's not as easy as it sounds. */
|
---|
62 | extern "C" {
|
---|
63 | NTKERNELAPI PDEVICE_OBJECT IoGetDeviceAttachmentBaseRef(__in PDEVICE_OBJECT DeviceObject);
|
---|
64 | }
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * state transitions:
|
---|
68 | *
|
---|
69 | * (we are not filtering this device )
|
---|
70 | * ADDED --> UNCAPTURED ------------------------------->-
|
---|
71 | * | |
|
---|
72 | * | (we are filtering this device, | (the device is being
|
---|
73 | * | waiting for our device driver | re-plugged to perform
|
---|
74 | * | to pick it up) | capture-uncapture transition)
|
---|
75 | * |-> CAPTURING -------------------------------->|---> REPLUGGING -----
|
---|
76 | * ^ | (device driver picked | |
|
---|
77 | * | | up the device) | (remove cased | (device is removed
|
---|
78 | * | ->---> CAPTURED ---------------------->| by "real" removal | the device info is removed form the list)
|
---|
79 | * | | |------------------->->--> REMOVED
|
---|
80 | * | | |
|
---|
81 | * |-----------<->---> USED_BY_GUEST ------->|
|
---|
82 | * | |
|
---|
83 | * |------------------------<-
|
---|
84 | *
|
---|
85 | * NOTE: the order of enums DOES MATTER!!
|
---|
86 | * Do not blindly modify!! as the code assumes the state is ordered this way.
|
---|
87 | */
|
---|
88 | typedef enum
|
---|
89 | {
|
---|
90 | VBOXUSBFLT_DEVSTATE_UNKNOWN = 0,
|
---|
91 | VBOXUSBFLT_DEVSTATE_REMOVED,
|
---|
92 | VBOXUSBFLT_DEVSTATE_REPLUGGING,
|
---|
93 | VBOXUSBFLT_DEVSTATE_ADDED,
|
---|
94 | VBOXUSBFLT_DEVSTATE_UNCAPTURED,
|
---|
95 | VBOXUSBFLT_DEVSTATE_CAPTURING,
|
---|
96 | VBOXUSBFLT_DEVSTATE_CAPTURED,
|
---|
97 | VBOXUSBFLT_DEVSTATE_USED_BY_GUEST,
|
---|
98 | VBOXUSBFLT_DEVSTATE_32BIT_HACK = 0x7fffffff
|
---|
99 | } VBOXUSBFLT_DEVSTATE;
|
---|
100 |
|
---|
101 | typedef struct VBOXUSBFLT_DEVICE
|
---|
102 | {
|
---|
103 | LIST_ENTRY GlobalLe;
|
---|
104 | /* auxiliary list to be used for gathering devices to be re-plugged
|
---|
105 | * only thread that puts the device to the REPLUGGING state can use this list */
|
---|
106 | LIST_ENTRY RepluggingLe;
|
---|
107 | /* Owning session. Each matched device has an owning session. */
|
---|
108 | struct VBOXUSBFLTCTX *pOwner;
|
---|
109 | /* filter id - if NULL AND device has an owner - the filter is destroyed */
|
---|
110 | uintptr_t uFltId;
|
---|
111 | /* true iff device is filtered with a one-shot filter */
|
---|
112 | bool fIsFilterOneShot;
|
---|
113 | /* true if descriptors could not be read and only inferred from PnP Manager data */
|
---|
114 | bool fInferredDesc;
|
---|
115 | /* The device state. If the non-owner session is requesting the state while the device is grabbed,
|
---|
116 | * the USBDEVICESTATE_USED_BY_HOST is returned. */
|
---|
117 | VBOXUSBFLT_DEVSTATE enmState;
|
---|
118 | volatile uint32_t cRefs;
|
---|
119 | PDEVICE_OBJECT Pdo;
|
---|
120 | uint16_t idVendor;
|
---|
121 | uint16_t idProduct;
|
---|
122 | uint16_t bcdDevice;
|
---|
123 | uint16_t bPort;
|
---|
124 | uint8_t bClass;
|
---|
125 | uint8_t bSubClass;
|
---|
126 | uint8_t bProtocol;
|
---|
127 | char szSerial[MAX_USB_SERIAL_STRING];
|
---|
128 | char szMfgName[MAX_USB_SERIAL_STRING];
|
---|
129 | char szProduct[MAX_USB_SERIAL_STRING];
|
---|
130 | WCHAR szLocationPath[768];
|
---|
131 | #if 0
|
---|
132 | char szDrvKeyName[512];
|
---|
133 | BOOLEAN fHighSpeed;
|
---|
134 | #endif
|
---|
135 | } VBOXUSBFLT_DEVICE, *PVBOXUSBFLT_DEVICE;
|
---|
136 |
|
---|
137 | #define PVBOXUSBFLT_DEVICE_FROM_LE(_pLe) ( (PVBOXUSBFLT_DEVICE)( ((uint8_t*)(_pLe)) - RT_OFFSETOF(VBOXUSBFLT_DEVICE, GlobalLe) ) )
|
---|
138 | #define PVBOXUSBFLT_DEVICE_FROM_REPLUGGINGLE(_pLe) ( (PVBOXUSBFLT_DEVICE)( ((uint8_t*)(_pLe)) - RT_OFFSETOF(VBOXUSBFLT_DEVICE, RepluggingLe) ) )
|
---|
139 | #define PVBOXUSBFLTCTX_FROM_LE(_pLe) ( (PVBOXUSBFLTCTX)( ((uint8_t*)(_pLe)) - RT_OFFSETOF(VBOXUSBFLTCTX, ListEntry) ) )
|
---|
140 |
|
---|
141 | typedef struct VBOXUSBFLT_LOCK
|
---|
142 | {
|
---|
143 | KSPIN_LOCK Lock;
|
---|
144 | KIRQL OldIrql;
|
---|
145 | } VBOXUSBFLT_LOCK, *PVBOXUSBFLT_LOCK;
|
---|
146 |
|
---|
147 | #define VBOXUSBFLT_LOCK_INIT() \
|
---|
148 | KeInitializeSpinLock(&g_VBoxUsbFltGlobals.Lock.Lock)
|
---|
149 | #define VBOXUSBFLT_LOCK_TERM() do { } while (0)
|
---|
150 | #define VBOXUSBFLT_LOCK_ACQUIRE() \
|
---|
151 | KeAcquireSpinLock(&g_VBoxUsbFltGlobals.Lock.Lock, &g_VBoxUsbFltGlobals.Lock.OldIrql);
|
---|
152 | #define VBOXUSBFLT_LOCK_RELEASE() \
|
---|
153 | KeReleaseSpinLock(&g_VBoxUsbFltGlobals.Lock.Lock, g_VBoxUsbFltGlobals.Lock.OldIrql);
|
---|
154 |
|
---|
155 |
|
---|
156 | typedef struct VBOXUSBFLT_BLDEV
|
---|
157 | {
|
---|
158 | LIST_ENTRY ListEntry;
|
---|
159 | uint16_t idVendor;
|
---|
160 | uint16_t idProduct;
|
---|
161 | uint16_t bcdDevice;
|
---|
162 | } VBOXUSBFLT_BLDEV, *PVBOXUSBFLT_BLDEV;
|
---|
163 |
|
---|
164 | #define PVBOXUSBFLT_BLDEV_FROM_LE(_pLe) ( (PVBOXUSBFLT_BLDEV)( ((uint8_t*)(_pLe)) - RT_OFFSETOF(VBOXUSBFLT_BLDEV, ListEntry) ) )
|
---|
165 |
|
---|
166 | typedef struct VBOXUSBFLTGLOBALS
|
---|
167 | {
|
---|
168 | LIST_ENTRY DeviceList;
|
---|
169 | LIST_ENTRY ContextList;
|
---|
170 | /* devices known to misbehave */
|
---|
171 | LIST_ENTRY BlackDeviceList;
|
---|
172 | VBOXUSBFLT_LOCK Lock;
|
---|
173 | /** Flag whether to force replugging a device we can't query descirptors from.
|
---|
174 | * Short term workaround for @bugref{9479}. */
|
---|
175 | ULONG dwForceReplugWhenDevPopulateFails;
|
---|
176 | } VBOXUSBFLTGLOBALS, *PVBOXUSBFLTGLOBALS;
|
---|
177 | static VBOXUSBFLTGLOBALS g_VBoxUsbFltGlobals;
|
---|
178 |
|
---|
179 | static bool vboxUsbFltBlDevMatchLocked(uint16_t idVendor, uint16_t idProduct, uint16_t bcdDevice)
|
---|
180 | {
|
---|
181 | for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.BlackDeviceList.Flink;
|
---|
182 | pEntry != &g_VBoxUsbFltGlobals.BlackDeviceList;
|
---|
183 | pEntry = pEntry->Flink)
|
---|
184 | {
|
---|
185 | PVBOXUSBFLT_BLDEV pDev = PVBOXUSBFLT_BLDEV_FROM_LE(pEntry);
|
---|
186 | if (pDev->idVendor != idVendor)
|
---|
187 | continue;
|
---|
188 | if (pDev->idProduct != idProduct)
|
---|
189 | continue;
|
---|
190 | if (pDev->bcdDevice != bcdDevice)
|
---|
191 | continue;
|
---|
192 |
|
---|
193 | return true;
|
---|
194 | }
|
---|
195 | return false;
|
---|
196 | }
|
---|
197 |
|
---|
198 | static NTSTATUS vboxUsbFltBlDevAddLocked(uint16_t idVendor, uint16_t idProduct, uint16_t bcdDevice)
|
---|
199 | {
|
---|
200 | if (vboxUsbFltBlDevMatchLocked(idVendor, idProduct, bcdDevice))
|
---|
201 | return STATUS_SUCCESS;
|
---|
202 | PVBOXUSBFLT_BLDEV pDev = (PVBOXUSBFLT_BLDEV)VBoxUsbMonMemAllocZ(sizeof (*pDev));
|
---|
203 | if (!pDev)
|
---|
204 | {
|
---|
205 | AssertFailed();
|
---|
206 | return STATUS_INSUFFICIENT_RESOURCES;
|
---|
207 | }
|
---|
208 |
|
---|
209 | pDev->idVendor = idVendor;
|
---|
210 | pDev->idProduct = idProduct;
|
---|
211 | pDev->bcdDevice = bcdDevice;
|
---|
212 | InsertHeadList(&g_VBoxUsbFltGlobals.BlackDeviceList, &pDev->ListEntry);
|
---|
213 | return STATUS_SUCCESS;
|
---|
214 | }
|
---|
215 |
|
---|
216 | static void vboxUsbFltBlDevClearLocked()
|
---|
217 | {
|
---|
218 | PLIST_ENTRY pNext;
|
---|
219 | for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.BlackDeviceList.Flink;
|
---|
220 | pEntry != &g_VBoxUsbFltGlobals.BlackDeviceList;
|
---|
221 | pEntry = pNext)
|
---|
222 | {
|
---|
223 | pNext = pEntry->Flink;
|
---|
224 | VBoxUsbMonMemFree(pEntry);
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | static void vboxUsbFltBlDevPopulateWithKnownLocked()
|
---|
229 | {
|
---|
230 | /* this one halts when trying to get string descriptors from it */
|
---|
231 | vboxUsbFltBlDevAddLocked(0x5ac, 0x921c, 0x115);
|
---|
232 | }
|
---|
233 |
|
---|
234 |
|
---|
235 | DECLINLINE(void) vboxUsbFltDevRetain(PVBOXUSBFLT_DEVICE pDevice)
|
---|
236 | {
|
---|
237 | Assert(pDevice->cRefs);
|
---|
238 | ASMAtomicIncU32(&pDevice->cRefs);
|
---|
239 | }
|
---|
240 |
|
---|
241 | static void vboxUsbFltDevDestroy(PVBOXUSBFLT_DEVICE pDevice)
|
---|
242 | {
|
---|
243 | Assert(!pDevice->cRefs);
|
---|
244 | Assert(pDevice->enmState == VBOXUSBFLT_DEVSTATE_REMOVED);
|
---|
245 | VBoxUsbMonMemFree(pDevice);
|
---|
246 | }
|
---|
247 |
|
---|
248 | DECLINLINE(void) vboxUsbFltDevRelease(PVBOXUSBFLT_DEVICE pDevice)
|
---|
249 | {
|
---|
250 | uint32_t cRefs = ASMAtomicDecU32(&pDevice->cRefs);
|
---|
251 | Assert(cRefs < UINT32_MAX/2);
|
---|
252 | if (!cRefs)
|
---|
253 | {
|
---|
254 | vboxUsbFltDevDestroy(pDevice);
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | static void vboxUsbFltDevOwnerSetLocked(PVBOXUSBFLT_DEVICE pDevice, PVBOXUSBFLTCTX pContext, uintptr_t uFltId, bool fIsOneShot)
|
---|
259 | {
|
---|
260 | ASSERT_WARN(!pDevice->pOwner, ("device 0x%p has an owner(0x%p)", pDevice, pDevice->pOwner));
|
---|
261 | ++pContext->cActiveFilters;
|
---|
262 | pDevice->pOwner = pContext;
|
---|
263 | pDevice->uFltId = uFltId;
|
---|
264 | pDevice->fIsFilterOneShot = fIsOneShot;
|
---|
265 | }
|
---|
266 |
|
---|
267 | static void vboxUsbFltDevOwnerClearLocked(PVBOXUSBFLT_DEVICE pDevice)
|
---|
268 | {
|
---|
269 | ASSERT_WARN(pDevice->pOwner, ("no owner for device 0x%p", pDevice));
|
---|
270 | --pDevice->pOwner->cActiveFilters;
|
---|
271 | ASSERT_WARN(pDevice->pOwner->cActiveFilters < UINT32_MAX/2, ("cActiveFilters (%d)", pDevice->pOwner->cActiveFilters));
|
---|
272 | pDevice->pOwner = NULL;
|
---|
273 | pDevice->uFltId = 0;
|
---|
274 | }
|
---|
275 |
|
---|
276 | static void vboxUsbFltDevOwnerUpdateLocked(PVBOXUSBFLT_DEVICE pDevice, PVBOXUSBFLTCTX pContext, uintptr_t uFltId, bool fIsOneShot)
|
---|
277 | {
|
---|
278 | if (pDevice->pOwner != pContext)
|
---|
279 | {
|
---|
280 | if (pDevice->pOwner)
|
---|
281 | vboxUsbFltDevOwnerClearLocked(pDevice);
|
---|
282 | if (pContext)
|
---|
283 | vboxUsbFltDevOwnerSetLocked(pDevice, pContext, uFltId, fIsOneShot);
|
---|
284 | }
|
---|
285 | else if (pContext)
|
---|
286 | {
|
---|
287 | pDevice->uFltId = uFltId;
|
---|
288 | pDevice->fIsFilterOneShot = fIsOneShot;
|
---|
289 | }
|
---|
290 | }
|
---|
291 |
|
---|
292 | static PVBOXUSBFLT_DEVICE vboxUsbFltDevGetLocked(PDEVICE_OBJECT pPdo)
|
---|
293 | {
|
---|
294 | #ifdef VBOX_USB_WITH_VERBOSE_LOGGING
|
---|
295 | for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
|
---|
296 | pEntry != &g_VBoxUsbFltGlobals.DeviceList;
|
---|
297 | pEntry = pEntry->Flink)
|
---|
298 | {
|
---|
299 | PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
|
---|
300 | for (PLIST_ENTRY pEntry2 = pEntry->Flink;
|
---|
301 | pEntry2 != &g_VBoxUsbFltGlobals.DeviceList;
|
---|
302 | pEntry2 = pEntry2->Flink)
|
---|
303 | {
|
---|
304 | PVBOXUSBFLT_DEVICE pDevice2 = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry2);
|
---|
305 | ASSERT_WARN( pDevice->idVendor != pDevice2->idVendor
|
---|
306 | || pDevice->idProduct != pDevice2->idProduct
|
---|
307 | || pDevice->bcdDevice != pDevice2->bcdDevice, ("duplicate devices in a list!!"));
|
---|
308 | }
|
---|
309 | }
|
---|
310 | #endif
|
---|
311 | for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
|
---|
312 | pEntry != &g_VBoxUsbFltGlobals.DeviceList;
|
---|
313 | pEntry = pEntry->Flink)
|
---|
314 | {
|
---|
315 | PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
|
---|
316 | ASSERT_WARN( pDevice->enmState == VBOXUSBFLT_DEVSTATE_REPLUGGING
|
---|
317 | || pDevice->enmState == VBOXUSBFLT_DEVSTATE_UNCAPTURED
|
---|
318 | || pDevice->enmState == VBOXUSBFLT_DEVSTATE_CAPTURING
|
---|
319 | || pDevice->enmState == VBOXUSBFLT_DEVSTATE_CAPTURED
|
---|
320 | || pDevice->enmState == VBOXUSBFLT_DEVSTATE_USED_BY_GUEST,
|
---|
321 | ("Invalid device state(%d) for device(0x%p) PDO(0x%p)", pDevice->enmState, pDevice, pDevice->Pdo));
|
---|
322 | if (pDevice->Pdo == pPdo)
|
---|
323 | return pDevice;
|
---|
324 | }
|
---|
325 | return NULL;
|
---|
326 | }
|
---|
327 |
|
---|
328 | static NTSTATUS vboxUsbFltPdoReplug(PDEVICE_OBJECT pDo)
|
---|
329 | {
|
---|
330 | LOG(("Replugging PDO(0x%p)", pDo));
|
---|
331 | NTSTATUS Status = VBoxUsbToolIoInternalCtlSendSync(pDo, IOCTL_INTERNAL_USB_CYCLE_PORT, NULL, NULL);
|
---|
332 | ASSERT_WARN(Status == STATUS_SUCCESS, ("replugging PDO(0x%p) failed Status(0x%x)", pDo, Status));
|
---|
333 | LOG(("Replugging PDO(0x%p) done with Status(0x%x)", pDo, Status));
|
---|
334 | return Status;
|
---|
335 | }
|
---|
336 |
|
---|
337 | static bool vboxUsbFltDevCanBeCaptured(PVBOXUSBFLT_DEVICE pDevice)
|
---|
338 | {
|
---|
339 | if (pDevice->bClass == USB_DEVICE_CLASS_HUB)
|
---|
340 | {
|
---|
341 | LOG(("device (0x%p), pdo (0x%p) is a hub, can not be captured", pDevice, pDevice->Pdo));
|
---|
342 | return false;
|
---|
343 | }
|
---|
344 | return true;
|
---|
345 | }
|
---|
346 |
|
---|
347 | static PVBOXUSBFLTCTX vboxUsbFltDevMatchLocked(PVBOXUSBFLT_DEVICE pDevice, uintptr_t *puId, bool fRemoveFltIfOneShot, bool *pfFilter, bool *pfIsOneShot)
|
---|
348 | {
|
---|
349 | *puId = 0;
|
---|
350 | *pfFilter = false;
|
---|
351 | *pfIsOneShot = false;
|
---|
352 | if (!vboxUsbFltDevCanBeCaptured(pDevice))
|
---|
353 | {
|
---|
354 | LOG(("vboxUsbFltDevCanBeCaptured returned false"));
|
---|
355 | return NULL;
|
---|
356 | }
|
---|
357 |
|
---|
358 | USBFILTER DevFlt;
|
---|
359 | USBFilterInit(&DevFlt, USBFILTERTYPE_CAPTURE);
|
---|
360 | USBFilterSetNumExact(&DevFlt, USBFILTERIDX_VENDOR_ID, pDevice->idVendor, true);
|
---|
361 | USBFilterSetNumExact(&DevFlt, USBFILTERIDX_PRODUCT_ID, pDevice->idProduct, true);
|
---|
362 | USBFilterSetNumExact(&DevFlt, USBFILTERIDX_DEVICE_REV, pDevice->bcdDevice, true);
|
---|
363 |
|
---|
364 | /* If we could not read a string descriptor, don't set the filter item at all. */
|
---|
365 | if (pDevice->szMfgName[0])
|
---|
366 | USBFilterSetStringExact(&DevFlt, USBFILTERIDX_MANUFACTURER_STR, pDevice->szMfgName, true /*fMustBePresent*/, true /*fPurge*/);
|
---|
367 | if (pDevice->szProduct[0])
|
---|
368 | USBFilterSetStringExact(&DevFlt, USBFILTERIDX_PRODUCT_STR, pDevice->szProduct, true /*fMustBePresent*/, true /*fPurge*/);
|
---|
369 | if (pDevice->szSerial[0])
|
---|
370 | USBFilterSetStringExact(&DevFlt, USBFILTERIDX_SERIAL_NUMBER_STR, pDevice->szSerial, true /*fMustBePresent*/, true /*fPurge*/);
|
---|
371 |
|
---|
372 | /* If device descriptor had to be inferred from PnP Manager data, the class/subclass/protocol may be wrong.
|
---|
373 | * When Windows reports CompatibleIDs 'USB\Class_03&SubClass_00&Prot_00', the device descriptor might be
|
---|
374 | * reporting class 3 (HID), *or* the device descriptor might be reporting class 0 (specified by interface)
|
---|
375 | * and the device's interface reporting class 3. Ignore the class/subclass/protocol in such case, since
|
---|
376 | * we are more or less guaranteed to rely on VID/PID anyway.
|
---|
377 | * See @bugref{9479}.
|
---|
378 | */
|
---|
379 | if (pDevice->fInferredDesc)
|
---|
380 | {
|
---|
381 | LOG(("Device descriptor was not read, only inferred; ignoring class/subclass/protocol!"));
|
---|
382 | }
|
---|
383 | else
|
---|
384 | {
|
---|
385 | LOG(("Setting filter class/subclass/protocol %02X/%02X/%02X\n", pDevice->bClass, pDevice->bSubClass, pDevice->bProtocol));
|
---|
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 | }
|
---|
390 |
|
---|
391 | /* If the port number looks valid, add it to the filter. */
|
---|
392 | if (pDevice->bPort)
|
---|
393 | {
|
---|
394 | LOG(("Setting filter port %04X\n", pDevice->bPort));
|
---|
395 | USBFilterSetNumExact(&DevFlt, USBFILTERIDX_PORT, pDevice->bPort, true);
|
---|
396 | }
|
---|
397 | else
|
---|
398 | LOG(("Port number not known, ignoring!"));
|
---|
399 |
|
---|
400 | /* Run filters on the thing. */
|
---|
401 | PVBOXUSBFLTCTX pOwner = VBoxUSBFilterMatchEx(&DevFlt, puId, fRemoveFltIfOneShot, pfFilter, pfIsOneShot);
|
---|
402 | USBFilterDelete(&DevFlt);
|
---|
403 | return pOwner;
|
---|
404 | }
|
---|
405 |
|
---|
406 | static void vboxUsbFltDevStateMarkReplugLocked(PVBOXUSBFLT_DEVICE pDevice)
|
---|
407 | {
|
---|
408 | vboxUsbFltDevOwnerUpdateLocked(pDevice, NULL, 0, false);
|
---|
409 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_REPLUGGING;
|
---|
410 | }
|
---|
411 |
|
---|
412 | static bool vboxUsbFltDevStateIsNotFiltered(PVBOXUSBFLT_DEVICE pDevice)
|
---|
413 | {
|
---|
414 | return pDevice->enmState == VBOXUSBFLT_DEVSTATE_UNCAPTURED;
|
---|
415 | }
|
---|
416 |
|
---|
417 | static bool vboxUsbFltDevStateIsFiltered(PVBOXUSBFLT_DEVICE pDevice)
|
---|
418 | {
|
---|
419 | return pDevice->enmState >= VBOXUSBFLT_DEVSTATE_CAPTURING;
|
---|
420 | }
|
---|
421 |
|
---|
422 | static uint16_t vboxUsbParseHexNumU16(WCHAR **ppStr)
|
---|
423 | {
|
---|
424 | WCHAR *pStr = *ppStr;
|
---|
425 | WCHAR wc;
|
---|
426 | uint16_t num = 0;
|
---|
427 | unsigned u;
|
---|
428 |
|
---|
429 | for (int i = 0; i < 4; ++i)
|
---|
430 | {
|
---|
431 | if (!*pStr) /* Just in case the string is too short. */
|
---|
432 | break;
|
---|
433 |
|
---|
434 | wc = *pStr;
|
---|
435 | u = wc >= 'A' ? wc - 'A' + 10 : wc - '0'; /* Hex digit to number. */
|
---|
436 | num |= u << (12 - 4 * i);
|
---|
437 | pStr++;
|
---|
438 | }
|
---|
439 | *ppStr = pStr;
|
---|
440 |
|
---|
441 | return num;
|
---|
442 | }
|
---|
443 |
|
---|
444 | static uint8_t vboxUsbParseHexNumU8(WCHAR **ppStr)
|
---|
445 | {
|
---|
446 | WCHAR *pStr = *ppStr;
|
---|
447 | WCHAR wc;
|
---|
448 | uint16_t num = 0;
|
---|
449 | unsigned u;
|
---|
450 |
|
---|
451 | for (int i = 0; i < 2; ++i)
|
---|
452 | {
|
---|
453 | if (!*pStr) /* Just in case the string is too short. */
|
---|
454 | break;
|
---|
455 |
|
---|
456 | wc = *pStr;
|
---|
457 | u = wc >= 'A' ? wc - 'A' + 10 : wc - '0'; /* Hex digit to number. */
|
---|
458 | num |= u << (4 - 4 * i);
|
---|
459 | pStr++;
|
---|
460 | }
|
---|
461 | *ppStr = pStr;
|
---|
462 |
|
---|
463 | return num;
|
---|
464 | }
|
---|
465 |
|
---|
466 | static bool vboxUsbParseHardwareID(WCHAR *pchIdStr, uint16_t *pVid, uint16_t *pPid, uint16_t *pRev)
|
---|
467 | {
|
---|
468 | #define VID_PREFIX L"USB\\VID_"
|
---|
469 | #define PID_PREFIX L"&PID_"
|
---|
470 | #define REV_PREFIX L"&REV_"
|
---|
471 |
|
---|
472 | *pVid = *pPid = *pRev = 0xFFFF;
|
---|
473 |
|
---|
474 | /* The Hardware ID is in the format USB\VID_xxxx&PID_xxxx&REV_xxxx, with 'xxxx'
|
---|
475 | * being 16-bit hexadecimal numbers. The string is coming from the
|
---|
476 | * Windows PnP manager so OEMs should have no opportunity to mess it up.
|
---|
477 | */
|
---|
478 |
|
---|
479 | if (wcsncmp(pchIdStr, VID_PREFIX, wcslen(VID_PREFIX)))
|
---|
480 | return false;
|
---|
481 | /* Point to the start of the vendor ID number and parse it. */
|
---|
482 | pchIdStr += wcslen(VID_PREFIX);
|
---|
483 | *pVid = vboxUsbParseHexNumU16(&pchIdStr);
|
---|
484 |
|
---|
485 | if (wcsncmp(pchIdStr, PID_PREFIX, wcslen(PID_PREFIX)))
|
---|
486 | return false;
|
---|
487 | /* Point to the start of the product ID number and parse it. */
|
---|
488 | pchIdStr += wcslen(PID_PREFIX);
|
---|
489 | *pPid = vboxUsbParseHexNumU16(&pchIdStr);
|
---|
490 |
|
---|
491 | /* The revision might not be there; the Windows documentation is not
|
---|
492 | * entirely clear if it will be always present for USB devices or not.
|
---|
493 | * If it's not there, still consider this a success. */
|
---|
494 | if (wcsncmp(pchIdStr, REV_PREFIX, wcslen(REV_PREFIX)))
|
---|
495 | return true;
|
---|
496 |
|
---|
497 | /* Point to the start of the revision number and parse it. */
|
---|
498 | pchIdStr += wcslen(REV_PREFIX);
|
---|
499 | *pRev = vboxUsbParseHexNumU16(&pchIdStr);
|
---|
500 |
|
---|
501 | return true;
|
---|
502 | #undef VID_PREFIX
|
---|
503 | #undef PID_PREFIX
|
---|
504 | #undef REV_PREFIX
|
---|
505 | }
|
---|
506 |
|
---|
507 | static bool vboxUsbParseCompatibleIDs(WCHAR *pchIdStr, uint8_t *pClass, uint8_t *pSubClass, uint8_t *pProt)
|
---|
508 | {
|
---|
509 | #define CLS_PREFIX L"USB\\Class_"
|
---|
510 | #define SUB_PREFIX L"&SubClass_"
|
---|
511 | #define PRO_PREFIX L"&Prot_"
|
---|
512 |
|
---|
513 | *pClass = *pSubClass = *pProt = 0xFF;
|
---|
514 |
|
---|
515 | /* The Compatible IDs string is in the format USB\Class_xx&SubClass_xx&Prot_xx,
|
---|
516 | * with 'xx' being 8-bit hexadecimal numbers. Since this string is provided by the
|
---|
517 | * PnP manager and USB devices always report these as part of the basic USB device
|
---|
518 | * descriptor, we assume all three must be present.
|
---|
519 | */
|
---|
520 |
|
---|
521 | if (wcsncmp(pchIdStr, CLS_PREFIX, wcslen(CLS_PREFIX)))
|
---|
522 | return false;
|
---|
523 | /* Point to the start of the device class and parse it. */
|
---|
524 | pchIdStr += wcslen(CLS_PREFIX);
|
---|
525 | *pClass = vboxUsbParseHexNumU8(&pchIdStr);
|
---|
526 |
|
---|
527 | if (wcsncmp(pchIdStr, SUB_PREFIX, wcslen(SUB_PREFIX)))
|
---|
528 | return false;
|
---|
529 |
|
---|
530 | /* Point to the start of the subclass and parse it. */
|
---|
531 | pchIdStr += wcslen(SUB_PREFIX);
|
---|
532 | *pSubClass = vboxUsbParseHexNumU8(&pchIdStr);
|
---|
533 |
|
---|
534 | if (wcsncmp(pchIdStr, PRO_PREFIX, wcslen(PRO_PREFIX)))
|
---|
535 | return false;
|
---|
536 |
|
---|
537 | /* Point to the start of the protocol and parse it. */
|
---|
538 | pchIdStr += wcslen(PRO_PREFIX);
|
---|
539 | *pProt = vboxUsbParseHexNumU8(&pchIdStr);
|
---|
540 |
|
---|
541 | return true;
|
---|
542 | #undef CLS_PREFIX
|
---|
543 | #undef SUB_PREFIX
|
---|
544 | #undef PRO_PREFIX
|
---|
545 | }
|
---|
546 |
|
---|
547 | #define VBOXUSBMON_POPULATE_REQUEST_TIMEOUT_MS 10000
|
---|
548 |
|
---|
549 | static NTSTATUS vboxUsbFltDevPopulate(PVBOXUSBFLT_DEVICE pDevice, PDEVICE_OBJECT pDo /*, BOOLEAN bPopulateNonFilterProps*/)
|
---|
550 | {
|
---|
551 | NTSTATUS Status;
|
---|
552 | USB_TOPOLOGY_ADDRESS TopoAddr;
|
---|
553 | PUSB_DEVICE_DESCRIPTOR pDevDr = 0;
|
---|
554 | ULONG ulResultLen;
|
---|
555 | DEVPROPTYPE type;
|
---|
556 | WCHAR wchPropBuf[256];
|
---|
557 | uint16_t port;
|
---|
558 | bool rc;
|
---|
559 |
|
---|
560 | pDevice->Pdo = pDo;
|
---|
561 |
|
---|
562 | LOG(("Populating Device(0x%p) for PDO(0x%p)", pDevice, pDo));
|
---|
563 |
|
---|
564 | pDevDr = (PUSB_DEVICE_DESCRIPTOR)VBoxUsbMonMemAllocZ(sizeof(*pDevDr));
|
---|
565 | if (pDevDr == NULL)
|
---|
566 | {
|
---|
567 | WARN(("Failed to alloc mem for urb"));
|
---|
568 | return STATUS_INSUFFICIENT_RESOURCES;
|
---|
569 | }
|
---|
570 |
|
---|
571 | do
|
---|
572 | {
|
---|
573 | pDevice->fInferredDesc = false;
|
---|
574 | Status = VBoxUsbToolGetDescriptor(pDo, pDevDr, sizeof(*pDevDr), USB_DEVICE_DESCRIPTOR_TYPE, 0, 0, VBOXUSBMON_POPULATE_REQUEST_TIMEOUT_MS);
|
---|
575 | if (!NT_SUCCESS(Status))
|
---|
576 | {
|
---|
577 | uint16_t vid, pid, rev;
|
---|
578 | uint8_t cls, sub, prt;
|
---|
579 |
|
---|
580 | WARN(("getting device descriptor failed, Status (0x%x); falling back to IoGetDeviceProperty", Status));
|
---|
581 |
|
---|
582 | /* Try falling back to IoGetDevicePropertyData. */
|
---|
583 | Status = IoGetDevicePropertyData(pDo, &DEVPKEY_Device_HardwareIds, LOCALE_NEUTRAL, 0, sizeof(wchPropBuf), wchPropBuf, &ulResultLen, &type);
|
---|
584 | if (!NT_SUCCESS(Status))
|
---|
585 | {
|
---|
586 | /* This just isn't our day. We have no idea what the device is. */
|
---|
587 | WARN(("IoGetDevicePropertyData failed for DEVPKEY_Device_HardwareIds, Status (0x%x)", Status));
|
---|
588 | break;
|
---|
589 | }
|
---|
590 | rc = vboxUsbParseHardwareID(wchPropBuf, &vid, &pid, &rev);
|
---|
591 | if (!rc)
|
---|
592 | {
|
---|
593 | /* This *really* should not happen. */
|
---|
594 | WARN(("Failed to parse Hardware ID"));
|
---|
595 | break;
|
---|
596 | }
|
---|
597 |
|
---|
598 | /* Now grab the Compatible IDs to get the class/subclass/protocol. */
|
---|
599 | Status = IoGetDevicePropertyData(pDo, &DEVPKEY_Device_CompatibleIds, LOCALE_NEUTRAL, 0, sizeof(wchPropBuf), wchPropBuf, &ulResultLen, &type);
|
---|
600 | if (!NT_SUCCESS(Status))
|
---|
601 | {
|
---|
602 | /* We really kind of need these. */
|
---|
603 | WARN(("IoGetDevicePropertyData failed for DEVPKEY_Device_CompatibleIds, Status (0x%x)", Status));
|
---|
604 | break;
|
---|
605 | }
|
---|
606 | rc = vboxUsbParseCompatibleIDs(wchPropBuf, &cls, &sub, &prt);
|
---|
607 | if (!rc)
|
---|
608 | {
|
---|
609 | /* This *really* should not happen. */
|
---|
610 | WARN(("Failed to parse Hardware ID"));
|
---|
611 | break;
|
---|
612 | }
|
---|
613 |
|
---|
614 | LOG(("Parsed HardwareID: vid=%04X, pid=%04X, rev=%04X, class=%02X, subcls=%02X, prot=%02X", vid, pid, rev, cls, sub, prt));
|
---|
615 | if (vid == 0xFFFF || pid == 0xFFFF)
|
---|
616 | break;
|
---|
617 |
|
---|
618 | LOG(("Successfully fell back to IoGetDeviceProperty result"));
|
---|
619 | pDevDr->idVendor = vid;
|
---|
620 | pDevDr->idProduct = pid;
|
---|
621 | pDevDr->bcdDevice = rev;
|
---|
622 | pDevDr->bDeviceClass = cls;
|
---|
623 | pDevDr->bDeviceSubClass = sub;
|
---|
624 | pDevDr->bDeviceProtocol = prt;
|
---|
625 |
|
---|
626 | /* The USB device class/subclass/protocol may not be accurate. We have to be careful when comparing
|
---|
627 | * and not take mismatches too seriously.
|
---|
628 | */
|
---|
629 | pDevice->fInferredDesc = true;
|
---|
630 | }
|
---|
631 |
|
---|
632 | /* Query the location path. The path is purely a function of the physical device location
|
---|
633 | * and does not change if the device changes, and also does not change depending on
|
---|
634 | * whether the device is captured or not.
|
---|
635 | * NB: We ignore any additional strings and only look at the first one.
|
---|
636 | */
|
---|
637 | Status = IoGetDevicePropertyData(pDo, &DEVPKEY_Device_LocationPaths, LOCALE_NEUTRAL, 0, sizeof(pDevice->szLocationPath), pDevice->szLocationPath, &ulResultLen, &type);
|
---|
638 | if (!NT_SUCCESS(Status))
|
---|
639 | {
|
---|
640 | /* We do need this, but not critically. On Windows 7, we may get STATUS_OBJECT_NAME_NOT_FOUND. */
|
---|
641 | WARN(("IoGetDevicePropertyData failed for DEVPKEY_Device_LocationPaths, Status (0x%x)", Status));
|
---|
642 | }
|
---|
643 | else
|
---|
644 | {
|
---|
645 | LOG_STRW(pDevice->szLocationPath);
|
---|
646 | }
|
---|
647 |
|
---|
648 | // Disabled, but could be used as a fallback instead of IoGetDevicePropertyData; it should work even
|
---|
649 | // when this code is entered from the PnP IRP processing path.
|
---|
650 | #if 0
|
---|
651 | {
|
---|
652 | HUB_DEVICE_CONFIG_INFO HubInfo;
|
---|
653 |
|
---|
654 | memset(&HubInfo, 0, sizeof(HubInfo));
|
---|
655 | HubInfo.Version = 1;
|
---|
656 | HubInfo.Length = sizeof(HubInfo);
|
---|
657 |
|
---|
658 | NTSTATUS Status = VBoxUsbToolIoInternalCtlSendSync(pDo, IOCTL_INTERNAL_USB_GET_DEVICE_CONFIG_INFO, &HubInfo, NULL);
|
---|
659 | ASSERT_WARN(Status == STATUS_SUCCESS, ("GET_DEVICE_CONFIG_INFO for PDO(0x%p) failed Status(0x%x)", pDo, Status));
|
---|
660 | LOG(("Querying hub device config info for PDO(0x%p) done with Status(0x%x)", pDo, Status));
|
---|
661 |
|
---|
662 | if (Status == STATUS_SUCCESS)
|
---|
663 | {
|
---|
664 | uint16_t vid, pid, rev;
|
---|
665 | uint8_t cls, sub, prt;
|
---|
666 |
|
---|
667 | LOG(("Hub flags: %X\n", HubInfo.HubFlags));
|
---|
668 | LOG_STRW(HubInfo.HardwareIds.Buffer);
|
---|
669 | LOG_STRW(HubInfo.CompatibleIds.Buffer);
|
---|
670 | if (HubInfo.DeviceDescription.Buffer)
|
---|
671 | LOG_STRW(HubInfo.DeviceDescription.Buffer);
|
---|
672 |
|
---|
673 | rc = vboxUsbParseHardwareID(HubInfo.HardwareIds.Buffer, &pid, &vid, &rev);
|
---|
674 | if (!rc)
|
---|
675 | {
|
---|
676 | /* This *really* should not happen. */
|
---|
677 | WARN(("Failed to parse Hardware ID"));
|
---|
678 | }
|
---|
679 |
|
---|
680 | /* The CompatibleID the IOCTL gives is not always the same as what the PnP Manager uses
|
---|
681 | * (thanks, Microsoft). It might look like "USB\DevClass_00&SubClass_00&Prot_00" or like
|
---|
682 | * "USB\USB30_HUB". In such cases, we must consider the class/subclass/protocol
|
---|
683 | * information simply unavailable.
|
---|
684 | */
|
---|
685 | rc = vboxUsbParseCompatibleIDs(HubInfo.CompatibleIds.Buffer, &cls, &sub, &prt);
|
---|
686 | if (!rc)
|
---|
687 | {
|
---|
688 | /* This is unfortunate but not fatal. */
|
---|
689 | WARN(("Failed to parse Compatible ID"));
|
---|
690 | }
|
---|
691 | LOG(("Parsed HardwareID from IOCTL: vid=%04X, pid=%04X, rev=%04X, class=%02X, subcls=%02X, prot=%02X", vid, pid, rev, cls, sub, prt));
|
---|
692 |
|
---|
693 | ExFreePool(HubInfo.HardwareIds.Buffer);
|
---|
694 | ExFreePool(HubInfo.CompatibleIds.Buffer);
|
---|
695 | if (HubInfo.DeviceDescription.Buffer)
|
---|
696 | ExFreePool(HubInfo.DeviceDescription.Buffer);
|
---|
697 | }
|
---|
698 | }
|
---|
699 | #endif
|
---|
700 |
|
---|
701 | /* Query the topology address from the hub driver. This is not trivial to translate to the location
|
---|
702 | * path, but at least we can get the port number this way.
|
---|
703 | */
|
---|
704 | memset(&TopoAddr, 0, sizeof(TopoAddr));
|
---|
705 | Status = VBoxUsbToolIoInternalCtlSendSync(pDo, IOCTL_INTERNAL_USB_GET_TOPOLOGY_ADDRESS, &TopoAddr, NULL);
|
---|
706 | ASSERT_WARN(Status == STATUS_SUCCESS, ("GET_TOPOLOGY_ADDRESS for PDO(0x%p) failed Status(0x%x)", pDo, Status));
|
---|
707 | LOG(("Querying topology address for PDO(0x%p) done with Status(0x%x)", pDo, Status));
|
---|
708 |
|
---|
709 | port = 0;
|
---|
710 | if (Status == STATUS_SUCCESS)
|
---|
711 | {
|
---|
712 | uint16_t *pPort = &TopoAddr.RootHubPortNumber;
|
---|
713 |
|
---|
714 | /* The last non-zero port number is the one we're looking for. It might be on the
|
---|
715 | * root hub directly, or on some downstream hub.
|
---|
716 | */
|
---|
717 | for (int i = 0; i < RT_ELEMENTS(TopoAddr.HubPortNumber) + 1; ++i) {
|
---|
718 | if (*pPort)
|
---|
719 | port = *pPort;
|
---|
720 | pPort++;
|
---|
721 | }
|
---|
722 | LOG(("PCI bus/dev/fn: %02X:%02X:%02X, parsed port: %u\n", TopoAddr.PciBusNumber, TopoAddr.PciDeviceNumber, TopoAddr.PciFunctionNumber, port));
|
---|
723 | LOG(("RH port: %u, hub ports: %u/%u/%u/%u/%u/%u\n", TopoAddr.RootHubPortNumber, TopoAddr.HubPortNumber[0],
|
---|
724 | TopoAddr.HubPortNumber[1], TopoAddr.HubPortNumber[2], TopoAddr.HubPortNumber[3], TopoAddr.HubPortNumber[4], TopoAddr.HubPortNumber[5]));
|
---|
725 |
|
---|
726 | /* In the extremely unlikely case that the port number does not fit into 8 bits, force
|
---|
727 | * it to zero to indicate that we can't use it.
|
---|
728 | */
|
---|
729 | if (port > 255)
|
---|
730 | port = 0;
|
---|
731 | }
|
---|
732 |
|
---|
733 | if (vboxUsbFltBlDevMatchLocked(pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice))
|
---|
734 | {
|
---|
735 | WARN(("found a known black list device, vid(0x%x), pid(0x%x), rev(0x%x)", pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice));
|
---|
736 | Status = STATUS_UNSUCCESSFUL;
|
---|
737 | break;
|
---|
738 | }
|
---|
739 |
|
---|
740 | LOG(("Device pid=%x vid=%x rev=%x port=%x", pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice, port));
|
---|
741 | pDevice->bPort = port;
|
---|
742 | pDevice->idVendor = pDevDr->idVendor;
|
---|
743 | pDevice->idProduct = pDevDr->idProduct;
|
---|
744 | pDevice->bcdDevice = pDevDr->bcdDevice;
|
---|
745 | pDevice->bClass = pDevDr->bDeviceClass;
|
---|
746 | pDevice->bSubClass = pDevDr->bDeviceSubClass;
|
---|
747 | pDevice->bProtocol = pDevDr->bDeviceProtocol;
|
---|
748 | pDevice->szSerial[0] = 0;
|
---|
749 | pDevice->szMfgName[0] = 0;
|
---|
750 | pDevice->szProduct[0] = 0;
|
---|
751 |
|
---|
752 | /* If there are no strings, don't even try to get any string descriptors. */
|
---|
753 | if (pDevDr->iSerialNumber || pDevDr->iManufacturer || pDevDr->iProduct)
|
---|
754 | {
|
---|
755 | int langId;
|
---|
756 |
|
---|
757 | Status = VBoxUsbToolGetLangID(pDo, &langId, VBOXUSBMON_POPULATE_REQUEST_TIMEOUT_MS);
|
---|
758 | if (!NT_SUCCESS(Status))
|
---|
759 | {
|
---|
760 | WARN(("reading language ID failed"));
|
---|
761 | if (Status == STATUS_CANCELLED)
|
---|
762 | {
|
---|
763 | WARN(("found a new black list device, vid(0x%x), pid(0x%x), rev(0x%x)", pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice));
|
---|
764 | vboxUsbFltBlDevAddLocked(pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice);
|
---|
765 | Status = STATUS_UNSUCCESSFUL;
|
---|
766 | }
|
---|
767 | break;
|
---|
768 | }
|
---|
769 |
|
---|
770 | if (pDevDr->iSerialNumber)
|
---|
771 | {
|
---|
772 | Status = VBoxUsbToolGetStringDescriptor(pDo, pDevice->szSerial, sizeof (pDevice->szSerial), pDevDr->iSerialNumber, langId, VBOXUSBMON_POPULATE_REQUEST_TIMEOUT_MS);
|
---|
773 | if (!NT_SUCCESS(Status))
|
---|
774 | {
|
---|
775 | WARN(("reading serial number failed"));
|
---|
776 | ASSERT_WARN(pDevice->szSerial[0] == '\0', ("serial is not zero!!"));
|
---|
777 | if (Status == STATUS_CANCELLED)
|
---|
778 | {
|
---|
779 | WARN(("found a new black list device, vid(0x%x), pid(0x%x), rev(0x%x)", pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice));
|
---|
780 | vboxUsbFltBlDevAddLocked(pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice);
|
---|
781 | Status = STATUS_UNSUCCESSFUL;
|
---|
782 | break;
|
---|
783 | }
|
---|
784 | LOG(("pretending success.."));
|
---|
785 | Status = STATUS_SUCCESS;
|
---|
786 | }
|
---|
787 | }
|
---|
788 |
|
---|
789 | if (pDevDr->iManufacturer)
|
---|
790 | {
|
---|
791 | Status = VBoxUsbToolGetStringDescriptor(pDo, pDevice->szMfgName, sizeof (pDevice->szMfgName), pDevDr->iManufacturer, langId, VBOXUSBMON_POPULATE_REQUEST_TIMEOUT_MS);
|
---|
792 | if (!NT_SUCCESS(Status))
|
---|
793 | {
|
---|
794 | WARN(("reading manufacturer name failed"));
|
---|
795 | ASSERT_WARN(pDevice->szMfgName[0] == '\0', ("szMfgName is not zero!!"));
|
---|
796 | if (Status == STATUS_CANCELLED)
|
---|
797 | {
|
---|
798 | WARN(("found a new black list device, vid(0x%x), pid(0x%x), rev(0x%x)", pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice));
|
---|
799 | vboxUsbFltBlDevAddLocked(pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice);
|
---|
800 | Status = STATUS_UNSUCCESSFUL;
|
---|
801 | break;
|
---|
802 | }
|
---|
803 | LOG(("pretending success.."));
|
---|
804 | Status = STATUS_SUCCESS;
|
---|
805 | }
|
---|
806 | }
|
---|
807 |
|
---|
808 | if (pDevDr->iProduct)
|
---|
809 | {
|
---|
810 | Status = VBoxUsbToolGetStringDescriptor(pDo, pDevice->szProduct, sizeof (pDevice->szProduct), pDevDr->iProduct, langId, VBOXUSBMON_POPULATE_REQUEST_TIMEOUT_MS);
|
---|
811 | if (!NT_SUCCESS(Status))
|
---|
812 | {
|
---|
813 | WARN(("reading product name failed"));
|
---|
814 | ASSERT_WARN(pDevice->szProduct[0] == '\0', ("szProduct is not zero!!"));
|
---|
815 | if (Status == STATUS_CANCELLED)
|
---|
816 | {
|
---|
817 | WARN(("found a new black list device, vid(0x%x), pid(0x%x), rev(0x%x)", pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice));
|
---|
818 | vboxUsbFltBlDevAddLocked(pDevDr->idVendor, pDevDr->idProduct, pDevDr->bcdDevice);
|
---|
819 | Status = STATUS_UNSUCCESSFUL;
|
---|
820 | break;
|
---|
821 | }
|
---|
822 | LOG(("pretending success.."));
|
---|
823 | Status = STATUS_SUCCESS;
|
---|
824 | }
|
---|
825 | }
|
---|
826 |
|
---|
827 | LOG((": strings: '%s':'%s':'%s' (lang ID %x)",
|
---|
828 | pDevice->szMfgName, pDevice->szProduct, pDevice->szSerial, langId));
|
---|
829 | }
|
---|
830 |
|
---|
831 | LOG(("Populating Device(0x%p) for PDO(0x%p) Succeeded", pDevice, pDo));
|
---|
832 | Status = STATUS_SUCCESS;
|
---|
833 | } while (0);
|
---|
834 |
|
---|
835 | VBoxUsbMonMemFree(pDevDr);
|
---|
836 | LOG(("Populating Device(0x%p) for PDO(0x%p) Done, Status (0x%x)", pDevice, pDo, Status));
|
---|
837 | return Status;
|
---|
838 | }
|
---|
839 |
|
---|
840 | static bool vboxUsbFltDevCheckReplugLocked(PVBOXUSBFLT_DEVICE pDevice, PVBOXUSBFLTCTX pContext)
|
---|
841 | {
|
---|
842 | ASSERT_WARN(pContext, ("context is NULL!"));
|
---|
843 |
|
---|
844 | LOG(("Current context is (0x%p)", pContext));
|
---|
845 | LOG(("Current Device owner is (0x%p)", pDevice->pOwner));
|
---|
846 |
|
---|
847 | /* check if device is already replugging */
|
---|
848 | if (pDevice->enmState <= VBOXUSBFLT_DEVSTATE_ADDED)
|
---|
849 | {
|
---|
850 | LOG(("Device (0x%p) is already replugging, return..", pDevice));
|
---|
851 | /* it is, do nothing */
|
---|
852 | ASSERT_WARN(pDevice->enmState == VBOXUSBFLT_DEVSTATE_REPLUGGING,
|
---|
853 | ("Device (0x%p) state is NOT REPLUGGING (%d)", pDevice, pDevice->enmState));
|
---|
854 | return false;
|
---|
855 | }
|
---|
856 |
|
---|
857 | if (pDevice->pOwner && pContext != pDevice->pOwner)
|
---|
858 | {
|
---|
859 | LOG(("Device (0x%p) is owned by another context(0x%p), current is(0x%p)", pDevice, pDevice->pOwner, pContext));
|
---|
860 | /* this device is owned by another context, we're not allowed to do anything */
|
---|
861 | return false;
|
---|
862 | }
|
---|
863 |
|
---|
864 | uintptr_t uId = 0;
|
---|
865 | bool bNeedReplug = false;
|
---|
866 | bool fFilter = false;
|
---|
867 | bool fIsOneShot = false;
|
---|
868 | PVBOXUSBFLTCTX pNewOwner = vboxUsbFltDevMatchLocked(pDevice, &uId,
|
---|
869 | false, /* do not remove a one-shot filter */
|
---|
870 | &fFilter, &fIsOneShot);
|
---|
871 | LOG(("Matching Info: Filter (0x%p), NewOwner(0x%p), fFilter(%d), fIsOneShot(%d)", uId, pNewOwner, (int)fFilter, (int)fIsOneShot));
|
---|
872 | if (pDevice->pOwner && pNewOwner && pDevice->pOwner != pNewOwner)
|
---|
873 | {
|
---|
874 | LOG(("Matching: Device (0x%p) is requested another owner(0x%p), current is(0x%p)", pDevice, pNewOwner, pDevice->pOwner));
|
---|
875 | /* the device is owned by another owner, we can not change the owner here */
|
---|
876 | return false;
|
---|
877 | }
|
---|
878 |
|
---|
879 | if (!fFilter)
|
---|
880 | {
|
---|
881 | LOG(("Matching: Device (0x%p) should NOT be filtered", pDevice));
|
---|
882 | /* the device should NOT be filtered, check the current state */
|
---|
883 | if (vboxUsbFltDevStateIsNotFiltered(pDevice))
|
---|
884 | {
|
---|
885 | LOG(("Device (0x%p) is NOT filtered", pDevice));
|
---|
886 | /* no changes */
|
---|
887 | if (fIsOneShot)
|
---|
888 | {
|
---|
889 | ASSERT_WARN(pNewOwner, ("no new owner"));
|
---|
890 | LOG(("Matching: This is a one-shot filter (0x%p), removing..", uId));
|
---|
891 | /* remove a one-shot filter and keep the original filter data */
|
---|
892 | int tmpRc = VBoxUSBFilterRemove(pNewOwner, uId);
|
---|
893 | ASSERT_WARN(RT_SUCCESS(tmpRc), ("remove filter failed, rc (%d)", tmpRc));
|
---|
894 | if (!pDevice->pOwner)
|
---|
895 | {
|
---|
896 | LOG(("Matching: updating the one-shot owner to (0x%p), fltId(0x%p)", pNewOwner, uId));
|
---|
897 | /* update owner for one-shot if the owner is changed (i.e. assigned) */
|
---|
898 | vboxUsbFltDevOwnerUpdateLocked(pDevice, pNewOwner, uId, true);
|
---|
899 | }
|
---|
900 | else
|
---|
901 | {
|
---|
902 | LOG(("Matching: device already has owner (0x%p) assigned", pDevice->pOwner));
|
---|
903 | }
|
---|
904 | }
|
---|
905 | else
|
---|
906 | {
|
---|
907 | LOG(("Matching: This is NOT a one-shot filter (0x%p), newOwner(0x%p)", uId, pNewOwner));
|
---|
908 | if (pNewOwner)
|
---|
909 | {
|
---|
910 | vboxUsbFltDevOwnerUpdateLocked(pDevice, pNewOwner, uId, false);
|
---|
911 | }
|
---|
912 | }
|
---|
913 | }
|
---|
914 | else
|
---|
915 | {
|
---|
916 | LOG(("Device (0x%p) IS filtered", pDevice));
|
---|
917 | /* the device is currently filtered, we should release it only if
|
---|
918 | * 1. device does not have an owner
|
---|
919 | * or
|
---|
920 | * 2. it should be released bue to a one-shot filter
|
---|
921 | * or
|
---|
922 | * 3. it is NOT grabbed by a one-shot filter */
|
---|
923 | if (!pDevice->pOwner || fIsOneShot || !pDevice->fIsFilterOneShot)
|
---|
924 | {
|
---|
925 | LOG(("Matching: Need replug"));
|
---|
926 | bNeedReplug = true;
|
---|
927 | }
|
---|
928 | }
|
---|
929 | }
|
---|
930 | else
|
---|
931 | {
|
---|
932 | LOG(("Matching: Device (0x%p) SHOULD be filtered", pDevice));
|
---|
933 | /* the device should be filtered, check the current state */
|
---|
934 | ASSERT_WARN(uId, ("zero uid"));
|
---|
935 | ASSERT_WARN(pNewOwner, ("zero pNewOwner"));
|
---|
936 | if (vboxUsbFltDevStateIsFiltered(pDevice))
|
---|
937 | {
|
---|
938 | LOG(("Device (0x%p) IS filtered", pDevice));
|
---|
939 | /* the device is filtered */
|
---|
940 | if (pNewOwner == pDevice->pOwner)
|
---|
941 | {
|
---|
942 | LOG(("Device owner match"));
|
---|
943 | /* no changes */
|
---|
944 | if (fIsOneShot)
|
---|
945 | {
|
---|
946 | LOG(("Matching: This is a one-shot filter (0x%p), removing..", uId));
|
---|
947 | /* remove a one-shot filter and keep the original filter data */
|
---|
948 | int tmpRc = VBoxUSBFilterRemove(pNewOwner, uId);
|
---|
949 | ASSERT_WARN(RT_SUCCESS(tmpRc), ("remove filter failed, rc (%d)", tmpRc));
|
---|
950 | }
|
---|
951 | else
|
---|
952 | {
|
---|
953 | LOG(("Matching: This is NOT a one-shot filter (0x%p), Owner(0x%p)", uId, pDevice->pOwner));
|
---|
954 | vboxUsbFltDevOwnerUpdateLocked(pDevice, pDevice->pOwner, uId, false);
|
---|
955 | }
|
---|
956 | }
|
---|
957 | else
|
---|
958 | {
|
---|
959 | ASSERT_WARN(!pDevice->pOwner, ("device should NOT have owner"));
|
---|
960 | LOG(("Matching: Need replug"));
|
---|
961 | /* the device needs to be filtered, but the owner changes, replug needed */
|
---|
962 | bNeedReplug = true;
|
---|
963 | }
|
---|
964 | }
|
---|
965 | else
|
---|
966 | {
|
---|
967 | /* the device is currently NOT filtered,
|
---|
968 | * we should replug it only if
|
---|
969 | * 1. device does not have an owner
|
---|
970 | * or
|
---|
971 | * 2. it should be captured due to a one-shot filter
|
---|
972 | * or
|
---|
973 | * 3. it is NOT released by a one-shot filter */
|
---|
974 | if (!pDevice->pOwner || fIsOneShot || !pDevice->fIsFilterOneShot)
|
---|
975 | {
|
---|
976 | bNeedReplug = true;
|
---|
977 | LOG(("Matching: Need replug"));
|
---|
978 | }
|
---|
979 | }
|
---|
980 | }
|
---|
981 |
|
---|
982 | if (bNeedReplug)
|
---|
983 | {
|
---|
984 | LOG(("Matching: Device needs replugging, marking as such"));
|
---|
985 | vboxUsbFltDevStateMarkReplugLocked(pDevice);
|
---|
986 | }
|
---|
987 | else
|
---|
988 | {
|
---|
989 | LOG(("Matching: Device does NOT need replugging"));
|
---|
990 | }
|
---|
991 |
|
---|
992 | return bNeedReplug;
|
---|
993 | }
|
---|
994 |
|
---|
995 | static void vboxUsbFltReplugList(PLIST_ENTRY pList)
|
---|
996 | {
|
---|
997 | PLIST_ENTRY pNext;
|
---|
998 | for (PLIST_ENTRY pEntry = pList->Flink;
|
---|
999 | pEntry != pList;
|
---|
1000 | pEntry = pNext)
|
---|
1001 | {
|
---|
1002 | pNext = pEntry->Flink;
|
---|
1003 | PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_REPLUGGINGLE(pEntry);
|
---|
1004 | LOG(("replugging matched PDO(0x%p), pDevice(0x%p)", pDevice->Pdo, pDevice));
|
---|
1005 | ASSERT_WARN(pDevice->enmState == VBOXUSBFLT_DEVSTATE_REPLUGGING
|
---|
1006 | || pDevice->enmState == VBOXUSBFLT_DEVSTATE_REMOVED,
|
---|
1007 | ("invalid state(0x%x) for device(0x%p)", pDevice->enmState, pDevice));
|
---|
1008 |
|
---|
1009 | vboxUsbFltPdoReplug(pDevice->Pdo);
|
---|
1010 | ObDereferenceObject(pDevice->Pdo);
|
---|
1011 | vboxUsbFltDevRelease(pDevice);
|
---|
1012 | }
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 | typedef struct VBOXUSBFLTCHECKWALKER
|
---|
1016 | {
|
---|
1017 | PVBOXUSBFLTCTX pContext;
|
---|
1018 | } VBOXUSBFLTCHECKWALKER, *PVBOXUSBFLTCHECKWALKER;
|
---|
1019 |
|
---|
1020 | static DECLCALLBACK(BOOLEAN) vboxUsbFltFilterCheckWalker(PFILE_OBJECT pHubFile,
|
---|
1021 | PDEVICE_OBJECT pHubDo, PVOID pvContext)
|
---|
1022 | {
|
---|
1023 | PVBOXUSBFLTCHECKWALKER pData = (PVBOXUSBFLTCHECKWALKER)pvContext;
|
---|
1024 | PVBOXUSBFLTCTX pContext = pData->pContext;
|
---|
1025 |
|
---|
1026 | LOG(("Visiting pHubFile(0x%p), pHubDo(0x%p), oContext(0x%p)", pHubFile, pHubDo, pContext));
|
---|
1027 | KIRQL Irql = KeGetCurrentIrql();
|
---|
1028 | ASSERT_WARN(Irql == PASSIVE_LEVEL, ("unexpected IRQL (%d)", Irql));
|
---|
1029 |
|
---|
1030 | PDEVICE_RELATIONS pDevRelations = NULL;
|
---|
1031 |
|
---|
1032 | NTSTATUS Status = VBoxUsbMonQueryBusRelations(pHubDo, pHubFile, &pDevRelations);
|
---|
1033 | if (Status == STATUS_SUCCESS && pDevRelations)
|
---|
1034 | {
|
---|
1035 | ULONG cReplugPdos = pDevRelations->Count;
|
---|
1036 | LIST_ENTRY ReplugDevList;
|
---|
1037 | InitializeListHead(&ReplugDevList);
|
---|
1038 | for (ULONG k = 0; k < pDevRelations->Count; ++k)
|
---|
1039 | {
|
---|
1040 | PDEVICE_OBJECT pDevObj;
|
---|
1041 |
|
---|
1042 | /* Grab the PDO+reference. We won't need the upper layer device object
|
---|
1043 | * anymore, so dereference that right here, and drop the PDO ref later.
|
---|
1044 | */
|
---|
1045 | pDevObj = IoGetDeviceAttachmentBaseRef(pDevRelations->Objects[k]);
|
---|
1046 | LOG(("DevObj=%p, PDO=%p\n", pDevRelations->Objects[k], pDevObj));
|
---|
1047 | ObDereferenceObject(pDevRelations->Objects[k]);
|
---|
1048 | pDevRelations->Objects[k] = pDevObj;
|
---|
1049 |
|
---|
1050 | LOG(("Found existing USB PDO 0x%p", pDevObj));
|
---|
1051 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1052 | PVBOXUSBFLT_DEVICE pDevice = vboxUsbFltDevGetLocked(pDevObj);
|
---|
1053 | if (pDevice)
|
---|
1054 | {
|
---|
1055 | LOG(("Found existing device info (0x%p) for PDO 0x%p", pDevice, pDevObj));
|
---|
1056 | bool bReplug = vboxUsbFltDevCheckReplugLocked(pDevice, pContext);
|
---|
1057 | if (bReplug)
|
---|
1058 | {
|
---|
1059 | LOG(("Replug needed for device (0x%p)", pDevice));
|
---|
1060 | InsertHeadList(&ReplugDevList, &pDevice->RepluggingLe);
|
---|
1061 | vboxUsbFltDevRetain(pDevice);
|
---|
1062 | /* do not dereference object since we will use it later */
|
---|
1063 | }
|
---|
1064 | else
|
---|
1065 | {
|
---|
1066 | LOG(("Replug NOT needed for device (0x%p)", pDevice));
|
---|
1067 | ObDereferenceObject(pDevObj);
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1071 |
|
---|
1072 | pDevRelations->Objects[k] = NULL;
|
---|
1073 | --cReplugPdos;
|
---|
1074 | ASSERT_WARN((uint32_t)cReplugPdos < UINT32_MAX/2, ("cReplugPdos(%d) state broken", cReplugPdos));
|
---|
1075 | continue;
|
---|
1076 | }
|
---|
1077 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1078 |
|
---|
1079 | LOG(("NO device info found for PDO 0x%p", pDevObj));
|
---|
1080 | VBOXUSBFLT_DEVICE Device;
|
---|
1081 | Status = vboxUsbFltDevPopulate(&Device, pDevObj /*, FALSE /* only need filter properties */);
|
---|
1082 | if (NT_SUCCESS(Status))
|
---|
1083 | {
|
---|
1084 | uintptr_t uId = 0;
|
---|
1085 | bool fFilter = false;
|
---|
1086 | bool fIsOneShot = false;
|
---|
1087 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1088 | PVBOXUSBFLTCTX pCtx = vboxUsbFltDevMatchLocked(&Device, &uId,
|
---|
1089 | false, /* do not remove a one-shot filter */
|
---|
1090 | &fFilter, &fIsOneShot);
|
---|
1091 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1092 | NOREF(pCtx);
|
---|
1093 | LOG(("Matching Info: Filter (0x%p), pCtx(0x%p), fFilter(%d), fIsOneShot(%d)", uId, pCtx, (int)fFilter, (int)fIsOneShot));
|
---|
1094 | if (fFilter)
|
---|
1095 | {
|
---|
1096 | LOG(("Matching: This device SHOULD be filtered"));
|
---|
1097 | /* this device needs to be filtered, but it's not,
|
---|
1098 | * leave the PDO in array to issue a replug request for it
|
---|
1099 | * later on */
|
---|
1100 | continue;
|
---|
1101 | }
|
---|
1102 | }
|
---|
1103 | else
|
---|
1104 | {
|
---|
1105 | WARN(("vboxUsbFltDevPopulate for PDO 0x%p failed with Status 0x%x", pDevObj, Status));
|
---|
1106 | if ( Status == STATUS_CANCELLED
|
---|
1107 | && g_VBoxUsbFltGlobals.dwForceReplugWhenDevPopulateFails)
|
---|
1108 | {
|
---|
1109 | /*
|
---|
1110 | * This can happen if the device got suspended and is in D3 state where we can't query any strings.
|
---|
1111 | * There is no known way to set the power state of the device, especially if there is no driver attached yet.
|
---|
1112 | * The sledgehammer approach is to just replug the device to force it out of suspend, see bugref @{9479}.
|
---|
1113 | */
|
---|
1114 | continue;
|
---|
1115 | }
|
---|
1116 | }
|
---|
1117 |
|
---|
1118 | LOG(("Matching: This device should NOT be filtered"));
|
---|
1119 | /* this device should not be filtered, and it's not */
|
---|
1120 | ObDereferenceObject(pDevObj);
|
---|
1121 | pDevRelations->Objects[k] = NULL;
|
---|
1122 | --cReplugPdos;
|
---|
1123 | ASSERT_WARN((uint32_t)cReplugPdos < UINT32_MAX/2, ("cReplugPdos is %d", cReplugPdos));
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 | LOG(("(%d) non-matched PDOs to be replugged", cReplugPdos));
|
---|
1127 |
|
---|
1128 | if (cReplugPdos)
|
---|
1129 | {
|
---|
1130 | for (ULONG k = 0; k < pDevRelations->Count; ++k)
|
---|
1131 | {
|
---|
1132 | if (!pDevRelations->Objects[k])
|
---|
1133 | continue;
|
---|
1134 |
|
---|
1135 | Status = vboxUsbFltPdoReplug(pDevRelations->Objects[k]);
|
---|
1136 | ASSERT_WARN(Status == STATUS_SUCCESS, ("vboxUsbFltPdoReplug failed! Status(0x%x)", Status));
|
---|
1137 | ObDereferenceObject(pDevRelations->Objects[k]);
|
---|
1138 | if (!--cReplugPdos)
|
---|
1139 | break;
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | ASSERT_WARN(!cReplugPdos, ("cReplugPdos reached zero!"));
|
---|
1143 | }
|
---|
1144 |
|
---|
1145 | vboxUsbFltReplugList(&ReplugDevList);
|
---|
1146 |
|
---|
1147 | ExFreePool(pDevRelations);
|
---|
1148 | }
|
---|
1149 | else
|
---|
1150 | {
|
---|
1151 | WARN(("VBoxUsbMonQueryBusRelations failed for hub DO(0x%p), Status(0x%x), pDevRelations(0x%p)",
|
---|
1152 | pHubDo, Status, pDevRelations));
|
---|
1153 | }
|
---|
1154 |
|
---|
1155 | LOG(("Done Visiting pHubFile(0x%p), pHubDo(0x%p), oContext(0x%p)", pHubFile, pHubDo, pContext));
|
---|
1156 |
|
---|
1157 | return TRUE;
|
---|
1158 | }
|
---|
1159 |
|
---|
1160 | NTSTATUS VBoxUsbFltFilterCheck(PVBOXUSBFLTCTX pContext)
|
---|
1161 | {
|
---|
1162 | KIRQL Irql = KeGetCurrentIrql();
|
---|
1163 | ASSERT_WARN(Irql == PASSIVE_LEVEL, ("unexpected IRQL (%d)", Irql));
|
---|
1164 |
|
---|
1165 | LOG(("Running filters, Context (0x%p)..", pContext));
|
---|
1166 |
|
---|
1167 | VBOXUSBFLTCHECKWALKER Data;
|
---|
1168 | Data.pContext = pContext;
|
---|
1169 | vboxUsbMonHubDevWalk(vboxUsbFltFilterCheckWalker, &Data);
|
---|
1170 |
|
---|
1171 | LOG(("DONE Running filters, Context (0x%p)", pContext));
|
---|
1172 |
|
---|
1173 | return STATUS_SUCCESS;
|
---|
1174 | }
|
---|
1175 |
|
---|
1176 | NTSTATUS VBoxUsbFltClose(PVBOXUSBFLTCTX pContext)
|
---|
1177 | {
|
---|
1178 | LOG(("Closing context(0x%p)", pContext));
|
---|
1179 | LIST_ENTRY ReplugDevList;
|
---|
1180 | InitializeListHead(&ReplugDevList);
|
---|
1181 |
|
---|
1182 | ASSERT_WARN(pContext, ("null context"));
|
---|
1183 |
|
---|
1184 | KIRQL Irql = KeGetCurrentIrql();
|
---|
1185 | ASSERT_WARN(Irql == PASSIVE_LEVEL, ("irql==(%d)", Irql));
|
---|
1186 |
|
---|
1187 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1188 |
|
---|
1189 | pContext->bRemoved = TRUE;
|
---|
1190 | RemoveEntryList(&pContext->ListEntry);
|
---|
1191 |
|
---|
1192 | LOG(("removing owner filters"));
|
---|
1193 | /* now re-arrange the filters */
|
---|
1194 | /* 1. remove filters */
|
---|
1195 | VBoxUSBFilterRemoveOwner(pContext);
|
---|
1196 |
|
---|
1197 | LOG(("enumerating devices.."));
|
---|
1198 | /* 2. check if there are devices owned */
|
---|
1199 | for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
|
---|
1200 | pEntry != &g_VBoxUsbFltGlobals.DeviceList;
|
---|
1201 | pEntry = pEntry->Flink)
|
---|
1202 | {
|
---|
1203 | PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
|
---|
1204 | if (pDevice->pOwner != pContext)
|
---|
1205 | continue;
|
---|
1206 |
|
---|
1207 | LOG(("found device(0x%p), pdo(0x%p), state(%d), filter id(0x%p), oneshot(%d)",
|
---|
1208 | pDevice, pDevice->Pdo, pDevice->enmState, pDevice->uFltId, (int)pDevice->fIsFilterOneShot));
|
---|
1209 | ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_ADDED, ("VBOXUSBFLT_DEVSTATE_ADDED state for device(0x%p)", pDevice));
|
---|
1210 | ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_REMOVED, ("VBOXUSBFLT_DEVSTATE_REMOVED state for device(0x%p)", pDevice));
|
---|
1211 |
|
---|
1212 | vboxUsbFltDevOwnerClearLocked(pDevice);
|
---|
1213 |
|
---|
1214 | if (vboxUsbFltDevCheckReplugLocked(pDevice, pContext))
|
---|
1215 | {
|
---|
1216 | LOG(("device needs replug"));
|
---|
1217 | InsertHeadList(&ReplugDevList, &pDevice->RepluggingLe);
|
---|
1218 | /* retain to ensure the device is not removed before we issue a replug */
|
---|
1219 | vboxUsbFltDevRetain(pDevice);
|
---|
1220 | /* keep the PDO alive */
|
---|
1221 | ObReferenceObject(pDevice->Pdo);
|
---|
1222 | }
|
---|
1223 | else
|
---|
1224 | {
|
---|
1225 | LOG(("device does NOT need replug"));
|
---|
1226 | }
|
---|
1227 | }
|
---|
1228 |
|
---|
1229 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1230 |
|
---|
1231 | /* this should replug all devices that were either skipped or grabbed due to the context's */
|
---|
1232 | vboxUsbFltReplugList(&ReplugDevList);
|
---|
1233 |
|
---|
1234 | LOG(("SUCCESS done context(0x%p)", pContext));
|
---|
1235 | return STATUS_SUCCESS;
|
---|
1236 | }
|
---|
1237 |
|
---|
1238 | NTSTATUS VBoxUsbFltCreate(PVBOXUSBFLTCTX pContext)
|
---|
1239 | {
|
---|
1240 | LOG(("Creating context(0x%p)", pContext));
|
---|
1241 | memset(pContext, 0, sizeof (*pContext));
|
---|
1242 | pContext->Process = RTProcSelf();
|
---|
1243 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1244 | InsertHeadList(&g_VBoxUsbFltGlobals.ContextList, &pContext->ListEntry);
|
---|
1245 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1246 | LOG(("SUCCESS context(0x%p)", pContext));
|
---|
1247 | return STATUS_SUCCESS;
|
---|
1248 | }
|
---|
1249 |
|
---|
1250 | int VBoxUsbFltAdd(PVBOXUSBFLTCTX pContext, PUSBFILTER pFilter, uintptr_t *pId)
|
---|
1251 | {
|
---|
1252 | LOG(("adding filter, Context (0x%p)..", pContext));
|
---|
1253 | *pId = 0;
|
---|
1254 | /* LOG the filter details. */
|
---|
1255 | LOG((__FUNCTION__": %s %s %s",
|
---|
1256 | USBFilterGetString(pFilter, USBFILTERIDX_MANUFACTURER_STR) ? USBFilterGetString(pFilter, USBFILTERIDX_MANUFACTURER_STR) : "<null>",
|
---|
1257 | USBFilterGetString(pFilter, USBFILTERIDX_PRODUCT_STR) ? USBFilterGetString(pFilter, USBFILTERIDX_PRODUCT_STR) : "<null>",
|
---|
1258 | USBFilterGetString(pFilter, USBFILTERIDX_SERIAL_NUMBER_STR) ? USBFilterGetString(pFilter, USBFILTERIDX_SERIAL_NUMBER_STR) : "<null>"));
|
---|
1259 | #ifdef VBOX_USB_WITH_VERBOSE_LOGGING
|
---|
1260 | LOG(("VBoxUSBClient::addFilter: idVendor=%#x idProduct=%#x bcdDevice=%#x bDeviceClass=%#x bDeviceSubClass=%#x bDeviceProtocol=%#x bBus=%#x bPort=%#x Type%#x",
|
---|
1261 | USBFilterGetNum(pFilter, USBFILTERIDX_VENDOR_ID),
|
---|
1262 | USBFilterGetNum(pFilter, USBFILTERIDX_PRODUCT_ID),
|
---|
1263 | USBFilterGetNum(pFilter, USBFILTERIDX_DEVICE_REV),
|
---|
1264 | USBFilterGetNum(pFilter, USBFILTERIDX_DEVICE_CLASS),
|
---|
1265 | USBFilterGetNum(pFilter, USBFILTERIDX_DEVICE_SUB_CLASS),
|
---|
1266 | USBFilterGetNum(pFilter, USBFILTERIDX_DEVICE_PROTOCOL),
|
---|
1267 | USBFilterGetNum(pFilter, USBFILTERIDX_BUS),
|
---|
1268 | USBFilterGetNum(pFilter, USBFILTERIDX_PORT),
|
---|
1269 | USBFilterGetFilterType(pFilter)));
|
---|
1270 | #endif
|
---|
1271 |
|
---|
1272 | /* We can't get the bus/port numbers. Ignore them while matching. */
|
---|
1273 | USBFilterSetMustBePresent(pFilter, USBFILTERIDX_BUS, false);
|
---|
1274 | USBFilterSetMustBePresent(pFilter, USBFILTERIDX_PORT, false);
|
---|
1275 |
|
---|
1276 | /* We may not be able to reconstruct the class/subclass/protocol if we aren't able to
|
---|
1277 | * read the device descriptor. Don't require these to be present. See also the fInferredDesc flag.
|
---|
1278 | */
|
---|
1279 | USBFilterSetMustBePresent(pFilter, USBFILTERIDX_DEVICE_CLASS, false);
|
---|
1280 | USBFilterSetMustBePresent(pFilter, USBFILTERIDX_DEVICE_SUB_CLASS, false);
|
---|
1281 | USBFilterSetMustBePresent(pFilter, USBFILTERIDX_DEVICE_PROTOCOL, false);
|
---|
1282 |
|
---|
1283 | /* We may also be unable to read string descriptors. Often the userland can't read the
|
---|
1284 | * string descriptors either because the device is in a low-power state, but it can happen
|
---|
1285 | * that the userland gets lucky and reads the strings, but by the time we get to read them
|
---|
1286 | * they're inaccessible due to power management. So, don't require the strings to be present.
|
---|
1287 | */
|
---|
1288 | USBFilterSetMustBePresent(pFilter, USBFILTERIDX_MANUFACTURER_STR, false);
|
---|
1289 | USBFilterSetMustBePresent(pFilter, USBFILTERIDX_PRODUCT_STR, false);
|
---|
1290 | USBFilterSetMustBePresent(pFilter, USBFILTERIDX_SERIAL_NUMBER_STR, false);
|
---|
1291 |
|
---|
1292 | uintptr_t uId = 0;
|
---|
1293 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1294 | /* Add the filter. */
|
---|
1295 | int rc = VBoxUSBFilterAdd(pFilter, pContext, &uId);
|
---|
1296 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1297 | if (RT_SUCCESS(rc))
|
---|
1298 | {
|
---|
1299 | LOG(("ADDED filter id 0x%p", uId));
|
---|
1300 | ASSERT_WARN(uId, ("uid is NULL"));
|
---|
1301 | #ifdef VBOX_USBMON_WITH_FILTER_AUTOAPPLY
|
---|
1302 | VBoxUsbFltFilterCheck();
|
---|
1303 | #endif
|
---|
1304 | }
|
---|
1305 | else
|
---|
1306 | {
|
---|
1307 | WARN(("VBoxUSBFilterAdd failed rc (%d)", rc));
|
---|
1308 | ASSERT_WARN(!uId, ("uid is not NULL"));
|
---|
1309 | }
|
---|
1310 |
|
---|
1311 | *pId = uId;
|
---|
1312 | return rc;
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 | int VBoxUsbFltRemove(PVBOXUSBFLTCTX pContext, uintptr_t uId)
|
---|
1316 | {
|
---|
1317 | LOG(("removing filter id(0x%p), Context (0x%p)..", pContext, uId));
|
---|
1318 | Assert(uId);
|
---|
1319 |
|
---|
1320 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1321 | int rc = VBoxUSBFilterRemove(pContext, uId);
|
---|
1322 | if (!RT_SUCCESS(rc))
|
---|
1323 | {
|
---|
1324 | WARN(("VBoxUSBFilterRemove failed rc (%d)", rc));
|
---|
1325 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1326 | return rc;
|
---|
1327 | }
|
---|
1328 |
|
---|
1329 | LOG(("enumerating devices.."));
|
---|
1330 | for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
|
---|
1331 | pEntry != &g_VBoxUsbFltGlobals.DeviceList;
|
---|
1332 | pEntry = pEntry->Flink)
|
---|
1333 | {
|
---|
1334 | PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
|
---|
1335 | if (pDevice->fIsFilterOneShot)
|
---|
1336 | {
|
---|
1337 | ASSERT_WARN(!pDevice->uFltId, ("oneshot filter on device(0x%p): unexpected uFltId(%d)", pDevice, pDevice->uFltId));
|
---|
1338 | }
|
---|
1339 |
|
---|
1340 | if (pDevice->uFltId != uId)
|
---|
1341 | continue;
|
---|
1342 |
|
---|
1343 | ASSERT_WARN(pDevice->pOwner == pContext, ("Device(0x%p) owner(0x%p) not match to (0x%p)", pDevice, pDevice->pOwner, pContext));
|
---|
1344 | if (pDevice->pOwner != pContext)
|
---|
1345 | continue;
|
---|
1346 |
|
---|
1347 | LOG(("found device(0x%p), pdo(0x%p), state(%d), filter id(0x%p), oneshot(%d)",
|
---|
1348 | pDevice, pDevice->Pdo, pDevice->enmState, pDevice->uFltId, (int)pDevice->fIsFilterOneShot));
|
---|
1349 | ASSERT_WARN(!pDevice->fIsFilterOneShot, ("device(0x%p) is filtered with a oneshot filter", pDevice));
|
---|
1350 | pDevice->uFltId = 0;
|
---|
1351 | /* clear the fIsFilterOneShot flag to ensure the device is replugged on the next VBoxUsbFltFilterCheck call */
|
---|
1352 | pDevice->fIsFilterOneShot = false;
|
---|
1353 | }
|
---|
1354 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1355 |
|
---|
1356 | LOG(("done enumerating devices"));
|
---|
1357 |
|
---|
1358 | if (RT_SUCCESS(rc))
|
---|
1359 | {
|
---|
1360 | #ifdef VBOX_USBMON_WITH_FILTER_AUTOAPPLY
|
---|
1361 | VBoxUsbFltFilterCheck();
|
---|
1362 | #endif
|
---|
1363 | }
|
---|
1364 | return rc;
|
---|
1365 | }
|
---|
1366 |
|
---|
1367 | static USBDEVICESTATE vboxUsbDevGetUserState(PVBOXUSBFLTCTX pContext, PVBOXUSBFLT_DEVICE pDevice)
|
---|
1368 | {
|
---|
1369 | if (vboxUsbFltDevStateIsNotFiltered(pDevice))
|
---|
1370 | return USBDEVICESTATE_USED_BY_HOST_CAPTURABLE;
|
---|
1371 |
|
---|
1372 | /* the device is filtered, or replugging */
|
---|
1373 | if (pDevice->enmState == VBOXUSBFLT_DEVSTATE_REPLUGGING)
|
---|
1374 | {
|
---|
1375 | ASSERT_WARN(!pDevice->pOwner, ("replugging device(0x%p) still has an owner(0x%p)", pDevice, pDevice->pOwner));
|
---|
1376 | ASSERT_WARN(!pDevice->uFltId, ("replugging device(0x%p) still has filter(0x%p)", pDevice, pDevice->uFltId));
|
---|
1377 | /* no user state for this, we should not return it tu the user */
|
---|
1378 | return USBDEVICESTATE_USED_BY_HOST;
|
---|
1379 | }
|
---|
1380 |
|
---|
1381 | /* the device is filtered, if owner differs from the context, return as USED_BY_HOST */
|
---|
1382 | ASSERT_WARN(pDevice->pOwner, ("device(0x%p) has noowner", pDevice));
|
---|
1383 | /* the id can be null if a filter is removed */
|
---|
1384 | // Assert(pDevice->uFltId);
|
---|
1385 |
|
---|
1386 | if (pDevice->pOwner != pContext)
|
---|
1387 | {
|
---|
1388 | LOG(("Device owner differs from the current context, returning used by host"));
|
---|
1389 | return USBDEVICESTATE_USED_BY_HOST;
|
---|
1390 | }
|
---|
1391 |
|
---|
1392 | switch (pDevice->enmState)
|
---|
1393 | {
|
---|
1394 | case VBOXUSBFLT_DEVSTATE_UNCAPTURED:
|
---|
1395 | case VBOXUSBFLT_DEVSTATE_CAPTURING:
|
---|
1396 | return USBDEVICESTATE_USED_BY_HOST_CAPTURABLE;
|
---|
1397 | case VBOXUSBFLT_DEVSTATE_CAPTURED:
|
---|
1398 | return USBDEVICESTATE_HELD_BY_PROXY;
|
---|
1399 | case VBOXUSBFLT_DEVSTATE_USED_BY_GUEST:
|
---|
1400 | return USBDEVICESTATE_USED_BY_GUEST;
|
---|
1401 | default:
|
---|
1402 | WARN(("unexpected device state(%d) for device(0x%p)", pDevice->enmState, pDevice));
|
---|
1403 | return USBDEVICESTATE_UNSUPPORTED;
|
---|
1404 | }
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | NTSTATUS VBoxUsbFltGetDevice(PVBOXUSBFLTCTX pContext, HVBOXUSBDEVUSR hDevice, PUSBSUP_GETDEV_MON pInfo)
|
---|
1408 | {
|
---|
1409 | if (!hDevice)
|
---|
1410 | return STATUS_INVALID_PARAMETER;
|
---|
1411 |
|
---|
1412 | memset (pInfo, 0, sizeof (*pInfo));
|
---|
1413 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1414 | for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
|
---|
1415 | pEntry != &g_VBoxUsbFltGlobals.DeviceList;
|
---|
1416 | pEntry = pEntry->Flink)
|
---|
1417 | {
|
---|
1418 | PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
|
---|
1419 | Assert(pDevice->enmState != VBOXUSBFLT_DEVSTATE_REMOVED);
|
---|
1420 | Assert(pDevice->enmState != VBOXUSBFLT_DEVSTATE_ADDED);
|
---|
1421 |
|
---|
1422 | if (pDevice != hDevice)
|
---|
1423 | continue;
|
---|
1424 |
|
---|
1425 | USBDEVICESTATE enmUsrState = vboxUsbDevGetUserState(pContext, pDevice);
|
---|
1426 | pInfo->enmState = enmUsrState;
|
---|
1427 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1428 | return STATUS_SUCCESS;
|
---|
1429 | }
|
---|
1430 |
|
---|
1431 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1432 |
|
---|
1433 | /* We should not get this far with valid input. */
|
---|
1434 | return STATUS_INVALID_PARAMETER;
|
---|
1435 | }
|
---|
1436 |
|
---|
1437 | NTSTATUS VBoxUsbFltPdoAdd(PDEVICE_OBJECT pPdo, BOOLEAN *pbFiltered)
|
---|
1438 | {
|
---|
1439 | *pbFiltered = FALSE;
|
---|
1440 | PVBOXUSBFLT_DEVICE pDevice;
|
---|
1441 |
|
---|
1442 | /* Find the real PDO+reference. Dereference when we're done with it. Note that
|
---|
1443 | * the input pPdo was not explicitly referenced so we're not dropping its ref.
|
---|
1444 | */
|
---|
1445 | PDEVICE_OBJECT pDevObj = IoGetDeviceAttachmentBaseRef(pPdo);
|
---|
1446 | LOG(("DevObj=%p, real PDO=%p\n", pPdo, pDevObj));
|
---|
1447 | pPdo = pDevObj;
|
---|
1448 |
|
---|
1449 | /* first check if device is in the a already */
|
---|
1450 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1451 | pDevice = vboxUsbFltDevGetLocked(pPdo);
|
---|
1452 | if (pDevice)
|
---|
1453 | {
|
---|
1454 | LOG(("found device (0x%p), state(%d) for PDO(0x%p)", pDevice, pDevice->enmState, pPdo));
|
---|
1455 | ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_ADDED, ("VBOXUSBFLT_DEVSTATE_ADDED state for device(0x%p)", pDevice));
|
---|
1456 | ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_REMOVED, ("VBOXUSBFLT_DEVSTATE_REMOVED state for device(0x%p)", pDevice));
|
---|
1457 | *pbFiltered = pDevice->enmState >= VBOXUSBFLT_DEVSTATE_CAPTURING;
|
---|
1458 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1459 | ObDereferenceObject(pPdo);
|
---|
1460 | return STATUS_SUCCESS;
|
---|
1461 | }
|
---|
1462 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1463 | pDevice = (PVBOXUSBFLT_DEVICE)VBoxUsbMonMemAllocZ(sizeof (*pDevice));
|
---|
1464 | if (!pDevice)
|
---|
1465 | {
|
---|
1466 | WARN(("VBoxUsbMonMemAllocZ failed"));
|
---|
1467 | ObDereferenceObject(pPdo);
|
---|
1468 | return STATUS_NO_MEMORY;
|
---|
1469 | }
|
---|
1470 |
|
---|
1471 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_ADDED;
|
---|
1472 | pDevice->cRefs = 1;
|
---|
1473 | NTSTATUS Status = vboxUsbFltDevPopulate(pDevice, pPdo /* , TRUE /* need all props */);
|
---|
1474 | if (!NT_SUCCESS(Status))
|
---|
1475 | {
|
---|
1476 | WARN(("vboxUsbFltDevPopulate failed, Status 0x%x", Status));
|
---|
1477 | ObDereferenceObject(pPdo);
|
---|
1478 | VBoxUsbMonMemFree(pDevice);
|
---|
1479 | return Status;
|
---|
1480 | }
|
---|
1481 |
|
---|
1482 | uintptr_t uId;
|
---|
1483 | bool fFilter = false;
|
---|
1484 | bool fIsOneShot = false;
|
---|
1485 | PVBOXUSBFLTCTX pCtx;
|
---|
1486 | PVBOXUSBFLT_DEVICE pTmpDev;
|
---|
1487 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1488 | /* (paranoia) re-check the device is still not here */
|
---|
1489 | pTmpDev = vboxUsbFltDevGetLocked(pPdo);
|
---|
1490 |
|
---|
1491 | /* Drop the PDO ref, now we won't need it anymore. */
|
---|
1492 | ObDereferenceObject(pPdo);
|
---|
1493 |
|
---|
1494 | if (pTmpDev)
|
---|
1495 | {
|
---|
1496 | LOG(("second try: found device (0x%p), state(%d) for PDO(0x%p)", pDevice, pDevice->enmState, pPdo));
|
---|
1497 | ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_ADDED, ("second try: VBOXUSBFLT_DEVSTATE_ADDED state for device(0x%p)", pDevice));
|
---|
1498 | ASSERT_WARN(pDevice->enmState != VBOXUSBFLT_DEVSTATE_REMOVED, ("second try: VBOXUSBFLT_DEVSTATE_REMOVED state for device(0x%p)", pDevice));
|
---|
1499 | *pbFiltered = pTmpDev->enmState >= VBOXUSBFLT_DEVSTATE_CAPTURING;
|
---|
1500 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1501 | VBoxUsbMonMemFree(pDevice);
|
---|
1502 | return STATUS_SUCCESS;
|
---|
1503 | }
|
---|
1504 |
|
---|
1505 | LOG(("Created Device 0x%p for PDO 0x%p", pDevice, pPdo));
|
---|
1506 |
|
---|
1507 | pCtx = vboxUsbFltDevMatchLocked(pDevice, &uId,
|
---|
1508 | true, /* remove a one-shot filter */
|
---|
1509 | &fFilter, &fIsOneShot);
|
---|
1510 | LOG(("Matching Info: Filter (0x%p), pCtx(0x%p), fFilter(%d), fIsOneShot(%d)", uId, pCtx, (int)fFilter, (int)fIsOneShot));
|
---|
1511 | if (fFilter)
|
---|
1512 | {
|
---|
1513 | LOG(("Created Device 0x%p should be filtered", pDevice));
|
---|
1514 | ASSERT_WARN(pCtx, ("zero ctx"));
|
---|
1515 | ASSERT_WARN(uId, ("zero uId"));
|
---|
1516 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_CAPTURING;
|
---|
1517 | }
|
---|
1518 | else
|
---|
1519 | {
|
---|
1520 | LOG(("Created Device 0x%p should NOT be filtered", pDevice));
|
---|
1521 | ASSERT_WARN(!uId == !pCtx, ("invalid uid(0x%p) - ctx(0x%p) pair", uId, pCtx)); /* either both zero or both not */
|
---|
1522 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_UNCAPTURED;
|
---|
1523 | }
|
---|
1524 |
|
---|
1525 | if (pCtx)
|
---|
1526 | vboxUsbFltDevOwnerSetLocked(pDevice, pCtx, fIsOneShot ? 0 : uId, fIsOneShot);
|
---|
1527 |
|
---|
1528 | InsertHeadList(&g_VBoxUsbFltGlobals.DeviceList, &pDevice->GlobalLe);
|
---|
1529 |
|
---|
1530 | /* do not need to signal anything here -
|
---|
1531 | * going to do that once the proxy device object starts */
|
---|
1532 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1533 |
|
---|
1534 | *pbFiltered = fFilter;
|
---|
1535 |
|
---|
1536 | return STATUS_SUCCESS;
|
---|
1537 | }
|
---|
1538 |
|
---|
1539 | BOOLEAN VBoxUsbFltPdoIsFiltered(PDEVICE_OBJECT pPdo)
|
---|
1540 | {
|
---|
1541 | VBOXUSBFLT_DEVSTATE enmState = VBOXUSBFLT_DEVSTATE_REMOVED;
|
---|
1542 |
|
---|
1543 | /* Find the real PDO+reference. Dereference when we're done with it. Note that
|
---|
1544 | * the input pPdo was not explicitly referenced so we're not dropping its ref.
|
---|
1545 | */
|
---|
1546 | PDEVICE_OBJECT pDevObj = IoGetDeviceAttachmentBaseRef(pPdo);
|
---|
1547 | LOG(("DevObj=%p, real PDO=%p\n", pPdo, pDevObj));
|
---|
1548 | pPdo = pDevObj;
|
---|
1549 |
|
---|
1550 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1551 |
|
---|
1552 | PVBOXUSBFLT_DEVICE pDevice = vboxUsbFltDevGetLocked(pPdo);
|
---|
1553 | if (pDevice)
|
---|
1554 | enmState = pDevice->enmState;
|
---|
1555 |
|
---|
1556 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1557 | ObDereferenceObject(pPdo);
|
---|
1558 |
|
---|
1559 | return enmState >= VBOXUSBFLT_DEVSTATE_CAPTURING;
|
---|
1560 | }
|
---|
1561 |
|
---|
1562 | NTSTATUS VBoxUsbFltPdoRemove(PDEVICE_OBJECT pPdo)
|
---|
1563 | {
|
---|
1564 | PVBOXUSBFLT_DEVICE pDevice;
|
---|
1565 | VBOXUSBFLT_DEVSTATE enmOldState;
|
---|
1566 |
|
---|
1567 | /* Find the real PDO+reference. Dereference when we're done with it. Note that
|
---|
1568 | * the input pPdo was not explicitly referenced so we're not dropping its ref.
|
---|
1569 | */
|
---|
1570 | PDEVICE_OBJECT pDevObj = IoGetDeviceAttachmentBaseRef(pPdo);
|
---|
1571 | LOG(("DevObj=%p, real PDO=%p\n", pPdo, pDevObj));
|
---|
1572 | pPdo = pDevObj;
|
---|
1573 |
|
---|
1574 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1575 | pDevice = vboxUsbFltDevGetLocked(pPdo);
|
---|
1576 | if (pDevice)
|
---|
1577 | {
|
---|
1578 | RemoveEntryList(&pDevice->GlobalLe);
|
---|
1579 | enmOldState = pDevice->enmState;
|
---|
1580 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_REMOVED;
|
---|
1581 | }
|
---|
1582 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1583 | ObDereferenceObject(pPdo);
|
---|
1584 | if (pDevice)
|
---|
1585 | vboxUsbFltDevRelease(pDevice);
|
---|
1586 | return STATUS_SUCCESS;
|
---|
1587 | }
|
---|
1588 |
|
---|
1589 | HVBOXUSBFLTDEV VBoxUsbFltProxyStarted(PDEVICE_OBJECT pPdo)
|
---|
1590 | {
|
---|
1591 | PVBOXUSBFLT_DEVICE pDevice;
|
---|
1592 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1593 |
|
---|
1594 | /* NB: The USB proxy (VBoxUSB.sys) passes us the real PDO, not anything above that. */
|
---|
1595 | pDevice = vboxUsbFltDevGetLocked(pPdo);
|
---|
1596 | /*
|
---|
1597 | * Prevent a host crash when vboxUsbFltDevGetLocked fails to locate the matching PDO
|
---|
1598 | * in g_VBoxUsbFltGlobals.DeviceList (see @bugref{6509}).
|
---|
1599 | */
|
---|
1600 | if (pDevice == NULL)
|
---|
1601 | {
|
---|
1602 | WARN(("failed to get device for PDO(0x%p)", pPdo));
|
---|
1603 | }
|
---|
1604 | else if (pDevice->enmState == VBOXUSBFLT_DEVSTATE_CAPTURING)
|
---|
1605 | {
|
---|
1606 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_CAPTURED;
|
---|
1607 | LOG(("The proxy notified proxy start for the captured device 0x%p", pDevice));
|
---|
1608 | vboxUsbFltDevRetain(pDevice);
|
---|
1609 | }
|
---|
1610 | else
|
---|
1611 | {
|
---|
1612 | WARN(("invalid state, %d", pDevice->enmState));
|
---|
1613 | pDevice = NULL;
|
---|
1614 | }
|
---|
1615 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1616 | return pDevice;
|
---|
1617 | }
|
---|
1618 |
|
---|
1619 | void VBoxUsbFltProxyStopped(HVBOXUSBFLTDEV hDev)
|
---|
1620 | {
|
---|
1621 | PVBOXUSBFLT_DEVICE pDevice = (PVBOXUSBFLT_DEVICE)hDev;
|
---|
1622 | /*
|
---|
1623 | * Prevent a host crash when VBoxUsbFltProxyStarted fails, returning NULL.
|
---|
1624 | * See @bugref{6509}.
|
---|
1625 | */
|
---|
1626 | if (pDevice == NULL)
|
---|
1627 | {
|
---|
1628 | WARN(("VBoxUsbFltProxyStopped called with NULL device pointer"));
|
---|
1629 | return;
|
---|
1630 | }
|
---|
1631 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1632 | if (pDevice->enmState == VBOXUSBFLT_DEVSTATE_CAPTURED
|
---|
1633 | || pDevice->enmState == VBOXUSBFLT_DEVSTATE_USED_BY_GUEST)
|
---|
1634 | {
|
---|
1635 | /* this is due to devie was physically removed */
|
---|
1636 | LOG(("The proxy notified proxy stop for the captured device 0x%p, current state %d", pDevice, pDevice->enmState));
|
---|
1637 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_CAPTURING;
|
---|
1638 | }
|
---|
1639 | else
|
---|
1640 | {
|
---|
1641 | if (pDevice->enmState != VBOXUSBFLT_DEVSTATE_REPLUGGING)
|
---|
1642 | {
|
---|
1643 | WARN(("invalid state, %d", pDevice->enmState));
|
---|
1644 | }
|
---|
1645 | }
|
---|
1646 | VBOXUSBFLT_LOCK_RELEASE();
|
---|
1647 |
|
---|
1648 | vboxUsbFltDevRelease(pDevice);
|
---|
1649 | }
|
---|
1650 |
|
---|
1651 |
|
---|
1652 | static NTSTATUS vboxUsbFltRegKeyQuery(PWSTR ValueName, ULONG ValueType, PVOID ValueData, ULONG ValueLength, PVOID Context, PVOID EntryContext)
|
---|
1653 | {
|
---|
1654 | NTSTATUS Status = STATUS_SUCCESS;
|
---|
1655 |
|
---|
1656 | RT_NOREF(ValueName, Context);
|
---|
1657 | if ( ValueType == REG_DWORD
|
---|
1658 | && ValueLength == sizeof(ULONG))
|
---|
1659 | *(ULONG *)EntryContext = *(ULONG *)ValueData;
|
---|
1660 | else
|
---|
1661 | Status = STATUS_OBJECT_TYPE_MISMATCH;
|
---|
1662 |
|
---|
1663 | return Status;
|
---|
1664 | }
|
---|
1665 |
|
---|
1666 |
|
---|
1667 | NTSTATUS VBoxUsbFltInit()
|
---|
1668 | {
|
---|
1669 | int rc = VBoxUSBFilterInit();
|
---|
1670 | if (RT_FAILURE(rc))
|
---|
1671 | {
|
---|
1672 | WARN(("VBoxUSBFilterInit failed, rc (%d)", rc));
|
---|
1673 | return STATUS_UNSUCCESSFUL;
|
---|
1674 | }
|
---|
1675 |
|
---|
1676 | memset(&g_VBoxUsbFltGlobals, 0, sizeof (g_VBoxUsbFltGlobals));
|
---|
1677 | InitializeListHead(&g_VBoxUsbFltGlobals.DeviceList);
|
---|
1678 | InitializeListHead(&g_VBoxUsbFltGlobals.ContextList);
|
---|
1679 | InitializeListHead(&g_VBoxUsbFltGlobals.BlackDeviceList);
|
---|
1680 | vboxUsbFltBlDevPopulateWithKnownLocked();
|
---|
1681 | VBOXUSBFLT_LOCK_INIT();
|
---|
1682 |
|
---|
1683 | /*
|
---|
1684 | * Check whether the setting to force replugging USB devices when
|
---|
1685 | * querying string descriptors fail is set in the registry,
|
---|
1686 | * see @bugref{9479}.
|
---|
1687 | */
|
---|
1688 | RTL_QUERY_REGISTRY_TABLE aParams[] =
|
---|
1689 | {
|
---|
1690 | {vboxUsbFltRegKeyQuery, 0, L"ForceReplugWhenDevPopulateFails", &g_VBoxUsbFltGlobals.dwForceReplugWhenDevPopulateFails, REG_DWORD, &g_VBoxUsbFltGlobals.dwForceReplugWhenDevPopulateFails, sizeof(ULONG) },
|
---|
1691 | { NULL, 0, NULL, NULL, 0, 0, 0 }
|
---|
1692 | };
|
---|
1693 | UNICODE_STRING UnicodePath = RTL_CONSTANT_STRING(L"\\VBoxUSB");
|
---|
1694 |
|
---|
1695 | NTSTATUS Status = RtlQueryRegistryValues(RTL_REGISTRY_CONTROL, UnicodePath.Buffer, &aParams[0], NULL, NULL);
|
---|
1696 | if (Status == STATUS_SUCCESS)
|
---|
1697 | {
|
---|
1698 | if (g_VBoxUsbFltGlobals.dwForceReplugWhenDevPopulateFails)
|
---|
1699 | LOG(("Forcing replug of USB devices where querying the descriptors fail\n"));
|
---|
1700 | }
|
---|
1701 | else
|
---|
1702 | LOG(("RtlQueryRegistryValues() -> %#x, assuming defaults\n", Status));
|
---|
1703 |
|
---|
1704 | return STATUS_SUCCESS;
|
---|
1705 | }
|
---|
1706 |
|
---|
1707 | NTSTATUS VBoxUsbFltTerm()
|
---|
1708 | {
|
---|
1709 | bool bBusy = false;
|
---|
1710 | VBOXUSBFLT_LOCK_ACQUIRE();
|
---|
1711 | do
|
---|
1712 | {
|
---|
1713 | if (!IsListEmpty(&g_VBoxUsbFltGlobals.ContextList))
|
---|
1714 | {
|
---|
1715 | AssertFailed();
|
---|
1716 | bBusy = true;
|
---|
1717 | break;
|
---|
1718 | }
|
---|
1719 |
|
---|
1720 | PLIST_ENTRY pNext = NULL;
|
---|
1721 | for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
|
---|
1722 | pEntry != &g_VBoxUsbFltGlobals.DeviceList;
|
---|
1723 | pEntry = pNext)
|
---|
1724 | {
|
---|
1725 | pNext = pEntry->Flink;
|
---|
1726 | PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
|
---|
1727 | Assert(!pDevice->uFltId);
|
---|
1728 | Assert(!pDevice->pOwner);
|
---|
1729 | if (pDevice->cRefs != 1)
|
---|
1730 | {
|
---|
1731 | AssertFailed();
|
---|
1732 | bBusy = true;
|
---|
1733 | break;
|
---|
1734 | }
|
---|
1735 | }
|
---|
1736 | } while (0);
|
---|
1737 |
|
---|
1738 | VBOXUSBFLT_LOCK_RELEASE()
|
---|
1739 |
|
---|
1740 | if (bBusy)
|
---|
1741 | {
|
---|
1742 | return STATUS_DEVICE_BUSY;
|
---|
1743 | }
|
---|
1744 |
|
---|
1745 | for (PLIST_ENTRY pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink;
|
---|
1746 | pEntry != &g_VBoxUsbFltGlobals.DeviceList;
|
---|
1747 | pEntry = g_VBoxUsbFltGlobals.DeviceList.Flink)
|
---|
1748 | {
|
---|
1749 | RemoveEntryList(pEntry);
|
---|
1750 | PVBOXUSBFLT_DEVICE pDevice = PVBOXUSBFLT_DEVICE_FROM_LE(pEntry);
|
---|
1751 | pDevice->enmState = VBOXUSBFLT_DEVSTATE_REMOVED;
|
---|
1752 | vboxUsbFltDevRelease(pDevice);
|
---|
1753 | }
|
---|
1754 |
|
---|
1755 | vboxUsbFltBlDevClearLocked();
|
---|
1756 |
|
---|
1757 | VBOXUSBFLT_LOCK_TERM();
|
---|
1758 |
|
---|
1759 | VBoxUSBFilterTerm();
|
---|
1760 |
|
---|
1761 | return STATUS_SUCCESS;
|
---|
1762 | }
|
---|
1763 |
|
---|