summaryrefslogtreecommitdiffstats
path: root/private/ntos/tdi/st/connect.c
blob: 8cb05b03b35d1b4ddcd25a47089101887f009c4b (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
/*++

Copyright (c) 1989-1993  Microsoft Corporation

Module Name:

    connect.c

Abstract:

    This module contains code which performs the following TDI services:

        o   TdiAccept
        o   TdiListen
        o   TdiConnect
        o   TdiDisconnect
        o   TdiAssociateAddress
        o   TdiDisassociateAddress
        o   OpenConnection
        o   CloseConnection

Environment:

    Kernel mode

Revision History:

--*/

#include "st.h"


NTSTATUS
StTdiAccept(
    IN PIRP Irp
    )

/*++

Routine Description:

    This routine performs the TdiAccept request for the transport provider.

Arguments:

    Irp - Pointer to the I/O Request Packet for this request.

Return Value:

    NTSTATUS - status of operation.

--*/

{
    PTP_CONNECTION connection;
    PIO_STACK_LOCATION irpSp;
    KIRQL oldirql;
    NTSTATUS status;

    //
    // Get the connection this is associated with; if there is none, get out.
    //

    irpSp = IoGetCurrentIrpStackLocation (Irp);

    connection  = irpSp->FileObject->FsContext;

    //
    // This adds a connection reference if successful.
    //

    status = StVerifyConnectionObject (connection);

    if (!NT_SUCCESS (status)) {
        return status;
    }

    //
    // just set the connection flags to allow reads and writes to proceed.
    //

    ACQUIRE_SPIN_LOCK (&connection->SpinLock, &oldirql);

    //
    // Turn off the stopping flag for this connection.
    //

    connection->Flags &= ~CONNECTION_FLAGS_STOPPING;
    connection->Status = STATUS_PENDING;

    connection->Flags2 |= CONNECTION_FLAGS2_ACCEPTED;


    if (connection->AddressFile->ConnectIndicationInProgress) {
        connection->Flags2 |= CONNECTION_FLAGS2_INDICATING;
    }

    if ((connection->Flags2 & CONNECTION_FLAGS2_WAIT_ACCEPT) != 0) {

        //
        // We previously completed a listen, now the user is
        // coming back with an accept, Set this flag to allow
        // the connection to proceed.
        //

        connection->Flags |= CONNECTION_FLAGS_READY;

        INCREMENT_COUNTER (connection->Provider, OpenConnections);

        //
        // Set this flag to enable disconnect indications; once
        // the client has accepted he expects those.
        //

        connection->Flags2 &= ~CONNECTION_FLAGS2_WAIT_ACCEPT;
        connection->Flags2 |= CONNECTION_FLAGS2_REQ_COMPLETED;

        StReferenceConnection("Pended listen completed", connection);

        RELEASE_SPIN_LOCK (&connection->SpinLock, oldirql);

    } else {

        //
        // This accept is being called at some point before
        // the link is up; directly from the connection handler
        // or at some point slightly later.
        //

        RELEASE_SPIN_LOCK (&connection->SpinLock, oldirql);

    }

    StDereferenceConnection ("Temp TdiAccept", connection);

    return STATUS_SUCCESS;

} /* StTdiAccept */


NTSTATUS
StTdiAssociateAddress(
    IN PIRP Irp
    )

/*++

Routine Description:

    This routine performs the association of the connection and the address for
    the user.

Arguments:

    Irp - Pointer to the I/O Request Packet for this request.

Return Value:

    NTSTATUS - status of operation.

--*/

{
    NTSTATUS status;
    PFILE_OBJECT fileObject;
    PTP_ADDRESS_FILE addressFile;
    PTP_ADDRESS oldAddress;
    PTP_CONNECTION connection;
    PIO_STACK_LOCATION irpSp;
    PTDI_REQUEST_KERNEL_ASSOCIATE parameters;
    PDEVICE_CONTEXT deviceContext;

    KIRQL oldirql, oldirql2;

    irpSp = IoGetCurrentIrpStackLocation (Irp);

    //
    // verify that the operation is taking place on a connection. At the same
    // time we do this, we reference the connection. This ensures it does not
    // get removed out from under us. Note also that we do the connection
    // lookup within a try/except clause, thus protecting ourselves against
    // really bogus handles
    //

    connection  = irpSp->FileObject->FsContext;
    status = StVerifyConnectionObject (connection);
    if (!NT_SUCCESS (status)) {
        return status;
    }


    //
    // Make sure this connection is ready to be associated.
    //

    oldAddress = (PTP_ADDRESS)NULL;

    ACQUIRE_SPIN_LOCK (&connection->SpinLock, &oldirql2);

    if ((connection->Flags2 & CONNECTION_FLAGS2_ASSOCIATED) &&
        ((connection->Flags2 & CONNECTION_FLAGS2_DISASSOCIATED) == 0)) {

        //
        // The connection is already associated with
        // an active connection...bad!
        //

        RELEASE_SPIN_LOCK (&connection->SpinLock, oldirql2);
        StDereferenceConnection ("Temp Ref Associate", connection);

        return STATUS_INVALID_CONNECTION;

    } else {

        //
        // See if there is an old association hanging around...
        // this happens if the connection has been disassociated,
        // but not closed.
        //

        if (connection->Flags2 & CONNECTION_FLAGS2_DISASSOCIATED) {

            //
            // Save this; since it is non-null this address
            // will be dereferenced after the connection
            // spinlock is released.
            //

            oldAddress = connection->AddressFile->Address;

            //
            // Remove the old association.
            //

            connection->Flags2 &= ~CONNECTION_FLAGS2_ASSOCIATED;
            RemoveEntryList (&connection->AddressList);
            RemoveEntryList (&connection->AddressFileList);
            InitializeListHead (&connection->AddressList);
            InitializeListHead (&connection->AddressFileList);
            connection->AddressFile = NULL;

        }

    }

    RELEASE_SPIN_LOCK (&connection->SpinLock, oldirql2);

    //
    // If we removed an old association, dereference the
    // address.
    //

    if (oldAddress != (PTP_ADDRESS)NULL) {

        StDereferenceAddress("Removed old association", oldAddress);

    }


    deviceContext = connection->Provider;

    parameters = (PTDI_REQUEST_KERNEL_ASSOCIATE)&irpSp->Parameters;

    //
    // get a pointer to the address File Object, which points us to the
    // transport's address object, which is where we want to put the
    // connection.
    //

    status = ObReferenceObjectByHandle (
                parameters->AddressHandle,
                0L,
                0,
                KernelMode,
                (PVOID *) &fileObject,
                NULL);

    if (NT_SUCCESS(status)) {

        //
        // we might have one of our address objects; verify that.
        //

        addressFile = fileObject->FsContext;

        if (NT_SUCCESS (StVerifyAddressObject (addressFile))) {

            //
            // have an address and connection object. Add the connection to the
            // address object database. Also add the connection to the address
            // file object db (used primarily for cleaning up). Reference the
            // address to account for one more reason for it staying open.
            //

            ACQUIRE_SPIN_LOCK (&addressFile->Address->SpinLock, &oldirql);
            if ((addressFile->Address->Flags & ADDRESS_FLAGS_STOPPING) == 0) {

                ACQUIRE_SPIN_LOCK (&connection->SpinLock, &oldirql2);

                if ((connection->Flags2 & CONNECTION_FLAGS2_CLOSING) == 0) {

                    StReferenceAddress (
                        "Connection associated",
                        addressFile->Address);

                    InsertTailList (
                        &addressFile->Address->ConnectionDatabase,
                        &connection->AddressList);

                    InsertTailList (
                        &addressFile->ConnectionDatabase,
                        &connection->AddressFileList);

                    connection->AddressFile = addressFile;
                    connection->Flags2 |= CONNECTION_FLAGS2_ASSOCIATED;
                    connection->Flags2 &= ~CONNECTION_FLAGS2_DISASSOCIATED;

                    if (addressFile->ConnectIndicationInProgress) {
                        connection->Flags2 |= CONNECTION_FLAGS2_INDICATING;
                    }

                    status = STATUS_SUCCESS;

                } else {

                    //
                    // The connection is closing, stop the
                    // association.
                    //

                    status = STATUS_INVALID_CONNECTION;

                }

                RELEASE_SPIN_LOCK (&connection->SpinLock, oldirql2);

            } else {

                status = STATUS_INVALID_HANDLE;
            }

            RELEASE_SPIN_LOCK (&addressFile->Address->SpinLock, oldirql);

            StDereferenceAddress ("Temp associate", addressFile->Address);

        } else {

            status = STATUS_INVALID_HANDLE;
        }

        //
        // Note that we don't keep a reference to this file object around.
        // That's because the IO subsystem manages the object for us; we simply
        // want to keep the association. We only use this association when the
        // IO subsystem has asked us to close one of the file object, and then
        // we simply remove the association.
        //

        ObDereferenceObject (fileObject);

    } else {
        status = STATUS_INVALID_HANDLE;
    }

    StDereferenceConnection ("Temp Ref Associate", connection);

    return status;

} /* TdiAssociateAddress */


NTSTATUS
StTdiDisassociateAddress(
    IN PIRP Irp
    )
/*++

Routine Description:

    This routine performs the disassociation of the connection and the address
    for the user. If the connection has not been stopped, it will be stopped
    here.

Arguments:

    Irp - Pointer to the I/O Request Packet for this request.

Return Value:

    NTSTATUS - status of operation.

--*/

{

    KIRQL oldirql;
    PIO_STACK_LOCATION irpSp;
    PTP_CONNECTION connection;
    NTSTATUS status;

    irpSp = IoGetCurrentIrpStackLocation (Irp);

    connection  = irpSp->FileObject->FsContext;

    //
    // If successful this adds a reference.
    //

    status = StVerifyConnectionObject (connection);

    if (!NT_SUCCESS (status)) {
        return status;
    }

    ACQUIRE_SPIN_LOCK (&connection->SpinLock, &oldirql);
    if ((connection->Flags & CONNECTION_FLAGS_STOPPING) == 0) {
        RELEASE_SPIN_LOCK (&connection->SpinLock, oldirql);
        StStopConnection (connection, STATUS_LOCAL_DISCONNECT);

    } else {
        RELEASE_SPIN_LOCK (&connection->SpinLock, oldirql);
    }

    //
    // and now we disassociate the address. This only removes
    // the appropriate reference for the connection, the
    // actually disassociation will be done later.
    //
    // The DISASSOCIATED flag is used to make sure that
    // only one person removes this reference.
    //

    ACQUIRE_SPIN_LOCK (&connection->SpinLock, &oldirql);
    if ((connection->Flags2 & CONNECTION_FLAGS2_ASSOCIATED) &&
            ((connection->Flags2 & CONNECTION_FLAGS2_DISASSOCIATED) == 0)) {
        connection->Flags2 |= CONNECTION_FLAGS2_DISASSOCIATED;
        RELEASE_SPIN_LOCK (&connection->SpinLock, oldirql);
    } else {
        RELEASE_SPIN_LOCK (&connection->SpinLock, oldirql);
    }

    StDereferenceConnection ("Temp use in Associate", connection);

    return STATUS_SUCCESS;

} /* TdiDisassociateAddress */


NTSTATUS
StTdiConnect(
    IN PIRP Irp
    )

/*++

Routine Description:

    This routine performs the TdiConnect request for the transport provider.

Arguments:

    Irp - Pointer to the I/O Request Packet for this request.

Return Value:

    NTSTATUS - status of operation.

--*/

{
    NTSTATUS status;
    PTP_CONNECTION connection;
    PSTRING GeneralBroadcastSourceRoute = NULL; // BUGBUG: define this later.
    LARGE_INTEGER timeout = {0,0};
    KIRQL oldirql, cancelirql;
    PTP_REQUEST tpRequest;
    PIO_STACK_LOCATION irpSp;
    PTDI_REQUEST_KERNEL parameters;
    PTA_NETBIOS_ADDRESS RemoteAddress;
    ULONG RemoteAddressLength;

    //
    // is the file object a connection?
    //

    irpSp = IoGetCurrentIrpStackLocation (Irp);
    connection  = irpSp->FileObject->FsContext;

    //
    // If successful this adds a reference.
    //

    status = StVerifyConnectionObject (connection);

    if (!NT_SUCCESS (status)) {
        return status;
    }

    parameters = (PTDI_REQUEST_KERNEL)(&irpSp->Parameters);

    //
    // fix up the timeout if required; no connect request should take more
    // than 15 seconds if there is someone out there. We'll assume that's
    // what the user wanted if they specify -1 as the timer length.
    //

    if (parameters->RequestSpecific != NULL) {
        if ((((PLARGE_INTEGER)(parameters->RequestSpecific))->LowPart == -1) &&
             (((PLARGE_INTEGER)(parameters->RequestSpecific))->HighPart == -1)) {

            timeout.LowPart = (ULONG)(-TDI_TIMEOUT_CONNECT * 10000000L);    // n * 10 ** 7 => 100ns units
            if (timeout.LowPart != 0) {
                timeout.HighPart = -1L;
            } else {
                timeout.HighPart = 0;
            }

        } else {

            timeout.LowPart = ((PLARGE_INTEGER)(parameters->RequestSpecific))->LowPart;
            timeout.HighPart = ((PLARGE_INTEGER)(parameters->RequestSpecific))->HighPart;
        }
    }

    //
    // Check that the remote is a Netbios address.
    //

    RemoteAddress = (PTA_NETBIOS_ADDRESS)
                      (parameters->RequestConnectionInformation->RemoteAddress);

    if (RemoteAddress->Address[0].AddressType != TDI_ADDRESS_TYPE_NETBIOS) {

        StDereferenceConnection ("Not Netbios", connection);
        return STATUS_BAD_NETWORK_PATH;           // don't even try to find it.

    }

    //
    // copy the called address someplace we can use it.
    //

    RemoteAddressLength = parameters->RequestConnectionInformation->RemoteAddressLength;

    if (RemoteAddressLength > sizeof(TA_NETBIOS_ADDRESS)) {
        RemoteAddressLength = sizeof(TA_NETBIOS_ADDRESS);
    }

    RtlCopyMemory(
        connection->CalledAddress.NetbiosName,
        RemoteAddress->Address[0].Address[0].NetbiosName,
        16);

    //
    // We need a request object to keep track of this TDI request.
    // Attach this request to the new connection object.
    //

    status = StCreateRequest (
                 Irp,                           // IRP for this request.
                 connection,                    // context.
                 REQUEST_FLAGS_CONNECTION,      // partial flags.
                 NULL,
                 0,
                 timeout,
                 &tpRequest);

    if (!NT_SUCCESS (status)) {                    // couldn't make the request.
        StDereferenceConnection ("Throw away", connection);
        return status;                          // return with failure.
    } else {

        // Reference the connection since StDestroyRequest derefs it.

        StReferenceConnection("For connect request", connection);

        tpRequest->Owner = ConnectionType;
        IoAcquireCancelSpinLock (&cancelirql);
        ACQUIRE_SPIN_LOCK (&connection->SpinLock,&oldirql);
        if ((connection->Flags & CONNECTION_FLAGS_STOPPING) != 0) {
            RELEASE_SPIN_LOCK (&connection->SpinLock,oldirql);
            IoReleaseCancelSpinLock (cancelirql);
            StCompleteRequest (
                tpRequest,
                connection->Status,
                0);
            StDereferenceConnection("Temporary Use 1", connection);
            return STATUS_PENDING;
        } else {
            InsertTailList (&connection->InProgressRequest,&tpRequest->Linkage);

            connection->Flags |= CONNECTION_FLAGS_CONNECTOR;   // we're the initiator.

            connection->Flags &= ~CONNECTION_FLAGS_STOPPING;
            connection->Status = STATUS_PENDING;

            connection->Flags2 &= ~CONNECTION_FLAGS2_INDICATING;
            RELEASE_SPIN_LOCK (&connection->SpinLock,oldirql);

            //
            // Check if the IRP has been cancelled.
            //

            if (Irp->Cancel) {
                Irp->CancelIrql = cancelirql;
                StCancelConnection((PDEVICE_OBJECT)(connection->Provider), Irp);
                StDereferenceConnection ("IRP cancelled", connection);   // release lookup hold.
                return STATUS_PENDING;
            }

            Irp->CancelRoutine = StCancelConnection;
            IoReleaseCancelSpinLock(cancelirql);

        }
    }

    status = StSendConnect (
                connection);

    if (!NT_SUCCESS(status)) {                    // can't send the name request
        StStopConnection (connection, status);
        StDereferenceConnection("Temporary Use 2", connection);

        //
        // Note that this return status isn't really a lie. We are waiting
        // for the connection to run down.
        //

        return STATUS_PENDING;
    }


    StDereferenceConnection("Temporary Use 3", connection);

    return STATUS_PENDING;                      // things are started.

} /* TdiConnect */


NTSTATUS
StTdiDisconnect(
    IN PIRP Irp
    )

/*++

Routine Description:

    This routine performs the TdiDisconnect request for the transport provider.

Arguments:

    Irp - Pointer to the I/O Request Packet for this request.

Return Value:

    NTSTATUS - status of operation.

--*/

{
    PTP_CONNECTION connection;
    LARGE_INTEGER timeout;
    PIO_STACK_LOCATION irpSp;
    PTDI_REQUEST_KERNEL parameters;
    KIRQL oldirql;
    NTSTATUS status;


    irpSp = IoGetCurrentIrpStackLocation (Irp);

    connection  = irpSp->FileObject->FsContext;

    //
    // If successful this adds a reference.
    //

    status = StVerifyConnectionObject (connection);
    if (!NT_SUCCESS (status)) {
        return status;
    }


    ACQUIRE_SPIN_LOCK (&connection->SpinLock, &oldirql);

    //
    // if the connection is currently stopping, there's no reason to blow
    // it away...
    //

    if ((connection->Flags & CONNECTION_FLAGS_STOPPING) != 0) {

        RELEASE_SPIN_LOCK (&connection->SpinLock, oldirql);
        StDereferenceConnection ("Ignoring disconnect", connection);       // release our lookup reference.
        return connection->Status;

    } else {
        connection->Flags2 &= ~ (CONNECTION_FLAGS2_ACCEPTED |
                                 CONNECTION_FLAGS2_PRE_ACCEPT |
                                 CONNECTION_FLAGS2_WAIT_ACCEPT);
        connection->Flags2 |= CONNECTION_FLAGS2_DISCONNECT;

        //
        // Set this flag so the disconnect IRP is completed.
        //
        // BUGBUG: If the connection goes down before we can
        // call StStopConnection with STATUS_LOCAL_DISCONNECT,
        // the disconnect IRP won't get completed.
        //

        connection->Flags2 |= CONNECTION_FLAGS2_REQ_COMPLETED;

        connection->DisconnectIrp = Irp;
        RELEASE_SPIN_LOCK (&connection->SpinLock, oldirql);
    }

    //
    // fix up the timeout if required; no disconnect request should take very
    // long. However, the user can cause the timeout to not happen if they
    // desire that.
    //

    parameters = (PTDI_REQUEST_KERNEL)(&irpSp->Parameters);

    //
    // fix up the timeout if required; no disconnect request should take more
    // than 15 seconds. We'll assume that's what the user wanted if they
    // specify -1 as the timer.
    //

    if (parameters->RequestSpecific != NULL) {
        if ((((PLARGE_INTEGER)(parameters->RequestSpecific))->LowPart == -1) &&
             (((PLARGE_INTEGER)(parameters->RequestSpecific))->HighPart == -1)) {

            timeout.LowPart = (ULONG)(-TDI_TIMEOUT_DISCONNECT * 10000000L);    // n * 10 ** 7 => 100ns units
            if (timeout.LowPart != 0) {
                timeout.HighPart = -1L;
            } else {
                timeout.HighPart = 0;
            }

        } else {
            timeout.LowPart = ((PLARGE_INTEGER)(parameters->RequestSpecific))->LowPart;
            timeout.HighPart = ((PLARGE_INTEGER)(parameters->RequestSpecific))->HighPart;
        }
    }

    //
    // Now the reason for the disconnect
    //

    if ((ULONG)(parameters->RequestFlags) & (ULONG)TDI_DISCONNECT_RELEASE) {
        connection->Flags |= CONNECTION_FLAGS_DESTROY;
    } else if ((ULONG)(parameters->RequestFlags) & (ULONG)TDI_DISCONNECT_ABORT) {
        connection->Flags |= CONNECTION_FLAGS_ABORT;
    } else if ((ULONG)(parameters->RequestFlags) & (ULONG)TDI_DISCONNECT_WAIT) {
        connection->Flags |= CONNECTION_FLAGS_ORDREL;
    }

    //
    // This will get passed to IoCompleteRequest during TdiDestroyConnection
    //

    StStopConnection (connection, STATUS_LOCAL_DISCONNECT);              // starts the abort sequence.
    StDereferenceConnection ("Disconnecting", connection);       // release our lookup reference.

    //
    // This request will be completed by TdiDestroyConnection once
    // the connection reference count drops to 0.
    //

    return STATUS_PENDING;
} /* TdiDisconnect */


NTSTATUS
StTdiListen(
    IN PIRP Irp
    )

/*++

Routine Description:

    This routine performs the TdiListen request for the transport provider.

Arguments:

    Irp - Pointer to the I/O Request Packet for this request.

Return Value:

    NTSTATUS - status of operation.

--*/

{
    NTSTATUS status;
    PTP_CONNECTION connection;
    LARGE_INTEGER timeout = {0,0};
    KIRQL oldirql, cancelirql;
    PTP_REQUEST tpRequest;
    PIO_STACK_LOCATION irpSp;
    PTDI_REQUEST_KERNEL_LISTEN parameters;

    //
    // validate this connection

    irpSp = IoGetCurrentIrpStackLocation (Irp);

    connection  = irpSp->FileObject->FsContext;

    //
    // If successful this adds a reference.
    //

    status = StVerifyConnectionObject (connection);

    if (!NT_SUCCESS (status)) {
        return status;
    }

    parameters = (PTDI_REQUEST_KERNEL_LISTEN)&irpSp->Parameters;

    //
    // We need a request object to keep track of this TDI request.
    // Attach this request to the new connection object.
    //

    status = StCreateRequest (
                 Irp,                           // IRP for this request.
                 connection,                    // context.
                 REQUEST_FLAGS_CONNECTION,      // partial flags.
                 NULL,
                 0,
                 timeout,                       // timeout value (can be 0).
                 &tpRequest);


    if (!NT_SUCCESS (status)) {                    // couldn't make the request.

        StDereferenceConnection ("For create", connection);
        return status;                          // return with failure.
    }

    // Reference the connection since StDestroyRequest derefs it.

    IoAcquireCancelSpinLock (&cancelirql);
    ACQUIRE_SPIN_LOCK (&connection->SpinLock, &oldirql);
    tpRequest->Owner = ConnectionType;

    StReferenceConnection("For listen request", connection);

    if ((connection->Flags & CONNECTION_FLAGS_STOPPING) != 0) {

        RELEASE_SPIN_LOCK (&connection->SpinLock,oldirql);
        IoReleaseCancelSpinLock(cancelirql);

        StCompleteRequest (
            tpRequest,
            connection->Status,
            0);
        StDereferenceConnection("Temp create", connection);
        return STATUS_PENDING;

    } else {

        InsertTailList (&connection->InProgressRequest,&tpRequest->Linkage);
        connection->Flags |= CONNECTION_FLAGS_LISTENER |     // we're the passive one.
                             CONNECTION_FLAGS_WAIT_LISTEN;   // waiting for a connect
        connection->Flags2 &= ~CONNECTION_FLAGS2_INDICATING;
        connection->Flags &= ~CONNECTION_FLAGS_STOPPING;
        connection->Status = STATUS_PENDING;

        //
        // If TDI_QUERY_ACCEPT is not set, then we set PRE_ACCEPT to
        // indicate that when the listen completes we do not have to
        // wait for a TDI_ACCEPT to continue.
        //

        if ((parameters->RequestFlags & TDI_QUERY_ACCEPT) == 0) {
            connection->Flags2 |= CONNECTION_FLAGS2_PRE_ACCEPT;
        }

        RELEASE_SPIN_LOCK (&connection->SpinLock,oldirql);

        //
        // Check if the IRP has been cancelled.
        //

        if (Irp->Cancel) {
            Irp->CancelIrql = cancelirql;
            StCancelConnection((PDEVICE_OBJECT)(connection->Provider), Irp);
            StDereferenceConnection ("IRP cancelled", connection);   // release lookup hold.
            return STATUS_PENDING;
        }

        Irp->CancelRoutine = StCancelConnection;
        IoReleaseCancelSpinLock(cancelirql);

    }

    //
    // Wait for an incoming NAME_QUERY frame.  The remainder of the
    // connectionless protocol to set up a connection is processed
    // in the NAME_QUERY frame handler.
    //

    StDereferenceConnection("Temp create", connection);

    return STATUS_PENDING;                      // things are started.
} /* TdiListen */


NTSTATUS
StOpenConnection (
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp,
    IN PIO_STACK_LOCATION IrpSp
    )

/*++

Routine Description:

    This routine is called to open a connection. Note that the connection that
    is open is of little use until associated with an address; until then,
    the only thing that can be done with it is close it.

Arguments:

    DeviceObject - Pointer to the device object for this driver.

    Irp - Pointer to the request packet representing the I/O request.

    IrpSp - Pointer to current IRP stack frame.

Return Value:

    The function value is the status of the operation.

--*/

{
    PDEVICE_CONTEXT DeviceContext;
    NTSTATUS status;
    PTP_CONNECTION connection;
    PFILE_FULL_EA_INFORMATION ea;

    UNREFERENCED_PARAMETER (Irp);

    DeviceContext = (PDEVICE_CONTEXT)DeviceObject;

    //
    // First, try to make a connection object to represent this pending
    // connection.  Then fill in the relevant fields.
    // In addition to the creation, if successful StCreateConnection
    // will create a second reference which is removed once the request
    // references the connection, or if the function exits before that.

    status = StCreateConnection (DeviceContext, &connection);
    if (!NT_SUCCESS (status)) {
        return status;                          // sorry, we couldn't make one.
    }

    //
    // set the connection context so we can connect the user to this data
    // structure
    //

    ea = (PFILE_FULL_EA_INFORMATION)Irp->AssociatedIrp.SystemBuffer;
    RtlCopyMemory (
        &connection->Context,
        &ea->EaName[ea->EaNameLength+1],
        sizeof (PVOID));

    //
    // let file object point at connection and connection at file object
    //

    IrpSp->FileObject->FsContext = (PVOID)connection;
    IrpSp->FileObject->FsContext2 = (PVOID)TDI_CONNECTION_FILE;
    connection->FileObject = IrpSp->FileObject;

    return status;

} /* StOpenConnection */


NTSTATUS
StCloseConnection (
    IN PDEVICE_OBJECT DeviceObject,
    IN PIRP Irp,
    IN PIO_STACK_LOCATION IrpSp
    )

/*++

Routine Description:

    This routine is called to close a connection. There may be actions in
    progress on this connection, so we note the closing IRP, mark the
    connection as closing, and complete it somewhere down the road (when all
    references have been removed).

Arguments:

    DeviceObject - Pointer to the device object for this driver.

    Irp - Pointer to the request packet representing the I/O request.

    IrpSp - Pointer to current IRP stack frame.

Return Value:

    The function value is the status of the operation.

--*/

{
    NTSTATUS status;
    KIRQL oldirql;
    PTP_CONNECTION connection;

    UNREFERENCED_PARAMETER (DeviceObject);
    UNREFERENCED_PARAMETER (Irp);

    //
    // is the file object a connection?
    //

    connection  = IrpSp->FileObject->FsContext;


    //
    // We duplicate the code from VerifyConnectionObject,
    // although we don't actually call that since it does
    // a reference, which we don't want (to avoid bouncing
    // the reference count up from 0 if this is a dead
    // link).
    //

    try {

        if ((connection->Size == sizeof (TP_CONNECTION)) &&
            (connection->Type == ST_CONNECTION_SIGNATURE)) {

            ACQUIRE_SPIN_LOCK (&connection->SpinLock, &oldirql);

            if ((connection->Flags2 & CONNECTION_FLAGS2_CLOSING) == 0) {

                status = STATUS_SUCCESS;

            } else {

                status = STATUS_INVALID_CONNECTION;
            }

            RELEASE_SPIN_LOCK (&connection->SpinLock, oldirql);

        } else {

            status = STATUS_INVALID_CONNECTION;
        }

    } except(EXCEPTION_EXECUTE_HANDLER) {

         return GetExceptionCode();
    }

    if (!NT_SUCCESS (status)) {
        return status;
    }

    //
    // We recognize it; is it closing already?
    //

    ACQUIRE_SPIN_LOCK (&connection->SpinLock, &oldirql);

    if ((connection->Flags2 & CONNECTION_FLAGS2_CLOSING) != 0) {
        RELEASE_SPIN_LOCK (&connection->SpinLock, oldirql);
        StDereferenceConnection("Temp Close", connection);
        return STATUS_INVALID_CONNECTION;
    }

    connection->Flags2 |= CONNECTION_FLAGS2_CLOSING;

    //
    // if there is activity on the connection, tear it down.
    //

    if ((connection->Flags & CONNECTION_FLAGS_STOPPING) == 0) {
        RELEASE_SPIN_LOCK (&connection->SpinLock, oldirql);
        StStopConnection (connection, STATUS_LOCAL_DISCONNECT);
        ACQUIRE_SPIN_LOCK (&connection->SpinLock, &oldirql);
    }

    //
    // If the connection is still associated, disassociate it.
    //

    if ((connection->Flags2 & CONNECTION_FLAGS2_ASSOCIATED) &&
            ((connection->Flags2 & CONNECTION_FLAGS2_DISASSOCIATED) == 0)) {
        connection->Flags2 |= CONNECTION_FLAGS2_DISASSOCIATED;
        RELEASE_SPIN_LOCK (&connection->SpinLock, oldirql);
    } else {
        RELEASE_SPIN_LOCK (&connection->SpinLock, oldirql);
    }


    //
    // Save this to complete the IRP later.
    //

    connection->CloseIrp = Irp;

    //
    // make it impossible to use this connection from the file object
    //

    IrpSp->FileObject->FsContext = NULL;
    IrpSp->FileObject->FsContext2 = NULL;
    connection->FileObject = NULL;

    //
    // dereference for the creation. Note that this dereference
    // here won't have any effect until the regular reference count
    // hits zero.
    //

    StDereferenceConnectionSpecial (" Closing Connection", connection);

    return STATUS_PENDING;

} /* StCloseConnection */