forked from g8bpq/linbpq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
L2Code.c
3962 lines (2823 loc) · 84.6 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-2022 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
#pragma data_seg("_BPQDATA")
#include "time.h"
#include "stdio.h"
#include "CHeaders.h"
#include "tncinfo.h"
#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 0x2 // 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 L2SENDXID(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);
VOID L2SENDINVALIDCTRL(struct PORTCONTROL * PORT, MESSAGE * Buffer, MESSAGE * ADJBUFFER, UCHAR CTL);
UCHAR * SETUPADDRESSES(struct _LINKTABLE * LINK, PMESSAGE Msg);
VOID ProcessXIDCommand(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESSAGE * Buffer, MESSAGE * ADJBUFFER, UCHAR CTL, UCHAR MSGFLAG);
int CountBits(uint32_t in);
void AttachKISSHF(struct PORTCONTROL * PORT, MESSAGE * Buffer);
void DetachKISSHF(struct PORTCONTROL * PORT);
void KISSHFConnected(struct PORTCONTROL * PORT, struct _LINKTABLE * LINK);
void WriteConnectLog(char * fromcall, char * tocall, UCHAR * Mode);
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 char * 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
extern BOOL LogAllConnects;
APPLCALLS * APPL;
VOID L2Routine(struct PORTCONTROL * PORT, PMESSAGE Buffer)
{
// LEVEL 2 PROCESSING
MESSAGE * ADJBUFFER;
struct _LINKTABLE * LINK;
UCHAR * ptr;
int n;
UCHAR CTL;
uintptr_t Work;
UCHAR c;
// Check for invalid length (< 22 7Header + 7Addr + 7Addr + CTL
if (Buffer->LENGTH < (18 + sizeof(void *)))
{
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;
c = *(ptr) >> 1;
if (c == ' ') // Blank Call
{
Debugprintf("BPQ32 Blank Call Port %d", PORT->PORTNUMBER);
ReleaseBuffer(Buffer);
return;
}
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);
/// TAJ added 07/12/2020 for 'all RX traffic as IfinOctects
InOctets[PORT->PORTNUMBER] += Buffer->LENGTH - MSGHDDRLEN;
// 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 = (uintptr_t)&Buffer->ORIGIN[6];
ptr -= Work; // ptr is now length of digis
Work = (uintptr_t)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
// 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;
if (CompareCalls(Buffer->DEST, NETROMCALL))
{
ISNETROMMSG = 1;
goto FORUS;
}
if (PORT->PORTL3FLAG) // L3 Only Port?
goto NOTFORUS; // If L3ONLY, only accept calls to NETROMCALL
ISNETROMMSG = 0;
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;
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
{
// WE MAY NOT BE ALLOWED TO USE THE BBS CALL ON SOME BANDS DUE TO
// THE RATHER ODD UK LICENCING RULES!
// For backward compatibility only apply to appl 1
if ((PORT->PERMITTEDAPPLS & APPLMASK) != 0)
{
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 Freq[64] = "";
char DIGI = '*';
double ReportFreq = 0;
// if port has a freq associated with it use it
GetPortFrequency(PORT->PORTNUMBER, Freq);
// if (Buffer->ORIGIN[6] & 1)
DIGI = 0; // DOn't think we want 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;
strcpy(MHBASE->MHFreq, Freq);
MHBASE->MHLocator[0] = 0;
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
// LINK points to an empty link table entry
struct ROUTE * ROUTE;
int CTLlessPF = CTL & ~PFBIT;
PORT->L2FRAMESFORUS++;
NO_CTEXT = 0;
// ONLY SABM or UI ALLOWED IF NO SESSION
// Plus XID/TEST/SABME if V2.2 support enabled
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 == SABME)
{
// Although some say V2.2 requires SABME I don't agree!
// Reject until we support Mod 128
L2SENDINVALIDCTRL(PORT, Buffer, ADJBUFFER, CTL);
return;
}
if (CTLlessPF == SREJ) // Used to see if other end supports SREJ on 2.0
{
// Send FRMR if dont support SREJ
// Send DM if we do
if (SUPPORT2point2)
L2SENDRESP(PORT, Buffer, ADJBUFFER, DM);
else
L2SENDINVALIDCTRL(PORT, Buffer, ADJBUFFER, CTL);
return;
}
if (CTLlessPF == XID)
{
// Send FRMR if we only support V 2.0
if (SUPPORT2point2 == FALSE)
{
L2SENDINVALIDCTRL(PORT, Buffer, ADJBUFFER, CTL);
return;
}
// if Support 2.2 drop through
}
if (CTLlessPF == TEST)
{
// I can't see amy harm in replying to TEST
L2SENDRESP(PORT, Buffer, ADJBUFFER, TEST);
return;
}
// if (CTLlessPF != SABM && CTLlessPF != SABME)
if (CTLlessPF != SABM && CTLlessPF != XID)
{
if ((MSGFLAG & CMDBIT) && (CTL & PFBIT)) // Command with P?
L2SENDDM(PORT, Buffer, ADJBUFFER);
else
ReleaseBuffer(Buffer); // Ignore if not
return;
}
// Exclude and limit tests are done for XID and SABM
if (NODE == 0 && BBS == 0) // Don't want any calls
{
ReleaseBuffer(Buffer);
return;
}
#ifdef EXCLUDEBITS
// CHECK ExcludeList
if (CheckExcludeList(Buffer->ORIGIN) == 0)
{
ReleaseBuffer(Buffer);
return;
}
#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;
}
}
// if KISSHF, check if attached. If so, reject. If not, attach.
if (PORT->TNC && PORT->TNC->Hardware == H_KISSHF)
{
struct TNCINFO * TNC = PORT->TNC;
if (TNC->PortRecord->ATTACHEDSESSIONS[0])
{
L2SENDDM(PORT, Buffer, ADJBUFFER);
return;
}
}
// OK to accept SABM or XID
if (CTLlessPF == XID)
{
ProcessXIDCommand(LINK, PORT, Buffer, ADJBUFFER, CTL, MSGFLAG);
return;
}
// Not XID, so must be SABM
L2SABM(LINK, PORT, Buffer, ADJBUFFER, MSGFLAG); // Process the SABM
}
VOID ProcessXIDCommand(struct _LINKTABLE * LINK, struct PORTCONTROL * PORT, MESSAGE * Buffer, MESSAGE * ADJBUFFER, UCHAR CTL, UCHAR MSGFLAG)
{
// I think it is fairly safe to accept XID as soon as we
// can process SREJ, but only accept Mod 8 and 256 Byte frames
// I think the only way to run 2.2 Mod 8 is to preceed a
// SABM with XID, but others don't seem to agree!
// Run through XID fields, changing any we don't like,
// then return an XID response
// Decode and process XID
UCHAR * ptr = &ADJBUFFER->PID;
UCHAR * ptr1, * ptr2;
UCHAR TEMPDIGI[57];
int n;
if (*ptr++ == 0x82 && *ptr++ == 0x80)
{
int Type;
int Len;
unsigned int value;
int xidlen = *(ptr++) << 8;
xidlen += *ptr++;
// XID is set of Type, Len, Value n-tuples
while (xidlen > 0)
{
Type = *ptr++;
Len = *ptr++;
value = 0;
xidlen -= (Len + 2);
while (Len--)
{
value <<=8;
value += *ptr++;
}
switch(Type)
{
case 2: //Bin fields
break;
case 3:
if ((value & OPMustHave) != OPMustHave)
goto BadXID;
if ((value & OPMod8) == 0)
goto BadXID;
if ((value & OPSREJMult) == 0)
goto BadXID;
// Reply Mod 8 SREJMULTI
value = OPMustHave | OPSREJMult | OPMod8;
ptr -=3;
*ptr++ = value >> 16;
*ptr++ = value >> 8;
*ptr++ = value;
break;
case 6: //RX Size
break;
case 8: //RX Window
break;
}
}
// Send back as XID response
LINK->L2STATE = 1; // XID received
LINK->Ver2point2 = TRUE; // Must support 2.2 if sent XID
LINK->L2TIME = PORT->PORTT1;
LINK->LINKPORT = PORT;
// save calls so we can match up SABM when it comes
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
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);
ReleaseBuffer(Buffer);
return;
}
memcpy(ptr2, ptr1, 7);
ptr1 += 7;
ptr2 += 7;
n--;
}
}
ADJBUFFER->CTL = CTL | PFBIT;
// Buffer->LENGTH = (UCHAR *)ADJBUFFER - (UCHAR *)Buffer + MSGHDDRLEN + 15; // SET UP BYTE COUNT
L2SWAPADDRESSES(Buffer); // SWAP ADDRESSES AND SET RESP BITS
// We need to save APPLMASK and ALIASPTR so following SABM connects to application
LINK->APPLMASK = APPLMASK;
LINK->ALIASPTR = ALIASPTR;
PUT_ON_PORT_Q(PORT, Buffer);
return;
}
BadXID:
L2SENDINVALIDCTRL(PORT, Buffer, ADJBUFFER, CTL);
return;
}
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++;
// 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);
if (PORT->TNC && PORT->TNC->Hardware == H_KISSHF)
DetachKISSHF(PORT);
return;