summaryrefslogtreecommitdiffstats
path: root/private/ntos/tdi/st/send.c
blob: 0e6a2b9f6fb7331208c54c5d54ddac58cb4dd6fd (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
/*++

Copyright (c) 1989-1993  Microsoft Corporation

Module Name:

    send.c

Abstract:

    This module contains code which performs the following TDI services:

        o   TdiSend
        o   TdiSendDatagram

Environment:

    Kernel mode

Revision History:

--*/

#include "st.h"


NTSTATUS
StTdiSend(
    IN PIRP Irp
    )

/*++

Routine Description:

    This routine performs the TdiSend request for the transport provider.

Arguments:

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

Return Value:

    NTSTATUS - status of operation.

--*/

{
    KIRQL oldirql, cancelirql;
    NTSTATUS status;
    PTP_CONNECTION connection;
    PMDL SendBuffer;
    ULONG SendBufferLength;
    PIO_STACK_LOCATION irpSp;
    PTDI_REQUEST_KERNEL_SEND parameters;
    PIRP TempIrp;

    //
    // Determine which connection this send belongs on.
    //

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

    //
    // Check that this is really a connection.
    //

    if ((connection->Size != sizeof (TP_CONNECTION)) ||
        (connection->Type != ST_CONNECTION_SIGNATURE)) {
        return STATUS_INVALID_CONNECTION;
    }

    //
    // Now map the data in to SVA space.
    //

    parameters = (PTDI_REQUEST_KERNEL_SEND)(&irpSp->Parameters);
    SendBuffer = Irp->MdlAddress;
    SendBufferLength = parameters->SendLength;

    //
    // Interpret send options.
    //

    //
    // Now we have a reference on the connection object.  Queue up this
    // send to the connection object.
    //


    // This reference is removed by TdiDestroyRequest

    StReferenceConnection("TdiSend", connection);

    IRP_CONNECTION(irpSp) = connection;
    IRP_REFCOUNT(irpSp) = 1;

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

    if ((connection->Flags & CONNECTION_FLAGS_STOPPING) != 0) {
        RELEASE_SPIN_LOCK (&connection->SpinLock,oldirql);
        IoReleaseCancelSpinLock(cancelirql);
        StCompleteSendIrp(
            Irp,
            connection->Status,
            0);
        status = STATUS_PENDING;
    } else {

        StReferenceConnection ("Verify Temp Use", connection);

        //
        // Insert onto the send queue, and make the IRP
        // cancellable.
        //

        InsertTailList (&connection->SendQueue,&Irp->Tail.Overlay.ListEntry);


        //
        // If this IRP has been cancelled, then call the
        // cancel routine.
        //

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

        Irp->CancelRoutine = StCancelSend;

        //
        // If this connection is waiting for an EOR to appear because a non-EOR
        // send failed at some point in the past, fail this send. Clear the
        // flag that causes this if this request has the EOR set.
        //
        // BUGBUG: Should the FailSend status be clearer here?
        //

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

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

            //
            // BUGBUG: Should we save status from real failure?
            //

            FailSend (connection, STATUS_LINK_FAILED, TRUE);

            if ( (parameters->SendFlags & TDI_SEND_PARTIAL) == 0) {
                connection->Flags &= ~CONNECTION_FLAGS_FAILING_TO_EOR;
            }

            StDereferenceConnection ("Failing to EOR", connection);   // release lookup hold.
            return STATUS_PENDING;
        }


        //
        // If the send state is either IDLE or W_EOR, then we should
        // begin packetizing this send.  Otherwise, some other event
        // will cause it to be packetized.
        //

        //
        // NOTE: If we call StartPacketizingConnection, we make
        // sure that it is the last operation we do on this
        // connection. This allows us to "hand off" the reference
        // we have to that function, which converts it into
        // a reference for being on the packetize queue.
        //

        switch (connection->SendState) {

        case CONNECTION_SENDSTATE_IDLE:

            InitializeSend (connection);   // sets state to PACKETIZE

            //
            // If we can, packetize right now.
            //

            if ((!(connection->Flags & CONNECTION_FLAGS_PACKETIZE)) &&
                (!(connection->Flags & CONNECTION_FLAGS_STOPPING))) {

                connection->Flags |= CONNECTION_FLAGS_PACKETIZE;

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

                PacketizeSend (connection);

            } else {

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

                StDereferenceConnection ("Stopping or already packetizing", connection);   // release lookup hold.

            }

            break;

        case CONNECTION_SENDSTATE_W_EOR:
            connection->SendState = CONNECTION_SENDSTATE_PACKETIZE;

            //
            // Adjust the send variables on the connection so that
            // they correctly point to this new send.  We can't call
            // InitializeSend to do that, because we need to keep
            // track of the other outstanding sends on this connection
            // which have been sent but are a part of this message.
            //

            TempIrp = CONTAINING_RECORD(
                connection->SendQueue.Flink,
                IRP,
                Tail.Overlay.ListEntry);

            connection->sp.CurrentSendIrp = TempIrp;
            connection->sp.CurrentSendMdl = TempIrp->MdlAddress;
            connection->sp.SendByteOffset = 0;
            connection->CurrentSendLength +=
                IRP_SEND_LENGTH(IoGetCurrentIrpStackLocation(TempIrp));

            StartPacketizingConnection (connection, TRUE, oldirql, cancelirql);
            break;

        default:

            //
            // The connection is in another state (such as
            // W_ACK or W_LINK), we just need to make sure
            // to call InitializeSend if the new one is
            // the first one on the list.
            //

            //
            // BUGBUG: Currently InitializeSend sets SendState,
            // we should fix this.
            //

            if (connection->SendQueue.Flink == &Irp->Tail.Overlay.ListEntry) {
                ULONG SavedSendState;
                SavedSendState = connection->SendState;
                InitializeSend (connection);
                connection->SendState = SavedSendState;
            }
            RELEASE_SPIN_LOCK (&connection->SpinLock, oldirql);
            IoReleaseCancelSpinLock(cancelirql);

            StDereferenceConnection("temp TdiSend", connection);

        }

    }

    status = STATUS_PENDING;


    return status;
} /* TdiSend */


NTSTATUS
StTdiSendDatagram(
    IN PIRP Irp
    )

/*++

Routine Description:

    This routine performs the TdiSendDatagram 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;
    KIRQL oldirql;
    PTP_REQUEST tpRequest;
    PTP_ADDRESS_FILE addressFile;
    PTP_ADDRESS address;
    PMDL SendBuffer;
    ULONG SendBufferLength;
    PIO_STACK_LOCATION irpSp;
    PTDI_REQUEST_KERNEL_SENDDG parameters;
    LARGE_INTEGER timeout = {0,0};
    UINT MaxUserData;

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

    status = StVerifyAddressObject (addressFile);
    if (!NT_SUCCESS (status)) {
        return status;
    }

    address = addressFile->Address;
    parameters = (PTDI_REQUEST_KERNEL_SENDDG)(&irpSp->Parameters);
    SendBuffer = Irp->MdlAddress;
    SendBufferLength = parameters->SendLength;

    //
    // Check that the length is short enough.
    //

    MacReturnMaxDataSize(
        &address->Provider->MacInfo,
        NULL,
        0,
        address->Provider->MaxSendPacketSize,
        &MaxUserData);

    if (SendBufferLength >
        (MaxUserData - sizeof(ST_HEADER))) {

        return STATUS_INVALID_PARAMETER;

    }

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

    status = StCreateRequest (
                 Irp,                           // IRP for this request.
                 address,                       // context.
                 REQUEST_FLAGS_ADDRESS,         // partial flags.
                 SendBuffer,                    // the data to be sent.
                 SendBufferLength,              // length of the data.
                 timeout,
                 &tpRequest);

    if (!NT_SUCCESS (status)) {
        StDereferenceAddress ("no send request", address);
        return status;                          // if we couldn't queue the request.
    }

    StReferenceAddress ("Send datagram", address);
    tpRequest->Owner = AddressType;

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

    if ((address->Flags & ADDRESS_FLAGS_STOPPING) != 0) {
        RELEASE_SPIN_LOCK (&address->SpinLock,oldirql);
        StCompleteRequest (tpRequest, STATUS_NETWORK_NAME_DELETED, 0);
        return STATUS_PENDING;
    } else {
        InsertTailList (
            &address->SendDatagramQueue,
            &tpRequest->Linkage);
        RELEASE_SPIN_LOCK (&address->SpinLock,oldirql);
    }

    //
    // The request is queued.  Ship the next request at the head of the queue,
    // provided the completion handler is not active.  We serialize this so
    // that only one MDL and ST datagram header needs to be statically
    // allocated for reuse by all send datagram requests.
    //

    (VOID)StSendDatagramsOnAddress (address);

    StDereferenceAddress("tmp send datagram", address);

    return STATUS_PENDING;

} /* StTdiSendDatagram */