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