blob: a7e1da8a3a0edcdfb5d6dc819c42255736534a3b [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
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100192static void dlc_set_active(struct osmo_fr_dlc *dlc, bool active)
193{
194 if (active == dlc->active)
195 return;
196
197 dlc->active = active;
198
199 LOGPFRL(dlc->link, LOGL_NOTICE, "DLCI %u became %s\n", dlc->dlci, active ? "active" : "inactive");
200 if (dlc->status_cb)
201 dlc->status_cb(dlc, dlc->cb_data, active);
202}
203
Alexander Couzens841817e2020-11-19 00:41:29 +0100204/* allocate a message buffer and put Q.933 Annex A headers (L2 + L3) */
205static struct msgb *q933_msgb_alloc(uint16_t dlci, uint8_t prot_disc, uint8_t msg_type)
206{
207 struct msgb *msg = msgb_alloc_headroom(1600+64, 64, "FR Q.933 Tx");
208 struct q933_a_hdr *qh;
209
210 if (!msg)
211 return NULL;
212
213 msg->l1h = msgb_put(msg, 2);
214 dlci_to_q922(msg->l1h, dlci);
215
216 /* LAPF UI control */
217 msg->l2h = msgb_put(msg, 1);
218 *msg->l2h = LAPF_UI;
219
220 msg->l3h = msgb_put(msg, sizeof(*qh));
221 qh = (struct q933_a_hdr *) msg->l3h;
222 qh->prot_disc = prot_disc;
223 qh->call_ref = LMI_Q933A_CALLREF;
224 qh->msg_type = msg_type;
225
226 return msg;
227}
228
229/* obtain the [next] transmit sequence number */
230static uint8_t link_get_tx_seq(struct osmo_fr_link *link)
231{
232 /* The {user equipment, network} increments the send sequence
233 * counter using modulo 256. The value zero is skipped. */
234 link->last_tx_seq++;
235 if (link->last_tx_seq == 0)
236 link->last_tx_seq++;
237
238 return link->last_tx_seq;
239}
240
241/* Append PVC Status IE according to Q.933 A.3.2 */
242static void msgb_put_link_int_verif(struct msgb *msg, struct osmo_fr_link *link)
243{
244 uint8_t link_int_tx[2];
245 link_int_tx[0] = link_get_tx_seq(link);
246 link_int_tx[1] = link->last_rx_seq;
247 msgb_tlv_put(msg, Q933_IEI_LINK_INT_VERIF, 2, link_int_tx);
248}
249
250static void dlc_destroy(struct osmo_fr_dlc *dlc)
251{
252 llist_del(&dlc->list);
253 talloc_free(dlc);
254}
255
256/* Append PVC Status IE according to Q.933 A.3.3 */
257static void msgb_put_pvc_status(struct msgb *msg, struct osmo_fr_dlc *dlc)
258{
259 uint8_t ie[3];
260
261 ie[0] = (dlc->dlci >> 4) & 0x3f;
262 /* extension bits */
263 ie[1] = 0x80 | ((dlc->dlci & 0xf) << 3);
264 /* extension bits */
265 ie[2] = 0x80;
266
267 /* FIXME: validate: this status should be added as long it's not yet acked by the remote */
268 if (dlc->active)
269 ie[2] |= Q933_PVC_STATUS_DLC_ACTIVE;
270
271 if (dlc->add) {
272 ie[2] |= Q933_PVC_STATUS_DLC_NEW;
273 /* we've reported it as new once, reset the status */
274 }
275
276 if (dlc->del) {
277 ie[2] |= Q933_PVC_STATUS_DLC_DELETE;
278 /* we've reported it as deleted once, destroy it */
279 dlc_destroy(dlc);
280 }
281
282 msgb_tlv_put(msg, Q933_IEI_PVC_STATUS, 3, ie);
283}
284
285/* Send a Q.933 STATUS ENQUIRY given type over given link */
286static int tx_lmi_q933_status_enq(struct osmo_fr_link *link, uint8_t rep_type)
287{
288 struct msgb *resp;
289
290 resp = q933_msgb_alloc(0, Q931_PDISC_CC, Q931_MSGT_STATUS_ENQUIRY);
291 if (!resp)
292 return -1;
293 resp->dst = link;
294 link->expected_rep = rep_type;
295
296 /* Table A.2/Q.933 */
297 msgb_tlv_put(resp, Q933_IEI_REPORT_TYPE, 1, &rep_type);
298 msgb_put_link_int_verif(resp, link);
299
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100300 return link->tx_cb(link->cb_data, resp);
Alexander Couzens841817e2020-11-19 00:41:29 +0100301}
302
303/* Send a Q.933 STATUS of given type over given link */
304static int tx_lmi_q933_status(struct osmo_fr_link *link, uint8_t rep_type)
305{
306 struct osmo_fr_dlc *dlc;
307 struct msgb *resp;
308
309 resp = q933_msgb_alloc(0, Q931_PDISC_CC, Q931_MSGT_STATUS);
310 if (!resp)
311 return -1;
312
313 resp->dst = link;
314
315 /* Table A.1/Q.933 */
316 msgb_tlv_put(resp, Q933_IEI_REPORT_TYPE, 1, &rep_type);
317 switch (rep_type) {
318 case Q933_REPT_FULL_STATUS:
319 msgb_put_link_int_verif(resp, link);
320 llist_for_each_entry(dlc, &link->dlc_list, list) {
321 if (dlc->add || dlc->del)
322 dlc->state_send = true;
323
324 msgb_put_pvc_status(resp, dlc);
325 }
326 break;
327 case Q933_REPT_LINK_INTEGRITY_VERIF:
328 msgb_put_link_int_verif(resp, link);
329 llist_for_each_entry(dlc, &link->dlc_list, list) {
330 if (dlc->add || dlc->del) {
331 msgb_put_pvc_status(resp, dlc);
332 dlc->state_send = true;
333 }
334 }
335 break;
336 case Q933_REPT_SINGLE_PVC_ASYNC_STS:
337 llist_for_each_entry(dlc, &link->dlc_list, list)
338 msgb_put_pvc_status(resp, dlc);
339 break;
340 }
341
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100342 return link->tx_cb(link->cb_data, resp);
Alexander Couzens841817e2020-11-19 00:41:29 +0100343}
344
345
346/* Q.933 */
347static int rx_lmi_q933_status_enq(struct msgb *msg, struct tlv_parsed *tp)
348{
349 struct osmo_fr_link *link = msg->dst;
350 struct osmo_fr_dlc *dlc;
351 const uint8_t *link_int_rx;
352 uint8_t rep_type;
353
354 OSMO_ASSERT(link);
355
356 if (link->role == FR_ROLE_USER_EQUIPMENT) {
Harald Welte78ebc3f2020-11-25 17:39:06 +0100357 LOGPFRL(link, LOGL_ERROR, "STATUS-ENQ aren't supported in role user\n");
Alexander Couzens841817e2020-11-19 00:41:29 +0100358 return -1;
359 }
360
361 /* check for mandatory IEs */
362 if (!TLVP_PRES_LEN(tp, Q933_IEI_REPORT_TYPE, 1) ||
363 !TLVP_PRES_LEN(tp, Q933_IEI_LINK_INT_VERIF, 2))
364 return -1;
365
366 rep_type = *TLVP_VAL(tp, Q933_IEI_REPORT_TYPE);
367
368 link_int_rx = TLVP_VAL(tp, Q933_IEI_LINK_INT_VERIF);
369 link->last_rx_seq = link_int_rx[0];
370
371 /* the network checks the receive sequence number received from
372 * the user equipment against its send sequence counter */
373 if (link_int_rx[1] != link->last_tx_seq) {
374 check_link_state(link, false);
375 link->err_count++;
376 } else {
377 check_link_state(link, true);
378 /* confirm DLC state changes */
379 llist_for_each_entry(dlc, &link->dlc_list, list) {
380 if (!dlc->state_send)
381 continue;
382
383 if (dlc->add) {
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100384 dlc_set_active(dlc, link->state);
Alexander Couzens841817e2020-11-19 00:41:29 +0100385 dlc->add = false;
386 }
387
388 if (dlc->del) {
389 dlc->del = false;
390 }
391
392 dlc->state_send = false;
393 }
394 }
395
396
397 /* The network responds to each STATUS ENQUIRY message with a
398 * STATUS message and resets the T392 timer */
399 osmo_timer_schedule(&link->t392, osmo_tdef_get(link->net->T_defs, 392, OSMO_TDEF_S, 15), 0);
400
401 return tx_lmi_q933_status(link, rep_type);
402}
403
404/* check if the link become active.
405 * The link becomes active when enough times a STATUS/STATUS ENQUIRY arrives without any loss.
406 * Look at the last N393 STATUS/STATUS ENQUIRY PDUs. The link is valid if at least N392
407 * got received.
408 * param[in] valid contains the status of the last packet */
409static void check_link_state(struct osmo_fr_link *link, bool valid)
410{
411 unsigned int last, i;
412 unsigned int carry = 0;
413 struct osmo_fr_dlc *dlc;
414
415 link->succeed <<= 1;
416 if (valid)
417 link->succeed |= 1;
418
419 /* count the bits */
420 last = link->succeed & ((1 << link->net->n393) - 1);
421 for (i = 0; i < link->net->n393; i++)
422 if (last & (1 << i))
423 carry++;
424
425 if (link->net->n393 - carry >= link->net->n392) {
426 /* failing link */
427 if (!link->state)
428 return;
429
430 LOGPFRL(link, LOGL_NOTICE, "Link failed\n");
431 link->state = false;
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100432 if (link->status_cb)
433 link->status_cb(link, link->cb_data, link->state);
Alexander Couzens841817e2020-11-19 00:41:29 +0100434
435 llist_for_each_entry(dlc, &link->dlc_list, list) {
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100436 dlc_set_active(dlc, false);
Alexander Couzens841817e2020-11-19 00:41:29 +0100437 }
438 } else {
439 /* good link */
440 if (link->state)
441 return;
442
443 LOGPFRL(link, LOGL_NOTICE, "Link recovered\n");
444 link->state = true;
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100445 if (link->status_cb)
446 link->status_cb(link, link->cb_data, link->state);
447
Harald Welte23190142021-01-31 17:06:34 +0100448 if (link->role == FR_ROLE_USER_EQUIPMENT) {
449 /* make sure the next STATUS ENQUIRY is for a full
450 * status report to get the configred DLCs ASAP */
451 link->polling_count = 0;
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100452 /* we must not proceed further below if we're in user role,
453 * as otherwise link recovery would set all DLCs as active */
Alexander Couzens841817e2020-11-19 00:41:29 +0100454 return;
Harald Welte23190142021-01-31 17:06:34 +0100455 }
Alexander Couzens841817e2020-11-19 00:41:29 +0100456
457 llist_for_each_entry(dlc, &link->dlc_list, list) {
458 if (!dlc->add && !dlc->del)
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100459 dlc_set_active(dlc, true);
Alexander Couzens841817e2020-11-19 00:41:29 +0100460 }
461 }
462}
463
464static int validate_pvc_status(struct tlv_parsed *tp, size_t tp_len)
465{
466 size_t i;
467 uint16_t len = 0;
468
469 for (i = 0; i < tp_len; i++) {
470 if (!TLVP_PRESENT(&tp[i], Q933_IEI_PVC_STATUS))
471 continue;
472
473 /* PVC status can be 2 or 3 bytes. If the PVC is bigger
474 * ignore this to be compatible to future extensions. */
475 len = TLVP_LEN(&tp[i], Q933_IEI_PVC_STATUS);
476 if (len <= 1) {
477 return -EINVAL;
478 }
479 /* FIXME: validate correct flags: are some flags invalid at the same time? */
480 }
481
482 return 0;
483}
484
485static int parse_full_pvc_status(struct osmo_fr_link *link, struct tlv_parsed *tp, size_t tp_len)
486{
487 size_t i;
488 int err = 0;
489 struct osmo_fr_dlc *dlc, *tmp;
490 struct q933_a_pvc_sts *pvc;
491 uint16_t dlci = 0;
492 uint16_t *dlcis = talloc_zero_array(link, uint16_t, tp_len);
493 if (!dlcis)
494 return -ENOMEM;
495
496 /* first run validate all PVCs */
497 err = validate_pvc_status(tp, tp_len);
498 if (err < 0)
499 goto out;
500
501 for (i = 0; i < tp_len; i++) {
502 if (!TLVP_PRESENT(&tp[i], Q933_IEI_PVC_STATUS))
503 continue;
504
505 /* parse only 3 byte PVCs */
506 pvc = (struct q933_a_pvc_sts *) TLVP_VAL_MINLEN(
507 &tp[i],
508 Q933_IEI_PVC_STATUS,
509 sizeof(struct q933_a_pvc_sts));
510 if (!pvc)
511 continue;
512
513 dlci = ((pvc->dlci_msb & 0x3f) << 4) | (pvc->dlci_lsb & 0xf);
514 dlcis[i] = dlci;
515 dlc = osmo_fr_dlc_by_dlci(link, dlci);
516 if (!dlc) {
517 dlc = osmo_fr_dlc_alloc(link, dlci);
518 if (!dlc) {
519 LOGPFRL(link, LOGL_ERROR, "Could not create DLC %d\n", dlci);
520 continue;
521 }
522 }
523
524 /* Figure A.3/Q.933: The delete bit is only applicable for timely notification
525 * using the optional single PVC asynchronous status report.
526 * Ignoring the delete. */
527 dlc->add = pvc->new;
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100528 dlc_set_active(dlc, pvc->active);
Alexander Couzens841817e2020-11-19 00:41:29 +0100529 dlc->del = 0;
530 }
531
532 /* check if all dlc are present in PVC Status */
533 llist_for_each_entry_safe(dlc, tmp, &link->dlc_list, list) {
534 bool found = false;
535 for (i = 0; i < tp_len; i++) {
536 if (dlcis[i] == dlc->dlci) {
537 found = true;
538 break;
539 }
540 }
541
542 if (!found) {
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100543 dlc_set_active(dlc, false);
Alexander Couzens841817e2020-11-19 00:41:29 +0100544 dlc->del = true;
545 }
546 }
547
548 return 0;
549out:
550 talloc_free(dlcis);
551 return err;
552}
553
554static int parse_link_pvc_status(struct osmo_fr_link *link, struct tlv_parsed *tp, size_t tp_len)
555{
556 int err;
557 size_t i;
558 struct q933_a_pvc_sts *pvc;
559 struct osmo_fr_dlc *dlc;
560 uint16_t dlci = 0;
561
562 err = validate_pvc_status(tp, tp_len);
563 if (err < 0)
564 return err;
565
566 for (i = 0; i < tp_len; i++) {
567 if (!TLVP_PRESENT(&tp[i], Q933_IEI_PVC_STATUS))
568 continue;
569
570 /* parse only 3 byte PVCs */
571 pvc = (struct q933_a_pvc_sts *) TLVP_VAL_MINLEN(
572 &tp[i],
573 Q933_IEI_PVC_STATUS,
574 sizeof(struct q933_a_pvc_sts));
575 if (!pvc)
576 continue;
577
578 dlci = ((pvc->dlci_msb & 0x3f) << 4) | (pvc->dlci_lsb & 0xf);
579 dlc = osmo_fr_dlc_by_dlci(link, dlci);
580 if (!dlc) {
581 /* don't create dlc's for the ones which are about to be deleted. */
Harald Welte29b77a62020-11-26 09:28:49 +0100582 if (pvc->delete)
Alexander Couzens841817e2020-11-19 00:41:29 +0100583 continue;
584
585 dlc = osmo_fr_dlc_alloc(link, dlci);
586 if (!dlc) {
587 LOGPFRL(link, LOGL_ERROR, "Rx STATUS: Could not create DLC %d\n", dlci);
588 }
589 }
590
591 if (pvc->delete) {
592 dlc->del = 1;
593 } else {
594 dlc->add = pvc->new;
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100595 dlc_set_active(dlc, pvc->active);
Alexander Couzens841817e2020-11-19 00:41:29 +0100596 dlc->del = 0;
597 }
598 }
599
600 return 0;
601}
602
603static size_t count_pvc_status(struct tlv_parsed *tp, size_t tp_len)
604{
605 size_t i, count = 0;
606 for (i = 0; i < tp_len; i++) {
607 if (!TLVP_PRESENT(&tp[i], Q933_IEI_PVC_STATUS))
608 continue;
609 count++;
610 }
611
612 return count;
613}
614
615static int rx_lmi_q933_status(struct msgb *msg, struct tlv_parsed *tp)
616{
617 struct osmo_fr_link *link = msg->dst;
618 const uint8_t *link_int_rx;
619 uint8_t rep_type;
620
621 OSMO_ASSERT(link);
622
623 if (link->role == FR_ROLE_NETWORK_EQUIPMENT) {
Harald Welte78ebc3f2020-11-25 17:39:06 +0100624 LOGPFRL(link, LOGL_ERROR, "Rx STATUS: STATUS aren't supported in role network\n");
Alexander Couzens841817e2020-11-19 00:41:29 +0100625 return -1;
626 }
627
628 /* check for mandatory IEs */
629 if (!TLVP_PRES_LEN(tp, Q933_IEI_REPORT_TYPE, 1)) {
630 LOGPFRL(link, LOGL_NOTICE, "Rx STATUSL: Missing TLV Q933 Report Type\n");
631 return -1;
632 }
633
634 rep_type = *TLVP_VAL(tp, Q933_IEI_REPORT_TYPE);
635
636 switch (rep_type) {
637 case Q933_REPT_FULL_STATUS:
638 case Q933_REPT_LINK_INTEGRITY_VERIF:
639 if (rep_type != link->expected_rep) {
640 LOGPFRL(link, LOGL_NOTICE, "Rx STATUS: Unexpected Q933 report type (got 0x%x != exp 0x%x)\n",
641 rep_type, link->expected_rep);
642 return -1;
643 }
644
645 if (!TLVP_PRES_LEN(tp, Q933_IEI_LINK_INT_VERIF, 2)) {
646 LOGPFRL(link, LOGL_NOTICE, "Rx STATUS: Missing TLV Q933 Link Integrety Verification\n");
647 return -1;
648 }
649 link_int_rx = TLVP_VAL(tp, Q933_IEI_LINK_INT_VERIF);
650 link->last_rx_seq = link_int_rx[0];
651 /* The received receive sequence number is not valid if
652 * it is not equal to the last transmitted send sequence
653 * number. Ignore messages containing this error. As a
654 * result, timer T391 expires and the user then
655 * increments the error count. */
656 if (link_int_rx[1] != link->last_tx_seq)
657 return 0;
658 break;
659 case Q933_REPT_SINGLE_PVC_ASYNC_STS:
660 default:
661 return -1;
662 }
663
664 check_link_state(link, true);
665 if (count_pvc_status(tp, MAX_SUPPORTED_PVC + 1) > MAX_SUPPORTED_PVC) {
666 LOGPFRL(link, LOGL_ERROR, "Rx STATUS: Too many PVC! Only %d are supported!\n", MAX_SUPPORTED_PVC);
667 }
668
669 switch (rep_type) {
670 case Q933_REPT_FULL_STATUS:
671 parse_full_pvc_status(link, tp, MAX_SUPPORTED_PVC);
672 break;
673 case Q933_REPT_LINK_INTEGRITY_VERIF:
674 parse_link_pvc_status(link, tp, MAX_SUPPORTED_PVC);
675 break;
676 default:
677 break;
678 }
679
680 /* The network responds to each STATUS ENQUIRY message with a
681 * STATUS message and resets the T392 timer */
682 osmo_timer_schedule(&link->t392, osmo_tdef_get(link->net->T_defs, 392, OSMO_TDEF_S, 15), 0);
683
684 return 0;
685}
686
687static int rx_lmi_q922(struct msgb *msg)
688{
689 struct osmo_fr_link *link = msg->dst;
690 struct q933_a_hdr *qh;
691 /* the + 1 is used to detect more than MAX_SUPPORTED_PVC */
692 struct tlv_parsed tp[MAX_SUPPORTED_PVC + 1];
693 uint8_t *lapf;
694 int rc;
695
696 OSMO_ASSERT(link);
697
698 if (msgb_l2len(msg) < 1)
699 return -1;
700 lapf = msgb_l2(msg);
701
702 /* we only support LAPF UI frames */
703 if (lapf[0] != LAPF_UI)
704 return -1;
705
706 msg->l3h = msg->l2h + 1;
707 if (msgb_l3len(msg) < 3)
708 return -1;
709
710 qh = (struct q933_a_hdr *) msgb_l3(msg);
711 if (qh->prot_disc != Q931_PDISC_CC) {
712 LOGPFRL(link, LOGL_NOTICE,
713 "Rx unsupported LMI protocol discriminator %u\n", qh->prot_disc);
714 return -1;
715 }
716
717 rc = tlv_parse2(tp, MAX_SUPPORTED_PVC + 1, &q933_att_tlvdef,
718 msgb_l3(msg) + sizeof(*qh),
719 msgb_l3len(msg) - sizeof(*qh), 0, 0);
720 if (rc < 0) {
721 LOGPFRL(link, LOGL_NOTICE,
722 "Failed to parse TLVs in LMI message type %u\n", qh->msg_type);
723 return rc;
724 }
725
726 switch (qh->msg_type) {
727 case Q931_MSGT_STATUS_ENQUIRY:
728 rc = rx_lmi_q933_status_enq(msg, tp);
729 break;
730 case Q931_MSGT_STATUS:
731 rc = rx_lmi_q933_status(msg, tp);
732 break;
733 default:
734 LOGPFRL(link, LOGL_NOTICE,
735 "Rx unsupported LMI message type %u\n", qh->msg_type);
736 rc = -1;
737 break;
738 }
739 msgb_free(msg);
740
741 return rc;
742}
743
744int osmo_fr_rx(struct msgb *msg)
745{
746 int rc = 0;
747 uint8_t *frh;
748 uint16_t dlci;
749 struct osmo_fr_dlc *dlc;
750 struct osmo_fr_link *link = msg->dst;
751
752 OSMO_ASSERT(link);
753
754 if (msgb_length(msg) < 2) {
755 LOGPFRL(link, LOGL_ERROR, "Rx short FR header: %u bytes\n", msgb_length(msg));
756 rc = -1;
757 goto out;
758 }
759
760 frh = msg->l1h = msgb_data(msg);
761 if (frh[0] & 0x01) {
762 LOGPFRL(link, LOGL_NOTICE, "Rx Unsupported single-byte FR address\n");
763 rc = -1;
764 goto out;
765 }
766 if ((frh[1] & 0x0f) != 0x01) {
767 LOGPFRL(link, LOGL_NOTICE, "Rx Unknown second FR octet 0x%02x\n", frh[1]);
768 rc = -1;
769 goto out;
770 }
771 dlci = q922_to_dlci(frh);
772 msg->l2h = frh + 2;
773
774 switch (dlci) {
775 case LMI_Q933A_DLCI:
776 return rx_lmi_q922(msg);
777 case LMI_CISCO_DLCI:
778 LOGPFRL(link, LOGL_ERROR, "Rx Unsupported FR DLCI %u\n", dlci);
779 goto out;
780 }
781
782 if (!link->state) {
783 LOGPFRL(link, LOGL_NOTICE, "Link is not reliable. Discarding Rx PDU on DLCI %d\n", dlci);
784 goto out;
785 }
786
787 llist_for_each_entry(dlc, &link->dlc_list, list) {
788 if (dlc->dlci == dlci) {
789 /* dispatch to handler of respective DLC */
790 msg->dst = dlc;
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100791 return dlc->rx_cb(dlc->cb_data, msg);
Alexander Couzens841817e2020-11-19 00:41:29 +0100792 }
793 }
794
795 if (link->unknown_dlc_rx_cb)
796 return link->unknown_dlc_rx_cb(link->unknown_dlc_rx_cb_data, msg);
797 else
798 LOGPFRL(link, LOGL_NOTICE, "DLCI %u doesn't exist, discarding\n", dlci);
799
800out:
801 msgb_free(msg);
802
803 return rc;
804}
805
806int osmo_fr_tx_dlc(struct msgb *msg)
807{
808 uint8_t *frh;
809 struct osmo_fr_dlc *dlc = msg->dst;
810 struct osmo_fr_link *link = dlc->link;
811
812 OSMO_ASSERT(dlc);
813 OSMO_ASSERT(link);
814
815 if (!link->state) {
816 LOGPFRL(link, LOGL_NOTICE, "Link is not reliable (yet), discarding Tx\n");
817 msgb_free(msg);
818 return -1;
819 }
820 if (!dlc->active) {
821 LOGPFRL(link, LOGL_NOTICE, "DLCI %u is not active (yet), discarding Tx\n", dlc->dlci);
822 msgb_free(msg);
823 return -1;
824 }
825 LOGPFRL(link, LOGL_DEBUG, "DLCI %u is active, sending message\n", dlc->dlci);
826
827 if (msgb_headroom(msg) < 2) {
828 msgb_free(msg);
829 return -ENOSPC;
830 }
831
832 frh = msgb_push(msg, 2);
833 dlci_to_q922(frh, dlc->dlci);
834
835 msg->dst = link;
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100836 return link->tx_cb(link->cb_data, msg);
Alexander Couzens841817e2020-11-19 00:41:29 +0100837}
838
839/* Every T391 seconds, the user equipment sends a STATUS ENQUIRY
840 * message to the network and resets its polling timer (T391). */
841static void fr_t391_cb(void *data)
842{
843 struct osmo_fr_link *link = data;
844
845 OSMO_ASSERT(link);
846
847 if (link->polling_count % link->net->n391 == 0)
848 tx_lmi_q933_status_enq(link, Q933_REPT_FULL_STATUS);
849 else
850 tx_lmi_q933_status_enq(link, Q933_REPT_LINK_INTEGRITY_VERIF);
851 link->polling_count++;
852 osmo_timer_schedule(&link->t391, osmo_tdef_get(link->net->T_defs, 391, OSMO_TDEF_S, 10), 0);
853}
854
855static void fr_t392_cb(void *data)
856{
857 struct osmo_fr_link *link = data;
858
859 OSMO_ASSERT(link);
860
861 /* A.5 The network increments the error count .. Non-receipt of
862 * a STATUS ENQUIRY within T392, which results in restarting
863 * T392 */
864 link->err_count++;
865 check_link_state(link, false);
866 osmo_timer_schedule(&link->t392, osmo_tdef_get(link->net->T_defs, 392, OSMO_TDEF_S, 15), 0);
867}
868
869/* allocate a frame relay network */
870struct osmo_fr_network *osmo_fr_network_alloc(void *ctx)
871{
872 struct osmo_fr_network *net = talloc_zero(ctx, struct osmo_fr_network);
Alexander Couzense249e362020-12-22 15:39:43 +0100873 if (!net)
874 return NULL;
Alexander Couzens841817e2020-11-19 00:41:29 +0100875
876 INIT_LLIST_HEAD(&net->links);
877 net->T_defs = fr_tdefs;
878 osmo_tdefs_reset(net->T_defs);
879 net->n391 = 6;
880 net->n392 = 3;
881 net->n393 = 4;
882
883 return net;
884}
885
886void osmo_fr_network_free(struct osmo_fr_network *net)
887{
888 struct osmo_fr_link *link, *tmp;
889
890 if (!net)
891 return;
892
893 llist_for_each_entry_safe(link, tmp, &net->links, list) {
894 osmo_fr_link_free(link);
895 }
896}
897
898/* allocate a frame relay link in a given network */
899struct osmo_fr_link *osmo_fr_link_alloc(struct osmo_fr_network *net, enum osmo_fr_role role, const char *name)
900{
901 struct osmo_fr_link *link = talloc_zero(net, struct osmo_fr_link);
902 if (!link)
903 return NULL;
Alexander Couzens841817e2020-11-19 00:41:29 +0100904 link->role = role;
905 link->net = net;
906 link->name = talloc_strdup(link, name);
907 INIT_LLIST_HEAD(&link->dlc_list);
908 llist_add_tail(&link->list, &net->links);
909
910 osmo_timer_setup(&link->t391, fr_t391_cb, link);
911 osmo_timer_setup(&link->t392, fr_t392_cb, link);
912
913 switch (role) {
914 case FR_ROLE_USER_EQUIPMENT:
915 osmo_timer_schedule(&link->t391, osmo_tdef_get(link->net->T_defs, 391, OSMO_TDEF_S, 15), 0);
916 break;
917 case FR_ROLE_NETWORK_EQUIPMENT:
918 osmo_timer_schedule(&link->t392, osmo_tdef_get(link->net->T_defs, 392, OSMO_TDEF_S, 15), 0);
919 break;
920 }
921
Alexander Couzensb6b62cd2020-12-22 15:40:34 +0100922 LOGPFRL(link, LOGL_INFO, "Creating frame relay link with role %s\n", osmo_fr_role_str(role));
923
Alexander Couzens841817e2020-11-19 00:41:29 +0100924 return link;
925}
926
927void osmo_fr_link_free(struct osmo_fr_link *link)
928{
929 struct osmo_fr_dlc *dlc, *tmp;
930
931 if (!link)
932 return;
933
934 osmo_timer_del(&link->t391);
935 osmo_timer_del(&link->t392);
936
937 llist_for_each_entry_safe(dlc, tmp, &link->dlc_list, list) {
938 osmo_fr_dlc_free(dlc);
939 }
940
941 llist_del(&link->list);
942 talloc_free(link);
943}
944
945/* allocate a data link connectoin on a given framerelay link */
946struct osmo_fr_dlc *osmo_fr_dlc_alloc(struct osmo_fr_link *link, uint16_t dlci)
947{
948 struct osmo_fr_dlc *dlc = talloc_zero(link, struct osmo_fr_dlc);
949 if (!dlc)
950 return NULL;
951
952 dlc->link = link;
953 dlc->dlci = dlci;
954 dlc->active = false;
955
956 llist_add_tail(&dlc->list, &link->dlc_list);
957
958 dlc->add = true;
959 tx_lmi_q933_status(link, Q933_IEI_PVC_STATUS);
960
961 return dlc;
962}
963
964void osmo_fr_dlc_free(struct osmo_fr_dlc *dlc)
965{
966 llist_del(&dlc->list);
967 talloc_free(dlc);
968}
969
970/* TODO: rework osmo_fr_dlc_alloc/free with handling it's own memory.
971 * For network role: The dlc have to created by the application (e.g. vty).
972 * The dlc shouldn't free'd directly. It should be communicated to the
973 * other side and wait until it's confirmed OR the link go off and free it afterwards.
974 * For user equpment role: The dlc can be created by the application or the dlc will be created
975 * by the frame relay because the network is configuring the dlc.
976 * The dlc shouldn't be free'd. Only the handler should be set to NULL.
977 */
978
979struct osmo_fr_dlc *osmo_fr_dlc_by_dlci(struct osmo_fr_link *link, uint16_t dlci)
980{
981 struct osmo_fr_dlc *dlc;
982
983 llist_for_each_entry(dlc, &link->dlc_list, list) {
984 if (dlc->dlci == dlci)
985 return dlc;
986 }
987 return NULL;
988}