blob: fba31f8c591ba0035b9f2c065cff0b8b7de3898b [file] [log] [blame]
Alexander Couzens841817e2020-11-19 00:41:29 +01001/*! \file frame_relay.c
2 * Implement frame relay/PVC by Q.933
3 */
4/* (C) 2020 Harald Welte <laforge@gnumonks.org>
5 * (C) 2020 sysmocom - s.f.m.c. GmbH
6 * Author: Alexander Couzens <lynxis@fe80.eu>
7 *
8 * All Rights Reserved
9 *
10 * SPDX-License-Identifier: GPL-2.0+
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 *
25 */
26
27#include <stdint.h>
28#include <stdbool.h>
29#include <unistd.h>
30#include <errno.h>
31
32#include <osmocom/gprs/frame_relay.h>
33#include <osmocom/core/endian.h>
34#include <osmocom/core/utils.h>
35#include <osmocom/core/logging.h>
36#include <osmocom/core/linuxlist.h>
37#include <osmocom/core/tdef.h>
38#include <osmocom/core/timer.h>
39#include <osmocom/core/msgb.h>
40
41#include <osmocom/gsm/tlv.h>
42
43#define LOGPFRL(frl, lvl, fmt, args ...) \
44 LOGP(DFR, lvl, "%s: " fmt, (frl)->name, ## args)
45
46#define DFR DLNS
47
48/* Table 4-2/Q.931 */
49enum q931_msgtype {
50 /* Call establishment message */
51 Q931_MSGT_ALERTING = 0x01,
52 Q931_MSGT_CALL_PROCEEDING = 0x02,
53 Q931_MSGT_CONNECT = 0x07,
54 Q931_MSGT_CONNECT_ACK = 0x0f,
55 Q931_MSGT_PROGRESS = 0x03,
56 Q931_MSGT_SETUP = 0x05,
57 Q931_MSGT_SETUP_ACK = 0x0d,
58 /* Call information phase message */
59 Q931_MSGT_RESUME = 0x26,
60 Q931_MSGT_RESUME_ACK = 0x2e,
61 Q931_MSGT_RESUME_REJ = 0x22,
62 Q931_MSGT_SUSPEND = 0x25,
63 Q931_MSGT_SUSPEND_ACK = 0x2d,
64 Q931_MSGT_USER_INFO = 0x20,
65 /* Call clearing message */
66 Q931_MSGT_DISCONNECT = 0x45,
67 Q931_MSGT_RELEASE = 0x4d,
68 Q931_MSGT_RELEASE_COMPLETE = 0x5a,
69 Q931_MSGT_RESTART = 0x46,
70 Q931_MSGT_RESTART_ACK = 0x4e,
71 /* Miscellaneous messages */
72 Q931_MSGT_SEGMENT = 0x60,
73 Q931_MSGT_CONGESTION_CONTROL = 0x79,
74 Q931_MSGT_IFORMATION = 0x7b,
75 Q931_MSGT_NOTIFY = 0x6e,
76 Q931_MSGT_STATUS = 0x7d,
77 Q931_MSGT_STATUS_ENQUIRY = 0x75,
78};
79
80
81/* Figure A.1/Q.933 Report type information element */
82enum q933_type_of_report {
83 Q933_REPT_FULL_STATUS = 0x00,
84 Q933_REPT_LINK_INTEGRITY_VERIF = 0x01,
85 Q933_REPT_SINGLE_PVC_ASYNC_STS = 0x02,
86};
87
88/* Q.933 Section A.3 */
89enum q933_iei {
90 Q933_IEI_REPORT_TYPE = 0x51,
91 Q933_IEI_LINK_INT_VERIF = 0x53,
92 Q933_IEI_PVC_STATUS = 0x57,
93};
94
95/* Q.933 Section A.3.3 */
96enum q933_pvc_status {
97 Q933_PVC_STATUS_DLC_ACTIVE = 0x02,
98 Q933_PVC_STATUS_DLC_DELETE = 0x04,
99 Q933_PVC_STATUS_DLC_NEW = 0x08,
100};
101
102
103
104#define LAPF_UI 0x03 /* UI control word */
105#define Q931_PDISC_CC 0x08 /* protocol discriminator */
106#define LMI_Q933A_CALLREF 0x00 /* NULL call-ref */
107
108/* LMI DLCI values */
109#define LMI_Q933A_DLCI 0 /* Q.933A DLCI */
110#define LMI_CISCO_DLCI 1023 /* Cisco DLCI */
111
112/* maximum of supported */
113#define MAX_SUPPORTED_PVC 10
114
115/* TODO: add counters since good connection */
116
117/* Message header of the L3 payload of a Q.933 Annex A message */
118struct q933_a_hdr {
119 uint8_t prot_disc;
120 uint8_t call_ref;
121 uint8_t msg_type;
122} __attribute__((packed));
123
124/* Value part of the Q.933 Annex A.3.3 IE */
125struct q933_a_pvc_sts {
126 uint8_t dlci_msb:6,
127 spare:1,
128 ext0:1;
129 uint8_t space1:3,
130 dlci_lsb:4,
131 ext1:1;
132 uint8_t reserved:1,
133 active:1,
134 delete:1,
135 new:1,
136 spare2:3,
137 ext2:1;
138
139} __attribute__((packed));
140
141/* RX Message: 14 [ 00 01 03 08 00 75 95 01 01 00 03 02 01 00 ] */
142/* RX Message: 13 [ 00 01 03 08 00 75 51 01 00 53 02 01 00 ] */
143
144const struct value_string osmo_fr_role_names[] = {
145 { FR_ROLE_USER_EQUIPMENT, "USER" },
146 { FR_ROLE_NETWORK_EQUIPMENT, "NETWORK" },
147 { 0, NULL }
148};
149
150/* Table A.4/Q.933 */
151struct osmo_tdef fr_tdefs[] = {
152 {
153 .T=391,
154 .default_val = 10,
155 .min_val = 5,
156 .max_val = 30,
157 .desc = "Link integrity verification polling timer",
158 .unit = OSMO_TDEF_S,
159 }, {
160 .T=392,
161 .default_val = 15,
162 .min_val = 5,
163 .max_val = 30,
164 .desc = "Polling verification timer",
165 .unit = OSMO_TDEF_S,
166 },
167 {}
168};
169
170static const struct tlv_definition q933_att_tlvdef = {
171 .def = {
172 [Q933_IEI_REPORT_TYPE] = { TLV_TYPE_TLV },
173 [Q933_IEI_LINK_INT_VERIF] = { TLV_TYPE_TLV },
174 [Q933_IEI_PVC_STATUS] = { TLV_TYPE_TLV },
175 },
176};
177
178static void check_link_state(struct osmo_fr_link *link, bool valid);
179
180static inline uint16_t q922_to_dlci(const uint8_t *hdr)
181{
182 return ((hdr[0] & 0xFC) << 2) | ((hdr[1] & 0xF0) >> 4);
183}
184
185
186static inline void dlci_to_q922(uint8_t *hdr, uint16_t dlci)
187{
188 hdr[0] = (dlci >> 2) & 0xFC;
189 hdr[1] = ((dlci << 4) & 0xF0) | 0x01;
190}
191
192/* allocate a message buffer and put Q.933 Annex A headers (L2 + L3) */
193static struct msgb *q933_msgb_alloc(uint16_t dlci, uint8_t prot_disc, uint8_t msg_type)
194{
195 struct msgb *msg = msgb_alloc_headroom(1600+64, 64, "FR Q.933 Tx");
196 struct q933_a_hdr *qh;
197
198 if (!msg)
199 return NULL;
200
201 msg->l1h = msgb_put(msg, 2);
202 dlci_to_q922(msg->l1h, dlci);
203
204 /* LAPF UI control */
205 msg->l2h = msgb_put(msg, 1);
206 *msg->l2h = LAPF_UI;
207
208 msg->l3h = msgb_put(msg, sizeof(*qh));
209 qh = (struct q933_a_hdr *) msg->l3h;
210 qh->prot_disc = prot_disc;
211 qh->call_ref = LMI_Q933A_CALLREF;
212 qh->msg_type = msg_type;
213
214 return msg;
215}
216
217/* obtain the [next] transmit sequence number */
218static uint8_t link_get_tx_seq(struct osmo_fr_link *link)
219{
220 /* The {user equipment, network} increments the send sequence
221 * counter using modulo 256. The value zero is skipped. */
222 link->last_tx_seq++;
223 if (link->last_tx_seq == 0)
224 link->last_tx_seq++;
225
226 return link->last_tx_seq;
227}
228
229/* Append PVC Status IE according to Q.933 A.3.2 */
230static void msgb_put_link_int_verif(struct msgb *msg, struct osmo_fr_link *link)
231{
232 uint8_t link_int_tx[2];
233 link_int_tx[0] = link_get_tx_seq(link);
234 link_int_tx[1] = link->last_rx_seq;
235 msgb_tlv_put(msg, Q933_IEI_LINK_INT_VERIF, 2, link_int_tx);
236}
237
238static void dlc_destroy(struct osmo_fr_dlc *dlc)
239{
240 llist_del(&dlc->list);
241 talloc_free(dlc);
242}
243
244/* Append PVC Status IE according to Q.933 A.3.3 */
245static void msgb_put_pvc_status(struct msgb *msg, struct osmo_fr_dlc *dlc)
246{
247 uint8_t ie[3];
248
249 ie[0] = (dlc->dlci >> 4) & 0x3f;
250 /* extension bits */
251 ie[1] = 0x80 | ((dlc->dlci & 0xf) << 3);
252 /* extension bits */
253 ie[2] = 0x80;
254
255 /* FIXME: validate: this status should be added as long it's not yet acked by the remote */
256 if (dlc->active)
257 ie[2] |= Q933_PVC_STATUS_DLC_ACTIVE;
258
259 if (dlc->add) {
260 ie[2] |= Q933_PVC_STATUS_DLC_NEW;
261 /* we've reported it as new once, reset the status */
262 }
263
264 if (dlc->del) {
265 ie[2] |= Q933_PVC_STATUS_DLC_DELETE;
266 /* we've reported it as deleted once, destroy it */
267 dlc_destroy(dlc);
268 }
269
270 msgb_tlv_put(msg, Q933_IEI_PVC_STATUS, 3, ie);
271}
272
273/* Send a Q.933 STATUS ENQUIRY given type over given link */
274static int tx_lmi_q933_status_enq(struct osmo_fr_link *link, uint8_t rep_type)
275{
276 struct msgb *resp;
277
278 resp = q933_msgb_alloc(0, Q931_PDISC_CC, Q931_MSGT_STATUS_ENQUIRY);
279 if (!resp)
280 return -1;
281 resp->dst = link;
282 link->expected_rep = rep_type;
283
284 /* Table A.2/Q.933 */
285 msgb_tlv_put(resp, Q933_IEI_REPORT_TYPE, 1, &rep_type);
286 msgb_put_link_int_verif(resp, link);
287
288 return link->tx_cb(link->tx_cb_data, resp);
289}
290
291/* Send a Q.933 STATUS of given type over given link */
292static int tx_lmi_q933_status(struct osmo_fr_link *link, uint8_t rep_type)
293{
294 struct osmo_fr_dlc *dlc;
295 struct msgb *resp;
296
297 resp = q933_msgb_alloc(0, Q931_PDISC_CC, Q931_MSGT_STATUS);
298 if (!resp)
299 return -1;
300
301 resp->dst = link;
302
303 /* Table A.1/Q.933 */
304 msgb_tlv_put(resp, Q933_IEI_REPORT_TYPE, 1, &rep_type);
305 switch (rep_type) {
306 case Q933_REPT_FULL_STATUS:
307 msgb_put_link_int_verif(resp, link);
308 llist_for_each_entry(dlc, &link->dlc_list, list) {
309 if (dlc->add || dlc->del)
310 dlc->state_send = true;
311
312 msgb_put_pvc_status(resp, dlc);
313 }
314 break;
315 case Q933_REPT_LINK_INTEGRITY_VERIF:
316 msgb_put_link_int_verif(resp, link);
317 llist_for_each_entry(dlc, &link->dlc_list, list) {
318 if (dlc->add || dlc->del) {
319 msgb_put_pvc_status(resp, dlc);
320 dlc->state_send = true;
321 }
322 }
323 break;
324 case Q933_REPT_SINGLE_PVC_ASYNC_STS:
325 llist_for_each_entry(dlc, &link->dlc_list, list)
326 msgb_put_pvc_status(resp, dlc);
327 break;
328 }
329
330 return link->tx_cb(link->tx_cb_data, resp);
331}
332
333
334/* Q.933 */
335static int rx_lmi_q933_status_enq(struct msgb *msg, struct tlv_parsed *tp)
336{
337 struct osmo_fr_link *link = msg->dst;
338 struct osmo_fr_dlc *dlc;
339 const uint8_t *link_int_rx;
340 uint8_t rep_type;
341
342 OSMO_ASSERT(link);
343
344 if (link->role == FR_ROLE_USER_EQUIPMENT) {
Harald Welte78ebc3f2020-11-25 17:39:06 +0100345 LOGPFRL(link, LOGL_ERROR, "STATUS-ENQ aren't supported in role user\n");
Alexander Couzens841817e2020-11-19 00:41:29 +0100346 return -1;
347 }
348
349 /* check for mandatory IEs */
350 if (!TLVP_PRES_LEN(tp, Q933_IEI_REPORT_TYPE, 1) ||
351 !TLVP_PRES_LEN(tp, Q933_IEI_LINK_INT_VERIF, 2))
352 return -1;
353
354 rep_type = *TLVP_VAL(tp, Q933_IEI_REPORT_TYPE);
355
356 link_int_rx = TLVP_VAL(tp, Q933_IEI_LINK_INT_VERIF);
357 link->last_rx_seq = link_int_rx[0];
358
359 /* the network checks the receive sequence number received from
360 * the user equipment against its send sequence counter */
361 if (link_int_rx[1] != link->last_tx_seq) {
362 check_link_state(link, false);
363 link->err_count++;
364 } else {
365 check_link_state(link, true);
366 /* confirm DLC state changes */
367 llist_for_each_entry(dlc, &link->dlc_list, list) {
368 if (!dlc->state_send)
369 continue;
370
371 if (dlc->add) {
372 dlc->active = link->state;
373 dlc->add = false;
374 }
375
376 if (dlc->del) {
377 dlc->del = false;
378 }
379
380 dlc->state_send = false;
381 }
382 }
383
384
385 /* The network responds to each STATUS ENQUIRY message with a
386 * STATUS message and resets the T392 timer */
387 osmo_timer_schedule(&link->t392, osmo_tdef_get(link->net->T_defs, 392, OSMO_TDEF_S, 15), 0);
388
389 return tx_lmi_q933_status(link, rep_type);
390}
391
392/* check if the link become active.
393 * The link becomes active when enough times a STATUS/STATUS ENQUIRY arrives without any loss.
394 * Look at the last N393 STATUS/STATUS ENQUIRY PDUs. The link is valid if at least N392
395 * got received.
396 * param[in] valid contains the status of the last packet */
397static void check_link_state(struct osmo_fr_link *link, bool valid)
398{
399 unsigned int last, i;
400 unsigned int carry = 0;
401 struct osmo_fr_dlc *dlc;
402
403 link->succeed <<= 1;
404 if (valid)
405 link->succeed |= 1;
406
407 /* count the bits */
408 last = link->succeed & ((1 << link->net->n393) - 1);
409 for (i = 0; i < link->net->n393; i++)
410 if (last & (1 << i))
411 carry++;
412
413 if (link->net->n393 - carry >= link->net->n392) {
414 /* failing link */
415 if (!link->state)
416 return;
417
418 LOGPFRL(link, LOGL_NOTICE, "Link failed\n");
419 link->state = false;
420 if (link->role == FR_ROLE_USER_EQUIPMENT)
421 return;
422
423 llist_for_each_entry(dlc, &link->dlc_list, list) {
424 dlc->active = false;
425 }
426 } else {
427 /* good link */
428 if (link->state)
429 return;
430
431 LOGPFRL(link, LOGL_NOTICE, "Link recovered\n");
432 link->state = true;
433 if (link->role == FR_ROLE_USER_EQUIPMENT)
434 return;
435
436 llist_for_each_entry(dlc, &link->dlc_list, list) {
437 if (!dlc->add && !dlc->del)
438 dlc->active = true;
439 }
440 }
441}
442
443static int validate_pvc_status(struct tlv_parsed *tp, size_t tp_len)
444{
445 size_t i;
446 uint16_t len = 0;
447
448 for (i = 0; i < tp_len; i++) {
449 if (!TLVP_PRESENT(&tp[i], Q933_IEI_PVC_STATUS))
450 continue;
451
452 /* PVC status can be 2 or 3 bytes. If the PVC is bigger
453 * ignore this to be compatible to future extensions. */
454 len = TLVP_LEN(&tp[i], Q933_IEI_PVC_STATUS);
455 if (len <= 1) {
456 return -EINVAL;
457 }
458 /* FIXME: validate correct flags: are some flags invalid at the same time? */
459 }
460
461 return 0;
462}
463
464static int parse_full_pvc_status(struct osmo_fr_link *link, struct tlv_parsed *tp, size_t tp_len)
465{
466 size_t i;
467 int err = 0;
468 struct osmo_fr_dlc *dlc, *tmp;
469 struct q933_a_pvc_sts *pvc;
470 uint16_t dlci = 0;
471 uint16_t *dlcis = talloc_zero_array(link, uint16_t, tp_len);
472 if (!dlcis)
473 return -ENOMEM;
474
475 /* first run validate all PVCs */
476 err = validate_pvc_status(tp, tp_len);
477 if (err < 0)
478 goto out;
479
480 for (i = 0; i < tp_len; i++) {
481 if (!TLVP_PRESENT(&tp[i], Q933_IEI_PVC_STATUS))
482 continue;
483
484 /* parse only 3 byte PVCs */
485 pvc = (struct q933_a_pvc_sts *) TLVP_VAL_MINLEN(
486 &tp[i],
487 Q933_IEI_PVC_STATUS,
488 sizeof(struct q933_a_pvc_sts));
489 if (!pvc)
490 continue;
491
492 dlci = ((pvc->dlci_msb & 0x3f) << 4) | (pvc->dlci_lsb & 0xf);
493 dlcis[i] = dlci;
494 dlc = osmo_fr_dlc_by_dlci(link, dlci);
495 if (!dlc) {
496 dlc = osmo_fr_dlc_alloc(link, dlci);
497 if (!dlc) {
498 LOGPFRL(link, LOGL_ERROR, "Could not create DLC %d\n", dlci);
499 continue;
500 }
501 }
502
503 /* Figure A.3/Q.933: The delete bit is only applicable for timely notification
504 * using the optional single PVC asynchronous status report.
505 * Ignoring the delete. */
506 dlc->add = pvc->new;
507 dlc->active = pvc->active;
508 dlc->del = 0;
509 }
510
511 /* check if all dlc are present in PVC Status */
512 llist_for_each_entry_safe(dlc, tmp, &link->dlc_list, list) {
513 bool found = false;
514 for (i = 0; i < tp_len; i++) {
515 if (dlcis[i] == dlc->dlci) {
516 found = true;
517 break;
518 }
519 }
520
521 if (!found) {
522 dlc->active = false;
523 dlc->del = true;
524 }
525 }
526
527 return 0;
528out:
529 talloc_free(dlcis);
530 return err;
531}
532
533static int parse_link_pvc_status(struct osmo_fr_link *link, struct tlv_parsed *tp, size_t tp_len)
534{
535 int err;
536 size_t i;
537 struct q933_a_pvc_sts *pvc;
538 struct osmo_fr_dlc *dlc;
539 uint16_t dlci = 0;
540
541 err = validate_pvc_status(tp, tp_len);
542 if (err < 0)
543 return err;
544
545 for (i = 0; i < tp_len; i++) {
546 if (!TLVP_PRESENT(&tp[i], Q933_IEI_PVC_STATUS))
547 continue;
548
549 /* parse only 3 byte PVCs */
550 pvc = (struct q933_a_pvc_sts *) TLVP_VAL_MINLEN(
551 &tp[i],
552 Q933_IEI_PVC_STATUS,
553 sizeof(struct q933_a_pvc_sts));
554 if (!pvc)
555 continue;
556
557 dlci = ((pvc->dlci_msb & 0x3f) << 4) | (pvc->dlci_lsb & 0xf);
558 dlc = osmo_fr_dlc_by_dlci(link, dlci);
559 if (!dlc) {
560 /* don't create dlc's for the ones which are about to be deleted. */
561 if (dlc->del)
562 continue;
563
564 dlc = osmo_fr_dlc_alloc(link, dlci);
565 if (!dlc) {
566 LOGPFRL(link, LOGL_ERROR, "Rx STATUS: Could not create DLC %d\n", dlci);
567 }
568 }
569
570 if (pvc->delete) {
571 dlc->del = 1;
572 } else {
573 dlc->add = pvc->new;
574 dlc->active = pvc->active;
575 dlc->del = 0;
576 }
577 }
578
579 return 0;
580}
581
582static size_t count_pvc_status(struct tlv_parsed *tp, size_t tp_len)
583{
584 size_t i, count = 0;
585 for (i = 0; i < tp_len; i++) {
586 if (!TLVP_PRESENT(&tp[i], Q933_IEI_PVC_STATUS))
587 continue;
588 count++;
589 }
590
591 return count;
592}
593
594static int rx_lmi_q933_status(struct msgb *msg, struct tlv_parsed *tp)
595{
596 struct osmo_fr_link *link = msg->dst;
597 const uint8_t *link_int_rx;
598 uint8_t rep_type;
599
600 OSMO_ASSERT(link);
601
602 if (link->role == FR_ROLE_NETWORK_EQUIPMENT) {
Harald Welte78ebc3f2020-11-25 17:39:06 +0100603 LOGPFRL(link, LOGL_ERROR, "Rx STATUS: STATUS aren't supported in role network\n");
Alexander Couzens841817e2020-11-19 00:41:29 +0100604 return -1;
605 }
606
607 /* check for mandatory IEs */
608 if (!TLVP_PRES_LEN(tp, Q933_IEI_REPORT_TYPE, 1)) {
609 LOGPFRL(link, LOGL_NOTICE, "Rx STATUSL: Missing TLV Q933 Report Type\n");
610 return -1;
611 }
612
613 rep_type = *TLVP_VAL(tp, Q933_IEI_REPORT_TYPE);
614
615 switch (rep_type) {
616 case Q933_REPT_FULL_STATUS:
617 case Q933_REPT_LINK_INTEGRITY_VERIF:
618 if (rep_type != link->expected_rep) {
619 LOGPFRL(link, LOGL_NOTICE, "Rx STATUS: Unexpected Q933 report type (got 0x%x != exp 0x%x)\n",
620 rep_type, link->expected_rep);
621 return -1;
622 }
623
624 if (!TLVP_PRES_LEN(tp, Q933_IEI_LINK_INT_VERIF, 2)) {
625 LOGPFRL(link, LOGL_NOTICE, "Rx STATUS: Missing TLV Q933 Link Integrety Verification\n");
626 return -1;
627 }
628 link_int_rx = TLVP_VAL(tp, Q933_IEI_LINK_INT_VERIF);
629 link->last_rx_seq = link_int_rx[0];
630 /* The received receive sequence number is not valid if
631 * it is not equal to the last transmitted send sequence
632 * number. Ignore messages containing this error. As a
633 * result, timer T391 expires and the user then
634 * increments the error count. */
635 if (link_int_rx[1] != link->last_tx_seq)
636 return 0;
637 break;
638 case Q933_REPT_SINGLE_PVC_ASYNC_STS:
639 default:
640 return -1;
641 }
642
643 check_link_state(link, true);
644 if (count_pvc_status(tp, MAX_SUPPORTED_PVC + 1) > MAX_SUPPORTED_PVC) {
645 LOGPFRL(link, LOGL_ERROR, "Rx STATUS: Too many PVC! Only %d are supported!\n", MAX_SUPPORTED_PVC);
646 }
647
648 switch (rep_type) {
649 case Q933_REPT_FULL_STATUS:
650 parse_full_pvc_status(link, tp, MAX_SUPPORTED_PVC);
651 break;
652 case Q933_REPT_LINK_INTEGRITY_VERIF:
653 parse_link_pvc_status(link, tp, MAX_SUPPORTED_PVC);
654 break;
655 default:
656 break;
657 }
658
659 /* The network responds to each STATUS ENQUIRY message with a
660 * STATUS message and resets the T392 timer */
661 osmo_timer_schedule(&link->t392, osmo_tdef_get(link->net->T_defs, 392, OSMO_TDEF_S, 15), 0);
662
663 return 0;
664}
665
666static int rx_lmi_q922(struct msgb *msg)
667{
668 struct osmo_fr_link *link = msg->dst;
669 struct q933_a_hdr *qh;
670 /* the + 1 is used to detect more than MAX_SUPPORTED_PVC */
671 struct tlv_parsed tp[MAX_SUPPORTED_PVC + 1];
672 uint8_t *lapf;
673 int rc;
674
675 OSMO_ASSERT(link);
676
677 if (msgb_l2len(msg) < 1)
678 return -1;
679 lapf = msgb_l2(msg);
680
681 /* we only support LAPF UI frames */
682 if (lapf[0] != LAPF_UI)
683 return -1;
684
685 msg->l3h = msg->l2h + 1;
686 if (msgb_l3len(msg) < 3)
687 return -1;
688
689 qh = (struct q933_a_hdr *) msgb_l3(msg);
690 if (qh->prot_disc != Q931_PDISC_CC) {
691 LOGPFRL(link, LOGL_NOTICE,
692 "Rx unsupported LMI protocol discriminator %u\n", qh->prot_disc);
693 return -1;
694 }
695
696 rc = tlv_parse2(tp, MAX_SUPPORTED_PVC + 1, &q933_att_tlvdef,
697 msgb_l3(msg) + sizeof(*qh),
698 msgb_l3len(msg) - sizeof(*qh), 0, 0);
699 if (rc < 0) {
700 LOGPFRL(link, LOGL_NOTICE,
701 "Failed to parse TLVs in LMI message type %u\n", qh->msg_type);
702 return rc;
703 }
704
705 switch (qh->msg_type) {
706 case Q931_MSGT_STATUS_ENQUIRY:
707 rc = rx_lmi_q933_status_enq(msg, tp);
708 break;
709 case Q931_MSGT_STATUS:
710 rc = rx_lmi_q933_status(msg, tp);
711 break;
712 default:
713 LOGPFRL(link, LOGL_NOTICE,
714 "Rx unsupported LMI message type %u\n", qh->msg_type);
715 rc = -1;
716 break;
717 }
718 msgb_free(msg);
719
720 return rc;
721}
722
723int osmo_fr_rx(struct msgb *msg)
724{
725 int rc = 0;
726 uint8_t *frh;
727 uint16_t dlci;
728 struct osmo_fr_dlc *dlc;
729 struct osmo_fr_link *link = msg->dst;
730
731 OSMO_ASSERT(link);
732
733 if (msgb_length(msg) < 2) {
734 LOGPFRL(link, LOGL_ERROR, "Rx short FR header: %u bytes\n", msgb_length(msg));
735 rc = -1;
736 goto out;
737 }
738
739 frh = msg->l1h = msgb_data(msg);
740 if (frh[0] & 0x01) {
741 LOGPFRL(link, LOGL_NOTICE, "Rx Unsupported single-byte FR address\n");
742 rc = -1;
743 goto out;
744 }
745 if ((frh[1] & 0x0f) != 0x01) {
746 LOGPFRL(link, LOGL_NOTICE, "Rx Unknown second FR octet 0x%02x\n", frh[1]);
747 rc = -1;
748 goto out;
749 }
750 dlci = q922_to_dlci(frh);
751 msg->l2h = frh + 2;
752
753 switch (dlci) {
754 case LMI_Q933A_DLCI:
755 return rx_lmi_q922(msg);
756 case LMI_CISCO_DLCI:
757 LOGPFRL(link, LOGL_ERROR, "Rx Unsupported FR DLCI %u\n", dlci);
758 goto out;
759 }
760
761 if (!link->state) {
762 LOGPFRL(link, LOGL_NOTICE, "Link is not reliable. Discarding Rx PDU on DLCI %d\n", dlci);
763 goto out;
764 }
765
766 llist_for_each_entry(dlc, &link->dlc_list, list) {
767 if (dlc->dlci == dlci) {
768 /* dispatch to handler of respective DLC */
769 msg->dst = dlc;
770 return dlc->rx_cb(dlc->rx_cb_data, msg);
771 }
772 }
773
774 if (link->unknown_dlc_rx_cb)
775 return link->unknown_dlc_rx_cb(link->unknown_dlc_rx_cb_data, msg);
776 else
777 LOGPFRL(link, LOGL_NOTICE, "DLCI %u doesn't exist, discarding\n", dlci);
778
779out:
780 msgb_free(msg);
781
782 return rc;
783}
784
785int osmo_fr_tx_dlc(struct msgb *msg)
786{
787 uint8_t *frh;
788 struct osmo_fr_dlc *dlc = msg->dst;
789 struct osmo_fr_link *link = dlc->link;
790
791 OSMO_ASSERT(dlc);
792 OSMO_ASSERT(link);
793
794 if (!link->state) {
795 LOGPFRL(link, LOGL_NOTICE, "Link is not reliable (yet), discarding Tx\n");
796 msgb_free(msg);
797 return -1;
798 }
799 if (!dlc->active) {
800 LOGPFRL(link, LOGL_NOTICE, "DLCI %u is not active (yet), discarding Tx\n", dlc->dlci);
801 msgb_free(msg);
802 return -1;
803 }
804 LOGPFRL(link, LOGL_DEBUG, "DLCI %u is active, sending message\n", dlc->dlci);
805
806 if (msgb_headroom(msg) < 2) {
807 msgb_free(msg);
808 return -ENOSPC;
809 }
810
811 frh = msgb_push(msg, 2);
812 dlci_to_q922(frh, dlc->dlci);
813
814 msg->dst = link;
815 return link->tx_cb(link->tx_cb_data, msg);
816}
817
818/* Every T391 seconds, the user equipment sends a STATUS ENQUIRY
819 * message to the network and resets its polling timer (T391). */
820static void fr_t391_cb(void *data)
821{
822 struct osmo_fr_link *link = data;
823
824 OSMO_ASSERT(link);
825
826 if (link->polling_count % link->net->n391 == 0)
827 tx_lmi_q933_status_enq(link, Q933_REPT_FULL_STATUS);
828 else
829 tx_lmi_q933_status_enq(link, Q933_REPT_LINK_INTEGRITY_VERIF);
830 link->polling_count++;
831 osmo_timer_schedule(&link->t391, osmo_tdef_get(link->net->T_defs, 391, OSMO_TDEF_S, 10), 0);
832}
833
834static void fr_t392_cb(void *data)
835{
836 struct osmo_fr_link *link = data;
837
838 OSMO_ASSERT(link);
839
840 /* A.5 The network increments the error count .. Non-receipt of
841 * a STATUS ENQUIRY within T392, which results in restarting
842 * T392 */
843 link->err_count++;
844 check_link_state(link, false);
845 osmo_timer_schedule(&link->t392, osmo_tdef_get(link->net->T_defs, 392, OSMO_TDEF_S, 15), 0);
846}
847
848/* allocate a frame relay network */
849struct osmo_fr_network *osmo_fr_network_alloc(void *ctx)
850{
851 struct osmo_fr_network *net = talloc_zero(ctx, struct osmo_fr_network);
852
853 INIT_LLIST_HEAD(&net->links);
854 net->T_defs = fr_tdefs;
855 osmo_tdefs_reset(net->T_defs);
856 net->n391 = 6;
857 net->n392 = 3;
858 net->n393 = 4;
859
860 return net;
861}
862
863void osmo_fr_network_free(struct osmo_fr_network *net)
864{
865 struct osmo_fr_link *link, *tmp;
866
867 if (!net)
868 return;
869
870 llist_for_each_entry_safe(link, tmp, &net->links, list) {
871 osmo_fr_link_free(link);
872 }
873}
874
875/* allocate a frame relay link in a given network */
876struct osmo_fr_link *osmo_fr_link_alloc(struct osmo_fr_network *net, enum osmo_fr_role role, const char *name)
877{
878 struct osmo_fr_link *link = talloc_zero(net, struct osmo_fr_link);
879 if (!link)
880 return NULL;
881
882 LOGPFRL(link, LOGL_INFO, "Creating frame relay link with role %s\n", osmo_fr_role_str(role));
883
884 link->role = role;
885 link->net = net;
886 link->name = talloc_strdup(link, name);
887 INIT_LLIST_HEAD(&link->dlc_list);
888 llist_add_tail(&link->list, &net->links);
889
890 osmo_timer_setup(&link->t391, fr_t391_cb, link);
891 osmo_timer_setup(&link->t392, fr_t392_cb, link);
892
893 switch (role) {
894 case FR_ROLE_USER_EQUIPMENT:
895 osmo_timer_schedule(&link->t391, osmo_tdef_get(link->net->T_defs, 391, OSMO_TDEF_S, 15), 0);
896 break;
897 case FR_ROLE_NETWORK_EQUIPMENT:
898 osmo_timer_schedule(&link->t392, osmo_tdef_get(link->net->T_defs, 392, OSMO_TDEF_S, 15), 0);
899 break;
900 }
901
902 return link;
903}
904
905void osmo_fr_link_free(struct osmo_fr_link *link)
906{
907 struct osmo_fr_dlc *dlc, *tmp;
908
909 if (!link)
910 return;
911
912 osmo_timer_del(&link->t391);
913 osmo_timer_del(&link->t392);
914
915 llist_for_each_entry_safe(dlc, tmp, &link->dlc_list, list) {
916 osmo_fr_dlc_free(dlc);
917 }
918
919 llist_del(&link->list);
920 talloc_free(link);
921}
922
923/* allocate a data link connectoin on a given framerelay link */
924struct osmo_fr_dlc *osmo_fr_dlc_alloc(struct osmo_fr_link *link, uint16_t dlci)
925{
926 struct osmo_fr_dlc *dlc = talloc_zero(link, struct osmo_fr_dlc);
927 if (!dlc)
928 return NULL;
929
930 dlc->link = link;
931 dlc->dlci = dlci;
932 dlc->active = false;
933
934 llist_add_tail(&dlc->list, &link->dlc_list);
935
936 dlc->add = true;
937 tx_lmi_q933_status(link, Q933_IEI_PVC_STATUS);
938
939 return dlc;
940}
941
942void osmo_fr_dlc_free(struct osmo_fr_dlc *dlc)
943{
944 llist_del(&dlc->list);
945 talloc_free(dlc);
946}
947
948/* TODO: rework osmo_fr_dlc_alloc/free with handling it's own memory.
949 * For network role: The dlc have to created by the application (e.g. vty).
950 * The dlc shouldn't free'd directly. It should be communicated to the
951 * other side and wait until it's confirmed OR the link go off and free it afterwards.
952 * For user equpment role: The dlc can be created by the application or the dlc will be created
953 * by the frame relay because the network is configuring the dlc.
954 * The dlc shouldn't be free'd. Only the handler should be set to NULL.
955 */
956
957struct osmo_fr_dlc *osmo_fr_dlc_by_dlci(struct osmo_fr_link *link, uint16_t dlci)
958{
959 struct osmo_fr_dlc *dlc;
960
961 llist_for_each_entry(dlc, &link->dlc_list, list) {
962 if (dlc->dlci == dlci)
963 return dlc;
964 }
965 return NULL;
966}