-
Notifications
You must be signed in to change notification settings - Fork 14
/
q921.c
3174 lines (2851 loc) · 75.9 KB
/
q921.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
/*
* libpri: An implementation of Primary Rate ISDN
*
* Written by Mark Spencer <[email protected]>
*
* Copyright (C) 2001-2005, Digium, Inc.
* All Rights Reserved.
*/
/*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2 as published by the
* Free Software Foundation. See the LICENSE file included with
* this program for more details.
*
* In addition, when this program is distributed with Asterisk in
* any form that would qualify as a 'combined work' or as a
* 'derivative work' (but not mere aggregation), you can redistribute
* and/or modify the combination under the terms of the license
* provided with that copy of Asterisk, instead of the license
* terms granted here.
*/
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "compat.h"
#include "libpri.h"
#include "pri_internal.h"
#include "pri_q921.h"
#include "pri_q931.h"
/*
* Define RANDOM_DROPS To randomly drop packets in order to simulate loss for testing
* retransmission functionality
*/
//#define RANDOM_DROPS 1
#define Q921_INIT(fr, l_sapi, l_tei) \
do { \
(fr)->h.sapi = l_sapi; \
(fr)->h.ea1 = 0; \
(fr)->h.ea2 = 1; \
(fr)->h.tei = l_tei; \
} while (0)
#define Q921_CLEAR_INIT(fr, l_sapi, l_tei) \
do { \
memset((fr), 0, sizeof(*(fr))); \
Q921_INIT((fr), (l_sapi), (l_tei)); \
} while (0)
static void q921_dump_pri(struct q921_link *link, char direction_tag);
static void q921_establish_data_link(struct q921_link *link);
static void q921_mdl_error(struct q921_link *link, char error);
static void q921_mdl_remove(struct q921_link *link);
static void q921_mdl_destroy(struct q921_link *link);
/*!
* \internal
* \brief Convert Q.921 TEI management message type to a string.
*
* \param message Q.921 TEI management message type to convert.
*
* \return TEI management message type name string
*/
static const char *q921_tei_mgmt2str(enum q921_tei_identity message)
{
switch (message) {
case Q921_TEI_IDENTITY_REQUEST:
return "TEI Identity Request";
case Q921_TEI_IDENTITY_ASSIGNED:
return "TEI Identity Assigned";
case Q921_TEI_IDENTITY_CHECK_REQUEST:
return "TEI Identity Check Request";
case Q921_TEI_IDENTITY_REMOVE:
return "TEI Identity Remove";
case Q921_TEI_IDENTITY_DENIED:
return "TEI Identity Denied";
case Q921_TEI_IDENTITY_CHECK_RESPONSE:
return "TEI Identity Check Response";
case Q921_TEI_IDENTITY_VERIFY:
return "TEI Identity Verify";
}
return "Unknown";
}
/*!
* \internal
* \brief Convert Q.921 state to a string.
*
* \param state Q.921 state to convert.
*
* \return State name string
*/
static const char *q921_state2str(enum q921_state state)
{
switch (state) {
case Q921_TEI_UNASSIGNED:
return "TEI unassigned";
case Q921_ASSIGN_AWAITING_TEI:
return "Assign awaiting TEI";
case Q921_ESTABLISH_AWAITING_TEI:
return "Establish awaiting TEI";
case Q921_TEI_ASSIGNED:
return "TEI assigned";
case Q921_AWAITING_ESTABLISHMENT:
return "Awaiting establishment";
case Q921_AWAITING_RELEASE:
return "Awaiting release";
case Q921_MULTI_FRAME_ESTABLISHED:
return "Multi-frame established";
case Q921_TIMER_RECOVERY:
return "Timer recovery";
}
return "Unknown state";
}
static void q921_setstate(struct q921_link *link, int newstate)
{
struct pri *ctrl;
ctrl = link->ctrl;
if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
/*
* Suppress displaying these state transitions:
* Q921_MULTI_FRAME_ESTABLISHED <--> Q921_TIMER_RECOVERY
*
* Q921 keeps flipping back and forth between these two states
* when it has nothing better to do.
*/
switch (link->state) {
case Q921_MULTI_FRAME_ESTABLISHED:
case Q921_TIMER_RECOVERY:
switch (newstate) {
case Q921_MULTI_FRAME_ESTABLISHED:
case Q921_TIMER_RECOVERY:
/* Suppress displaying this state transition. */
link->state = newstate;
return;
default:
break;
}
break;
default:
break;
}
if (link->state != newstate) {
pri_message(ctrl, "Changing from state %d(%s) to %d(%s)\n",
link->state, q921_state2str(link->state),
newstate, q921_state2str(newstate));
}
}
link->state = newstate;
}
static void q921_discard_iqueue(struct q921_link *link)
{
struct q921_frame *f, *p;
f = link->tx_queue;
while (f) {
p = f;
f = f->next;
/* Free frame */
free(p);
}
link->tx_queue = NULL;
}
static int q921_transmit(struct pri *ctrl, q921_h *h, int len)
{
int res;
#ifdef RANDOM_DROPS
if (!(random() % 3)) {
pri_message(ctrl, " === Dropping Packet ===\n");
return 0;
}
#endif
ctrl->q921_txcount++;
/* Just send it raw */
if (ctrl->debug & (PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW))
q921_dump(ctrl, h, len, ctrl->debug, 1);
/* Write an extra two bytes for the FCS */
res = ctrl->write_func ? ctrl->write_func(ctrl, h, len + 2) : 0;
if (res != (len + 2)) {
pri_error(ctrl, "Short write: %d/%d (%s)\n", res, len + 2, strerror(errno));
return -1;
}
return 0;
}
static void q921_mdl_send(struct pri *ctrl, enum q921_tei_identity message, int ri, int ai, int iscommand)
{
q921_u *f;
if (!(f = calloc(1, sizeof(*f) + 5)))
return;
Q921_INIT(f, Q921_SAPI_LAYER2_MANAGEMENT, Q921_TEI_GROUP);
f->h.c_r = (ctrl->localtype == PRI_NETWORK) ? iscommand : !iscommand;
f->ft = Q921_FRAMETYPE_U;
f->data[0] = 0x0f; /* Management entity */
f->data[1] = (ri >> 8) & 0xff;
f->data[2] = ri & 0xff;
f->data[3] = message;
f->data[4] = (ai << 1) | 1;
if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
pri_message(ctrl,
"Sending MDL message: %d(%s), TEI=%d\n",
message, q921_tei_mgmt2str(message), ai);
}
q921_transmit(ctrl, (q921_h *)f, 8);
free(f);
}
static void t202_expire(void *vlink)
{
struct q921_link *link = vlink;
struct pri *ctrl;
ctrl = link->ctrl;
/* Start the TEI request timer. */
pri_schedule_del(ctrl, link->t202_timer);
link->t202_timer =
pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T202], t202_expire, link);
if (ctrl->l2_persistence != PRI_L2_PERSISTENCE_KEEP_UP) {
/* Only try to get a TEI for N202 times if layer 2 is not persistent. */
++link->n202_counter;
}
if (!link->t202_timer || link->n202_counter > ctrl->timers[PRI_TIMER_N202]) {
if (!link->t202_timer) {
pri_error(ctrl, "Could not start T202 timer.");
} else {
pri_schedule_del(ctrl, link->t202_timer);
link->t202_timer = 0;
}
pri_error(ctrl, "Unable to receive TEI from network in state %d(%s)!\n",
link->state, q921_state2str(link->state));
switch (link->state) {
case Q921_ASSIGN_AWAITING_TEI:
break;
case Q921_ESTABLISH_AWAITING_TEI:
q921_discard_iqueue(link);
/* DL-RELEASE indication */
q931_dl_event(link, Q931_DL_EVENT_DL_RELEASE_IND);
break;
default:
break;
}
q921_setstate(link, Q921_TEI_UNASSIGNED);
return;
}
/* Send TEI request */
link->ri = random() % 65535;
q921_mdl_send(ctrl, Q921_TEI_IDENTITY_REQUEST, link->ri, Q921_TEI_GROUP, 1);
}
static void q921_tei_request(struct q921_link *link)
{
link->n202_counter = 0;
t202_expire(link);
}
static void q921_tei_remove(struct pri *ctrl, int tei)
{
/*
* Q.921 Section 5.3.2 says we should send the remove message
* twice, in case of message loss.
*/
q921_mdl_send(ctrl, Q921_TEI_IDENTITY_REMOVE, 0, tei, 1);
q921_mdl_send(ctrl, Q921_TEI_IDENTITY_REMOVE, 0, tei, 1);
}
static void q921_send_dm(struct q921_link *link, int fbit)
{
q921_h h;
struct pri *ctrl;
ctrl = link->ctrl;
Q921_CLEAR_INIT(&h, link->sapi, link->tei);
h.u.m3 = 0; /* M3 = 0 */
h.u.m2 = 3; /* M2 = 3 */
h.u.p_f = fbit; /* Final set appropriately */
h.u.ft = Q921_FRAMETYPE_U;
switch (ctrl->localtype) {
case PRI_NETWORK:
h.h.c_r = 0;
break;
case PRI_CPE:
h.h.c_r = 1;
break;
default:
pri_error(ctrl, "Don't know how to DM on a type %d node\n", ctrl->localtype);
return;
}
if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
pri_message(ctrl, "TEI=%d Sending DM\n", link->tei);
}
q921_transmit(ctrl, &h, 3);
}
static void q921_send_disc(struct q921_link *link, int pbit)
{
q921_h h;
struct pri *ctrl;
ctrl = link->ctrl;
Q921_CLEAR_INIT(&h, link->sapi, link->tei);
h.u.m3 = 2; /* M3 = 2 */
h.u.m2 = 0; /* M2 = 0 */
h.u.p_f = pbit; /* Poll set appropriately */
h.u.ft = Q921_FRAMETYPE_U;
switch (ctrl->localtype) {
case PRI_NETWORK:
h.h.c_r = 0;
break;
case PRI_CPE:
h.h.c_r = 1;
break;
default:
pri_error(ctrl, "Don't know how to DISC on a type %d node\n", ctrl->localtype);
return;
}
if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
pri_message(ctrl, "TEI=%d Sending DISC\n", link->tei);
}
q921_transmit(ctrl, &h, 3);
}
static void q921_send_ua(struct q921_link *link, int fbit)
{
q921_h h;
struct pri *ctrl;
ctrl = link->ctrl;
Q921_CLEAR_INIT(&h, link->sapi, link->tei);
h.u.m3 = 3; /* M3 = 3 */
h.u.m2 = 0; /* M2 = 0 */
h.u.p_f = fbit; /* Final set appropriately */
h.u.ft = Q921_FRAMETYPE_U;
switch (ctrl->localtype) {
case PRI_NETWORK:
h.h.c_r = 0;
break;
case PRI_CPE:
h.h.c_r = 1;
break;
default:
pri_error(ctrl, "Don't know how to UA on a type %d node\n", ctrl->localtype);
return;
}
if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
pri_message(ctrl, "TEI=%d Sending UA\n", link->tei);
}
q921_transmit(ctrl, &h, 3);
}
static void q921_send_sabme(struct q921_link *link)
{
q921_h h;
struct pri *ctrl;
ctrl = link->ctrl;
Q921_CLEAR_INIT(&h, link->sapi, link->tei);
h.u.m3 = 3; /* M3 = 3 */
h.u.m2 = 3; /* M2 = 3 */
h.u.p_f = 1; /* Poll bit set */
h.u.ft = Q921_FRAMETYPE_U;
switch (ctrl->localtype) {
case PRI_NETWORK:
h.h.c_r = 1;
break;
case PRI_CPE:
h.h.c_r = 0;
break;
default:
pri_error(ctrl, "Don't know how to SABME on a type %d node\n", ctrl->localtype);
return;
}
if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
pri_message(ctrl, "TEI=%d Sending SABME\n", link->tei);
}
q921_transmit(ctrl, &h, 3);
}
static int q921_ack_packet(struct q921_link *link, int num)
{
struct q921_frame *f;
struct q921_frame *prev;
struct pri *ctrl;
ctrl = link->ctrl;
for (prev = NULL, f = link->tx_queue; f; prev = f, f = f->next) {
if (f->status != Q921_TX_FRAME_SENT) {
break;
}
if (f->h.n_s == num) {
/* Cancel each packet as necessary */
/* That's our packet */
if (prev)
prev->next = f->next;
else
link->tx_queue = f->next;
if (ctrl->debug & PRI_DEBUG_Q921_DUMP) {
pri_message(ctrl,
"-- ACKing N(S)=%d, tx_queue head is N(S)=%d (-1 is empty, -2 is not transmitted)\n",
f->h.n_s,
link->tx_queue
? link->tx_queue->status == Q921_TX_FRAME_SENT
? link->tx_queue->h.n_s
: -2
: -1);
}
/* Update v_a */
free(f);
return 1;
}
}
return 0;
}
static void t203_expire(void *vlink);
static void t200_expire(void *vlink);
#define restart_t200(link) reschedule_t200(link)
static void reschedule_t200(struct q921_link *link)
{
struct pri *ctrl;
ctrl = link->ctrl;
if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
pri_message(ctrl, "-- Restarting T200 timer\n");
pri_schedule_del(ctrl, link->t200_timer);
link->t200_timer = pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T200], t200_expire, link);
}
#if 0
static void reschedule_t203(struct q921_link *link)
{
struct pri *ctrl;
ctrl = link->ctrl;
if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
pri_message(ctrl, "-- Restarting T203 timer\n");
pri_schedule_del(ctrl, link->t203_timer);
link->t203_timer = pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T203], t203_expire, link);
}
#endif
static void start_t203(struct q921_link *link)
{
struct pri *ctrl;
ctrl = link->ctrl;
if (link->t203_timer) {
if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
pri_message(ctrl, "T203 requested to start without stopping first\n");
pri_schedule_del(ctrl, link->t203_timer);
}
if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
pri_message(ctrl, "-- Starting T203 timer\n");
link->t203_timer = pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T203], t203_expire, link);
}
static void stop_t203(struct q921_link *link)
{
struct pri *ctrl;
ctrl = link->ctrl;
if (link->t203_timer) {
if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
pri_message(ctrl, "-- Stopping T203 timer\n");
pri_schedule_del(ctrl, link->t203_timer);
link->t203_timer = 0;
} else {
if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
pri_message(ctrl, "-- T203 requested to stop when not started\n");
}
}
static void start_t200(struct q921_link *link)
{
struct pri *ctrl;
ctrl = link->ctrl;
if (link->t200_timer) {
if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
pri_message(ctrl, "T200 requested to start without stopping first\n");
pri_schedule_del(ctrl, link->t200_timer);
}
if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
pri_message(ctrl, "-- Starting T200 timer\n");
link->t200_timer = pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T200], t200_expire, link);
}
static void stop_t200(struct q921_link *link)
{
struct pri *ctrl;
ctrl = link->ctrl;
if (link->t200_timer) {
if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
pri_message(ctrl, "-- Stopping T200 timer\n");
pri_schedule_del(ctrl, link->t200_timer);
link->t200_timer = 0;
} else {
if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
pri_message(ctrl, "-- T200 requested to stop when not started\n");
}
}
/*!
* \internal
* \brief Initiate bringing up layer 2 link.
*
* \param link Layer 2 link to bring up.
*
* \return Nothing
*/
static void kick_start_link(struct q921_link *link)
{
struct pri *ctrl;
ctrl = link->ctrl;
switch (link->state) {
case Q921_TEI_UNASSIGNED:
if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
pri_message(ctrl, "Kick starting link from no TEI.\n");
}
q921_setstate(link, Q921_ESTABLISH_AWAITING_TEI);
q921_tei_request(link);
break;
case Q921_ASSIGN_AWAITING_TEI:
if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
pri_message(ctrl, "Kick starting link when awaiting TEI.\n");
}
q921_setstate(link, Q921_ESTABLISH_AWAITING_TEI);
break;
case Q921_TEI_ASSIGNED:
if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
pri_message(ctrl, "SAPI/TEI=%d/%d Kick starting link\n", link->sapi,
link->tei);
}
q921_discard_iqueue(link);
q921_establish_data_link(link);
link->l3_initiated = 1;
q921_setstate(link, Q921_AWAITING_ESTABLISHMENT);
break;
default:
break;
}
}
static void restart_timer_expire(void *vlink)
{
struct q921_link *link = vlink;
struct pri *ctrl;
ctrl = link->ctrl;
link->restart_timer = 0;
switch (link->state) {
case Q921_TEI_UNASSIGNED:
case Q921_ASSIGN_AWAITING_TEI:
case Q921_TEI_ASSIGNED:
/* Try to bring layer 2 up. */
kick_start_link(link);
break;
default:
/* Looks like someone forgot to stop the restart timer. */
pri_error(ctrl, "SAPI/TEI=%d/%d Link restart delay timer expired in state %d(%s)\n",
link->sapi, link->tei, link->state, q921_state2str(link->state));
break;
}
}
static void restart_timer_stop(struct q921_link *link)
{
struct pri *ctrl;
ctrl = link->ctrl;
pri_schedule_del(ctrl, link->restart_timer);
link->restart_timer = 0;
}
/*! \note Only call on the transition to state Q921_TEI_ASSIGNED or already there. */
static void restart_timer_start(struct q921_link *link)
{
struct pri *ctrl;
ctrl = link->ctrl;
if (ctrl->debug & PRI_DEBUG_Q921_DUMP) {
pri_message(ctrl, "SAPI/TEI=%d/%d Starting link restart delay timer\n",
link->sapi, link->tei);
}
pri_schedule_del(ctrl, link->restart_timer);
link->restart_timer =
pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T200], restart_timer_expire, link);
}
/*! \note Only call on the transition to state Q921_TEI_ASSIGNED or already there. */
static pri_event *q921_check_delay_restart(struct q921_link *link)
{
pri_event *ev;
struct pri *ctrl;
ctrl = link->ctrl;
if (ctrl->l2_persistence == PRI_L2_PERSISTENCE_KEEP_UP) {
/*
* For PTP links:
* This is where we act a bit like L3 instead of L2, since we've
* got an L3 that depends on us keeping L2 automatically alive
* and happy.
*
* For PTMP links:
* We can optionally keep L2 automatically alive and happy.
*/
restart_timer_start(link);
}
if (PTP_MODE(ctrl)) {
switch (link->state) {
case Q921_MULTI_FRAME_ESTABLISHED:
case Q921_TIMER_RECOVERY:
/* Notify the upper layer that layer 2 went down. */
ctrl->schedev = 1;
ctrl->ev.gen.e = PRI_EVENT_DCHAN_DOWN;
ev = &ctrl->ev;
break;
default:
ev = NULL;
break;
}
} else {
ev = NULL;
}
return ev;
}
/*!
* \brief Bring all layer 2 links up.
*
* \param ctrl D channel controller.
*
* \return Nothing
*/
void q921_bring_layer2_up(struct pri *ctrl)
{
struct q921_link *link;
if (PTMP_MODE(ctrl)) {
/* Don't start with the broadcast link. */
link = ctrl->link.next;
} else {
link = &ctrl->link;
}
for (; link; link = link->next) {
if (!link->restart_timer) {
/* A restart on the link is not already in the works. */
kick_start_link(link);
}
}
}
/* This is the equivalent of the I-Frame queued up path in Figure B.7 in MULTI_FRAME_ESTABLISHED */
static int q921_send_queued_iframes(struct q921_link *link)
{
struct pri *ctrl;
struct q921_frame *f;
int frames_txd = 0;
ctrl = link->ctrl;
for (f = link->tx_queue; f; f = f->next) {
if (f->status != Q921_TX_FRAME_SENT) {
/* This frame needs to be sent. */
break;
}
}
if (!f) {
/* The Tx queue has no pending frames. */
return 0;
}
if (link->peer_rx_busy) {
/* Don't flood debug trace if not really looking at Q.921 layer. */
if (ctrl->debug & (/* PRI_DEBUG_Q921_STATE | */ PRI_DEBUG_Q921_DUMP)) {
pri_message(ctrl,
"TEI=%d Couldn't transmit I-frame at this time due to peer busy condition\n",
link->tei);
}
return 0;
}
if (link->v_s == Q921_ADD(link->v_a, ctrl->timers[PRI_TIMER_K])) {
/* Don't flood debug trace if not really looking at Q.921 layer. */
if (ctrl->debug & (/* PRI_DEBUG_Q921_STATE | */ PRI_DEBUG_Q921_DUMP)) {
pri_message(ctrl,
"TEI=%d Couldn't transmit I-frame at this time due to window shut\n",
link->tei);
}
return 0;
}
/* Send all pending frames that fit in the window. */
for (; f; f = f->next) {
if (link->v_s == Q921_ADD(link->v_a, ctrl->timers[PRI_TIMER_K])) {
/* The window is no longer open. */
break;
}
/* Send it now... */
switch (f->status) {
case Q921_TX_FRAME_NEVER_SENT:
if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
pri_message(ctrl,
"TEI=%d Transmitting N(S)=%d, window is open V(A)=%d K=%d\n",
link->tei, link->v_s, link->v_a, ctrl->timers[PRI_TIMER_K]);
}
break;
case Q921_TX_FRAME_PUSHED_BACK:
if (f->h.n_s != link->v_s) {
/* Should never happen. */
pri_error(ctrl,
"TEI=%d Retransmitting frame with old N(S)=%d as N(S)=%d!\n",
link->tei, f->h.n_s, link->v_s);
} else if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
pri_message(ctrl, "TEI=%d Retransmitting frame N(S)=%d now!\n",
link->tei, link->v_s);
}
break;
default:
/* Should never happen. */
pri_error(ctrl, "Unexpected Tx Q frame status: %d", f->status);
break;
}
/*
* Send the frame out on the assigned TEI.
* Done now because the frame may have been queued before we
* had an assigned TEI.
*/
f->h.h.tei = link->tei;
f->h.n_s = link->v_s;
f->h.n_r = link->v_r;
f->h.ft = 0;
f->h.p_f = 0;
q921_transmit(ctrl, (q921_h *) (&f->h), f->len);
Q921_INC(link->v_s);
++frames_txd;
if ((ctrl->debug & PRI_DEBUG_Q931_DUMP)
&& f->status == Q921_TX_FRAME_NEVER_SENT) {
/*
* The transmit operation might dump the Q.921 header, so logging
* the Q.931 message body after the transmit puts the sections of
* the message in the right order in the log.
*
* Also dump the Q.931 part only once instead of for every
* retransmission.
*/
q931_dump(ctrl, link->tei, (q931_h *) f->h.data, f->len - 4, 1);
}
f->status = Q921_TX_FRAME_SENT;
}
if (frames_txd) {
link->acknowledge_pending = 0;
if (!link->t200_timer) {
stop_t203(link);
start_t200(link);
}
}
return frames_txd;
}
static void q921_reject(struct q921_link *link, int pf)
{
q921_h h;
struct pri *ctrl;
ctrl = link->ctrl;
Q921_CLEAR_INIT(&h, link->sapi, link->tei);
h.s.x0 = 0; /* Always 0 */
h.s.ss = 2; /* Reject */
h.s.ft = 1; /* Frametype (01) */
h.s.n_r = link->v_r; /* Where to start retransmission N(R) */
h.s.p_f = pf;
switch (ctrl->localtype) {
case PRI_NETWORK:
h.h.c_r = 0;
break;
case PRI_CPE:
h.h.c_r = 1;
break;
default:
pri_error(ctrl, "Don't know how to REJ on a type %d node\n", ctrl->localtype);
return;
}
if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
pri_message(ctrl, "TEI=%d Sending REJ N(R)=%d\n", link->tei, link->v_r);
}
q921_transmit(ctrl, &h, 4);
}
static void q921_rr(struct q921_link *link, int pbit, int cmd)
{
q921_h h;
struct pri *ctrl;
ctrl = link->ctrl;
Q921_CLEAR_INIT(&h, link->sapi, link->tei);
h.s.x0 = 0; /* Always 0 */
h.s.ss = 0; /* Receive Ready */
h.s.ft = 1; /* Frametype (01) */
h.s.n_r = link->v_r; /* N(R) */
h.s.p_f = pbit; /* Poll/Final set appropriately */
switch (ctrl->localtype) {
case PRI_NETWORK:
if (cmd)
h.h.c_r = 1;
else
h.h.c_r = 0;
break;
case PRI_CPE:
if (cmd)
h.h.c_r = 0;
else
h.h.c_r = 1;
break;
default:
pri_error(ctrl, "Don't know how to RR on a type %d node\n", ctrl->localtype);
return;
}
#if 0 /* Don't flood debug trace with RR if not really looking at Q.921 layer. */
if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
pri_message(ctrl, "TEI=%d Sending RR N(R)=%d\n", link->tei, link->v_r);
}
#endif
q921_transmit(ctrl, &h, 4);
}
static void transmit_enquiry(struct q921_link *link)
{
if (!link->own_rx_busy) {
q921_rr(link, 1, 1);
link->acknowledge_pending = 0;
start_t200(link);
} else {
/* XXX: Implement me... */
}
}
static void t200_expire(void *vlink)
{
struct q921_link *link = vlink;
struct pri *ctrl;
ctrl = link->ctrl;
if (ctrl->debug & PRI_DEBUG_Q921_DUMP) {
pri_message(ctrl, "%s\n", __FUNCTION__);
q921_dump_pri(link, ' ');
}
link->t200_timer = 0;
switch (link->state) {
case Q921_MULTI_FRAME_ESTABLISHED:
link->RC = 0;
transmit_enquiry(link);
link->RC++;
q921_setstate(link, Q921_TIMER_RECOVERY);
break;
case Q921_TIMER_RECOVERY:
/* SDL Flow Figure B.8/Q.921 Page 81 */
if (link->RC != ctrl->timers[PRI_TIMER_N200]) {
#if 0
if (link->v_s == link->v_a) {
transmit_enquiry(link);
}
#else
/* We are chosing to enquiry by default (to reduce risk of T200 timer errors at the other
* side, instead of retransmission of the last I-frame we sent */
transmit_enquiry(link);
#endif
link->RC++;
} else {
q921_mdl_error(link, 'I');
q921_establish_data_link(link);
link->l3_initiated = 0;
q921_setstate(link, Q921_AWAITING_ESTABLISHMENT);
if (PTP_MODE(ctrl)) {
ctrl->schedev = 1;
ctrl->ev.gen.e = PRI_EVENT_DCHAN_DOWN;
}
}
break;
case Q921_AWAITING_ESTABLISHMENT:
if (link->RC != ctrl->timers[PRI_TIMER_N200]) {
link->RC++;
q921_send_sabme(link);
start_t200(link);
} else {
q921_check_delay_restart(link);
q921_discard_iqueue(link);
q921_mdl_error(link, 'G');
q921_setstate(link, Q921_TEI_ASSIGNED);
/* DL-RELEASE indication */
q931_dl_event(link, Q931_DL_EVENT_DL_RELEASE_IND);
}
break;
case Q921_AWAITING_RELEASE:
if (link->RC != ctrl->timers[PRI_TIMER_N200]) {
++link->RC;
q921_send_disc(link, 1);
start_t200(link);
} else {
q921_check_delay_restart(link);
q921_mdl_error(link, 'H');
/* DL-RELEASE confirm */
q931_dl_event(link, Q931_DL_EVENT_DL_RELEASE_CONFIRM);
q921_setstate(link, Q921_TEI_ASSIGNED);
}
break;
default:
/* Looks like someone forgot to stop the T200 timer. */
pri_error(ctrl, "T200 expired in state %d(%s)\n",
link->state, q921_state2str(link->state));
break;
}
}
/* This is sending a DL-UNIT-DATA request */
int q921_transmit_uiframe(struct q921_link *link, void *buf, int len)
{
uint8_t ubuf[512];
q921_h *h = (void *)&ubuf[0];
struct pri *ctrl;
ctrl = link->ctrl;
if (len >= 512) {
pri_error(ctrl, "Requested to send UI-frame larger than 512 bytes!\n");
return -1;
}
memset(ubuf, 0, sizeof(ubuf));
h->h.sapi = 0;
h->h.ea1 = 0;
h->h.ea2 = 1;
h->h.tei = link->tei;
h->u.m3 = 0;
h->u.m2 = 0;
h->u.p_f = 0; /* Poll bit set */
h->u.ft = Q921_FRAMETYPE_U;
switch (ctrl->localtype) {
case PRI_NETWORK:
h->h.c_r = 1;
break;
case PRI_CPE:
h->h.c_r = 0;
break;
default:
pri_error(ctrl, "Don't know how to UI-frame on a type %d node\n", ctrl->localtype);
return -1;