forked from erikarn/LinBPQ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
L2Code.c
3006 lines (2132 loc) · 60.2 KB
/
L2Code.c
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2001-2015 John Wiseman G8BPQ
This file is part of LinBPQ/BPQ32.
LinBPQ/BPQ32 is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LinBPQ/BPQ32 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with LinBPQ/BPQ32. If not, see http://www.gnu.org/licenses
*/
//
// C replacement for L2Code.asm
//
#define Kernel
#define _CRT_SECURE_NO_DEPRECATE
#define _USE_32BIT_TIME_T
#pragma data_seg("_BPQDATA")
#include "time.h"
#include "stdio.h"
#include "CHeaders.h"
#define UI 3
#define SABM 0x2F
#define DISC 0x43
#define DM 0x0F
#define UA 0x63
#define FRMR 0x87
#define RR 1
#define RNR 5
#define REJ 9
#define PFBIT 0x10 // POLL/FINAL BIT IN CONTROL BYTE
#define REJSENT 1 // SET WHEN FIRST REJ IS SENT IN REPLY
// TO AN I(P)
#define RNRSET 2 // RNR RECEIVED FROM OTHER END
#define DISCPENDING 8 // SEND DISC WHEN ALL DATA ACK'ED
#define RNRSENT 0x10 // WE HAVE SEND RNR
#define POLLSENT 0x20 // POLL BIT OUTSTANDING
#define ONEMINUTE 60*3
#define TENSECS 10*3
#define THREESECS 3*3
VOID L2SENDCOMMAND();
VOID L2ROUTINE();
MESSAGE * SETUPL2MESSAGE(struct _LINKTABLE * LINK, UCHAR CMD);
VOID SendSupervisCmd(struct _LINKTABLE * LINK);
void SEND_RR_RESP(struct _LINKTABLE * LINK, UCHAR PF);
VOID L2SENDRESPONSE(struct _LINKTABLE * LINK, int CMD);
VOID L2SENDCOMMAND(struct _LINKTABLE * LINK, int CMD);
VOID ACKMSG(struct _LINKTABLE * LINK);
VOID InformPartner(struct _LINKTABLE * LINK, int Reason);
UINT RR_OR_RNR(struct _LINKTABLE * LINK);
VOID L2TIMEOUT(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT);
VOID CLEAROUTLINK(struct _LINKTABLE * LINK);
VOID SENDFRMR(struct _LINKTABLE * LINK);
char * SetupNodeHeader(struct DATAMESSAGE * Buffer);
VOID CLEARSESSIONENTRY(TRANSPORTENTRY * Session);
VOID SDFRMR(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT);
VOID SDNRCHK(struct _LINKTABLE * LINK, UCHAR CTL);
VOID RESETNS(struct _LINKTABLE * LINK, UCHAR NS);
VOID PROC_I_FRAME(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESSAGE * Buffer);
VOID RESET2X(struct _LINKTABLE * LINK);
VOID RESET2(struct _LINKTABLE * LINK);
VOID CONNECTREFUSED(struct _LINKTABLE * LINK);
VOID SDUFRM(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESSAGE * Buffer, UCHAR CTL);
VOID SFRAME(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, UCHAR CTL, UCHAR MSGFLAG);
VOID SDIFRM(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESSAGE * Buffer, UCHAR CTL, UCHAR MSGFLAG);
VOID SENDCONNECTREPLY(struct _LINKTABLE * LINK);
VOID SETUPNEWL2SESSION(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESSAGE * Buffer, UCHAR MSGFLAG);
BOOL FindNeighbour(UCHAR * Call, int Port, struct ROUTE ** REQROUTE);
VOID L2SABM(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESSAGE * Buffer, MESSAGE * ADJBUFFER, UCHAR MSGFLAG);
VOID L2SENDUA(struct PORTCONTROL * PORT, MESSAGE * Buffer, MESSAGE * ADJBUFFER);
VOID L2SENDDM(struct PORTCONTROL * PORT, MESSAGE * Buffer, MESSAGE * ADJBUFFER);
VOID L2SENDRESP(struct PORTCONTROL * PORT, MESSAGE * Buffer, MESSAGE * ADJBUFFER, UCHAR CTL);
int COUNTLINKS(int Port);
VOID L2_PROCESS(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESSAGE * Buffer, UCHAR CTL, UCHAR MSGFLAG);
TRANSPORTENTRY * SetupSessionForL2(struct _LINKTABLE * LINK);
BOOL cATTACHTOBBS(TRANSPORTENTRY * Session, UINT Mask, int Paclen, int * AnySessions);
VOID PUT_ON_PORT_Q(struct PORTCONTROL * PORT, MESSAGE * Buffer);
VOID L2SWAPADDRESSES(MESSAGE * Buffer);
BOOL FindLink(UCHAR * LinkCall, UCHAR * OurCall, int Port, struct _LINKTABLE ** REQLINK);
VOID SENDSABM(struct _LINKTABLE * LINK);
VOID __cdecl Debugprintf(const char * format, ...);
VOID Q_IP_MSG(MESSAGE * Buffer);
VOID PROCESSNODEMESSAGE(MESSAGE * Msg, struct PORTCONTROL * PORT);
VOID L2LINKACTIVE(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESSAGE * Buffer, MESSAGE * ADJBUFFER, UCHAR CTL, UCHAR MSGFLAG);
BOOL CompareAliases(UCHAR * c1, UCHAR * c2);
VOID L2FORUS(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESSAGE * Buffer, MESSAGE * ADJBUFFER, UCHAR CTL, UCHAR MSGFLAG);
VOID Digipeat(struct PORTCONTROL * PORT, MESSAGE * Buffer, UCHAR * OurCall, int toPort, int UIOnly);
VOID DigiToMultiplePorts(struct PORTCONTROL * PORTVEC, PMESSAGE Msg);
VOID MHPROC(struct PORTCONTROL * PORT, MESSAGE * Buffer);
BOOL CheckForListeningSession(struct PORTCONTROL * PORT, MESSAGE * Msg);
extern int REALTIMETICKS;
// MSGFLAG contains CMD/RESPONSE BITS
#define CMDBIT 4 // CURRENT MESSAGE IS A COMMAND
#define RESP 2 // CURRENT MSG IS RESPONSE
#define VER1 1 // CURRENT MSG IS VERSION 1
// FRMR REJECT FLAGS
#define SDINVC 1 // INVALID COMMAND
#define SDNRER 8 // INVALID N(R)
UCHAR NO_CTEXT = 0;
UCHAR ALIASMSG = 0;
extern UINT APPLMASK;
static UCHAR ISNETROMMSG = 0;
UCHAR MSGFLAG = 0;
extern UCHAR * ALIASPTR;
UCHAR QSTCALL[7] = {'Q'+'Q','S'+'S','T'+'T',0x40,0x40,0x40,0xe0}; // QST IN AX25
UCHAR NODECALL[7] = {0x9C, 0x9E, 0x88, 0x8A, 0xA6, 0x40, 0xE0}; // 'NODES' IN AX25 FORMAT
VOID L2Routine(struct PORTCONTROL * PORT, MESSAGE * Buffer)
{
// LEVEL 2 PROCESSING
MESSAGE * ADJBUFFER;
struct _LINKTABLE * LINK;
APPLCALLS * APPL;
UCHAR * ptr;
int n;
UCHAR CTL;
UINT Work;
UCHAR c;
// Check for invalid length (< 22 7Header + 7Addr + 7Addr + CTL
if (Buffer->LENGTH < 22)
{
Debugprintf("BPQ32 Bad L2 Msg Port %d Len %d", PORT->PORTNUMBER, Buffer->LENGTH);
ReleaseBuffer(Buffer);
return;
}
PORT->L2FRAMES++;
ALIASMSG = 0;
APPLMASK = 0;
ISNETROMMSG = 0;
MSGFLAG = 0; // CMD/RESP UNDEFINED
// Check for Corrupted Callsign in Origin (to keep MH list clean)
ptr = &Buffer->ORIGIN[0];
n = 6;
while(n--)
{
// Try a bit harder to detect corruption
c = *(ptr++);
if (c & 1)
{
ReleaseBuffer(Buffer);
return;
}
c = c >> 1;
if (!isalnum(c) && !(c == '#') && !(c == ' '))
{
ReleaseBuffer(Buffer);
return;
}
}
// Check Digis if present
if ((Buffer->ORIGIN[6] & 1) == 0) // Digis
{
ptr = &Buffer->CTL;
n = 6;
while(n--)
{
c = *(ptr++);
if (c & 1)
{
ReleaseBuffer(Buffer);
return;
}
c = c >> 1;
if (!isalnum(c) && !(c == '#') && !(c == ' '))
{
ReleaseBuffer(Buffer);
return;
}
}
}
BPQTRACE(Buffer, TRUE); // TRACE - RX frames to APRS
if (PORT->PORTMHEARD)
MHPROC(PORT, Buffer);
// CHECK THAT ALL DIGIS HAVE BEEN ACTIONED,
// AND ADJUST FOR DIGIPEATERS IF PRESENT
n = 8; // MAX DIGIS
ptr = &Buffer->ORIGIN[6]; // End of Address bit
while ((*ptr & 1) == 0)
{
// MORE TO COME
ptr += 7;
if ((*ptr & 0x80) == 0) // Digi'd bit
{
// FRAME HAS NOT BEEN REPEATED THROUGH CURRENT DIGI -
// SEE IF WE ARE MEANT TO DIGI IT
struct XDIGI * XDigi = PORT->XDIGIS; // Cross port digi setup
ptr -= 6; // To start of Call
if (CompareCalls(ptr, MYCALL) || CompareAliases(ptr, MYALIAS) ||
CompareCalls(ptr, PORT->PORTALIAS) || CompareCalls(ptr, PORT->PORTALIAS2))
{
Digipeat(PORT, Buffer, ptr, 0, 0); // Digi it (if enabled)
return;
}
while (XDigi)
{
if (CompareCalls(ptr, XDigi->Call))
{
Digipeat(PORT, Buffer, ptr, XDigi->Port, XDigi->UIOnly); // Digi it (if enabled)
return;
}
XDigi = XDigi->Next;
}
ReleaseBuffer(Buffer);
return; // not complete and not for us
}
n--;
if (n == 0)
{
ReleaseBuffer(Buffer);
return; // Corrupt - no end of address bit
}
}
// Reached End of digis, and all actioned, so can process it
Work = (UINT)&Buffer->ORIGIN[6];
ptr -= Work; // ptr is now length of digis
Work = (UINT)Buffer;
ptr += Work;
ADJBUFFER = (MESSAGE * )ptr; // ADJBUFFER points to CTL, etc. allowing for digis
// GET CMD/RESP BITS
if (Buffer->DEST[6] & 0x80)
{
if (Buffer->ORIGIN[6] & 0x80) // Both set, assume V1
MSGFLAG |= VER1;
else
MSGFLAG |= CMDBIT;
}
else
{
if (Buffer->ORIGIN[6] & 0x80) // Only Dest Set
MSGFLAG |= RESP;
else
MSGFLAG |= VER1; // Neither, assume V1
}
// SEE IF FOR AN ACTIVE LINK SESSION
CTL = ADJBUFFER->CTL;
// IF A UI, THERE IS NO SESSION
if (FindLink(Buffer->ORIGIN, Buffer->DEST, PORT->PORTNUMBER, &LINK))
{
L2LINKACTIVE(LINK, PORT, Buffer,ADJBUFFER, CTL, MSGFLAG);
return;
}
// NOT FOR ACTIVE LINK - SEE IF ADDRESSED TO OUR ADDRESSES
ISNETROMMSG = PORT->PORTL3FLAG; // Set if L3 only and call to NETROMCALL
if (CompareCalls(Buffer->DEST, NETROMCALL))
goto FORUS;
if (PORT->PORTL3FLAG) // L3 Only Port?
goto NOTFORUS; // If L3ONLY, only accept calls to NETROMCALL
ISNETROMMSG = 0;
// FIRST TRY PORT ADDR/ALIAS
if(PORT->PORTBBSFLAG == 1)
goto PORTCALLISBBS; // PORT CALL/ALIAS ARE FOR BBS
if (NODE)
goto USING_NODE;
PORTCALLISBBS:
// NODE IS NOT ACTIVE, SO PASS CALLS TO PORTCALL/ALIAS TO BBS
APPLMASK = 1;
USING_NODE:
if (CompareCalls(Buffer->DEST, PORT->PORTCALL))
goto FORUS;
ALIASMSG = 1;
if (CompareAliases(Buffer->DEST, PORT->PORTALIAS)) // only compare 6 bits - allow any ssid
goto FORUS;
if (NODE == 0)
goto TRYBBS; // NOT USING NODE SYSTEM
ALIASMSG = 0;
if (CompareCalls(Buffer->DEST, MYCALL))
goto FORUS;
ALIASMSG = 1;
if (CompareAliases(Buffer->DEST, MYALIAS)) // only compare 6 bits - allow any ssid
goto FORUS;
// WE MAY NOT BE ALLOWED TO USE THE BBS CALL ON SOME BANDS DUE TO
// THE RATHER ODD UK LICENCING RULES!
if (PORT->BBSBANNED == 1)
goto NOWTRY_NODES;
TRYBBS:
if (BBS == 0)
goto NOWTRY_NODES; // NOT USING BBS CALLS
// TRY APPLICATION CALLSIGNS/ALIASES
APPLMASK = 1;
ALIASPTR = &CMDALIAS[0][0];
n = NumberofAppls;
APPL = APPLCALLTABLE;
while (n--)
{
if (APPL->APPLCALL[0] > 0x40) // Valid ax.25 addr
{
ALIASMSG = 0;
if (CompareCalls(Buffer->DEST, APPL->APPLCALL))
goto FORUS;
ALIASMSG = 1;
if (CompareAliases(Buffer->DEST, APPL->APPLALIAS)) // only compare 6 bits - allow any ssid
goto FORUS;
if (CompareAliases(Buffer->DEST, APPL->L2ALIAS)) // only compare 6 bits - allow any ssid
goto FORUS;
}
APPLMASK <<= 1;
ALIASPTR += ALIASLEN;
APPL++;
}
// NOT FOR US - SEE IF 'NODES' OR IP/ARP BROADCAST MESSAGE
NOWTRY_NODES:
if (CompareCalls(Buffer->DEST, QSTCALL))
{
Q_IP_MSG(Buffer); // IP BROADCAST
return;
}
if (ADJBUFFER->PID != 0xCF) // NETROM MSG?
goto NOTFORUS; // NO
if (CompareCalls(Buffer->DEST, NODECALL))
{
if (Buffer->L2DATA[0] == 0xff) // Valid NODES Broadcast
{
PROCESSNODEMESSAGE(Buffer, PORT);
}
}
ReleaseBuffer(Buffer);
return;
NOTFORUS:
//
// MAY JUST BE A REPLY TO A 'PRIMED' CQ CALL
//
if ((CTL & ~PFBIT) == SABM)
if (CheckForListeningSession(PORT, Buffer))
return; // Used buffer to send UA
ReleaseBuffer(Buffer);
return;
FORUS:
// if a UI frame and UIHook Specified, call it
if (PORT->UIHook && CTL == 3)
PORT->UIHook(LINK, PORT, Buffer, ADJBUFFER, CTL, MSGFLAG);
L2FORUS(LINK, PORT, Buffer, ADJBUFFER, CTL, MSGFLAG);
}
VOID MHPROC(struct PORTCONTROL * PORT, MESSAGE * Buffer)
{
PMHSTRUC MH = PORT->PORTMHEARD;
PMHSTRUC MHBASE = MH;
int i;
int OldCount = 0;
char DIGI = '*';
// if (Buffer->ORIGIN[6] & 1)
DIGI = 0; // DOn't think we wnat to do this
// See if in list
for (i = 0; i < MHENTRIES; i++)
{
if ((MH->MHCALL[0] == 0) || (CompareCalls(Buffer->ORIGIN, MH->MHCALL) && MH->MHDIGI == DIGI)) // Spare or our entry
{
OldCount = MH->MHCOUNT;
goto DoMove;
}
MH++;
}
// TABLE FULL AND ENTRY NOT FOUND - MOVE DOWN ONE, AND ADD TO TOP
i = MHENTRIES - 1;
// Move others down and add at front
DoMove:
if (i != 0) // First
memmove(MHBASE + 1, MHBASE, i * sizeof(MHSTRUC));
memcpy (MHBASE->MHCALL, Buffer->ORIGIN, 7 * 9); // Save Digis
MHBASE->MHDIGI = DIGI;
MHBASE->MHTIME = time(NULL);
MHBASE->MHCOUNT = ++OldCount;
return;
}
int CountFramesQueuedOnSession(TRANSPORTENTRY * Session)
{
// COUNT NUMBER OF FRAMES QUEUED ON A SESSION
if (Session == 0)
return 0;
if (Session->L4CIRCUITTYPE & BPQHOST)
{
return C_Q_COUNT(&Session->L4TX_Q);
}
if (Session->L4CIRCUITTYPE & SESSION)
{
// L4 SESSION - GET NUMBER UNACKED, AND ADD NUMBER ON TX QUEUE
int Count = C_Q_COUNT(&Session->L4TX_Q);
UCHAR Unacked = Session->TXSEQNO - Session->L4WS;
return Count + Unacked;
}
if (Session->L4CIRCUITTYPE & PACTOR)
{
// PACTOR Type - Frames are queued on the Port Entry
struct PORTCONTROL * PORT = Session->L4TARGET.PORT;
EXTPORTDATA * EXT = (EXTPORTDATA *)PORT;
int ret = EXT->FramesQueued;
// Check L4 Queue as messages can stay there briefly
ret += C_Q_COUNT(&Session->L4RX_Q);
return ret + C_Q_COUNT(&PORT->PORTTX_Q);
}
// L2 CIRCUIT
{
int SessCount = C_Q_COUNT(&Session->L4TX_Q);
struct _LINKTABLE * LINK = Session->L4TARGET.LINK;
int L2 = COUNT_AT_L2(LINK);
return SessCount + L2;
}
}
int CHECKIFBUSYL2(TRANSPORTENTRY * Session)
{
// RETURN TOP BIT OF AL SET IF SESSION PARTNER IS BUSY
if (Session->L4CROSSLINK) // CONNECTED?
{
Session = Session->L4CROSSLINK;
if (CountFramesQueuedOnSession(Session) > 10)
return L4BUSY;;
}
return 0;
}
VOID L2FORUS(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESSAGE * Buffer, MESSAGE * ADJBUFFER, UCHAR CTL, UCHAR MSGFLAG)
{
// MESSAGE ADDRESSED TO OUR CALL OR ALIAS, BUT NOT FOR AN ACTIVE SESSION
struct ROUTE * ROUTE;
int CTLlessPF = CTL & ~PFBIT;
PORT->L2FRAMESFORUS++;
InOctets[PORT->PORTNUMBER] += Buffer->LENGTH - 7;
NO_CTEXT = 0;
// ONLY SABM or UI ALLOWED IF NO SESSION
if (CTLlessPF == 3) // UI
{
// A UI ADDRESSED TO US - SHOULD ONLY BE FOR IP, or possibly addressed NODES
switch(ADJBUFFER->PID)
{
case 0xcf: // Netrom
if (Buffer->L2DATA[0] == 0xff) // NODES
PROCESSNODEMESSAGE(Buffer, PORT);
break;
case 0xcc: // TCP
case 0xcd: // ARP
case 0x08: // NOS FRAGMENTED AX25 TCP/IP
Q_IP_MSG( Buffer);
return;
}
ReleaseBuffer(Buffer);
return;
}
if (PORT->PortUIONLY) // Port is for UI only
{
ReleaseBuffer(Buffer);
return;
}
if (CTLlessPF != SABM)
{
if ((MSGFLAG & CMDBIT) && (CTL & PFBIT)) // Command with P?
L2SENDDM(PORT, Buffer, ADJBUFFER);
else
ReleaseBuffer(Buffer); // Ignore if not
return;
}
#ifdef BLACKBITS
;
; CHECK BLACKLIST
;
EXTRN CHECKBLACKLIST:NEAR
MOV EDI,_BUFFER
LEA ESI,MSGORIGIN[EDI] ; ORIGIN FROM MESSAGE
CALL CHECKBLACKLIST ; RETURN Z IF _NODE IS IN LIST
JZ SHORT IGNORESABM ; DONT ACCEPT
#endif
// IF WE HAVE A PERMITTED CALLS LIST, SEE IF HE IS IN IT
if (PORT->PERMITTEDCALLS)
{
UCHAR * ptr = PORT->PERMITTEDCALLS;
while (TRUE)
{
if (memcmp(Buffer->ORIGIN, ptr, 6) == 0) // Ignore SSID
break;
ptr += 7;
if ((*ptr) == 0) // Not in list
{
ReleaseBuffer(Buffer);
return;
}
}
}
// IF CALL REQUEST IS FROM A LOCKED NODE WITH QUALITY ZERO, IGNORE IT
if (FindNeighbour(Buffer->ORIGIN, PORT->PORTNUMBER, &ROUTE))
{
// From a known node
NO_CTEXT = 1;
if (ROUTE->NEIGHBOUR_FLAG == 1 && ROUTE->NEIGHBOUR_QUAL == 0) // Locked, qual 0
{
ReleaseBuffer(Buffer);
return;
}
}
// CHECK PORT CONNECT LIMITS
if (PORT->USERS)
{
if (COUNTLINKS(PORT->PORTNUMBER) >= PORT->USERS)
{
L2SENDDM(PORT, Buffer, ADJBUFFER);
return;
}
}
L2SABM(LINK, PORT, Buffer, ADJBUFFER, MSGFLAG); // Process the SABM
}
int COUNTLINKS(int Port)
{
//COUNT LINKS ON PORT
int i = MAXLINKS, n = 0;
struct _LINKTABLE * LINK = LINKS;
while (i--)
{
if (LINK->LINKPORT && LINK->LINKPORT->PORTNUMBER == Port)
n++;
LINK++;
}
return n;
}
VOID L2LINKACTIVE(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESSAGE * Buffer, MESSAGE * ADJBUFFER, UCHAR CTL, UCHAR MSGFLAG)
{
// MESSAGE ON AN ACTIVE LINK
int CTLlessPF = CTL & ~PFBIT;
PORT->L2FRAMESFORUS++;
InOctets[PORT->PORTNUMBER] += Buffer->LENGTH - 7;
// ONLY SABM or UI ALLOWED IF NO SESSION
if (CTLlessPF == 3) // UI
{
// A UI ADDRESSED TO US - SHOULD ONLY BE FOR IP, or possibly addressed NODES
switch(ADJBUFFER->PID)
{
case 0xcf: // Netrom
if (Buffer->L2DATA[0] == 0xff) // NODES
PROCESSNODEMESSAGE(Buffer, PORT);
break;
case 0xcc: // TCP
case 0xcd: // ARP
case 0x08: // NOS FRAGMENTED AX25 TCP/IP
Q_IP_MSG( Buffer);
return;
}
ReleaseBuffer(Buffer);
return;
}
if (CTLlessPF == DISC)
{
InformPartner(LINK, NORMALCLOSE); // SEND DISC TO OTHER END
CLEAROUTLINK(LINK);
L2SENDUA(PORT, Buffer, ADJBUFFER);
return;
}
if (CTLlessPF == SABM)
{
// SABM ON EXISTING SESSION - IF DISCONNECTING, REJECT
if (LINK->L2STATE == 4) // DISCONNECTING?
{
L2SENDDM(PORT, Buffer, ADJBUFFER);
return;
}
// THIS IS A SABM ON AN EXISTING SESSION
// THERE ARE SEVERAL POSSIBILITIES:
// 1. RECONNECT COMMAND TO TNC
// 2. OTHER END THINKS LINK HAS DIED
// 3. RECOVERY FROM FRMR CONDITION
// 4. REPEAT OF ORIGINAL SABM COS OTHER END MISSED UA
// FOR 1-3 IT IS REASONABLE TO FULLY RESET THE CIRCUIT, BUT IN 4
// SUCH ACTION WILL LOSE THE INITIAL SIGNON MSG IF CONNECTING TO A
// BBS. THE PROBLEM IS TELLING THE DIFFERENCE. I'M GOING TO SET A FLAG
// WHEN FIRST INFO RECEIVED - IF SABM REPEATED BEFORE THIS, I'LL ASSUME
// CONDITION 4, AND JUST RESEND THE UA
if (LINK->SESSACTIVE == 0) // RESET OF ACTIVE CIRCUIT?
{
L2SENDUA(PORT, Buffer, ADJBUFFER); // No, so repeat UA
return;
}
InformPartner(LINK, NORMALCLOSE); // SEND DISC TO OTHER END
L2SABM(LINK, PORT, Buffer, ADJBUFFER, MSGFLAG); // Process the SABM
return;
}
L2_PROCESS(LINK, PORT, Buffer, CTL, MSGFLAG);
}
VOID L2SABM(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESSAGE * Buffer, MESSAGE * ADJBUFFER, UCHAR MSGFLAG)
{
// SET UP NEW SESSION (OR RESET EXISTING ONE)
TRANSPORTENTRY * Session;
int CONERROR;
if (LINK == 0) // NO LINK ENTRIES - SEND DM RESPONSE
{
L2SENDDM(PORT, Buffer, ADJBUFFER);
return;
}
SETUPNEWL2SESSION(LINK, PORT, Buffer, MSGFLAG);
if (LINK->L2STATE != 5) // Setup OK?
{
L2SENDDM(PORT, Buffer, ADJBUFFER); // Failed
return;
}
// IF CONNECT TO APPL ADDRESS, SET UP APPL SESSION
if (APPLMASK == 0)
{
// Not ATTACH TO APPL
// Send CTEXT if connect to NODE/Port Alias, or NODE/Port Call, and FULL_CTEXT set
// Dont sent to known NODEs, or appl connects
L2SENDUA(PORT, Buffer, ADJBUFFER);
if (NO_CTEXT == 1)
return;
if (CTEXTLEN && (FULL_CTEXT == 1 || ALIASMSG)) // Any connect, or call to alias
{
struct DATAMESSAGE * Msg;
int Totallen = CTEXTLEN;
int Paclen= PORT->PORTPACLEN;
UCHAR * ptr = CTEXTMSG;
if (Paclen == 0)
Paclen = PACLEN;
while(Totallen)
{
Msg = GetBuff();
if (Msg == NULL)
break; // No Buffers
Msg->PID = 0xf0;
if (Paclen > Totallen)
Paclen = Totallen;
memcpy(Msg->L2DATA, ptr, Paclen);
Msg->LENGTH = Paclen + MSGHDDRLEN + 1;
C_Q_ADD(&LINK->TX_Q, Msg);
ptr += Paclen;
Totallen -= Paclen;
}
}
return;
}
// Connnect to APPL
if (LINK->LINKTYPE != 1)
{
L2SENDUA(PORT, Buffer, ADJBUFFER); // RESET OF DOWN/CROSSLINK
return;
}
if (LINK->CIRCUITPOINTER)
{
L2SENDUA(PORT, Buffer, ADJBUFFER); // ALREADY SET UP - MUST BE REPEAT OF SABM OR LINK RESET
return;
}
// IF RUNNING ONLY BBS (NODE=0), THIS MAY BE EITHER A USER OR NODE
// TRYING TO SET UP A L4 CIRCUIT - WE DONT WANT TO ATTACH A NODE TO
// THE BBS!
if (NODE == 0)
{
// NOW THINGS GET DIFICULT - WE MUST EITHER WAIT TO SEE IF A PID CF MSG
// ARRIVES, OR ASSUME ALL NODES ARE IN NEIGHBOURS - I'LL TRY THE LATTER
// AND SEE HOW IT GOES. tHIS MEANS THAT YOU MUST DEFINE ALL ROUTES
// IN CONFIG FILE
struct ROUTE * ROUTE;
if (FindNeighbour(Buffer->ORIGIN, PORT->PORTNUMBER, &ROUTE))
{
// It's a node
L2SENDUA(PORT, Buffer, ADJBUFFER); // ALREADY SET UP - MUST BE REPEAT OF SABM OR LINK RESET
return;
}
}
Session = SetupSessionForL2(LINK); // CREATE INCOMING L4 SESSION
if (Session == NULL)
{
CLEAROUTLINK(LINK);
L2SENDDM(PORT, Buffer, ADJBUFFER);
return;
}
// NOW TRY A BBS CONNECT
// IF APPL CONNECT, SEE IF APPL HAS AN ALIAS
if (ALIASPTR[0] > ' ')
{
struct DATAMESSAGE * Msg;
// ACCEPT THE CONNECT, THEN INVOKE THE ALIAS
L2SENDUA(PORT, Buffer, ADJBUFFER);
Msg = GetBuff();
if (Msg)
{
Msg->PID = 0xf0;
memcpy(Msg->L2DATA, ALIASPTR, ALIASLEN);
Msg->L2DATA[ALIASLEN] = 13;
Msg->LENGTH = MSGHDDRLEN + ALIASLEN + 2; // 2 for PID and CR
C_Q_ADD(&LINK->RX_Q, Msg);
}
return;
}
if (cATTACHTOBBS(Session, APPLMASK, PORT->PORTPACLEN, &CONERROR) == 0)
{
// NO BBS AVAILABLE
CLEARSESSIONENTRY(Session);
CLEAROUTLINK(LINK);
L2SENDDM(PORT, Buffer, ADJBUFFER);
return;
}
L2SENDUA(PORT, Buffer, ADJBUFFER);
}
VOID SETUPNEWL2SESSION(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESSAGE * Buffer, UCHAR MSGFLAG)
{
// COPY ADDRESS INFO TO LINK TABLE
UCHAR * ptr1, * ptr2;
UCHAR TEMPDIGI[57];
int n;
memcpy(LINK->LINKCALL, Buffer->ORIGIN, 7);
LINK->LINKCALL[6] &= 0x1e; // Mask SSID
memcpy(LINK->OURCALL, Buffer->DEST, 7);
LINK->OURCALL[6] &= 0x1e; // Mask SSID
memset(LINK->DIGIS, 0, 56); // CLEAR DIGI FIELD IN CASE RECONNECT
LINK->L2TIME = PORT->PORTT1; // Set tomeoiut for no digis
if ((Buffer->ORIGIN[6] & 1) == 0) // End of Address
{
// THERE ARE DIGIS TO PROCESS - COPY TO WORK AREA reversed, THEN COPY BACK
memset(TEMPDIGI, 0, 57); // CLEAR DIGI FIELD IN CASE RECONNECT
ptr1 = &Buffer->ORIGIN[6]; // End of add
ptr2 = &TEMPDIGI[7 * 7]; // Last Temp Digi
while((*ptr1 & 1) == 0) // End of address bit
{
ptr1++;
memcpy(ptr2, ptr1, 7);
ptr2[6] &= 0x1e; // Mask Repeated and Last bits
ptr2 -= 7;
ptr1 += 6;
}
// LIST OF DIGI CALLS COMPLETE - COPY TO LINK CONTROL ENTRY
n = PORT->PORTMAXDIGIS;
ptr1 = ptr2 + 7; // First in TEMPDIGIS
ptr2 = &LINK->DIGIS[0];
while (*ptr1)
{
if (n == 0)
{
// Too many for us
CLEAROUTLINK(LINK);
return;
}
memcpy(ptr2, ptr1, 7);
ptr1 += 7;
ptr2 += 7;
n--;