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