summaryrefslogblamecommitdiffstats
path: root/private/ntos/dlc/llcaddr.c
blob: 1a6c22e72f49009cc1c4c8edd535846d98c9e20b (plain) (tree)
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































































































































































































































































































































































































































































































































































































































































































































                                                                                                     
/*++

Copyright (c) 1991  Microsoft Corporation

Module Name:

    llcaddr.c

Abstract:

    This module provides a network independent interface to build
    lan headers for the sent and received frames.
    This is a re-entarnt library module having no access to the data
    structures of data link module.

    Contents:
        LlcBuildAddress
        LlcBuildAddressFromLanHeader
        LlcGetMaxInfoField
        LlcCopyReceivedLanHeader
        CopyLanHeader

Author:

    Antti Saarenheimo (o-anttis) 30-MAY-1991

Revision History:

--*/

#include <llc.h>

#define FDDI_FC 0x57    // Frame Control: async, 48-bit address, LLC, pri 7

static USHORT ausLongestFrameTranslation[8] = {
    516,
    1470,
    2052,
    4472,
    8144,
    11407,
    17800,
    17800
};


UINT
LlcBuildAddress(
    IN NDIS_MEDIUM NdisMedium,
    IN PUCHAR DestinationAddress,
    IN PVOID pSrcRouting,
    IN OUT PUCHAR pLanHeader
    )

/*++

Routine Description:

    The primitive implements a network independent handling of
    the address information in the LAN header. It builds LAN a
    802.3, DIX or 802.5 header from an address and an optional source
    routing info. The source SAP number and the current node address are
    provided by the link driver.

Arguments:

    NdisMedium          - medium that MAC talks over
    DestinationAddress  - LAN destination address (or a broadcast/multicast/
                          functional address).
    pSrcRouting         - optional source routing information. Must be set
                          NULL when not used.
    pLanHeader          - buffer provided by the upper protocol for the
                          address information of the frame header.

Return Value:

    length of the LAN header

--*/

{
    UCHAR SourceRoutingIndicator = 0x01;
    UINT minimumLanHeader = 14;

    if (NdisMedium == NdisMediumFddi) {
        pLanHeader[0] = FDDI_FC;
        ++pLanHeader;
        minimumLanHeader = 13;
    } else if (NdisMedium == NdisMedium802_5) {
        pLanHeader[0] = 0;          // DEFAULT_TR_ACCESS;
        pLanHeader[1] = 0x40;       // NON_MAC_FRAME;
        pLanHeader += 2;
        SourceRoutingIndicator = 0x80;
    } else {

        //
        // We build always the DIX ethernet header, even if it is not used
        // when the link works in 802.3.  IN the automatic mode
        // we need just to copy the lan header length from all received U-
        // command frames and we are using the same mode as the other side.
        // However, there is a problem: the remote node may response
        // the SAMBE request with Rxx or even with I-frame, but we
        // don't want to put any extra checking on our main code path
        // (U- command frames would definitely be outside of it).
        // The other side will send RR- if it cannot get any acknowledge
        // from us, and after a while it closes the link.
        //-----
        // We considered the using of connect timeout to change the
        // ethernet mode, but that might have procedured too long waithing
        // times (something 1s and 30s)
        //-----
        // Usually the client tries to connect to the host and the client
        // sends first the data.  We may also assume, that IBM host machines
        // use the standard LLC implementations (SABME is always acknowledged
        // by UA)
        //

        pLanHeader[12] = 0x80;
        pLanHeader[13] = 0xD5;
    }
    LlcMemCpy(pLanHeader, DestinationAddress, 6);

    //
    // We must support the source routing information also for
    // ethernet, because the underlaying network could be token-ring.
    // The source routing bit must be a local variable, because it
    // is different in token-ring and ethernet
    //

    if (!pSrcRouting) {
        pLanHeader[6] &= ~SourceRoutingIndicator;
        return minimumLanHeader;
    } else {
        pLanHeader[6] |= SourceRoutingIndicator;
        pLanHeader[0] &= ~SourceRoutingIndicator;

        //
        // 5 Lowest bits are the source routing information length.
        //

        if (*((PUCHAR)pSrcRouting) & 0x1f > MAX_TR_SRC_ROUTING_INFO) {
            return DLC_STATUS_INVALID_ROUTING_INFO;
        }
        LlcMemCpy(&pLanHeader[12],
                  pSrcRouting,
                  *((PUCHAR)pSrcRouting) & 0x1f      // Length
                  );
        return minimumLanHeader + *((PUCHAR)pSrcRouting) & 0x1f;
    }
}


UINT
LlcBuildAddressFromLanHeader(
    IN NDIS_MEDIUM NdisMedium,
    IN PUCHAR pRcvLanHeader,
    IN OUT PUCHAR pLanHeader
    )

/*++

Routine Description:

    The routine builds a transmit lan header form the received frame and
    returns the frames header length, nul if invalid address

Arguments:

    NdisMedium      - Ndis menium of the address information
    pRcvFrameHeader - pointer to any frame received from the network.
                      The buffer must include the whole LAN header of the frame
                      including the LLC header.
    pLanHeader      - buffer to receive the information (minimum size is 32 bytes)

Return Value:

    None

--*/

{
    UINT LlcOffset;

    if (NdisMedium == NdisMedium802_3) {

#ifndef SUPPORT_ETHERNET_CLIENT

        LlcOffset = LlcBuildAddress(NdisMedium,
                                    &pRcvLanHeader[6],
                                    NULL,
                                    pLanHeader
                                    );

#else

        LlcOffset = LlcBuildAddress(NdisMedium,
                                    &pRcvLanHeader[6],
                                    (pRcvLanHeader[6] & 0x01)
                                        ? &pRcvLanHeader[12]
                                        : NULL,
                                    pLanHeader
                                    );

        //
        // We must swap the direction bit and reset the possible
        // broadcast type in the source routing header.
        //

        if (pLanHeader[6] & 0x01) {
            pLanHeader[12] &= 0x1f;
            pLanHeader[13] ^= 0x80;
        }

#endif

    } else if (NdisMedium == NdisMediumFddi) {
        LlcOffset = LlcBuildAddress(NdisMedium,
                                    &pRcvLanHeader[7],
                                    NULL,   // no source routing in FDDI
                                    pLanHeader
                                    );
    } else {
        LlcOffset = LlcBuildAddress(NdisMedium,
                                    &pRcvLanHeader[8],
                                    (pRcvLanHeader[8] & 0x80)
                                        ? &pRcvLanHeader[14]
                                        : NULL,
                                    pLanHeader
                                    );

        //
        // We must swap the direction bit and reset the possible broadcast type
        //

        if (pLanHeader[8] & 0x80) {
            pLanHeader[14] &= 0x1f;
            pLanHeader[15] ^= 0x80;
        }
    }
    return LlcOffset;
}


USHORT
LlcGetMaxInfoField(
    IN NDIS_MEDIUM NdisMedium,
    IN PVOID hBinding,
    IN PUCHAR pLanHeader
    )

/*++

Routine Description:

    Procedure returns the maximum informatiuon field for
    the given LAN header.  It checks both the source routing
    information and the maximum packet length defined for
    the adapter and decrements the LAN and LLC headers
    from the length

Arguments:

    NdisMedium  - Ndis medium of the address information
    pBinding    - current binding context on data link driver
    pLanHeader  - any lan header

Return Value:

    Maximum information field length

--*/

{
    PUCHAR pSourceRouting;
    UCHAR LanHeaderLength = 14;
    USHORT MaxFrameSize;
    USHORT usMaxBridgeSize;

    MaxFrameSize = (USHORT)((PBINDING_CONTEXT)hBinding)->pAdapterContext->MaxFrameSize;

    //
    // We may run DLC within the DIX in ethernet network, so add 3 bytes for
    // SNA DIX LAN header and padding
    //

    if (((PBINDING_CONTEXT)hBinding)->pAdapterContext->NdisMedium == NdisMedium802_3) {
        LanHeaderLength += 3;
    }

    pSourceRouting = NULL;
    if (NdisMedium == NdisMedium802_5) {
        if (pLanHeader[8] & 0x80) {
            pSourceRouting = pLanHeader + 14;
        }
    } else if (NdisMedium == NdisMediumFddi) {
        if (pLanHeader[6] & 0x80) {
            pSourceRouting = pLanHeader + 13;
        }
    } else {
        if (pLanHeader[6] & 0x01) {
            pSourceRouting = pLanHeader + 12;
        }
    }
    if (pSourceRouting != NULL) {

        //
        // Add the source routing info length to the LAN header length
        //

        LanHeaderLength += (UCHAR)(*pSourceRouting & (UCHAR)0x1f);
        usMaxBridgeSize = ausLongestFrameTranslation[(pSourceRouting[1] & SRC_ROUTING_LF_MASK) >> 4];

        //
        // RLF 10/01/92. Ignore the 'bridge size'. This is misleading. For
        // instance, the IBM mainframe is currently sending RI of 02 80 in
        // all frames, indicating that the max I-frame it can accept/transmit
        // is 516 bytes (x000xxxx in byte #2). It later sends us a 712 byte
        // frame, 683 bytes of which are info field. We reject it (FRMR)
        //

        //
        // BUGBUG - must work this out properly
        //

        //if (MaxFrameSize > usMaxBridgeSize) {
        //    MaxFrameSize = usMaxBridgeSize;
        //}
    }
    return (USHORT)(MaxFrameSize - LanHeaderLength - sizeof(LLC_HEADER));
}


UINT
LlcCopyReceivedLanHeader(
    IN PBINDING_CONTEXT pBinding,
    IN PUCHAR DestinationAddress,
    IN PUCHAR SourceAddress
    )

/*++

Routine Description:

    Function copies and translates the received lan header to
    the address format used by the client.  By default the
    source is the current received frame.

Arguments:

    pBinding            - pointer to Binding Context
    DestinationAddress  - pointer to output destination net address
    SourceAddress       - pointer to input source net address

Return Value:

    Length of copied LAN header

--*/

{
    if (SourceAddress == NULL) {
        SourceAddress = pBinding->pAdapterContext->pHeadBuf;
    }
    if (pBinding->pAdapterContext->FrameType == LLC_DIRECT_ETHERNET_TYPE) {

        //
        // when receiving a DIX frame, the LAN header is always the 12 bytes
        // before the ethernet type field
        //

        LlcMemCpy(DestinationAddress, SourceAddress, 12);
        return 12;
    } else {

        UCHAR LanHeaderLength = 14;

        switch (pBinding->AddressTranslation) {
        case LLC_SEND_802_5_TO_802_5:

            //
            // Copy also the source routing info, if it has been defined
            //

            LlcMemCpy(DestinationAddress,
                      SourceAddress,
                      (SourceAddress[8] & 0x80)
                        ? LanHeaderLength + (SourceAddress[14] & 0x1f)
                        : LanHeaderLength
                      );
            break;

        case LLC_SEND_802_5_TO_802_3:
        case LLC_SEND_802_5_TO_DIX:
            DestinationAddress[0] = 0;          // default AC
            DestinationAddress[1] = 0x40;       // default frame type: non-MAC

            //
            // RLF 03/31/93
            //
            // we are receiving an Ethernet frame. We always reverse the bits
            // in the destination address (ie our node address). We reverse the
            // source (sender's node address) based on the SwapAddressBits flag.
            // If this is TRUE (default) then we also swap the source address
            // bits so that ethernet addresses are presented to the app in
            // standard non-canonical format. If SwapAddressBits is FALSE then
            // we leave them in canonical format (a real ethernet address) or
            // presumably the address is a Token Ring address from the other
            // side of an ethernet-token ring bridge
            //

            SwappingMemCpy(&DestinationAddress[2],
                           SourceAddress,
                           6
                           );
            SwapMemCpy(pBinding->pAdapterContext->ConfigInfo.SwapAddressBits,
                       &DestinationAddress[8],
                       SourceAddress+6,
                       6
                       );
            break;

        case LLC_SEND_802_5_TO_FDDI:
            DestinationAddress[0] = 0;
            DestinationAddress[1] = 0x40;
            SwappingMemCpy(&DestinationAddress[2],
                           SourceAddress+1,
                           6
                           );
            SwapMemCpy(pBinding->pAdapterContext->ConfigInfo.SwapAddressBits,
                       &DestinationAddress[8],
                       SourceAddress+7,
                       6
                       );
            break;

#ifdef SUPPORT_ETHERNET_CLIENT
        case LLC_SEND_802_3_TO_802_3:
        case LLC_SEND_802_3_TO_DIX:
            LlcMemCpy(DestinationAddress, SourceAddress, LanHeaderLength);
            break;

        case LLC_SEND_802_3_TO_802_5:

            //
            // check the source routing bit
            //

            SwappingMemCpy(DestinationAddress, &SourceAddress[2], 12);

            if (SourceAddress[8] & 0x80) {
                LanHeaderLength += SourceAddress[14] & 0x1f;
                LlcMemCpy(&DestinationAddress[12],
                          &SourceAddress[14],
                          SourceAddress[14] & 0x1f
                          );
            }
            break;
#endif

        case LLC_SEND_FDDI_TO_FDDI:
            DestinationAddress[0] = FDDI_FC;
            LlcMemCpy(DestinationAddress+1, SourceAddress, 12);
            return 13;

#if LLC_DBG
        default:
            LlcInvalidObjectType();
            break;
#endif

        }
        return LanHeaderLength;
    }
}


UCHAR
CopyLanHeader(
    IN UINT AddressTranslationMode,
    IN PUCHAR pSrcLanHeader,
    IN PUCHAR pNodeAddress,
    OUT PUCHAR pDestLanHeader,
    IN BOOLEAN SwapAddressBits
    )

/*++

Routine Description:

    The primitive translates the given LAN header and its type to
    a real network header and patches the local node address to
    the source address field.  It also returns the length
    of the LAN header (== offset of LLC header).

Arguments:

    AddressTranslationMode  - the network format mapping case
    pSrcLanHeader           - the initial LAN header
    pNodeAddress            - the current node address
    pDestLanHeader          - storage for the new LAN header
    SwapAddressBits         - TRUE if we will swap address bits for Ethernet/FDDI

Return Value:

    Length of the built network header

--*/

{
    UCHAR LlcOffset = 14;
    UCHAR NodeAddressOffset = 6;
    UCHAR SourceRoutingFlag = 0;

    //
    // LLC driver API supports both 802.3 (ethernet) and 802.5 (token-ring)
    // address presentation formats. The 802.3 header may include the source
    // routing information, when it is used on token-ring.
    //
    // Internally LLC supports 802.3, DIX and 802.5 networks.
    // The transport level driver needs to support only one format. It is
    // translated to the actual network header by LLC.
    // Thus we have these six address mappings.
    //

    switch (AddressTranslationMode) {
    case LLC_SEND_802_5_TO_802_5:

        //
        // TOKEN-RING 802.5 -> TOKEN-RING 802.5
        //

        NodeAddressOffset = 8;
        LlcMemCpy(pDestLanHeader, pSrcLanHeader, 8);

        //
        // set the AC & FC bytes: FC = 0x40 == LLC-level frame
        //
        // BUGBUG - THIS MAY NOT BE THE CORRECT PLACE TO DO THIS, UNLESS MAC
        //          LEVEL FRAMES CHANGE FC BACK TO 0x00 AFTER THIS ROUTINE AND
        //          BEFORE THE FRAME IS PUT ON THE WIRE
        //

        pDestLanHeader[0] = 0;      // AC = no pritority
        pDestLanHeader[1] = 0x40;   // FS = Non-MAC
        SourceRoutingFlag = pSrcLanHeader[8] & (UCHAR)0x80;
        if (SourceRoutingFlag) {

            //
            // Copy the source routing info
            //

            LlcOffset += pSrcLanHeader[14] & 0x1f;
            LlcMemCpy(&pDestLanHeader[14],
                      &pSrcLanHeader[14],
                      pSrcLanHeader[14] & 0x1f
                      );
        }
        break;

    case LLC_SEND_802_5_TO_DIX:

        //
        // TOKEN-RING -> DIX-ETHERNET
        //
        // The ethernet type is a small endiand!!
        //

        pDestLanHeader[12] = 0x80;
        pDestLanHeader[13] = 0xD5;
        LlcOffset = 17;

        //
        // **** FALL THROUGH ****
        //

    case LLC_SEND_802_5_TO_802_3:

        //
        // TOKEN-RING 802.5 -> ETHERNET 802.3
        //

        //
        // RLF 03/31/93
        //
        // Once again, we swap the bits in the destination address ONLY if the
        // SwapAddressBits flag is TRUE.
        // This *could* be a frame intended to go to an ethernet-token ring
        // bridge. The bridge may or may not swap the destination address bits
        // depending on whether there's a 'y' in the month
        //

        SwapMemCpy(SwapAddressBits,
                   pDestLanHeader,
                   &pSrcLanHeader[2],
                   6
                   );
        break;

    case LLC_SEND_802_5_TO_FDDI:
        pDestLanHeader[0] = FDDI_FC;
        SwapMemCpy(SwapAddressBits, pDestLanHeader+1, &pSrcLanHeader[2], 6);
        NodeAddressOffset = 7;
        LlcOffset = 13;
        break;

    case LLC_SEND_DIX_TO_DIX:
        LlcOffset = 12;

    case LLC_SEND_802_3_TO_802_3:

        //
        // ETHERNET 802.3 -> ETHERNET 802.3
        //

        LlcMemCpy(pDestLanHeader, pSrcLanHeader, 6);
        break;

    case LLC_SEND_802_3_TO_DIX:

        //
        // The ethernet type is a small endiand!!
        //

        pDestLanHeader[12] = 0x80;
        pDestLanHeader[13] = 0xD5;
        LlcOffset = 17;

        //
        // ETHERNET 802.3 -> DIX-ETHERNET
        //

        LlcMemCpy(pDestLanHeader, pSrcLanHeader, 6);
        break;

#ifdef SUPPORT_ETHERNET_CLIENT

    case LLC_SEND_802_3_TO_802_5:

        //
        // ETHERNET 802.3 -> TOKEN-RING 802.5
        //

        NodeAddressOffset = 8;
        pDestLanHeader[0] = 0;      // AC = no pritority
        pDestLanHeader[1] = 0x40;   // FS = Non-MAC
        SwappingMemCpy(pDestLanHeader + 2, pSrcLanHeader, 6);

        //
        // Note: Ethernet source routing info indication flag is swapped!
        //

        if (pSrcLanHeader[6] & 0x01) {
            SourceRoutingFlag = 0x80;

            //
            // Copy the source routing info, the source routing info
            // must always be in token-ring bit order (reverse)
            //

            pDestLanHeader[8] |= 0x80;
            LlcOffset += pSrcLanHeader[12] & 0x1f;
            LlcMemCpy(&pDestLanHeader[14],
                      &pSrcLanHeader[12],
                      pSrcLanHeader[12] & 0x1f
                      );
        }
        break;
#endif

    case LLC_SEND_UNMODIFIED:
        return 0;
        break;

    case LLC_SEND_FDDI_TO_FDDI:
        LlcMemCpy(pDestLanHeader, pSrcLanHeader, 7);
        NodeAddressOffset = 7;
        LlcOffset = 13;
        break;

    case LLC_SEND_FDDI_TO_802_5:
        break;

    case LLC_SEND_FDDI_TO_802_3:
        break;

#if LLC_DBG
    default:
        LlcInvalidObjectType();
        break;
#endif

    }

    //
    // copy the source address from the node address (ie. our adapter's address)
    // in the correct format for the medium (canonical or non-canonical address
    // format)
    //

    LlcMemCpy(&pDestLanHeader[NodeAddressOffset], pNodeAddress, 6);
    pDestLanHeader[NodeAddressOffset] |= SourceRoutingFlag;

    return LlcOffset;
}