summaryrefslogtreecommitdiffstats
path: root/private/ntos/ndis/elnkmc/loopback.c
blob: afef5e7b6ec54708fe50b68a537d2aa004ccd70e (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
/*++

Copyright (c) 1992  Microsoft Corporation

Module Name:

    loopback.c

Abstract:

    The routines here indicate packets on the loopback queue and are
    responsible for inserting and removing packets from the loopback
    queue and the send finishing queue.

Author:

    Johnson R. Apacible (JohnsonA) 10-Jul-1991

Environment:

    Operates at dpc level - or the equivalent on os2 and dos.

Revision History:

--*/

#include <ndis.h>

//
// So we can trace things...
//
#define STATIC

#include <efilter.h>
#include <elnkhw.h>
#include <elnksw.h>


VOID
ElnkProcessLoopback(
    IN PELNK_ADAPTER Adapter
    )

/*++

Routine Description:

    This routine is responsible for indicating *one* packet on
    the loopback queue either completing it or moving on to the
    finish send queue.

    NOTE: Called with the lock held!!!

Arguments:

    Adapter - The adapter whose loopback queue we are processing.

Return Value:

    None.

--*/

{
    if (Adapter->FirstLoopBack) {

        //
        // Packet at the head of the loopback list.
        //
        PNDIS_PACKET PacketToMove;

        //
        // The reserved portion of the above packet.
        //
        PELNK_RESERVED Reserved;

        //
        // The first buffer in the ndis packet to be loopbacked.
        //
        PNDIS_BUFFER FirstBuffer;

        //
        // The total amount of user data in the packet to be
        // loopbacked.
        //
        UINT TotalPacketLength;

        //
        // Eventually the address of the data to be indicated
        // to the transport.
        //
        PVOID BufferAddress;

        //
        // Eventually the length of the data to be indicated
        // to the transport.
        //
        UINT BufferLength;

        PELNK_OPEN Open;

        PacketToMove = Adapter->FirstLoopBack;

        Reserved = PELNK_RESERVED_FROM_PACKET(PacketToMove);

        //
        // Remove packet from loopback queue
        //

        if (!Reserved->Next) {

            Adapter->LastLoopBack = NULL;

        }

        Adapter->FirstLoopBack = Reserved->Next;

        Adapter->IndicatedAPacket = TRUE;

        Open = PELNK_OPEN_FROM_BINDING_HANDLE(Reserved->MacBindingHandle);

        NdisReleaseSpinLock(&Adapter->Lock);

        //
        // See if we need to copy the data from the packet
        // into the loopback buffer.
        //
        // We need to copy to the local loopback buffer if
        // the first buffer of the packet is less than the
        // minimum loopback size AND the first buffer isn't
        // the total packet.
        //

        NdisQueryPacket(
            PacketToMove,
            NULL,
            NULL,
            &FirstBuffer,
            &TotalPacketLength
            );

        NdisQueryBuffer(
            FirstBuffer,
            &BufferAddress,
            &BufferLength
            );

        if ((BufferLength < ELNK_SIZE_OF_LOOKAHEAD) &&
            (BufferLength != TotalPacketLength)) {

            ElnkCopyFromPacketToBuffer(
                PacketToMove,
                0,
                ELNK_SIZE_OF_LOOKAHEAD,
                Adapter->Loopback,
                &BufferLength
                );

            BufferAddress = Adapter->Loopback;

        }

        //
        // Indicate the packet to every open binding
        // that could want it.
        //

        if ELNKDEBUG DPrint1("Loopback: indicating receive\n");

        if (BufferLength < ELNK_HEADER_SIZE) {

            //
            // Must have at least an address
            //

            if (BufferLength > 5) {

                EthFilterIndicateReceive(
                    Adapter->FilterDB,
                    PacketToMove,
                    ((PCHAR)BufferAddress),
                    BufferAddress,
                    BufferLength,
                    NULL,
                    0,
                    0
                    );

            }

        } else {

            EthFilterIndicateReceive(
                Adapter->FilterDB,
                PacketToMove,
                ((PCHAR)BufferAddress),
                BufferAddress,
                ELNK_HEADER_SIZE,
                ((PUCHAR)BufferAddress) + ELNK_HEADER_SIZE,
                BufferLength - ELNK_HEADER_SIZE,
                TotalPacketLength - ELNK_HEADER_SIZE
                );

        }

        NdisCompleteSend(
            Open->NdisBindingContext,
            PacketToMove,
            ((Reserved->SuccessfulTransmit)?
             (NDIS_STATUS_SUCCESS):(NDIS_STATUS_FAILURE))
            );

        NdisAcquireSpinLock(&Adapter->Lock);

        //
        // We can decrement the reference count since it is the one left
        // from when we submitted the packet.
        //

        Open->References--;

    }

}

VOID
ElnkPutPacketOnFinishTrans(
    IN PELNK_ADAPTER Adapter,
    IN PNDIS_PACKET Packet
    )

/*++

Routine Description:

    Put the packet on the adapter wide queue for packets that
    are transmitting.

    NOTE: This routine assumes that the lock is held.

    NOTE: By definition any packet given to this routine is ready
    to complete.

Arguments:

    Adapter - The adapter that contains the queue.

    Packet - The packet to be put on the queue.

Return Value:

    None.

--*/

{

    PELNK_RESERVED Reserved = PELNK_RESERVED_FROM_PACKET(Packet);

    if (Adapter->LastFinishTransmit) {

        PELNK_RESERVED LastReserved =
           PELNK_RESERVED_FROM_PACKET(Adapter->LastFinishTransmit);

        LastReserved->Next = Packet;

    }

    Reserved->Next = NULL;

    Adapter->LastFinishTransmit = Packet;

    if (!Adapter->FirstFinishTransmit) {

        Adapter->FirstFinishTransmit = Packet;

    }

    Adapter->TransmitsQueued--;

}