summaryrefslogtreecommitdiffstats
path: root/src/VBox/HostDrivers/VBoxNetFlt/win/drv/VBoxNetFltP-win.cpp
blob: c2a7b7dc59e92b23e42d5336e3d6ca93dd2fb7d6 (plain)
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
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
/* $Id: VBoxNetFltP-win.cpp $ */
/** @file
 * VBoxNetFltP-win.cpp - Bridged Networking Driver, Windows Specific Code.
 * Protocol edge
 */
/*
 * 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.
 */
#include "VBoxNetFltCmn-win.h"

#ifdef VBOXNETADP
# error "No protocol edge"
#endif

#define VBOXNETFLT_PT_STATUS_IS_FILTERED(_s) (\
       (_s) == NDIS_STATUS_MEDIA_CONNECT \
    || (_s) == NDIS_STATUS_MEDIA_DISCONNECT \
    )

/**
 * performs binding to the given adapter
 */
DECLHIDDEN(NDIS_STATUS) vboxNetFltWinPtDoBinding(PVBOXNETFLTINS pThis, PNDIS_STRING pOurDeviceName, PNDIS_STRING pBindToDeviceName)
{
    Assert(pThis->u.s.WinIf.PtState.PowerState == NdisDeviceStateD3);
    Assert(pThis->u.s.WinIf.PtState.OpState == kVBoxNetDevOpState_Deinitialized);
    Assert(KeGetCurrentIrql() == PASSIVE_LEVEL);

    vboxNetFltWinSetOpState(&pThis->u.s.WinIf.PtState, kVBoxNetDevOpState_Initializing);

    NDIS_STATUS Status = vboxNetFltWinCopyString(&pThis->u.s.WinIf.MpDeviceName, pOurDeviceName);
    Assert (Status == NDIS_STATUS_SUCCESS);
    if (Status == NDIS_STATUS_SUCCESS)
    {
        vboxNetFltWinSetPowerState(&pThis->u.s.WinIf.PtState, NdisDeviceStateD0);
        pThis->u.s.WinIf.OpenCloseStatus = NDIS_STATUS_SUCCESS;

        UINT iMedium;
        NDIS_STATUS TmpStatus;
        NDIS_MEDIUM aenmNdisMedium[] =
        {
                /* Ethernet */
                NdisMedium802_3,
                /* Wan */
                NdisMediumWan
        };

        NdisResetEvent(&pThis->u.s.WinIf.OpenCloseEvent);

        NdisOpenAdapter(&Status, &TmpStatus, &pThis->u.s.WinIf.hBinding, &iMedium,
                aenmNdisMedium, RT_ELEMENTS(aenmNdisMedium),
                g_VBoxNetFltGlobalsWin.Pt.hProtocol,
                pThis,
                pBindToDeviceName,
                0, /* IN UINT OpenOptions, (reserved, should be NULL) */
                NULL /* IN PSTRING AddressingInformation  OPTIONAL */
                );
        Assert(Status == NDIS_STATUS_PENDING || Status == STATUS_SUCCESS);
        if (Status == NDIS_STATUS_PENDING)
        {
            NdisWaitEvent(&pThis->u.s.WinIf.OpenCloseEvent, 0);
            Status = pThis->u.s.WinIf.OpenCloseStatus;
        }

        Assert(Status == NDIS_STATUS_SUCCESS);
        if (Status == NDIS_STATUS_SUCCESS)
        {
            Assert(pThis->u.s.WinIf.hBinding);
            pThis->u.s.WinIf.enmMedium = aenmNdisMedium[iMedium];
            vboxNetFltWinSetOpState(&pThis->u.s.WinIf.PtState, kVBoxNetDevOpState_Initialized);

            Status = vboxNetFltWinMpInitializeDevideInstance(pThis);
            Assert(Status == NDIS_STATUS_SUCCESS);
            if (Status == NDIS_STATUS_SUCCESS)
            {
                return NDIS_STATUS_SUCCESS;
            }
            else
            {
                LogRelFunc(("vboxNetFltWinMpInitializeDevideInstance failed, Status 0x%x\n", Status));
            }

            vboxNetFltWinSetOpState(&pThis->u.s.WinIf.PtState, kVBoxNetDevOpState_Deinitializing);
            vboxNetFltWinPtCloseInterface(pThis, &TmpStatus);
            Assert(TmpStatus == NDIS_STATUS_SUCCESS);
            vboxNetFltWinSetOpState(&pThis->u.s.WinIf.PtState, kVBoxNetDevOpState_Deinitialized);
        }
        else
        {
            LogRelFunc(("NdisOpenAdapter failed, Status (0x%x)", Status));
        }

        vboxNetFltWinSetOpState(&pThis->u.s.WinIf.PtState, kVBoxNetDevOpState_Deinitialized);
        pThis->u.s.WinIf.hBinding = NULL;
    }

    return Status;
}

static VOID vboxNetFltWinPtBindAdapter(OUT PNDIS_STATUS pStatus,
        IN NDIS_HANDLE hBindContext,
        IN PNDIS_STRING pDeviceNameStr,
        IN PVOID pvSystemSpecific1,
        IN PVOID pvSystemSpecific2)
{
    LogFlowFuncEnter();
    RT_NOREF2(hBindContext, pvSystemSpecific2);

    NDIS_STATUS Status;
    NDIS_HANDLE hConfig = NULL;

    NdisOpenProtocolConfiguration(&Status, &hConfig, (PNDIS_STRING)pvSystemSpecific1);
    Assert(Status == NDIS_STATUS_SUCCESS);
    if (Status == NDIS_STATUS_SUCCESS)
    {
        PNDIS_CONFIGURATION_PARAMETER pParam;
        NDIS_STRING UppedBindStr = NDIS_STRING_CONST("UpperBindings");
        NdisReadConfiguration(&Status, &pParam, hConfig, &UppedBindStr, NdisParameterString);
        Assert(Status == NDIS_STATUS_SUCCESS);
        if (Status == NDIS_STATUS_SUCCESS)
        {
            PVBOXNETFLTINS pNetFlt;
            Status = vboxNetFltWinPtInitBind(&pNetFlt, &pParam->ParameterData.StringData, pDeviceNameStr);
            Assert(Status == NDIS_STATUS_SUCCESS);
        }

        NdisCloseConfiguration(hConfig);
    }

    *pStatus = Status;

    LogFlowFunc(("LEAVE: Status 0x%x\n", Status));
}

static VOID vboxNetFltWinPtOpenAdapterComplete(IN NDIS_HANDLE hProtocolBindingContext, IN NDIS_STATUS Status, IN NDIS_STATUS OpenErrorStatus)
{
    PVBOXNETFLTINS pNetFlt = (PVBOXNETFLTINS)hProtocolBindingContext;
    RT_NOREF1(OpenErrorStatus);

    LogFlowFunc(("ENTER: pNetFlt (0x%p), Status (0x%x), OpenErrorStatus(0x%x)\n", pNetFlt, Status, OpenErrorStatus));
    Assert(pNetFlt->u.s.WinIf.OpenCloseStatus == NDIS_STATUS_SUCCESS);
    Assert(Status == NDIS_STATUS_SUCCESS);
    if (pNetFlt->u.s.WinIf.OpenCloseStatus == NDIS_STATUS_SUCCESS)
    {
        pNetFlt->u.s.WinIf.OpenCloseStatus = Status;
        Assert(Status == NDIS_STATUS_SUCCESS);
        if (Status != NDIS_STATUS_SUCCESS)
            LogRelFunc(("Open Complete status is 0x%x", Status));
    }
    else
        LogRelFunc(("Adapter maintained status is 0x%x", pNetFlt->u.s.WinIf.OpenCloseStatus));
    NdisSetEvent(&pNetFlt->u.s.WinIf.OpenCloseEvent);
    LogFlowFunc(("LEAVE: pNetFlt (0x%p), Status (0x%x), OpenErrorStatus(0x%x)\n", pNetFlt, Status, OpenErrorStatus));
}

static void vboxNetFltWinPtRequestsWaitComplete(PVBOXNETFLTINS pNetFlt)
{
    /* wait for request to complete */
    while (vboxNetFltWinAtomicUoReadWinState(pNetFlt->u.s.WinIf.StateFlags).fRequestInfo == VBOXNDISREQUEST_INPROGRESS)
    {
        vboxNetFltWinSleep(2);
    }

    /*
     * If the below miniport is going to low power state, complete the queued request
     */
    RTSpinlockAcquire(pNetFlt->hSpinlock);
    if (pNetFlt->u.s.WinIf.StateFlags.fRequestInfo & VBOXNDISREQUEST_QUEUED)
    {
        /* mark the request as InProgress before posting it to RequestComplete */
        pNetFlt->u.s.WinIf.StateFlags.fRequestInfo = VBOXNDISREQUEST_INPROGRESS;
        RTSpinlockRelease(pNetFlt->hSpinlock);
        vboxNetFltWinPtRequestComplete(pNetFlt, &pNetFlt->u.s.WinIf.PassDownRequest, NDIS_STATUS_FAILURE);
    }
    else
    {
        RTSpinlockRelease(pNetFlt->hSpinlock);
    }
}

DECLHIDDEN(NDIS_STATUS) vboxNetFltWinPtDoUnbinding(PVBOXNETFLTINS pNetFlt, bool bOnUnbind)
{
    NDIS_STATUS Status;
    uint64_t NanoTS = RTTimeSystemNanoTS();
    int cPPUsage;

    LogFlowFunc(("ENTER: pNetFlt 0x%p\n", pNetFlt));

    Assert(KeGetCurrentIrql() == PASSIVE_LEVEL);

    Assert(vboxNetFltWinGetOpState(&pNetFlt->u.s.WinIf.PtState) == kVBoxNetDevOpState_Initialized);

    RTSpinlockAcquire(pNetFlt->hSpinlock);

    ASMAtomicUoWriteBool(&pNetFlt->fDisconnectedFromHost, true);
    ASMAtomicUoWriteBool(&pNetFlt->fRediscoveryPending, false);
    ASMAtomicUoWriteU64(&pNetFlt->NanoTSLastRediscovery, NanoTS);

    vboxNetFltWinSetOpState(&pNetFlt->u.s.WinIf.PtState, kVBoxNetDevOpState_Deinitializing);
    if (!bOnUnbind)
    {
        vboxNetFltWinSetOpState(&pNetFlt->u.s.WinIf.MpState, kVBoxNetDevOpState_Deinitializing);
    }

    RTSpinlockRelease(pNetFlt->hSpinlock);

    vboxNetFltWinPtRequestsWaitComplete(pNetFlt);

    vboxNetFltWinWaitDereference(&pNetFlt->u.s.WinIf.MpState);
    vboxNetFltWinWaitDereference(&pNetFlt->u.s.WinIf.PtState);

    /* check packet pool is empty */
    cPPUsage = NdisPacketPoolUsage(pNetFlt->u.s.WinIf.hSendPacketPool);
    Assert(cPPUsage == 0);
    cPPUsage = NdisPacketPoolUsage(pNetFlt->u.s.WinIf.hRecvPacketPool);
    Assert(cPPUsage == 0);
    /* for debugging only, ignore the err in release */
    NOREF(cPPUsage);

    if (!bOnUnbind || !vboxNetFltWinMpDeInitializeDeviceInstance(pNetFlt, &Status))
    {
        vboxNetFltWinPtCloseInterface(pNetFlt, &Status);
        vboxNetFltWinSetOpState(&pNetFlt->u.s.WinIf.PtState, kVBoxNetDevOpState_Deinitialized);

        if (!bOnUnbind)
        {
            Assert(vboxNetFltWinGetOpState(&pNetFlt->u.s.WinIf.MpState) == kVBoxNetDevOpState_Deinitializing);
            vboxNetFltWinSetOpState(&pNetFlt->u.s.WinIf.MpState, kVBoxNetDevOpState_Deinitialized);
        }
        else
        {
            Assert(vboxNetFltWinGetOpState(&pNetFlt->u.s.WinIf.MpState) == kVBoxNetDevOpState_Deinitialized);
        }
    }
    else
    {
        Assert(vboxNetFltWinGetOpState(&pNetFlt->u.s.WinIf.MpState) == kVBoxNetDevOpState_Deinitialized);
    }

    LogFlowFunc(("LEAVE: pNetFlt 0x%p\n", pNetFlt));

    return Status;
}

static VOID vboxNetFltWinPtUnbindAdapter(OUT PNDIS_STATUS pStatus,
        IN NDIS_HANDLE hContext,
        IN NDIS_HANDLE hUnbindContext)
{
    PVBOXNETFLTINS pNetFlt = (PVBOXNETFLTINS)hContext;
    RT_NOREF1(hUnbindContext);

    LogFlowFunc(("ENTER: pNetFlt (0x%p)\n", pNetFlt));

    *pStatus = vboxNetFltWinDetachFromInterface(pNetFlt, true);
    Assert(*pStatus == NDIS_STATUS_SUCCESS);

    LogFlowFunc(("LEAVE: pNetFlt (0x%p)\n", pNetFlt));
}

static VOID vboxNetFltWinPtUnloadProtocol()
{
    LogFlowFuncEnter();
    NDIS_STATUS Status = vboxNetFltWinPtDeregister(&g_VBoxNetFltGlobalsWin.Pt);
    Assert(Status == NDIS_STATUS_SUCCESS); NOREF(Status);
    LogFlowFunc(("LEAVE: PtDeregister Status (0x%x)\n", Status));
}


static VOID vboxNetFltWinPtCloseAdapterComplete(IN NDIS_HANDLE ProtocolBindingContext, IN NDIS_STATUS Status)
{
    PVBOXNETFLTINS pNetFlt = (PVBOXNETFLTINS)ProtocolBindingContext;

    LogFlowFunc(("ENTER: pNetFlt (0x%p), Status (0x%x)\n", pNetFlt, Status));
    Assert(pNetFlt->u.s.WinIf.OpenCloseStatus == NDIS_STATUS_SUCCESS);
    Assert(Status == NDIS_STATUS_SUCCESS);
    Assert(pNetFlt->u.s.WinIf.OpenCloseStatus == NDIS_STATUS_SUCCESS);
    if (pNetFlt->u.s.WinIf.OpenCloseStatus == NDIS_STATUS_SUCCESS)
    {
        pNetFlt->u.s.WinIf.OpenCloseStatus = Status;
    }
    NdisSetEvent(&pNetFlt->u.s.WinIf.OpenCloseEvent);
    LogFlowFunc(("LEAVE: pNetFlt (0x%p), Status (0x%x)\n", pNetFlt, Status));
}

static VOID vboxNetFltWinPtResetComplete(IN NDIS_HANDLE hProtocolBindingContext, IN NDIS_STATUS Status)
{
    RT_NOREF2(hProtocolBindingContext, Status);
    LogFlowFunc(("ENTER: pNetFlt 0x%p, Status 0x%x\n", hProtocolBindingContext, Status));
    /*
     * should never be here
     */
    AssertFailed();
    LogFlowFunc(("LEAVE: pNetFlt 0x%p, Status 0x%x\n", hProtocolBindingContext, Status));
}

static NDIS_STATUS vboxNetFltWinPtHandleQueryInfoComplete(PVBOXNETFLTINS pNetFlt, NDIS_STATUS Status)
{
    PNDIS_REQUEST pRequest = &pNetFlt->u.s.WinIf.PassDownRequest;

    switch (pRequest->DATA.QUERY_INFORMATION.Oid)
    {
        case OID_PNP_CAPABILITIES:
        {
            if (Status == NDIS_STATUS_SUCCESS)
            {
                if (pRequest->DATA.QUERY_INFORMATION.InformationBufferLength >= sizeof (NDIS_PNP_CAPABILITIES))
                {
                    PNDIS_PNP_CAPABILITIES pPnPCaps = (PNDIS_PNP_CAPABILITIES)(pRequest->DATA.QUERY_INFORMATION.InformationBuffer);
                    PNDIS_PM_WAKE_UP_CAPABILITIES pPmWuCaps = &pPnPCaps->WakeUpCapabilities;
                    pPmWuCaps->MinMagicPacketWakeUp = NdisDeviceStateUnspecified;
                    pPmWuCaps->MinPatternWakeUp = NdisDeviceStateUnspecified;
                    pPmWuCaps->MinLinkChangeWakeUp = NdisDeviceStateUnspecified;
                    *pNetFlt->u.s.WinIf.pcPDRBytesRW = sizeof (NDIS_PNP_CAPABILITIES);
                    *pNetFlt->u.s.WinIf.pcPDRBytesNeeded = 0;
                    Status = NDIS_STATUS_SUCCESS;
                }
                else
                {
                    AssertFailed();
                    *pNetFlt->u.s.WinIf.pcPDRBytesNeeded = sizeof(NDIS_PNP_CAPABILITIES);
                    Status = NDIS_STATUS_RESOURCES;
                }
            }
            break;
        }

        case OID_GEN_MAC_OPTIONS:
        {
            if (Status == NDIS_STATUS_SUCCESS)
            {
                if (pRequest->DATA.QUERY_INFORMATION.InformationBufferLength >= sizeof (ULONG))
                {
                    pNetFlt->u.s.WinIf.fMacOptions = *(PULONG)pRequest->DATA.QUERY_INFORMATION.InformationBuffer;
#ifndef VBOX_LOOPBACK_USEFLAGS
                    /* clearing this flag tells ndis we'll handle loopback ourselves
                     * the ndis layer or nic driver below us would loopback packets as necessary */
                    *(PULONG)pRequest->DATA.QUERY_INFORMATION.InformationBuffer &= ~NDIS_MAC_OPTION_NO_LOOPBACK;
#else
                    /* we have to catch loopbacks from the underlying driver, so no duplications will occur,
                     * just indicate NDIS to handle loopbacks for the packets coming from the protocol */
                    *(PULONG)pRequest->DATA.QUERY_INFORMATION.InformationBuffer |= NDIS_MAC_OPTION_NO_LOOPBACK;
#endif
                }
                else
                {
                    AssertFailed();
                    *pNetFlt->u.s.WinIf.pcPDRBytesNeeded = sizeof (ULONG);
                    Status = NDIS_STATUS_RESOURCES;
                }
            }
            break;
        }

        case OID_GEN_CURRENT_PACKET_FILTER:
        {
            if (VBOXNETFLT_PROMISCUOUS_SUPPORTED(pNetFlt))
            {
                /* we're here _ONLY_ in the passthru mode */
                Assert(pNetFlt->u.s.WinIf.StateFlags.fProcessingPacketFilter && !pNetFlt->u.s.WinIf.StateFlags.fPPFNetFlt);
                if (pNetFlt->u.s.WinIf.StateFlags.fProcessingPacketFilter && !pNetFlt->u.s.WinIf.StateFlags.fPPFNetFlt)
                {
                    Assert(pNetFlt->enmTrunkState != INTNETTRUNKIFSTATE_ACTIVE);
                    vboxNetFltWinDereferenceModePassThru(pNetFlt);
                    vboxNetFltWinDereferenceWinIf(pNetFlt);
                }

                if (Status == NDIS_STATUS_SUCCESS)
                {
                    if (pRequest->DATA.QUERY_INFORMATION.InformationBufferLength >= sizeof (ULONG))
                    {
                        /* the filter request is issued below only in case netflt is not active,
                         * simply update the cache here */
                        /* cache the filter used by upper protocols */
                        pNetFlt->u.s.WinIf.fUpperProtocolSetFilter = *(PULONG)pRequest->DATA.QUERY_INFORMATION.InformationBuffer;
                        pNetFlt->u.s.WinIf.StateFlags.fUpperProtSetFilterInitialized = TRUE;
                    }
                    else
                    {
                        AssertFailed();
                        *pNetFlt->u.s.WinIf.pcPDRBytesNeeded = sizeof (ULONG);
                        Status = NDIS_STATUS_RESOURCES;
                    }
                }
            }
            break;
        }

        default:
            Assert(pRequest->DATA.QUERY_INFORMATION.Oid != OID_PNP_QUERY_POWER);
            break;
    }

    *pNetFlt->u.s.WinIf.pcPDRBytesRW = pRequest->DATA.QUERY_INFORMATION.BytesWritten;
    *pNetFlt->u.s.WinIf.pcPDRBytesNeeded = pRequest->DATA.QUERY_INFORMATION.BytesNeeded;

    return Status;
}

static NDIS_STATUS vboxNetFltWinPtHandleSetInfoComplete(PVBOXNETFLTINS pNetFlt, NDIS_STATUS Status)
{
    PNDIS_REQUEST pRequest = &pNetFlt->u.s.WinIf.PassDownRequest;

    switch (pRequest->DATA.SET_INFORMATION.Oid)
    {
        case OID_GEN_CURRENT_PACKET_FILTER:
        {
            if (VBOXNETFLT_PROMISCUOUS_SUPPORTED(pNetFlt))
            {
                Assert(Status == NDIS_STATUS_SUCCESS);
                if (pNetFlt->u.s.WinIf.StateFlags.fProcessingPacketFilter)
                {
                    if (pNetFlt->u.s.WinIf.StateFlags.fPPFNetFlt)
                    {
                        Assert(pNetFlt->enmTrunkState == INTNETTRUNKIFSTATE_ACTIVE);
                        pNetFlt->u.s.WinIf.StateFlags.fPPFNetFlt = 0;
                        if (Status == NDIS_STATUS_SUCCESS)
                        {
                            if (pRequest->DATA.SET_INFORMATION.InformationBufferLength >= sizeof (ULONG))
                            {
                                pNetFlt->u.s.WinIf.fOurSetFilter = *((PULONG)pRequest->DATA.SET_INFORMATION.InformationBuffer);
                                Assert(pNetFlt->u.s.WinIf.fOurSetFilter == NDIS_PACKET_TYPE_PROMISCUOUS);
                            }
                            else
                            {
                                AssertFailed();
                                *pNetFlt->u.s.WinIf.pcPDRBytesNeeded = sizeof (ULONG);
                                Status = NDIS_STATUS_RESOURCES;
                            }
                        }
                        vboxNetFltWinDereferenceNetFlt(pNetFlt);
                    }
                    else
                    {
                        Assert(pNetFlt->enmTrunkState != INTNETTRUNKIFSTATE_ACTIVE);

                        if (Status == NDIS_STATUS_SUCCESS)
                        {
                            if (pRequest->DATA.SET_INFORMATION.InformationBufferLength >= sizeof (ULONG))
                            {
                                /* the request was issued when the netflt was not active, simply update the cache here */
                                pNetFlt->u.s.WinIf.fUpperProtocolSetFilter = *((PULONG)pRequest->DATA.SET_INFORMATION.InformationBuffer);
                                pNetFlt->u.s.WinIf.StateFlags.fUpperProtSetFilterInitialized = TRUE;
                            }
                            else
                            {
                                AssertFailed();
                                *pNetFlt->u.s.WinIf.pcPDRBytesNeeded = sizeof (ULONG);
                                Status = NDIS_STATUS_RESOURCES;
                            }
                        }
                        vboxNetFltWinDereferenceModePassThru(pNetFlt);
                    }

                    pNetFlt->u.s.WinIf.StateFlags.fProcessingPacketFilter = 0;
                    vboxNetFltWinDereferenceWinIf(pNetFlt);
                }
#ifdef DEBUG_misha
                else
                {
                    AssertFailed();
                }
#endif
            }
            break;
        }

        default:
            Assert(pRequest->DATA.SET_INFORMATION.Oid != OID_PNP_SET_POWER);
            break;
    }

    *pNetFlt->u.s.WinIf.pcPDRBytesRW = pRequest->DATA.SET_INFORMATION.BytesRead;
    *pNetFlt->u.s.WinIf.pcPDRBytesNeeded = pRequest->DATA.SET_INFORMATION.BytesNeeded;

    return Status;
}

DECLHIDDEN(VOID) vboxNetFltWinPtRequestComplete(NDIS_HANDLE hContext, PNDIS_REQUEST pNdisRequest, NDIS_STATUS Status)
{
    PVBOXNETFLTINS pNetFlt = (PVBOXNETFLTINS)hContext;
    PNDIS_REQUEST pSynchRequest = pNetFlt->u.s.WinIf.pSynchRequest;

    LogFlowFunc(("ENTER: pNetFlt (0x%p), pNdisRequest (0x%p), Status (0x%x)\n", pNetFlt, pNdisRequest, Status));

    if (pSynchRequest == pNdisRequest)
    {
        /* asynchronous completion of our sync request */
        /*1.set the status */
        pNetFlt->u.s.WinIf.SynchCompletionStatus = Status;
        /* 2. set event */
        KeSetEvent(&pNetFlt->u.s.WinIf.hSynchCompletionEvent, 0, FALSE);
        /* 3. return; */

        LogFlowFunc(("LEAVE: pNetFlt (0x%p), pNdisRequest (0x%p), Status (0x%x)\n", pNetFlt, pNdisRequest, Status));
        return;
    }

    Assert(&pNetFlt->u.s.WinIf.PassDownRequest == pNdisRequest);
    Assert(pNetFlt->u.s.WinIf.StateFlags.fRequestInfo == VBOXNDISREQUEST_INPROGRESS);
    vboxNetFltWinMpRequestStateComplete(pNetFlt);

    switch (pNdisRequest->RequestType)
    {
      case NdisRequestQueryInformation:
          Status = vboxNetFltWinPtHandleQueryInfoComplete(pNetFlt, Status);
          NdisMQueryInformationComplete(pNetFlt->u.s.WinIf.hMiniport, Status);
          break;

      case NdisRequestSetInformation:
          Status = vboxNetFltWinPtHandleSetInfoComplete(pNetFlt, Status);
          NdisMSetInformationComplete(pNetFlt->u.s.WinIf.hMiniport, Status);
          break;

      default:
          AssertFailed();
          break;
    }

    LogFlowFunc(("LEAVE: pNetFlt (0x%p), pNdisRequest (0x%p), Status (0x%x)\n", pNetFlt, pNdisRequest, Status));
}

static VOID vboxNetFltWinPtStatus(IN NDIS_HANDLE hProtocolBindingContext, IN NDIS_STATUS GeneralStatus, IN PVOID pvStatusBuffer, IN UINT cbStatusBuffer)
{
    PVBOXNETFLTINS pNetFlt = (PVBOXNETFLTINS)hProtocolBindingContext;

    LogFlowFunc(("ENTER: pNetFlt (0x%p), GeneralStatus (0x%x)\n", pNetFlt, GeneralStatus));

    if (vboxNetFltWinReferenceWinIf(pNetFlt))
    {
        Assert(pNetFlt->u.s.WinIf.hMiniport);

        if (VBOXNETFLT_PT_STATUS_IS_FILTERED(GeneralStatus))
        {
            pNetFlt->u.s.WinIf.MpIndicatedMediaStatus = GeneralStatus;
        }
        NdisMIndicateStatus(pNetFlt->u.s.WinIf.hMiniport,
                            GeneralStatus,
                            pvStatusBuffer,
                            cbStatusBuffer);

        vboxNetFltWinDereferenceWinIf(pNetFlt);
    }
    else
    {
        if (pNetFlt->u.s.WinIf.hMiniport != NULL
                && VBOXNETFLT_PT_STATUS_IS_FILTERED(GeneralStatus)
           )
        {
            pNetFlt->u.s.WinIf.MpUnindicatedMediaStatus = GeneralStatus;
        }
    }

    LogFlowFunc(("LEAVE: pNetFlt (0x%p), GeneralStatus (0x%x)\n", pNetFlt, GeneralStatus));
}


static VOID vboxNetFltWinPtStatusComplete(IN NDIS_HANDLE hProtocolBindingContext)
{
    PVBOXNETFLTINS pNetFlt = (PVBOXNETFLTINS)hProtocolBindingContext;

    LogFlowFunc(("ENTER: pNetFlt (0x%p)\n", pNetFlt));

    if (vboxNetFltWinReferenceWinIf(pNetFlt))
    {
        NdisMIndicateStatusComplete(pNetFlt->u.s.WinIf.hMiniport);

        vboxNetFltWinDereferenceWinIf(pNetFlt);
    }

    LogFlowFunc(("LEAVE: pNetFlt (0x%p)\n", pNetFlt));
}

static VOID vboxNetFltWinPtSendComplete(IN NDIS_HANDLE hProtocolBindingContext, IN PNDIS_PACKET pPacket, IN NDIS_STATUS Status)
{
    PVBOXNETFLTINS pNetFlt = (PVBOXNETFLTINS)hProtocolBindingContext;
    PVBOXNETFLT_PKTRSVD_PT pSendInfo = (PVBOXNETFLT_PKTRSVD_PT)pPacket->ProtocolReserved;
    PNDIS_PACKET pOrigPacket = pSendInfo->pOrigPacket;
    PVOID pBufToFree = pSendInfo->pBufToFree;
    LogFlowFunc(("ENTER: pNetFlt (0x%p), pPacket (0x%p), Status (0x%x)\n", pNetFlt, pPacket, Status));

#if defined(DEBUG_NETFLT_PACKETS) || !defined(VBOX_LOOPBACK_USEFLAGS)
    /** @todo for optimization we could check only for netflt-mode packets
     * do it for all for now */
     vboxNetFltWinLbRemoveSendPacket(pNetFlt, pPacket);
#endif

     if (pOrigPacket)
     {
         NdisIMCopySendCompletePerPacketInfo(pOrigPacket, pPacket);
         NdisFreePacket(pPacket);
         /* the ptk was posted from the upperlying protocol */
         NdisMSendComplete(pNetFlt->u.s.WinIf.hMiniport, pOrigPacket, Status);
     }
     else
     {
         /* if the pOrigPacket is zero - the ptk was originated by netFlt send/receive
          * need to free packet buffers */
         vboxNetFltWinFreeSGNdisPacket(pPacket, !pBufToFree);
     }

     if (pBufToFree)
     {
         vboxNetFltWinMemFree(pBufToFree);
     }

    vboxNetFltWinDereferenceWinIf(pNetFlt);

    LogFlowFunc(("LEAVE: pNetFlt (0x%p), pPacket (0x%p), Status (0x%x)\n", pNetFlt, pPacket, Status));
}

/**
 * removes searches for the packet in the list and removes it if found
 * @return true if the packet was found and removed, false - otherwise
 */
static bool vboxNetFltWinRemovePacketFromList(PVBOXNETFLT_INTERLOCKED_SINGLE_LIST pList, PNDIS_PACKET pPacket)
{
    PVBOXNETFLT_PKTRSVD_TRANSFERDATA_PT pTDR = (PVBOXNETFLT_PKTRSVD_TRANSFERDATA_PT)pPacket->ProtocolReserved;
    return vboxNetFltWinInterlockedSearchListEntry(pList, &pTDR->ListEntry, true /* remove*/);
}

/**
 * puts the packet to the tail of the list
 */
static void vboxNetFltWinPutPacketToList(PVBOXNETFLT_INTERLOCKED_SINGLE_LIST pList, PNDIS_PACKET pPacket, PNDIS_BUFFER pOrigBuffer)
{
    PVBOXNETFLT_PKTRSVD_TRANSFERDATA_PT pTDR = (PVBOXNETFLT_PKTRSVD_TRANSFERDATA_PT)pPacket->ProtocolReserved;
    pTDR->pOrigBuffer = pOrigBuffer;
    vboxNetFltWinInterlockedPutTail(pList, &pTDR->ListEntry);
}

static bool vboxNetFltWinPtTransferDataCompleteActive(PVBOXNETFLTINS pNetFltIf, PNDIS_PACKET pPacket, NDIS_STATUS Status)
{
    PNDIS_BUFFER pBuffer;
    PVBOXNETFLT_PKTRSVD_TRANSFERDATA_PT pTDR;

    if (!vboxNetFltWinRemovePacketFromList(&pNetFltIf->u.s.WinIf.TransferDataList, pPacket))
        return false;

    pTDR = (PVBOXNETFLT_PKTRSVD_TRANSFERDATA_PT)pPacket->ProtocolReserved;
    Assert(pTDR);
    Assert(pTDR->pOrigBuffer);

    do
    {
        NdisUnchainBufferAtFront(pPacket, &pBuffer);

        Assert(pBuffer);

        NdisFreeBuffer(pBuffer);

        pBuffer = pTDR->pOrigBuffer;

        NdisChainBufferAtBack(pPacket, pBuffer);

        /* data transfer was initiated when the netFlt was active
         * the netFlt is still retained by us
         * 1. check if loopback
         * 2. enqueue packet
         * 3. release netFlt */

        if (Status == NDIS_STATUS_SUCCESS)
        {

#ifdef VBOX_LOOPBACK_USEFLAGS
            if (vboxNetFltWinIsLoopedBackPacket(pPacket))
            {
                /* should not be here */
                AssertFailed();
            }
#else
            PNDIS_PACKET pLb = vboxNetFltWinLbSearchLoopBack(pNetFltIf, pPacket, false);
            if (pLb)
            {
#ifndef DEBUG_NETFLT_RECV_TRANSFERDATA
                /* should not be here */
                AssertFailed();
#endif
                if (!vboxNetFltWinLbIsFromIntNet(pLb))
                {
                    /* the packet is not from int net, need to pass it up to the host */
                    NdisMIndicateReceivePacket(pNetFltIf->u.s.WinIf.hMiniport, &pPacket, 1);
                    /* dereference NetFlt, WinIf will be dereferenced on Packet return */
                    vboxNetFltWinDereferenceNetFlt(pNetFltIf);
                    break;
                }
            }
#endif
            else
            {
                /* 2. enqueue */
                /* use the same packet info to put the packet in the processing packet queue */
                PVBOXNETFLT_PKTRSVD_MP pRecvInfo = (PVBOXNETFLT_PKTRSVD_MP)pPacket->MiniportReserved;

                VBOXNETFLT_LBVERIFY(pNetFltIf, pPacket);

                pRecvInfo->pOrigPacket = NULL;
                pRecvInfo->pBufToFree = NULL;

                NdisGetPacketFlags(pPacket) = 0;
# ifdef VBOXNETFLT_NO_PACKET_QUEUE
                if (vboxNetFltWinPostIntnet(pNetFltIf, pPacket, 0))
                {
                    /* drop it */
                    vboxNetFltWinFreeSGNdisPacket(pPacket, true);
                    vboxNetFltWinDereferenceWinIf(pNetFltIf);
                }
                else
                {
                    NdisMIndicateReceivePacket(pNetFltIf->u.s.WinIf.hMiniport, &pPacket, 1);
                }
                vboxNetFltWinDereferenceNetFlt(pNetFltIf);
                break;
# else
                Status = vboxNetFltWinQuEnqueuePacket(pNetFltIf, pPacket, PACKET_MINE);
                if (Status == NDIS_STATUS_SUCCESS)
                {
                    break;
                }
                AssertFailed();
# endif
            }
        }
        else
        {
            AssertFailed();
        }
        /* we are here because of error either in data transfer or in enqueueing the packet */
        vboxNetFltWinFreeSGNdisPacket(pPacket, true);
        vboxNetFltWinDereferenceNetFlt(pNetFltIf);
        vboxNetFltWinDereferenceWinIf(pNetFltIf);
    } while (0);

    return true;
}

static VOID vboxNetFltWinPtTransferDataComplete(IN NDIS_HANDLE hProtocolBindingContext,
                    IN PNDIS_PACKET pPacket,
                    IN NDIS_STATUS Status,
                    IN UINT cbTransferred)
{
    PVBOXNETFLTINS pNetFlt = (PVBOXNETFLTINS)hProtocolBindingContext;
    LogFlowFunc(("ENTER: pNetFlt (0x%p), pPacket (0x%p), Status (0x%x), cbTransfered (%d)\n", pNetFlt, pPacket, Status, cbTransferred));
    if (!vboxNetFltWinPtTransferDataCompleteActive(pNetFlt, pPacket, Status))
    {
        if (pNetFlt->u.s.WinIf.hMiniport)
        {
            NdisMTransferDataComplete(pNetFlt->u.s.WinIf.hMiniport,
                                      pPacket,
                                      Status,
                                      cbTransferred);
        }

        vboxNetFltWinDereferenceWinIf(pNetFlt);
    }
    /* else - all processing is done with vboxNetFltWinPtTransferDataCompleteActive already */

    LogFlowFunc(("LEAVE: pNetFlt (0x%p), pPacket (0x%p), Status (0x%x), cbTransfered (%d)\n", pNetFlt, pPacket, Status, cbTransferred));
}

static INT vboxNetFltWinRecvPacketPassThru(PVBOXNETFLTINS pNetFlt, PNDIS_PACKET pPacket)
{
    Assert(KeGetCurrentIrql() == DISPATCH_LEVEL);

    PNDIS_PACKET pMyPacket;
    NDIS_STATUS Status = vboxNetFltWinPrepareRecvPacket(pNetFlt, pPacket, &pMyPacket, true);
    /* the Status holds the current packet status it will be checked for NDIS_STATUS_RESOURCES later
     * (see below) */
    Assert(pMyPacket);
    if (pMyPacket)
    {
        NdisMIndicateReceivePacket(pNetFlt->u.s.WinIf.hMiniport, &pMyPacket, 1);
        if (Status == NDIS_STATUS_RESOURCES)
        {
            NdisDprFreePacket(pMyPacket);
            return 0;
        }

        return 1;
    }

    return 0;
}

/**
 * process the packet receive in a "passthru" mode
 */
static NDIS_STATUS vboxNetFltWinRecvPassThru(PVBOXNETFLTINS pNetFlt, PNDIS_PACKET pPacket)
{
    Assert(KeGetCurrentIrql() == DISPATCH_LEVEL);

    NDIS_STATUS Status;
    PNDIS_PACKET pMyPacket;

    NdisDprAllocatePacket(&Status, &pMyPacket, pNetFlt->u.s.WinIf.hRecvPacketPool);
    Assert(Status == NDIS_STATUS_SUCCESS);
    if (Status == NDIS_STATUS_SUCCESS)
    {
        vboxNetFltWinCopyPacketInfoOnRecv(pMyPacket, pPacket, true /* force NDIS_STATUS_RESOURCES */);
        Assert(NDIS_GET_PACKET_STATUS(pMyPacket) == NDIS_STATUS_RESOURCES);

        NdisMIndicateReceivePacket(pNetFlt->u.s.WinIf.hMiniport, &pMyPacket, 1);

        NdisDprFreePacket(pMyPacket);
    }
    return Status;
}

static VOID vboxNetFltWinRecvIndicatePassThru(PVBOXNETFLTINS pNetFlt, NDIS_HANDLE MacReceiveContext,
                PVOID pHeaderBuffer, UINT cbHeaderBuffer, PVOID pLookAheadBuffer, UINT cbLookAheadBuffer, UINT cbPacket)
{
    /* Note: we're using KeGetCurrentProcessorNumber, which is not entirely correct in case
    * we're running on 64bit win7+, which can handle > 64 CPUs, however since KeGetCurrentProcessorNumber
    * always returns the number < than the number of CPUs in the first group, we're guaranteed to have CPU index < 64
    * @todo: use KeGetCurrentProcessorNumberEx for Win7+ 64 and dynamically extended array */
    ULONG Proc = KeGetCurrentProcessorNumber();
    Assert(Proc < RT_ELEMENTS(pNetFlt->u.s.WinIf.abIndicateRxComplete));
    pNetFlt->u.s.WinIf.abIndicateRxComplete[Proc] = TRUE;
    switch (pNetFlt->u.s.WinIf.enmMedium)
    {
        case NdisMedium802_3:
        case NdisMediumWan:
            NdisMEthIndicateReceive(pNetFlt->u.s.WinIf.hMiniport,
                                         MacReceiveContext,
                                         (PCHAR)pHeaderBuffer,
                                         cbHeaderBuffer,
                                         pLookAheadBuffer,
                                         cbLookAheadBuffer,
                                         cbPacket);
            break;
        default:
            AssertFailed();
            break;
    }
}

/**
 * process the ProtocolReceive in an "active" mode
 *
 * @return NDIS_STATUS_SUCCESS - the packet is processed
 * NDIS_STATUS_PENDING - the packet is being processed, we are waiting for the ProtocolTransferDataComplete to be called
 * NDIS_STATUS_NOT_ACCEPTED - the packet is not needed - typically this is because this is a loopback packet
 * NDIS_STATUS_FAILURE - packet processing failed
 */
static NDIS_STATUS vboxNetFltWinPtReceiveActive(PVBOXNETFLTINS pNetFlt, NDIS_HANDLE MacReceiveContext, PVOID pHeaderBuffer, UINT cbHeaderBuffer,
                        PVOID pLookaheadBuffer, UINT cbLookaheadBuffer, UINT cbPacket)
{
    NDIS_STATUS Status = NDIS_STATUS_SUCCESS;

    do
    {
        if (cbHeaderBuffer != VBOXNETFLT_PACKET_ETHEADER_SIZE)
        {
            Status = NDIS_STATUS_NOT_ACCEPTED;
            break;
        }

#ifndef DEBUG_NETFLT_RECV_TRANSFERDATA
        if (cbPacket == cbLookaheadBuffer)
        {
            PINTNETSG pSG;
            PUCHAR pRcvData;
#ifndef VBOX_LOOPBACK_USEFLAGS
            PNDIS_PACKET pLb;
#endif

            /* allocate SG buffer */
            Status = vboxNetFltWinAllocSG(cbPacket + cbHeaderBuffer, &pSG);
            if (Status != NDIS_STATUS_SUCCESS)
            {
                AssertFailed();
                break;
            }

            pRcvData = (PUCHAR)pSG->aSegs[0].pv;

            NdisMoveMappedMemory(pRcvData, pHeaderBuffer, cbHeaderBuffer);

            NdisCopyLookaheadData(pRcvData+cbHeaderBuffer,
                                                  pLookaheadBuffer,
                                                  cbLookaheadBuffer,
                                                  pNetFlt->u.s.WinIf.fMacOptions);
#ifndef VBOX_LOOPBACK_USEFLAGS
            pLb = vboxNetFltWinLbSearchLoopBackBySG(pNetFlt, pSG, false);
            if (pLb)
            {
#ifndef DEBUG_NETFLT_RECV_NOPACKET
                /* should not be here */
                AssertFailed();
#endif
                if (!vboxNetFltWinLbIsFromIntNet(pLb))
                {
                    PNDIS_PACKET pMyPacket;
                    pMyPacket = vboxNetFltWinNdisPacketFromSG(pNetFlt, /* PVBOXNETFLTINS */
                        pSG, /* PINTNETSG */
                        pSG, /* PVOID pBufToFree */
                        false, /* bool bToWire */
                        false); /* bool bCopyMemory */
                    if (pMyPacket)
                    {
                        NdisMIndicateReceivePacket(pNetFlt->u.s.WinIf.hMiniport, &pMyPacket, 1);
                        /* dereference the NetFlt here & indicate SUCCESS, which would mean the caller would not do a dereference
                         * the WinIf dereference will be done on packet return */
                        vboxNetFltWinDereferenceNetFlt(pNetFlt);
                        Status = NDIS_STATUS_SUCCESS;
                    }
                    else
                    {
                        vboxNetFltWinMemFree(pSG);
                        Status = NDIS_STATUS_FAILURE;
                    }
                }
                else
                {
                    vboxNetFltWinMemFree(pSG);
                    Status = NDIS_STATUS_NOT_ACCEPTED;
                }
                break;
            }
#endif
            VBOXNETFLT_LBVERIFYSG(pNetFlt, pSG);

                /* enqueue SG */
# ifdef VBOXNETFLT_NO_PACKET_QUEUE
            if (vboxNetFltWinPostIntnet(pNetFlt, pSG, VBOXNETFLT_PACKET_SG))
            {
                /* drop it */
                vboxNetFltWinMemFree(pSG);
                vboxNetFltWinDereferenceWinIf(pNetFlt);
            }
            else
            {
                PNDIS_PACKET pMyPacket = vboxNetFltWinNdisPacketFromSG(pNetFlt, /* PVBOXNETFLTINS */
                        pSG, /* PINTNETSG */
                        pSG, /* PVOID pBufToFree */
                        false, /* bool bToWire */
                        false); /* bool bCopyMemory */
                Assert(pMyPacket);
                if (pMyPacket)
                {
                    NDIS_SET_PACKET_STATUS(pMyPacket, NDIS_STATUS_SUCCESS);

                    DBG_CHECK_PACKET_AND_SG(pMyPacket, pSG);

                    LogFlow(("non-ndis packet info, packet created (%p)\n", pMyPacket));
                    NdisMIndicateReceivePacket(pNetFlt->u.s.WinIf.hMiniport, &pMyPacket, 1);
                }
                else
                {
                    vboxNetFltWinDereferenceWinIf(pNetFlt);
                    Status = NDIS_STATUS_RESOURCES;
                }
            }
            vboxNetFltWinDereferenceNetFlt(pNetFlt);
# else
            Status = vboxNetFltWinQuEnqueuePacket(pNetFlt, pSG, PACKET_SG | PACKET_MINE);
            if (Status != NDIS_STATUS_SUCCESS)
            {
                AssertFailed();
                vboxNetFltWinMemFree(pSG);
                break;
            }
# endif
#endif
        }
        else
        {
            PNDIS_PACKET pPacket;
            PNDIS_BUFFER pTransferBuffer;
            PNDIS_BUFFER pOrigBuffer;
            PUCHAR pMemBuf;
            UINT cbBuf = cbPacket + cbHeaderBuffer;
            UINT cbTransferred;

            /* allocate NDIS Packet buffer */
            NdisAllocatePacket(&Status, &pPacket, pNetFlt->u.s.WinIf.hRecvPacketPool);
            if (Status != NDIS_STATUS_SUCCESS)
            {
                AssertFailed();
                break;
            }

            VBOXNETFLT_OOB_INIT(pPacket);

#ifdef VBOX_LOOPBACK_USEFLAGS
            /* set "don't loopback" flags */
            NdisGetPacketFlags(pPacket) = g_VBoxNetFltGlobalsWin.fPacketDontLoopBack;
#else
            NdisGetPacketFlags(pPacket) =  0;
#endif

            Status = vboxNetFltWinMemAlloc((PVOID*)(&pMemBuf), cbBuf);
            if (Status != NDIS_STATUS_SUCCESS)
            {
                AssertFailed();
                NdisFreePacket(pPacket);
                break;
            }
            NdisAllocateBuffer(&Status, &pTransferBuffer, pNetFlt->u.s.WinIf.hRecvBufferPool, pMemBuf + cbHeaderBuffer, cbPacket);
            if (Status != NDIS_STATUS_SUCCESS)
            {
                AssertFailed();
                Status = NDIS_STATUS_FAILURE;
                NdisFreePacket(pPacket);
                vboxNetFltWinMemFree(pMemBuf);
                break;
            }

            NdisAllocateBuffer(&Status, &pOrigBuffer, pNetFlt->u.s.WinIf.hRecvBufferPool, pMemBuf, cbBuf);
            if (Status != NDIS_STATUS_SUCCESS)
            {
                AssertFailed();
                Status = NDIS_STATUS_FAILURE;
                NdisFreeBuffer(pTransferBuffer);
                NdisFreePacket(pPacket);
                vboxNetFltWinMemFree(pMemBuf);
                break;
            }

            NdisChainBufferAtBack(pPacket, pTransferBuffer);

            NdisMoveMappedMemory(pMemBuf, pHeaderBuffer, cbHeaderBuffer);

            vboxNetFltWinPutPacketToList(&pNetFlt->u.s.WinIf.TransferDataList, pPacket, pOrigBuffer);

#ifdef DEBUG_NETFLT_RECV_TRANSFERDATA
            if (cbPacket == cbLookaheadBuffer)
            {
                NdisCopyLookaheadData(pMemBuf+cbHeaderBuffer,
                                                  pLookaheadBuffer,
                                                  cbLookaheadBuffer,
                                                  pNetFlt->u.s.WinIf.fMacOptions);
            }
            else
#endif
            {
                Assert(cbPacket > cbLookaheadBuffer);

                NdisTransferData(&Status, pNetFlt->u.s.WinIf.hBinding, MacReceiveContext,
                        0,  /* ByteOffset */
                        cbPacket, pPacket, &cbTransferred);
            }

            if (Status != NDIS_STATUS_PENDING)
            {
                vboxNetFltWinPtTransferDataComplete(pNetFlt, pPacket, Status, cbTransferred);
            }
        }
    } while (0);

    return Status;
}

static NDIS_STATUS vboxNetFltWinPtReceive(IN NDIS_HANDLE hProtocolBindingContext,
                        IN NDIS_HANDLE MacReceiveContext,
                        IN PVOID pHeaderBuffer,
                        IN UINT cbHeaderBuffer,
                        IN PVOID pLookAheadBuffer,
                        IN UINT cbLookAheadBuffer,
                        IN UINT cbPacket)
{
    PVBOXNETFLTINS pNetFlt = (PVBOXNETFLTINS)hProtocolBindingContext;
    PNDIS_PACKET pPacket = NULL;
    NDIS_STATUS Status = NDIS_STATUS_SUCCESS;
    bool bNetFltActive;
    bool fWinIfActive = vboxNetFltWinReferenceWinIfNetFlt(pNetFlt, &bNetFltActive);
    const bool bPassThruActive = !bNetFltActive;

    LogFlowFunc(("ENTER: pNetFlt (0x%p)\n", pNetFlt));

    if (fWinIfActive)
    {
        do
        {
#ifndef DEBUG_NETFLT_RECV_NOPACKET
            pPacket = NdisGetReceivedPacket(pNetFlt->u.s.WinIf.hBinding, MacReceiveContext);
            if (pPacket)
            {
# ifndef VBOX_LOOPBACK_USEFLAGS
                PNDIS_PACKET pLb = NULL;
# else
                if (vboxNetFltWinIsLoopedBackPacket(pPacket))
                {
                    AssertFailed();
                    /* nothing else to do here, just return the packet */
                    //NdisReturnPackets(&pPacket, 1);
                    Status = NDIS_STATUS_NOT_ACCEPTED;
                    break;
                }

                VBOXNETFLT_LBVERIFY(pNetFlt, pPacket);
# endif

                if (bNetFltActive)
                {
# ifndef VBOX_LOOPBACK_USEFLAGS
                    pLb = vboxNetFltWinLbSearchLoopBack(pNetFlt, pPacket, false);
                    if (!pLb)
# endif
                    {
                        VBOXNETFLT_LBVERIFY(pNetFlt, pPacket);

# ifdef VBOXNETFLT_NO_PACKET_QUEUE
                        if (vboxNetFltWinPostIntnet(pNetFlt, pPacket, 0))
                        {
                            /* drop it */
                            break;
                        }
# else
                        Status = vboxNetFltWinQuEnqueuePacket(pNetFlt, pPacket, PACKET_COPY);
                        Assert(Status == NDIS_STATUS_SUCCESS);
                        if (Status == NDIS_STATUS_SUCCESS)
                        {
                            //NdisReturnPackets(&pPacket, 1);
                            fWinIfActive = false;
                            bNetFltActive = false;
                            break;
                        }
# endif
                    }
# ifndef VBOX_LOOPBACK_USEFLAGS
                    else if (vboxNetFltWinLbIsFromIntNet(pLb))
                    {
                        /* nothing else to do here, just return the packet */
                        //NdisReturnPackets(&pPacket, 1);
                        Status = NDIS_STATUS_NOT_ACCEPTED;
                        break;
                    }
                    /* we are here because this is a looped back packet set not from intnet
                     * we will post it to the upper protocol */
# endif
                }

                Assert(Status == STATUS_SUCCESS);
                if (Status == STATUS_SUCCESS)
                {
# ifndef VBOX_LOOPBACK_USEFLAGS
                    Assert(!pLb || !vboxNetFltWinLbIsFromIntNet(pLb));
# endif
                    Status = vboxNetFltWinRecvPassThru(pNetFlt, pPacket);
                    Assert(Status == STATUS_SUCCESS);
                    /* we are done with packet processing, and we will
                     * not receive packet return event for this packet,
                     * fWinIfActive should be true to ensure we release WinIf*/
                    Assert(fWinIfActive);
                    if (Status == STATUS_SUCCESS)
                        break;
                }
                else
                {
                    /* intnet processing failed - fall back to no-packet mode */
                    Assert(bNetFltActive);
                    Assert(fWinIfActive);
                }

            }
#endif /* #ifndef DEBUG_NETFLT_RECV_NOPACKET */

            if (bNetFltActive)
            {
                Status = vboxNetFltWinPtReceiveActive(pNetFlt, MacReceiveContext, pHeaderBuffer, cbHeaderBuffer,
                        pLookAheadBuffer, cbLookAheadBuffer, cbPacket);
                if (NT_SUCCESS(Status))
                {
                    if (Status != NDIS_STATUS_NOT_ACCEPTED)
                    {
                        fWinIfActive = false;
                        bNetFltActive = false;
                    }
                    else
                    {
#ifndef VBOX_LOOPBACK_USEFLAGS
                        /* this is a loopback packet, nothing to do here */
#else
                        AssertFailed();
                        /* should not be here */
#endif
                    }
                    break;
                }
            }

            /* we are done with packet processing, and we will
             * not receive packet return event for this packet,
             * fWinIfActive should be true to ensure we release WinIf*/
            Assert(fWinIfActive);

            vboxNetFltWinRecvIndicatePassThru(pNetFlt, MacReceiveContext, pHeaderBuffer, cbHeaderBuffer, pLookAheadBuffer, cbLookAheadBuffer, cbPacket);
            /* the status could contain an error value here in case the IntNet recv failed,
             * ensure we return back success status */
            Status = NDIS_STATUS_SUCCESS;

        } while (0);

        if (bNetFltActive)
        {
            vboxNetFltWinDereferenceNetFlt(pNetFlt);
        }
        else if (bPassThruActive)
        {
            vboxNetFltWinDereferenceModePassThru(pNetFlt);
        }
        if (fWinIfActive)
        {
            vboxNetFltWinDereferenceWinIf(pNetFlt);
        }
    }
    else
    {
        Status = NDIS_STATUS_FAILURE;
    }

    LogFlowFunc(("LEAVE: pNetFlt (0x%p)\n", pNetFlt));

    return Status;

}

static VOID vboxNetFltWinPtReceiveComplete(NDIS_HANDLE hProtocolBindingContext)
{
    PVBOXNETFLTINS pNetFlt = (PVBOXNETFLTINS)hProtocolBindingContext;
    bool fNetFltActive;
    bool fWinIfActive = vboxNetFltWinReferenceWinIfNetFlt(pNetFlt, &fNetFltActive);
    NDIS_HANDLE hMiniport = pNetFlt->u.s.WinIf.hMiniport;
    /* Note: we're using KeGetCurrentProcessorNumber, which is not entirely correct in case
    * we're running on 64bit win7+, which can handle > 64 CPUs, however since KeGetCurrentProcessorNumber
    * always returns the number < than the number of CPUs in the first group, we're guaranteed to have CPU index < 64
    * @todo: use KeGetCurrentProcessorNumberEx for Win7+ 64 and dynamically extended array */
    ULONG iProc = KeGetCurrentProcessorNumber();
    Assert(iProc < RT_ELEMENTS(pNetFlt->u.s.WinIf.abIndicateRxComplete));

    LogFlowFunc(("ENTER: pNetFlt (0x%p)\n", pNetFlt));

    if (hMiniport != NULL && pNetFlt->u.s.WinIf.abIndicateRxComplete[iProc])
    {
        switch (pNetFlt->u.s.WinIf.enmMedium)
        {
            case NdisMedium802_3:
            case NdisMediumWan:
                NdisMEthIndicateReceiveComplete(hMiniport);
                break;
            default:
                AssertFailed();
                break;
        }
    }

    pNetFlt->u.s.WinIf.abIndicateRxComplete[iProc] = FALSE;

    if (fWinIfActive)
    {
        if (fNetFltActive)
            vboxNetFltWinDereferenceNetFlt(pNetFlt);
        else
            vboxNetFltWinDereferenceModePassThru(pNetFlt);
        vboxNetFltWinDereferenceWinIf(pNetFlt);
    }

    LogFlowFunc(("LEAVE: pNetFlt (0x%p)\n", pNetFlt));
}

static INT vboxNetFltWinPtReceivePacket(NDIS_HANDLE hProtocolBindingContext, PNDIS_PACKET pPacket)
{
    PVBOXNETFLTINS pNetFlt = (PVBOXNETFLTINS)hProtocolBindingContext;
    INT cRefCount = 0;
    bool bNetFltActive;
    bool fWinIfActive = vboxNetFltWinReferenceWinIfNetFlt(pNetFlt, &bNetFltActive);
    const bool bPassThruActive = !bNetFltActive;

    LogFlowFunc(("ENTER: pNetFlt (0x%p)\n", pNetFlt));

    if (fWinIfActive)
    {
        do
        {
#ifdef VBOX_LOOPBACK_USEFLAGS
            if (vboxNetFltWinIsLoopedBackPacket(pPacket))
            {
                AssertFailed();
                Log(("lb_rp"));

                /* nothing else to do here, just return the packet */
                cRefCount = 0;
                //NdisReturnPackets(&pPacket, 1);
                break;
            }

            VBOXNETFLT_LBVERIFY(pNetFlt, pPacket);
#endif

            if (bNetFltActive)
            {
#ifndef VBOX_LOOPBACK_USEFLAGS
                PNDIS_PACKET pLb = vboxNetFltWinLbSearchLoopBack(pNetFlt, pPacket, false);
                if (!pLb)
#endif
                {
#ifndef VBOXNETFLT_NO_PACKET_QUEUE
                    NDIS_STATUS fStatus;
#endif
                    bool fResources = NDIS_GET_PACKET_STATUS(pPacket) == NDIS_STATUS_RESOURCES; NOREF(fResources);

                    VBOXNETFLT_LBVERIFY(pNetFlt, pPacket);
#ifdef DEBUG_misha
                    /** @todo remove this assert.
                     * this is a temporary assert for debugging purposes:
                     * we're probably doing something wrong with the packets if the miniport reports NDIS_STATUS_RESOURCES */
                    Assert(!fResources);
#endif

#ifdef VBOXNETFLT_NO_PACKET_QUEUE
                    if (vboxNetFltWinPostIntnet(pNetFlt, pPacket, 0))
                    {
                        /* drop it */
                        cRefCount = 0;
                        break;
                    }

#else
                    fStatus = vboxNetFltWinQuEnqueuePacket(pNetFlt, pPacket, fResources ? PACKET_COPY : 0);
                    if (fStatus == NDIS_STATUS_SUCCESS)
                    {
                        bNetFltActive = false;
                        fWinIfActive = false;
                        if (fResources)
                        {
                            cRefCount = 0;
                            //NdisReturnPackets(&pPacket, 1);
                        }
                        else
                            cRefCount = 1;
                        break;
                    }
                    else
                    {
                        AssertFailed();
                    }
#endif
                }
#ifndef VBOX_LOOPBACK_USEFLAGS
                else if (vboxNetFltWinLbIsFromIntNet(pLb))
                {
                    /* the packet is from intnet, it has already been set to the host,
                     * no need for loopng it back to the host again */
                    /* nothing else to do here, just return the packet */
                    cRefCount = 0;
                    //NdisReturnPackets(&pPacket, 1);
                    break;
                }
#endif
            }

            cRefCount = vboxNetFltWinRecvPacketPassThru(pNetFlt, pPacket);
            if (cRefCount)
            {
                Assert(cRefCount == 1);
                fWinIfActive = false;
            }

        } while (FALSE);

        if (bNetFltActive)
        {
            vboxNetFltWinDereferenceNetFlt(pNetFlt);
        }
        else if (bPassThruActive)
        {
            vboxNetFltWinDereferenceModePassThru(pNetFlt);
        }
        if (fWinIfActive)
        {
            vboxNetFltWinDereferenceWinIf(pNetFlt);
        }
    }
    else
    {
        cRefCount = 0;
        //NdisReturnPackets(&pPacket, 1);
    }

    LogFlowFunc(("LEAVE: pNetFlt (0x%p), cRefCount (%d)\n", pNetFlt, cRefCount));

    return cRefCount;
}

DECLHIDDEN(bool) vboxNetFltWinPtCloseInterface(PVBOXNETFLTINS pNetFlt, PNDIS_STATUS pStatus)
{
    RTSpinlockAcquire(pNetFlt->hSpinlock);

    if (pNetFlt->u.s.WinIf.StateFlags.fInterfaceClosing)
    {
        RTSpinlockRelease(pNetFlt->hSpinlock);
        AssertFailed();
        return false;
    }
    if (pNetFlt->u.s.WinIf.hBinding == NULL)
    {
        RTSpinlockRelease(pNetFlt->hSpinlock);
        AssertFailed();
        return false;
    }

    pNetFlt->u.s.WinIf.StateFlags.fInterfaceClosing = TRUE;
    RTSpinlockRelease(pNetFlt->hSpinlock);

    NdisResetEvent(&pNetFlt->u.s.WinIf.OpenCloseEvent);
    NdisCloseAdapter(pStatus, pNetFlt->u.s.WinIf.hBinding);
    if (*pStatus == NDIS_STATUS_PENDING)
    {
        NdisWaitEvent(&pNetFlt->u.s.WinIf.OpenCloseEvent, 0);
        *pStatus = pNetFlt->u.s.WinIf.OpenCloseStatus;
    }

    Assert (*pStatus == NDIS_STATUS_SUCCESS);

    pNetFlt->u.s.WinIf.hBinding = NULL;

    return true;
}

static NDIS_STATUS vboxNetFltWinPtPnPSetPower(PVBOXNETFLTINS pNetFlt, NDIS_DEVICE_POWER_STATE enmPowerState)
{
    NDIS_DEVICE_POWER_STATE enmPrevPowerState = vboxNetFltWinGetPowerState(&pNetFlt->u.s.WinIf.PtState);

    RTSpinlockAcquire(pNetFlt->hSpinlock);

    vboxNetFltWinSetPowerState(&pNetFlt->u.s.WinIf.PtState, enmPowerState);

    if (vboxNetFltWinGetPowerState(&pNetFlt->u.s.WinIf.PtState) > NdisDeviceStateD0)
    {
        if (enmPrevPowerState == NdisDeviceStateD0)
        {
            pNetFlt->u.s.WinIf.StateFlags.fStandBy = TRUE;
        }
        RTSpinlockRelease(pNetFlt->hSpinlock);
        vboxNetFltWinPtRequestsWaitComplete(pNetFlt);
        vboxNetFltWinWaitDereference(&pNetFlt->u.s.WinIf.MpState);
        vboxNetFltWinWaitDereference(&pNetFlt->u.s.WinIf.PtState);

        /* check packet pool is empty */
        UINT cPPUsage = NdisPacketPoolUsage(pNetFlt->u.s.WinIf.hSendPacketPool);
        Assert(cPPUsage == 0);
        cPPUsage = NdisPacketPoolUsage(pNetFlt->u.s.WinIf.hRecvPacketPool);
        Assert(cPPUsage == 0);
        /* for debugging only, ignore the err in release */
        NOREF(cPPUsage);

        Assert(!pNetFlt->u.s.WinIf.StateFlags.fRequestInfo);
    }
    else
    {
        if (enmPrevPowerState > NdisDeviceStateD0)
        {
            pNetFlt->u.s.WinIf.StateFlags.fStandBy = FALSE;
        }

        if (pNetFlt->u.s.WinIf.StateFlags.fRequestInfo & VBOXNDISREQUEST_QUEUED)
        {
            pNetFlt->u.s.WinIf.StateFlags.fRequestInfo = VBOXNDISREQUEST_INPROGRESS;
            RTSpinlockRelease(pNetFlt->hSpinlock);

            vboxNetFltWinMpRequestPost(pNetFlt);
        }
        else
        {
            RTSpinlockRelease(pNetFlt->hSpinlock);
        }
    }

    return NDIS_STATUS_SUCCESS;
}


static NDIS_STATUS vboxNetFltWinPtPnPEvent(IN NDIS_HANDLE hProtocolBindingContext, IN PNET_PNP_EVENT pNetPnPEvent)
{
    PVBOXNETFLTINS pNetFlt = (PVBOXNETFLTINS)hProtocolBindingContext;

    LogFlowFunc(("ENTER: pNetFlt (0x%p), NetEvent (%d)\n", pNetFlt, pNetPnPEvent->NetEvent));

    switch (pNetPnPEvent->NetEvent)
    {
        case NetEventSetPower:
        {
            NDIS_DEVICE_POWER_STATE enmPowerState = *((PNDIS_DEVICE_POWER_STATE)pNetPnPEvent->Buffer);
            NDIS_STATUS rcNdis = vboxNetFltWinPtPnPSetPower(pNetFlt, enmPowerState);
            LogFlowFunc(("LEAVE: pNetFlt (0x%p), NetEvent (%d), rcNdis=%#x\n", pNetFlt, pNetPnPEvent->NetEvent, rcNdis));
            return rcNdis;
        }

        case NetEventReconfigure:
        {
            if (!pNetFlt)
            {
                NdisReEnumerateProtocolBindings(g_VBoxNetFltGlobalsWin.Pt.hProtocol);
            }
        }
        /** @todo r=bird: Is the fall thru intentional?? */
        default:
            LogFlowFunc(("LEAVE: pNetFlt (0x%p), NetEvent (%d)\n", pNetFlt, pNetPnPEvent->NetEvent));
            return NDIS_STATUS_SUCCESS;
    }

}

#ifdef __cplusplus
# define PTCHARS_40(_p) ((_p).Ndis40Chars)
#else
# define PTCHARS_40(_p) (_p)
#endif

/**
 * register the protocol edge
 */
DECLHIDDEN(NDIS_STATUS) vboxNetFltWinPtRegister(PVBOXNETFLTGLOBALS_PT pGlobalsPt, PDRIVER_OBJECT pDriverObject,
                                                PUNICODE_STRING pRegistryPathStr)
{
    RT_NOREF2(pDriverObject, pRegistryPathStr);
    NDIS_PROTOCOL_CHARACTERISTICS PtChars;
    NDIS_STRING NameStr;

    NdisInitUnicodeString(&NameStr, VBOXNETFLT_NAME_PROTOCOL);

    NdisZeroMemory(&PtChars, sizeof (PtChars));
    PTCHARS_40(PtChars).MajorNdisVersion = VBOXNETFLT_VERSION_PT_NDIS_MAJOR;
    PTCHARS_40(PtChars).MinorNdisVersion = VBOXNETFLT_VERSION_PT_NDIS_MINOR;

    PTCHARS_40(PtChars).Name = NameStr;
    PTCHARS_40(PtChars).OpenAdapterCompleteHandler = vboxNetFltWinPtOpenAdapterComplete;
    PTCHARS_40(PtChars).CloseAdapterCompleteHandler = vboxNetFltWinPtCloseAdapterComplete;
    PTCHARS_40(PtChars).SendCompleteHandler = vboxNetFltWinPtSendComplete;
    PTCHARS_40(PtChars).TransferDataCompleteHandler = vboxNetFltWinPtTransferDataComplete;
    PTCHARS_40(PtChars).ResetCompleteHandler = vboxNetFltWinPtResetComplete;
    PTCHARS_40(PtChars).RequestCompleteHandler = vboxNetFltWinPtRequestComplete;
    PTCHARS_40(PtChars).ReceiveHandler = vboxNetFltWinPtReceive;
    PTCHARS_40(PtChars).ReceiveCompleteHandler = vboxNetFltWinPtReceiveComplete;
    PTCHARS_40(PtChars).StatusHandler = vboxNetFltWinPtStatus;
    PTCHARS_40(PtChars).StatusCompleteHandler = vboxNetFltWinPtStatusComplete;
    PTCHARS_40(PtChars).BindAdapterHandler = vboxNetFltWinPtBindAdapter;
    PTCHARS_40(PtChars).UnbindAdapterHandler = vboxNetFltWinPtUnbindAdapter;
    PTCHARS_40(PtChars).UnloadHandler = vboxNetFltWinPtUnloadProtocol;
#if !defined(DEBUG_NETFLT_RECV)
    PTCHARS_40(PtChars).ReceivePacketHandler = vboxNetFltWinPtReceivePacket;
#endif
    PTCHARS_40(PtChars).PnPEventHandler = vboxNetFltWinPtPnPEvent;

    NDIS_STATUS Status;
    NdisRegisterProtocol(&Status, &pGlobalsPt->hProtocol, &PtChars, sizeof (PtChars));
    Assert(Status == STATUS_SUCCESS);
    return Status;
}

/**
 * deregister the protocol edge
 */
DECLHIDDEN(NDIS_STATUS) vboxNetFltWinPtDeregister(PVBOXNETFLTGLOBALS_PT pGlobalsPt)
{
    if (!pGlobalsPt->hProtocol)
        return NDIS_STATUS_SUCCESS;

    NDIS_STATUS Status;

    NdisDeregisterProtocol(&Status, pGlobalsPt->hProtocol);
    Assert (Status == NDIS_STATUS_SUCCESS);
    if (Status == NDIS_STATUS_SUCCESS)
    {
        NdisZeroMemory(pGlobalsPt, sizeof (*pGlobalsPt));
    }
    return Status;
}