summaryrefslogtreecommitdiffstats
path: root/private/ntos/nbt/nt/driver.c
blob: 0294ea69bd55cd1e1e0323535e1650f88739040f (plain) (blame)
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
/*++

Copyright (c) 1989-1993  Microsoft Corporation

Module Name:

    Driver.c

Abstract:

    This module implements the DRIVER_INITIALIZATION routine for the
    NBT Transport and other routines that are specific to the NT implementation
    of a driver.

Author:

    Jim Stewart (Jimst)    10-2-92

Revision History:

--*/


#include "nbtprocs.h"
#include <nbtioctl.h>

#if DBG
// allocate storage for the global debug flag NbtDebug
#ifdef _PNP_POWER
ULONG   NbtDebug=NBT_DEBUG_PNP_POWER;    // NT PNP debugging
#else // _PNP_POWER
ULONG   NbtDebug=0x00000000;             // disable all debugging
#endif // _PNP_POWER
#endif // DBG

NTSTATUS
DriverEntry(
    IN PDRIVER_OBJECT DriverObject,
    IN PUNICODE_STRING RegistryPath
    );

NTSTATUS
NbtDispatchCleanup(
    IN PDEVICE_OBJECT   Device,
    IN PIRP             irp
    );

NTSTATUS
NbtDispatchClose(
    IN PDEVICE_OBJECT   device,
    IN PIRP             irp
    );

NTSTATUS
NbtDispatchCreate(
    IN PDEVICE_OBJECT   Device,
    IN PIRP             pIrp
    );

NTSTATUS
NbtDispatchDevCtrl(
    IN PDEVICE_OBJECT   device,
    IN PIRP             irp
    );

NTSTATUS
NbtDispatchInternalCtrl(
    IN PDEVICE_OBJECT   device,
    IN PIRP             irp
    );

PFILE_FULL_EA_INFORMATION
FindInEA(
    IN PFILE_FULL_EA_INFORMATION    start,
    IN PCHAR                        wanted
    );

VOID
ReturnIrp(
    IN PIRP     irp,
    IN int      status
    );

VOID
MakePending(
    IN PIRP     pIrp
    );

#ifdef RASAUTODIAL
VOID
NbtAcdBind();
#endif // RASAUTODIAL

//*******************  Pageable Routine Declarations ****************
#ifdef ALLOC_PRAGMA
#pragma CTEMakePageable(INIT, DriverEntry)
#ifdef RASAUTODIAL
#pragma CTEMakePageable(INIT, NbtAcdBind)
#endif // RASAUTODIAL
#pragma CTEMakePageable(PAGE, NbtDispatchCleanup)
#pragma CTEMakePageable(PAGE, NbtDispatchClose)
#pragma CTEMakePageable(PAGE, NbtDispatchCreate)
#pragma CTEMakePageable(PAGE, NbtDispatchDevCtrl)
#pragma CTEMakePageable(PAGE, FindInEA)
#endif
//*******************  Pageable Routine Declarations ****************

//----------------------------------------------------------------------------
NTSTATUS
DriverEntry(
    IN PDRIVER_OBJECT DriverObject,
    IN PUNICODE_STRING RegistryPath
    )

/*++

Routine Description:

    This is the initialization routine for the NBT device driver.
    This routine creates the device object for the NBT
    device and calls a routine to perform other driver initialization.

Arguments:

    DriverObject - Pointer to driver object created by the system.

Return Value:

    NTSTATUS - The function value is the final status from the initialization
        operation.

--*/

{
    NTSTATUS            status;
    tADDRARRAY          *pAddr;
    tDEVICES            *pBindDevices=NULL;
    tDEVICES            *pExportDevices=NULL;
    tADDRARRAY          *pAddrArray=NULL;

#ifndef _PNP_LATER
    int                 i;
    PLIST_ENTRY         pHead;
    PLIST_ENTRY         pEntry;
    NTSTATUS            Locstatus;
    tDEVICECONTEXT      *pDeviceContext;
    ULONG               DevicesStarted;
#endif // _PNP_POWER

    CTEPagedCode();

#ifdef _PNP_POWER
    TdiInitialize();
#endif

    //
    // get the file system process for NBT since we need to know this for
    // allocating and freeing handles
    //
    NbtFspProcess =(PEPROCESS)PsGetCurrentProcess();

    //
    // read in registry configuration data
    //
    status = NbtReadRegistry(RegistryPath,
                             DriverObject,
                             &NbtConfig,
                             &pBindDevices,
                             &pExportDevices,
                             &pAddrArray);
    if (!NT_SUCCESS(status))
    {
        KdPrint(("NBT:Fatal Error - Failed registry read! status = %X\n",
                status));
        return(status);
    }


    //
    // Initialize NBT global data.
    //
    status = InitNotOs() ;
    if (!NT_SUCCESS(status))
    {
        NbtLogEvent(EVENT_NBT_NON_OS_INIT,status);
        KdPrint(("NBT:OS Independent initialization failed! status = %X\n",
                status));
        return(status);
    }

    //
    // Initialize the driver object with this driver's entry points.
    //
    DriverObject->MajorFunction[IRP_MJ_CREATE] =
        (PDRIVER_DISPATCH)NbtDispatchCreate;
    DriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] =
        (PDRIVER_DISPATCH)NbtDispatchDevCtrl;
    DriverObject->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] =
        (PDRIVER_DISPATCH)NbtDispatchInternalCtrl;

    DriverObject->MajorFunction[IRP_MJ_CLEANUP] =
        (PDRIVER_DISPATCH)NbtDispatchCleanup;
    DriverObject->MajorFunction[IRP_MJ_CLOSE] =
        (PDRIVER_DISPATCH)NbtDispatchClose;

    DriverObject->DriverUnload = NULL;

#ifndef _PNP_LATER

    // start some timers
    status = InitTimersNotOs();

    if (!NT_SUCCESS(status))
    {
        NbtLogEvent(EVENT_NBT_TIMERS,status);
        KdPrint(("NBT:Failed to Initialize the Timers!,status = %X\n",
                status));
        StopInitTimers();
        return(status);
    }

// #else // _PNP_POWER

    // ASSERT (status == STATUS_SUCCESS);

    NbtConfig.uDevicesStarted = 0;

#endif // _PNP_POWER

    pNbtGlobConfig->iBufferSize[eNBT_FREE_SESSION_MDLS] = sizeof(tSESSIONHDR);
    pNbtGlobConfig->iBufferSize[eNBT_DGRAM_MDLS] = DGRAM_HDR_SIZE
                                    + (pNbtGlobConfig->ScopeLength << 1);

    // create some MDLs, for session sends to speed up the sends.
    status = NbtInitMdlQ(
                        &NbtConfig.SessionMdlFreeSingleList,
                        eNBT_FREE_SESSION_MDLS);

    if (!NT_SUCCESS(status))
    {
        KdPrint(("NBT:Failed to Initialize the Session MDL Queue!,status = %X\n",
                status));
        return(status);
    }
    // create some MDLs for datagram sends
    status = NbtInitMdlQ(
                        &NbtConfig.DgramMdlFreeSingleList,
                        eNBT_DGRAM_MDLS);

    if (!NT_SUCCESS(status))
    {
        KdPrint(("NBT:Failed to Initialize the Dgram MDL Queue!,status = %X\n",
                status));
        return(status);
    }

#ifndef _PNP_LATER

    //
    // Create the NBT device object for each adapter configured
    //
    pAddr = pAddrArray;

    DevicesStarted = 0;

    for (i=0; i<pNbtGlobConfig->uNumDevices; i++ )
    {

        // this call ultimately allocates storage for the returned NameString
        // that holds the Ipaddress
        status = NbtCreateDeviceObject(
                    DriverObject,
                    pNbtGlobConfig,
                    &pBindDevices->Names[i],
                    &pExportDevices->Names[i],
                    pAddr,
                    RegistryPath,
#ifndef _IO_DELETE_DEVICE_SUPPORTED
                    FALSE,
#endif
                    &pDeviceContext);

        // for a Bnode there are no Wins server addresses, so this Ptr can
        // be null.
        if (pAddr)
        {
            pAddr++;
        }

        //
        // allow not having an address to succeed - DHCP will
        // provide an address later
        //
        if (pDeviceContext != NULL)
        {
            if (status == STATUS_INVALID_ADDRESS)
            {
                //
                // set to null so we know not to allow connections or dgram
                // sends on this adapter
                //
                pDeviceContext->IpAddress = 0;

                DevicesStarted++;

                status = STATUS_SUCCESS;

            }


            if ((NT_SUCCESS(status)) ||
                (status == STATUS_INVALID_ADDRESS_COMPONENT)) {

                status = STATUS_SUCCESS;
                NbtConfig.uDevicesStarted++;
            }


            {
                //
                // We can tolerate the invalid address component failure since IP does not know of
                // static addresses at this time.
                //
                if (!NT_SUCCESS(status))
                {

                    pDeviceContext->RegistrationHandle = NULL;

                    KdPrint((" Create Device Object Failed with status= %X, num devices = %X\n",status,
                                            NbtConfig.uNumDevices));

                    NbtLogEvent(EVENT_NBT_CREATE_DEVICE,status);
                    //
                    // this device will not be started so decrement the count of started
                    // ones.
                    //
                    NbtConfig.AdapterCount--;

                    //
                    // cleanup the mess and free the device object since we had some
                    // sort of failure.
                    //
                    pHead = &NbtConfig.DeviceContexts;
                    pEntry = RemoveTailList(pHead);

                    ASSERT (pDeviceContext == CONTAINING_RECORD(pEntry,tDEVICECONTEXT,Linkage));

                    if (pDeviceContext->hNameServer)
                    {
                        ObDereferenceObject(pDeviceContext->pNameServerFileObject);
                        Locstatus =  NTZwCloseFile(pDeviceContext->hNameServer);
                        KdPrint(("Close NameSrv File status = %X\n",Locstatus));
                    }
                    if (pDeviceContext->hDgram)
                    {
                        ObDereferenceObject(pDeviceContext->pDgramFileObject);
                        Locstatus = NTZwCloseFile(pDeviceContext->hDgram);
                        KdPrint(("Close Dgram File status = %X\n",Locstatus));
                    }
                    if (pDeviceContext->hSession)
                    {
                        ObDereferenceObject(pDeviceContext->pSessionFileObject);
                        Locstatus = NTZwCloseFile(pDeviceContext->hSession);
                        KdPrint(("Close Session File status = %X\n",Locstatus));
                    }
                    if (pDeviceContext->hControl)
                    {
                        ObDereferenceObject(pDeviceContext->pControlFileObject);
                        Locstatus = NTZwCloseFile(pDeviceContext->hControl);
                        KdPrint(("Close Control File status = %X\n",Locstatus));
                    }

                    IoDeleteDevice((PDEVICE_OBJECT)pDeviceContext);

                }
                else
                {
                    //
                    // So we know that we need to register this device when an IP addres
                    // appears
                    //
                    pDeviceContext->RegistrationHandle = NULL;
                    DevicesStarted++;
                    pDeviceContext->DeviceObject.Flags &= ~DO_DEVICE_INITIALIZING;
                }
            }
        }

    }
    //
    // if no devices were created, then stop the timers and free the resources
    //
    if (DevicesStarted == 0)
    {
        ExDeleteResource(&NbtConfig.Resource);
        StopInitTimers();
    }
    else
    {
        //
        // at least one device context was created successfully, so return success
        //
        status = STATUS_SUCCESS;
    }

    if (NbtConfig.uNumDevices == 0)
    {
        NbtLogEvent(EVENT_NBT_NO_DEVICES,0);
    }

#endif // _PNP_POWER

    if (pBindDevices)
    {
        CTEMemFree((PVOID)pBindDevices->RegistrySpace);
        CTEMemFree((PVOID)pBindDevices);
    }
    if (pExportDevices)
    {
        CTEMemFree((PVOID)pExportDevices->RegistrySpace);
        CTEMemFree((PVOID)pExportDevices);
    }
    if (pAddrArray)
    {
        CTEMemFree((PVOID)pAddrArray);
    }

#ifndef _PNP_LATER

    //
    // Get an Irp for the out of resource queue (used to disconnect sessions
    // when really low on memory)
    //
    if (!IsListEmpty(&NbtConfig.DeviceContexts))
    {
        pEntry = NbtConfig.DeviceContexts.Flink;
        pDeviceContext = CONTAINING_RECORD(pEntry,tDEVICECONTEXT,Linkage);

        NbtConfig.OutOfRsrc.pIrp = NTAllocateNbtIrp(&pDeviceContext->DeviceObject);

        if (!NbtConfig.OutOfRsrc.pIrp)
        {
            status = STATUS_INSUFFICIENT_RESOURCES;
        }
        else
        {
            //
            // allocate a dpc structure and keep it: we might need if we hit an
            // out-of-resource condition
            //
            NbtConfig.OutOfRsrc.pDpc = NbtAllocMem(sizeof(KDPC),NBT_TAG('a'));
            if (!NbtConfig.OutOfRsrc.pDpc)
            {
                IoFreeIrp(NbtConfig.OutOfRsrc.pIrp);
                status = STATUS_INSUFFICIENT_RESOURCES;
            }
        }

#ifdef RASAUTODIAL
        //
        // Get the automatic connection driver
        // entry points.
        //
        if (status == STATUS_SUCCESS)
        {
            NbtAcdBind();
        }
#endif

#ifdef _PNP_POWER

        (void)TdiRegisterAddressChangeHandler(
                        AddressArrival,
                        AddressDeletion,
                        &AddressChangeHandle
                            );

#ifdef WATCHBIND
        (void)TdiRegisterNotificationHandler(
                        BindHandler,
                        UnbindHandler,
                        &BindingHandle
                        );
#endif // WATCHBIND

#endif // _PNP_POWER
    }
    else
    {
        KdPrint(("NetBT!DriverEntry: Huh?  Started NetBT with no devices!"));
    }

#endif // _PNP_POWER

    NbtConfig.InterfaceIndex = 0;

    //
    // Return to the caller.
    //

    return(status);
}

//----------------------------------------------------------------------------
NTSTATUS
NbtDispatchCleanup(
    IN PDEVICE_OBJECT   Device,
    IN PIRP             irp
    )

/*++

Routine Description:

    This is the NBT driver's dispatch function for IRP_MJ_CLEANUP
    requests.

    This function is called when the last reference to the handle is closed.
    Hence, an NtClose() results in an IRP_MJ_CLEANUP first, and then an
    IRP_MJ_CLOSE.  This function runs down all activity on the object, and
    when the close comes in the object is actually deleted.

Arguments:

    device    - ptr to device object for target device
    irp       - ptr to I/O request packet

Return Value:

    STATUS_SUCCESS

--*/

{
    NTSTATUS            status;
    PIO_STACK_LOCATION  irpsp;
    tDEVICECONTEXT   *pDeviceContext;

    CTEPagedCode();
    pDeviceContext = (tDEVICECONTEXT *)Device;

    irpsp = IoGetCurrentIrpStackLocation(irp);

    // check that we got the correct major function code
    ASSERT(irpsp->MajorFunction == IRP_MJ_CLEANUP);

    // look at the context value that NBT put into the FSContext2 value to
    // decide what to do
    switch ((USHORT)irpsp->FileObject->FsContext2)
    {
        case NBT_ADDRESS_TYPE:
            // the client is closing the address file, so we must cleanup
            // and memory blocks associated with it.
            status = NTCleanUpAddress(pDeviceContext,irp);
            break;

        case NBT_CONNECTION_TYPE:
            // the client is closing a connection, so we must clean up any
            // memory blocks associated with it.
            status = NTCleanUpConnection(pDeviceContext,irp);
            break;

        case NBT_CONTROL_TYPE:
            // there is nothing to do here....
            status = STATUS_SUCCESS;
            break;

        default:
            /*
             * complete the i/o successfully.
             */
            status = STATUS_SUCCESS;
            break;
        }

    //
    // Complete the Irp
    //
    ReturnIrp(irp, status);
    return(status);


} // DispatchCleanup


//----------------------------------------------------------------------------
NTSTATUS
NbtDispatchClose(
    IN PDEVICE_OBJECT   Device,
    IN PIRP             pIrp
    )

/*++

Routine Description:

    This is the NBT driver's dispatch function for IRP_MJ_CLOSE
    requests.  This is called after Cleanup (above) is called.

Arguments:

    device  - ptr to device object for target device
    pIrp     - ptr to I/O request packet

Return Value:

    an NT status code.

--*/

{
    NTSTATUS status;
    PIO_STACK_LOCATION irpsp;
    tDEVICECONTEXT   *pDeviceContext;

    CTEPagedCode();
    pDeviceContext = (tDEVICECONTEXT *)Device;

    //
    // close operations are synchronous.
    //
    pIrp->IoStatus.Status      = STATUS_SUCCESS;
    pIrp->IoStatus.Information = 0;

    irpsp = IoGetCurrentIrpStackLocation(pIrp);
    ASSERT(irpsp->MajorFunction == IRP_MJ_CLOSE);

    switch ((ULONG)irpsp->FileObject->FsContext2)
        {
        case NBT_ADDRESS_TYPE:
            status = NTCloseAddress(pDeviceContext,pIrp);
            break;

        case NBT_CONNECTION_TYPE:
            status = NTCloseConnection(pDeviceContext,pIrp);
            break;

        case NBT_WINS_TYPE:
            status = NTCloseWinsAddr(pDeviceContext,pIrp);
            break;

        case NBT_CONTROL_TYPE:
            // the client is closing the Control Object...
            // there is nothing to do here....
            status = STATUS_SUCCESS;
            break;

        default:
            KdPrint(("Nbt:Close Received for unknown object type = %X\n",
                                         irpsp->FileObject->FsContext2));
            status = STATUS_SUCCESS;
            break;
        }

    // NTCloseAddress can return Pending until the ref count actually gets
    // to zero.
    //
    if (status != STATUS_PENDING)
    {
        ReturnIrp(pIrp, status);
    }

    return(status);

} // DispatchClose


//----------------------------------------------------------------------------
NTSTATUS
NbtDispatchCreate(
    IN PDEVICE_OBJECT   Device,
    IN PIRP             pIrp
    )

/*++

Routine Description:

    This is the NBT driver's dispatch function for IRP_MJ_CREATE
    requests.  It is called as a consequence of one of the following:

        a. TdiOpenConnection("\Device\Nbt_Elnkii0"),
        b. TdiOpenAddress("\Device\Nbt_Elnkii0"),

Arguments:

    Device - ptr to device object being opened
    pIrp    - ptr to I/O request packet
    pIrp->Status => return status
    pIrp->MajorFunction => IRP_MD_CREATE
    pIrp->MinorFunction => not used
    pIpr->FileObject    => ptr to file obj created by I/O system. NBT fills in FsContext
    pIrp->AssociatedIrp.SystemBuffer => ptr to EA buffer with address of obj to open(Netbios Name)
    pIrp->Parameters.Create.EaLength => length of buffer specifying the Xport Addr.

Return Value:

    STATUS_SUCCESS or STATUS_PENDING

--*/

{
    NTSTATUS                    status;
    PIO_STACK_LOCATION          pIrpsp;
    PFILE_FULL_EA_INFORMATION   ea;
    tDEVICECONTEXT              *pDeviceContext;
    UCHAR                       IrpFlags;

    CTEPagedCode();
    pDeviceContext = (tDEVICECONTEXT *)Device;

    //
    // If this device was destroyed, then reject all opens on it.
    // Ideally we would like the IO sub-system to guarantee that no
    // requests come down on IoDeleted devices, but.....
    //
    if (InterlockedExchangeAdd(&pDeviceContext->IsDestroyed, 0) != 0) {
        // IF_DBG(NBT_DEBUG_DRIVER)
            KdPrint(("Nbt Rejecting Create minor Func\n"));

        pIrp->IoStatus.Status = STATUS_INVALID_DEVICE_STATE;
        IoCompleteRequest (pIrp, IO_NETWORK_INCREMENT);
        return STATUS_INVALID_DEVICE_STATE;
    }

    pIrpsp = IoGetCurrentIrpStackLocation(pIrp);
    ASSERT(pIrpsp->MajorFunction == IRP_MJ_CREATE);
    IrpFlags = pIrpsp->Control;

    //
    // set the pending flag here so that it is sure to be set BEFORE the
    // completion routine gets hit.
    //
    pIrp->IoStatus.Information = 0;
    pIrp->IoStatus.Status = STATUS_PENDING;
    IoMarkIrpPending(pIrp);

    IF_DBG(NBT_DEBUG_DRIVER)
        KdPrint(("Nbt Internal Ctrl minor Func = %X\n",pIrpsp->MinorFunction));

    /*
     * was this a TdiOpenConnection() or TdiOpenAddress()?
     * Get the Extended Attribute pointer and look at the text
     * value passed in for a match with "TransportAddress" or
     * "ConnectionContext" (in FindEa)
     */
    ea = (PFILE_FULL_EA_INFORMATION) pIrp->AssociatedIrp.SystemBuffer;

    if (!ea)
    {
        // a null ea means open the control object
        status = NTOpenControl(pDeviceContext,pIrp);
    }
    else
    if (FindInEA(ea, TdiConnectionContext))
    {
        // not allowed to pass in both a Connect Request and a Transport Address
        ASSERT(!FindInEA(ea, TdiTransportAddress));
        status = NTOpenConnection(pDeviceContext,pIrp);
    }
    else
    if (FindInEA(ea, TdiTransportAddress))
    {
        status = NTOpenAddr(pDeviceContext,pIrp);
    }
    else
    if (FindInEA(ea, WINS_INTERFACE_NAME))
    {
        status = NTOpenWinsAddr(pDeviceContext,pIrp);
    }
    else
    {
        status = STATUS_INVALID_EA_NAME;
        pIrpsp->Control = IrpFlags;
        ReturnIrp(pIrp, status);
        return(status);
    }

    // complete the irp if the status is anything EXCEPT status_pending
    // since the name query completion routine NTCompletIO completes pending
    // open addresses

    if (status != STATUS_PENDING)
    {

#if DBG
        // *TODO* for debug...
        if (!NT_SUCCESS(status))
        {
            IF_DBG(NBT_DEBUG_NAMESRV)
            KdPrint(("Nbt: error return status = %X\n",status));
            //ASSERTMSG("An error Status reported from NBT",0L);
        }
#endif

        // reset the pending returned bit, since we are NOT returning pending
        pIrpsp->Control = IrpFlags;

        ReturnIrp(pIrp,status);

    }


    return(status);



}


//----------------------------------------------------------------------------
NTSTATUS
NbtDispatchDevCtrl(
    IN PDEVICE_OBJECT   Device,
    IN PIRP             irp
    )

/*++

Routine Description:

    This is the NBT driver's dispatch function for all
    IRP_MJ_DEVICE_CONTROL requests.

Arguments:

    device - ptr to device object for target device
    irp    - ptr to I/O request packet

Return Value:

    NTSTATUS -- Indicates whether the request was successfully queued.

--*/

{
    NTSTATUS            status;
    PIO_STACK_LOCATION  irpsp;
    tDEVICECONTEXT   *pDeviceContext;

    CTEPagedCode();
    pDeviceContext = (tDEVICECONTEXT *)Device;

    irpsp = IoGetCurrentIrpStackLocation(irp);

    //
    // If this device was destroyed, then reject all requests on it.
    // Ideally we would like the IO sub-system to guarantee that no
    // requests come down on IoDeleted devices, but.....
    //
    if (InterlockedExchangeAdd(&pDeviceContext->IsDestroyed, 0) != 0) {
        // IF_DBG(NBT_DEBUG_DRIVER)
            KdPrint(("Nbt Rejecting Dev Ctrl code = %X\n",irpsp->Parameters.DeviceIoControl.IoControlCode));
        irp->IoStatus.Status = STATUS_INVALID_DEVICE_STATE;
        IoCompleteRequest (irp, IO_NETWORK_INCREMENT);
        return STATUS_INVALID_DEVICE_STATE;
    }

    /*
     * Initialize the I/O status block.
     */
    irp->IoStatus.Status      = STATUS_PENDING;
    irp->IoStatus.Information = 0;

    ASSERT(irpsp->MajorFunction == IRP_MJ_DEVICE_CONTROL);

    IF_DBG(NBT_DEBUG_DRIVER)
    KdPrint(("Nbt:DevCtrl hit with ControlCode == %X\n",
            irpsp->Parameters.DeviceIoControl.IoControlCode));

    if ((irpsp->Parameters.DeviceIoControl.IoControlCode >= IOCTL_NETBT_PURGE_CACHE) &&
        (irpsp->Parameters.DeviceIoControl.IoControlCode <IOCTL_NETBT_LAST_IOCTL))
    {
        return(DispatchIoctls((tDEVICECONTEXT *)Device,irp, irpsp));
    }
    else
    {
        /*
         * if possible, convert the (external) device control into internal
         * format, then treat it as if it had arrived that way.
         */
        status = TdiMapUserRequest(Device, irp, irpsp);

        if (status == STATUS_SUCCESS)
        {
            return(NbtDispatchInternalCtrl(Device, irp));
        }
    }

    ReturnIrp(irp, STATUS_INVALID_DEVICE_REQUEST);
    return(STATUS_INVALID_DEVICE_REQUEST);

} // NbtDispatchDevCtrl


//----------------------------------------------------------------------------
NTSTATUS
NbtDispatchInternalCtrl(
    IN PDEVICE_OBJECT   Device,
    IN PIRP             pIrp
    )

/*++

Routine Description:

    This is the driver's dispatch function for all
    IRP_MJ_INTERNAL_DEVICE_CONTROL requests.

Arguments:

    device - ptr to device object for target device
    irp    - ptr to I/O request packet

Return Value:

    NTSTATUS -- Indicates whether the request was successfully queued.

--*/

{
    tDEVICECONTEXT      *pDeviceContext;
    PIO_STACK_LOCATION  pIrpsp;
    NTSTATUS            status;
    UCHAR               IrpFlags;

    pDeviceContext = (tDEVICECONTEXT *)Device;

    pIrpsp = IoGetCurrentIrpStackLocation(pIrp);

    //
    // If this device was destroyed, then reject all operations on it.
    // Ideally we would like the IO sub-system to guarantee that no
    // requests come down on IoDeleted devices, but.....
    //
    if (InterlockedExchangeAdd(&pDeviceContext->IsDestroyed, 0) != 0) {
        // IF_DBG(NBT_DEBUG_DRIVER)
            KdPrint(("Nbt Rejecting Internal minor fn. = %X\n",pIrpsp->MinorFunction));
        pIrp->IoStatus.Status = STATUS_INVALID_DEVICE_STATE;
        IoCompleteRequest (pIrp, IO_NETWORK_INCREMENT);
        return STATUS_INVALID_DEVICE_STATE;
    }

    /*
     * Initialize the I/O status block.
     */

    //
    // this check if first to optimize the Send path
    //
    if (pIrpsp->MinorFunction ==TDI_SEND)
    {
        //
        // this routine decides if it should complete the irp or not
        // It never returns status pending, so we can turn off the
        // pending bit
        //
        return( NTSend(pDeviceContext,pIrp) );

    }

    IrpFlags = pIrpsp->Control;

    ASSERT(pIrpsp->MajorFunction == IRP_MJ_INTERNAL_DEVICE_CONTROL);

    IF_DBG(NBT_DEBUG_DRIVER)
        KdPrint(("Nbt Internal Ctrl minor Func = %X\n",pIrpsp->MinorFunction));

    switch (pIrpsp->MinorFunction)
    {
        case TDI_ACCEPT:
            MakePending(pIrp);
            status = NTAccept(pDeviceContext,pIrp);
            break;

        case TDI_ASSOCIATE_ADDRESS:
            MakePending(pIrp);
            status = NTAssocAddress(pDeviceContext,pIrp);
            break;

        case TDI_DISASSOCIATE_ADDRESS:
            MakePending(pIrp);
            status = NTDisAssociateAddress(pDeviceContext,pIrp);
            break;

        case TDI_CONNECT:
            MakePending(pIrp);
            status = NTConnect(pDeviceContext,pIrp);
            break;

        case TDI_DISCONNECT:
            MakePending(pIrp);
            status = NTDisconnect(pDeviceContext,pIrp);
            break;

        case TDI_LISTEN:
            status = NTListen(pDeviceContext,pIrp);
            return(status);
            break;

        case TDI_QUERY_INFORMATION:
            status = NTQueryInformation(pDeviceContext,pIrp);
#if DBG
            if (!NT_SUCCESS(status))
            {
                IF_DBG(NBT_DEBUG_NAMESRV)
                KdPrint(("Nbt: Bad status from Query Info = %X\n",status));
            }
#endif
            return(status);
            break;

        case TDI_RECEIVE:
            status = NTReceive(pDeviceContext,pIrp);
            return(status);

            break;

        case TDI_RECEIVE_DATAGRAM:
            status = NTReceiveDatagram(pDeviceContext,pIrp);
            return(status);
            break;


    case TDI_SEND_DATAGRAM:

            status = NTSendDatagram(pDeviceContext,pIrp);
#if DBG
            if (!NT_SUCCESS(status))
            {
                IF_DBG(NBT_DEBUG_NAMESRV)
                KdPrint(("Nbt: Bad status from Dgram Send = %X\n",status));
            }
#endif
            return(status);
            break;

        case TDI_SET_EVENT_HANDLER:
            MakePending(pIrp);
            status = NTSetEventHandler(pDeviceContext,pIrp);
            break;

        case TDI_SET_INFORMATION:
            MakePending(pIrp);
            status = NTSetInformation(pDeviceContext,pIrp);
            break;

    #if DBG
        //
        // 0x7f is a request by the redirector to put a "magic bullet" out on
        // the wire, to trigger the Network General Sniffer.
        //
        case 0x7f:
            KdPrint(("NBT:DispatchInternalCtrl - 07f minor function code\n"));
            ReturnIrp(pIrp, STATUS_NOT_SUPPORTED);
            return(STATUS_NOT_SUPPORTED);

    #endif /* DBG */

        default:
            KdPrint(("NBT:Dispatch Internal Ctl - invalid minor function %X\n",
                            pIrpsp->MinorFunction));
            ReturnIrp(pIrp, STATUS_INVALID_DEVICE_REQUEST);
            return(STATUS_INVALID_DEVICE_REQUEST);
    }

    // if the returned status is pending, then we do not complete the IRP
    // here since it will be completed elsewhere in the code...
    //
    if (status != STATUS_PENDING)
    {
#if DBG
        // *TODO* for debug...
        if (!NT_SUCCESS(status))
        {
            IF_DBG(NBT_DEBUG_NAMESRV)
            KdPrint(("NBT:error return status = %X,MinorFunc = %X\n",status,pIrpsp->MinorFunction));
//            ASSERTMSG("An error Status reported from NBT",0L);
        }

#endif
        pIrpsp->Control = IrpFlags;

        ReturnIrp(pIrp,status);

    }

    return(status);


} // NbtDispatchInternalCtrl


//----------------------------------------------------------------------------
PFILE_FULL_EA_INFORMATION
FindInEA(
    IN PFILE_FULL_EA_INFORMATION    start,
    IN PCHAR                        wanted
    )

/*++

Routine Description:

    This function check for the "Wanted" string in the Ea structure and
    returns a pointer to the extended attribute structure
    representing the given extended attribute name.

Arguments:

    device - ptr to device object for target device
    pIrp    - ptr to I/O request packet

Return Value:

    pointer to the extended attribute structure, or NULL if not found.

--*/

{
    PFILE_FULL_EA_INFORMATION eabuf;

    CTEPagedCode();

    for (eabuf = start; eabuf; eabuf += eabuf->NextEntryOffset)
    {

        if (strncmp(eabuf->EaName,wanted,eabuf->EaNameLength) == 0)
        {
           return eabuf;
        }

        if (eabuf->NextEntryOffset == 0)
        {
            return((PFILE_FULL_EA_INFORMATION) NULL);
        }

    }
    return((PFILE_FULL_EA_INFORMATION) NULL);

} // FindEA



//----------------------------------------------------------------------------
VOID
ReturnIrp(
    IN PIRP     pIrp,
    IN int      status
    )

/*++

Routine Description:

    This function completes an IRP, and arranges for return parameters,
    if any, to be copied.

    Although somewhat a misnomer, this function is named after a similar
    function in the SpiderSTREAMS emulator.

Arguments:

    pIrp     -  pointer to the IRP to complete
    status  -  completion status of the IRP

Return Value:

    number of bytes copied back to the user.

--*/

{
    KIRQL oldlevel;
    CCHAR priboost;

    //
    // pIrp->IoStatus.Information is meaningful only for STATUS_SUCCESS
    //

    // set the Irps cancel routine to null or the system may bugcheck
    // with a bug code of CANCEL_STATE_IN_COMPLETED_IRP
    //
    // refer to IoCancelIrp()  ..\ntos\io\iosubs.c
    //
    IoAcquireCancelSpinLock(&oldlevel);
    IoSetCancelRoutine(pIrp,NULL);
    IoReleaseCancelSpinLock(oldlevel);

    pIrp->IoStatus.Status      = status;

    priboost = (CCHAR) ((status == STATUS_SUCCESS) ?
                        IO_NETWORK_INCREMENT : IO_NO_INCREMENT);

    IoCompleteRequest(pIrp, priboost);

    return;

}
//----------------------------------------------------------------------------
VOID
MakePending(
    IN PIRP     pIrp
    )

/*++

Routine Description:

    This function marks an irp pending and sets the correct status.

Arguments:

    pIrp     -  pointer to the IRP to complete
    status  -  completion status of the IRP

Return Value:


--*/

{
    IoMarkIrpPending(pIrp);
    pIrp->IoStatus.Status = STATUS_PENDING;
    pIrp->IoStatus.Information = 0;

}