q921.c 75.9 KB
Newer Older
Mark Spencer's avatar
Mark Spencer committed
1 2 3
/*
 * libpri: An implementation of Primary Rate ISDN
 *
4
 * Written by Mark Spencer <markster@digium.com>
Mark Spencer's avatar
Mark Spencer committed
5
 *
6
 * Copyright (C) 2001-2005, Digium, Inc.
Mark Spencer's avatar
Mark Spencer committed
7
 * All Rights Reserved.
8 9 10 11 12 13 14 15
 */

/*
 * 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.
Mark Spencer's avatar
Mark Spencer committed
16
 *
17 18 19 20
 * 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.
Mark Spencer's avatar
Mark Spencer committed
21
 *
22 23 24 25 26 27
 * 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.
Mark Spencer's avatar
Mark Spencer committed
28
 */
29

30
#include <stdint.h>
Mark Spencer's avatar
Mark Spencer committed
31 32 33 34 35
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
36
#include "compat.h"
Mark Spencer's avatar
Mark Spencer committed
37 38 39 40 41
#include "libpri.h"
#include "pri_internal.h"
#include "pri_q921.h" 
#include "pri_q931.h"

Mark Spencer's avatar
Mark Spencer committed
42 43 44 45
/*
 * Define RANDOM_DROPS To randomly drop packets in order to simulate loss for testing
 * retransmission functionality
 */
46
//#define RANDOM_DROPS	1
Mark Spencer's avatar
Mark Spencer committed
47

48 49 50 51 52 53 54 55 56 57 58 59 60
#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)
Mark Spencer's avatar
Mark Spencer committed
61

62 63 64 65
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);
66
static void q921_mdl_destroy(struct q921_link *link);
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
/*!
 * \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";
}

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
/*!
 * \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";
}

130
static void q921_setstate(struct q921_link *link, int newstate)
131
{
132 133
	struct pri *ctrl;

134
	ctrl = link->ctrl;
135
	if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
136 137 138 139 140 141 142
		/*
		 * 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.
		 */
143
		switch (link->state) {
144 145 146 147 148 149
		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. */
150
				link->state = newstate;
151 152 153 154 155 156 157 158
				return;
			default:
				break;
			}
			break;
		default:
			break;
		}
159
		if (link->state != newstate) {
160
			pri_message(ctrl, "Changing from state %d(%s) to %d(%s)\n",
161
				link->state, q921_state2str(link->state),
162
				newstate, q921_state2str(newstate));
163 164
		}
	}
165
	link->state = newstate;
166
}
167

168
static void q921_discard_iqueue(struct q921_link *link)
Mark Spencer's avatar
Mark Spencer committed
169 170
{
	struct q921_frame *f, *p;
171

172
	f = link->tx_queue;
173
	while (f) {
Mark Spencer's avatar
Mark Spencer committed
174 175 176 177 178
		p = f;
		f = f->next;
		/* Free frame */
		free(p);
	}
179
	link->tx_queue = NULL;
Mark Spencer's avatar
Mark Spencer committed
180 181
}

182
static int q921_transmit(struct pri *ctrl, q921_h *h, int len) 
183
{
Mark Spencer's avatar
Mark Spencer committed
184
	int res;
185

Mark Spencer's avatar
Mark Spencer committed
186
#ifdef RANDOM_DROPS
187 188 189 190 191 192
	if (!(random() % 3)) {
		pri_message(ctrl, " === Dropping Packet ===\n");
		return 0;
	}
#endif
	ctrl->q921_txcount++;
Mark Spencer's avatar
Mark Spencer committed
193
	/* Just send it raw */
194
	if (ctrl->debug & (PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW))
195
		q921_dump(ctrl, h, len, ctrl->debug, 1);
Mark Spencer's avatar
Mark Spencer committed
196
	/* Write an extra two bytes for the FCS */
197
	res = ctrl->write_func ? ctrl->write_func(ctrl, h, len + 2) : 0;
Mark Spencer's avatar
Mark Spencer committed
198
	if (res != (len + 2)) {
199
		pri_error(ctrl, "Short write: %d/%d (%s)\n", res, len + 2, strerror(errno));
Mark Spencer's avatar
Mark Spencer committed
200 201 202 203 204
		return -1;
	}
	return 0;
}

205
static void q921_mdl_send(struct pri *ctrl, enum q921_tei_identity message, int ri, int ai, int iscommand)
206 207 208 209 210 211
{
	q921_u *f;

	if (!(f = calloc(1, sizeof(*f) + 5)))
		return;

212
	Q921_INIT(f, Q921_SAPI_LAYER2_MANAGEMENT, Q921_TEI_GROUP);
213
	f->h.c_r = (ctrl->localtype == PRI_NETWORK) ? iscommand : !iscommand;
214 215 216 217 218 219
	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;
220
	if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
221
		pri_message(ctrl,
222
			"Sending MDL message: %d(%s), TEI=%d\n",
223
			message, q921_tei_mgmt2str(message), ai);
224
	}
225
	q921_transmit(ctrl, (q921_h *)f, 8);
226 227 228
	free(f);
}

229
static void t202_expire(void *vlink)
230
{
231
	struct q921_link *link = vlink;
232 233
	struct pri *ctrl;

234
	ctrl = link->ctrl;
235 236

	/* Start the TEI request timer. */
237 238 239
	pri_schedule_del(ctrl, link->t202_timer);
	link->t202_timer =
		pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T202], t202_expire, link);
240

241 242 243 244
	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;
	}
245 246
	if (!link->t202_timer || link->n202_counter > ctrl->timers[PRI_TIMER_N202]) {
		if (!link->t202_timer) {
247 248
			pri_error(ctrl, "Could not start T202 timer.");
		} else {
249 250
			pri_schedule_del(ctrl, link->t202_timer);
			link->t202_timer = 0;
251 252
		}
		pri_error(ctrl, "Unable to receive TEI from network in state %d(%s)!\n",
253 254
			link->state, q921_state2str(link->state));
		switch (link->state) {
255 256 257
		case Q921_ASSIGN_AWAITING_TEI:
			break;
		case Q921_ESTABLISH_AWAITING_TEI:
258
			q921_discard_iqueue(link);
259
			/* DL-RELEASE indication */
260
			q931_dl_event(link, Q931_DL_EVENT_DL_RELEASE_IND);
261 262 263 264
			break;
		default:
			break;
		}
265
		q921_setstate(link, Q921_TEI_UNASSIGNED);
266 267
		return;
	}
268 269

	/* Send TEI request */
270
	link->ri = random() % 65535;
271
	q921_mdl_send(ctrl, Q921_TEI_IDENTITY_REQUEST, link->ri, Q921_TEI_GROUP, 1);
272 273
}

274
static void q921_tei_request(struct q921_link *link)
275
{
276 277
	link->n202_counter = 0;
	t202_expire(link);
278 279
}

280 281 282 283 284 285
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.
	 */
286 287
	q921_mdl_send(ctrl, Q921_TEI_IDENTITY_REMOVE, 0, tei, 1);
	q921_mdl_send(ctrl, Q921_TEI_IDENTITY_REMOVE, 0, tei, 1);
288 289
}

290
static void q921_send_dm(struct q921_link *link, int fbit)
291 292
{
	q921_h h;
293 294
	struct pri *ctrl;

295
	ctrl = link->ctrl;
296

297
	Q921_CLEAR_INIT(&h, link->sapi, link->tei);
298 299 300 301
	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;
302
	switch (ctrl->localtype) {
303 304 305 306 307 308 309 310 311 312 313
	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) {
314
		pri_message(ctrl, "TEI=%d Sending DM\n", link->tei);
315
	}
316
	q921_transmit(ctrl, &h, 3);
317 318
}

319
static void q921_send_disc(struct q921_link *link, int pbit)
320 321
{
	q921_h h;
322 323
	struct pri *ctrl;

324
	ctrl = link->ctrl;
325

326
	Q921_CLEAR_INIT(&h, link->sapi, link->tei);
327 328 329 330
	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;
331
	switch (ctrl->localtype) {
332 333 334 335 336 337 338 339 340 341 342
	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) {
343
		pri_message(ctrl, "TEI=%d Sending DISC\n", link->tei);
344
	}
345
	q921_transmit(ctrl, &h, 3);
346 347
}

348
static void q921_send_ua(struct q921_link *link, int fbit)
Mark Spencer's avatar
Mark Spencer committed
349 350
{
	q921_h h;
351 352
	struct pri *ctrl;

353
	ctrl = link->ctrl;
354

355
	Q921_CLEAR_INIT(&h, link->sapi, link->tei);
Mark Spencer's avatar
Mark Spencer committed
356 357
	h.u.m3 = 3;		/* M3 = 3 */
	h.u.m2 = 0;		/* M2 = 0 */
358
	h.u.p_f = fbit;	/* Final set appropriately */
Mark Spencer's avatar
Mark Spencer committed
359
	h.u.ft = Q921_FRAMETYPE_U;
360
	switch (ctrl->localtype) {
Mark Spencer's avatar
Mark Spencer committed
361 362 363 364 365 366 367
	case PRI_NETWORK:
		h.h.c_r = 0;
		break;
	case PRI_CPE:
		h.h.c_r = 1;
		break;
	default:
368
		pri_error(ctrl, "Don't know how to UA on a type %d node\n", ctrl->localtype);
Mark Spencer's avatar
Mark Spencer committed
369 370
		return;
	}
371
	if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
372
		pri_message(ctrl, "TEI=%d Sending UA\n", link->tei);
373
	}
374
	q921_transmit(ctrl, &h, 3);
Mark Spencer's avatar
Mark Spencer committed
375 376
}

377
static void q921_send_sabme(struct q921_link *link)
Mark Spencer's avatar
Mark Spencer committed
378 379
{
	q921_h h;
380
	struct pri *ctrl;
381

382
	ctrl = link->ctrl;
383

384
	Q921_CLEAR_INIT(&h, link->sapi, link->tei);
Mark Spencer's avatar
Mark Spencer committed
385 386
	h.u.m3 = 3;	/* M3 = 3 */
	h.u.m2 = 3;	/* M2 = 3 */
Mark Spencer's avatar
Mark Spencer committed
387 388
	h.u.p_f = 1;	/* Poll bit set */
	h.u.ft = Q921_FRAMETYPE_U;
389
	switch (ctrl->localtype) {
Mark Spencer's avatar
Mark Spencer committed
390 391 392 393 394 395 396
	case PRI_NETWORK:
		h.h.c_r = 1;
		break;
	case PRI_CPE:
		h.h.c_r = 0;
		break;
	default:
397
		pri_error(ctrl, "Don't know how to SABME on a type %d node\n", ctrl->localtype);
Mark Spencer's avatar
Mark Spencer committed
398 399
		return;
	}
400
	if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
401
		pri_message(ctrl, "TEI=%d Sending SABME\n", link->tei);
402
	}
403
	q921_transmit(ctrl, &h, 3);
Mark Spencer's avatar
Mark Spencer committed
404 405
}

406
static int q921_ack_packet(struct q921_link *link, int num)
Mark Spencer's avatar
Mark Spencer committed
407
{
408 409
	struct q921_frame *f;
	struct q921_frame *prev;
410
	struct pri *ctrl;
411

412
	ctrl = link->ctrl;
413

414
	for (prev = NULL, f = link->tx_queue; f; prev = f, f = f->next) {
415
		if (f->status != Q921_TX_FRAME_SENT) {
416 417
			break;
		}
Mark Spencer's avatar
Mark Spencer committed
418
		if (f->h.n_s == num) {
419
			/* Cancel each packet as necessary */
Mark Spencer's avatar
Mark Spencer committed
420 421 422 423
			/* That's our packet */
			if (prev)
				prev->next = f->next;
			else
424
				link->tx_queue = f->next;
425 426
			if (ctrl->debug & PRI_DEBUG_Q921_DUMP) {
				pri_message(ctrl,
427
					"-- ACKing N(S)=%d, tx_queue head is N(S)=%d (-1 is empty, -2 is not transmitted)\n",
428
					f->h.n_s,
429
					link->tx_queue
430
						? link->tx_queue->status == Q921_TX_FRAME_SENT
431
							? link->tx_queue->h.n_s
432 433 434
							: -2
						: -1);
			}
435
			/* Update v_a */
Mark Spencer's avatar
Mark Spencer committed
436 437 438 439 440 441 442
			free(f);
			return 1;
		}
	}
	return 0;
}

443 444
static void t203_expire(void *vlink);
static void t200_expire(void *vlink);
445

446
#define restart_t200(link) reschedule_t200(link)
447
static void reschedule_t200(struct q921_link *link)
448
{
449 450
	struct pri *ctrl;

451
	ctrl = link->ctrl;
452

453 454
	if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
		pri_message(ctrl, "-- Restarting T200 timer\n");
455 456
	pri_schedule_del(ctrl, link->t200_timer);
	link->t200_timer = pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T200], t200_expire, link);
457 458
}

459
#if 0
460
static void reschedule_t203(struct q921_link *link)
461
{
462 463
	struct pri *ctrl;

464
	ctrl = link->ctrl;
465

466 467
	if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
		pri_message(ctrl, "-- Restarting T203 timer\n");
468 469
	pri_schedule_del(ctrl, link->t203_timer);
	link->t203_timer = pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T203], t203_expire, link);
470
}
471
#endif
Mark Spencer's avatar
Mark Spencer committed
472

473
static void start_t203(struct q921_link *link)
Mark Spencer's avatar
Mark Spencer committed
474
{
475 476
	struct pri *ctrl;

477
	ctrl = link->ctrl;
478 479

	if (link->t203_timer) {
480 481
		if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
			pri_message(ctrl, "T203 requested to start without stopping first\n");
482
		pri_schedule_del(ctrl, link->t203_timer);
483 484 485
	}
	if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
		pri_message(ctrl, "-- Starting T203 timer\n");
486
	link->t203_timer = pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T203], t203_expire, link);
487 488
}

489
static void stop_t203(struct q921_link *link)
490
{
491 492
	struct pri *ctrl;

493
	ctrl = link->ctrl;
494 495

	if (link->t203_timer) {
496 497
		if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
			pri_message(ctrl, "-- Stopping T203 timer\n");
498 499
		pri_schedule_del(ctrl, link->t203_timer);
		link->t203_timer = 0;
500
	} else {
501 502
		if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
			pri_message(ctrl, "-- T203 requested to stop when not started\n");
Mark Spencer's avatar
Mark Spencer committed
503
	}
504 505
}

506
static void start_t200(struct q921_link *link)
507
{
508 509
	struct pri *ctrl;

510
	ctrl = link->ctrl;
511 512

	if (link->t200_timer) {
513 514
		if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
			pri_message(ctrl, "T200 requested to start without stopping first\n");
515
		pri_schedule_del(ctrl, link->t200_timer);
516 517 518
	}
	if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
		pri_message(ctrl, "-- Starting T200 timer\n");
519
	link->t200_timer = pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T200], t200_expire, link);
520 521
}

522
static void stop_t200(struct q921_link *link)
523
{
524 525
	struct pri *ctrl;

526
	ctrl = link->ctrl;
527 528

	if (link->t200_timer) {
529 530
		if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
			pri_message(ctrl, "-- Stopping T200 timer\n");
531 532
		pri_schedule_del(ctrl, link->t200_timer);
		link->t200_timer = 0;
Mark Spencer's avatar
Mark Spencer committed
533
	} else {
534 535
		if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
			pri_message(ctrl, "-- T200 requested to stop when not started\n");
Mark Spencer's avatar
Mark Spencer committed
536
	}
537 538
}

539 540 541 542 543 544 545 546 547
/*!
 * \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)
548
{
549 550 551
	struct pri *ctrl;

	ctrl = link->ctrl;
552

553 554 555 556 557 558 559 560 561 562
	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) {
563
			pri_message(ctrl, "Kick starting link when awaiting TEI.\n");
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578
		}
		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;
579
	}
580 581 582 583 584 585 586 587
}

static void restart_timer_expire(void *vlink)
{
	struct q921_link *link = vlink;
	struct pri *ctrl;

	ctrl = link->ctrl;
588

589
	link->restart_timer = 0;
590 591

	switch (link->state) {
592 593
	case Q921_TEI_UNASSIGNED:
	case Q921_ASSIGN_AWAITING_TEI:
594 595
	case Q921_TEI_ASSIGNED:
		/* Try to bring layer 2 up. */
596
		kick_start_link(link);
597 598 599
		break;
	default:
		/* Looks like someone forgot to stop the restart timer. */
600 601
		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));
602 603 604 605
		break;
	}
}

606
static void restart_timer_stop(struct q921_link *link)
607
{
608 609 610 611 612
	struct pri *ctrl;

	ctrl = link->ctrl;
	pri_schedule_del(ctrl, link->restart_timer);
	link->restart_timer = 0;
613 614
}

615
/*! \note Only call on the transition to state Q921_TEI_ASSIGNED or already there. */
616
static void restart_timer_start(struct q921_link *link)
617
{
618 619 620 621
	struct pri *ctrl;

	ctrl = link->ctrl;

622
	if (ctrl->debug & PRI_DEBUG_Q921_DUMP) {
623 624
		pri_message(ctrl, "SAPI/TEI=%d/%d Starting link restart delay timer\n",
			link->sapi, link->tei);
625
	}
626 627 628
	pri_schedule_del(ctrl, link->restart_timer);
	link->restart_timer =
		pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T200], restart_timer_expire, link);
629 630
}

631 632
/*! \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)
633 634
{
	pri_event *ev;
635 636 637
	struct pri *ctrl;

	ctrl = link->ctrl;
638

639
	if (ctrl->l2_persistence == PRI_L2_PERSISTENCE_KEEP_UP) {
640
		/*
641
		 * For PTP links:
642 643
		 * 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
644 645 646 647
		 * and happy.
		 *
		 * For PTMP links:
		 * We can optionally keep L2 automatically alive and happy.
648
		 */
649
		restart_timer_start(link);
650 651
	}
	if (PTP_MODE(ctrl)) {
652 653 654 655 656 657 658 659 660 661 662 663
		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;
		}
664 665 666 667 668 669
	} else {
		ev = NULL;
	}
	return ev;
}

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
/*!
 * \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);
		}
	}
}

695
/* This is the equivalent of the I-Frame queued up path in Figure B.7 in MULTI_FRAME_ESTABLISHED */
696
static int q921_send_queued_iframes(struct q921_link *link)
697
{
698
	struct pri *ctrl;
699 700 701
	struct q921_frame *f;
	int frames_txd = 0;

702
	ctrl = link->ctrl;
703

704
	for (f = link->tx_queue; f; f = f->next) {
705 706
		if (f->status != Q921_TX_FRAME_SENT) {
			/* This frame needs to be sent. */
707 708 709 710 711
			break;
		}
	}
	if (!f) {
		/* The Tx queue has no pending frames. */
712 713 714
		return 0;
	}

715
	if (link->peer_rx_busy) {
716 717 718
		/* 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,
719 720 721 722 723 724 725 726 727 728
				"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",
729
				link->tei);
730 731 732
		}
		return 0;
	}
733

734 735
	/* Send all pending frames that fit in the window. */
	for (; f; f = f->next) {
736
		if (link->v_s == Q921_ADD(link->v_a, ctrl->timers[PRI_TIMER_K])) {
737 738 739 740 741
			/* The window is no longer open. */
			break;
		}

		/* Send it now... */
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
		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;
		}
766

767 768 769 770 771
		/*
		 * Send the frame out on the assigned TEI.
		 * Done now because the frame may have been queued before we
		 * had an assigned TEI.
		 */
772
		f->h.h.tei = link->tei;
773

774 775
		f->h.n_s = link->v_s;
		f->h.n_r = link->v_r;
776 777
		f->h.ft = 0;
		f->h.p_f = 0;
778 779
		q921_transmit(ctrl, (q921_h *) (&f->h), f->len);
		Q921_INC(link->v_s);
780
		++frames_txd;
781

782 783
		if ((ctrl->debug & PRI_DEBUG_Q931_DUMP)
			&& f->status == Q921_TX_FRAME_NEVER_SENT) {
784 785 786 787 788
			/*
			 * 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.
			 *
789 790
			 * Also dump the Q.931 part only once instead of for every
			 * retransmission.
791
			 */
792
			q931_dump(ctrl, link->tei, (q931_h *) f->h.data, f->len - 4, 1);
793
		}
794
		f->status = Q921_TX_FRAME_SENT;
795 796 797
	}

	if (frames_txd) {
798 799 800 801
		link->acknowledge_pending = 0;
		if (!link->t200_timer) {
			stop_t203(link);
			start_t200(link);
802 803 804 805
		}
	}

	return frames_txd;
Mark Spencer's avatar
Mark Spencer committed
806 807
}

808
static void q921_reject(struct q921_link *link, int pf)
Mark Spencer's avatar
Mark Spencer committed
809 810
{
	q921_h h;
811
	struct pri *ctrl;
812

813
	ctrl = link->ctrl;
814

815
	Q921_CLEAR_INIT(&h, link->sapi, link->tei);
Mark Spencer's avatar
Mark Spencer committed
816 817 818
	h.s.x0 = 0;	/* Always 0 */
	h.s.ss = 2;	/* Reject */
	h.s.ft = 1;	/* Frametype (01) */
819
	h.s.n_r = link->v_r;	/* Where to start retransmission N(R) */
820
	h.s.p_f = pf;	
821
	switch (ctrl->localtype) {
Mark Spencer's avatar
Mark Spencer committed
822 823 824 825 826 827 828
	case PRI_NETWORK:
		h.h.c_r = 0;
		break;
	case PRI_CPE:
		h.h.c_r = 1;
		break;
	default:
829
		pri_error(ctrl, "Don't know how to REJ on a type %d node\n", ctrl->localtype);
Mark Spencer's avatar
Mark Spencer committed
830 831
		return;
	}
832
	if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
833
		pri_message(ctrl, "TEI=%d Sending REJ N(R)=%d\n", link->tei, link->v_r);
834
	}
835
	q921_transmit(ctrl, &h, 4);
Mark Spencer's avatar
Mark Spencer committed
836 837
}

838
static void q921_rr(struct q921_link *link, int pbit, int cmd)
839
{
Mark Spencer's avatar
Mark Spencer committed
840
	q921_h h;
841
	struct pri *ctrl;
842

843
	ctrl = link->ctrl;
844

845
	Q921_CLEAR_INIT(&h, link->sapi, link->tei);
Mark Spencer's avatar
Mark Spencer committed
846 847 848
	h.s.x0 = 0;	/* Always 0 */
	h.s.ss = 0; /* Receive Ready */
	h.s.ft = 1;	/* Frametype (01) */
849
	h.s.n_r = link->v_r;	/* N(R) */
Mark Spencer's avatar
Mark Spencer committed
850
	h.s.p_f = pbit;		/* Poll/Final set appropriately */
851
	switch (ctrl->localtype) {
Mark Spencer's avatar
Mark Spencer committed
852
	case PRI_NETWORK:
853 854 855 856
		if (cmd)
			h.h.c_r = 1;
		else
			h.h.c_r = 0;
Mark Spencer's avatar
Mark Spencer committed
857 858
		break;
	case PRI_CPE:
859 860 861 862
		if (cmd)
			h.h.c_r = 0;
		else
			h.h.c_r = 1;
Mark Spencer's avatar
Mark Spencer committed
863 864
		break;
	default:
865
		pri_error(ctrl, "Don't know how to RR on a type %d node\n", ctrl->localtype);
Mark Spencer's avatar
Mark Spencer committed
866 867
		return;
	}
868 869
#if 0	/* Don't flood debug trace with RR if not really looking at Q.921 layer. */
	if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
870
		pri_message(ctrl, "TEI=%d Sending RR N(R)=%d\n", link->tei, link->v_r);
871 872
	}
#endif
873
	q921_transmit(ctrl, &h, 4);
Mark Spencer's avatar
Mark Spencer committed
874 875
}

876
static void transmit_enquiry(struct q921_link *link)
877
{
878 879 880 881
	if (!link->own_rx_busy) {
		q921_rr(link, 1, 1);
		link->acknowledge_pending = 0;
		start_t200(link);
882 883 884 885 886
	} else {
		/* XXX: Implement me... */
	}
}

887
static void t200_expire(void *vlink)
Mark Spencer's avatar
Mark Spencer committed
888
{
889
	struct q921_link *link = vlink;
890 891
	struct pri *ctrl;

892
	ctrl = link->ctrl;
893

894 895
	if (ctrl->debug & PRI_DEBUG_Q921_DUMP) {
		pri_message(ctrl, "%s\n", __FUNCTION__);
896
		q921_dump_pri(link, ' ');
897
	}
898

899
	link->t200_timer = 0;
900

901
	switch (link->state) {
902
	case Q921_MULTI_FRAME_ESTABLISHED:
903 904 905 906
		link->RC = 0;
		transmit_enquiry(link);
		link->RC++;
		q921_setstate(link, Q921_TIMER_RECOVERY);
907 908 909
		break;
	case Q921_TIMER_RECOVERY:
		/* SDL Flow Figure B.8/Q.921 Page 81 */
910
		if (link->RC != ctrl->timers[PRI_TIMER_N200]) {
911
#if 0
912 913
			if (link->v_s == link->v_a) {
				transmit_enquiry(link);
914
			}
915 916
#else
			/* We are chosing to enquiry by default (to reduce risk of T200 timer errors at the other
917
			 * side, instead of retransmission of the last I-frame we sent */
918
			transmit_enquiry(link);
919
#endif
920
			link->RC++;
921
		} else {
922 923
			q921_mdl_error(link, 'I');
			q921_establish_data_link(link);
924
			link->l3_initiated = 0;
925
			q921_setstate(link, Q921_AWAITING_ESTABLISHMENT);
926 927 928 929
			if (PTP_MODE(ctrl)) {
				ctrl->schedev = 1;
				ctrl->ev.gen.e = PRI_EVENT_DCHAN_DOWN;
			}
930
		}
931 932
		break;
	case Q921_AWAITING_ESTABLISHMENT:
933 934 935 936
		if (link->RC != ctrl->timers[PRI_TIMER_N200]) {
			link->RC++;
			q921_send_sabme(link);
			start_t200(link);
937
		} else {
938
			q921_check_delay_restart(link);
939 940 941
			q921_discard_iqueue(link);
			q921_mdl_error(link, 'G');
			q921_setstate(link, Q921_TEI_ASSIGNED);
942
			/* DL-RELEASE indication */
943
			q931_dl_event(link, Q931_DL_EVENT_DL_RELEASE_IND);
944
		}
945
		break;
946
	case Q921_AWAITING_RELEASE:
947 948 949 950
		if (link->RC != ctrl->timers[PRI_TIMER_N200]) {
			++link->RC;
			q921_send_disc(link, 1);
			start_t200(link);
951
		} else {
952
			q921_check_delay_restart(link);
953
			q921_mdl_error(link, 'H');
954
			/* DL-RELEASE confirm */
955 956
			q931_dl_event(link, Q931_DL_EVENT_DL_RELEASE_CONFIRM);
			q921_setstate(link, Q921_TEI_ASSIGNED);
957 958
		}
		break;
959
	default:
960 961
		/* Looks like someone forgot to stop the T200 timer. */
		pri_error(ctrl, "T200 expired in state %d(%s)\n",
962
			link->state, q921_state2str(link->state));
963
		break;
Mark Spencer's avatar
Mark Spencer committed
964 965 966
	}
}

967
/* This is sending a DL-UNIT-DATA request */
968
int q921_transmit_uiframe(struct q921_link *link, void *buf, int len)
969 970 971
{
	uint8_t ubuf[512];
	q921_h *h = (void *)&ubuf[0];
972 973
	struct pri *ctrl;

974
	ctrl = link->ctrl;
975 976

	if (len >= 512) {
977
		pri_error(ctrl, "Requested to send UI-frame larger than 512 bytes!\n");
978 979 980 981 982 983 984
		return -1;
	}

	memset(ubuf, 0, sizeof(ubuf));
	h->h.sapi = 0;
	h->h.ea1 = 0;
	h->h.ea2 = 1;
985
	h->h.tei = link->tei;
986 987 988 989 990
	h->u.m3 = 0;
	h->u.m2 = 0;
	h->u.p_f = 0;	/* Poll bit set */
	h->u.ft = Q921_FRAMETYPE_U;

991
	switch (ctrl->localtype) {
992 993 994 995 996 997 998
	case PRI_NETWORK:
		h->h.c_r = 1;
		break;
	case PRI_CPE:
		h->h.c_r = 0;
		break;
	default:
999
		pri_error(ctrl, "Don't know how to UI-frame on a type %d node\n", ctrl->localtype);
1000 1001 1002 1003 1004
		return -1;
	}

	memcpy(h->u.data, buf, len);

1005
	q921_transmit(ctrl, h, len + 3);
1006 1007 1008 1009

	return 0;
}

1010
static struct q921_link *pri_find_tei(struct pri *ctrl, int sapi, int tei)
1011
{
1012 1013 1014 1015 1016
	struct q921_link *link;

	for (link = &ctrl->link; link; link = link->next) {
		if (link->tei == tei && link->sapi == sapi)
			return link;
1017 1018 1019 1020 1021
	}

	return NULL;
}

1022
/* This is the equivalent of a DL-DATA request, as well as the I-frame queued up outcome */
1023
int q921_transmit_iframe(struct q921_link *link, void *buf, int len, int cr)
Mark Spencer's avatar
Mark Spencer committed
1024
{
1025
	struct q921_frame *f, *prev=NULL;
1026
	struct pri *ctrl;
1027

1028
	ctrl = link->ctrl;
1029

1030 1031 1032
	if (PTMP_MODE(ctrl)) {
		if (link->tei == Q921_TEI_GROUP) {
			pri_error(ctrl, "Huh?! For PTMP, we shouldn't be sending I-frames out the group TEI\n");
1033
			return 0;
Mark Spencer's avatar
Mark Spencer committed
1034
		}
1035
		if (BRI_TE_PTMP(ctrl)) {
1036
			switch (link->state) {
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
			case Q921_TEI_UNASSIGNED:
				q921_setstate(link, Q921_ESTABLISH_AWAITING_TEI);
				q921_tei_request(link);
				break;
			case Q921_ASSIGN_AWAITING_TEI:
				q921_setstate(link, Q921_ESTABLISH_AWAITING_TEI);
				break;
			default:
				break;
			}
1047 1048
		}
	} else {
1049
		/* PTP modes, which shouldn't have subs */
1050 1051 1052
	}

	/* Figure B.7/Q.921 Page 70 */
1053
	switch (link->state) {
1054 1055
	case Q921_TEI_ASSIGNED:
		/* If we aren't in a state compatiable with DL-DATA requests, start getting us there here */
1056
		restart_timer_stop(link);
1057
		q921_establish_data_link(link);
1058
		link->l3_initiated = 1;
1059
		q921_setstate(link, Q921_AWAITING_ESTABLISHMENT);
1060 1061 1062 1063 1064
		/* For all rest, we've done the work to get us up prior to this and fall through */
	case Q921_ESTABLISH_AWAITING_TEI:
	case Q921_TIMER_RECOVERY:
	case Q921_AWAITING_ESTABLISHMENT:
	case Q921_MULTI_FRAME_ESTABLISHED:
1065
		/* Find queue tail. */
1066
		for (f = link->tx_queue; f; f = f->next) {
1067 1068 1069
			prev = f;
		}

1070
		f = calloc(1, sizeof(struct q921_frame) + len + 2);
1071
		if (f) {
1072
			Q921_INIT(&f->h, link->sapi, link->tei);
1073
			switch (ctrl->localtype) {
1074 1075 1076 1077 1078
			case PRI_NETWORK:
				if (cr)
					f->h.h.c_r = 1;
				else
					f->h.h.c_r = 0;
1079
				break;
1080 1081 1082 1083 1084
			case PRI_CPE:
				if (cr)
					f->h.h.c_r = 0;
				else
					f->h.h.c_r = 1;
1085
				break;
1086
			}
1087 1088

			/* Put new frame on queue tail. */
1089
			f->next = NULL;
1090
			f->status = Q921_TX_FRAME_NEVER_SENT;
1091 1092 1093 1094 1095
			f->len = len + 4;
			memcpy(f->h.data, buf, len);
			if (prev)
				prev->next = f;
			else
1096
				link->tx_queue = f;
1097

1098
			if (link->state != Q921_MULTI_FRAME_ESTABLISHED) {
1099
				if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
1100
					pri_message(ctrl,
1101
						"TEI=%d Just queued I-frame since in state %d(%s)\n",
1102
						link->tei,
1103
						link->state, q921_state2str(link->state));
1104
				}
1105
				break;
1106
			}
1107
			if (link->peer_rx_busy) {
1108 1109
				if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
					pri_message(ctrl,
1110
						"TEI=%d Just queued I-frame due to peer busy condition\n",
1111
						link->tei);
1112
				}
1113
				break;
1114
			}
1115

1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
			if (!q921_send_queued_iframes(link)) {
				/*
				 * No frames sent even though we just put a frame on the queue.
				 *
				 * Special debug message/test here because we want to say what
				 * happened to the Q.931 message just queued but we don't want
				 * to flood the debug trace if we are not really looking at the
				 * Q.921 layer.
				 */
				if ((ctrl->debug & (PRI_DEBUG_Q921_STATE | PRI_DEBUG_Q921_DUMP))
					== PRI_DEBUG_Q921_STATE) {
					pri_message(ctrl, "TEI=%d Just queued I-frame due to window shut\n",
						link->tei);
				}
			}
1131
		} else {
1132
			pri_error(ctrl, "!! Out of memory for Q.921 transmit\n");
1133
			return -1;
1134
		}
1135
		break;
1136 1137
	case Q921_TEI_UNASSIGNED:
	case Q921_ASSIGN_AWAITING_TEI:
1138
	case Q921_AWAITING_RELEASE:
1139
	default:
1140
		pri_error(ctrl, "Cannot transmit frames in state %d(%s)\n",
1141
			link->state, q921_state2str(link->state));
1142
		break;
Mark Spencer's avatar
Mark Spencer committed
1143 1144 1145 1146
	}
	return 0;
}

1147
static void t203_expire(void *vlink)
Mark Spencer's avatar
Mark Spencer committed
1148
{
1149
	struct q921_link *link = vlink;
1150 1151
	struct pri *ctrl;

1152
	ctrl = link->ctrl;
1153

1154 1155
	if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
		pri_message(ctrl, "%s\n", __FUNCTION__);
1156

1157
	link->t203_timer = 0;
1158

1159
	switch (link->state) {
1160
	case Q921_MULTI_FRAME_ESTABLISHED:
1161 1162