summaryrefslogtreecommitdiff
path: root/windows/TapDriver6/oidrequest.c
blob: a6882f89659b163cbadb5d83d78327bab9b05688 (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
/*
 *  TAP-Windows -- A kernel driver to provide virtual tap
 *                 device functionality on Windows.
 *
 *  This code was inspired by the CIPE-Win32 driver by Damion K. Wilson.
 *
 *  This source code is Copyright (C) 2002-2014 OpenVPN Technologies, Inc.,
 *  and is released under the GPL version 2 (see below).
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2
 *  as published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program (see the file COPYING included with this
 *  distribution); if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

//
// Include files.
//

#include "tap.h"

#ifndef DBG

#define DBG_PRINT_OID_NAME

#else

VOID
DBG_PRINT_OID_NAME(
    __in  NDIS_OID  Oid
    )
{
    PCHAR oidName = NULL;

    switch (Oid){

        #undef MAKECASE
        #define MAKECASE(oidx) case oidx: oidName = #oidx "\n"; break;

        /* Operational OIDs */
        MAKECASE(OID_GEN_SUPPORTED_LIST)
        MAKECASE(OID_GEN_HARDWARE_STATUS)
        MAKECASE(OID_GEN_MEDIA_SUPPORTED)
        MAKECASE(OID_GEN_MEDIA_IN_USE)
        MAKECASE(OID_GEN_MAXIMUM_LOOKAHEAD)
        MAKECASE(OID_GEN_MAXIMUM_FRAME_SIZE)
        MAKECASE(OID_GEN_LINK_SPEED)
        MAKECASE(OID_GEN_TRANSMIT_BUFFER_SPACE)
        MAKECASE(OID_GEN_RECEIVE_BUFFER_SPACE)
        MAKECASE(OID_GEN_TRANSMIT_BLOCK_SIZE)
        MAKECASE(OID_GEN_RECEIVE_BLOCK_SIZE)
        MAKECASE(OID_GEN_VENDOR_ID)
        MAKECASE(OID_GEN_VENDOR_DESCRIPTION)
        MAKECASE(OID_GEN_VENDOR_DRIVER_VERSION)
        MAKECASE(OID_GEN_CURRENT_PACKET_FILTER)
        MAKECASE(OID_GEN_CURRENT_LOOKAHEAD)
        MAKECASE(OID_GEN_DRIVER_VERSION)
        MAKECASE(OID_GEN_MAXIMUM_TOTAL_SIZE)
        MAKECASE(OID_GEN_PROTOCOL_OPTIONS)
        MAKECASE(OID_GEN_MAC_OPTIONS)
        MAKECASE(OID_GEN_MEDIA_CONNECT_STATUS)
        MAKECASE(OID_GEN_MAXIMUM_SEND_PACKETS)
        MAKECASE(OID_GEN_SUPPORTED_GUIDS)
        MAKECASE(OID_GEN_NETWORK_LAYER_ADDRESSES)
        MAKECASE(OID_GEN_TRANSPORT_HEADER_OFFSET)
        MAKECASE(OID_GEN_MEDIA_CAPABILITIES)
        MAKECASE(OID_GEN_PHYSICAL_MEDIUM)
        MAKECASE(OID_GEN_MACHINE_NAME)
        MAKECASE(OID_GEN_VLAN_ID)
        MAKECASE(OID_GEN_RNDIS_CONFIG_PARAMETER)

        /* Operational OIDs for NDIS 6.0 */
        MAKECASE(OID_GEN_MAX_LINK_SPEED)
        MAKECASE(OID_GEN_LINK_STATE)
        MAKECASE(OID_GEN_LINK_PARAMETERS)
        MAKECASE(OID_GEN_MINIPORT_RESTART_ATTRIBUTES)
        MAKECASE(OID_GEN_ENUMERATE_PORTS)
        MAKECASE(OID_GEN_PORT_STATE)
        MAKECASE(OID_GEN_PORT_AUTHENTICATION_PARAMETERS)
        MAKECASE(OID_GEN_INTERRUPT_MODERATION)
        MAKECASE(OID_GEN_PHYSICAL_MEDIUM_EX)

        /* Statistical OIDs */
        MAKECASE(OID_GEN_XMIT_OK)
        MAKECASE(OID_GEN_RCV_OK)
        MAKECASE(OID_GEN_XMIT_ERROR)
        MAKECASE(OID_GEN_RCV_ERROR)
        MAKECASE(OID_GEN_RCV_NO_BUFFER)
        MAKECASE(OID_GEN_DIRECTED_BYTES_XMIT)
        MAKECASE(OID_GEN_DIRECTED_FRAMES_XMIT)
        MAKECASE(OID_GEN_MULTICAST_BYTES_XMIT)
        MAKECASE(OID_GEN_MULTICAST_FRAMES_XMIT)
        MAKECASE(OID_GEN_BROADCAST_BYTES_XMIT)
        MAKECASE(OID_GEN_BROADCAST_FRAMES_XMIT)
        MAKECASE(OID_GEN_DIRECTED_BYTES_RCV)
        MAKECASE(OID_GEN_DIRECTED_FRAMES_RCV)
        MAKECASE(OID_GEN_MULTICAST_BYTES_RCV)
        MAKECASE(OID_GEN_MULTICAST_FRAMES_RCV)
        MAKECASE(OID_GEN_BROADCAST_BYTES_RCV)
        MAKECASE(OID_GEN_BROADCAST_FRAMES_RCV)
        MAKECASE(OID_GEN_RCV_CRC_ERROR)
        MAKECASE(OID_GEN_TRANSMIT_QUEUE_LENGTH)

        /* Statistical OIDs for NDIS 6.0 */
        MAKECASE(OID_GEN_STATISTICS)
        MAKECASE(OID_GEN_BYTES_RCV)
        MAKECASE(OID_GEN_BYTES_XMIT)
        MAKECASE(OID_GEN_RCV_DISCARDS)
        MAKECASE(OID_GEN_XMIT_DISCARDS)

        /* Misc OIDs */
        MAKECASE(OID_GEN_GET_TIME_CAPS)
        MAKECASE(OID_GEN_GET_NETCARD_TIME)
        MAKECASE(OID_GEN_NETCARD_LOAD)
        MAKECASE(OID_GEN_DEVICE_PROFILE)
        MAKECASE(OID_GEN_INIT_TIME_MS)
        MAKECASE(OID_GEN_RESET_COUNTS)
        MAKECASE(OID_GEN_MEDIA_SENSE_COUNTS)

        /* PnP power management operational OIDs */
        MAKECASE(OID_PNP_CAPABILITIES)
        MAKECASE(OID_PNP_SET_POWER)
        MAKECASE(OID_PNP_QUERY_POWER)
        MAKECASE(OID_PNP_ADD_WAKE_UP_PATTERN)
        MAKECASE(OID_PNP_REMOVE_WAKE_UP_PATTERN)
        MAKECASE(OID_PNP_ENABLE_WAKE_UP)
        MAKECASE(OID_PNP_WAKE_UP_PATTERN_LIST)

        /* PnP power management statistical OIDs */
        MAKECASE(OID_PNP_WAKE_UP_ERROR)
        MAKECASE(OID_PNP_WAKE_UP_OK)

        /* Ethernet operational OIDs */
        MAKECASE(OID_802_3_PERMANENT_ADDRESS)
        MAKECASE(OID_802_3_CURRENT_ADDRESS)
        MAKECASE(OID_802_3_MULTICAST_LIST)
        MAKECASE(OID_802_3_MAXIMUM_LIST_SIZE)
        MAKECASE(OID_802_3_MAC_OPTIONS)

        /* Ethernet operational OIDs for NDIS 6.0 */
        MAKECASE(OID_802_3_ADD_MULTICAST_ADDRESS)
        MAKECASE(OID_802_3_DELETE_MULTICAST_ADDRESS)

        /* Ethernet statistical OIDs */
        MAKECASE(OID_802_3_RCV_ERROR_ALIGNMENT)
        MAKECASE(OID_802_3_XMIT_ONE_COLLISION)
        MAKECASE(OID_802_3_XMIT_MORE_COLLISIONS)
        MAKECASE(OID_802_3_XMIT_DEFERRED)
        MAKECASE(OID_802_3_XMIT_MAX_COLLISIONS)
        MAKECASE(OID_802_3_RCV_OVERRUN)
        MAKECASE(OID_802_3_XMIT_UNDERRUN)
        MAKECASE(OID_802_3_XMIT_HEARTBEAT_FAILURE)
        MAKECASE(OID_802_3_XMIT_TIMES_CRS_LOST)
        MAKECASE(OID_802_3_XMIT_LATE_COLLISIONS)

        /*  TCP/IP OIDs */
        MAKECASE(OID_TCP_TASK_OFFLOAD)
        MAKECASE(OID_TCP_TASK_IPSEC_ADD_SA)
        MAKECASE(OID_TCP_TASK_IPSEC_DELETE_SA)
        MAKECASE(OID_TCP_SAN_SUPPORT)
        MAKECASE(OID_TCP_TASK_IPSEC_ADD_UDPESP_SA)
        MAKECASE(OID_TCP_TASK_IPSEC_DELETE_UDPESP_SA)
        MAKECASE(OID_TCP4_OFFLOAD_STATS)
        MAKECASE(OID_TCP6_OFFLOAD_STATS)
        MAKECASE(OID_IP4_OFFLOAD_STATS)
        MAKECASE(OID_IP6_OFFLOAD_STATS)

        /* TCP offload OIDs for NDIS 6 */
        MAKECASE(OID_TCP_OFFLOAD_CURRENT_CONFIG)
        MAKECASE(OID_TCP_OFFLOAD_PARAMETERS)
        MAKECASE(OID_TCP_OFFLOAD_HARDWARE_CAPABILITIES)
        MAKECASE(OID_TCP_CONNECTION_OFFLOAD_CURRENT_CONFIG)
        MAKECASE(OID_TCP_CONNECTION_OFFLOAD_HARDWARE_CAPABILITIES)
        MAKECASE(OID_OFFLOAD_ENCAPSULATION)

#if (NDIS_SUPPORT_NDIS620)
        /* VMQ OIDs for NDIS 6.20 */
        MAKECASE(OID_RECEIVE_FILTER_FREE_QUEUE)
        MAKECASE(OID_RECEIVE_FILTER_CLEAR_FILTER)
        MAKECASE(OID_RECEIVE_FILTER_ALLOCATE_QUEUE)
        MAKECASE(OID_RECEIVE_FILTER_QUEUE_ALLOCATION_COMPLETE)
        MAKECASE(OID_RECEIVE_FILTER_SET_FILTER)
#endif

#if (NDIS_SUPPORT_NDIS630)
        /* NDIS QoS OIDs for NDIS 6.30 */
        MAKECASE(OID_QOS_PARAMETERS)
#endif
    }

    if (oidName)
    {
        DEBUGP(("OID: %s", oidName));
    }
    else
    {
        DEBUGP(("<** Unknown OID 0x%08x **>\n", Oid));
    }
}

#endif // DBG

//======================================================================
// TAP NDIS 6 OID Request Callbacks
//======================================================================

NDIS_STATUS
tapSetMulticastList(
    __in PTAP_ADAPTER_CONTEXT   Adapter,
    __in PNDIS_OID_REQUEST      OidRequest
    )
{
    NDIS_STATUS   status = NDIS_STATUS_SUCCESS;

    //
    // Initialize.
    //
    OidRequest->DATA.SET_INFORMATION.BytesNeeded = MACADDR_SIZE;
    OidRequest->DATA.SET_INFORMATION.BytesRead
        = OidRequest->DATA.SET_INFORMATION.InformationBufferLength;


    do
    {
        if (OidRequest->DATA.SET_INFORMATION.InformationBufferLength % MACADDR_SIZE)
        {
            status = NDIS_STATUS_INVALID_LENGTH;
            break;
        }

        if (OidRequest->DATA.SET_INFORMATION.InformationBufferLength > (TAP_MAX_MCAST_LIST * MACADDR_SIZE))
        {
            status = NDIS_STATUS_MULTICAST_FULL;
            OidRequest->DATA.SET_INFORMATION.BytesNeeded = TAP_MAX_MCAST_LIST * MACADDR_SIZE;
            break;
        }

        // BUGBUG!!! Is lock needed??? If so, use NDIS_RW_LOCK. Also apply to packet filter.

        NdisZeroMemory(Adapter->MCList,
                       TAP_MAX_MCAST_LIST * MACADDR_SIZE);

        NdisMoveMemory(Adapter->MCList,
                       OidRequest->DATA.SET_INFORMATION.InformationBuffer,
                       OidRequest->DATA.SET_INFORMATION.InformationBufferLength);

        Adapter->ulMCListSize = OidRequest->DATA.SET_INFORMATION.InformationBufferLength / MACADDR_SIZE;

    } while(FALSE);
    return status;
}

NDIS_STATUS
tapSetPacketFilter(
    __in PTAP_ADAPTER_CONTEXT   Adapter,
    __in ULONG                  PacketFilter
    )
{
    NDIS_STATUS   status = NDIS_STATUS_SUCCESS;

    // any bits not supported?
    if (PacketFilter & ~(TAP_SUPPORTED_FILTERS))
    {
        DEBUGP (("[TAP] Unsupported packet filter: 0x%08x\n", PacketFilter));
        status = NDIS_STATUS_NOT_SUPPORTED;
    }
    else
    {
        // Any actual filtering changes?
        if (PacketFilter != Adapter->PacketFilter)
        {
            //
            // Change the filtering modes on hardware
            //

            // Save the new packet filter value
            Adapter->PacketFilter = PacketFilter;
        }
    }

    return status;
}

NDIS_STATUS
AdapterSetPowerD0(
    __in PTAP_ADAPTER_CONTEXT   Adapter
    )
/*++
Routine Description:

    NIC power has been restored to the working power state (D0).
    Prepare the NIC for normal operation:
        - Restore hardware context (packet filters, multicast addresses, MAC address, etc.)
        - Enable interrupts and the NIC's DMA engine.

Arguments:

    Adapter     - Pointer to adapter block

Return Value:

    NDIS_STATUS   

--*/      
{
    NDIS_STATUS status = NDIS_STATUS_SUCCESS;

    DEBUGP (("[TAP] PowerState: Fully powered\n"));

    // Start data path...

    return status;
}

NDIS_STATUS
AdapterSetPowerLow(
    __in PTAP_ADAPTER_CONTEXT       Adapter,
    __in NDIS_DEVICE_POWER_STATE    PowerState
    )
/*++
Routine Description:

    The NIC is about to be transitioned to a low power state. 
    Prepare the NIC for the sleeping state:
        - Disable interrupts and the NIC's DMA engine, cancel timers.  
        - Save any hardware context that the NIC cannot preserve in 
          a sleeping state (packet filters, multicast addresses, 
          the current MAC address, etc.)
    A miniport driver cannot access the NIC hardware after 
    the NIC has been set to the D3 state by the bus driver.

    Miniport drivers NDIS v6.30 and above 
        Do NOT wait for NDIS to return the ownership of all 
        NBLs from outstanding receive indications
        Retain ownership of all the receive descriptors and 
        packet buffers previously owned by the hardware.

Arguments:

    Adapter         - Pointer to adapter block
    PowerState      - New power state

Return Value:

    NDIS_STATUS   

--*/      
{
    NDIS_STATUS status = NDIS_STATUS_SUCCESS;

    DEBUGP (("[TAP] PowerState: Low-power\n"));

    //
    // Miniport drivers NDIS v6.20 and below are 
    // paused prior the low power transition 
    //

    // Check for paused state...
    // Verify data path stopped...

    return status;
}

NDIS_STATUS
tapSetInformation(
    __in PTAP_ADAPTER_CONTEXT   Adapter,
    __in PNDIS_OID_REQUEST      OidRequest
    )
/*++

Routine Description:

    Helper function to perform a set OID request

Arguments:

    Adapter         -
    NdisSetRequest  - The OID to set

Return Value:

    NDIS_STATUS

--*/
{
    NDIS_STATUS    status = NDIS_STATUS_SUCCESS;

    DBG_PRINT_OID_NAME(OidRequest->DATA.SET_INFORMATION.Oid);

    switch(OidRequest->DATA.SET_INFORMATION.Oid)
    {
    case OID_802_3_MULTICAST_LIST:
        //
        // Set the multicast address list on the NIC for packet reception.
        // The NIC driver can set a limit on the number of multicast
        // addresses bound protocol drivers can enable simultaneously.
        // NDIS returns NDIS_STATUS_MULTICAST_FULL if a protocol driver
        // exceeds this limit or if it specifies an invalid multicast
        // address.
        //
        status = tapSetMulticastList(Adapter,OidRequest);
        break;

    case OID_GEN_CURRENT_LOOKAHEAD:
        //
        // A protocol driver can set a suggested value for the number
        // of bytes to be used in its binding; however, the underlying
        // NIC driver is never required to limit its indications to
        // the value set.
        //
        if (OidRequest->DATA.SET_INFORMATION.InformationBufferLength != sizeof(ULONG))
        {
            OidRequest->DATA.SET_INFORMATION.BytesNeeded = sizeof(ULONG);
            status = NDIS_STATUS_INVALID_LENGTH;
            break;
        }

        Adapter->ulLookahead = *(PULONG)OidRequest->DATA.SET_INFORMATION.InformationBuffer;

        OidRequest->DATA.SET_INFORMATION.BytesRead = sizeof(ULONG);
        status = NDIS_STATUS_SUCCESS;
        break;

    case OID_GEN_CURRENT_PACKET_FILTER:
            //
            // Program the hardware to indicate the packets
            // of certain filter types.
            //
            if(OidRequest->DATA.SET_INFORMATION.InformationBufferLength != sizeof(ULONG))
            {
                OidRequest->DATA.SET_INFORMATION.BytesNeeded = sizeof(ULONG);
                status = NDIS_STATUS_INVALID_LENGTH;
                break;
            }

            OidRequest->DATA.SET_INFORMATION.BytesRead
                = OidRequest->DATA.SET_INFORMATION.InformationBufferLength;

            status = tapSetPacketFilter(
                            Adapter,
                            *((PULONG)OidRequest->DATA.SET_INFORMATION.InformationBuffer)
                            );

            break;

    case OID_PNP_SET_POWER:
        {
            // Sanity check.
            if (OidRequest->DATA.SET_INFORMATION.InformationBufferLength
                < sizeof(NDIS_DEVICE_POWER_STATE)
                )
            {
                status = NDIS_STATUS_INVALID_LENGTH;
            }
            else
            {
                NDIS_DEVICE_POWER_STATE     PowerState;

                PowerState = *(PNDIS_DEVICE_POWER_STATE UNALIGNED)OidRequest->DATA.SET_INFORMATION.InformationBuffer;
                OidRequest->DATA.SET_INFORMATION.BytesRead = sizeof(NDIS_DEVICE_POWER_STATE);

                if(PowerState < NdisDeviceStateD0  ||
                    PowerState > NdisDeviceStateD3)
                {
                    status = NDIS_STATUS_INVALID_DATA;
                }
                else
                {
                    Adapter->CurrentPowerState = PowerState;

                    if (PowerState == NdisDeviceStateD0)
                    {
                        status = AdapterSetPowerD0(Adapter);
                    }
                    else
                    {
                        status = AdapterSetPowerLow(Adapter, PowerState);
                    }
                }
            }
        }
        break;

#if (NDIS_SUPPORT_NDIS61)
    case OID_PNP_ADD_WAKE_UP_PATTERN:
    case OID_PNP_REMOVE_WAKE_UP_PATTERN:
    case OID_PNP_ENABLE_WAKE_UP:
#endif
        ASSERT(!"NIC does not support wake on LAN OIDs"); 
    default:
        //
        // The entry point may by used by other requests
        //
        status = NDIS_STATUS_NOT_SUPPORTED;
        break;
    }

    return status;
}

NDIS_STATUS
tapQueryInformation(
    __in PTAP_ADAPTER_CONTEXT   Adapter,
    __in PNDIS_OID_REQUEST      OidRequest
    )
/*++

Routine Description:

    Helper function to perform a query OID request

Arguments:

    Adapter         -
    OidRequest  - The OID request that is being queried

Return Value:

    NDIS_STATUS

--*/
{
    NDIS_STATUS             status = NDIS_STATUS_SUCCESS;
    NDIS_MEDIUM             Medium = TAP_MEDIUM_TYPE;
    NDIS_HARDWARE_STATUS    HardwareStatus = NdisHardwareStatusReady;
    UCHAR                   VendorDesc[] = TAP_VENDOR_DESC;
    ULONG                   ulInfo;
    USHORT                  usInfo;
    ULONG64                 ulInfo64;

    // Default to returning the ULONG value
    PVOID                   pInfo=NULL;
    ULONG                   ulInfoLen = sizeof(ulInfo);

    // ATTENTION!!! Ignore OIDs to noisy to print...
    if((OidRequest->DATA.QUERY_INFORMATION.Oid != OID_GEN_STATISTICS)
        && (OidRequest->DATA.QUERY_INFORMATION.Oid != OID_IP4_OFFLOAD_STATS)
        && (OidRequest->DATA.QUERY_INFORMATION.Oid != OID_IP6_OFFLOAD_STATS)
        )
    {
        DBG_PRINT_OID_NAME(OidRequest->DATA.QUERY_INFORMATION.Oid);
    }

    // Dispatch based on object identifier (OID).
    switch(OidRequest->DATA.QUERY_INFORMATION.Oid)
    {
    case OID_GEN_HARDWARE_STATUS:
        //
        // Specify the current hardware status of the underlying NIC as
        // one of the following NDIS_HARDWARE_STATUS-type values.
        //
        pInfo = (PVOID) &HardwareStatus;
        ulInfoLen = sizeof(NDIS_HARDWARE_STATUS);
        break;

    case OID_802_3_PERMANENT_ADDRESS:
        //
        // Return the MAC address of the NIC burnt in the hardware.
        //
        pInfo = Adapter->PermanentAddress;
        ulInfoLen = MACADDR_SIZE;
        break;

    case OID_802_3_CURRENT_ADDRESS:
        //
        // Return the MAC address the NIC is currently programmed to
        // use. Note that this address could be different from the
        // permananent address as the user can override using
        // registry. Read NdisReadNetworkAddress doc for more info.
        //
        pInfo = Adapter->CurrentAddress;
        ulInfoLen = MACADDR_SIZE;
        break;

    case OID_GEN_MEDIA_SUPPORTED:
        //
        // Return an array of media that are supported by the miniport.
        // This miniport only supports one medium (Ethernet), so the OID
        // returns identical results to OID_GEN_MEDIA_IN_USE.
        //

        __fallthrough;

    case OID_GEN_MEDIA_IN_USE:
        //
        // Return an array of media that are currently in use by the
        // miniport.  This array should be a subset of the array returned
        // by OID_GEN_MEDIA_SUPPORTED.
        //
        pInfo = &Medium;
        ulInfoLen = sizeof(Medium);
        break;

    case OID_GEN_MAXIMUM_TOTAL_SIZE:
        //
        // Specify the maximum total packet length, in bytes, the NIC
        // supports including the header. A protocol driver might use
        // this returned length as a gauge to determine the maximum
        // size packet that a NIC driver could forward to the
        // protocol driver. The miniport driver must never indicate
        // up to the bound protocol driver packets received over the
        // network that are longer than the packet size specified by
        // OID_GEN_MAXIMUM_TOTAL_SIZE.
        //

        __fallthrough;

    case OID_GEN_TRANSMIT_BLOCK_SIZE:
        //
        // The OID_GEN_TRANSMIT_BLOCK_SIZE OID specifies the minimum
        // number of bytes that a single net packet occupies in the
        // transmit buffer space of the NIC. In our case, the transmit
        // block size is identical to its maximum packet size.
        __fallthrough;

    case OID_GEN_RECEIVE_BLOCK_SIZE:
        //
        // The OID_GEN_RECEIVE_BLOCK_SIZE OID specifies the amount of
        // storage, in bytes, that a single packet occupies in the receive
        // buffer space of the NIC.
        //
        ulInfo = (ULONG) TAP_MAX_FRAME_SIZE;
        pInfo = &ulInfo;
        break;

    case OID_GEN_INTERRUPT_MODERATION:
        {
            PNDIS_INTERRUPT_MODERATION_PARAMETERS moderationParams
                = (PNDIS_INTERRUPT_MODERATION_PARAMETERS)OidRequest->DATA.QUERY_INFORMATION.InformationBuffer;

            moderationParams->Header.Type = NDIS_OBJECT_TYPE_DEFAULT; 
            moderationParams->Header.Revision = NDIS_INTERRUPT_MODERATION_PARAMETERS_REVISION_1;
            moderationParams->Header.Size = NDIS_SIZEOF_INTERRUPT_MODERATION_PARAMETERS_REVISION_1;
            moderationParams->Flags = 0;
            moderationParams->InterruptModeration = NdisInterruptModerationNotSupported;
            ulInfoLen = NDIS_SIZEOF_INTERRUPT_MODERATION_PARAMETERS_REVISION_1;
        }
        break;

    case OID_PNP_QUERY_POWER:
        // Simply succeed this.
        break;

    case OID_GEN_VENDOR_ID:
        //
        // Specify a three-byte IEEE-registered vendor code, followed
        // by a single byte that the vendor assigns to identify a
        // particular NIC. The IEEE code uniquely identifies the vendor
        // and is the same as the three bytes appearing at the beginning
        // of the NIC hardware address. Vendors without an IEEE-registered
        // code should use the value 0xFFFFFF.
        //

        ulInfo = TAP_VENDOR_ID;
        pInfo = &ulInfo;
        break;

    case OID_GEN_VENDOR_DESCRIPTION:
        //
        // Specify a zero-terminated string describing the NIC vendor.
        //
        pInfo = VendorDesc;
        ulInfoLen = sizeof(VendorDesc);
        break;

    case OID_GEN_VENDOR_DRIVER_VERSION:
        //
        // Specify the vendor-assigned version number of the NIC driver.
        // The low-order half of the return value specifies the minor
        // version; the high-order half specifies the major version.
        //

        ulInfo = TAP_DRIVER_VENDOR_VERSION;
        pInfo = &ulInfo;
        break;

    case OID_GEN_DRIVER_VERSION:
        //
        // Specify the NDIS version in use by the NIC driver. The high
        // byte is the major version number; the low byte is the minor
        // version number.
        //
        usInfo = (USHORT) (TAP_NDIS_MAJOR_VERSION<<8) + TAP_NDIS_MINOR_VERSION;
        pInfo = (PVOID) &usInfo;
        ulInfoLen = sizeof(USHORT);
        break;

    case OID_802_3_MAXIMUM_LIST_SIZE:
        //
        // The maximum number of multicast addresses the NIC driver
        // can manage. This list is global for all protocols bound
        // to (or above) the NIC. Consequently, a protocol can receive
        // NDIS_STATUS_MULTICAST_FULL from the NIC driver when
        // attempting to set the multicast address list, even if
        // the number of elements in the given list is less than
        // the number originally returned for this query.
        //

        ulInfo = TAP_MAX_MCAST_LIST;
        pInfo = &ulInfo;
        break;

    case OID_GEN_XMIT_ERROR:
        ulInfo = (ULONG)
            (Adapter->TxAbortExcessCollisions +
            Adapter->TxDmaUnderrun +
            Adapter->TxLostCRS +
            Adapter->TxLateCollisions+
            Adapter->TransmitFailuresOther);
        pInfo = &ulInfo;
        break;

    case OID_GEN_RCV_ERROR:
        ulInfo = (ULONG)
            (Adapter->RxCrcErrors +
            Adapter->RxAlignmentErrors +
            Adapter->RxDmaOverrunErrors +
            Adapter->RxRuntErrors);
        pInfo = &ulInfo;
        break;

    case OID_GEN_RCV_DISCARDS:
        ulInfo = (ULONG)Adapter->RxResourceErrors;
        pInfo = &ulInfo;
        break;

    case OID_GEN_RCV_NO_BUFFER:
        ulInfo = (ULONG)Adapter->RxResourceErrors;
        pInfo = &ulInfo;
        break;

    case OID_GEN_XMIT_OK:
        ulInfo64 = Adapter->FramesTxBroadcast
            + Adapter->FramesTxMulticast
            + Adapter->FramesTxDirected;
        pInfo = &ulInfo64;
        if (OidRequest->DATA.QUERY_INFORMATION.InformationBufferLength >= sizeof(ULONG64) ||
            OidRequest->DATA.QUERY_INFORMATION.InformationBufferLength == 0)
        {
            ulInfoLen = sizeof(ULONG64);
        }
        else
        {
            ulInfoLen = sizeof(ULONG);
        }

        // We should always report that only 8 bytes are required to keep ndistest happy
        OidRequest->DATA.QUERY_INFORMATION.BytesNeeded =  sizeof(ULONG64);
        break;

    case OID_GEN_RCV_OK:
        ulInfo64 = Adapter->FramesRxBroadcast
            + Adapter->FramesRxMulticast
            + Adapter->FramesRxDirected;

        pInfo = &ulInfo64;

        if (OidRequest->DATA.QUERY_INFORMATION.InformationBufferLength >= sizeof(ULONG64) ||
            OidRequest->DATA.QUERY_INFORMATION.InformationBufferLength == 0)
        {
            ulInfoLen = sizeof(ULONG64);
        }
        else
        {
            ulInfoLen = sizeof(ULONG);
        }

        // We should always report that only 8 bytes are required to keep ndistest happy
        OidRequest->DATA.QUERY_INFORMATION.BytesNeeded =  sizeof(ULONG64);
        break;

    case OID_802_3_RCV_ERROR_ALIGNMENT:

        ulInfo = Adapter->RxAlignmentErrors;
        pInfo = &ulInfo;
        break;

    case OID_802_3_XMIT_ONE_COLLISION:

        ulInfo = Adapter->OneRetry;
        pInfo = &ulInfo;
        break;

    case OID_802_3_XMIT_MORE_COLLISIONS:

        ulInfo = Adapter->MoreThanOneRetry;
        pInfo = &ulInfo;
        break;

    case OID_802_3_XMIT_DEFERRED:

        ulInfo = Adapter->TxOKButDeferred;
        pInfo = &ulInfo;
        break;

    case OID_802_3_XMIT_MAX_COLLISIONS:

        ulInfo = Adapter->TxAbortExcessCollisions;
        pInfo = &ulInfo;
        break;

    case OID_802_3_RCV_OVERRUN:

        ulInfo = Adapter->RxDmaOverrunErrors;
        pInfo = &ulInfo;
        break;

    case OID_802_3_XMIT_UNDERRUN:

        ulInfo = Adapter->TxDmaUnderrun;
        pInfo = &ulInfo;
        break;

    case OID_GEN_STATISTICS:

        if (OidRequest->DATA.QUERY_INFORMATION.InformationBufferLength < sizeof(NDIS_STATISTICS_INFO))
        {
            status = NDIS_STATUS_INVALID_LENGTH;
            OidRequest->DATA.QUERY_INFORMATION.BytesNeeded = sizeof(NDIS_STATISTICS_INFO);
            break;
        }
        else
        {
            PNDIS_STATISTICS_INFO Statistics
                = (PNDIS_STATISTICS_INFO)OidRequest->DATA.QUERY_INFORMATION.InformationBuffer;

            {C_ASSERT(sizeof(NDIS_STATISTICS_INFO) >= NDIS_SIZEOF_STATISTICS_INFO_REVISION_1);}
            Statistics->Header.Type = NDIS_OBJECT_TYPE_DEFAULT;
            Statistics->Header.Size = NDIS_SIZEOF_STATISTICS_INFO_REVISION_1;
            Statistics->Header.Revision = NDIS_STATISTICS_INFO_REVISION_1;

            Statistics->SupportedStatistics = TAP_SUPPORTED_STATISTICS;

            /* Bytes in */
            Statistics->ifHCInOctets =
                Adapter->BytesRxDirected +
                Adapter->BytesRxMulticast +
                Adapter->BytesRxBroadcast;

            Statistics->ifHCInUcastOctets =
                Adapter->BytesRxDirected;

            Statistics->ifHCInMulticastOctets =
                Adapter->BytesRxMulticast;

            Statistics->ifHCInBroadcastOctets =
                Adapter->BytesRxBroadcast;

            /* Packets in */
            Statistics->ifHCInUcastPkts =
                Adapter->FramesRxDirected;

            Statistics->ifHCInMulticastPkts =
                Adapter->FramesRxMulticast;

            Statistics->ifHCInBroadcastPkts =
                Adapter->FramesRxBroadcast;

            /* Errors in */
            Statistics->ifInErrors =
                Adapter->RxCrcErrors +
                Adapter->RxAlignmentErrors +
                Adapter->RxDmaOverrunErrors +
                Adapter->RxRuntErrors;

            Statistics->ifInDiscards =
                Adapter->RxResourceErrors;


            /* Bytes out */
            Statistics->ifHCOutOctets =
                Adapter->BytesTxDirected +
                Adapter->BytesTxMulticast +
                Adapter->BytesTxBroadcast;

            Statistics->ifHCOutUcastOctets =
                Adapter->BytesTxDirected;

            Statistics->ifHCOutMulticastOctets =
                Adapter->BytesTxMulticast;

            Statistics->ifHCOutBroadcastOctets =
                Adapter->BytesTxBroadcast;

            /* Packets out */
            Statistics->ifHCOutUcastPkts =
                Adapter->FramesTxDirected;

            Statistics->ifHCOutMulticastPkts =
                Adapter->FramesTxMulticast;

            Statistics->ifHCOutBroadcastPkts =
                Adapter->FramesTxBroadcast;

            /* Errors out */
            Statistics->ifOutErrors =
                Adapter->TxAbortExcessCollisions +
                Adapter->TxDmaUnderrun +
                Adapter->TxLostCRS +
                Adapter->TxLateCollisions+
                Adapter->TransmitFailuresOther;

            Statistics->ifOutDiscards = 0ULL;

            ulInfoLen = NDIS_SIZEOF_STATISTICS_INFO_REVISION_1;
        }

        break;

        // TODO: Inplement these query information requests.
    case OID_GEN_RECEIVE_BUFFER_SPACE:
    case OID_GEN_MAXIMUM_SEND_PACKETS:
    case OID_GEN_TRANSMIT_QUEUE_LENGTH:
    case OID_802_3_XMIT_HEARTBEAT_FAILURE:
    case OID_802_3_XMIT_TIMES_CRS_LOST:
    case OID_802_3_XMIT_LATE_COLLISIONS:

    default:
        //
        // The entry point may by used by other requests
        //
        status = NDIS_STATUS_NOT_SUPPORTED;
        break;
    }

    if (status == NDIS_STATUS_SUCCESS)
    {
        ASSERT(ulInfoLen > 0);

        if (ulInfoLen <= OidRequest->DATA.QUERY_INFORMATION.InformationBufferLength)
        {
            if(pInfo)
            {
                // Copy result into InformationBuffer
                NdisMoveMemory(
                    OidRequest->DATA.QUERY_INFORMATION.InformationBuffer,
                    pInfo,
                    ulInfoLen
                    );
            }

            OidRequest->DATA.QUERY_INFORMATION.BytesWritten = ulInfoLen;
        }
        else
        {
            // too short
            OidRequest->DATA.QUERY_INFORMATION.BytesNeeded = ulInfoLen;
            status = NDIS_STATUS_BUFFER_TOO_SHORT;
        }
    }

    return status;
}

NDIS_STATUS
AdapterOidRequest(
    __in  NDIS_HANDLE             MiniportAdapterContext,
    __in  PNDIS_OID_REQUEST       OidRequest
    )
/*++

Routine Description:

    Entry point called by NDIS to get or set the value of a specified OID.

Arguments:

    MiniportAdapterContext  - Our adapter handle
    NdisRequest             - The OID request to handle

Return Value:

    Return code from the NdisRequest below.

--*/
{
    PTAP_ADAPTER_CONTEXT   adapter = (PTAP_ADAPTER_CONTEXT )MiniportAdapterContext;
    NDIS_STATUS    status;

    // Dispatch based on request type.
    switch (OidRequest->RequestType)
    {
    case NdisRequestSetInformation:
        status = tapSetInformation(adapter,OidRequest);
        break;

    case NdisRequestQueryInformation:
    case NdisRequestQueryStatistics:
        status = tapQueryInformation(adapter,OidRequest);
        break;

    case NdisRequestMethod: // TAP doesn't need to respond to this request type.
    default:
        //
        // The entry point may by used by other requests
        //
        status = NDIS_STATUS_NOT_SUPPORTED;
        break;
    }

    return status;
}

VOID
AdapterCancelOidRequest(
    __in NDIS_HANDLE              MiniportAdapterContext,
    __in PVOID                    RequestId
    )
{
    PTAP_ADAPTER_CONTEXT   adapter = (PTAP_ADAPTER_CONTEXT )MiniportAdapterContext;

    UNREFERENCED_PARAMETER(RequestId);

    //
    // This miniport sample does not pend any OID requests, so we don't have
    // to worry about cancelling them.
    //
}