1 /*
2  * libpri: An implementation of Primary Rate ISDN
3  *
4  * Written by Mark Spencer <markster@digium.com>
5  *
6  * Copyright (C) 2001-2005, Digium, Inc.
7  * All Rights Reserved.
8  */
9 
10 /*
11  * See http://www.asterisk.org for more information about
12  * the Asterisk project. Please do not directly contact
13  * any of the maintainers of this project for assistance;
14  * the project provides a web site, mailing lists and IRC
15  * channels for your use.
16  *
17  * This program is free software, distributed under the terms of
18  * the GNU General Public License Version 2 as published by the
19  * Free Software Foundation. See the LICENSE file included with
20  * this program for more details.
21  *
22  * In addition, when this program is distributed with Asterisk in
23  * any form that would qualify as a 'combined work' or as a
24  * 'derivative work' (but not mere aggregation), you can redistribute
25  * and/or modify the combination under the terms of the license
26  * provided with that copy of Asterisk, instead of the license
27  * terms granted here.
28  */
29 
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include "compat.h"
37 #include "libpri.h"
38 #include "pri_internal.h"
39 #include "pri_q921.h"
40 #include "pri_q931.h"
41 
42 /*
43  * Define RANDOM_DROPS To randomly drop packets in order to simulate loss for testing
44  * retransmission functionality
45  */
46 //#define RANDOM_DROPS	1
47 
48 #define Q921_INIT(fr, l_sapi, l_tei) \
49 	do { \
50 		(fr)->h.sapi = l_sapi; \
51 		(fr)->h.ea1 = 0; \
52 		(fr)->h.ea2 = 1; \
53 		(fr)->h.tei = l_tei; \
54 	} while (0)
55 
56 #define Q921_CLEAR_INIT(fr, l_sapi, l_tei) \
57 	do { \
58 		memset((fr), 0, sizeof(*(fr))); \
59 		Q921_INIT((fr), (l_sapi), (l_tei)); \
60 	} while (0)
61 
62 static void q921_dump_pri(struct q921_link *link, char direction_tag);
63 static void q921_establish_data_link(struct q921_link *link);
64 static void q921_mdl_error(struct q921_link *link, char error);
65 static void q921_mdl_remove(struct q921_link *link);
66 static void q921_mdl_destroy(struct q921_link *link);
67 
68 /*!
69  * \internal
70  * \brief Convert Q.921 TEI management message type to a string.
71  *
72  * \param message Q.921 TEI management message type to convert.
73  *
74  * \return TEI management message type name string
75  */
q921_tei_mgmt2str(enum q921_tei_identity message)76 static const char *q921_tei_mgmt2str(enum q921_tei_identity message)
77 {
78 	switch (message) {
79 	case Q921_TEI_IDENTITY_REQUEST:
80 		return "TEI Identity Request";
81 	case Q921_TEI_IDENTITY_ASSIGNED:
82 		return "TEI Identity Assigned";
83 	case Q921_TEI_IDENTITY_CHECK_REQUEST:
84 		return "TEI Identity Check Request";
85 	case Q921_TEI_IDENTITY_REMOVE:
86 		return "TEI Identity Remove";
87 	case Q921_TEI_IDENTITY_DENIED:
88 		return "TEI Identity Denied";
89 	case Q921_TEI_IDENTITY_CHECK_RESPONSE:
90 		return "TEI Identity Check Response";
91 	case Q921_TEI_IDENTITY_VERIFY:
92 		return "TEI Identity Verify";
93 	}
94 
95 	return "Unknown";
96 }
97 
98 /*!
99  * \internal
100  * \brief Convert Q.921 state to a string.
101  *
102  * \param state Q.921 state to convert.
103  *
104  * \return State name string
105  */
q921_state2str(enum q921_state state)106 static const char *q921_state2str(enum q921_state state)
107 {
108 	switch (state) {
109 	case Q921_TEI_UNASSIGNED:
110 		return "TEI unassigned";
111 	case Q921_ASSIGN_AWAITING_TEI:
112 		return "Assign awaiting TEI";
113 	case Q921_ESTABLISH_AWAITING_TEI:
114 		return "Establish awaiting TEI";
115 	case Q921_TEI_ASSIGNED:
116 		return "TEI assigned";
117 	case Q921_AWAITING_ESTABLISHMENT:
118 		return "Awaiting establishment";
119 	case Q921_AWAITING_RELEASE:
120 		return "Awaiting release";
121 	case Q921_MULTI_FRAME_ESTABLISHED:
122 		return "Multi-frame established";
123 	case Q921_TIMER_RECOVERY:
124 		return "Timer recovery";
125 	}
126 
127 	return "Unknown state";
128 }
129 
q921_setstate(struct q921_link * link,int newstate)130 static void q921_setstate(struct q921_link *link, int newstate)
131 {
132 	struct pri *ctrl;
133 
134 	ctrl = link->ctrl;
135 	if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
136 		/*
137 		 * Suppress displaying these state transitions:
138 		 * Q921_MULTI_FRAME_ESTABLISHED <--> Q921_TIMER_RECOVERY
139 		 *
140 		 * Q921 keeps flipping back and forth between these two states
141 		 * when it has nothing better to do.
142 		 */
143 		switch (link->state) {
144 		case Q921_MULTI_FRAME_ESTABLISHED:
145 		case Q921_TIMER_RECOVERY:
146 			switch (newstate) {
147 			case Q921_MULTI_FRAME_ESTABLISHED:
148 			case Q921_TIMER_RECOVERY:
149 				/* Suppress displaying this state transition. */
150 				link->state = newstate;
151 				return;
152 			default:
153 				break;
154 			}
155 			break;
156 		default:
157 			break;
158 		}
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 
q921_discard_iqueue(struct q921_link * link)168 static void q921_discard_iqueue(struct q921_link *link)
169 {
170 	struct q921_frame *f, *p;
171 
172 	f = link->tx_queue;
173 	while (f) {
174 		p = f;
175 		f = f->next;
176 		/* Free frame */
177 		free(p);
178 	}
179 	link->tx_queue = NULL;
180 }
181 
q921_transmit(struct pri * ctrl,q921_h * h,int len)182 static int q921_transmit(struct pri *ctrl, q921_h *h, int len)
183 {
184 	int res;
185 
186 #ifdef RANDOM_DROPS
187 	if (!(random() % 3)) {
188 		pri_message(ctrl, " === Dropping Packet ===\n");
189 		return 0;
190 	}
191 #endif
192 	ctrl->q921_txcount++;
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);
196 	/* Write an extra two bytes for the FCS */
197 	res = ctrl->write_func ? ctrl->write_func(ctrl, h, len + 2) : 0;
198 	if (res != (len + 2)) {
199 		pri_error(ctrl, "Short write: %d/%d (%s)\n", res, len + 2, strerror(errno));
200 		return -1;
201 	}
202 	return 0;
203 }
204 
q921_mdl_send(struct pri * ctrl,enum q921_tei_identity message,int ri,int ai,int iscommand)205 static void q921_mdl_send(struct pri *ctrl, enum q921_tei_identity message, int ri, int ai, int iscommand)
206 {
207 	q921_u *f;
208 
209 	if (!(f = calloc(1, sizeof(*f) + 5)))
210 		return;
211 
212 	Q921_INIT(f, Q921_SAPI_LAYER2_MANAGEMENT, Q921_TEI_GROUP);
213 	f->h.c_r = (ctrl->localtype == PRI_NETWORK) ? iscommand : !iscommand;
214 	f->ft = Q921_FRAMETYPE_U;
215 	f->data[0] = 0x0f;	/* Management entity */
216 	f->data[1] = (ri >> 8) & 0xff;
217 	f->data[2] = ri & 0xff;
218 	f->data[3] = message;
219 	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 	free(f);
227 }
228 
t202_expire(void * vlink)229 static void t202_expire(void *vlink)
230 {
231 	struct q921_link *link = vlink;
232 	struct pri *ctrl;
233 
234 	ctrl = link->ctrl;
235 
236 	/* Start the TEI request timer. */
237 	pri_schedule_del(ctrl, link->t202_timer);
238 	link->t202_timer =
239 		pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T202], t202_expire, link);
240 
241 	if (ctrl->l2_persistence != PRI_L2_PERSISTENCE_KEEP_UP) {
242 		/* Only try to get a TEI for N202 times if layer 2 is not persistent. */
243 		++link->n202_counter;
244 	}
245 	if (!link->t202_timer || link->n202_counter > ctrl->timers[PRI_TIMER_N202]) {
246 		if (!link->t202_timer) {
247 			pri_error(ctrl, "Could not start T202 timer.");
248 		} else {
249 			pri_schedule_del(ctrl, link->t202_timer);
250 			link->t202_timer = 0;
251 		}
252 		pri_error(ctrl, "Unable to receive TEI from network in state %d(%s)!\n",
253 			link->state, q921_state2str(link->state));
254 		switch (link->state) {
255 		case Q921_ASSIGN_AWAITING_TEI:
256 			break;
257 		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 			break;
262 		default:
263 			break;
264 		}
265 		q921_setstate(link, Q921_TEI_UNASSIGNED);
266 		return;
267 	}
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 
q921_tei_request(struct q921_link * link)274 static void q921_tei_request(struct q921_link *link)
275 {
276 	link->n202_counter = 0;
277 	t202_expire(link);
278 }
279 
q921_tei_remove(struct pri * ctrl,int tei)280 static void q921_tei_remove(struct pri *ctrl, int tei)
281 {
282 	/*
283 	 * Q.921 Section 5.3.2 says we should send the remove message
284 	 * twice, in case of message loss.
285 	 */
286 	q921_mdl_send(ctrl, Q921_TEI_IDENTITY_REMOVE, 0, tei, 1);
287 	q921_mdl_send(ctrl, Q921_TEI_IDENTITY_REMOVE, 0, tei, 1);
288 }
289 
q921_send_dm(struct q921_link * link,int fbit)290 static void q921_send_dm(struct q921_link *link, int fbit)
291 {
292 	q921_h h;
293 	struct pri *ctrl;
294 
295 	ctrl = link->ctrl;
296 
297 	Q921_CLEAR_INIT(&h, link->sapi, link->tei);
298 	h.u.m3 = 0;	/* M3 = 0 */
299 	h.u.m2 = 3;	/* M2 = 3 */
300 	h.u.p_f = fbit;	/* Final set appropriately */
301 	h.u.ft = Q921_FRAMETYPE_U;
302 	switch (ctrl->localtype) {
303 	case PRI_NETWORK:
304 		h.h.c_r = 0;
305 		break;
306 	case PRI_CPE:
307 		h.h.c_r = 1;
308 		break;
309 	default:
310 		pri_error(ctrl, "Don't know how to DM on a type %d node\n", ctrl->localtype);
311 		return;
312 	}
313 	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 
q921_send_disc(struct q921_link * link,int pbit)319 static void q921_send_disc(struct q921_link *link, int pbit)
320 {
321 	q921_h h;
322 	struct pri *ctrl;
323 
324 	ctrl = link->ctrl;
325 
326 	Q921_CLEAR_INIT(&h, link->sapi, link->tei);
327 	h.u.m3 = 2;	/* M3 = 2 */
328 	h.u.m2 = 0;	/* M2 = 0 */
329 	h.u.p_f = pbit;	/* Poll set appropriately */
330 	h.u.ft = Q921_FRAMETYPE_U;
331 	switch (ctrl->localtype) {
332 	case PRI_NETWORK:
333 		h.h.c_r = 0;
334 		break;
335 	case PRI_CPE:
336 		h.h.c_r = 1;
337 		break;
338 	default:
339 		pri_error(ctrl, "Don't know how to DISC on a type %d node\n", ctrl->localtype);
340 		return;
341 	}
342 	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 
q921_send_ua(struct q921_link * link,int fbit)348 static void q921_send_ua(struct q921_link *link, int fbit)
349 {
350 	q921_h h;
351 	struct pri *ctrl;
352 
353 	ctrl = link->ctrl;
354 
355 	Q921_CLEAR_INIT(&h, link->sapi, link->tei);
356 	h.u.m3 = 3;		/* M3 = 3 */
357 	h.u.m2 = 0;		/* M2 = 0 */
358 	h.u.p_f = fbit;	/* Final set appropriately */
359 	h.u.ft = Q921_FRAMETYPE_U;
360 	switch (ctrl->localtype) {
361 	case PRI_NETWORK:
362 		h.h.c_r = 0;
363 		break;
364 	case PRI_CPE:
365 		h.h.c_r = 1;
366 		break;
367 	default:
368 		pri_error(ctrl, "Don't know how to UA on a type %d node\n", ctrl->localtype);
369 		return;
370 	}
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);
375 }
376 
q921_send_sabme(struct q921_link * link)377 static void q921_send_sabme(struct q921_link *link)
378 {
379 	q921_h h;
380 	struct pri *ctrl;
381 
382 	ctrl = link->ctrl;
383 
384 	Q921_CLEAR_INIT(&h, link->sapi, link->tei);
385 	h.u.m3 = 3;	/* M3 = 3 */
386 	h.u.m2 = 3;	/* M2 = 3 */
387 	h.u.p_f = 1;	/* Poll bit set */
388 	h.u.ft = Q921_FRAMETYPE_U;
389 	switch (ctrl->localtype) {
390 	case PRI_NETWORK:
391 		h.h.c_r = 1;
392 		break;
393 	case PRI_CPE:
394 		h.h.c_r = 0;
395 		break;
396 	default:
397 		pri_error(ctrl, "Don't know how to SABME on a type %d node\n", ctrl->localtype);
398 		return;
399 	}
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);
404 }
405 
q921_ack_packet(struct q921_link * link,int num)406 static int q921_ack_packet(struct q921_link *link, int num)
407 {
408 	struct q921_frame *f;
409 	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 			break;
417 		}
418 		if (f->h.n_s == num) {
419 			/* Cancel each packet as necessary */
420 			/* That's our packet */
421 			if (prev)
422 				prev->next = f->next;
423 			else
424 				link->tx_queue = f->next;
425 			if (ctrl->debug & PRI_DEBUG_Q921_DUMP) {
426 				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 							: -2
433 						: -1);
434 			}
435 			/* Update v_a */
436 			free(f);
437 			return 1;
438 		}
439 	}
440 	return 0;
441 }
442 
443 static void t203_expire(void *vlink);
444 static void t200_expire(void *vlink);
445 
446 #define restart_t200(link) reschedule_t200(link)
reschedule_t200(struct q921_link * link)447 static void reschedule_t200(struct q921_link *link)
448 {
449 	struct pri *ctrl;
450 
451 	ctrl = link->ctrl;
452 
453 	if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
454 		pri_message(ctrl, "-- Restarting T200 timer\n");
455 	pri_schedule_del(ctrl, link->t200_timer);
456 	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 	struct pri *ctrl;
463 
464 	ctrl = link->ctrl;
465 
466 	if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
467 		pri_message(ctrl, "-- Restarting T203 timer\n");
468 	pri_schedule_del(ctrl, link->t203_timer);
469 	link->t203_timer = pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T203], t203_expire, link);
470 }
471 #endif
472 
start_t203(struct q921_link * link)473 static void start_t203(struct q921_link *link)
474 {
475 	struct pri *ctrl;
476 
477 	ctrl = link->ctrl;
478 
479 	if (link->t203_timer) {
480 		if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
481 			pri_message(ctrl, "T203 requested to start without stopping first\n");
482 		pri_schedule_del(ctrl, link->t203_timer);
483 	}
484 	if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
485 		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 
stop_t203(struct q921_link * link)489 static void stop_t203(struct q921_link *link)
490 {
491 	struct pri *ctrl;
492 
493 	ctrl = link->ctrl;
494 
495 	if (link->t203_timer) {
496 		if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
497 			pri_message(ctrl, "-- Stopping T203 timer\n");
498 		pri_schedule_del(ctrl, link->t203_timer);
499 		link->t203_timer = 0;
500 	} else {
501 		if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
502 			pri_message(ctrl, "-- T203 requested to stop when not started\n");
503 	}
504 }
505 
start_t200(struct q921_link * link)506 static void start_t200(struct q921_link *link)
507 {
508 	struct pri *ctrl;
509 
510 	ctrl = link->ctrl;
511 
512 	if (link->t200_timer) {
513 		if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
514 			pri_message(ctrl, "T200 requested to start without stopping first\n");
515 		pri_schedule_del(ctrl, link->t200_timer);
516 	}
517 	if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
518 		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 
stop_t200(struct q921_link * link)522 static void stop_t200(struct q921_link *link)
523 {
524 	struct pri *ctrl;
525 
526 	ctrl = link->ctrl;
527 
528 	if (link->t200_timer) {
529 		if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
530 			pri_message(ctrl, "-- Stopping T200 timer\n");
531 		pri_schedule_del(ctrl, link->t200_timer);
532 		link->t200_timer = 0;
533 	} else {
534 		if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
535 			pri_message(ctrl, "-- T200 requested to stop when not started\n");
536 	}
537 }
538 
539 /*!
540  * \internal
541  * \brief Initiate bringing up layer 2 link.
542  *
543  * \param link Layer 2 link to bring up.
544  *
545  * \return Nothing
546  */
kick_start_link(struct q921_link * link)547 static void kick_start_link(struct q921_link *link)
548 {
549 	struct pri *ctrl;
550 
551 	ctrl = link->ctrl;
552 
553 	switch (link->state) {
554 	case Q921_TEI_UNASSIGNED:
555 		if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
556 			pri_message(ctrl, "Kick starting link from no TEI.\n");
557 		}
558 		q921_setstate(link, Q921_ESTABLISH_AWAITING_TEI);
559 		q921_tei_request(link);
560 		break;
561 	case Q921_ASSIGN_AWAITING_TEI:
562 		if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
563 			pri_message(ctrl, "Kick starting link when awaiting TEI.\n");
564 		}
565 		q921_setstate(link, Q921_ESTABLISH_AWAITING_TEI);
566 		break;
567 	case Q921_TEI_ASSIGNED:
568 		if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
569 			pri_message(ctrl, "SAPI/TEI=%d/%d Kick starting link\n", link->sapi,
570 				link->tei);
571 		}
572 		q921_discard_iqueue(link);
573 		q921_establish_data_link(link);
574 		link->l3_initiated = 1;
575 		q921_setstate(link, Q921_AWAITING_ESTABLISHMENT);
576 		break;
577 	default:
578 		break;
579 	}
580 }
581 
restart_timer_expire(void * vlink)582 static void restart_timer_expire(void *vlink)
583 {
584 	struct q921_link *link = vlink;
585 	struct pri *ctrl;
586 
587 	ctrl = link->ctrl;
588 
589 	link->restart_timer = 0;
590 
591 	switch (link->state) {
592 	case Q921_TEI_UNASSIGNED:
593 	case Q921_ASSIGN_AWAITING_TEI:
594 	case Q921_TEI_ASSIGNED:
595 		/* Try to bring layer 2 up. */
596 		kick_start_link(link);
597 		break;
598 	default:
599 		/* Looks like someone forgot to stop the restart timer. */
600 		pri_error(ctrl, "SAPI/TEI=%d/%d Link restart delay timer expired in state %d(%s)\n",
601 			link->sapi, link->tei, link->state, q921_state2str(link->state));
602 		break;
603 	}
604 }
605 
restart_timer_stop(struct q921_link * link)606 static void restart_timer_stop(struct q921_link *link)
607 {
608 	struct pri *ctrl;
609 
610 	ctrl = link->ctrl;
611 	pri_schedule_del(ctrl, link->restart_timer);
612 	link->restart_timer = 0;
613 }
614 
615 /*! \note Only call on the transition to state Q921_TEI_ASSIGNED or already there. */
restart_timer_start(struct q921_link * link)616 static void restart_timer_start(struct q921_link *link)
617 {
618 	struct pri *ctrl;
619 
620 	ctrl = link->ctrl;
621 
622 	if (ctrl->debug & PRI_DEBUG_Q921_DUMP) {
623 		pri_message(ctrl, "SAPI/TEI=%d/%d Starting link restart delay timer\n",
624 			link->sapi, link->tei);
625 	}
626 	pri_schedule_del(ctrl, link->restart_timer);
627 	link->restart_timer =
628 		pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T200], restart_timer_expire, link);
629 }
630 
631 /*! \note Only call on the transition to state Q921_TEI_ASSIGNED or already there. */
q921_check_delay_restart(struct q921_link * link)632 static pri_event *q921_check_delay_restart(struct q921_link *link)
633 {
634 	pri_event *ev;
635 	struct pri *ctrl;
636 
637 	ctrl = link->ctrl;
638 
639 	if (ctrl->l2_persistence == PRI_L2_PERSISTENCE_KEEP_UP) {
640 		/*
641 		 * For PTP links:
642 		 * This is where we act a bit like L3 instead of L2, since we've
643 		 * got an L3 that depends on us keeping L2 automatically alive
644 		 * and happy.
645 		 *
646 		 * For PTMP links:
647 		 * We can optionally keep L2 automatically alive and happy.
648 		 */
649 		restart_timer_start(link);
650 	}
651 	if (PTP_MODE(ctrl)) {
652 		switch (link->state) {
653 		case Q921_MULTI_FRAME_ESTABLISHED:
654 		case Q921_TIMER_RECOVERY:
655 			/* Notify the upper layer that layer 2 went down. */
656 			ctrl->schedev = 1;
657 			ctrl->ev.gen.e = PRI_EVENT_DCHAN_DOWN;
658 			ev = &ctrl->ev;
659 			break;
660 		default:
661 			ev = NULL;
662 			break;
663 		}
664 	} else {
665 		ev = NULL;
666 	}
667 	return ev;
668 }
669 
670 /*!
671  * \brief Bring all layer 2 links up.
672  *
673  * \param ctrl D channel controller.
674  *
675  * \return Nothing
676  */
q921_bring_layer2_up(struct pri * ctrl)677 void q921_bring_layer2_up(struct pri *ctrl)
678 {
679 	struct q921_link *link;
680 
681 	if (PTMP_MODE(ctrl)) {
682 		/* Don't start with the broadcast link. */
683 		link = ctrl->link.next;
684 	} else {
685 		link = &ctrl->link;
686 	}
687 	for (; link; link = link->next) {
688 		if (!link->restart_timer) {
689 			/* A restart on the link is not already in the works. */
690 			kick_start_link(link);
691 		}
692 	}
693 }
694 
695 /* This is the equivalent of the I-Frame queued up path in Figure B.7 in MULTI_FRAME_ESTABLISHED */
q921_send_queued_iframes(struct q921_link * link)696 static int q921_send_queued_iframes(struct q921_link *link)
697 {
698 	struct pri *ctrl;
699 	struct q921_frame *f;
700 	int frames_txd = 0;
701 
702 	ctrl = link->ctrl;
703 
704 	for (f = link->tx_queue; f; f = f->next) {
705 		if (f->status != Q921_TX_FRAME_SENT) {
706 			/* This frame needs to be sent. */
707 			break;
708 		}
709 	}
710 	if (!f) {
711 		/* The Tx queue has no pending frames. */
712 		return 0;
713 	}
714 
715 	if (link->peer_rx_busy) {
716 		/* Don't flood debug trace if not really looking at Q.921 layer. */
717 		if (ctrl->debug & (/* PRI_DEBUG_Q921_STATE | */ PRI_DEBUG_Q921_DUMP)) {
718 			pri_message(ctrl,
719 				"TEI=%d Couldn't transmit I-frame at this time due to peer busy condition\n",
720 				link->tei);
721 		}
722 		return 0;
723 	}
724 	if (link->v_s == Q921_ADD(link->v_a, ctrl->timers[PRI_TIMER_K])) {
725 		/* Don't flood debug trace if not really looking at Q.921 layer. */
726 		if (ctrl->debug & (/* PRI_DEBUG_Q921_STATE | */ PRI_DEBUG_Q921_DUMP)) {
727 			pri_message(ctrl,
728 				"TEI=%d Couldn't transmit I-frame at this time due to window shut\n",
729 				link->tei);
730 		}
731 		return 0;
732 	}
733 
734 	/* Send all pending frames that fit in the window. */
735 	for (; f; f = f->next) {
736 		if (link->v_s == Q921_ADD(link->v_a, ctrl->timers[PRI_TIMER_K])) {
737 			/* The window is no longer open. */
738 			break;
739 		}
740 
741 		/* Send it now... */
742 		switch (f->status) {
743 		case Q921_TX_FRAME_NEVER_SENT:
744 			if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
745 				pri_message(ctrl,
746 					"TEI=%d Transmitting N(S)=%d, window is open V(A)=%d K=%d\n",
747 					link->tei, link->v_s, link->v_a, ctrl->timers[PRI_TIMER_K]);
748 			}
749 			break;
750 		case Q921_TX_FRAME_PUSHED_BACK:
751 			if (f->h.n_s != link->v_s) {
752 				/* Should never happen. */
753 				pri_error(ctrl,
754 					"TEI=%d Retransmitting frame with old N(S)=%d as N(S)=%d!\n",
755 					link->tei, f->h.n_s, link->v_s);
756 			} else if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
757 				pri_message(ctrl, "TEI=%d Retransmitting frame N(S)=%d now!\n",
758 					link->tei, link->v_s);
759 			}
760 			break;
761 		default:
762 			/* Should never happen. */
763 			pri_error(ctrl, "Unexpected Tx Q frame status: %d", f->status);
764 			break;
765 		}
766 
767 		/*
768 		 * Send the frame out on the assigned TEI.
769 		 * Done now because the frame may have been queued before we
770 		 * had an assigned TEI.
771 		 */
772 		f->h.h.tei = link->tei;
773 
774 		f->h.n_s = link->v_s;
775 		f->h.n_r = link->v_r;
776 		f->h.ft = 0;
777 		f->h.p_f = 0;
778 		q921_transmit(ctrl, (q921_h *) (&f->h), f->len);
779 		Q921_INC(link->v_s);
780 		++frames_txd;
781 
782 		if ((ctrl->debug & PRI_DEBUG_Q931_DUMP)
783 			&& f->status == Q921_TX_FRAME_NEVER_SENT) {
784 			/*
785 			 * The transmit operation might dump the Q.921 header, so logging
786 			 * the Q.931 message body after the transmit puts the sections of
787 			 * the message in the right order in the log.
788 			 *
789 			 * Also dump the Q.931 part only once instead of for every
790 			 * 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 		link->acknowledge_pending = 0;
799 		if (!link->t200_timer) {
800 			stop_t203(link);
801 			start_t200(link);
802 		}
803 	}
804 
805 	return frames_txd;
806 }
807 
q921_reject(struct q921_link * link,int pf)808 static void q921_reject(struct q921_link *link, int pf)
809 {
810 	q921_h h;
811 	struct pri *ctrl;
812 
813 	ctrl = link->ctrl;
814 
815 	Q921_CLEAR_INIT(&h, link->sapi, link->tei);
816 	h.s.x0 = 0;	/* Always 0 */
817 	h.s.ss = 2;	/* Reject */
818 	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) {
822 	case PRI_NETWORK:
823 		h.h.c_r = 0;
824 		break;
825 	case PRI_CPE:
826 		h.h.c_r = 1;
827 		break;
828 	default:
829 		pri_error(ctrl, "Don't know how to REJ on a type %d node\n", ctrl->localtype);
830 		return;
831 	}
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);
836 }
837 
q921_rr(struct q921_link * link,int pbit,int cmd)838 static void q921_rr(struct q921_link *link, int pbit, int cmd)
839 {
840 	q921_h h;
841 	struct pri *ctrl;
842 
843 	ctrl = link->ctrl;
844 
845 	Q921_CLEAR_INIT(&h, link->sapi, link->tei);
846 	h.s.x0 = 0;	/* Always 0 */
847 	h.s.ss = 0; /* Receive Ready */
848 	h.s.ft = 1;	/* Frametype (01) */
849 	h.s.n_r = link->v_r;	/* N(R) */
850 	h.s.p_f = pbit;		/* Poll/Final set appropriately */
851 	switch (ctrl->localtype) {
852 	case PRI_NETWORK:
853 		if (cmd)
854 			h.h.c_r = 1;
855 		else
856 			h.h.c_r = 0;
857 		break;
858 	case PRI_CPE:
859 		if (cmd)
860 			h.h.c_r = 0;
861 		else
862 			h.h.c_r = 1;
863 		break;
864 	default:
865 		pri_error(ctrl, "Don't know how to RR on a type %d node\n", ctrl->localtype);
866 		return;
867 	}
868 #if 0	/* Don't flood debug trace with RR if not really looking at Q.921 layer. */
869 	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);
874 }
875 
transmit_enquiry(struct q921_link * link)876 static void transmit_enquiry(struct q921_link *link)
877 {
878 	if (!link->own_rx_busy) {
879 		q921_rr(link, 1, 1);
880 		link->acknowledge_pending = 0;
881 		start_t200(link);
882 	} else {
883 		/* XXX: Implement me... */
884 	}
885 }
886 
t200_expire(void * vlink)887 static void t200_expire(void *vlink)
888 {
889 	struct q921_link *link = vlink;
890 	struct pri *ctrl;
891 
892 	ctrl = link->ctrl;
893 
894 	if (ctrl->debug & PRI_DEBUG_Q921_DUMP) {
895 		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 		link->RC = 0;
904 		transmit_enquiry(link);
905 		link->RC++;
906 		q921_setstate(link, Q921_TIMER_RECOVERY);
907 		break;
908 	case Q921_TIMER_RECOVERY:
909 		/* SDL Flow Figure B.8/Q.921 Page 81 */
910 		if (link->RC != ctrl->timers[PRI_TIMER_N200]) {
911 #if 0
912 			if (link->v_s == link->v_a) {
913 				transmit_enquiry(link);
914 			}
915 #else
916 			/* 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 			q921_mdl_error(link, 'I');
923 			q921_establish_data_link(link);
924 			link->l3_initiated = 0;
925 			q921_setstate(link, Q921_AWAITING_ESTABLISHMENT);
926 			if (PTP_MODE(ctrl)) {
927 				ctrl->schedev = 1;
928 				ctrl->ev.gen.e = PRI_EVENT_DCHAN_DOWN;
929 			}
930 		}
931 		break;
932 	case Q921_AWAITING_ESTABLISHMENT:
933 		if (link->RC != ctrl->timers[PRI_TIMER_N200]) {
934 			link->RC++;
935 			q921_send_sabme(link);
936 			start_t200(link);
937 		} else {
938 			q921_check_delay_restart(link);
939 			q921_discard_iqueue(link);
940 			q921_mdl_error(link, 'G');
941 			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 		if (link->RC != ctrl->timers[PRI_TIMER_N200]) {
948 			++link->RC;
949 			q921_send_disc(link, 1);
950 			start_t200(link);
951 		} else {
952 			q921_check_delay_restart(link);
953 			q921_mdl_error(link, 'H');
954 			/* DL-RELEASE confirm */
955 			q931_dl_event(link, Q931_DL_EVENT_DL_RELEASE_CONFIRM);
956 			q921_setstate(link, Q921_TEI_ASSIGNED);
957 		}
958 		break;
959 	default:
960 		/* Looks like someone forgot to stop the T200 timer. */
961 		pri_error(ctrl, "T200 expired in state %d(%s)\n",
962 			link->state, q921_state2str(link->state));
963 		break;
964 	}
965 }
966 
967 /* This is sending a DL-UNIT-DATA request */
q921_transmit_uiframe(struct q921_link * link,void * buf,int len)968 int q921_transmit_uiframe(struct q921_link *link, void *buf, int len)
969 {
970 	uint8_t ubuf[512];
971 	q921_h *h = (void *)&ubuf[0];
972 	struct pri *ctrl;
973 
974 	ctrl = link->ctrl;
975 
976 	if (len >= 512) {
977 		pri_error(ctrl, "Requested to send UI-frame larger than 512 bytes!\n");
978 		return -1;
979 	}
980 
981 	memset(ubuf, 0, sizeof(ubuf));
982 	h->h.sapi = 0;
983 	h->h.ea1 = 0;
984 	h->h.ea2 = 1;
985 	h->h.tei = link->tei;
986 	h->u.m3 = 0;
987 	h->u.m2 = 0;
988 	h->u.p_f = 0;	/* Poll bit set */
989 	h->u.ft = Q921_FRAMETYPE_U;
990 
991 	switch (ctrl->localtype) {
992 	case PRI_NETWORK:
993 		h->h.c_r = 1;
994 		break;
995 	case PRI_CPE:
996 		h->h.c_r = 0;
997 		break;
998 	default:
999 		pri_error(ctrl, "Don't know how to UI-frame on a type %d node\n", ctrl->localtype);
1000 		return -1;
1001 	}
1002 
1003 	memcpy(h->u.data, buf, len);
1004 
1005 	q921_transmit(ctrl, h, len + 3);
1006 
1007 	return 0;
1008 }
1009 
pri_find_tei(struct pri * ctrl,int sapi,int tei)1010 static struct q921_link *pri_find_tei(struct pri *ctrl, int sapi, int tei)
1011 {
1012 	struct q921_link *link;
1013 
1014 	for (link = &ctrl->link; link; link = link->next) {
1015 		if (link->tei == tei && link->sapi == sapi)
1016 			return link;
1017 	}
1018 
1019 	return NULL;
1020 }
1021 
1022 /* This is the equivalent of a DL-DATA request, as well as the I-frame queued up outcome */
q921_transmit_iframe(struct q921_link * link,void * buf,int len,int cr)1023 int q921_transmit_iframe(struct q921_link *link, void *buf, int len, int cr)
1024 {
1025 	struct q921_frame *f, *prev=NULL;
1026 	struct pri *ctrl;
1027 
1028 	ctrl = link->ctrl;
1029 
1030 	if (PTMP_MODE(ctrl)) {
1031 		if (link->tei == Q921_TEI_GROUP) {
1032 			pri_error(ctrl, "Huh?! For PTMP, we shouldn't be sending I-frames out the group TEI\n");
1033 			return 0;
1034 		}
1035 		if (BRI_TE_PTMP(ctrl)) {
1036 			switch (link->state) {
1037 			case Q921_TEI_UNASSIGNED:
1038 				q921_setstate(link, Q921_ESTABLISH_AWAITING_TEI);
1039 				q921_tei_request(link);
1040 				break;
1041 			case Q921_ASSIGN_AWAITING_TEI:
1042 				q921_setstate(link, Q921_ESTABLISH_AWAITING_TEI);
1043 				break;
1044 			default:
1045 				break;
1046 			}
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 	case Q921_TEI_ASSIGNED:
1055 		/* 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 		/* For all rest, we've done the work to get us up prior to this and fall through */
1061 	case Q921_ESTABLISH_AWAITING_TEI:
1062 	case Q921_TIMER_RECOVERY:
1063 	case Q921_AWAITING_ESTABLISHMENT:
1064 	case Q921_MULTI_FRAME_ESTABLISHED:
1065 		/* Find queue tail. */
1066 		for (f = link->tx_queue; f; f = f->next) {
1067 			prev = f;
1068 		}
1069 
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 			case PRI_NETWORK:
1075 				if (cr)
1076 					f->h.h.c_r = 1;
1077 				else
1078 					f->h.h.c_r = 0;
1079 				break;
1080 			case PRI_CPE:
1081 				if (cr)
1082 					f->h.h.c_r = 0;
1083 				else
1084 					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 			f->len = len + 4;
1092 			memcpy(f->h.data, buf, len);
1093 			if (prev)
1094 				prev->next = f;
1095 			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 				if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
1109 					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 			if (!q921_send_queued_iframes(link)) {
1117 				/*
1118 				 * No frames sent even though we just put a frame on the queue.
1119 				 *
1120 				 * Special debug message/test here because we want to say what
1121 				 * happened to the Q.931 message just queued but we don't want
1122 				 * to flood the debug trace if we are not really looking at the
1123 				 * Q.921 layer.
1124 				 */
1125 				if ((ctrl->debug & (PRI_DEBUG_Q921_STATE | PRI_DEBUG_Q921_DUMP))
1126 					== PRI_DEBUG_Q921_STATE) {
1127 					pri_message(ctrl, "TEI=%d Just queued I-frame due to window shut\n",
1128 						link->tei);
1129 				}
1130 			}
1131 		} else {
1132 			pri_error(ctrl, "!! Out of memory for Q.921 transmit\n");
1133 			return -1;
1134 		}
1135 		break;
1136 	case Q921_TEI_UNASSIGNED:
1137 	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;
1143 	}
1144 	return 0;
1145 }
1146 
t203_expire(void * vlink)1147 static void t203_expire(void *vlink)
1148 {
1149 	struct q921_link *link = vlink;
1150 	struct pri *ctrl;
1151 
1152 	ctrl = link->ctrl;
1153 
1154 	if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
1155 		pri_message(ctrl, "%s\n", __FUNCTION__);
1156 
1157 	link->t203_timer = 0;
1158 
1159 	switch (link->state) {
1160 	case Q921_MULTI_FRAME_ESTABLISHED:
1161 		transmit_enquiry(link);
1162 		link->RC = 0;
1163 		q921_setstate(link, Q921_TIMER_RECOVERY);
1164 		break;
1165 	default:
1166 		/* Looks like someone forgot to stop the T203 timer. */
1167 		pri_error(ctrl, "T203 expired in state %d(%s)\n",
1168 			link->state, q921_state2str(link->state));
1169 		break;
1170 	}
1171 }
1172 
q921_dump_iqueue_info(struct q921_link * link)1173 static void q921_dump_iqueue_info(struct q921_link *link)
1174 {
1175 	struct pri *ctrl;
1176 	struct q921_frame *f;
1177 	int pending = 0;
1178 	int unacked = 0;
1179 
1180 	ctrl = link->ctrl;
1181 
1182 	for (f = link->tx_queue; f; f = f->next) {
1183 		if (f->status == Q921_TX_FRAME_SENT) {
1184 			unacked++;
1185 		} else {
1186 			pending++;
1187 		}
1188 	}
1189 
1190 	pri_error(ctrl, "Number of pending packets %d, sent but unacked %d\n", pending, unacked);
1191 }
1192 
1193 static void q921_dump_pri_by_h(struct pri *ctrl, char direction_tag, q921_h *h);
1194 
q921_dump(struct pri * ctrl,q921_h * h,int len,int debugflags,int txrx)1195 void q921_dump(struct pri *ctrl, q921_h *h, int len, int debugflags, int txrx)
1196 {
1197 	int x;
1198 	const char *type;
1199 	char direction_tag;
1200 
1201 	direction_tag = txrx ? '>' : '<';
1202 
1203 	pri_message(ctrl, "\n");
1204 	if (debugflags & PRI_DEBUG_Q921_DUMP) {
1205 		q921_dump_pri_by_h(ctrl, direction_tag, h);
1206 	}
1207 
1208 	if (debugflags & PRI_DEBUG_Q921_RAW) {
1209 		char *buf = malloc(len * 3 + 1);
1210 		int buflen = 0;
1211 		if (buf) {
1212 			for (x=0;x<len;x++)
1213 				buflen += sprintf(buf + buflen, "%02x ", h->raw[x]);
1214 			pri_message(ctrl, "%c [ %s]\n", direction_tag, buf);
1215 			free(buf);
1216 		}
1217 	}
1218 
1219 	if (debugflags & PRI_DEBUG_Q921_DUMP) {
1220 		switch (h->h.data[0] & Q921_FRAMETYPE_MASK) {
1221 		case 0:
1222 		case 2:
1223 			pri_message(ctrl, "%c Informational frame:\n", direction_tag);
1224 			break;
1225 		case 1:
1226 			pri_message(ctrl, "%c Supervisory frame:\n", direction_tag);
1227 			break;
1228 		case 3:
1229 			pri_message(ctrl, "%c Unnumbered frame:\n", direction_tag);
1230 			break;
1231 		}
1232 
1233 		pri_message(ctrl, "%c SAPI: %02d  C/R: %d EA: %d\n",
1234 			direction_tag,
1235 			h->h.sapi,
1236 			h->h.c_r,
1237 			h->h.ea1);
1238 		pri_message(ctrl, "%c  TEI: %03d        EA: %d\n",
1239 			direction_tag,
1240 			h->h.tei,
1241 			h->h.ea2);
1242 
1243 		switch (h->h.data[0] & Q921_FRAMETYPE_MASK) {
1244 		case 0:
1245 		case 2:
1246 			/* Informational frame */
1247 			pri_message(ctrl, "%c N(S): %03d   0: %d\n",
1248 				direction_tag,
1249 				h->i.n_s,
1250 				h->i.ft);
1251 			pri_message(ctrl, "%c N(R): %03d   P: %d\n",
1252 				direction_tag,
1253 				h->i.n_r,
1254 				h->i.p_f);
1255 			pri_message(ctrl, "%c %d bytes of data\n",
1256 				direction_tag,
1257 				len - 4);
1258 			break;
1259 		case 1:
1260 			/* Supervisory frame */
1261 			type = "???";
1262 			switch (h->s.ss) {
1263 			case 0:
1264 				type = "RR (receive ready)";
1265 				break;
1266 			case 1:
1267 				type = "RNR (receive not ready)";
1268 				break;
1269 			case 2:
1270 				type = "REJ (reject)";
1271 				break;
1272 			}
1273 			pri_message(ctrl, "%c Zero: %d     S: %d 01: %d  [ %s ]\n",
1274 				direction_tag,
1275 				h->s.x0,
1276 				h->s.ss,
1277 				h->s.ft,
1278 				type);
1279 			pri_message(ctrl, "%c N(R): %03d P/F: %d\n",
1280 				direction_tag,
1281 				h->s.n_r,
1282 				h->s.p_f);
1283 			pri_message(ctrl, "%c %d bytes of data\n",
1284 				direction_tag,
1285 				len - 4);
1286 			break;
1287 		case 3:
1288 			/* Unnumbered frame */
1289 			type = "???";
1290 			if (h->u.ft == 3) {
1291 				switch (h->u.m3) {
1292 				case 0:
1293 					if (h->u.m2 == 3)
1294 						type = "DM (disconnect mode)";
1295 					else if (h->u.m2 == 0)
1296 						type = "UI (unnumbered information)";
1297 					break;
1298 				case 2:
1299 					if (h->u.m2 == 0)
1300 						type = "DISC (disconnect)";
1301 					break;
1302 				case 3:
1303 					if (h->u.m2 == 3)
1304 						type = "SABME (set asynchronous balanced mode extended)";
1305 					else if (h->u.m2 == 0)
1306 						type = "UA (unnumbered acknowledgement)";
1307 					break;
1308 				case 4:
1309 					if (h->u.m2 == 1)
1310 						type = "FRMR (frame reject)";
1311 					break;
1312 				case 5:
1313 					if (h->u.m2 == 3)
1314 						type = "XID (exchange identification note)";
1315 					break;
1316 				default:
1317 					break;
1318 				}
1319 			}
1320 			pri_message(ctrl, "%c   M3: %d   P/F: %d M2: %d 11: %d  [ %s ]\n",
1321 				direction_tag,
1322 				h->u.m3,
1323 				h->u.p_f,
1324 				h->u.m2,
1325 				h->u.ft,
1326 				type);
1327 			pri_message(ctrl, "%c %d bytes of data\n",
1328 				direction_tag,
1329 				len - 3);
1330 			break;
1331 		}
1332 
1333 		if ((h->u.ft == 3) && (h->u.m3 == 0) && (h->u.m2 == 0) && (h->u.data[0] == 0x0f)) {
1334 			int ri;
1335 			u_int8_t *action;
1336 
1337 			/* TEI management related */
1338 			type = q921_tei_mgmt2str(h->u.data[3]);
1339 			pri_message(ctrl, "%c MDL Message: %d(%s)\n", direction_tag, h->u.data[3], type);
1340 			ri = (h->u.data[1] << 8) | h->u.data[2];
1341 			pri_message(ctrl, "%c Ri: %d\n", direction_tag, ri);
1342 			action = &h->u.data[4];
1343 			for (x = len - (action - (u_int8_t *) h); 0 < x; --x, ++action) {
1344 				pri_message(ctrl, "%c Ai: %d E:%d\n",
1345 					direction_tag, (*action >> 1) & 0x7f, *action & 0x01);
1346 			}
1347 		}
1348 	}
1349 }
1350 
q921_dump_pri(struct q921_link * link,char direction_tag)1351 static void q921_dump_pri(struct q921_link *link, char direction_tag)
1352 {
1353 	struct pri *ctrl;
1354 
1355 	ctrl = link->ctrl;
1356 
1357 	pri_message(ctrl, "%c TEI: %d State %d(%s)\n",
1358 		direction_tag, link->tei, link->state, q921_state2str(link->state));
1359 	pri_message(ctrl, "%c V(A)=%d, V(S)=%d, V(R)=%d\n",
1360 		direction_tag, link->v_a, link->v_s, link->v_r);
1361 	pri_message(ctrl, "%c K=%d, RC=%d, l3_initiated=%d, reject_except=%d, ack_pend=%d\n",
1362 		direction_tag, ctrl->timers[PRI_TIMER_K], link->RC, link->l3_initiated,
1363 		link->reject_exception, link->acknowledge_pending);
1364 	pri_message(ctrl, "%c T200_id=%d, N200=%d, T203_id=%d\n",
1365 		direction_tag, link->t200_timer, ctrl->timers[PRI_TIMER_N200], link->t203_timer);
1366 }
1367 
q921_dump_pri_by_h(struct pri * ctrl,char direction_tag,q921_h * h)1368 static void q921_dump_pri_by_h(struct pri *ctrl, char direction_tag, q921_h *h)
1369 {
1370 	struct q921_link *link;
1371 
1372 	if (!ctrl) {
1373 		return;
1374 	}
1375 
1376 	if (BRI_NT_PTMP(ctrl)) {
1377 		link = pri_find_tei(ctrl, h->h.sapi, h->h.tei);
1378 	} else if (BRI_TE_PTMP(ctrl)) {
1379 		/* We're operating on the specific TEI link */
1380 		link = ctrl->link.next;
1381 	} else {
1382 		link = &ctrl->link;
1383 	}
1384 
1385 	if (link) {
1386 		q921_dump_pri(link, direction_tag);
1387 	} else {
1388 		pri_message(ctrl, "%c Link not found for this frame.\n", direction_tag);
1389 	}
1390 }
1391 
1392 #define Q921_TEI_CHECK_MAX_POLLS	2
1393 
t201_expire(void * vctrl)1394 static void t201_expire(void *vctrl)
1395 {
1396 	struct pri *ctrl;
1397 	struct q921_link *link;
1398 	struct q921_link *link_next;
1399 
1400 	ctrl = vctrl;
1401 
1402 	/* Start the TEI check timer. */
1403 	ctrl->t201_timer =
1404 		pri_schedule_event(ctrl, ctrl->timers[PRI_TIMER_T201], t201_expire, ctrl);
1405 
1406 	++ctrl->t201_expirycnt;
1407 	if (Q921_TEI_CHECK_MAX_POLLS < ctrl->t201_expirycnt) {
1408 		pri_schedule_del(ctrl, ctrl->t201_timer);
1409 		ctrl->t201_timer = 0;
1410 
1411 		/* Reclaim any dead TEI links. */
1412 		for (link = ctrl->link.next; link; link = link_next) {
1413 			link_next = link->next;
1414 
1415 			switch (link->tei_check) {
1416 			case Q921_TEI_CHECK_DEAD:
1417 				link->tei_check = Q921_TEI_CHECK_NONE;
1418 				q921_tei_remove(ctrl, link->tei);
1419 				q921_mdl_destroy(link);
1420 				break;
1421 			default:
1422 				link->tei_check = Q921_TEI_CHECK_NONE;
1423 				break;
1424 			}
1425 		}
1426 		return;
1427 	}
1428 
1429 	if (!ctrl->t201_timer) {
1430 		pri_error(ctrl, "Could not start T201 timer.");
1431 
1432 		/* Abort the remaining TEI check. */
1433 		for (link = ctrl->link.next; link; link = link->next) {
1434 			link->tei_check = Q921_TEI_CHECK_NONE;
1435 		}
1436 		return;
1437 	}
1438 
1439 	if (ctrl->t201_expirycnt == 1) {
1440 		/* First poll.  Setup TEI check state. */
1441 		for (link = ctrl->link.next; link; link = link->next) {
1442 			if (link->state < Q921_TEI_ASSIGNED) {
1443 				/* We do not have a TEI. */
1444 				link->tei_check = Q921_TEI_CHECK_NONE;
1445 			} else {
1446 				/* Mark TEI as dead until proved otherwise. */
1447 				link->tei_check = Q921_TEI_CHECK_DEAD;
1448 			}
1449 		}
1450 	} else {
1451 		/* Subsequent polls.  Setup for new TEI check poll. */
1452 		for (link = ctrl->link.next; link; link = link->next) {
1453 			switch (link->tei_check) {
1454 			case Q921_TEI_CHECK_REPLY:
1455 				link->tei_check = Q921_TEI_CHECK_DEAD_REPLY;
1456 				break;
1457 			default:
1458 				break;
1459 			}
1460 		}
1461 	}
1462 	q921_mdl_send(ctrl, Q921_TEI_IDENTITY_CHECK_REQUEST, 0, Q921_TEI_GROUP, 1);
1463 }
1464 
q921_tei_check(struct pri * ctrl)1465 static void q921_tei_check(struct pri *ctrl)
1466 {
1467 	if (ctrl->t201_timer) {
1468 		/* TEI check procedure already in progress.  Do not disturb it. */
1469 		return;
1470 	}
1471 	ctrl->t201_expirycnt = 0;
1472 	t201_expire(ctrl);
1473 }
1474 
q921_mdl_ignore(struct pri * ctrl,q921_u * h,const char * reason)1475 static void q921_mdl_ignore(struct pri *ctrl, q921_u *h, const char *reason)
1476 {
1477 	if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
1478 		/*
1479 		 * Send out this message in debug modes since it is possible the
1480 		 * user has misconfigured their link for the wrong mode.
1481 		 */
1482 		pri_message(ctrl, "Ignoring MDL message: %d(%s)  %s\n",
1483 			h->data[3], q921_tei_mgmt2str(h->data[3]), reason);
1484 	}
1485 }
1486 
q921_mdl_receive(struct pri * ctrl,q921_u * h,int len)1487 static pri_event *q921_mdl_receive(struct pri *ctrl, q921_u *h, int len)
1488 {
1489 	int ri;
1490 	struct q921_link *sub;
1491 	struct q921_link *link;
1492 	pri_event *res = NULL;
1493 	u_int8_t *action;
1494 	int count;
1495 	int tei;
1496 
1497 	if (len <= &h->data[0] - (u_int8_t *) h) {
1498 		pri_error(ctrl, "Received short MDL frame\n");
1499 		return NULL;
1500 	}
1501 	if (h->data[0] != 0x0f) {
1502 		pri_error(ctrl, "Received MDL with unsupported management entity %02x\n",
1503 			h->data[0]);
1504 		return NULL;
1505 	}
1506 	if (len <= &h->data[4] - (u_int8_t *) h) {
1507 		pri_error(ctrl, "Received short MDL message\n");
1508 		return NULL;
1509 	}
1510 	if (h->data[3] != Q921_TEI_IDENTITY_CHECK_RESPONSE
1511 		&& !(h->data[4] & 0x01)) {
1512 		pri_error(ctrl, "Received MDL message: %d(%s) with Ai E bit not set.\n",
1513 			h->data[3], q921_tei_mgmt2str(h->data[3]));
1514 		return NULL;
1515 	}
1516 	if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
1517 		pri_message(ctrl, "Received MDL message: %d(%s)\n",
1518 			h->data[3], q921_tei_mgmt2str(h->data[3]));
1519 	}
1520 	if (PTP_MODE(ctrl) && NT_MODE(ctrl)) {
1521 		/*
1522 		 * We are not managing automatic TEI's in this mode so we can
1523 		 * ignore MDL messages from the CPE.
1524 		 */
1525 		q921_mdl_ignore(ctrl, h, "We are in NT-PTP mode.");
1526 		return NULL;
1527 	}
1528 	ri = (h->data[1] << 8) | h->data[2];
1529 	tei = (h->data[4] >> 1);
1530 
1531 	switch (h->data[3]) {
1532 	case Q921_TEI_IDENTITY_REQUEST:
1533 		if (!BRI_NT_PTMP(ctrl)) {
1534 			q921_mdl_ignore(ctrl, h, "We are not in NT-PTMP mode.");
1535 			return NULL;
1536 		}
1537 
1538 		if (tei != Q921_TEI_GROUP) {
1539 			pri_error(ctrl, "Received %s with invalid TEI %d\n",
1540 				q921_tei_mgmt2str(Q921_TEI_IDENTITY_REQUEST), tei);
1541 			q921_mdl_send(ctrl, Q921_TEI_IDENTITY_DENIED, ri, tei, 1);
1542 			return NULL;
1543 		}
1544 
1545 		/* Find a TEI that is not allocated. */
1546 		tei = Q921_TEI_AUTO_FIRST;
1547 		do {
1548 			for (sub = &ctrl->link; sub->next; sub = sub->next) {
1549 				if (sub->next->tei == tei) {
1550 					/* This TEI is already assigned, try next one. */
1551 					++tei;
1552 					if (tei <= Q921_TEI_AUTO_LAST) {
1553 						break;
1554 					}
1555 					pri_error(ctrl, "TEI pool exhausted.  Reclaiming dead TEIs.\n");
1556 					q921_mdl_send(ctrl, Q921_TEI_IDENTITY_DENIED, ri, Q921_TEI_GROUP, 1);
1557 					q921_tei_check(ctrl);
1558 					return NULL;
1559 				}
1560 			}
1561 		} while (sub->next);
1562 
1563 		if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
1564 			pri_message(ctrl, "Allocating new TEI %d\n", tei);
1565 		}
1566 		link = pri_link_new(ctrl, Q921_SAPI_CALL_CTRL, tei);
1567 		if (!link) {
1568 			pri_error(ctrl, "Unable to allocate layer 2 link for new TEI %d\n", tei);
1569 			return NULL;
1570 		}
1571 		sub->next = link;
1572 		q921_setstate(link, Q921_TEI_ASSIGNED);
1573 		q921_mdl_send(ctrl, Q921_TEI_IDENTITY_ASSIGNED, ri, tei, 1);
1574 
1575 		count = 0;
1576 		for (sub = ctrl->link.next; sub; sub = sub->next) {
1577 			++count;
1578 		}
1579 		if (Q921_TEI_AUTO_LAST - Q921_TEI_AUTO_FIRST + 1 <= count) {
1580 			/*
1581 			 * We just allocated the last TEI.  Try to reclaim dead TEIs
1582 			 * before another is requested.
1583 			 */
1584 			if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
1585 				pri_message(ctrl, "Allocated last TEI.  Reclaiming dead TEIs.\n");
1586 			}
1587 			q921_tei_check(ctrl);
1588 		}
1589 
1590 		if (ctrl->l2_persistence == PRI_L2_PERSISTENCE_KEEP_UP) {
1591 			/*
1592 			 * Layer 2 is persistent so give the peer some time to setup
1593 			 * it's new TEI and bring the link up itself before we bring the
1594 			 * link up.
1595 			 */
1596 			restart_timer_start(link);
1597 		}
1598 		break;
1599 	case Q921_TEI_IDENTITY_CHECK_RESPONSE:
1600 		if (!BRI_NT_PTMP(ctrl)) {
1601 			/* Silently ignore the message since we never asked for the check. */
1602 			return NULL;
1603 		}
1604 
1605 		/* For each TEI listed in the message */
1606 		action = &h->data[4];
1607 		len -= (action - (u_int8_t *) h);
1608 		for (; len; --len, ++action) {
1609 			if (*action & 0x01) {
1610 				/* This is the last TEI in the list because the Ai E bit is set. */
1611 				len = 1;
1612 			}
1613 			tei = (*action >> 1);
1614 
1615 			if (tei == Q921_TEI_GROUP) {
1616 				pri_error(ctrl, "Received %s with invalid TEI %d\n",
1617 					q921_tei_mgmt2str(Q921_TEI_IDENTITY_CHECK_RESPONSE), tei);
1618 				continue;
1619 			}
1620 
1621 			for (sub = ctrl->link.next; sub; sub = sub->next) {
1622 				if (sub->tei == tei) {
1623 					/* Found the TEI. */
1624 					switch (sub->tei_check) {
1625 					case Q921_TEI_CHECK_NONE:
1626 						break;
1627 					case Q921_TEI_CHECK_DEAD:
1628 					case Q921_TEI_CHECK_DEAD_REPLY:
1629 						sub->tei_check = Q921_TEI_CHECK_REPLY;
1630 						break;
1631 					case Q921_TEI_CHECK_REPLY:
1632 						/* Duplicate TEI detected. */
1633 						sub->tei_check = Q921_TEI_CHECK_NONE;
1634 						q921_tei_remove(ctrl, tei);
1635 						q921_mdl_destroy(sub);
1636 						break;
1637 					}
1638 					break;
1639 				}
1640 			}
1641 			if (!sub) {
1642 				/* TEI not found. */
1643 				q921_tei_remove(ctrl, tei);
1644 			}
1645 		}
1646 		break;
1647 	case Q921_TEI_IDENTITY_VERIFY:
1648 		if (!BRI_NT_PTMP(ctrl)) {
1649 			q921_mdl_ignore(ctrl, h, "We are not in NT-PTMP mode.");
1650 			return NULL;
1651 		}
1652 		if (tei == Q921_TEI_GROUP) {
1653 			pri_error(ctrl, "Received %s with invalid TEI %d\n",
1654 				q921_tei_mgmt2str(Q921_TEI_IDENTITY_VERIFY), tei);
1655 			return NULL;
1656 		}
1657 		q921_tei_check(ctrl);
1658 		break;
1659 	case Q921_TEI_IDENTITY_ASSIGNED:
1660 		if (NT_MODE(ctrl)) {
1661 			/* We should not be receiving this message. */
1662 			q921_mdl_ignore(ctrl, h, "We are the network.");
1663 			return NULL;
1664 		}
1665 		if (!BRI_TE_PTMP(ctrl)) {
1666 			/*
1667 			 * Silently ignore the message.  It must not be for us
1668 			 * since we will never ask for one.
1669 			 */
1670 			return NULL;
1671 		}
1672 
1673 		/* We're operating on the specific TEI link here */
1674 		link = ctrl->link.next;
1675 
1676 		switch (link->state) {
1677 		case Q921_TEI_UNASSIGNED:
1678 			/*
1679 			 * We do not have a TEI and we are not currently asking for one.
1680 			 * Start asking for one.
1681 			 */
1682 			q921_setstate(link, Q921_ASSIGN_AWAITING_TEI);
1683 			q921_tei_request(link);
1684 			return NULL;
1685 		case Q921_ASSIGN_AWAITING_TEI:
1686 		case Q921_ESTABLISH_AWAITING_TEI:
1687 			/* We do not have a TEI and we want one. */
1688 			break;
1689 		default:
1690 			/* We already have a TEI. */
1691 			if (tei == link->tei) {
1692 				/*
1693 				 * The TEI assignment conflicts with ours.  Our TEI is the
1694 				 * duplicate so we should remove it.  Q.921 Section 5.3.4.2
1695 				 * condition c.
1696 				 */
1697 				pri_error(ctrl, "TEI=%d Conflicting TEI assignment.  Removing our TEI.\n",
1698 					tei);
1699 				q921_mdl_remove(link);
1700 				q921_start(link);
1701 			}
1702 			return NULL;
1703 		}
1704 
1705 		if (ri != link->ri) {
1706 			if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
1707 				pri_message(ctrl,
1708 					"TEI assignment received for another Ri %02x (ours is %02x)\n",
1709 					ri, link->ri);
1710 			}
1711 			return NULL;
1712 		}
1713 
1714 		pri_schedule_del(ctrl, link->t202_timer);
1715 		link->t202_timer = 0;
1716 
1717 		link->tei = tei;
1718 		if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
1719 			pri_message(ctrl, "Got assigned TEI %d\n", tei);
1720 		}
1721 
1722 		switch (link->state) {
1723 		case Q921_ASSIGN_AWAITING_TEI:
1724 			q921_setstate(link, Q921_TEI_ASSIGNED);
1725 			if (ctrl->l2_persistence != PRI_L2_PERSISTENCE_KEEP_UP) {
1726 				ctrl->ev.gen.e = PRI_EVENT_DCHAN_UP;
1727 				res = &ctrl->ev;
1728 				break;
1729 			}
1730 			/* Fall through: Layer 2 is persistent so bring it up. */
1731 		case Q921_ESTABLISH_AWAITING_TEI:
1732 			q921_establish_data_link(link);
1733 			link->l3_initiated = 1;
1734 			q921_setstate(link, Q921_AWAITING_ESTABLISHMENT);
1735 			ctrl->ev.gen.e = PRI_EVENT_DCHAN_UP;
1736 			res = &ctrl->ev;
1737 			break;
1738 		default:
1739 			break;
1740 		}
1741 		break;
1742 	case Q921_TEI_IDENTITY_CHECK_REQUEST:
1743 		if (NT_MODE(ctrl)) {
1744 			/* We should not be receiving this message. */
1745 			q921_mdl_ignore(ctrl, h, "We are the network.");
1746 			return NULL;
1747 		}
1748 
1749 		if (PTP_MODE(ctrl)) {
1750 			/*
1751 			 * Some telco switches/devices get very unhappy if we don't
1752 			 * respond to the TEI check request with our permanently
1753 			 * assigned TEI.
1754 			 */
1755 			link = &ctrl->link;
1756 		} else if (BRI_TE_PTMP(ctrl)) {
1757 			/* We're operating on the specific TEI link here */
1758 			link = ctrl->link.next;
1759 
1760 			if (link->state < Q921_TEI_ASSIGNED) {
1761 				/* We do not have a TEI. */
1762 				return NULL;
1763 			}
1764 		} else {
1765 			/* Should never get here. */
1766 			return NULL;
1767 		}
1768 
1769 		/* If it's addressed to the group TEI or to our TEI specifically, we respond */
1770 		if (tei == Q921_TEI_GROUP || tei == link->tei) {
1771 			q921_mdl_send(ctrl, Q921_TEI_IDENTITY_CHECK_RESPONSE, random() % 65535, link->tei, 1);
1772 		}
1773 		break;
1774 	case Q921_TEI_IDENTITY_REMOVE:
1775 		if (NT_MODE(ctrl)) {
1776 			/* We should not be receiving this message. */
1777 			q921_mdl_ignore(ctrl, h, "We are the network.");
1778 			return NULL;
1779 		}
1780 		if (!BRI_TE_PTMP(ctrl)) {
1781 			/*
1782 			 * Silently ignore the message.  If we are TE-PTP our
1783 			 * TEI is permanently assigned and cannot be removed.
1784 			 */
1785 			return NULL;
1786 		}
1787 
1788 		/* We're operating on the specific TEI link here */
1789 		link = ctrl->link.next;
1790 
1791 		if (link->state < Q921_TEI_ASSIGNED) {
1792 			/* We do not have a TEI. */
1793 			return NULL;
1794 		}
1795 
1796 		/* If it's addressed to the group TEI or to our TEI specifically, we respond */
1797 		if (tei == Q921_TEI_GROUP || tei == link->tei) {
1798 			q921_mdl_remove(link);
1799 			q921_start(link);
1800 		}
1801 		break;
1802 	}
1803 	return res;	/* Do we need to return something??? */
1804 }
1805 
is_command(struct pri * ctrl,q921_h * h)1806 static int is_command(struct pri *ctrl, q921_h *h)
1807 {
1808 	int command = 0;
1809 	int c_r = h->s.h.c_r;
1810 
1811 	if ((ctrl->localtype == PRI_NETWORK && c_r == 0) ||
1812 		(ctrl->localtype == PRI_CPE && c_r == 1))
1813 		command = 1;
1814 
1815 	return command;
1816 }
1817 
q921_clear_exception_conditions(struct q921_link * link)1818 static void q921_clear_exception_conditions(struct q921_link *link)
1819 {
1820 	link->own_rx_busy = 0;
1821 	link->peer_rx_busy = 0;
1822 	link->reject_exception = 0;
1823 	link->acknowledge_pending = 0;
1824 }
1825 
q921_sabme_rx(struct q921_link * link,q921_h * h)1826 static pri_event *q921_sabme_rx(struct q921_link *link, q921_h *h)
1827 {
1828 	pri_event *res = NULL;
1829 	struct pri *ctrl;
1830 	enum Q931_DL_EVENT delay_q931_dl_event;
1831 
1832 	ctrl = link->ctrl;
1833 
1834 	switch (link->state) {
1835 	case Q921_TIMER_RECOVERY:
1836 		/* Timer recovery state handling is same as multiframe established */
1837 	case Q921_MULTI_FRAME_ESTABLISHED:
1838 		/* Send Unnumbered Acknowledgement */
1839 		q921_send_ua(link, h->u.p_f);
1840 		q921_clear_exception_conditions(link);
1841 		q921_mdl_error(link, 'F');
1842 		if (link->v_s != link->v_a) {
1843 			q921_discard_iqueue(link);
1844 			/* DL-ESTABLISH indication */
1845 			delay_q931_dl_event = Q931_DL_EVENT_DL_ESTABLISH_IND;
1846 		} else {
1847 			delay_q931_dl_event = Q931_DL_EVENT_NONE;
1848 		}
1849 		stop_t200(link);
1850 		start_t203(link);
1851 		link->v_s = link->v_a = link->v_r = 0;
1852 		q921_setstate(link, Q921_MULTI_FRAME_ESTABLISHED);
1853 		if (delay_q931_dl_event != Q931_DL_EVENT_NONE) {
1854 			/* Delayed because Q.931 could send STATUS messages. */
1855 			q931_dl_event(link, delay_q931_dl_event);
1856 		}
1857 		break;
1858 	case Q921_TEI_ASSIGNED:
1859 		restart_timer_stop(link);
1860 		q921_send_ua(link, h->u.p_f);
1861 		q921_clear_exception_conditions(link);
1862 		link->v_s = link->v_a = link->v_r = 0;
1863 		/* DL-ESTABLISH indication */
1864 		delay_q931_dl_event = Q931_DL_EVENT_DL_ESTABLISH_IND;
1865 		if (PTP_MODE(ctrl)) {
1866 			ctrl->ev.gen.e = PRI_EVENT_DCHAN_UP;
1867 			res = &ctrl->ev;
1868 		}
1869 		start_t203(link);
1870 		q921_setstate(link, Q921_MULTI_FRAME_ESTABLISHED);
1871 		if (delay_q931_dl_event != Q931_DL_EVENT_NONE) {
1872 			/* Delayed because Q.931 could send STATUS messages. */
1873 			q931_dl_event(link, delay_q931_dl_event);
1874 		}
1875 		break;
1876 	case Q921_AWAITING_ESTABLISHMENT:
1877 		q921_send_ua(link, h->u.p_f);
1878 		break;
1879 	case Q921_AWAITING_RELEASE:
1880 		q921_send_dm(link, h->u.p_f);
1881 		break;
1882 	default:
1883 		pri_error(ctrl, "Cannot handle SABME in state %d(%s)\n",
1884 			link->state, q921_state2str(link->state));
1885 		break;
1886 	}
1887 
1888 	return res;
1889 }
1890 
q921_disc_rx(struct q921_link * link,q921_h * h)1891 static pri_event *q921_disc_rx(struct q921_link *link, q921_h *h)
1892 {
1893 	pri_event *res = NULL;
1894 	struct pri *ctrl;
1895 
1896 	ctrl = link->ctrl;
1897 
1898 	if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
1899 		pri_message(ctrl, "TEI=%d Got DISC\n", link->tei);
1900 	}
1901 
1902 	switch (link->state) {
1903 	case Q921_TEI_ASSIGNED:
1904 	case Q921_AWAITING_ESTABLISHMENT:
1905 		q921_send_dm(link, h->u.p_f);
1906 		break;
1907 	case Q921_AWAITING_RELEASE:
1908 		q921_send_ua(link, h->u.p_f);
1909 		break;
1910 	case Q921_MULTI_FRAME_ESTABLISHED:
1911 	case Q921_TIMER_RECOVERY:
1912 		res = q921_check_delay_restart(link);
1913 		q921_discard_iqueue(link);
1914 		q921_send_ua(link, h->u.p_f);
1915 		/* DL-RELEASE indication */
1916 		q931_dl_event(link, Q931_DL_EVENT_DL_RELEASE_IND);
1917 		stop_t200(link);
1918 		if (link->state == Q921_MULTI_FRAME_ESTABLISHED)
1919 			stop_t203(link);
1920 		q921_setstate(link, Q921_TEI_ASSIGNED);
1921 		break;
1922 	default:
1923 		pri_error(ctrl, "Don't know what to do with DISC in state %d(%s)\n",
1924 			link->state, q921_state2str(link->state));
1925 		break;
1926 	}
1927 
1928 	return res;
1929 }
1930 
q921_mdl_remove(struct q921_link * link)1931 static void q921_mdl_remove(struct q921_link *link)
1932 {
1933 	int mdl_free_me;
1934 	struct pri *ctrl;
1935 
1936 	ctrl = link->ctrl;
1937 
1938 	if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
1939 		pri_message(ctrl, "MDL-REMOVE: Removing TEI %d\n", link->tei);
1940 	}
1941 	if (BRI_NT_PTMP(ctrl)) {
1942 		if (link == &ctrl->link) {
1943 			pri_error(ctrl, "Bad bad bad!  Cannot MDL-REMOVE master\n");
1944 			return;
1945 		}
1946 		mdl_free_me = 1;
1947 	} else {
1948 		mdl_free_me = 0;
1949 	}
1950 
1951 	switch (link->state) {
1952 	case Q921_TEI_ASSIGNED:
1953 		if (mdl_free_me) {
1954 			restart_timer_stop(link);
1955 		}
1956 		/* XXX: deviation! Since we don't have a UI queue, we just discard our I-queue */
1957 		q921_discard_iqueue(link);
1958 		q921_setstate(link, Q921_TEI_UNASSIGNED);
1959 		break;
1960 	case Q921_AWAITING_ESTABLISHMENT:
1961 		q921_discard_iqueue(link);
1962 		/* DL-RELEASE indication */
1963 		q931_dl_event(link, Q931_DL_EVENT_DL_RELEASE_IND);
1964 		stop_t200(link);
1965 		q921_setstate(link, Q921_TEI_UNASSIGNED);
1966 		break;
1967 	case Q921_AWAITING_RELEASE:
1968 		q921_discard_iqueue(link);
1969 		/* DL-RELEASE confirm */
1970 		q931_dl_event(link, Q931_DL_EVENT_DL_RELEASE_CONFIRM);
1971 		stop_t200(link);
1972 		q921_setstate(link, Q921_TEI_UNASSIGNED);
1973 		break;
1974 	case Q921_MULTI_FRAME_ESTABLISHED:
1975 		q921_discard_iqueue(link);
1976 		/* DL-RELEASE indication */
1977 		q931_dl_event(link, Q931_DL_EVENT_DL_RELEASE_IND);
1978 		stop_t200(link);
1979 		stop_t203(link);
1980 		q921_setstate(link, Q921_TEI_UNASSIGNED);
1981 		break;
1982 	case Q921_TIMER_RECOVERY:
1983 		q921_discard_iqueue(link);
1984 		/* DL-RELEASE indication */
1985 		q931_dl_event(link, Q931_DL_EVENT_DL_RELEASE_IND);
1986 		stop_t200(link);
1987 		q921_setstate(link, Q921_TEI_UNASSIGNED);
1988 		break;
1989 	default:
1990 		pri_error(ctrl, "MDL-REMOVE when in state %d(%s)\n",
1991 			link->state, q921_state2str(link->state));
1992 		return;
1993 	}
1994 
1995 	q931_dl_event(link, Q931_DL_EVENT_TEI_REMOVAL);
1996 
1997 	/*
1998 	 * Negate the TEI value so debug messages will display a
1999 	 * negated TEI when it is actually unassigned.
2000 	 */
2001 	link->tei = -link->tei;
2002 
2003 	link->mdl_free_me = mdl_free_me;
2004 }
2005 
q921_mdl_link_destroy(struct q921_link * link)2006 static void q921_mdl_link_destroy(struct q921_link *link)
2007 {
2008 	struct pri *ctrl;
2009 	struct q921_link *freep;
2010 	struct q921_link *prev;
2011 
2012 	ctrl = link->ctrl;
2013 
2014 	freep = NULL;
2015 	for (prev = &ctrl->link; prev->next; prev = prev->next) {
2016 		if (prev->next == link) {
2017 			prev->next = link->next;
2018 			freep = link;
2019 			break;
2020 		}
2021 	}
2022 	if (freep == NULL) {
2023 		pri_error(ctrl, "Huh!? no match found in list for TEI %d\n", -link->tei);
2024 		return;
2025 	}
2026 
2027 	if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
2028 		pri_message(ctrl, "Freeing TEI of %d\n", -freep->tei);
2029 	}
2030 
2031 	pri_link_destroy(freep);
2032 }
2033 
q921_mdl_destroy(struct q921_link * link)2034 static void q921_mdl_destroy(struct q921_link *link)
2035 {
2036 	q921_mdl_remove(link);
2037 	if (link->mdl_free_me) {
2038 		q921_mdl_link_destroy(link);
2039 	}
2040 }
2041 
q921_mdl_handle_network_error(struct q921_link * link,char error)2042 static void q921_mdl_handle_network_error(struct q921_link *link, char error)
2043 {
2044 	struct pri *ctrl;
2045 
2046 	switch (error) {
2047 	case 'C':
2048 	case 'D':
2049 	case 'G':
2050 	case 'H':
2051 		q921_mdl_remove(link);
2052 		break;
2053 	case 'A':
2054 	case 'B':
2055 	case 'E':
2056 	case 'F':
2057 	case 'I':
2058 	case 'J':
2059 	case 'K':
2060 		break;
2061 	default:
2062 		ctrl = link->ctrl;
2063 		pri_error(ctrl, "Network MDL can't handle error of type %c\n", error);
2064 		break;
2065 	}
2066 }
2067 
q921_mdl_handle_cpe_error(struct q921_link * link,char error)2068 static void q921_mdl_handle_cpe_error(struct q921_link *link, char error)
2069 {
2070 	struct pri *ctrl;
2071 
2072 	switch (error) {
2073 	case 'C':
2074 	case 'D':
2075 	case 'G':
2076 	case 'H':
2077 		q921_mdl_remove(link);
2078 		break;
2079 	case 'A':
2080 	case 'B':
2081 	case 'E':
2082 	case 'F':
2083 	case 'I':
2084 	case 'J':
2085 	case 'K':
2086 		break;
2087 	default:
2088 		ctrl = link->ctrl;
2089 		pri_error(ctrl, "CPE MDL can't handle error of type %c\n", error);
2090 		break;
2091 	}
2092 }
2093 
q921_mdl_handle_ptp_error(struct q921_link * link,char error)2094 static void q921_mdl_handle_ptp_error(struct q921_link *link, char error)
2095 {
2096 	struct pri *ctrl;
2097 
2098 	ctrl = link->ctrl;
2099 
2100 	switch (error) {
2101 	case 'J':
2102 		/*
2103 		 * This is for the transition to Q921_AWAITING_ESTABLISHMENT.
2104 		 * The event is genereated here rather than where the MDL_ERROR
2105 		 * 'J' is posted because of the potential event conflict with
2106 		 * incoming I-frame information passed to Q.931.
2107 		 */
2108 		ctrl->schedev = 1;
2109 		ctrl->ev.gen.e = PRI_EVENT_DCHAN_DOWN;
2110 		break;
2111 	case 'A':
2112 	case 'B':
2113 	case 'C':
2114 	case 'D':
2115 	case 'E':
2116 	case 'F':
2117 	case 'G':
2118 	case 'H':
2119 	case 'I':
2120 	case 'K':
2121 		break;
2122 	default:
2123 		pri_error(ctrl, "PTP MDL can't handle error of type %c\n", error);
2124 		break;
2125 	}
2126 }
2127 
q921_mdl_handle_error(struct q921_link * link,char error)2128 static void q921_mdl_handle_error(struct q921_link *link, char error)
2129 {
2130 	struct pri *ctrl;
2131 
2132 	ctrl = link->ctrl;
2133 
2134 	if (PTP_MODE(ctrl)) {
2135 		q921_mdl_handle_ptp_error(link, error);
2136 	} else {
2137 		if (ctrl->localtype == PRI_NETWORK) {
2138 			q921_mdl_handle_network_error(link, error);
2139 		} else {
2140 			q921_mdl_handle_cpe_error(link, error);
2141 		}
2142 	}
2143 }
2144 
q921_mdl_handle_error_callback(void * vlink)2145 static void q921_mdl_handle_error_callback(void *vlink)
2146 {
2147 	struct q921_link *link = vlink;
2148 
2149 	q921_mdl_handle_error(link, link->mdl_error);
2150 
2151 	link->mdl_error = 0;
2152 	link->mdl_timer = 0;
2153 
2154 	if (link->mdl_free_me) {
2155 		q921_mdl_link_destroy(link);
2156 	}
2157 }
2158 
q921_mdl_error(struct q921_link * link,char error)2159 static void q921_mdl_error(struct q921_link *link, char error)
2160 {
2161 	int is_debug_q921_state;
2162 	struct pri *ctrl;
2163 
2164 	ctrl = link->ctrl;
2165 
2166 	/* Log the MDL-ERROR event when detected. */
2167 	is_debug_q921_state = (ctrl->debug & PRI_DEBUG_Q921_STATE);
2168 	switch (error) {
2169 	case 'A':
2170 		pri_message(ctrl,
2171 			"TEI=%d MDL-ERROR (A): Got supervisory frame with F=1 in state %d(%s)\n",
2172 			link->tei, link->state, q921_state2str(link->state));
2173 		break;
2174 	case 'B':
2175 	case 'E':
2176 		pri_message(ctrl, "TEI=%d MDL-ERROR (%c): DM (F=%c) in state %d(%s)\n",
2177 			link->tei, error, (error == 'B') ? '1' : '0',
2178 			link->state, q921_state2str(link->state));
2179 		break;
2180 	case 'C':
2181 	case 'D':
2182 		if (is_debug_q921_state || PTP_MODE(ctrl)) {
2183 			pri_message(ctrl, "TEI=%d MDL-ERROR (%c): UA (F=%c) in state %d(%s)\n",
2184 				link->tei, error, (error == 'C') ? '1' : '0',
2185 				link->state, q921_state2str(link->state));
2186 		}
2187 		break;
2188 	case 'F':
2189 		/*
2190 		 * The peer is restarting the link.
2191 		 * Some reasons this might happen:
2192 		 * 1) Our link establishment requests collided.
2193 		 * 2) They got reset.
2194 		 * 3) They could not talk to us for some reason because
2195 		 * their T200 timer expired N200 times.
2196 		 * 4) They got an MDL-ERROR (J).
2197 		 */
2198 		if (is_debug_q921_state) {
2199 			/*
2200 			 * This message is rather annoying and is normal for
2201 			 * reasons 1-3 above.
2202 			 */
2203 			pri_message(ctrl, "TEI=%d MDL-ERROR (F): SABME in state %d(%s)\n",
2204 				link->tei, link->state, q921_state2str(link->state));
2205 		}
2206 		break;
2207 	case 'G':
2208 		/* We could not get a response from the peer. */
2209 		if (is_debug_q921_state) {
2210 			pri_message(ctrl,
2211 				"TEI=%d MDL-ERROR (G): T200 expired N200 times sending SABME in state %d(%s)\n",
2212 				link->tei, link->state, q921_state2str(link->state));
2213 		}
2214 		break;
2215 	case 'H':
2216 		/* We could not get a response from the peer. */
2217 		if (is_debug_q921_state) {
2218 			pri_message(ctrl,
2219 				"TEI=%d MDL-ERROR (H): T200 expired N200 times sending DISC in state %d(%s)\n",
2220 				link->tei, link->state, q921_state2str(link->state));
2221 		}
2222 		break;
2223 	case 'I':
2224 		/* We could not get a response from the peer. */
2225 		if (is_debug_q921_state) {
2226 			pri_message(ctrl,
2227 				"TEI=%d MDL-ERROR (I): T200 expired N200 times sending RR/RNR in state %d(%s)\n",
2228 				link->tei, link->state, q921_state2str(link->state));
2229 		}
2230 		break;
2231 	case 'J':
2232 		/* N(R) not within ack window. */
2233 		pri_error(ctrl, "TEI=%d MDL-ERROR (J): N(R) error in state %d(%s)\n",
2234 			link->tei, link->state, q921_state2str(link->state));
2235 		break;
2236 	case 'K':
2237 		/*
2238 		 * Received a frame reject frame.
2239 		 * The other end does not like what we are doing at all for some reason.
2240 		 */
2241 		pri_error(ctrl, "TEI=%d MDL-ERROR (K): FRMR in state %d(%s)\n",
2242 			link->tei, link->state, q921_state2str(link->state));
2243 		break;
2244 	default:
2245 		pri_message(ctrl, "TEI=%d MDL-ERROR (%c): in state %d(%s)\n",
2246 			link->tei, error, link->state, q921_state2str(link->state));
2247 		break;
2248 	}
2249 
2250 	if (link->mdl_error) {
2251 		/* This should not happen. */
2252 		pri_error(ctrl,
2253 			"Trying to queue MDL-ERROR (%c) when MDL-ERROR (%c) is already scheduled\n",
2254 			error, link->mdl_error);
2255 		return;
2256 	}
2257 	link->mdl_error = error;
2258 	link->mdl_timer = pri_schedule_event(ctrl, 0, q921_mdl_handle_error_callback, link);
2259 	if (!link->mdl_timer) {
2260 		/* Timer allocation failed */
2261 		link->mdl_error = 0;
2262 	}
2263 }
2264 
q921_ua_rx(struct q921_link * link,q921_h * h)2265 static pri_event *q921_ua_rx(struct q921_link *link, q921_h *h)
2266 {
2267 	struct pri *ctrl;
2268 	pri_event *res = NULL;
2269 	enum Q931_DL_EVENT delay_q931_dl_event;
2270 
2271 	ctrl = link->ctrl;
2272 
2273 	if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
2274 		pri_message(ctrl, "TEI=%d Got UA\n", link->tei);
2275 	}
2276 
2277 	switch (link->state) {
2278 	case Q921_TEI_ASSIGNED:
2279 	case Q921_MULTI_FRAME_ESTABLISHED:
2280 	case Q921_TIMER_RECOVERY:
2281 		if (h->u.p_f) {
2282 			q921_mdl_error(link, 'C');
2283 		} else {
2284 			q921_mdl_error(link, 'D');
2285 		}
2286 		break;
2287 	case Q921_AWAITING_ESTABLISHMENT:
2288 		if (!h->u.p_f) {
2289 			q921_mdl_error(link, 'D');
2290 			break;
2291 		}
2292 
2293 		delay_q931_dl_event = Q931_DL_EVENT_NONE;
2294 		if (!link->l3_initiated) {
2295 			if (link->v_s != link->v_a) {
2296 				q921_discard_iqueue(link);
2297 				/* DL-ESTABLISH indication */
2298 				delay_q931_dl_event = Q931_DL_EVENT_DL_ESTABLISH_IND;
2299 			}
2300 		} else {
2301 			link->l3_initiated = 0;
2302 			/* DL-ESTABLISH confirm */
2303 			delay_q931_dl_event = Q931_DL_EVENT_DL_ESTABLISH_CONFIRM;
2304 		}
2305 
2306 		if (PTP_MODE(ctrl)) {
2307 			ctrl->ev.gen.e = PRI_EVENT_DCHAN_UP;
2308 			res = &ctrl->ev;
2309 		}
2310 
2311 		stop_t200(link);
2312 		start_t203(link);
2313 
2314 		link->v_r = link->v_s = link->v_a = 0;
2315 
2316 		q921_setstate(link, Q921_MULTI_FRAME_ESTABLISHED);
2317 		if (delay_q931_dl_event != Q931_DL_EVENT_NONE) {
2318 			/* Delayed because Q.931 could send STATUS messages. */
2319 			q931_dl_event(link, delay_q931_dl_event);
2320 		}
2321 		break;
2322 	case Q921_AWAITING_RELEASE:
2323 		if (!h->u.p_f) {
2324 			q921_mdl_error(link, 'D');
2325 		} else {
2326 			res = q921_check_delay_restart(link);
2327 			/* DL-RELEASE confirm */
2328 			q931_dl_event(link, Q931_DL_EVENT_DL_RELEASE_CONFIRM);
2329 			stop_t200(link);
2330 			q921_setstate(link, Q921_TEI_ASSIGNED);
2331 		}
2332 		break;
2333 	default:
2334 		pri_error(ctrl, "Don't know what to do with UA in state %d(%s)\n",
2335 			link->state, q921_state2str(link->state));
2336 		break;
2337 	}
2338 
2339 	return res;
2340 }
2341 
q921_enquiry_response(struct q921_link * link)2342 static void q921_enquiry_response(struct q921_link *link)
2343 {
2344 	struct pri *ctrl;
2345 
2346 	if (link->own_rx_busy) {
2347 		/* XXX : TODO later sometime */
2348 		ctrl = link->ctrl;
2349 		pri_error(ctrl, "Implement me %s: own_rx_busy\n", __FUNCTION__);
2350 		//q921_rnr(link);
2351 	} else {
2352 		q921_rr(link, 1, 0);
2353 	}
2354 
2355 	link->acknowledge_pending = 0;
2356 }
2357 
n_r_error_recovery(struct q921_link * link)2358 static void n_r_error_recovery(struct q921_link *link)
2359 {
2360 	q921_mdl_error(link, 'J');
2361 
2362 	q921_establish_data_link(link);
2363 
2364 	link->l3_initiated = 0;
2365 }
2366 
update_v_a(struct q921_link * link,int n_r)2367 static void update_v_a(struct q921_link *link, int n_r)
2368 {
2369 	int idealcnt = 0, realcnt = 0;
2370 	int x;
2371 	struct pri *ctrl;
2372 
2373 	ctrl = link->ctrl;
2374 
2375 	/* Cancel each packet as necessary */
2376 	if (ctrl->debug & PRI_DEBUG_Q921_DUMP)
2377 		pri_message(ctrl, "-- Got ACK for N(S)=%d to (but not including) N(S)=%d\n", link->v_a, n_r);
2378 	for (x = link->v_a; x != n_r; Q921_INC(x)) {
2379 		idealcnt++;
2380 		realcnt += q921_ack_packet(link, x);
2381 	}
2382 	if (idealcnt != realcnt) {
2383 		pri_error(ctrl, "Ideally should have ack'd %d frames, but actually ack'd %d.  This is not good.\n", idealcnt, realcnt);
2384 		q921_dump_iqueue_info(link);
2385 	}
2386 
2387 	link->v_a = n_r;
2388 }
2389 
2390 /*! \brief Is V(A) <= N(R) <= V(S) ? */
n_r_is_valid(struct q921_link * link,int n_r)2391 static int n_r_is_valid(struct q921_link *link, int n_r)
2392 {
2393 	int x;
2394 
2395 	for (x = link->v_a; x != n_r && x != link->v_s; Q921_INC(x)) {
2396 	}
2397 	if (x != n_r) {
2398 		return 0;
2399 	} else {
2400 		return 1;
2401 	}
2402 }
2403 
2404 static int q921_invoke_retransmission(struct q921_link *link, int n_r);
2405 
timer_recovery_rr_rej_rx(struct q921_link * link,q921_h * h)2406 static pri_event *timer_recovery_rr_rej_rx(struct q921_link *link, q921_h *h)
2407 {
2408 	struct pri *ctrl;
2409 
2410 	ctrl = link->ctrl;
2411 
2412 	/* Figure B.7/Q.921 Page 74 */
2413 	link->peer_rx_busy = 0;
2414 
2415 	if (is_command(ctrl, h)) {
2416 		if (h->s.p_f) {
2417 			/* Enquiry response */
2418 			q921_enquiry_response(link);
2419 		}
2420 		if (n_r_is_valid(link, h->s.n_r)) {
2421 			update_v_a(link, h->s.n_r);
2422 		} else {
2423 			goto n_r_error_out;
2424 		}
2425 	} else {
2426 		if (!h->s.p_f) {
2427 			if (n_r_is_valid(link, h->s.n_r)) {
2428 				update_v_a(link, h->s.n_r);
2429 			} else {
2430 				goto n_r_error_out;
2431 			}
2432 		} else {
2433 			if (n_r_is_valid(link, h->s.n_r)) {
2434 				update_v_a(link, h->s.n_r);
2435 				stop_t200(link);
2436 				start_t203(link);
2437 				q921_invoke_retransmission(link, h->s.n_r);
2438 				q921_setstate(link, Q921_MULTI_FRAME_ESTABLISHED);
2439 			} else {
2440 				goto n_r_error_out;
2441 			}
2442 		}
2443 	}
2444 	return NULL;
2445 n_r_error_out:
2446 	n_r_error_recovery(link);
2447 	q921_setstate(link, Q921_AWAITING_ESTABLISHMENT);
2448 	return NULL;
2449 }
2450 
q921_rr_rx(struct q921_link * link,q921_h * h)2451 static pri_event *q921_rr_rx(struct q921_link *link, q921_h *h)
2452 {
2453 	pri_event *res = NULL;
2454 	struct pri *ctrl;
2455 
2456 	ctrl = link->ctrl;
2457 
2458 #if 0	/* Don't flood debug trace with RR if not really looking at Q.921 layer. */
2459 	if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
2460 		pri_message(ctrl, "TEI=%d Got RR N(R)=%d\n", link->tei, h->s.n_r);
2461 	}
2462 #endif
2463 
2464 	switch (link->state) {
2465 	case Q921_TIMER_RECOVERY:
2466 		res = timer_recovery_rr_rej_rx(link, h);
2467 		break;
2468 	case Q921_MULTI_FRAME_ESTABLISHED:
2469 		/* Figure B.7/Q.921 Page 74 */
2470 		link->peer_rx_busy = 0;
2471 
2472 		if (is_command(ctrl, h)) {
2473 			if (h->s.p_f) {
2474 				/* Enquiry response */
2475 				q921_enquiry_response(link);
2476 			}
2477 		} else {
2478 			if (h->s.p_f) {
2479 				q921_mdl_error(link, 'A');
2480 			}
2481 		}
2482 
2483 		if (!n_r_is_valid(link, h->s.n_r)) {
2484 			n_r_error_recovery(link);
2485 			q921_setstate(link, Q921_AWAITING_ESTABLISHMENT);
2486 		} else {
2487 			if (h->s.n_r == link->v_s) {
2488 				update_v_a(link, h->s.n_r);
2489 				stop_t200(link);
2490 				start_t203(link);
2491 			} else {
2492 				if (h->s.n_r != link->v_a) {
2493 					/* Need to check the validity of n_r as well.. */
2494 					update_v_a(link, h->s.n_r);
2495 					restart_t200(link);
2496 				}
2497 			}
2498 		}
2499 		break;
2500 	case Q921_TEI_ASSIGNED:
2501 	case Q921_AWAITING_ESTABLISHMENT:
2502 	case Q921_AWAITING_RELEASE:
2503 		/*
2504 		 * Ignore this frame.
2505 		 * We likely got reset and the other end has not realized it yet.
2506 		 */
2507 		break;
2508 	default:
2509 		pri_error(ctrl, "Don't know what to do with RR in state %d(%s)\n",
2510 			link->state, q921_state2str(link->state));
2511 		break;
2512 	}
2513 
2514 	return res;
2515 }
2516 
q921_invoke_retransmission(struct q921_link * link,int n_r)2517 static int q921_invoke_retransmission(struct q921_link *link, int n_r)
2518 {
2519 	struct q921_frame *f;
2520 	struct pri *ctrl;
2521 
2522 	ctrl = link->ctrl;
2523 
2524 	/*
2525 	 * All acked frames should already have been removed from the queue.
2526 	 * Push back all sent frames.
2527 	 */
2528 	for (f = link->tx_queue; f && f->status == Q921_TX_FRAME_SENT; f = f->next) {
2529 		f->status = Q921_TX_FRAME_PUSHED_BACK;
2530 
2531 		/* Sanity check: Is V(A) <= N(S) <= V(S)? */
2532 		if (!n_r_is_valid(link, f->h.n_s)) {
2533 			pri_error(ctrl,
2534 				"Tx Q frame with invalid N(S)=%d.  Must be (V(A)=%d) <= N(S) <= (V(S)=%d)\n",
2535 				f->h.n_s, link->v_a, link->v_s);
2536 		}
2537 	}
2538 	link->v_s = n_r;
2539 	return q921_send_queued_iframes(link);
2540 }
2541 
q921_rej_rx(struct q921_link * link,q921_h * h)2542 static pri_event *q921_rej_rx(struct q921_link *link, q921_h *h)
2543 {
2544 	pri_event *res = NULL;
2545 	struct pri *ctrl;
2546 
2547 	ctrl = link->ctrl;
2548 
2549 	if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
2550 		pri_message(ctrl, "TEI=%d Got REJ N(R)=%d\n", link->tei, h->s.n_r);
2551 	}
2552 
2553 	switch (link->state) {
2554 	case Q921_TIMER_RECOVERY:
2555 		res = timer_recovery_rr_rej_rx(link, h);
2556 		break;
2557 	case Q921_MULTI_FRAME_ESTABLISHED:
2558 		/* Figure B.7/Q.921 Page 74 */
2559 		link->peer_rx_busy = 0;
2560 
2561 		if (is_command(ctrl, h)) {
2562 			if (h->s.p_f) {
2563 				/* Enquiry response */
2564 				q921_enquiry_response(link);
2565 			}
2566 		} else {
2567 			if (h->s.p_f) {
2568 				q921_mdl_error(link, 'A');
2569 			}
2570 		}
2571 
2572 		if (!n_r_is_valid(link, h->s.n_r)) {
2573 			n_r_error_recovery(link);
2574 			q921_setstate(link, Q921_AWAITING_ESTABLISHMENT);
2575 		} else {
2576 			update_v_a(link, h->s.n_r);
2577 			stop_t200(link);
2578 			start_t203(link);
2579 			q921_invoke_retransmission(link, h->s.n_r);
2580 		}
2581 		break;
2582 	case Q921_TEI_ASSIGNED:
2583 	case Q921_AWAITING_ESTABLISHMENT:
2584 	case Q921_AWAITING_RELEASE:
2585 		/*
2586 		 * Ignore this frame.
2587 		 * We likely got reset and the other end has not realized it yet.
2588 		 */
2589 		break;
2590 	default:
2591 		pri_error(ctrl, "Don't know what to do with REJ in state %d(%s)\n",
2592 			link->state, q921_state2str(link->state));
2593 		break;
2594 	}
2595 
2596 	return res;
2597 }
2598 
q921_frmr_rx(struct q921_link * link,q921_h * h)2599 static pri_event *q921_frmr_rx(struct q921_link *link, q921_h *h)
2600 {
2601 	pri_event *res = NULL;
2602 	struct pri *ctrl;
2603 
2604 	ctrl = link->ctrl;
2605 
2606 	if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
2607 		pri_message(ctrl, "TEI=%d Got FRMR\n", link->tei);
2608 	}
2609 
2610 	switch (link->state) {
2611 	case Q921_TIMER_RECOVERY:
2612 	case Q921_MULTI_FRAME_ESTABLISHED:
2613 		q921_mdl_error(link, 'K');
2614 		q921_establish_data_link(link);
2615 		link->l3_initiated = 0;
2616 		q921_setstate(link, Q921_AWAITING_ESTABLISHMENT);
2617 		if (PTP_MODE(ctrl)) {
2618 			ctrl->ev.gen.e = PRI_EVENT_DCHAN_DOWN;
2619 			res = &ctrl->ev;
2620 		}
2621 		break;
2622 	case Q921_TEI_ASSIGNED:
2623 	case Q921_AWAITING_ESTABLISHMENT:
2624 	case Q921_AWAITING_RELEASE:
2625 		/*
2626 		 * Ignore this frame.
2627 		 * We likely got reset and the other end has not realized it yet.
2628 		 */
2629 		if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
2630 			pri_message(ctrl, "TEI=%d Ignoring FRMR.\n", link->tei);
2631 		}
2632 		break;
2633 	default:
2634 		pri_error(ctrl, "Don't know what to do with FRMR in state %d(%s)\n",
2635 			link->state, q921_state2str(link->state));
2636 		break;
2637 	}
2638 
2639 	return res;
2640 }
2641 
q921_iframe_rx(struct q921_link * link,q921_h * h,int len)2642 static pri_event *q921_iframe_rx(struct q921_link *link, q921_h *h, int len)
2643 {
2644 	struct pri *ctrl;
2645 	pri_event *eres = NULL;
2646 	int res = 0;
2647 	int delay_q931_receive;
2648 
2649 	ctrl = link->ctrl;
2650 
2651 	switch (link->state) {
2652 	case Q921_TIMER_RECOVERY:
2653 	case Q921_MULTI_FRAME_ESTABLISHED:
2654 		delay_q931_receive = 0;
2655 		/* FIXME: Verify that it's a command ... */
2656 		if (link->own_rx_busy) {
2657 			/* DEVIATION: Handle own rx busy */
2658 		} else if (h->i.n_s == link->v_r) {
2659 			Q921_INC(link->v_r);
2660 
2661 			link->reject_exception = 0;
2662 
2663 			/*
2664 			 * Dump Q.931 message where Q.921 says to queue it to Q.931 so if
2665 			 * Q.921 is dumping its frames they will be in the correct order.
2666 			 */
2667 			if (ctrl->debug & PRI_DEBUG_Q931_DUMP) {
2668 				q931_dump(ctrl, h->h.tei, (q931_h *) h->i.data, len - 4, 0);
2669 			}
2670 			delay_q931_receive = 1;
2671 
2672 			if (h->i.p_f) {
2673 				q921_rr(link, 1, 0);
2674 				link->acknowledge_pending = 0;
2675 			} else {
2676 				link->acknowledge_pending = 1;
2677 			}
2678 		} else {
2679 			if (link->reject_exception) {
2680 				if (h->i.p_f) {
2681 					q921_rr(link, 1, 0);
2682 					link->acknowledge_pending = 0;
2683 				}
2684 			} else {
2685 				link->reject_exception = 1;
2686 				q921_reject(link, h->i.p_f);
2687 				link->acknowledge_pending = 0;
2688 			}
2689 		}
2690 
2691 		if (!n_r_is_valid(link, h->i.n_r)) {
2692 			n_r_error_recovery(link);
2693 			q921_setstate(link, Q921_AWAITING_ESTABLISHMENT);
2694 		} else {
2695 			if (link->state == Q921_TIMER_RECOVERY) {
2696 				update_v_a(link, h->i.n_r);
2697 			} else {
2698 				if (link->peer_rx_busy) {
2699 					update_v_a(link, h->i.n_r);
2700 				} else {
2701 					if (h->i.n_r == link->v_s) {
2702 						update_v_a(link, h->i.n_r);
2703 						stop_t200(link);
2704 						start_t203(link);
2705 					} else {
2706 						if (h->i.n_r != link->v_a) {
2707 							update_v_a(link, h->i.n_r);
2708 							reschedule_t200(link);
2709 						}
2710 					}
2711 				}
2712 			}
2713 		}
2714 		if (delay_q931_receive) {
2715 			/* Q.921 has finished processing the frame so we can give it to Q.931 now. */
2716 			res = q931_receive(link, (q931_h *) h->i.data, len - 4);
2717 			if (res != -1 && (res & Q931_RES_HAVEEVENT)) {
2718 				eres = &ctrl->ev;
2719 			}
2720 		}
2721 		break;
2722 	case Q921_TEI_ASSIGNED:
2723 	case Q921_AWAITING_ESTABLISHMENT:
2724 	case Q921_AWAITING_RELEASE:
2725 		/*
2726 		 * Ignore this frame.
2727 		 * We likely got reset and the other end has not realized it yet.
2728 		 */
2729 		break;
2730 	default:
2731 		pri_error(ctrl, "Don't know what to do with an I-frame in state %d(%s)\n",
2732 			link->state, q921_state2str(link->state));
2733 		break;
2734 	}
2735 
2736 	return eres;
2737 }
2738 
q921_dm_rx(struct q921_link * link,q921_h * h)2739 static pri_event *q921_dm_rx(struct q921_link *link, q921_h *h)
2740 {
2741 	pri_event *res = NULL;
2742 	struct pri *ctrl;
2743 
2744 	ctrl = link->ctrl;
2745 
2746 	if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
2747 		pri_message(ctrl, "TEI=%d Got DM\n", link->tei);
2748 	}
2749 
2750 	switch (link->state) {
2751 	case Q921_TEI_ASSIGNED:
2752 		if (h->u.p_f)
2753 			break;
2754 		/* else */
2755 		restart_timer_stop(link);
2756 		q921_establish_data_link(link);
2757 		link->l3_initiated = 1;
2758 		q921_setstate(link, Q921_AWAITING_ESTABLISHMENT);
2759 		break;
2760 	case Q921_AWAITING_ESTABLISHMENT:
2761 		if (!h->u.p_f)
2762 			break;
2763 
2764 		res = q921_check_delay_restart(link);
2765 		q921_discard_iqueue(link);
2766 		/* DL-RELEASE indication */
2767 		q931_dl_event(link, Q931_DL_EVENT_DL_RELEASE_IND);
2768 		stop_t200(link);
2769 		q921_setstate(link, Q921_TEI_ASSIGNED);
2770 		break;
2771 	case Q921_AWAITING_RELEASE:
2772 		if (!h->u.p_f)
2773 			break;
2774 		res = q921_check_delay_restart(link);
2775 		/* DL-RELEASE confirm */
2776 		q931_dl_event(link, Q931_DL_EVENT_DL_RELEASE_CONFIRM);
2777 		stop_t200(link);
2778 		q921_setstate(link, Q921_TEI_ASSIGNED);
2779 		break;
2780 	case Q921_MULTI_FRAME_ESTABLISHED:
2781 		if (h->u.p_f) {
2782 			q921_mdl_error(link, 'B');
2783 			break;
2784 		}
2785 
2786 		q921_mdl_error(link, 'E');
2787 		q921_establish_data_link(link);
2788 		link->l3_initiated = 0;
2789 		q921_setstate(link, Q921_AWAITING_ESTABLISHMENT);
2790 		if (PTP_MODE(ctrl)) {
2791 			ctrl->ev.gen.e = PRI_EVENT_DCHAN_DOWN;
2792 			res = &ctrl->ev;
2793 		}
2794 		break;
2795 	case Q921_TIMER_RECOVERY:
2796 		if (h->u.p_f) {
2797 			q921_mdl_error(link, 'B');
2798 		} else {
2799 			q921_mdl_error(link, 'E');
2800 		}
2801 		q921_establish_data_link(link);
2802 		link->l3_initiated = 0;
2803 		q921_setstate(link, Q921_AWAITING_ESTABLISHMENT);
2804 		if (PTP_MODE(ctrl)) {
2805 			ctrl->ev.gen.e = PRI_EVENT_DCHAN_DOWN;
2806 			res = &ctrl->ev;
2807 		}
2808 		break;
2809 	default:
2810 		pri_error(ctrl, "Don't know what to do with DM frame in state %d(%s)\n",
2811 			link->state, q921_state2str(link->state));
2812 		break;
2813 	}
2814 
2815 	return res;
2816 }
2817 
q921_rnr_rx(struct q921_link * link,q921_h * h)2818 static pri_event *q921_rnr_rx(struct q921_link *link, q921_h *h)
2819 {
2820 	pri_event *res = NULL;
2821 	struct pri *ctrl;
2822 
2823 	ctrl = link->ctrl;
2824 
2825 	if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
2826 		pri_message(ctrl, "TEI=%d Got RNR N(R)=%d\n", link->tei, h->s.n_r);
2827 	}
2828 
2829 	switch (link->state) {
2830 	case Q921_MULTI_FRAME_ESTABLISHED:
2831 		link->peer_rx_busy = 1;
2832 		if (!is_command(ctrl, h)) {
2833 			if (h->s.p_f) {
2834 				q921_mdl_error(link, 'A');
2835 			}
2836 		} else {
2837 			if (h->s.p_f) {
2838 				q921_enquiry_response(link);
2839 			}
2840 		}
2841 
2842 		if (!n_r_is_valid(link, h->s.n_r)) {
2843 			n_r_error_recovery(link);
2844 			q921_setstate(link, Q921_AWAITING_ESTABLISHMENT);
2845 		} else {
2846 			update_v_a(link, h->s.n_r);
2847 			stop_t203(link);
2848 			restart_t200(link);
2849 		}
2850 		break;
2851 	case Q921_TIMER_RECOVERY:
2852 		/* Q.921 Figure B.8 Q921 (Sheet 6 of 9) Page 85 */
2853 		link->peer_rx_busy = 1;
2854 		if (is_command(ctrl, h)) {
2855 			if (h->s.p_f) {
2856 				q921_enquiry_response(link);
2857 			}
2858 			if (n_r_is_valid(link, h->s.n_r)) {
2859 				update_v_a(link, h->s.n_r);
2860 				break;
2861 			} else {
2862 				n_r_error_recovery(link);
2863 				q921_setstate(link, Q921_AWAITING_ESTABLISHMENT);
2864 				break;
2865 			}
2866 		} else {
2867 			if (h->s.p_f) {
2868 				if (n_r_is_valid(link, h->s.n_r)) {
2869 					update_v_a(link, h->s.n_r);
2870 					restart_t200(link);
2871 					q921_invoke_retransmission(link, h->s.n_r);
2872 					q921_setstate(link, Q921_MULTI_FRAME_ESTABLISHED);
2873 					break;
2874 				} else {
2875 					n_r_error_recovery(link);
2876 					q921_setstate(link, Q921_AWAITING_ESTABLISHMENT);
2877 					break;
2878 				}
2879 			} else {
2880 				if (n_r_is_valid(link, h->s.n_r)) {
2881 					update_v_a(link, h->s.n_r);
2882 					break;
2883 				} else {
2884 					n_r_error_recovery(link);
2885 					q921_setstate(link, Q921_AWAITING_ESTABLISHMENT);
2886 					break;
2887 				}
2888 			}
2889 		}
2890 		break;
2891 	case Q921_TEI_ASSIGNED:
2892 	case Q921_AWAITING_ESTABLISHMENT:
2893 	case Q921_AWAITING_RELEASE:
2894 		/*
2895 		 * Ignore this frame.
2896 		 * We likely got reset and the other end has not realized it yet.
2897 		 */
2898 		break;
2899 	default:
2900 		pri_error(ctrl, "Don't know what to do with RNR in state %d(%s)\n",
2901 			link->state, q921_state2str(link->state));
2902 		break;
2903 	}
2904 
2905 	return res;
2906 }
2907 
q921_acknowledge_pending_check(struct q921_link * link)2908 static void q921_acknowledge_pending_check(struct q921_link *link)
2909 {
2910 	if (link->acknowledge_pending) {
2911 		link->acknowledge_pending = 0;
2912 		q921_rr(link, 0, 0);
2913 	}
2914 }
2915 
q921_statemachine_check(struct q921_link * link)2916 static void q921_statemachine_check(struct q921_link *link)
2917 {
2918 	switch (link->state) {
2919 	case Q921_MULTI_FRAME_ESTABLISHED:
2920 		q921_send_queued_iframes(link);
2921 		q921_acknowledge_pending_check(link);
2922 		break;
2923 	case Q921_TIMER_RECOVERY:
2924 		q921_acknowledge_pending_check(link);
2925 		break;
2926 	default:
2927 		break;
2928 	}
2929 }
2930 
__q921_receive_qualified(struct q921_link * link,q921_h * h,int len)2931 static pri_event *__q921_receive_qualified(struct q921_link *link, q921_h *h, int len)
2932 {
2933 	int res;
2934 	pri_event *ev = NULL;
2935 	struct pri *ctrl;
2936 
2937 	ctrl = link->ctrl;
2938 
2939 	switch (h->h.data[0] & Q921_FRAMETYPE_MASK) {
2940 	case 0:
2941 	case 2:
2942 		ev = q921_iframe_rx(link, h, len);
2943 		break;
2944 	case 1:
2945 		switch ((h->s.x0 << 2) | h->s.ss) {
2946 		case 0x00:
2947 			ev = q921_rr_rx(link, h);
2948 			break;
2949  		case 0x01:
2950 			ev = q921_rnr_rx(link, h);
2951 			break;
2952  		case 0x02:
2953 			ev = q921_rej_rx(link, h);
2954 			break;
2955 		default:
2956 			pri_error(ctrl,
2957 				"!! XXX Unknown Supervisory frame x0=%d ss=%d, pf=%d, N(R)=%d, V(A)=%d, V(S)=%d XXX\n",
2958 				h->s.x0, h->s.ss, h->s.p_f, h->s.n_r, link->v_a, link->v_s);
2959 			break;
2960 		}
2961 		break;
2962 	case 3:
2963 		if (len < 3) {
2964 			pri_error(ctrl, "!! Received short unnumbered frame\n");
2965 			break;
2966 		}
2967 		switch ((h->u.m3 << 2) | h->u.m2) {
2968 		case 0x03:
2969 			ev = q921_dm_rx(link, h);
2970 			break;
2971 		case 0x00:
2972 			/* UI-frame */
2973 			if (ctrl->debug & PRI_DEBUG_Q931_DUMP) {
2974 				q931_dump(ctrl, h->h.tei, (q931_h *) h->u.data, len - 3, 0);
2975 			}
2976 			res = q931_receive(link, (q931_h *) h->u.data, len - 3);
2977 			if (res != -1 && (res & Q931_RES_HAVEEVENT)) {
2978 				ev = &ctrl->ev;
2979 			}
2980 			break;
2981 		case 0x08:
2982 			ev = q921_disc_rx(link, h);
2983 			break;
2984 		case 0x0F:
2985 			/* SABME */
2986 			if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
2987 				pri_message(ctrl, "TEI=%d Got SABME from %s peer.\n",
2988 					link->tei, h->h.c_r ? "network" : "cpe");
2989 			}
2990 			if (h->h.c_r) {
2991 				ctrl->remotetype = PRI_NETWORK;
2992 				if (ctrl->localtype == PRI_NETWORK) {
2993 					/* We can't both be networks */
2994 					ev = pri_mkerror(ctrl, "We think we're the network, but they think they're the network, too.");
2995 					break;
2996 				}
2997 			} else {
2998 				ctrl->remotetype = PRI_CPE;
2999 				if (ctrl->localtype == PRI_CPE) {
3000 					/* We can't both be CPE */
3001 					ev = pri_mkerror(ctrl, "We think we're the CPE, but they think they're the CPE too.\n");
3002 					break;
3003 				}
3004 			}
3005 			ev = q921_sabme_rx(link, h);
3006 			break;
3007 		case 0x0C:
3008 			ev = q921_ua_rx(link, h);
3009 			break;
3010 		case 0x11:
3011 			ev = q921_frmr_rx(link, h);
3012 			break;
3013 		case 0x17:
3014 			pri_error(ctrl, "!! XID frames not supported\n");
3015 			break;
3016 		default:
3017 			pri_error(ctrl, "!! Don't know what to do with u-frame (m3=%d, m2=%d)\n",
3018 				h->u.m3, h->u.m2);
3019 			break;
3020 		}
3021 		break;
3022 	}
3023 
3024 	q921_statemachine_check(link);
3025 
3026 	return ev;
3027 }
3028 
q921_handle_unmatched_frame(struct pri * ctrl,q921_h * h,int len)3029 static pri_event *q921_handle_unmatched_frame(struct pri *ctrl, q921_h *h, int len)
3030 {
3031 	if (!BRI_NT_PTMP(ctrl)) {
3032 		return NULL;
3033 	}
3034 
3035 	if (h->h.tei < Q921_TEI_AUTO_FIRST) {
3036 		if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
3037 			pri_message(ctrl, "Manual TEI range is not supported in NT-PTMP mode. Discarding\n");
3038 		}
3039 		return NULL;
3040 	}
3041 
3042 	if (h->h.sapi != Q921_SAPI_CALL_CTRL) {
3043 		if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
3044 			pri_message(ctrl, "Message with SAPI other than CALL CTRL is discarded\n");
3045 		}
3046 		return NULL;
3047 	}
3048 
3049 	/* This means an unrecognized TEI that we'll kill */
3050 	if (ctrl->debug & PRI_DEBUG_Q921_STATE) {
3051 		pri_message(ctrl,
3052 			"Could not find a layer 2 link for received frame with SAPI/TEI of %d/%d.\n",
3053 			h->h.sapi, h->h.tei);
3054 		pri_message(ctrl, "Sending TEI release, in order to re-establish TEI state\n");
3055 	}
3056 	q921_tei_remove(ctrl, h->h.tei);
3057 
3058 	return NULL;
3059 }
3060 
3061 /* This code assumes that the pri structure is the master pri */
__q921_receive(struct pri * ctrl,q921_h * h,int len)3062 static pri_event *__q921_receive(struct pri *ctrl, q921_h *h, int len)
3063 {
3064 	pri_event *ev = NULL;
3065 	struct q921_link *link;
3066 
3067 	/* Discard FCS */
3068 	len -= 2;
3069 
3070 	if (ctrl->debug & (PRI_DEBUG_Q921_DUMP | PRI_DEBUG_Q921_RAW)) {
3071 		q921_dump(ctrl, h, len, ctrl->debug, 0);
3072 	}
3073 
3074 	/* Check some reject conditions -- Start by rejecting improper ea's */
3075 	if (h->h.ea1 || !h->h.ea2) {
3076 		return NULL;
3077 	}
3078 
3079 	if (h->h.sapi == Q921_SAPI_LAYER2_MANAGEMENT) {
3080 		return q921_mdl_receive(ctrl, &h->u, len);
3081 	}
3082 
3083 	if (h->h.tei == Q921_TEI_GROUP && h->h.sapi != Q921_SAPI_CALL_CTRL) {
3084 		pri_error(ctrl, "Do not handle group messages to services other than MDL or CALL CTRL\n");
3085 		return NULL;
3086 	}
3087 
3088 	if (BRI_TE_PTMP(ctrl)) {
3089 		/* We're operating on the specific TEI link */
3090 		link = ctrl->link.next;
3091 		if (h->h.sapi == link->sapi
3092 			&& ((link->state >= Q921_TEI_ASSIGNED
3093 				&& h->h.tei == link->tei)
3094 				|| h->h.tei == Q921_TEI_GROUP)) {
3095 			ev = __q921_receive_qualified(link, h, len);
3096 		}
3097 		/* Only support reception on our specific TEI link */
3098 	} else if (BRI_NT_PTMP(ctrl)) {
3099 		link = pri_find_tei(ctrl, h->h.sapi, h->h.tei);
3100 		if (link) {
3101 			ev = __q921_receive_qualified(link, h, len);
3102 		} else {
3103 			ev = q921_handle_unmatched_frame(ctrl, h, len);
3104 		}
3105 	} else if (PTP_MODE(ctrl)
3106 		&& h->h.sapi == ctrl->link.sapi
3107 		&& (h->h.tei == ctrl->link.tei || h->h.tei == Q921_TEI_GROUP)) {
3108 		ev = __q921_receive_qualified(&ctrl->link, h, len);
3109 	} else {
3110 		ev = NULL;
3111 	}
3112 
3113 	if (ctrl->debug & PRI_DEBUG_Q921_DUMP) {
3114 		pri_message(ctrl, "Done handling message for SAPI/TEI=%d/%d\n", h->h.sapi, h->h.tei);
3115 	}
3116 
3117 	return ev;
3118 }
3119 
q921_receive(struct pri * ctrl,q921_h * h,int len)3120 pri_event *q921_receive(struct pri *ctrl, q921_h *h, int len)
3121 {
3122 	pri_event *e;
3123 	e = __q921_receive(ctrl, h, len);
3124 	ctrl->q921_rxcount++;
3125 	return e;
3126 }
3127 
q921_establish_data_link(struct q921_link * link)3128 static void q921_establish_data_link(struct q921_link *link)
3129 {
3130 	q921_clear_exception_conditions(link);
3131 	link->RC = 0;
3132 	stop_t203(link);
3133 	reschedule_t200(link);
3134 	q921_send_sabme(link);
3135 }
3136 
nt_ptmp_dchannel_up(void * vpri)3137 static void nt_ptmp_dchannel_up(void *vpri)
3138 {
3139 	struct pri *ctrl = vpri;
3140 
3141 	ctrl->schedev = 1;
3142 	ctrl->ev.gen.e = PRI_EVENT_DCHAN_UP;
3143 }
3144 
q921_start(struct q921_link * link)3145 void q921_start(struct q921_link *link)
3146 {
3147 	struct pri *ctrl;
3148 
3149 	ctrl = link->ctrl;
3150 	if (PTMP_MODE(ctrl)) {
3151 		if (TE_MODE(ctrl)) {
3152 			q921_setstate(link, Q921_ASSIGN_AWAITING_TEI);
3153 			q921_tei_request(link);
3154 		} else {
3155 			q921_setstate(link, Q921_TEI_UNASSIGNED);
3156 			pri_schedule_event(ctrl, 0, nt_ptmp_dchannel_up, ctrl);
3157 			if (!ctrl->link.next) {
3158 				/*
3159 				 * We do not have any TEI's so make sure there are no devices
3160 				 * that think they have a TEI.  A device may think it has a TEI
3161 				 * if the upper layer program is restarted or the system
3162 				 * reboots.
3163 				 */
3164 				q921_tei_remove(ctrl, Q921_TEI_GROUP);
3165 			}
3166 		}
3167 	} else {
3168 		/* PTP mode, no need for TEI management junk */
3169 		q921_establish_data_link(link);
3170 		link->l3_initiated = 1;
3171 		q921_setstate(link, Q921_AWAITING_ESTABLISHMENT);
3172 	}
3173 }
3174 
3175