summaryrefslogtreecommitdiffstats
path: root/private/ntos/tdi/st/stmac.h
blob: cea2feff0b599388f7f12f8ec76fc201c7dcb4c0 (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
/*++

Copyright (c) 1989-1993  Microsoft Corporation

Module Name:

    stmac.h

Abstract:

    This header file defines manifest constants and necessary macros for use
    by transports dealing with multiple MAC cards through the NDIS interface.

Revision History:

--*/

#ifndef _STMAC_
#define _STMAC_

//
// MAC-specific definitions, some of which get used below
//

#define MAX_MAC_HEADER_LENGTH       32
#define MAX_SOURCE_ROUTING          18
#define MAX_DEFAULT_SR               2

#define ETHERNET_ADDRESS_LENGTH        6
#define ETHERNET_PACKET_LENGTH      1514  // max size of an ethernet packet
#define ETHERNET_HEADER_LENGTH        14  // size of the ethernet MAC header
#define ETHERNET_DATA_LENGTH_OFFSET   12
#define ETHERNET_DESTINATION_OFFSET    0
#define ETHERNET_SOURCE_OFFSET         6

#define TR_ADDRESS_LENGTH        6
#define TR_ADDRESS_OFFSET        2
#define TR_SPECIFIC_OFFSET       0
#define TR_PACKET_LENGTH      1514  // max size of a TR packet //BUGBUG
#define TR_HEADER_LENGTH        36  // size of the MAC header w/o source routing
#define TR_DATA_LENGTH_OFFSET    0
#define TR_DESTINATION_OFFSET    2
#define TR_SOURCE_OFFSET         8
#define TR_ROUTING_OFFSET       14      // starts at the 14th byte
#define TR_GR_BCAST_LENGTH       2
#define TR_GR_BROADCAST         0xC270  // what a general route b'cast looks like
#define TR_ROUTING_LENGTH_MASK  0x1F    // low 5 bits in byte
#define TR_DIRECTION_MASK       0x80    // returns direction bit

#define TR_PREAMBLE_AC        0x10  // how would these be specified?
#define TR_PREAMBLE_FC        0x40

#define TR_HEADER_BYTE_0            0x10
#define TR_HEADER_BYTE_1            0x40

#define FDDI_ADDRESS_LENGTH         6
#define FDDI_HEADER_BYTE            0x57



//
// We need this to define information about the MAC. Note that
// it is a strange structure in that the first four elements
// are for use internally by the stmac routines, while the
// DeviceContext knows about and uses the last two.
//

typedef struct _ST_NDIS_IDENTIFICATION {
  NDIS_MEDIUM MediumType;
  BOOLEAN SourceRouting;
  BOOLEAN MediumAsync;
  BOOLEAN CopyLookahead;
  ULONG DestinationOffset;
  ULONG SourceOffset;
  ULONG AddressLength;
  ULONG MaxHeaderLength;
} ST_NDIS_IDENTIFICATION, *PST_NDIS_IDENTIFICATION;



VOID
MacConstructHeader(
    IN PST_NDIS_IDENTIFICATION MacInfo,
    IN PUCHAR Buffer,
    IN PUCHAR DestinationAddress,
    IN PUCHAR SourceAddress,
    IN UINT PacketLength,
    IN PUCHAR SourceRouting,
    IN UINT SourceRoutingLength,
    OUT PUINT HeaderLength
    );

VOID
MacReturnMaxDataSize(
    IN PST_NDIS_IDENTIFICATION MacInfo,
    IN PUCHAR SourceRouting,
    IN UINT SourceRoutingLength,
    IN UINT DeviceMaxFrameSize,
    OUT PUINT MaxFrameSize
    );

VOID
MacInitializeMacInfo(
    IN NDIS_MEDIUM MacType,
    OUT PST_NDIS_IDENTIFICATION MacInfo
    );


extern UCHAR SingleRouteSourceRouting[];
extern UCHAR GeneralRouteSourceRouting[];
extern ULONG DefaultSourceRoutingLength;


//++
//
// VOID
// MacReturnDestinationAddress(
//     IN PST_NDIS_IDENTIFICATION MacInfo,
//     IN PVOID Packet,
//     OUT PVOID * DestinationAddress
//     );
//
// Routine Description:
//
//     Returns the a pointer to the destination address in the packet.
//
// Arguments:
//
//     MacInfo - Describes the MAC we wish to decode.
//
//     Packet - The packet data.
//
//     DestinationAddress - Returns the start of the destination address.
//
// Return Value:
//
//     None.
//
//--

#define MacReturnDestinationAddress(_MacInfo, _Packet, _DestinationAddress) \
    *(_DestinationAddress) = ((PUCHAR)(_Packet)) + (_MacInfo)->DestinationOffset


//++
//
// VOID
// MacReturnSourceAddress(
//     IN PST_NDIS_IDENTIFICATION MacInfo,
//     IN PVOID Packet,
//     OUT PHARDWARE_ADDRESS SourceAddressBuffer,
//     OUT PHARDWARE_ADDRESS * SourceAddress,
//     );
//
// Routine Description:
//
//     Copies the source address in the packet into SourceAddress.
//     NOTE THAT IT MAY COPY THE DATA, UNLIKE ReturnDestinationAddress
//     AND ReturnSourceRouting.  Optionally, indicates whether the
//     destination address is a multicast address.
//
// Arguments:
//
//     MacInfo - Describes the MAC we wish to decode.
//
//     Packet - The packet data.
//
//     SourceAddressBuffer - A buffer to hold the source address,
//         if needed.
//
//     SourceAddress - Returns a pointer to the source address.
//
//     Multicast - Optional pointer to a BOOLEAN to receive indication
//         of whether the destination was a multicast address.
//
// Return Value:
//
//     None.
//
//--

//
// NOTE:  The default case below handles Ethernet and FDDI.
//

#define MacReturnSourceAddress(_MacInfo, _Packet, _SourceAddressBuffer, _SourceAddress) \
{                                                                       \
    PUCHAR TmpPacket = (PUCHAR)(_Packet);                               \
    PUCHAR SrcBuffer = (PUCHAR)(_SourceAddressBuffer);                  \
                                                                        \
    switch ((_MacInfo)->MediumType) {                                   \
    case NdisMedium802_5:                                               \
        *(PULONG)SrcBuffer = *(ULONG UNALIGNED *)(&TmpPacket[8]) & ~0x80;\
        SrcBuffer[4] = TmpPacket[12];                                   \
        SrcBuffer[5] = TmpPacket[13];                                   \
        *(_SourceAddress) = (PHARDWARE_ADDRESS)SrcBuffer;               \
        break;                                                          \
    default:                                                            \
        *(_SourceAddress) = (PHARDWARE_ADDRESS)(TmpPacket +             \
                                        (_MacInfo)->SourceOffset);      \
        break;                                                          \
    }                                                                   \
}


//++
//
// VOID
// MacReturnSourceRouting(
//     IN PST_NDIS_IDENTIFICATION MacInfo,
//     IN PVOID Packet,
//     OUT PVOID * SourceRouting
//     OUT PUINT SourceRoutingLength
//     );
//
// Routine Description:
//
//     Returns the a pointer to the source routing info in the packet.
//
// Arguments:
//
//     MacInfo - Describes the MAC we wish to decode.
//
//     Packet - The packet data.
//
//     SourceRouting - Returns the start of the source routing information,
//         or NULL if none is present.
//
//     SourceRoutingLength - Returns the length of the source routing
//         information.
//
// Return Value:
//
//     None.
//
//--

#define MacReturnSourceRouting(_MacInfo, _Packet, _SourceRouting, _SourceRoutingLength) \
{                                                               \
    PUCHAR TmpPacket = (PUCHAR)(_Packet);                       \
    if ((_MacInfo)->SourceRouting) {                            \
        if (TmpPacket[8] & 0x80) {                              \
            *(_SourceRouting) = TmpPacket + 14;                 \
            *(_SourceRoutingLength) = TmpPacket[14] & 0x1f;     \
        } else {                                                \
            *(_SourceRouting) = NULL;                           \
        }                                                       \
    } else {                                                    \
        *(_SourceRouting) = NULL;                               \
    }                                                           \
}

//++
//
// VOID
// MacReturnPacketLength(
//     IN PST_NDIS_IDENTIFICATION MacInfo,
//     IN PVOID Header,
//     IN UINT PacketLength,
//     OUT PUINT DataLength
//     );
//
// Routine Description:
//
//     Returns the length of data in the packet given the header.
//
// Arguments:
//
//     MacInfo - Describes the MAC we wish to decode.
//
//     Header - The packet header.
//
//     PacketLength - The length of the data (not including header).
//
//     DataLength - Returns the length of the data.  Unchanged if the
//         packet is not recognized.  Should be initialized by caller to 0.
//
// Return Value:
//
//     None.
//
//--

#define MacReturnPacketLength(_MacInfo, _Header, _HeaderLength, _PacketLength, _DataLength) \
{                                                               \
    PUCHAR TmpPacket = (PUCHAR)(_Header);                       \
    UINT TmpLength;                                             \
                                                                \
    switch ((_MacInfo)->MediumType) {                           \
    case NdisMedium802_3:                                       \
        if ((_HeaderLength) >= 14) {                            \
            TmpLength = (TmpPacket[12] << 8) | TmpPacket[13];   \
            if (TmpLength <= 0x600) {                           \
                if (TmpLength <= (_PacketLength)) {             \
                    *(_DataLength) = TmpLength;                 \
                }                                               \
            }                                                   \
        }                                                       \
        break;                                                  \
    case NdisMedium802_5:                                       \
        if (((_HeaderLength) >= 14) &&                          \
            (!(TmpPacket[8] & 0x80) ||                          \
             ((_HeaderLength) >=                                \
                       (UINT)(14 + (TmpPacket[14] & 0x1f))))) { \
            *(_DataLength) = (_PacketLength);                   \
        }                                                       \
        break;                                                  \
    case NdisMediumFddi:                                        \
        if ((_HeaderLength) >= 13) {                            \
            *(_DataLength) = (_PacketLength);                   \
        }                                                       \
        break;                                                  \
    }                                                           \
}

//++
//
// VOID
// MacReturnHeaderLength(
//     IN PST_NDIS_IDENTIFICATION MacInfo,
//     IN PVOID Packet,
//     OUT PVOID HeaderLength,
//     );
//
// Routine Description:
//
//     Returns the length of the MAC header in a packet (this
//     is used for loopback indications to separate header
//     and data).
//
// Arguments:
//
//     MacInfo - Describes the MAC we wish to decode.
//
//     Header - The packet header.
//
//     HeaderLength - Returns the length of the header.
//
// Return Value:
//
//     None.
//
//--

#define MacReturnHeaderLength(_MacInfo, _Header, _HeaderLength) \
{                                                               \
    PUCHAR TmpPacket = (PUCHAR)(_Header);                       \
                                                                \
    switch ((_MacInfo)->MediumType) {                           \
    case NdisMedium802_3:                                       \
        *(_HeaderLength) = 14;                                  \
        break;                                                  \
    case NdisMedium802_5:                                       \
         if (TmpPacket[8] & 0x80) {                             \
             *(_HeaderLength) = (TmpPacket[14] & 0x1f) + 14;    \
         } else {                                               \
             *(_HeaderLength) = 14;                             \
         }                                                      \
        break;                                                  \
    case NdisMediumFddi:                                        \
        *(_HeaderLength) = 13;                                  \
        break;                                                  \
    }                                                           \
}

//++
//
// VOID
// MacReturnSingleRouteSR(
//     IN PST_NDIS_IDENTIFICATION MacInfo,
//     OUT PVOID * SingleRouteSR,
//     OUT PUINT SingleRouteSRLength
//     );
//
// Routine Description:
//
//     Returns the a pointer to the standard single route broadcast
//     source routing information for the media type. This is used
//     for ADD_NAME_QUERY, DATAGRAM, NAME_IN_CONFLICT, NAME_QUERY,
//     and STATUS_QUERY frames.
//
// Arguments:
//
//     MacInfo - Describes the MAC we wish to decode.
//
//     SingleRouteSR - Returns a pointer to the data.
//
//     SingleRouteSRLength - The length of SingleRouteSR.
//
// Return Value:
//
//     None.
//
//--

#define MacReturnSingleRouteSR(_MacInfo, _SingleRouteSR, _SingleRouteSRLength) \
{                                                               \
    switch ((_MacInfo)->MediumType) {                           \
    case NdisMedium802_5:                                       \
        *(_SingleRouteSR) = SingleRouteSourceRouting;           \
        *(_SingleRouteSRLength) = DefaultSourceRoutingLength;   \
        break;                                                  \
    default:                                                    \
        *(_SingleRouteSR) = NULL;                               \
        break;                                                  \
    }                                                           \
}


//++
//
// VOID
// MacReturnGeneralRouteSR(
//     IN PST_NDIS_IDENTIFICATION MacInfo,
//     OUT PVOID * GeneralRouteSR,
//     OUT PUINT GeneralRouteSRLength
//     );
//
// Routine Description:
//
//     Returns the a pointer to the standard general route broadcast
//     source routing information for the media type. This is used
//     for NAME_RECOGNIZED frames.
//
// Arguments:
//
//     MacInfo - Describes the MAC we wish to decode.
//
//     GeneralRouteSR - Returns a pointer to the data.
//
//     GeneralRouteSRLength - The length of GeneralRouteSR.
//
// Return Value:
//
//     None.
//
//--

#define MacReturnGeneralRouteSR(_MacInfo, _GeneralRouteSR, _GeneralRouteSRLength) \
{                                                               \
    switch ((_MacInfo)->MediumType) {                           \
    case NdisMedium802_5:                                       \
        *(_GeneralRouteSR) = GeneralRouteSourceRouting;         \
        *(_GeneralRouteSRLength) = DefaultSourceRoutingLength;  \
        break;                                                  \
    default:                                                    \
        *(_GeneralRouteSR) = NULL;                              \
        break;                                                  \
    }                                                           \
}

#if 0

//++
//
// VOID
// MacCreateGeneralRouteReplySR(
//     IN PST_NDIS_IDENTIFICATION MacInfo,
//     IN PUCHAR ExistingSR,
//     IN UINT ExistingSRLength,
//     OUT PUCHAR * NewSR
//     );
//
// Routine Description:
//
//     This modifies an existing source routing entry to make
//     it into a general-route source routing entry. The assumption
//     is that is to reply to existing source routing, so the
//     direction bit is also reversed. In addition, if it is
//     determined that no source routing is needed in the reply,
//     then NULL is returned.
//
//     Note that the information is modified in-place, but a
//     separate pointer is returned (to allow NULL to be returned).
//
// Arguments:
//
//     MacInfo - Describes the MAC we wish to decode.
//
//     ExistingSR - The existing source routing to be modified.
//
// Return Value:
//
//     None.
//
//--

#define MacCreateGeneralRouteReplySR(_MacInfo, _ExistingSR, _ExistingSRLength, _NewSR)  \
{                                                               \
    if (_ExistingSR) {                                          \
        PUCHAR TmpSR = (PUCHAR)(_ExistingSR);                   \
        switch ((_MacInfo)->MediumType) {                       \
        case NdisMedium802_5:                                   \
            TmpSR[0] = (TmpSR[0] & 0x1f) | 0x80;                \
            TmpSR[1] = (TmpSR[1] ^ 0x80) | 0x70;                \
            *(_NewSR) = (_ExistingSR);                          \
            break;                                              \
        default:                                                \
            *(_NewSR) = (_ExistingSR);                          \
            break;                                              \
        }                                                       \
    } else {                                                    \
        *(_NewSR) = NULL;                                       \
    }                                                           \
}
#endif


//++
//
// VOID
// MacCreateNonBroadcastReplySR(
//     IN PST_NDIS_IDENTIFICATION MacInfo,
//     IN PUCHAR ExistingSR,
//     IN UINT ExistingSRLength,
//     OUT PUCHAR * NewSR
//     );
//
// Routine Description:
//
//     This modifies an existing source routing entry to make
//     it into a non-broadcast source routing entry. The assumption
//     is that is to reply to existing source routing, so the
//     direction bit is also reversed. In addition, if it is
//     determined that no source routing is needed in the reply,
//     then NULL is returned.
//
//     Note that the information is modified in-place, but a
//     separate pointer is returned (to allow NULL to be returned).
//
// Arguments:
//
//     MacInfo - Describes the MAC we wish to decode.
//
//     ExistingSR - The existing source routing to be modified.
//
// Return Value:
//
//     None.
//
//--

#define MacCreateNonBroadcastReplySR(_MacInfo, _ExistingSR, _ExistingSRLength, _NewSR)  \
{                                                               \
    if (_ExistingSR) {                                          \
        PUCHAR TmpSR = (PUCHAR)(_ExistingSR);                   \
        switch ((_MacInfo)->MediumType) {                       \
        case NdisMedium802_5:                                   \
            if ((_ExistingSRLength) == 2) {                     \
                *(_NewSR) = NULL;                               \
            } else {                                            \
                TmpSR[0] = (TmpSR[0] & 0x1f);                   \
                TmpSR[1] = (TmpSR[1] ^ 0x80) | 0x70;            \
                *(_NewSR) = (_ExistingSR);                      \
            }                                                   \
            break;                                              \
        default:                                                \
            *(_NewSR) = (_ExistingSR);                          \
            break;                                              \
        }                                                       \
    } else {                                                    \
        *(_NewSR) = NULL;                                       \
    }                                                           \
}


//++
//
// VOID
// MacModifyHeader(
//     IN PST_NDIS_IDENTIFICATION MacInfo,
//     IN PUCHAR Header,
//     IN UINT PacketLength
//     );
//
// Routine Description:
//
//     Modifies a pre-built packet header to include the
//     packet length. Used for connection-oriented traffic
//     where the header is pre-built.
//
// Arguments:
//
//     MacInfo - Describes the MAC we wish to decode.
//
//     Header - The header to modify.
//
//     PacketLength - Packet length (not including the header).
//       Currently this is the only field that cannot be pre-built.
//
// Return Value:
//
//     None.
//
//--

#define MacModifyHeader(_MacInfo, _Header, _PacketLength)            \
{                                                                    \
    switch ((_MacInfo)->MediumType) {                                \
    case NdisMedium802_3:                                            \
        (_Header)[12] = (UCHAR)((_PacketLength) >> 8);               \
        (_Header)[13] = (UCHAR)((_PacketLength) & 0xff);             \
        break;                                                       \
    }                                                                \
}


VOID
MacSetMulticastAddress(
    IN NDIS_MEDIUM Type,
    IN PUCHAR Buffer
    );



//  VOID
//  StSetNdisPacketLength (
//      IN NDIS_PACKET Packet,
//      IN ULONG Length
//      );
//
// NB: This is not a general purpose macro; it assumes that we are setting the
//     length of an NDIS packet with only one NDIS_BUFFER chained. We do
//     this to save time during the sending of short control packets.
//

#define StSetNdisPacketLength(_packet,_length) {              \
    PNDIS_BUFFER NdisBuffer;                                   \
    NdisQueryPacket((_packet), NULL, NULL, &NdisBuffer, NULL); \
    NdisAdjustBufferLength(NdisBuffer, (_length));             \
    NdisRecalculatePacketCounts(_packet);                      \
}

#endif // ifdef _STMAC_