blob: 679a3151e546765053b0e68cc1c2b845e67133b1 [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
Harald Weltecfe0ed62021-02-04 19:23:03 +0100346static void link_set_failed(struct osmo_fr_link *link)
347{
348 struct osmo_fr_dlc *dlc;
349
350 LOGPFRL(link, LOGL_NOTICE, "Link failed\n");
351 link->state = false;
352 if (link->status_cb)
353 link->status_cb(link, link->cb_data, link->state);
354
355 llist_for_each_entry(dlc, &link->dlc_list, list) {
356 dlc_set_active(dlc, false);
357 }
358}
359
Alexander Couzens841817e2020-11-19 00:41:29 +0100360/* Q.933 */
361static int rx_lmi_q933_status_enq(struct msgb *msg, struct tlv_parsed *tp)
362{
363 struct osmo_fr_link *link = msg->dst;
364 struct osmo_fr_dlc *dlc;
365 const uint8_t *link_int_rx;
366 uint8_t rep_type;
367
368 OSMO_ASSERT(link);
369
370 if (link->role == FR_ROLE_USER_EQUIPMENT) {
Harald Welte78ebc3f2020-11-25 17:39:06 +0100371 LOGPFRL(link, LOGL_ERROR, "STATUS-ENQ aren't supported in role user\n");
Alexander Couzens841817e2020-11-19 00:41:29 +0100372 return -1;
373 }
374
375 /* check for mandatory IEs */
376 if (!TLVP_PRES_LEN(tp, Q933_IEI_REPORT_TYPE, 1) ||
377 !TLVP_PRES_LEN(tp, Q933_IEI_LINK_INT_VERIF, 2))
378 return -1;
379
380 rep_type = *TLVP_VAL(tp, Q933_IEI_REPORT_TYPE);
381
382 link_int_rx = TLVP_VAL(tp, Q933_IEI_LINK_INT_VERIF);
383 link->last_rx_seq = link_int_rx[0];
384
385 /* the network checks the receive sequence number received from
386 * the user equipment against its send sequence counter */
387 if (link_int_rx[1] != link->last_tx_seq) {
388 check_link_state(link, false);
389 link->err_count++;
390 } else {
391 check_link_state(link, true);
392 /* confirm DLC state changes */
393 llist_for_each_entry(dlc, &link->dlc_list, list) {
394 if (!dlc->state_send)
395 continue;
396
397 if (dlc->add) {
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100398 dlc_set_active(dlc, link->state);
Alexander Couzens841817e2020-11-19 00:41:29 +0100399 dlc->add = false;
400 }
401
402 if (dlc->del) {
403 dlc->del = false;
404 }
405
406 dlc->state_send = false;
407 }
408 }
409
410
411 /* The network responds to each STATUS ENQUIRY message with a
412 * STATUS message and resets the T392 timer */
413 osmo_timer_schedule(&link->t392, osmo_tdef_get(link->net->T_defs, 392, OSMO_TDEF_S, 15), 0);
414
415 return tx_lmi_q933_status(link, rep_type);
416}
417
418/* check if the link become active.
419 * The link becomes active when enough times a STATUS/STATUS ENQUIRY arrives without any loss.
420 * Look at the last N393 STATUS/STATUS ENQUIRY PDUs. The link is valid if at least N392
421 * got received.
422 * param[in] valid contains the status of the last packet */
423static void check_link_state(struct osmo_fr_link *link, bool valid)
424{
425 unsigned int last, i;
426 unsigned int carry = 0;
427 struct osmo_fr_dlc *dlc;
428
429 link->succeed <<= 1;
430 if (valid)
431 link->succeed |= 1;
432
433 /* count the bits */
434 last = link->succeed & ((1 << link->net->n393) - 1);
435 for (i = 0; i < link->net->n393; i++)
436 if (last & (1 << i))
437 carry++;
438
439 if (link->net->n393 - carry >= link->net->n392) {
440 /* failing link */
441 if (!link->state)
442 return;
443
Harald Weltecfe0ed62021-02-04 19:23:03 +0100444 link_set_failed(link);
Alexander Couzens841817e2020-11-19 00:41:29 +0100445 } else {
446 /* good link */
447 if (link->state)
448 return;
449
450 LOGPFRL(link, LOGL_NOTICE, "Link recovered\n");
451 link->state = true;
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100452 if (link->status_cb)
453 link->status_cb(link, link->cb_data, link->state);
454
Harald Welte23190142021-01-31 17:06:34 +0100455 if (link->role == FR_ROLE_USER_EQUIPMENT) {
456 /* make sure the next STATUS ENQUIRY is for a full
457 * status report to get the configred DLCs ASAP */
458 link->polling_count = 0;
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100459 /* we must not proceed further below if we're in user role,
460 * as otherwise link recovery would set all DLCs as active */
Alexander Couzens841817e2020-11-19 00:41:29 +0100461 return;
Harald Welte23190142021-01-31 17:06:34 +0100462 }
Alexander Couzens841817e2020-11-19 00:41:29 +0100463
464 llist_for_each_entry(dlc, &link->dlc_list, list) {
465 if (!dlc->add && !dlc->del)
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100466 dlc_set_active(dlc, true);
Alexander Couzens841817e2020-11-19 00:41:29 +0100467 }
468 }
469}
470
471static int validate_pvc_status(struct tlv_parsed *tp, size_t tp_len)
472{
473 size_t i;
474 uint16_t len = 0;
475
476 for (i = 0; i < tp_len; i++) {
477 if (!TLVP_PRESENT(&tp[i], Q933_IEI_PVC_STATUS))
478 continue;
479
480 /* PVC status can be 2 or 3 bytes. If the PVC is bigger
481 * ignore this to be compatible to future extensions. */
482 len = TLVP_LEN(&tp[i], Q933_IEI_PVC_STATUS);
483 if (len <= 1) {
484 return -EINVAL;
485 }
486 /* FIXME: validate correct flags: are some flags invalid at the same time? */
487 }
488
489 return 0;
490}
491
492static int parse_full_pvc_status(struct osmo_fr_link *link, struct tlv_parsed *tp, size_t tp_len)
493{
494 size_t i;
495 int err = 0;
496 struct osmo_fr_dlc *dlc, *tmp;
497 struct q933_a_pvc_sts *pvc;
498 uint16_t dlci = 0;
499 uint16_t *dlcis = talloc_zero_array(link, uint16_t, tp_len);
500 if (!dlcis)
501 return -ENOMEM;
502
503 /* first run validate all PVCs */
504 err = validate_pvc_status(tp, tp_len);
505 if (err < 0)
506 goto out;
507
508 for (i = 0; i < tp_len; i++) {
509 if (!TLVP_PRESENT(&tp[i], Q933_IEI_PVC_STATUS))
510 continue;
511
512 /* parse only 3 byte PVCs */
513 pvc = (struct q933_a_pvc_sts *) TLVP_VAL_MINLEN(
514 &tp[i],
515 Q933_IEI_PVC_STATUS,
516 sizeof(struct q933_a_pvc_sts));
517 if (!pvc)
518 continue;
519
520 dlci = ((pvc->dlci_msb & 0x3f) << 4) | (pvc->dlci_lsb & 0xf);
521 dlcis[i] = dlci;
522 dlc = osmo_fr_dlc_by_dlci(link, dlci);
523 if (!dlc) {
524 dlc = osmo_fr_dlc_alloc(link, dlci);
525 if (!dlc) {
526 LOGPFRL(link, LOGL_ERROR, "Could not create DLC %d\n", dlci);
527 continue;
528 }
529 }
530
531 /* Figure A.3/Q.933: The delete bit is only applicable for timely notification
532 * using the optional single PVC asynchronous status report.
533 * Ignoring the delete. */
534 dlc->add = pvc->new;
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100535 dlc_set_active(dlc, pvc->active);
Alexander Couzens841817e2020-11-19 00:41:29 +0100536 dlc->del = 0;
537 }
538
539 /* check if all dlc are present in PVC Status */
540 llist_for_each_entry_safe(dlc, tmp, &link->dlc_list, list) {
541 bool found = false;
542 for (i = 0; i < tp_len; i++) {
543 if (dlcis[i] == dlc->dlci) {
544 found = true;
545 break;
546 }
547 }
548
549 if (!found) {
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100550 dlc_set_active(dlc, false);
Alexander Couzens841817e2020-11-19 00:41:29 +0100551 dlc->del = true;
552 }
553 }
554
555 return 0;
556out:
557 talloc_free(dlcis);
558 return err;
559}
560
561static int parse_link_pvc_status(struct osmo_fr_link *link, struct tlv_parsed *tp, size_t tp_len)
562{
563 int err;
564 size_t i;
565 struct q933_a_pvc_sts *pvc;
566 struct osmo_fr_dlc *dlc;
567 uint16_t dlci = 0;
568
569 err = validate_pvc_status(tp, tp_len);
570 if (err < 0)
571 return err;
572
573 for (i = 0; i < tp_len; i++) {
574 if (!TLVP_PRESENT(&tp[i], Q933_IEI_PVC_STATUS))
575 continue;
576
577 /* parse only 3 byte PVCs */
578 pvc = (struct q933_a_pvc_sts *) TLVP_VAL_MINLEN(
579 &tp[i],
580 Q933_IEI_PVC_STATUS,
581 sizeof(struct q933_a_pvc_sts));
582 if (!pvc)
583 continue;
584
585 dlci = ((pvc->dlci_msb & 0x3f) << 4) | (pvc->dlci_lsb & 0xf);
586 dlc = osmo_fr_dlc_by_dlci(link, dlci);
587 if (!dlc) {
588 /* don't create dlc's for the ones which are about to be deleted. */
Harald Welte29b77a62020-11-26 09:28:49 +0100589 if (pvc->delete)
Alexander Couzens841817e2020-11-19 00:41:29 +0100590 continue;
591
592 dlc = osmo_fr_dlc_alloc(link, dlci);
593 if (!dlc) {
594 LOGPFRL(link, LOGL_ERROR, "Rx STATUS: Could not create DLC %d\n", dlci);
Alexander Couzensca3550a2021-02-03 11:35:48 +0100595 continue;
Alexander Couzens841817e2020-11-19 00:41:29 +0100596 }
597 }
598
599 if (pvc->delete) {
600 dlc->del = 1;
601 } else {
602 dlc->add = pvc->new;
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100603 dlc_set_active(dlc, pvc->active);
Alexander Couzens841817e2020-11-19 00:41:29 +0100604 dlc->del = 0;
605 }
606 }
607
608 return 0;
609}
610
611static size_t count_pvc_status(struct tlv_parsed *tp, size_t tp_len)
612{
613 size_t i, count = 0;
614 for (i = 0; i < tp_len; i++) {
615 if (!TLVP_PRESENT(&tp[i], Q933_IEI_PVC_STATUS))
616 continue;
617 count++;
618 }
619
620 return count;
621}
622
623static int rx_lmi_q933_status(struct msgb *msg, struct tlv_parsed *tp)
624{
625 struct osmo_fr_link *link = msg->dst;
626 const uint8_t *link_int_rx;
627 uint8_t rep_type;
628
629 OSMO_ASSERT(link);
630
631 if (link->role == FR_ROLE_NETWORK_EQUIPMENT) {
Harald Welte78ebc3f2020-11-25 17:39:06 +0100632 LOGPFRL(link, LOGL_ERROR, "Rx STATUS: STATUS aren't supported in role network\n");
Alexander Couzens841817e2020-11-19 00:41:29 +0100633 return -1;
634 }
635
636 /* check for mandatory IEs */
637 if (!TLVP_PRES_LEN(tp, Q933_IEI_REPORT_TYPE, 1)) {
638 LOGPFRL(link, LOGL_NOTICE, "Rx STATUSL: Missing TLV Q933 Report Type\n");
639 return -1;
640 }
641
642 rep_type = *TLVP_VAL(tp, Q933_IEI_REPORT_TYPE);
643
644 switch (rep_type) {
645 case Q933_REPT_FULL_STATUS:
646 case Q933_REPT_LINK_INTEGRITY_VERIF:
647 if (rep_type != link->expected_rep) {
648 LOGPFRL(link, LOGL_NOTICE, "Rx STATUS: Unexpected Q933 report type (got 0x%x != exp 0x%x)\n",
649 rep_type, link->expected_rep);
650 return -1;
651 }
652
653 if (!TLVP_PRES_LEN(tp, Q933_IEI_LINK_INT_VERIF, 2)) {
654 LOGPFRL(link, LOGL_NOTICE, "Rx STATUS: Missing TLV Q933 Link Integrety Verification\n");
655 return -1;
656 }
657 link_int_rx = TLVP_VAL(tp, Q933_IEI_LINK_INT_VERIF);
658 link->last_rx_seq = link_int_rx[0];
659 /* The received receive sequence number is not valid if
660 * it is not equal to the last transmitted send sequence
661 * number. Ignore messages containing this error. As a
662 * result, timer T391 expires and the user then
663 * increments the error count. */
664 if (link_int_rx[1] != link->last_tx_seq)
665 return 0;
666 break;
667 case Q933_REPT_SINGLE_PVC_ASYNC_STS:
668 default:
669 return -1;
670 }
671
672 check_link_state(link, true);
673 if (count_pvc_status(tp, MAX_SUPPORTED_PVC + 1) > MAX_SUPPORTED_PVC) {
674 LOGPFRL(link, LOGL_ERROR, "Rx STATUS: Too many PVC! Only %d are supported!\n", MAX_SUPPORTED_PVC);
675 }
676
677 switch (rep_type) {
678 case Q933_REPT_FULL_STATUS:
679 parse_full_pvc_status(link, tp, MAX_SUPPORTED_PVC);
680 break;
681 case Q933_REPT_LINK_INTEGRITY_VERIF:
682 parse_link_pvc_status(link, tp, MAX_SUPPORTED_PVC);
683 break;
684 default:
685 break;
686 }
687
688 /* The network responds to each STATUS ENQUIRY message with a
689 * STATUS message and resets the T392 timer */
690 osmo_timer_schedule(&link->t392, osmo_tdef_get(link->net->T_defs, 392, OSMO_TDEF_S, 15), 0);
691
692 return 0;
693}
694
695static int rx_lmi_q922(struct msgb *msg)
696{
697 struct osmo_fr_link *link = msg->dst;
698 struct q933_a_hdr *qh;
699 /* the + 1 is used to detect more than MAX_SUPPORTED_PVC */
700 struct tlv_parsed tp[MAX_SUPPORTED_PVC + 1];
701 uint8_t *lapf;
702 int rc;
703
704 OSMO_ASSERT(link);
705
706 if (msgb_l2len(msg) < 1)
707 return -1;
708 lapf = msgb_l2(msg);
709
710 /* we only support LAPF UI frames */
711 if (lapf[0] != LAPF_UI)
712 return -1;
713
714 msg->l3h = msg->l2h + 1;
715 if (msgb_l3len(msg) < 3)
716 return -1;
717
718 qh = (struct q933_a_hdr *) msgb_l3(msg);
719 if (qh->prot_disc != Q931_PDISC_CC) {
720 LOGPFRL(link, LOGL_NOTICE,
721 "Rx unsupported LMI protocol discriminator %u\n", qh->prot_disc);
722 return -1;
723 }
724
725 rc = tlv_parse2(tp, MAX_SUPPORTED_PVC + 1, &q933_att_tlvdef,
726 msgb_l3(msg) + sizeof(*qh),
727 msgb_l3len(msg) - sizeof(*qh), 0, 0);
728 if (rc < 0) {
729 LOGPFRL(link, LOGL_NOTICE,
730 "Failed to parse TLVs in LMI message type %u\n", qh->msg_type);
731 return rc;
732 }
733
734 switch (qh->msg_type) {
735 case Q931_MSGT_STATUS_ENQUIRY:
736 rc = rx_lmi_q933_status_enq(msg, tp);
737 break;
738 case Q931_MSGT_STATUS:
739 rc = rx_lmi_q933_status(msg, tp);
740 break;
741 default:
742 LOGPFRL(link, LOGL_NOTICE,
743 "Rx unsupported LMI message type %u\n", qh->msg_type);
744 rc = -1;
745 break;
746 }
747 msgb_free(msg);
748
749 return rc;
750}
751
752int osmo_fr_rx(struct msgb *msg)
753{
754 int rc = 0;
755 uint8_t *frh;
756 uint16_t dlci;
757 struct osmo_fr_dlc *dlc;
758 struct osmo_fr_link *link = msg->dst;
759
760 OSMO_ASSERT(link);
761
762 if (msgb_length(msg) < 2) {
763 LOGPFRL(link, LOGL_ERROR, "Rx short FR header: %u bytes\n", msgb_length(msg));
764 rc = -1;
765 goto out;
766 }
767
768 frh = msg->l1h = msgb_data(msg);
769 if (frh[0] & 0x01) {
770 LOGPFRL(link, LOGL_NOTICE, "Rx Unsupported single-byte FR address\n");
771 rc = -1;
772 goto out;
773 }
774 if ((frh[1] & 0x0f) != 0x01) {
775 LOGPFRL(link, LOGL_NOTICE, "Rx Unknown second FR octet 0x%02x\n", frh[1]);
776 rc = -1;
777 goto out;
778 }
779 dlci = q922_to_dlci(frh);
780 msg->l2h = frh + 2;
781
782 switch (dlci) {
783 case LMI_Q933A_DLCI:
784 return rx_lmi_q922(msg);
785 case LMI_CISCO_DLCI:
786 LOGPFRL(link, LOGL_ERROR, "Rx Unsupported FR DLCI %u\n", dlci);
787 goto out;
788 }
789
790 if (!link->state) {
791 LOGPFRL(link, LOGL_NOTICE, "Link is not reliable. Discarding Rx PDU on DLCI %d\n", dlci);
792 goto out;
793 }
794
Harald Welte36c5e2b2021-01-31 18:36:27 +0100795 dlc = osmo_fr_dlc_by_dlci(link, dlci);
796 if (dlc) {
797 if (dlc->active) {
Alexander Couzens841817e2020-11-19 00:41:29 +0100798 /* dispatch to handler of respective DLC */
799 msg->dst = dlc;
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100800 return dlc->rx_cb(dlc->cb_data, msg);
Harald Welte36c5e2b2021-01-31 18:36:27 +0100801 } else {
Harald Welted6cec242021-01-31 18:57:06 +0100802 LOGPFRL(link, LOGL_NOTICE, "DLCI %u not yet active. Discarding Rx PDU\n", dlci);
Alexander Couzens841817e2020-11-19 00:41:29 +0100803 }
Harald Welte36c5e2b2021-01-31 18:36:27 +0100804 } else {
805 if (link->unknown_dlc_rx_cb)
806 return link->unknown_dlc_rx_cb(link->unknown_dlc_rx_cb_data, msg);
807 else
Harald Welted6cec242021-01-31 18:57:06 +0100808 LOGPFRL(link, LOGL_NOTICE, "DLCI %u doesn't exist. Discarding Rx PDU\n", dlci);
Alexander Couzens841817e2020-11-19 00:41:29 +0100809 }
810
Alexander Couzens841817e2020-11-19 00:41:29 +0100811out:
812 msgb_free(msg);
813
814 return rc;
815}
816
817int osmo_fr_tx_dlc(struct msgb *msg)
818{
819 uint8_t *frh;
820 struct osmo_fr_dlc *dlc = msg->dst;
821 struct osmo_fr_link *link = dlc->link;
822
823 OSMO_ASSERT(dlc);
824 OSMO_ASSERT(link);
825
826 if (!link->state) {
827 LOGPFRL(link, LOGL_NOTICE, "Link is not reliable (yet), discarding Tx\n");
828 msgb_free(msg);
829 return -1;
830 }
831 if (!dlc->active) {
832 LOGPFRL(link, LOGL_NOTICE, "DLCI %u is not active (yet), discarding Tx\n", dlc->dlci);
833 msgb_free(msg);
834 return -1;
835 }
836 LOGPFRL(link, LOGL_DEBUG, "DLCI %u is active, sending message\n", dlc->dlci);
837
838 if (msgb_headroom(msg) < 2) {
839 msgb_free(msg);
840 return -ENOSPC;
841 }
842
843 frh = msgb_push(msg, 2);
844 dlci_to_q922(frh, dlc->dlci);
845
846 msg->dst = link;
Harald Welte2cc1d4d2021-01-31 18:33:31 +0100847 return link->tx_cb(link->cb_data, msg);
Alexander Couzens841817e2020-11-19 00:41:29 +0100848}
849
850/* Every T391 seconds, the user equipment sends a STATUS ENQUIRY
851 * message to the network and resets its polling timer (T391). */
852static void fr_t391_cb(void *data)
853{
854 struct osmo_fr_link *link = data;
855
856 OSMO_ASSERT(link);
857
858 if (link->polling_count % link->net->n391 == 0)
859 tx_lmi_q933_status_enq(link, Q933_REPT_FULL_STATUS);
860 else
861 tx_lmi_q933_status_enq(link, Q933_REPT_LINK_INTEGRITY_VERIF);
862 link->polling_count++;
863 osmo_timer_schedule(&link->t391, osmo_tdef_get(link->net->T_defs, 391, OSMO_TDEF_S, 10), 0);
864}
865
866static void fr_t392_cb(void *data)
867{
868 struct osmo_fr_link *link = data;
869
870 OSMO_ASSERT(link);
871
872 /* A.5 The network increments the error count .. Non-receipt of
873 * a STATUS ENQUIRY within T392, which results in restarting
874 * T392 */
875 link->err_count++;
876 check_link_state(link, false);
877 osmo_timer_schedule(&link->t392, osmo_tdef_get(link->net->T_defs, 392, OSMO_TDEF_S, 15), 0);
878}
879
880/* allocate a frame relay network */
881struct osmo_fr_network *osmo_fr_network_alloc(void *ctx)
882{
883 struct osmo_fr_network *net = talloc_zero(ctx, struct osmo_fr_network);
Alexander Couzense249e362020-12-22 15:39:43 +0100884 if (!net)
885 return NULL;
Alexander Couzens841817e2020-11-19 00:41:29 +0100886
887 INIT_LLIST_HEAD(&net->links);
888 net->T_defs = fr_tdefs;
889 osmo_tdefs_reset(net->T_defs);
890 net->n391 = 6;
891 net->n392 = 3;
892 net->n393 = 4;
893
894 return net;
895}
896
897void osmo_fr_network_free(struct osmo_fr_network *net)
898{
899 struct osmo_fr_link *link, *tmp;
900
901 if (!net)
902 return;
903
904 llist_for_each_entry_safe(link, tmp, &net->links, list) {
905 osmo_fr_link_free(link);
906 }
907}
908
909/* allocate a frame relay link in a given network */
910struct osmo_fr_link *osmo_fr_link_alloc(struct osmo_fr_network *net, enum osmo_fr_role role, const char *name)
911{
912 struct osmo_fr_link *link = talloc_zero(net, struct osmo_fr_link);
913 if (!link)
914 return NULL;
Alexander Couzens841817e2020-11-19 00:41:29 +0100915 link->role = role;
916 link->net = net;
917 link->name = talloc_strdup(link, name);
918 INIT_LLIST_HEAD(&link->dlc_list);
919 llist_add_tail(&link->list, &net->links);
920
921 osmo_timer_setup(&link->t391, fr_t391_cb, link);
922 osmo_timer_setup(&link->t392, fr_t392_cb, link);
923
924 switch (role) {
925 case FR_ROLE_USER_EQUIPMENT:
926 osmo_timer_schedule(&link->t391, osmo_tdef_get(link->net->T_defs, 391, OSMO_TDEF_S, 15), 0);
927 break;
928 case FR_ROLE_NETWORK_EQUIPMENT:
929 osmo_timer_schedule(&link->t392, osmo_tdef_get(link->net->T_defs, 392, OSMO_TDEF_S, 15), 0);
930 break;
931 }
932
Alexander Couzensb6b62cd2020-12-22 15:40:34 +0100933 LOGPFRL(link, LOGL_INFO, "Creating frame relay link with role %s\n", osmo_fr_role_str(role));
934
Alexander Couzens841817e2020-11-19 00:41:29 +0100935 return link;
936}
937
938void osmo_fr_link_free(struct osmo_fr_link *link)
939{
940 struct osmo_fr_dlc *dlc, *tmp;
941
942 if (!link)
943 return;
944
945 osmo_timer_del(&link->t391);
946 osmo_timer_del(&link->t392);
947
948 llist_for_each_entry_safe(dlc, tmp, &link->dlc_list, list) {
949 osmo_fr_dlc_free(dlc);
950 }
951
952 llist_del(&link->list);
953 talloc_free(link);
954}
955
956/* allocate a data link connectoin on a given framerelay link */
957struct osmo_fr_dlc *osmo_fr_dlc_alloc(struct osmo_fr_link *link, uint16_t dlci)
958{
959 struct osmo_fr_dlc *dlc = talloc_zero(link, struct osmo_fr_dlc);
960 if (!dlc)
961 return NULL;
962
963 dlc->link = link;
964 dlc->dlci = dlci;
965 dlc->active = false;
966
967 llist_add_tail(&dlc->list, &link->dlc_list);
968
969 dlc->add = true;
970 tx_lmi_q933_status(link, Q933_IEI_PVC_STATUS);
971
972 return dlc;
973}
974
975void osmo_fr_dlc_free(struct osmo_fr_dlc *dlc)
976{
977 llist_del(&dlc->list);
978 talloc_free(dlc);
979}
980
981/* TODO: rework osmo_fr_dlc_alloc/free with handling it's own memory.
982 * For network role: The dlc have to created by the application (e.g. vty).
983 * The dlc shouldn't free'd directly. It should be communicated to the
984 * other side and wait until it's confirmed OR the link go off and free it afterwards.
985 * For user equpment role: The dlc can be created by the application or the dlc will be created
986 * by the frame relay because the network is configuring the dlc.
987 * The dlc shouldn't be free'd. Only the handler should be set to NULL.
988 */
989
990struct osmo_fr_dlc *osmo_fr_dlc_by_dlci(struct osmo_fr_link *link, uint16_t dlci)
991{
992 struct osmo_fr_dlc *dlc;
993
994 llist_for_each_entry(dlc, &link->dlc_list, list) {
995 if (dlc->dlci == dlci)
996 return dlc;
997 }
998 return NULL;
999}