Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 1 | /*! \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 */ |
| 49 | enum 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 */ |
| 82 | enum 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 */ |
| 89 | enum 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 */ |
| 96 | enum 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 */ |
| 118 | struct 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 */ |
| 125 | struct q933_a_pvc_sts { |
Pau Espin Pedrol | 15b5acb | 2021-02-04 12:37:25 +0100 | [diff] [blame] | 126 | #if OSMO_IS_LITTLE_ENDIAN |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 127 | uint8_t dlci_msb:6, |
| 128 | spare:1, |
| 129 | ext0:1; |
| 130 | uint8_t space1:3, |
| 131 | dlci_lsb:4, |
| 132 | ext1:1; |
| 133 | uint8_t reserved:1, |
| 134 | active:1, |
| 135 | delete:1, |
| 136 | new:1, |
| 137 | spare2:3, |
| 138 | ext2:1; |
| 139 | |
Pau Espin Pedrol | 15b5acb | 2021-02-04 12:37:25 +0100 | [diff] [blame] | 140 | #elif OSMO_IS_BIG_ENDIAN |
Oliver Smith | 0b5c09b | 2023-02-17 10:35:38 +0100 | [diff] [blame] | 141 | /* auto-generated from the little endian part above (libosmocore/contrib/struct_endianness.py) */ |
Pau Espin Pedrol | 15b5acb | 2021-02-04 12:37:25 +0100 | [diff] [blame] | 142 | uint8_t ext0:1, spare:1, dlci_msb:6; |
| 143 | uint8_t ext1:1, dlci_lsb:4, space1:3; |
| 144 | uint8_t ext2:1, spare2:3, new:1, delete:1, active:1, reserved:1; |
| 145 | #endif |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 146 | } __attribute__((packed)); |
| 147 | |
| 148 | /* RX Message: 14 [ 00 01 03 08 00 75 95 01 01 00 03 02 01 00 ] */ |
| 149 | /* RX Message: 13 [ 00 01 03 08 00 75 51 01 00 53 02 01 00 ] */ |
| 150 | |
| 151 | const struct value_string osmo_fr_role_names[] = { |
| 152 | { FR_ROLE_USER_EQUIPMENT, "USER" }, |
| 153 | { FR_ROLE_NETWORK_EQUIPMENT, "NETWORK" }, |
| 154 | { 0, NULL } |
| 155 | }; |
| 156 | |
| 157 | /* Table A.4/Q.933 */ |
| 158 | struct osmo_tdef fr_tdefs[] = { |
| 159 | { |
| 160 | .T=391, |
| 161 | .default_val = 10, |
| 162 | .min_val = 5, |
| 163 | .max_val = 30, |
| 164 | .desc = "Link integrity verification polling timer", |
| 165 | .unit = OSMO_TDEF_S, |
| 166 | }, { |
| 167 | .T=392, |
| 168 | .default_val = 15, |
| 169 | .min_val = 5, |
| 170 | .max_val = 30, |
| 171 | .desc = "Polling verification timer", |
| 172 | .unit = OSMO_TDEF_S, |
| 173 | }, |
| 174 | {} |
| 175 | }; |
| 176 | |
| 177 | static const struct tlv_definition q933_att_tlvdef = { |
| 178 | .def = { |
| 179 | [Q933_IEI_REPORT_TYPE] = { TLV_TYPE_TLV }, |
| 180 | [Q933_IEI_LINK_INT_VERIF] = { TLV_TYPE_TLV }, |
| 181 | [Q933_IEI_PVC_STATUS] = { TLV_TYPE_TLV }, |
| 182 | }, |
| 183 | }; |
| 184 | |
| 185 | static void check_link_state(struct osmo_fr_link *link, bool valid); |
| 186 | |
| 187 | static inline uint16_t q922_to_dlci(const uint8_t *hdr) |
| 188 | { |
| 189 | return ((hdr[0] & 0xFC) << 2) | ((hdr[1] & 0xF0) >> 4); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | static inline void dlci_to_q922(uint8_t *hdr, uint16_t dlci) |
| 194 | { |
| 195 | hdr[0] = (dlci >> 2) & 0xFC; |
| 196 | hdr[1] = ((dlci << 4) & 0xF0) | 0x01; |
| 197 | } |
| 198 | |
Harald Welte | 2cc1d4d | 2021-01-31 18:33:31 +0100 | [diff] [blame] | 199 | static void dlc_set_active(struct osmo_fr_dlc *dlc, bool active) |
| 200 | { |
| 201 | if (active == dlc->active) |
| 202 | return; |
| 203 | |
| 204 | dlc->active = active; |
| 205 | |
| 206 | LOGPFRL(dlc->link, LOGL_NOTICE, "DLCI %u became %s\n", dlc->dlci, active ? "active" : "inactive"); |
| 207 | if (dlc->status_cb) |
| 208 | dlc->status_cb(dlc, dlc->cb_data, active); |
| 209 | } |
| 210 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 211 | /* allocate a message buffer and put Q.933 Annex A headers (L2 + L3) */ |
| 212 | static struct msgb *q933_msgb_alloc(uint16_t dlci, uint8_t prot_disc, uint8_t msg_type) |
| 213 | { |
| 214 | struct msgb *msg = msgb_alloc_headroom(1600+64, 64, "FR Q.933 Tx"); |
| 215 | struct q933_a_hdr *qh; |
| 216 | |
| 217 | if (!msg) |
| 218 | return NULL; |
| 219 | |
| 220 | msg->l1h = msgb_put(msg, 2); |
| 221 | dlci_to_q922(msg->l1h, dlci); |
| 222 | |
| 223 | /* LAPF UI control */ |
| 224 | msg->l2h = msgb_put(msg, 1); |
| 225 | *msg->l2h = LAPF_UI; |
| 226 | |
| 227 | msg->l3h = msgb_put(msg, sizeof(*qh)); |
| 228 | qh = (struct q933_a_hdr *) msg->l3h; |
| 229 | qh->prot_disc = prot_disc; |
| 230 | qh->call_ref = LMI_Q933A_CALLREF; |
| 231 | qh->msg_type = msg_type; |
| 232 | |
| 233 | return msg; |
| 234 | } |
| 235 | |
| 236 | /* obtain the [next] transmit sequence number */ |
| 237 | static uint8_t link_get_tx_seq(struct osmo_fr_link *link) |
| 238 | { |
| 239 | /* The {user equipment, network} increments the send sequence |
| 240 | * counter using modulo 256. The value zero is skipped. */ |
| 241 | link->last_tx_seq++; |
| 242 | if (link->last_tx_seq == 0) |
| 243 | link->last_tx_seq++; |
| 244 | |
| 245 | return link->last_tx_seq; |
| 246 | } |
| 247 | |
| 248 | /* Append PVC Status IE according to Q.933 A.3.2 */ |
| 249 | static void msgb_put_link_int_verif(struct msgb *msg, struct osmo_fr_link *link) |
| 250 | { |
| 251 | uint8_t link_int_tx[2]; |
| 252 | link_int_tx[0] = link_get_tx_seq(link); |
| 253 | link_int_tx[1] = link->last_rx_seq; |
| 254 | msgb_tlv_put(msg, Q933_IEI_LINK_INT_VERIF, 2, link_int_tx); |
| 255 | } |
| 256 | |
| 257 | static void dlc_destroy(struct osmo_fr_dlc *dlc) |
| 258 | { |
| 259 | llist_del(&dlc->list); |
| 260 | talloc_free(dlc); |
| 261 | } |
| 262 | |
| 263 | /* Append PVC Status IE according to Q.933 A.3.3 */ |
| 264 | static void msgb_put_pvc_status(struct msgb *msg, struct osmo_fr_dlc *dlc) |
| 265 | { |
| 266 | uint8_t ie[3]; |
| 267 | |
| 268 | ie[0] = (dlc->dlci >> 4) & 0x3f; |
| 269 | /* extension bits */ |
| 270 | ie[1] = 0x80 | ((dlc->dlci & 0xf) << 3); |
| 271 | /* extension bits */ |
| 272 | ie[2] = 0x80; |
| 273 | |
| 274 | /* FIXME: validate: this status should be added as long it's not yet acked by the remote */ |
| 275 | if (dlc->active) |
| 276 | ie[2] |= Q933_PVC_STATUS_DLC_ACTIVE; |
| 277 | |
| 278 | if (dlc->add) { |
| 279 | ie[2] |= Q933_PVC_STATUS_DLC_NEW; |
| 280 | /* we've reported it as new once, reset the status */ |
| 281 | } |
| 282 | |
| 283 | if (dlc->del) { |
| 284 | ie[2] |= Q933_PVC_STATUS_DLC_DELETE; |
| 285 | /* we've reported it as deleted once, destroy it */ |
| 286 | dlc_destroy(dlc); |
| 287 | } |
| 288 | |
| 289 | msgb_tlv_put(msg, Q933_IEI_PVC_STATUS, 3, ie); |
| 290 | } |
| 291 | |
| 292 | /* Send a Q.933 STATUS ENQUIRY given type over given link */ |
| 293 | static int tx_lmi_q933_status_enq(struct osmo_fr_link *link, uint8_t rep_type) |
| 294 | { |
| 295 | struct msgb *resp; |
| 296 | |
| 297 | resp = q933_msgb_alloc(0, Q931_PDISC_CC, Q931_MSGT_STATUS_ENQUIRY); |
| 298 | if (!resp) |
| 299 | return -1; |
| 300 | resp->dst = link; |
| 301 | link->expected_rep = rep_type; |
| 302 | |
| 303 | /* Table A.2/Q.933 */ |
| 304 | msgb_tlv_put(resp, Q933_IEI_REPORT_TYPE, 1, &rep_type); |
| 305 | msgb_put_link_int_verif(resp, link); |
| 306 | |
Harald Welte | 2cc1d4d | 2021-01-31 18:33:31 +0100 | [diff] [blame] | 307 | return link->tx_cb(link->cb_data, resp); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /* Send a Q.933 STATUS of given type over given link */ |
| 311 | static int tx_lmi_q933_status(struct osmo_fr_link *link, uint8_t rep_type) |
| 312 | { |
| 313 | struct osmo_fr_dlc *dlc; |
| 314 | struct msgb *resp; |
| 315 | |
| 316 | resp = q933_msgb_alloc(0, Q931_PDISC_CC, Q931_MSGT_STATUS); |
| 317 | if (!resp) |
| 318 | return -1; |
| 319 | |
| 320 | resp->dst = link; |
| 321 | |
| 322 | /* Table A.1/Q.933 */ |
| 323 | msgb_tlv_put(resp, Q933_IEI_REPORT_TYPE, 1, &rep_type); |
| 324 | switch (rep_type) { |
| 325 | case Q933_REPT_FULL_STATUS: |
| 326 | msgb_put_link_int_verif(resp, link); |
| 327 | llist_for_each_entry(dlc, &link->dlc_list, list) { |
| 328 | if (dlc->add || dlc->del) |
| 329 | dlc->state_send = true; |
| 330 | |
| 331 | msgb_put_pvc_status(resp, dlc); |
| 332 | } |
| 333 | break; |
| 334 | case Q933_REPT_LINK_INTEGRITY_VERIF: |
| 335 | msgb_put_link_int_verif(resp, link); |
| 336 | llist_for_each_entry(dlc, &link->dlc_list, list) { |
| 337 | if (dlc->add || dlc->del) { |
| 338 | msgb_put_pvc_status(resp, dlc); |
| 339 | dlc->state_send = true; |
| 340 | } |
| 341 | } |
| 342 | break; |
| 343 | case Q933_REPT_SINGLE_PVC_ASYNC_STS: |
| 344 | llist_for_each_entry(dlc, &link->dlc_list, list) |
| 345 | msgb_put_pvc_status(resp, dlc); |
| 346 | break; |
| 347 | } |
| 348 | |
Harald Welte | 2cc1d4d | 2021-01-31 18:33:31 +0100 | [diff] [blame] | 349 | return link->tx_cb(link->cb_data, resp); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | |
Harald Welte | cfe0ed6 | 2021-02-04 19:23:03 +0100 | [diff] [blame] | 353 | static void link_set_failed(struct osmo_fr_link *link) |
| 354 | { |
| 355 | struct osmo_fr_dlc *dlc; |
| 356 | |
| 357 | LOGPFRL(link, LOGL_NOTICE, "Link failed\n"); |
| 358 | link->state = false; |
| 359 | if (link->status_cb) |
| 360 | link->status_cb(link, link->cb_data, link->state); |
| 361 | |
| 362 | llist_for_each_entry(dlc, &link->dlc_list, list) { |
| 363 | dlc_set_active(dlc, false); |
| 364 | } |
| 365 | } |
| 366 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 367 | /* Q.933 */ |
| 368 | static int rx_lmi_q933_status_enq(struct msgb *msg, struct tlv_parsed *tp) |
| 369 | { |
| 370 | struct osmo_fr_link *link = msg->dst; |
| 371 | struct osmo_fr_dlc *dlc; |
| 372 | const uint8_t *link_int_rx; |
| 373 | uint8_t rep_type; |
| 374 | |
| 375 | OSMO_ASSERT(link); |
| 376 | |
| 377 | if (link->role == FR_ROLE_USER_EQUIPMENT) { |
Harald Welte | 78ebc3f | 2020-11-25 17:39:06 +0100 | [diff] [blame] | 378 | LOGPFRL(link, LOGL_ERROR, "STATUS-ENQ aren't supported in role user\n"); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 379 | return -1; |
| 380 | } |
| 381 | |
| 382 | /* check for mandatory IEs */ |
| 383 | if (!TLVP_PRES_LEN(tp, Q933_IEI_REPORT_TYPE, 1) || |
| 384 | !TLVP_PRES_LEN(tp, Q933_IEI_LINK_INT_VERIF, 2)) |
| 385 | return -1; |
| 386 | |
| 387 | rep_type = *TLVP_VAL(tp, Q933_IEI_REPORT_TYPE); |
| 388 | |
| 389 | link_int_rx = TLVP_VAL(tp, Q933_IEI_LINK_INT_VERIF); |
| 390 | link->last_rx_seq = link_int_rx[0]; |
| 391 | |
Harald Welte | 8373c05 | 2021-02-04 19:26:18 +0100 | [diff] [blame] | 392 | /* this is a bit of a hack. Q.933 explicitly forbids either side from ever |
| 393 | * sending a sequence number of '0'. Values start from '1' and are modulo 256, |
| 394 | * but '0' is always skipped. So if the peer is sending us a "last received |
| 395 | * sequence number of '0' it means it has not yet received any packets from us, |
| 396 | * which in turn can only mean that it has just been restarted. Let's treat |
| 397 | * this as "service affecting condition" and notify upper layers. This helps |
| 398 | * particularly in recovering from rapidly re-starting peers, where the Q.933 |
| 399 | * nor NS have time to actually detect the connection was lost. Se OS#4974 */ |
| 400 | if (link_int_rx[1] == 0) { |
| 401 | link_set_failed(link); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 402 | /* the network checks the receive sequence number received from |
| 403 | * the user equipment against its send sequence counter */ |
Harald Welte | 8373c05 | 2021-02-04 19:26:18 +0100 | [diff] [blame] | 404 | } else if (link_int_rx[1] != link->last_tx_seq) { |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 405 | check_link_state(link, false); |
| 406 | link->err_count++; |
| 407 | } else { |
| 408 | check_link_state(link, true); |
| 409 | /* confirm DLC state changes */ |
| 410 | llist_for_each_entry(dlc, &link->dlc_list, list) { |
| 411 | if (!dlc->state_send) |
| 412 | continue; |
| 413 | |
| 414 | if (dlc->add) { |
Harald Welte | 2cc1d4d | 2021-01-31 18:33:31 +0100 | [diff] [blame] | 415 | dlc_set_active(dlc, link->state); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 416 | dlc->add = false; |
| 417 | } |
| 418 | |
| 419 | if (dlc->del) { |
| 420 | dlc->del = false; |
| 421 | } |
| 422 | |
| 423 | dlc->state_send = false; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | |
| 428 | /* The network responds to each STATUS ENQUIRY message with a |
| 429 | * STATUS message and resets the T392 timer */ |
| 430 | osmo_timer_schedule(&link->t392, osmo_tdef_get(link->net->T_defs, 392, OSMO_TDEF_S, 15), 0); |
| 431 | |
| 432 | return tx_lmi_q933_status(link, rep_type); |
| 433 | } |
| 434 | |
| 435 | /* check if the link become active. |
| 436 | * The link becomes active when enough times a STATUS/STATUS ENQUIRY arrives without any loss. |
| 437 | * Look at the last N393 STATUS/STATUS ENQUIRY PDUs. The link is valid if at least N392 |
| 438 | * got received. |
| 439 | * param[in] valid contains the status of the last packet */ |
| 440 | static void check_link_state(struct osmo_fr_link *link, bool valid) |
| 441 | { |
| 442 | unsigned int last, i; |
| 443 | unsigned int carry = 0; |
| 444 | struct osmo_fr_dlc *dlc; |
| 445 | |
| 446 | link->succeed <<= 1; |
| 447 | if (valid) |
| 448 | link->succeed |= 1; |
| 449 | |
| 450 | /* count the bits */ |
| 451 | last = link->succeed & ((1 << link->net->n393) - 1); |
| 452 | for (i = 0; i < link->net->n393; i++) |
| 453 | if (last & (1 << i)) |
| 454 | carry++; |
| 455 | |
| 456 | if (link->net->n393 - carry >= link->net->n392) { |
| 457 | /* failing link */ |
| 458 | if (!link->state) |
| 459 | return; |
| 460 | |
Harald Welte | cfe0ed6 | 2021-02-04 19:23:03 +0100 | [diff] [blame] | 461 | link_set_failed(link); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 462 | } else { |
| 463 | /* good link */ |
| 464 | if (link->state) |
| 465 | return; |
| 466 | |
| 467 | LOGPFRL(link, LOGL_NOTICE, "Link recovered\n"); |
| 468 | link->state = true; |
Harald Welte | 2cc1d4d | 2021-01-31 18:33:31 +0100 | [diff] [blame] | 469 | if (link->status_cb) |
| 470 | link->status_cb(link, link->cb_data, link->state); |
| 471 | |
Harald Welte | 2319014 | 2021-01-31 17:06:34 +0100 | [diff] [blame] | 472 | if (link->role == FR_ROLE_USER_EQUIPMENT) { |
| 473 | /* make sure the next STATUS ENQUIRY is for a full |
| 474 | * status report to get the configred DLCs ASAP */ |
| 475 | link->polling_count = 0; |
Harald Welte | 2cc1d4d | 2021-01-31 18:33:31 +0100 | [diff] [blame] | 476 | /* we must not proceed further below if we're in user role, |
| 477 | * as otherwise link recovery would set all DLCs as active */ |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 478 | return; |
Harald Welte | 2319014 | 2021-01-31 17:06:34 +0100 | [diff] [blame] | 479 | } |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 480 | |
| 481 | llist_for_each_entry(dlc, &link->dlc_list, list) { |
| 482 | if (!dlc->add && !dlc->del) |
Harald Welte | 2cc1d4d | 2021-01-31 18:33:31 +0100 | [diff] [blame] | 483 | dlc_set_active(dlc, true); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | static int validate_pvc_status(struct tlv_parsed *tp, size_t tp_len) |
| 489 | { |
| 490 | size_t i; |
| 491 | uint16_t len = 0; |
| 492 | |
| 493 | for (i = 0; i < tp_len; i++) { |
| 494 | if (!TLVP_PRESENT(&tp[i], Q933_IEI_PVC_STATUS)) |
| 495 | continue; |
| 496 | |
| 497 | /* PVC status can be 2 or 3 bytes. If the PVC is bigger |
| 498 | * ignore this to be compatible to future extensions. */ |
| 499 | len = TLVP_LEN(&tp[i], Q933_IEI_PVC_STATUS); |
| 500 | if (len <= 1) { |
| 501 | return -EINVAL; |
| 502 | } |
| 503 | /* FIXME: validate correct flags: are some flags invalid at the same time? */ |
| 504 | } |
| 505 | |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | static int parse_full_pvc_status(struct osmo_fr_link *link, struct tlv_parsed *tp, size_t tp_len) |
| 510 | { |
| 511 | size_t i; |
| 512 | int err = 0; |
| 513 | struct osmo_fr_dlc *dlc, *tmp; |
| 514 | struct q933_a_pvc_sts *pvc; |
| 515 | uint16_t dlci = 0; |
| 516 | uint16_t *dlcis = talloc_zero_array(link, uint16_t, tp_len); |
| 517 | if (!dlcis) |
| 518 | return -ENOMEM; |
| 519 | |
| 520 | /* first run validate all PVCs */ |
| 521 | err = validate_pvc_status(tp, tp_len); |
| 522 | if (err < 0) |
| 523 | goto out; |
| 524 | |
| 525 | for (i = 0; i < tp_len; i++) { |
| 526 | if (!TLVP_PRESENT(&tp[i], Q933_IEI_PVC_STATUS)) |
| 527 | continue; |
| 528 | |
| 529 | /* parse only 3 byte PVCs */ |
| 530 | pvc = (struct q933_a_pvc_sts *) TLVP_VAL_MINLEN( |
| 531 | &tp[i], |
| 532 | Q933_IEI_PVC_STATUS, |
| 533 | sizeof(struct q933_a_pvc_sts)); |
| 534 | if (!pvc) |
| 535 | continue; |
| 536 | |
| 537 | dlci = ((pvc->dlci_msb & 0x3f) << 4) | (pvc->dlci_lsb & 0xf); |
| 538 | dlcis[i] = dlci; |
| 539 | dlc = osmo_fr_dlc_by_dlci(link, dlci); |
| 540 | if (!dlc) { |
| 541 | dlc = osmo_fr_dlc_alloc(link, dlci); |
| 542 | if (!dlc) { |
| 543 | LOGPFRL(link, LOGL_ERROR, "Could not create DLC %d\n", dlci); |
| 544 | continue; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | /* Figure A.3/Q.933: The delete bit is only applicable for timely notification |
| 549 | * using the optional single PVC asynchronous status report. |
| 550 | * Ignoring the delete. */ |
| 551 | dlc->add = pvc->new; |
Harald Welte | 2cc1d4d | 2021-01-31 18:33:31 +0100 | [diff] [blame] | 552 | dlc_set_active(dlc, pvc->active); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 553 | dlc->del = 0; |
| 554 | } |
| 555 | |
| 556 | /* check if all dlc are present in PVC Status */ |
| 557 | llist_for_each_entry_safe(dlc, tmp, &link->dlc_list, list) { |
| 558 | bool found = false; |
| 559 | for (i = 0; i < tp_len; i++) { |
| 560 | if (dlcis[i] == dlc->dlci) { |
| 561 | found = true; |
| 562 | break; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | if (!found) { |
Harald Welte | 2cc1d4d | 2021-01-31 18:33:31 +0100 | [diff] [blame] | 567 | dlc_set_active(dlc, false); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 568 | dlc->del = true; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | return 0; |
| 573 | out: |
| 574 | talloc_free(dlcis); |
| 575 | return err; |
| 576 | } |
| 577 | |
| 578 | static int parse_link_pvc_status(struct osmo_fr_link *link, struct tlv_parsed *tp, size_t tp_len) |
| 579 | { |
| 580 | int err; |
| 581 | size_t i; |
| 582 | struct q933_a_pvc_sts *pvc; |
| 583 | struct osmo_fr_dlc *dlc; |
| 584 | uint16_t dlci = 0; |
| 585 | |
| 586 | err = validate_pvc_status(tp, tp_len); |
| 587 | if (err < 0) |
| 588 | return err; |
| 589 | |
| 590 | for (i = 0; i < tp_len; i++) { |
| 591 | if (!TLVP_PRESENT(&tp[i], Q933_IEI_PVC_STATUS)) |
| 592 | continue; |
| 593 | |
| 594 | /* parse only 3 byte PVCs */ |
| 595 | pvc = (struct q933_a_pvc_sts *) TLVP_VAL_MINLEN( |
| 596 | &tp[i], |
| 597 | Q933_IEI_PVC_STATUS, |
| 598 | sizeof(struct q933_a_pvc_sts)); |
| 599 | if (!pvc) |
| 600 | continue; |
| 601 | |
| 602 | dlci = ((pvc->dlci_msb & 0x3f) << 4) | (pvc->dlci_lsb & 0xf); |
| 603 | dlc = osmo_fr_dlc_by_dlci(link, dlci); |
| 604 | if (!dlc) { |
| 605 | /* don't create dlc's for the ones which are about to be deleted. */ |
Harald Welte | 29b77a6 | 2020-11-26 09:28:49 +0100 | [diff] [blame] | 606 | if (pvc->delete) |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 607 | continue; |
| 608 | |
| 609 | dlc = osmo_fr_dlc_alloc(link, dlci); |
| 610 | if (!dlc) { |
| 611 | LOGPFRL(link, LOGL_ERROR, "Rx STATUS: Could not create DLC %d\n", dlci); |
Alexander Couzens | ca3550a | 2021-02-03 11:35:48 +0100 | [diff] [blame] | 612 | continue; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | |
| 616 | if (pvc->delete) { |
| 617 | dlc->del = 1; |
| 618 | } else { |
| 619 | dlc->add = pvc->new; |
Harald Welte | 2cc1d4d | 2021-01-31 18:33:31 +0100 | [diff] [blame] | 620 | dlc_set_active(dlc, pvc->active); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 621 | dlc->del = 0; |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | static size_t count_pvc_status(struct tlv_parsed *tp, size_t tp_len) |
| 629 | { |
| 630 | size_t i, count = 0; |
| 631 | for (i = 0; i < tp_len; i++) { |
| 632 | if (!TLVP_PRESENT(&tp[i], Q933_IEI_PVC_STATUS)) |
| 633 | continue; |
| 634 | count++; |
| 635 | } |
| 636 | |
| 637 | return count; |
| 638 | } |
| 639 | |
| 640 | static int rx_lmi_q933_status(struct msgb *msg, struct tlv_parsed *tp) |
| 641 | { |
| 642 | struct osmo_fr_link *link = msg->dst; |
| 643 | const uint8_t *link_int_rx; |
| 644 | uint8_t rep_type; |
| 645 | |
| 646 | OSMO_ASSERT(link); |
| 647 | |
| 648 | if (link->role == FR_ROLE_NETWORK_EQUIPMENT) { |
Harald Welte | 78ebc3f | 2020-11-25 17:39:06 +0100 | [diff] [blame] | 649 | LOGPFRL(link, LOGL_ERROR, "Rx STATUS: STATUS aren't supported in role network\n"); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 650 | return -1; |
| 651 | } |
| 652 | |
| 653 | /* check for mandatory IEs */ |
| 654 | if (!TLVP_PRES_LEN(tp, Q933_IEI_REPORT_TYPE, 1)) { |
Daniel Willmann | 1f666e8 | 2021-10-26 16:15:51 +0200 | [diff] [blame] | 655 | LOGPFRL(link, LOGL_NOTICE, "Rx STATUS: Missing TLV Q933 Report Type\n"); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 656 | return -1; |
| 657 | } |
| 658 | |
| 659 | rep_type = *TLVP_VAL(tp, Q933_IEI_REPORT_TYPE); |
| 660 | |
| 661 | switch (rep_type) { |
| 662 | case Q933_REPT_FULL_STATUS: |
| 663 | case Q933_REPT_LINK_INTEGRITY_VERIF: |
| 664 | if (rep_type != link->expected_rep) { |
| 665 | LOGPFRL(link, LOGL_NOTICE, "Rx STATUS: Unexpected Q933 report type (got 0x%x != exp 0x%x)\n", |
| 666 | rep_type, link->expected_rep); |
| 667 | return -1; |
| 668 | } |
| 669 | |
| 670 | if (!TLVP_PRES_LEN(tp, Q933_IEI_LINK_INT_VERIF, 2)) { |
| 671 | LOGPFRL(link, LOGL_NOTICE, "Rx STATUS: Missing TLV Q933 Link Integrety Verification\n"); |
| 672 | return -1; |
| 673 | } |
| 674 | link_int_rx = TLVP_VAL(tp, Q933_IEI_LINK_INT_VERIF); |
| 675 | link->last_rx_seq = link_int_rx[0]; |
| 676 | /* The received receive sequence number is not valid if |
| 677 | * it is not equal to the last transmitted send sequence |
| 678 | * number. Ignore messages containing this error. As a |
| 679 | * result, timer T391 expires and the user then |
| 680 | * increments the error count. */ |
| 681 | if (link_int_rx[1] != link->last_tx_seq) |
| 682 | return 0; |
| 683 | break; |
| 684 | case Q933_REPT_SINGLE_PVC_ASYNC_STS: |
| 685 | default: |
| 686 | return -1; |
| 687 | } |
| 688 | |
| 689 | check_link_state(link, true); |
| 690 | if (count_pvc_status(tp, MAX_SUPPORTED_PVC + 1) > MAX_SUPPORTED_PVC) { |
| 691 | LOGPFRL(link, LOGL_ERROR, "Rx STATUS: Too many PVC! Only %d are supported!\n", MAX_SUPPORTED_PVC); |
| 692 | } |
| 693 | |
| 694 | switch (rep_type) { |
| 695 | case Q933_REPT_FULL_STATUS: |
| 696 | parse_full_pvc_status(link, tp, MAX_SUPPORTED_PVC); |
| 697 | break; |
| 698 | case Q933_REPT_LINK_INTEGRITY_VERIF: |
| 699 | parse_link_pvc_status(link, tp, MAX_SUPPORTED_PVC); |
| 700 | break; |
| 701 | default: |
| 702 | break; |
| 703 | } |
| 704 | |
| 705 | /* The network responds to each STATUS ENQUIRY message with a |
| 706 | * STATUS message and resets the T392 timer */ |
| 707 | osmo_timer_schedule(&link->t392, osmo_tdef_get(link->net->T_defs, 392, OSMO_TDEF_S, 15), 0); |
| 708 | |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | static int rx_lmi_q922(struct msgb *msg) |
| 713 | { |
| 714 | struct osmo_fr_link *link = msg->dst; |
| 715 | struct q933_a_hdr *qh; |
| 716 | /* the + 1 is used to detect more than MAX_SUPPORTED_PVC */ |
| 717 | struct tlv_parsed tp[MAX_SUPPORTED_PVC + 1]; |
| 718 | uint8_t *lapf; |
| 719 | int rc; |
| 720 | |
| 721 | OSMO_ASSERT(link); |
| 722 | |
| 723 | if (msgb_l2len(msg) < 1) |
| 724 | return -1; |
| 725 | lapf = msgb_l2(msg); |
| 726 | |
| 727 | /* we only support LAPF UI frames */ |
| 728 | if (lapf[0] != LAPF_UI) |
| 729 | return -1; |
| 730 | |
| 731 | msg->l3h = msg->l2h + 1; |
| 732 | if (msgb_l3len(msg) < 3) |
| 733 | return -1; |
| 734 | |
| 735 | qh = (struct q933_a_hdr *) msgb_l3(msg); |
| 736 | if (qh->prot_disc != Q931_PDISC_CC) { |
| 737 | LOGPFRL(link, LOGL_NOTICE, |
| 738 | "Rx unsupported LMI protocol discriminator %u\n", qh->prot_disc); |
| 739 | return -1; |
| 740 | } |
| 741 | |
| 742 | rc = tlv_parse2(tp, MAX_SUPPORTED_PVC + 1, &q933_att_tlvdef, |
| 743 | msgb_l3(msg) + sizeof(*qh), |
| 744 | msgb_l3len(msg) - sizeof(*qh), 0, 0); |
| 745 | if (rc < 0) { |
| 746 | LOGPFRL(link, LOGL_NOTICE, |
| 747 | "Failed to parse TLVs in LMI message type %u\n", qh->msg_type); |
| 748 | return rc; |
| 749 | } |
| 750 | |
| 751 | switch (qh->msg_type) { |
| 752 | case Q931_MSGT_STATUS_ENQUIRY: |
| 753 | rc = rx_lmi_q933_status_enq(msg, tp); |
| 754 | break; |
| 755 | case Q931_MSGT_STATUS: |
| 756 | rc = rx_lmi_q933_status(msg, tp); |
| 757 | break; |
| 758 | default: |
| 759 | LOGPFRL(link, LOGL_NOTICE, |
| 760 | "Rx unsupported LMI message type %u\n", qh->msg_type); |
| 761 | rc = -1; |
| 762 | break; |
| 763 | } |
| 764 | msgb_free(msg); |
| 765 | |
| 766 | return rc; |
| 767 | } |
| 768 | |
| 769 | int osmo_fr_rx(struct msgb *msg) |
| 770 | { |
| 771 | int rc = 0; |
| 772 | uint8_t *frh; |
| 773 | uint16_t dlci; |
| 774 | struct osmo_fr_dlc *dlc; |
| 775 | struct osmo_fr_link *link = msg->dst; |
| 776 | |
| 777 | OSMO_ASSERT(link); |
| 778 | |
| 779 | if (msgb_length(msg) < 2) { |
| 780 | LOGPFRL(link, LOGL_ERROR, "Rx short FR header: %u bytes\n", msgb_length(msg)); |
| 781 | rc = -1; |
| 782 | goto out; |
| 783 | } |
| 784 | |
| 785 | frh = msg->l1h = msgb_data(msg); |
| 786 | if (frh[0] & 0x01) { |
| 787 | LOGPFRL(link, LOGL_NOTICE, "Rx Unsupported single-byte FR address\n"); |
| 788 | rc = -1; |
| 789 | goto out; |
| 790 | } |
| 791 | if ((frh[1] & 0x0f) != 0x01) { |
| 792 | LOGPFRL(link, LOGL_NOTICE, "Rx Unknown second FR octet 0x%02x\n", frh[1]); |
| 793 | rc = -1; |
| 794 | goto out; |
| 795 | } |
| 796 | dlci = q922_to_dlci(frh); |
| 797 | msg->l2h = frh + 2; |
| 798 | |
| 799 | switch (dlci) { |
| 800 | case LMI_Q933A_DLCI: |
| 801 | return rx_lmi_q922(msg); |
| 802 | case LMI_CISCO_DLCI: |
| 803 | LOGPFRL(link, LOGL_ERROR, "Rx Unsupported FR DLCI %u\n", dlci); |
| 804 | goto out; |
| 805 | } |
| 806 | |
| 807 | if (!link->state) { |
| 808 | LOGPFRL(link, LOGL_NOTICE, "Link is not reliable. Discarding Rx PDU on DLCI %d\n", dlci); |
| 809 | goto out; |
| 810 | } |
| 811 | |
Harald Welte | 36c5e2b | 2021-01-31 18:36:27 +0100 | [diff] [blame] | 812 | dlc = osmo_fr_dlc_by_dlci(link, dlci); |
| 813 | if (dlc) { |
| 814 | if (dlc->active) { |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 815 | /* dispatch to handler of respective DLC */ |
| 816 | msg->dst = dlc; |
Harald Welte | 2cc1d4d | 2021-01-31 18:33:31 +0100 | [diff] [blame] | 817 | return dlc->rx_cb(dlc->cb_data, msg); |
Harald Welte | 36c5e2b | 2021-01-31 18:36:27 +0100 | [diff] [blame] | 818 | } else { |
Harald Welte | d6cec24 | 2021-01-31 18:57:06 +0100 | [diff] [blame] | 819 | LOGPFRL(link, LOGL_NOTICE, "DLCI %u not yet active. Discarding Rx PDU\n", dlci); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 820 | } |
Harald Welte | 36c5e2b | 2021-01-31 18:36:27 +0100 | [diff] [blame] | 821 | } else { |
| 822 | if (link->unknown_dlc_rx_cb) |
| 823 | return link->unknown_dlc_rx_cb(link->unknown_dlc_rx_cb_data, msg); |
| 824 | else |
Harald Welte | d6cec24 | 2021-01-31 18:57:06 +0100 | [diff] [blame] | 825 | LOGPFRL(link, LOGL_NOTICE, "DLCI %u doesn't exist. Discarding Rx PDU\n", dlci); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 826 | } |
| 827 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 828 | out: |
| 829 | msgb_free(msg); |
| 830 | |
| 831 | return rc; |
| 832 | } |
| 833 | |
| 834 | int osmo_fr_tx_dlc(struct msgb *msg) |
| 835 | { |
| 836 | uint8_t *frh; |
| 837 | struct osmo_fr_dlc *dlc = msg->dst; |
| 838 | struct osmo_fr_link *link = dlc->link; |
| 839 | |
| 840 | OSMO_ASSERT(dlc); |
| 841 | OSMO_ASSERT(link); |
| 842 | |
| 843 | if (!link->state) { |
| 844 | LOGPFRL(link, LOGL_NOTICE, "Link is not reliable (yet), discarding Tx\n"); |
| 845 | msgb_free(msg); |
| 846 | return -1; |
| 847 | } |
| 848 | if (!dlc->active) { |
| 849 | LOGPFRL(link, LOGL_NOTICE, "DLCI %u is not active (yet), discarding Tx\n", dlc->dlci); |
| 850 | msgb_free(msg); |
| 851 | return -1; |
| 852 | } |
| 853 | LOGPFRL(link, LOGL_DEBUG, "DLCI %u is active, sending message\n", dlc->dlci); |
| 854 | |
| 855 | if (msgb_headroom(msg) < 2) { |
| 856 | msgb_free(msg); |
| 857 | return -ENOSPC; |
| 858 | } |
| 859 | |
| 860 | frh = msgb_push(msg, 2); |
| 861 | dlci_to_q922(frh, dlc->dlci); |
| 862 | |
| 863 | msg->dst = link; |
Harald Welte | 2cc1d4d | 2021-01-31 18:33:31 +0100 | [diff] [blame] | 864 | return link->tx_cb(link->cb_data, msg); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | /* Every T391 seconds, the user equipment sends a STATUS ENQUIRY |
| 868 | * message to the network and resets its polling timer (T391). */ |
| 869 | static void fr_t391_cb(void *data) |
| 870 | { |
| 871 | struct osmo_fr_link *link = data; |
| 872 | |
| 873 | OSMO_ASSERT(link); |
| 874 | |
| 875 | if (link->polling_count % link->net->n391 == 0) |
| 876 | tx_lmi_q933_status_enq(link, Q933_REPT_FULL_STATUS); |
| 877 | else |
| 878 | tx_lmi_q933_status_enq(link, Q933_REPT_LINK_INTEGRITY_VERIF); |
| 879 | link->polling_count++; |
| 880 | osmo_timer_schedule(&link->t391, osmo_tdef_get(link->net->T_defs, 391, OSMO_TDEF_S, 10), 0); |
| 881 | } |
| 882 | |
| 883 | static void fr_t392_cb(void *data) |
| 884 | { |
| 885 | struct osmo_fr_link *link = data; |
| 886 | |
| 887 | OSMO_ASSERT(link); |
| 888 | |
| 889 | /* A.5 The network increments the error count .. Non-receipt of |
| 890 | * a STATUS ENQUIRY within T392, which results in restarting |
| 891 | * T392 */ |
| 892 | link->err_count++; |
| 893 | check_link_state(link, false); |
| 894 | osmo_timer_schedule(&link->t392, osmo_tdef_get(link->net->T_defs, 392, OSMO_TDEF_S, 15), 0); |
| 895 | } |
| 896 | |
| 897 | /* allocate a frame relay network */ |
| 898 | struct osmo_fr_network *osmo_fr_network_alloc(void *ctx) |
| 899 | { |
| 900 | struct osmo_fr_network *net = talloc_zero(ctx, struct osmo_fr_network); |
Alexander Couzens | e249e36 | 2020-12-22 15:39:43 +0100 | [diff] [blame] | 901 | if (!net) |
| 902 | return NULL; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 903 | |
| 904 | INIT_LLIST_HEAD(&net->links); |
| 905 | net->T_defs = fr_tdefs; |
| 906 | osmo_tdefs_reset(net->T_defs); |
| 907 | net->n391 = 6; |
| 908 | net->n392 = 3; |
| 909 | net->n393 = 4; |
| 910 | |
| 911 | return net; |
| 912 | } |
| 913 | |
| 914 | void osmo_fr_network_free(struct osmo_fr_network *net) |
| 915 | { |
| 916 | struct osmo_fr_link *link, *tmp; |
| 917 | |
| 918 | if (!net) |
| 919 | return; |
| 920 | |
| 921 | llist_for_each_entry_safe(link, tmp, &net->links, list) { |
| 922 | osmo_fr_link_free(link); |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | /* allocate a frame relay link in a given network */ |
| 927 | struct osmo_fr_link *osmo_fr_link_alloc(struct osmo_fr_network *net, enum osmo_fr_role role, const char *name) |
| 928 | { |
| 929 | struct osmo_fr_link *link = talloc_zero(net, struct osmo_fr_link); |
| 930 | if (!link) |
| 931 | return NULL; |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 932 | link->role = role; |
| 933 | link->net = net; |
| 934 | link->name = talloc_strdup(link, name); |
| 935 | INIT_LLIST_HEAD(&link->dlc_list); |
| 936 | llist_add_tail(&link->list, &net->links); |
| 937 | |
| 938 | osmo_timer_setup(&link->t391, fr_t391_cb, link); |
| 939 | osmo_timer_setup(&link->t392, fr_t392_cb, link); |
| 940 | |
| 941 | switch (role) { |
| 942 | case FR_ROLE_USER_EQUIPMENT: |
| 943 | osmo_timer_schedule(&link->t391, osmo_tdef_get(link->net->T_defs, 391, OSMO_TDEF_S, 15), 0); |
| 944 | break; |
| 945 | case FR_ROLE_NETWORK_EQUIPMENT: |
| 946 | osmo_timer_schedule(&link->t392, osmo_tdef_get(link->net->T_defs, 392, OSMO_TDEF_S, 15), 0); |
| 947 | break; |
| 948 | } |
| 949 | |
Alexander Couzens | b6b62cd | 2020-12-22 15:40:34 +0100 | [diff] [blame] | 950 | LOGPFRL(link, LOGL_INFO, "Creating frame relay link with role %s\n", osmo_fr_role_str(role)); |
| 951 | |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 952 | return link; |
| 953 | } |
| 954 | |
| 955 | void osmo_fr_link_free(struct osmo_fr_link *link) |
| 956 | { |
| 957 | struct osmo_fr_dlc *dlc, *tmp; |
| 958 | |
| 959 | if (!link) |
| 960 | return; |
| 961 | |
| 962 | osmo_timer_del(&link->t391); |
| 963 | osmo_timer_del(&link->t392); |
| 964 | |
| 965 | llist_for_each_entry_safe(dlc, tmp, &link->dlc_list, list) { |
| 966 | osmo_fr_dlc_free(dlc); |
| 967 | } |
| 968 | |
| 969 | llist_del(&link->list); |
| 970 | talloc_free(link); |
| 971 | } |
| 972 | |
| 973 | /* allocate a data link connectoin on a given framerelay link */ |
| 974 | struct osmo_fr_dlc *osmo_fr_dlc_alloc(struct osmo_fr_link *link, uint16_t dlci) |
| 975 | { |
| 976 | struct osmo_fr_dlc *dlc = talloc_zero(link, struct osmo_fr_dlc); |
| 977 | if (!dlc) |
| 978 | return NULL; |
| 979 | |
| 980 | dlc->link = link; |
| 981 | dlc->dlci = dlci; |
| 982 | dlc->active = false; |
| 983 | |
| 984 | llist_add_tail(&dlc->list, &link->dlc_list); |
| 985 | |
| 986 | dlc->add = true; |
Harald Welte | f3dc0f9 | 2021-02-05 11:09:30 +0100 | [diff] [blame] | 987 | tx_lmi_q933_status(link, Q933_REPT_SINGLE_PVC_ASYNC_STS); |
Alexander Couzens | 841817e | 2020-11-19 00:41:29 +0100 | [diff] [blame] | 988 | |
| 989 | return dlc; |
| 990 | } |
| 991 | |
| 992 | void osmo_fr_dlc_free(struct osmo_fr_dlc *dlc) |
| 993 | { |
| 994 | llist_del(&dlc->list); |
| 995 | talloc_free(dlc); |
| 996 | } |
| 997 | |
| 998 | /* TODO: rework osmo_fr_dlc_alloc/free with handling it's own memory. |
| 999 | * For network role: The dlc have to created by the application (e.g. vty). |
| 1000 | * The dlc shouldn't free'd directly. It should be communicated to the |
| 1001 | * other side and wait until it's confirmed OR the link go off and free it afterwards. |
| 1002 | * For user equpment role: The dlc can be created by the application or the dlc will be created |
| 1003 | * by the frame relay because the network is configuring the dlc. |
| 1004 | * The dlc shouldn't be free'd. Only the handler should be set to NULL. |
| 1005 | */ |
| 1006 | |
| 1007 | struct osmo_fr_dlc *osmo_fr_dlc_by_dlci(struct osmo_fr_link *link, uint16_t dlci) |
| 1008 | { |
| 1009 | struct osmo_fr_dlc *dlc; |
| 1010 | |
| 1011 | llist_for_each_entry(dlc, &link->dlc_list, list) { |
| 1012 | if (dlc->dlci == dlci) |
| 1013 | return dlc; |
| 1014 | } |
| 1015 | return NULL; |
| 1016 | } |
Harald Welte | 069967b | 2021-03-30 12:05:31 +0200 | [diff] [blame] | 1017 | |
| 1018 | |
| 1019 | #include <osmocom/vty/vty.h> |
| 1020 | #include <osmocom/vty/tdef_vty.h> |
| 1021 | |
| 1022 | static void fr_dlc_dump_vty(struct vty *vty, const struct osmo_fr_dlc *dlc) |
| 1023 | { |
| 1024 | vty_out(vty, " FR DLC %05u: %s%s%s%s", dlc->dlci, |
| 1025 | dlc->active ? "ACTIVE" : "INACTIVE", |
| 1026 | dlc->add ? " ADDED" : "", dlc->del ? " DELETED" : "", VTY_NEWLINE); |
| 1027 | } |
| 1028 | |
| 1029 | static void fr_link_dump_vty(struct vty *vty, const struct osmo_fr_link *link) |
| 1030 | { |
| 1031 | const struct osmo_fr_dlc *dlc; |
| 1032 | |
| 1033 | vty_out(vty, "FR Link '%s': Role %s, LastRxSeq %u, LastTxSeq %u%s", |
| 1034 | link->name, link->role == FR_ROLE_USER_EQUIPMENT ? "USER" : "NETWORK", |
| 1035 | link->last_rx_seq, link->last_tx_seq, VTY_NEWLINE); |
| 1036 | llist_for_each_entry(dlc, &link->dlc_list, list) { |
| 1037 | fr_dlc_dump_vty(vty, dlc); |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | void osmo_fr_network_dump_vty(struct vty *vty, const struct osmo_fr_network *net) |
| 1042 | { |
| 1043 | struct osmo_fr_link *link; |
| 1044 | |
| 1045 | vty_out(vty, "FR Network: N391 %u, N392 %u, N393 %u%s", |
| 1046 | net->n391, net->n392, net->n393, VTY_NEWLINE); |
| 1047 | osmo_tdef_vty_out_all(vty, net->T_defs, " "); |
| 1048 | llist_for_each_entry(link, &net->links, list) { |
| 1049 | fr_link_dump_vty(vty, link); |
| 1050 | } |
| 1051 | } |