blob: 03b847aa3fe943c4da2187a5d61ddd58371d5722 [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;
Harald Welte23190142021-01-31 17:06:34 +0100433 if (link->role == FR_ROLE_USER_EQUIPMENT) {
434 /* make sure the next STATUS ENQUIRY is for a full
435 * status report to get the configred DLCs ASAP */
436 link->polling_count = 0;
Alexander Couzens841817e2020-11-19 00:41:29 +0100437 return;
Harald Welte23190142021-01-31 17:06:34 +0100438 }
Alexander Couzens841817e2020-11-19 00:41:29 +0100439
440 llist_for_each_entry(dlc, &link->dlc_list, list) {
441 if (!dlc->add && !dlc->del)
442 dlc->active = true;
443 }
444 }
445}
446
447static int validate_pvc_status(struct tlv_parsed *tp, size_t tp_len)
448{
449 size_t i;
450 uint16_t len = 0;
451
452 for (i = 0; i < tp_len; i++) {
453 if (!TLVP_PRESENT(&tp[i], Q933_IEI_PVC_STATUS))
454 continue;
455
456 /* PVC status can be 2 or 3 bytes. If the PVC is bigger
457 * ignore this to be compatible to future extensions. */
458 len = TLVP_LEN(&tp[i], Q933_IEI_PVC_STATUS);
459 if (len <= 1) {
460 return -EINVAL;
461 }
462 /* FIXME: validate correct flags: are some flags invalid at the same time? */
463 }
464
465 return 0;
466}
467
468static int parse_full_pvc_status(struct osmo_fr_link *link, struct tlv_parsed *tp, size_t tp_len)
469{
470 size_t i;
471 int err = 0;
472 struct osmo_fr_dlc *dlc, *tmp;
473 struct q933_a_pvc_sts *pvc;
474 uint16_t dlci = 0;
475 uint16_t *dlcis = talloc_zero_array(link, uint16_t, tp_len);
476 if (!dlcis)
477 return -ENOMEM;
478
479 /* first run validate all PVCs */
480 err = validate_pvc_status(tp, tp_len);
481 if (err < 0)
482 goto out;
483
484 for (i = 0; i < tp_len; i++) {
485 if (!TLVP_PRESENT(&tp[i], Q933_IEI_PVC_STATUS))
486 continue;
487
488 /* parse only 3 byte PVCs */
489 pvc = (struct q933_a_pvc_sts *) TLVP_VAL_MINLEN(
490 &tp[i],
491 Q933_IEI_PVC_STATUS,
492 sizeof(struct q933_a_pvc_sts));
493 if (!pvc)
494 continue;
495
496 dlci = ((pvc->dlci_msb & 0x3f) << 4) | (pvc->dlci_lsb & 0xf);
497 dlcis[i] = dlci;
498 dlc = osmo_fr_dlc_by_dlci(link, dlci);
499 if (!dlc) {
500 dlc = osmo_fr_dlc_alloc(link, dlci);
501 if (!dlc) {
502 LOGPFRL(link, LOGL_ERROR, "Could not create DLC %d\n", dlci);
503 continue;
504 }
505 }
506
507 /* Figure A.3/Q.933: The delete bit is only applicable for timely notification
508 * using the optional single PVC asynchronous status report.
509 * Ignoring the delete. */
510 dlc->add = pvc->new;
511 dlc->active = pvc->active;
512 dlc->del = 0;
513 }
514
515 /* check if all dlc are present in PVC Status */
516 llist_for_each_entry_safe(dlc, tmp, &link->dlc_list, list) {
517 bool found = false;
518 for (i = 0; i < tp_len; i++) {
519 if (dlcis[i] == dlc->dlci) {
520 found = true;
521 break;
522 }
523 }
524
525 if (!found) {
526 dlc->active = false;
527 dlc->del = true;
528 }
529 }
530
531 return 0;
532out:
533 talloc_free(dlcis);
534 return err;
535}
536
537static int parse_link_pvc_status(struct osmo_fr_link *link, struct tlv_parsed *tp, size_t tp_len)
538{
539 int err;
540 size_t i;
541 struct q933_a_pvc_sts *pvc;
542 struct osmo_fr_dlc *dlc;
543 uint16_t dlci = 0;
544
545 err = validate_pvc_status(tp, tp_len);
546 if (err < 0)
547 return err;
548
549 for (i = 0; i < tp_len; i++) {
550 if (!TLVP_PRESENT(&tp[i], Q933_IEI_PVC_STATUS))
551 continue;
552
553 /* parse only 3 byte PVCs */
554 pvc = (struct q933_a_pvc_sts *) TLVP_VAL_MINLEN(
555 &tp[i],
556 Q933_IEI_PVC_STATUS,
557 sizeof(struct q933_a_pvc_sts));
558 if (!pvc)
559 continue;
560
561 dlci = ((pvc->dlci_msb & 0x3f) << 4) | (pvc->dlci_lsb & 0xf);
562 dlc = osmo_fr_dlc_by_dlci(link, dlci);
563 if (!dlc) {
564 /* don't create dlc's for the ones which are about to be deleted. */
Harald Welte29b77a62020-11-26 09:28:49 +0100565 if (pvc->delete)
Alexander Couzens841817e2020-11-19 00:41:29 +0100566 continue;
567
568 dlc = osmo_fr_dlc_alloc(link, dlci);
569 if (!dlc) {
570 LOGPFRL(link, LOGL_ERROR, "Rx STATUS: Could not create DLC %d\n", dlci);
571 }
572 }
573
574 if (pvc->delete) {
575 dlc->del = 1;
576 } else {
577 dlc->add = pvc->new;
578 dlc->active = pvc->active;
579 dlc->del = 0;
580 }
581 }
582
583 return 0;
584}
585
586static size_t count_pvc_status(struct tlv_parsed *tp, size_t tp_len)
587{
588 size_t i, count = 0;
589 for (i = 0; i < tp_len; i++) {
590 if (!TLVP_PRESENT(&tp[i], Q933_IEI_PVC_STATUS))
591 continue;
592 count++;
593 }
594
595 return count;
596}
597
598static int rx_lmi_q933_status(struct msgb *msg, struct tlv_parsed *tp)
599{
600 struct osmo_fr_link *link = msg->dst;
601 const uint8_t *link_int_rx;
602 uint8_t rep_type;
603
604 OSMO_ASSERT(link);
605
606 if (link->role == FR_ROLE_NETWORK_EQUIPMENT) {
Harald Welte78ebc3f2020-11-25 17:39:06 +0100607 LOGPFRL(link, LOGL_ERROR, "Rx STATUS: STATUS aren't supported in role network\n");
Alexander Couzens841817e2020-11-19 00:41:29 +0100608 return -1;
609 }
610
611 /* check for mandatory IEs */
612 if (!TLVP_PRES_LEN(tp, Q933_IEI_REPORT_TYPE, 1)) {
613 LOGPFRL(link, LOGL_NOTICE, "Rx STATUSL: Missing TLV Q933 Report Type\n");
614 return -1;
615 }
616
617 rep_type = *TLVP_VAL(tp, Q933_IEI_REPORT_TYPE);
618
619 switch (rep_type) {
620 case Q933_REPT_FULL_STATUS:
621 case Q933_REPT_LINK_INTEGRITY_VERIF:
622 if (rep_type != link->expected_rep) {
623 LOGPFRL(link, LOGL_NOTICE, "Rx STATUS: Unexpected Q933 report type (got 0x%x != exp 0x%x)\n",
624 rep_type, link->expected_rep);
625 return -1;
626 }
627
628 if (!TLVP_PRES_LEN(tp, Q933_IEI_LINK_INT_VERIF, 2)) {
629 LOGPFRL(link, LOGL_NOTICE, "Rx STATUS: Missing TLV Q933 Link Integrety Verification\n");
630 return -1;
631 }
632 link_int_rx = TLVP_VAL(tp, Q933_IEI_LINK_INT_VERIF);
633 link->last_rx_seq = link_int_rx[0];
634 /* The received receive sequence number is not valid if
635 * it is not equal to the last transmitted send sequence
636 * number. Ignore messages containing this error. As a
637 * result, timer T391 expires and the user then
638 * increments the error count. */
639 if (link_int_rx[1] != link->last_tx_seq)
640 return 0;
641 break;
642 case Q933_REPT_SINGLE_PVC_ASYNC_STS:
643 default:
644 return -1;
645 }
646
647 check_link_state(link, true);
648 if (count_pvc_status(tp, MAX_SUPPORTED_PVC + 1) > MAX_SUPPORTED_PVC) {
649 LOGPFRL(link, LOGL_ERROR, "Rx STATUS: Too many PVC! Only %d are supported!\n", MAX_SUPPORTED_PVC);
650 }
651
652 switch (rep_type) {
653 case Q933_REPT_FULL_STATUS:
654 parse_full_pvc_status(link, tp, MAX_SUPPORTED_PVC);
655 break;
656 case Q933_REPT_LINK_INTEGRITY_VERIF:
657 parse_link_pvc_status(link, tp, MAX_SUPPORTED_PVC);
658 break;
659 default:
660 break;
661 }
662
663 /* The network responds to each STATUS ENQUIRY message with a
664 * STATUS message and resets the T392 timer */
665 osmo_timer_schedule(&link->t392, osmo_tdef_get(link->net->T_defs, 392, OSMO_TDEF_S, 15), 0);
666
667 return 0;
668}
669
670static int rx_lmi_q922(struct msgb *msg)
671{
672 struct osmo_fr_link *link = msg->dst;
673 struct q933_a_hdr *qh;
674 /* the + 1 is used to detect more than MAX_SUPPORTED_PVC */
675 struct tlv_parsed tp[MAX_SUPPORTED_PVC + 1];
676 uint8_t *lapf;
677 int rc;
678
679 OSMO_ASSERT(link);
680
681 if (msgb_l2len(msg) < 1)
682 return -1;
683 lapf = msgb_l2(msg);
684
685 /* we only support LAPF UI frames */
686 if (lapf[0] != LAPF_UI)
687 return -1;
688
689 msg->l3h = msg->l2h + 1;
690 if (msgb_l3len(msg) < 3)
691 return -1;
692
693 qh = (struct q933_a_hdr *) msgb_l3(msg);
694 if (qh->prot_disc != Q931_PDISC_CC) {
695 LOGPFRL(link, LOGL_NOTICE,
696 "Rx unsupported LMI protocol discriminator %u\n", qh->prot_disc);
697 return -1;
698 }
699
700 rc = tlv_parse2(tp, MAX_SUPPORTED_PVC + 1, &q933_att_tlvdef,
701 msgb_l3(msg) + sizeof(*qh),
702 msgb_l3len(msg) - sizeof(*qh), 0, 0);
703 if (rc < 0) {
704 LOGPFRL(link, LOGL_NOTICE,
705 "Failed to parse TLVs in LMI message type %u\n", qh->msg_type);
706 return rc;
707 }
708
709 switch (qh->msg_type) {
710 case Q931_MSGT_STATUS_ENQUIRY:
711 rc = rx_lmi_q933_status_enq(msg, tp);
712 break;
713 case Q931_MSGT_STATUS:
714 rc = rx_lmi_q933_status(msg, tp);
715 break;
716 default:
717 LOGPFRL(link, LOGL_NOTICE,
718 "Rx unsupported LMI message type %u\n", qh->msg_type);
719 rc = -1;
720 break;
721 }
722 msgb_free(msg);
723
724 return rc;
725}
726
727int osmo_fr_rx(struct msgb *msg)
728{
729 int rc = 0;
730 uint8_t *frh;
731 uint16_t dlci;
732 struct osmo_fr_dlc *dlc;
733 struct osmo_fr_link *link = msg->dst;
734
735 OSMO_ASSERT(link);
736
737 if (msgb_length(msg) < 2) {
738 LOGPFRL(link, LOGL_ERROR, "Rx short FR header: %u bytes\n", msgb_length(msg));
739 rc = -1;
740 goto out;
741 }
742
743 frh = msg->l1h = msgb_data(msg);
744 if (frh[0] & 0x01) {
745 LOGPFRL(link, LOGL_NOTICE, "Rx Unsupported single-byte FR address\n");
746 rc = -1;
747 goto out;
748 }
749 if ((frh[1] & 0x0f) != 0x01) {
750 LOGPFRL(link, LOGL_NOTICE, "Rx Unknown second FR octet 0x%02x\n", frh[1]);
751 rc = -1;
752 goto out;
753 }
754 dlci = q922_to_dlci(frh);
755 msg->l2h = frh + 2;
756
757 switch (dlci) {
758 case LMI_Q933A_DLCI:
759 return rx_lmi_q922(msg);
760 case LMI_CISCO_DLCI:
761 LOGPFRL(link, LOGL_ERROR, "Rx Unsupported FR DLCI %u\n", dlci);
762 goto out;
763 }
764
765 if (!link->state) {
766 LOGPFRL(link, LOGL_NOTICE, "Link is not reliable. Discarding Rx PDU on DLCI %d\n", dlci);
767 goto out;
768 }
769
770 llist_for_each_entry(dlc, &link->dlc_list, list) {
771 if (dlc->dlci == dlci) {
772 /* dispatch to handler of respective DLC */
773 msg->dst = dlc;
774 return dlc->rx_cb(dlc->rx_cb_data, msg);
775 }
776 }
777
778 if (link->unknown_dlc_rx_cb)
779 return link->unknown_dlc_rx_cb(link->unknown_dlc_rx_cb_data, msg);
780 else
781 LOGPFRL(link, LOGL_NOTICE, "DLCI %u doesn't exist, discarding\n", dlci);
782
783out:
784 msgb_free(msg);
785
786 return rc;
787}
788
789int osmo_fr_tx_dlc(struct msgb *msg)
790{
791 uint8_t *frh;
792 struct osmo_fr_dlc *dlc = msg->dst;
793 struct osmo_fr_link *link = dlc->link;
794
795 OSMO_ASSERT(dlc);
796 OSMO_ASSERT(link);
797
798 if (!link->state) {
799 LOGPFRL(link, LOGL_NOTICE, "Link is not reliable (yet), discarding Tx\n");
800 msgb_free(msg);
801 return -1;
802 }
803 if (!dlc->active) {
804 LOGPFRL(link, LOGL_NOTICE, "DLCI %u is not active (yet), discarding Tx\n", dlc->dlci);
805 msgb_free(msg);
806 return -1;
807 }
808 LOGPFRL(link, LOGL_DEBUG, "DLCI %u is active, sending message\n", dlc->dlci);
809
810 if (msgb_headroom(msg) < 2) {
811 msgb_free(msg);
812 return -ENOSPC;
813 }
814
815 frh = msgb_push(msg, 2);
816 dlci_to_q922(frh, dlc->dlci);
817
818 msg->dst = link;
819 return link->tx_cb(link->tx_cb_data, msg);
820}
821
822/* Every T391 seconds, the user equipment sends a STATUS ENQUIRY
823 * message to the network and resets its polling timer (T391). */
824static void fr_t391_cb(void *data)
825{
826 struct osmo_fr_link *link = data;
827
828 OSMO_ASSERT(link);
829
830 if (link->polling_count % link->net->n391 == 0)
831 tx_lmi_q933_status_enq(link, Q933_REPT_FULL_STATUS);
832 else
833 tx_lmi_q933_status_enq(link, Q933_REPT_LINK_INTEGRITY_VERIF);
834 link->polling_count++;
835 osmo_timer_schedule(&link->t391, osmo_tdef_get(link->net->T_defs, 391, OSMO_TDEF_S, 10), 0);
836}
837
838static void fr_t392_cb(void *data)
839{
840 struct osmo_fr_link *link = data;
841
842 OSMO_ASSERT(link);
843
844 /* A.5 The network increments the error count .. Non-receipt of
845 * a STATUS ENQUIRY within T392, which results in restarting
846 * T392 */
847 link->err_count++;
848 check_link_state(link, false);
849 osmo_timer_schedule(&link->t392, osmo_tdef_get(link->net->T_defs, 392, OSMO_TDEF_S, 15), 0);
850}
851
852/* allocate a frame relay network */
853struct osmo_fr_network *osmo_fr_network_alloc(void *ctx)
854{
855 struct osmo_fr_network *net = talloc_zero(ctx, struct osmo_fr_network);
Alexander Couzense249e362020-12-22 15:39:43 +0100856 if (!net)
857 return NULL;
Alexander Couzens841817e2020-11-19 00:41:29 +0100858
859 INIT_LLIST_HEAD(&net->links);
860 net->T_defs = fr_tdefs;
861 osmo_tdefs_reset(net->T_defs);
862 net->n391 = 6;
863 net->n392 = 3;
864 net->n393 = 4;
865
866 return net;
867}
868
869void osmo_fr_network_free(struct osmo_fr_network *net)
870{
871 struct osmo_fr_link *link, *tmp;
872
873 if (!net)
874 return;
875
876 llist_for_each_entry_safe(link, tmp, &net->links, list) {
877 osmo_fr_link_free(link);
878 }
879}
880
881/* allocate a frame relay link in a given network */
882struct osmo_fr_link *osmo_fr_link_alloc(struct osmo_fr_network *net, enum osmo_fr_role role, const char *name)
883{
884 struct osmo_fr_link *link = talloc_zero(net, struct osmo_fr_link);
885 if (!link)
886 return NULL;
Alexander Couzens841817e2020-11-19 00:41:29 +0100887 link->role = role;
888 link->net = net;
889 link->name = talloc_strdup(link, name);
890 INIT_LLIST_HEAD(&link->dlc_list);
891 llist_add_tail(&link->list, &net->links);
892
893 osmo_timer_setup(&link->t391, fr_t391_cb, link);
894 osmo_timer_setup(&link->t392, fr_t392_cb, link);
895
896 switch (role) {
897 case FR_ROLE_USER_EQUIPMENT:
898 osmo_timer_schedule(&link->t391, osmo_tdef_get(link->net->T_defs, 391, OSMO_TDEF_S, 15), 0);
899 break;
900 case FR_ROLE_NETWORK_EQUIPMENT:
901 osmo_timer_schedule(&link->t392, osmo_tdef_get(link->net->T_defs, 392, OSMO_TDEF_S, 15), 0);
902 break;
903 }
904
Alexander Couzensb6b62cd2020-12-22 15:40:34 +0100905 LOGPFRL(link, LOGL_INFO, "Creating frame relay link with role %s\n", osmo_fr_role_str(role));
906
Alexander Couzens841817e2020-11-19 00:41:29 +0100907 return link;
908}
909
910void osmo_fr_link_free(struct osmo_fr_link *link)
911{
912 struct osmo_fr_dlc *dlc, *tmp;
913
914 if (!link)
915 return;
916
917 osmo_timer_del(&link->t391);
918 osmo_timer_del(&link->t392);
919
920 llist_for_each_entry_safe(dlc, tmp, &link->dlc_list, list) {
921 osmo_fr_dlc_free(dlc);
922 }
923
924 llist_del(&link->list);
925 talloc_free(link);
926}
927
928/* allocate a data link connectoin on a given framerelay link */
929struct osmo_fr_dlc *osmo_fr_dlc_alloc(struct osmo_fr_link *link, uint16_t dlci)
930{
931 struct osmo_fr_dlc *dlc = talloc_zero(link, struct osmo_fr_dlc);
932 if (!dlc)
933 return NULL;
934
935 dlc->link = link;
936 dlc->dlci = dlci;
937 dlc->active = false;
938
939 llist_add_tail(&dlc->list, &link->dlc_list);
940
941 dlc->add = true;
942 tx_lmi_q933_status(link, Q933_IEI_PVC_STATUS);
943
944 return dlc;
945}
946
947void osmo_fr_dlc_free(struct osmo_fr_dlc *dlc)
948{
949 llist_del(&dlc->list);
950 talloc_free(dlc);
951}
952
953/* TODO: rework osmo_fr_dlc_alloc/free with handling it's own memory.
954 * For network role: The dlc have to created by the application (e.g. vty).
955 * The dlc shouldn't free'd directly. It should be communicated to the
956 * other side and wait until it's confirmed OR the link go off and free it afterwards.
957 * For user equpment role: The dlc can be created by the application or the dlc will be created
958 * by the frame relay because the network is configuring the dlc.
959 * The dlc shouldn't be free'd. Only the handler should be set to NULL.
960 */
961
962struct osmo_fr_dlc *osmo_fr_dlc_by_dlci(struct osmo_fr_link *link, uint16_t dlci)
963{
964 struct osmo_fr_dlc *dlc;
965
966 llist_for_each_entry(dlc, &link->dlc_list, list) {
967 if (dlc->dlci == dlci)
968 return dlc;
969 }
970 return NULL;
971}