summaryrefslogtreecommitdiffstats
path: root/private/ntos/tdi/loopback/datagram.c
blob: 0c5ec7c02480ba6f83083c44185199c7dd713051 (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
/*++

Copyright (c) 1989  Microsoft Corporation

Module Name:

    datagram.c

Abstract:

    This module implements datagram logic for the loopback Transport
    Provider driver for NT LAN Manager.

Author:

    Chuck Lenzmeier (chuckl)    15-Aug-1991

Revision History:

--*/

#include "loopback.h"

//
// Local declarations
//

STATIC
VOID
CompleteReceiveDatagram (
    IN PIRP ReceiveIrp,
    IN PIRP SendIrp
    );

STATIC
VOID
IndicateReceiveDatagram (
    IN PLOOP_CONNECTION ReceivingConnection,
    IN PIRP InitialSendIrp
    );


NTSTATUS
LoopReceiveDatagram (
    IN PIRP Irp,
    IN PIO_STACK_LOCATION IrpSp
    )

/*++

Routine Description:

    This routine processes a Receive Datagram request.

Arguments:

    Irp - Pointer to I/O request packet

    IrpSp - Pointer to current stack location in IRP

Return Value:

    NTSTATUS - Status of request

--*/

{
    PLOOP_ENDPOINT receivingEndpoint;
    PLOOP_CONNECTION sendingConnection;

    IF_DEBUG(LOOP1) DbgPrint( "  Receive Datagram request\n" );

    //
    // Verify that the receiving endpoint is connected.
    //

    receivingEndpoint = (PLOOP_ENDPOINT)IrpSp->FileObject->FsContext;

    if ( receivingConnection == NULL ) {
        IF_DEBUG(LOOP2) DbgPrint( "    Can't Receive on control channel\n" );
        Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
        IoCompleteRequest( Irp, 0 );
        IF_DEBUG(LOOP1) DbgPrint( "  Receive request complete\n" );
        return STATUS_INVALID_PARAMETER;
    }

    sendingConnection = receivingConnection->RemoteConnection;
    IF_DEBUG(LOOP2) {
        DbgPrint( "    Receiving connection: %lx\n", receivingConnection );
        DbgPrint( "    Sending connection: %lx\n", sendingConnection );
    }

    ACQUIRE_LOOP_LOCK( "Receive initial" );

    if ( (sendingConnection == NULL) ||
         (GET_BLOCK_STATE(receivingConnection) != BlockStateActive) ) {
        RELEASE_LOOP_LOCK( "Receive closing" );
        IF_DEBUG(LOOP2) DbgPrint( "    Connection not connected\n" );
        Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
        IoCompleteRequest( Irp, 0 );
        IF_DEBUG(LOOP1) DbgPrint( "  Receive request complete\n" );
        return STATUS_DISCONNECTED;
    }

    //
    // Queue the Receive to the connection's Pending Receive list.
    //

    InsertTailList(
        &receivingConnection->PendingReceiveList,
        &Irp->Tail.Overlay.ListEntry
        );

    IoMarkIrpPending( Irp );

    //
    // Check for pending data.
    //

    if ( (receivingConnection->IndicatingSendIrp != NULL) ||
         (receivingConnection->IncomingSendList.Flink ==
                        &receivingConnection->IncomingSendList) ) {

        //
        // There is no pending Send, or an indication is already in
        // progress.  Reference the connection to account for the
        // Receive IRP.
        //

        IF_DEBUG(LOOP2) {
            DbgPrint( "    No pending Send; leaving IRP %lx queued \n", Irp );
        }
        receivingConnection->BlockHeader.ReferenceCount++;
        IF_DEBUG(LOOP3) {
            DbgPrint( "      New refcnt on connection %lx is %lx\n",
                        receivingConnection,
                        receivingConnection->BlockHeader.ReferenceCount );
        }

        RELEASE_LOOP_LOCK( "Receive no Send" );

    } else {

        //
        // There is pending data.  Call IndicateReceive to satisfy
        // the Receive.
        //
        // *** Note that IndicateReceive returns with the loopback
        //     driver spin lock released.
        //

        IF_DEBUG(LOOP2) DbgPrint( "    LoopReceive indicating receive\n" );
        IndicateReceive( receivingConnection, NULL );

    }

    IF_DEBUG(LOOP1) DbgPrint( "  Receive request %lx complete\n", Irp );
    return STATUS_PENDING;

} // LoopReceive


NTSTATUS
LoopSend (
    IN PIRP Irp,
    IN PIO_STACK_LOCATION IrpSp
    )

/*++

Routine Description:

    This routine processes a Send request.

Arguments:

    Irp - Pointer to I/O request packet

    IrpSp - Pointer to current stack location in IRP

Return Value:

    NTSTATUS - Status of request

--*/

{
    PLOOP_CONNECTION sendingConnection;
    PLOOP_CONNECTION receivingConnection;
    BOOLEAN firstSend;

    IF_DEBUG(LOOP1) DbgPrint( "  Send request\n" );

    //
    // Verify that the sending connection is connected.
    //

    sendingConnection = (PLOOP_CONNECTION)IrpSp->FileObject->FsContext;

    if ( sendingConnection == NULL ) {
        IF_DEBUG(LOOP2) DbgPrint( "    Can't Send on control channel\n" );
        Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
        IoCompleteRequest( Irp, 0 );
        IF_DEBUG(LOOP1) DbgPrint( "  Send request complete\n" );
        return STATUS_INVALID_PARAMETER;
    }

    receivingConnection = sendingConnection->RemoteConnection;
    IF_DEBUG(LOOP2) {
        DbgPrint( "    Sending connection: %lx\n", sendingConnection );
        DbgPrint( "    Receiving connection: %lx\n", receivingConnection );
    }

    ACQUIRE_LOOP_LOCK( "Send initial" );

    if ( (receivingConnection == NULL) ||
         (GET_BLOCK_STATE(sendingConnection) != BlockStateActive) ) {
        RELEASE_LOOP_LOCK( "Send closing" );
        IF_DEBUG(LOOP2) DbgPrint( "    Connection not connected\n" );
        Irp->IoStatus.Status = STATUS_INVALID_PARAMETER;
        IoCompleteRequest( Irp, 0 );
        IF_DEBUG(LOOP1) DbgPrint( "  Send request complete\n" );
        return STATUS_DISCONNECTED;
    }

    //
    // Reference both ends.
    //

    sendingConnection->BlockHeader.ReferenceCount++;
    receivingConnection->BlockHeader.ReferenceCount++;
    IF_DEBUG(LOOP3) {
        DbgPrint( "      New refcnt on connection %lx is %lx\n",
                    sendingConnection,
                    sendingConnection->BlockHeader.ReferenceCount );
        DbgPrint( "      New refcnt on connection %lx is %lx\n",
                    receivingConnection,
                    receivingConnection->BlockHeader.ReferenceCount );
    }

    //
    // Determine whether this is the first active incoming Send for the
    // receiving connection.
    //

    firstSend = (BOOLEAN)( receivingConnection->IncomingSendList.Flink ==
                            &receivingConnection->IncomingSendList );

    //
    // Queue the Send to the receiving connection's Incoming Send list,
    // in order to prevent another Send from getting ahead of this one.
    //

    InsertTailList(
        &receivingConnection->IncomingSendList,
        &Irp->Tail.Overlay.ListEntry
        );

    IoMarkIrpPending( Irp );

    if ( !firstSend || (receivingConnection->IndicatingSendIrp != NULL) ) {

        //
        // Pending data already exists, or an indication is already in
        // progress.  This Send remains behind the already pending data.
        //

        IF_DEBUG(LOOP2) DbgPrint( "    Data already pending\n" );

        RELEASE_LOOP_LOCK( "Send sends pending" );

    } else {

        //
        // Indicate the incoming data.
        //
        // *** Note that IndicateReceive returns with the loopback
        //     driver spin lock released.
        //

        IF_DEBUG(LOOP2) DbgPrint( "    LoopSend indicating receive\n" );
        IndicateReceive( receivingConnection, Irp );

    }

    IF_DEBUG(LOOP1) DbgPrint( "  Send request %lx complete\n", Irp );
    return STATUS_PENDING;

} // LoopSend


VOID
CompleteReceive (
    IN PIRP ReceiveIrp,
    IN PIRP SendIrp
    )

/*++

Routine Description:

    This routine completes the process of sending a message.  It copies
    the message from the source buffer into the destination buffer.

    The reference counts on the owning connections must have been
    incremented to account for the requesting IRPs in order to prevent
    their deletion.

Arguments:

    ReceiveIrp - Pointer to IRP used for Receive request

    SendIrp - Pointer to IRP used for Send request

Return Value:

    NTSTATUS - Indicates whether the connection was successfully created

--*/

{
    NTSTATUS status;
    PIO_STACK_LOCATION sendIrpSp;
    PIO_STACK_LOCATION receiveIrpSp;
    ULONG copyLength;
    PTDI_REQUEST_KERNEL_SEND sendRequest;
    PTDI_REQUEST_KERNEL_RECEIVE receiveRequest;
    PLOOP_CONNECTION sendingConnection;
    PLOOP_CONNECTION receivingConnection;

    IF_DEBUG(LOOP2) {
        DbgPrint( "    Send IRP %lx completes receive IRP %lx\n",
                    SendIrp, ReceiveIrp );
    }

    //
    // Copy the data from the source buffer into the destination buffer.
    //
    // *** Note that we special-case zero-length copies.  We don't
    //     bother to call LoopCopyData.  Part of the reason for this
    //     is that a zero-length send or receive issued from user mode
    //     yields an IRP that has a NULL MDL address.
    //

    sendIrpSp = IoGetCurrentIrpStackLocation( SendIrp );
    sendRequest = (PTDI_REQUEST_KERNEL_SEND)&sendIrpSp->Parameters;
    copyLength = sendRequest->SendLength;
    IF_DEBUG(LOOP4) {
        DbgPrint( "      Send request at %lx, send length %lx\n",
                    sendRequest, copyLength );
    }
    receiveIrpSp = IoGetCurrentIrpStackLocation( ReceiveIrp );
    receiveRequest = (PTDI_REQUEST_KERNEL_RECEIVE)&receiveIrpSp->Parameters;
    IF_DEBUG(LOOP4) {
        DbgPrint( "      Receive request at %lx, receive length %lx\n",
                    receiveRequest, receiveRequest->ReceiveLength );
    }
    status = STATUS_SUCCESS;
    if ( copyLength > receiveRequest->ReceiveLength ) {
        status = STATUS_BUFFER_OVERFLOW;
        copyLength = receiveRequest->ReceiveLength;
    }

    if ( copyLength != 0 ) {

        ASSERT( ReceiveIrp->MdlAddress != NULL );
        ASSERT( SendIrp->MdlAddress != NULL );

        LoopCopyData(
            ReceiveIrp->MdlAddress,
            SendIrp->MdlAddress,
            copyLength
            );

    }

    //
    // Complete the Receive and Send requests.
    //

    receivingConnection =
        (PLOOP_CONNECTION)receiveIrpSp->FileObject->FsContext;
    sendingConnection =
        (PLOOP_CONNECTION)sendIrpSp->FileObject->FsContext;

    ReceiveIrp->IoStatus.Status = status;
    ReceiveIrp->IoStatus.Information = copyLength;

    SendIrp->IoStatus.Status = STATUS_SUCCESS;
    SendIrp->IoStatus.Information = copyLength;

    IoCompleteRequest( ReceiveIrp, 2 );
    IoCompleteRequest( SendIrp, 2 );

    //
    // Dereference the connections.
    //

    ACQUIRE_LOOP_LOCK( "CompleteReceive dereference" );
    LoopDereferenceConnection( receivingConnection );
    LoopDereferenceConnection( sendingConnection );
    RELEASE_LOOP_LOCK( "CompleteReceive dereference" );

    return;

} // CompleteReceive


VOID
IndicateReceive (
    IN PLOOP_CONNECTION ReceivingConnection,
    IN PIRP InitialSendIrp
    )
{
    NTSTATUS status;
    PLOOP_ENDPOINT receivingEndpoint;
    PLOOP_CONNECTION sendingConnection;
    PLIST_ENTRY listEntry;
    PIRP receiveIrp;
    PIRP sendIrp;
    PTDI_IND_RECEIVE receiveHandler;
    PVOID receiveContext;
    PIO_STACK_LOCATION sendIrpSp;
    PTDI_REQUEST_KERNEL_SEND sendRequest;
    ULONG length;
    PMDL mdl;
    PVOID address;
    ULONG bytesTaken;

    receivingEndpoint = ReceivingConnection->Endpoint;
    IF_DEBUG(LOOP2) {
        DbgPrint( "    Receiving endpoint: %lx\n", receivingEndpoint );
    }

    //
    // Reference the receiving connection to prevent it from going away
    // while this routine is running.
    //

    ReceivingConnection->BlockHeader.ReferenceCount++;
    IF_DEBUG(LOOP3) {
        DbgPrint( "      New refcnt on connection %lx is %lx\n",
                ReceivingConnection,
                ReceivingConnection->BlockHeader.ReferenceCount );
    }

    //
    // Capture the address of the sending connection, as the receiving
    // connection's pointer can be zeroed if a Disconnect occurs.
    //

    sendingConnection = ReceivingConnection->RemoteConnection;
    ASSERT( sendingConnection != NULL );

    //
    // If the receiving connection has a pending Receive, satisfy it
    // with this Send.  If there is no pending Receive, and a Receive
    // handler has been enabled on the receiving connection, call it.
    // If the Receive handler returns with a Receive IRP, use it to
    // satisfy this Send.  If the Receive handler doesn't return an IRP,
    // leave this Send pending.
    //
    // !!! Note that the current implementation only works because
    //     partial sends are not supported.
    //

    while ( TRUE ) {

        //
        // We have a Send pending.  Is there a pending Receive?
        //

        listEntry = RemoveHeadList( &ReceivingConnection->PendingReceiveList );

        if ( listEntry != &ReceivingConnection->PendingReceiveList ) {

            //
            // Found a pending Receive.  Use it to satisfy the first
            // incoming Send.
            //

            receiveIrp = CONTAINING_RECORD(
                            listEntry,
                            IRP,
                            Tail.Overlay.ListEntry
                            );
            IF_DEBUG(LOOP2) {
                DbgPrint( "    Receive IRP pending: %lx\n", receiveIrp );
            }

            listEntry = RemoveHeadList(
                            &ReceivingConnection->IncomingSendList
                            );
            ASSERT( listEntry != &ReceivingConnection->IncomingSendList );
            sendIrp = CONTAINING_RECORD(
                        listEntry,
                        IRP,
                        Tail.Overlay.ListEntry
                        );
            IF_DEBUG(LOOP2) {
                DbgPrint( "    Send IRP pending: %lx\n", sendIrp );
            }

            //
            // If this is the first time through the loop, and we were
            // called to process a newly queued Send, dereference the
            // receiving connection -- LoopSend referenced the
            // connection an extra time in case the Send had to remain
            // queued.
            //

            if ( InitialSendIrp != NULL ) {
                LoopDereferenceConnection( ReceivingConnection );
            }

            RELEASE_LOOP_LOCK( "IndicateReceive pending Receive" );

            CompleteReceive( receiveIrp, sendIrp );

            ACQUIRE_LOOP_LOCK( "IndicateReceive pending Receive completed" );

            //
            // Fall to bottom of loop to handle more incoming Sends.
            //

        }  else {

            //
            // No pending Receive.  Is there a Receive handler?
            //

            receiveHandler = receivingEndpoint->ReceiveHandler;
            receiveContext = receivingEndpoint->ReceiveContext;

            if ( receiveHandler == NULL ) {

                //
                // No Receive handler.  The Send must remain queued.
                //

                IF_DEBUG(LOOP2) DbgPrint( "    No Receive handler\n" );

                break;

            }

            //
            // The receiving endpoint has a Receive handler.  Call it.
            // If it returns STATUS_SUCCESS, it completely handled the
            // data.  If it returns STATUS_MORE_PROCESSING_REQUIRED, it
            // also returns a Receive IRP describing where to put the
            // data.  Any other return status means the receiver can't
            // take the data just now, so we leave the Send queued and
            // wait for the receiver to post a Receive IRP.
            //
            // !!! Note that we don't currently handle partial data
            //     acceptance.
            //
            // First, remove the first Send from the Incoming Send list,
            // and make it the Indicating Send.  It must be removed from
            // the list to ensure that it isn't completed by
            // LoopDoDisconnection while we're indicating it.
            //

            listEntry = RemoveHeadList(
                            &ReceivingConnection->IncomingSendList
                            );
            ASSERT( listEntry != &ReceivingConnection->IncomingSendList );
            sendIrp = CONTAINING_RECORD(
                        listEntry,
                        IRP,
                        Tail.Overlay.ListEntry
                        );
            ReceivingConnection->IndicatingSendIrp = sendIrp;

            RELEASE_LOOP_LOCK( "IndicateReceive calling Receive handler" );

            IF_DEBUG(LOOP2) {
                DbgPrint( "    Receive handler: %lx\n", receiveHandler );
                DbgPrint( "    Send IRP: %lx\n", sendIrp );
            }

            sendIrpSp = IoGetCurrentIrpStackLocation( sendIrp );
            sendRequest = (PTDI_REQUEST_KERNEL_SEND)&sendIrpSp->Parameters;

            length = sendRequest->SendLength;
            ASSERTMSG(
                "Loopback driver doesn't handle partial or expedited sends",
                sendRequest->SendFlags == 0
                );

            //
            // Map the send buffer, if necessary.
            //

            mdl = sendIrp->MdlAddress;
            if ( MmGetMdlByteCount(mdl) == 0 ) {
                address = NULL;
            } else {
                address = MmGetSystemAddressForMdl( mdl );
            }

            //
            // Call the Receive handler.
            //

            status = receiveHandler(
                        receiveContext,
                        ReceivingConnection->ConnectionContext,
                        0,
                        MmGetMdlByteCount( mdl ),
                        length,
                        &bytesTaken,
                        address,
                        &receiveIrp
                        );

            ACQUIRE_LOOP_LOCK( "IndicateReceive after calling handler" );
            IF_DEBUG(LOOP2) {
                DbgPrint( "    Indication for send IRP %lx done\n", sendIrp );
            }

            ReceivingConnection->IndicatingSendIrp = NULL;

            if ( status == STATUS_SUCCESS ) {

                //
                // The Receive handler completely handled the data.
                // Complete the Send.
                //

                IF_DEBUG(LOOP2) {
                    DbgPrint( "    Receive handler handled data\n" );
                }

                ASSERTMSG(
                    "Loopback driver doesn't handle partial acceptance "
                        "of indications",
                    bytesTaken == length
                    );

                //
                // Dereference the sending and receiving connections --
                // LoopSend referenced the connections when it queued
                // the Send.
                //

                LoopDereferenceConnection( ReceivingConnection );
                LoopDereferenceConnection( sendingConnection );

                RELEASE_LOOP_LOCK( "IndicateReceive completely handled" );

                //
                // Complete the Send IRP.
                //

                sendIrp->IoStatus.Status = STATUS_SUCCESS;
                sendIrp->IoStatus.Information = sendRequest->SendLength;

                IoCompleteRequest( sendIrp, 2 );

                ACQUIRE_LOOP_LOCK( "IndicateReceive send completed" );

                //
                // Fall to bottom of loop to handle more incoming
                // Sends.
                //

            } else if ( status == STATUS_MORE_PROCESSING_REQUIRED ) {

                //
                // The Receive handler returned a Receive IRP to be used
                // to satisfy the Send.
                //

                IF_DEBUG(LOOP2) {
                    DbgPrint( "    Receive handler returned IRP: %lx\n",
                                receiveIrp );
                }

                ASSERTMSG(
                    "Loopback driver doesn't handle partial acceptance "
                        "of indications",
                    bytesTaken == 0
                    );

                //
                // Complete the Receive using the current Send.
                //
                // *** Note that the pending Send references both the
                //     sending and receiving connections.

                RELEASE_LOOP_LOCK( "IndicateReceive complete new Receive" );

                CompleteReceive( receiveIrp, sendIrp );

                ACQUIRE_LOOP_LOCK( "IndicateReceive new receive completed" );

                //
                // Fall to bottom of loop to handle more incoming
                // Sends.
                //

            } else {

                //
                // The Receive handler couldn't take the data.  This
                // Send will have to wait until receiver can post a
                // Receive IRP.
                //
                // *** Because we didn't hold the spin lock around the
                //     call to the Receive handler, it's possible that
                //     the receiver has already posted a Receive IRP.
                //     Because we were in the middle of an indication,
                //     that Receive would have been queued to the
                //     Pending Receive list, and we should go get it
                //     now.  If the receiver hasn't posted a Receive
                //     yet, then this Send will be put back on the
                //     Incoming Send list before the Receive does come
                //     in (since we're now holding the lock).
                //

                ASSERT( status == STATUS_DATA_NOT_ACCEPTED );

                IF_DEBUG(LOOP2) DbgPrint( "    Data not accepted\n" );

                if ( GET_BLOCK_STATE(ReceivingConnection) !=
                                                BlockStateActive ) {

                    //
                    // The connection is closing.  Abort the current
                    // Send and leave.
                    //

                    LoopDereferenceConnection( ReceivingConnection );
                    LoopDereferenceConnection( sendingConnection );

                    RELEASE_LOOP_LOCK( "IndicateReceive disconnecting" );

                    sendIrp->IoStatus.Status = STATUS_DISCONNECTED;
                    IoCompleteRequest( sendIrp, 2 );

                    ACQUIRE_LOOP_LOCK( "IndicateReceive send aborted" );

                    break;

                }

                listEntry = RemoveHeadList(
                                &ReceivingConnection->PendingReceiveList
                                );

                if ( listEntry !=
                            &ReceivingConnection->PendingReceiveList ) {

                    //
                    // A Receive has been posted.  Use it to satisfy
                    // this Send.
                    //

                    receiveIrp = CONTAINING_RECORD(
                                    listEntry,
                                    IRP,
                                    Tail.Overlay.ListEntry
                                    );
                    IF_DEBUG(LOOP2) {
                        DbgPrint( "    Receive IRP pending: %lx\n",
                                    receiveIrp );
                    }

                    //
                    // Complete the Receive using the current Send.
                    //

                    RELEASE_LOOP_LOCK(
                        "IndicateReceive complete posted Receive"
                        );

                    CompleteReceive( receiveIrp, sendIrp );

                    ACQUIRE_LOOP_LOCK(
                        "IndicateReceive posted receive completed"
                        );

                    //
                    // Fall to bottom of loop to handle more incoming
                    // Sends.
                    //

                } else {

                    //
                    // The handler didn't take the data, and it didn't
                    // post a Receive IRP.  Requeue the current send and
                    // get out.
                    //

                    InsertHeadList(
                        &ReceivingConnection->IncomingSendList,
                        &sendIrp->Tail.Overlay.ListEntry
                        );

                    break;

                }

            }

        } // pending receive?

        //
        // If we get here, we need to indicate the next incoming Send,
        // if there is one.
        //

        InitialSendIrp = NULL;

        if ( (GET_BLOCK_STATE(ReceivingConnection) != BlockStateActive) ||
             (ReceivingConnection->IncomingSendList.Flink ==
                            &ReceivingConnection->IncomingSendList) ) {

            //
            // No more Sends, or connection no longer active.  Leave.
            //

            break;

        }

        //
        // Process the next Send.
        //

    } // while ( TRUE )

    //
    // Remove the connection reference acquired at the start of this
    // routine.
    //

    LoopDereferenceConnection( ReceivingConnection );

    RELEASE_LOOP_LOCK( "IndicateReceive done" );

    return;

} // IndicateReceive