Harald Welte | 9fe1f9f | 2018-11-29 13:47:39 +0100 | [diff] [blame] | 1 | /*! \file iu_up.c |
| 2 | * IuUP (Iu User Plane) according to 3GPP TS 25.415 */ |
| 3 | /* |
| 4 | * (C) 2017 by Harald Welte <laforge@gnumonks.org> |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <inttypes.h> |
| 21 | |
| 22 | #include <osmocom/core/crc8gen.h> |
| 23 | #include <osmocom/core/crc16gen.h> |
| 24 | #include <osmocom/core/fsm.h> |
| 25 | #include <osmocom/core/prim.h> |
| 26 | #include <osmocom/core/timer.h> |
| 27 | #include <osmocom/core/logging.h> |
| 28 | |
| 29 | #include <osmocom/gsm/prim.h> |
| 30 | #include <osmocom/gsm/protocol/gsm_25_415.h> |
| 31 | #include <osmocom/gsm/iuup.h> |
| 32 | |
| 33 | /*********************************************************************** |
| 34 | * CRC Calculation |
| 35 | ***********************************************************************/ |
| 36 | |
| 37 | /* Section 6.6.3.8 Header CRC */ |
| 38 | const struct osmo_crc8gen_code iuup_hdr_crc_code = { |
| 39 | .bits = 6, |
| 40 | .poly = 47, |
| 41 | .init = 0, |
| 42 | .remainder = 0, |
| 43 | }; |
| 44 | |
| 45 | /* Section 6.6.3.9 Payload CRC */ |
| 46 | const struct osmo_crc16gen_code iuup_data_crc_code = { |
| 47 | .bits = 10, |
| 48 | .poly = 563, |
| 49 | .init = 0, |
| 50 | .remainder = 0, |
| 51 | }; |
| 52 | |
| 53 | static int iuup_get_payload_offset(const uint8_t *iuup_pdu) |
| 54 | { |
| 55 | uint8_t pdu_type = iuup_pdu[0] >> 4; |
| 56 | switch (pdu_type) { |
| 57 | case 0: |
| 58 | case 14: |
| 59 | return 4; |
| 60 | case 1: |
| 61 | return 3; |
| 62 | default: |
| 63 | return -1; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | int osmo_iuup_compute_payload_crc(const uint8_t *iuup_pdu, unsigned int pdu_len) |
| 68 | { |
| 69 | ubit_t buf[1024*8]; |
| 70 | uint8_t pdu_type; |
| 71 | int offset, payload_len_bytes; |
| 72 | |
| 73 | if (pdu_len < 1) |
| 74 | return -1; |
| 75 | |
| 76 | pdu_type = iuup_pdu[0] >> 4; |
| 77 | |
| 78 | /* Type 1 has no CRC */ |
| 79 | if (pdu_type == 1) |
| 80 | return 0; |
| 81 | |
| 82 | offset = iuup_get_payload_offset(iuup_pdu); |
| 83 | if (offset < 0) |
| 84 | return offset; |
| 85 | |
| 86 | if (pdu_len < offset) |
| 87 | return -1; |
| 88 | |
| 89 | payload_len_bytes = pdu_len - offset; |
| 90 | osmo_pbit2ubit(buf, iuup_pdu+offset, payload_len_bytes*8); |
| 91 | return osmo_crc16gen_compute_bits(&iuup_data_crc_code, buf, payload_len_bytes*8); |
| 92 | } |
| 93 | |
| 94 | int osmo_iuup_compute_header_crc(const uint8_t *iuup_pdu, unsigned int pdu_len) |
| 95 | { |
| 96 | ubit_t buf[2*8]; |
| 97 | |
| 98 | if (pdu_len < 2) |
| 99 | return -1; |
| 100 | |
| 101 | osmo_pbit2ubit(buf, iuup_pdu, 2*8); |
| 102 | return osmo_crc8gen_compute_bits(&iuup_hdr_crc_code, buf, 2*8); |
| 103 | } |
| 104 | |
| 105 | /*********************************************************************** |
| 106 | * Internal State / FSM (Annex B) |
| 107 | ***********************************************************************/ |
| 108 | |
| 109 | #define S(x) (1 << (x)) |
| 110 | |
| 111 | #define IUUP_TIMER_INIT 1 |
| 112 | #define IUUP_TIMER_TA 2 |
| 113 | #define IUUP_TIMER_RC 3 |
| 114 | |
| 115 | struct osmo_timer_nt { |
| 116 | uint32_t n; /* number of repetitions */ |
| 117 | struct osmo_iuup_tnl_prim *retrans_itp; |
| 118 | struct osmo_timer_list timer; |
| 119 | }; |
| 120 | |
| 121 | struct osmo_iuup_instance { |
| 122 | struct osmo_iuup_rnl_config config; |
| 123 | struct osmo_fsm_inst *fi; |
| 124 | |
| 125 | uint8_t mode_version; |
| 126 | |
| 127 | struct { |
| 128 | struct osmo_timer_nt init; |
| 129 | struct osmo_timer_nt ta; |
| 130 | struct osmo_timer_nt rc; |
| 131 | } timer; |
| 132 | /* call-back function to pass primitives up to the user */ |
| 133 | osmo_prim_cb user_prim_cb; |
| 134 | void *user_prim_priv; |
| 135 | osmo_prim_cb transport_prim_cb; |
| 136 | void *transport_prim_priv; |
| 137 | uint8_t type14_fn; /* 2 bits */ |
| 138 | }; |
| 139 | |
| 140 | enum iuup_fsm_state { |
| 141 | IUUP_FSM_ST_NULL, |
| 142 | IUUP_FSM_ST_INIT, |
| 143 | IUUP_FSM_ST_TrM_DATA_XFER_READY, |
| 144 | IUUP_FSM_ST_SMpSDU_DATA_XFER_READY, |
| 145 | }; |
| 146 | |
| 147 | enum iuup_fsm_event { |
| 148 | IUUP_FSM_EVT_IUUP_CONFIG_REQ, |
| 149 | IUUP_FSM_EVT_IUUP_DATA_REQ, |
| 150 | IUUP_FSM_EVT_IUUP_DATA_IND, |
| 151 | IUUP_FSM_EVT_IUUP_STATUS_REQ, |
| 152 | IUUP_FSM_EVT_IUUP_STATUS_IND, |
| 153 | IUUP_FSM_EVT_SSASAR_UNITDATA_REQ, |
| 154 | IUUP_FSM_EVT_SSASAR_UNITDATA_IND, |
| 155 | IUUP_FSM_EVT_IUUP_UNITDATA_REQ, |
| 156 | IUUP_FSM_EVT_IUUP_UNITDATA_IND, |
| 157 | IUUP_FSM_EVT_INIT, |
| 158 | IUUP_FSM_EVT_LAST_INIT_ACK, |
| 159 | IUUP_FSM_EVT_INIT_NACK, |
| 160 | }; |
| 161 | |
| 162 | static const struct value_string iuup_fsm_event_names[] = { |
Philipp Maier | bdd7df3 | 2022-01-18 11:58:18 +0100 | [diff] [blame] | 163 | { IUUP_FSM_EVT_IUUP_CONFIG_REQ, "IuUP-CONFIG-req" }, |
| 164 | { IUUP_FSM_EVT_IUUP_DATA_REQ, "IuUP-DATA-req" }, |
| 165 | { IUUP_FSM_EVT_IUUP_DATA_IND, "IuUP-DATA-ind" }, |
| 166 | { IUUP_FSM_EVT_IUUP_STATUS_REQ, "IuUP-STATUS-req" }, |
| 167 | { IUUP_FSM_EVT_IUUP_STATUS_IND, "IuUP-STATUS-ind" }, |
| 168 | { IUUP_FSM_EVT_SSASAR_UNITDATA_REQ, "SSSAR-UNITDATA-req" }, |
| 169 | { IUUP_FSM_EVT_SSASAR_UNITDATA_IND, "SSSAR-UNITDATA-ind" }, |
| 170 | { IUUP_FSM_EVT_IUUP_UNITDATA_REQ, "IuUP-UNITDATA-req" }, |
| 171 | { IUUP_FSM_EVT_IUUP_UNITDATA_IND, "IuUP-UNITDATA-ind" }, |
Harald Welte | 9fe1f9f | 2018-11-29 13:47:39 +0100 | [diff] [blame] | 172 | { IUUP_FSM_EVT_INIT, "INIT" }, |
| 173 | { IUUP_FSM_EVT_LAST_INIT_ACK, "LAST_INIT_ACK" }, |
| 174 | { IUUP_FSM_EVT_INIT_NACK, "INIT_NACK" }, |
| 175 | { 0, NULL } |
| 176 | }; |
| 177 | |
| 178 | static inline uint8_t iuup_get_pdu_type(const uint8_t *data) |
| 179 | { |
| 180 | return data[0] >> 4; |
| 181 | } |
| 182 | |
| 183 | static inline uint8_t iuup_get_hdr_crc(const uint8_t *data) |
| 184 | { |
| 185 | return data[2] >> 2; |
| 186 | } |
| 187 | |
| 188 | /* Helper functions to store non-packed structs in msgb so that pointers are properly aligned: */ |
| 189 | #define IUUP_MSGB_SIZE 4096 |
| 190 | #define PTR_ALIGNMENT_BYTES 8 |
| 191 | #define IUUP_MSGB_HEADROOM_MIN_REQUIRED (OSMO_MAX(sizeof(struct osmo_iuup_tnl_prim), sizeof(struct osmo_iuup_rnl_prim)) + (PTR_ALIGNMENT_BYTES - 1)) |
| 192 | static inline struct msgb *osmo_iuup_msgb_alloc_c(void *ctx, size_t size) |
| 193 | { |
| 194 | osmo_static_assert(size > IUUP_MSGB_HEADROOM_MIN_REQUIRED, iuup_msgb_alloc_headroom_bigger); |
| 195 | return msgb_alloc_headroom_c(ctx, size, IUUP_MSGB_HEADROOM_MIN_REQUIRED, "iuup-msgb"); |
| 196 | } |
| 197 | |
| 198 | /* push data so that the resulting pointer to write to is aligned to 8 byte */ |
| 199 | static inline __attribute__((assume_aligned(PTR_ALIGNMENT_BYTES))) |
| 200 | unsigned char *aligned_msgb_push(struct msgb *msg, unsigned int len) |
| 201 | { |
| 202 | uint8_t *ptr = (msgb_data(msg) - len); |
| 203 | size_t extra_size = ((uintptr_t)ptr & (PTR_ALIGNMENT_BYTES - 1)); |
| 204 | |
| 205 | return msgb_push(msg, len + extra_size); |
| 206 | } |
| 207 | |
| 208 | struct osmo_iuup_rnl_prim *osmo_iuup_rnl_prim_alloc(void *ctx, unsigned int primitive, unsigned int operation, unsigned int size) |
| 209 | { |
| 210 | struct msgb *msg; |
| 211 | struct osmo_iuup_rnl_prim *irp; |
| 212 | |
| 213 | msg = osmo_iuup_msgb_alloc_c(ctx, size); |
| 214 | irp = (struct osmo_iuup_rnl_prim *)aligned_msgb_push(msg, sizeof(*irp)); |
| 215 | osmo_prim_init(&irp->oph, SAP_IUUP_RNL, primitive, operation, msg); |
| 216 | return irp; |
| 217 | } |
| 218 | |
| 219 | struct osmo_iuup_tnl_prim *osmo_iuup_tnl_prim_alloc(void *ctx, unsigned int primitive, unsigned int operation, unsigned int size) |
| 220 | { |
| 221 | struct msgb *msg; |
| 222 | struct osmo_iuup_tnl_prim *itp; |
| 223 | |
| 224 | msg = osmo_iuup_msgb_alloc_c(ctx, size); |
| 225 | itp = (struct osmo_iuup_tnl_prim *) aligned_msgb_push(msg, sizeof(*itp)); |
| 226 | osmo_prim_init(&itp->oph, SAP_IUUP_TNL, primitive, operation, msg); |
| 227 | return itp; |
| 228 | } |
| 229 | |
| 230 | /* 6.6.2.3.2 */ |
| 231 | static struct osmo_iuup_tnl_prim *itp_ctrl_ack_alloc(struct osmo_iuup_instance *iui, enum iuup_procedure proc_ind, uint8_t fn) |
| 232 | { |
| 233 | struct osmo_iuup_tnl_prim *itp; |
| 234 | struct iuup_ctrl_ack *ack; |
| 235 | itp = osmo_iuup_tnl_prim_alloc(iui, OSMO_IUUP_TNL_UNITDATA, PRIM_OP_REQUEST, IUUP_MSGB_SIZE); |
| 236 | itp->oph.msg->l2h = msgb_put(itp->oph.msg, sizeof(struct iuup_ctrl_ack)); |
| 237 | ack = (struct iuup_ctrl_ack *) msgb_l2(itp->oph.msg); |
| 238 | *ack = (struct iuup_ctrl_ack){ |
| 239 | .hdr = { |
| 240 | .frame_nr = fn, |
| 241 | .ack_nack = IUUP_AN_ACK, |
| 242 | .pdu_type = IUUP_PDU_T_CONTROL, |
| 243 | .proc_ind = proc_ind, |
| 244 | .mode_version = iui->mode_version, |
| 245 | .payload_crc_hi = 0, |
| 246 | .header_crc = 0, |
| 247 | .payload_crc_lo = 0, |
| 248 | }, |
| 249 | }; |
| 250 | ack->hdr.header_crc = osmo_iuup_compute_header_crc(msgb_l2(itp->oph.msg), msgb_l2len(itp->oph.msg)); |
| 251 | return itp; |
| 252 | } |
| 253 | |
| 254 | /* 6.6.2.3.3 */ |
| 255 | static struct osmo_iuup_tnl_prim *tnp_ctrl_nack_alloc(struct osmo_iuup_instance *iui, enum iuup_procedure proc_ind, enum iuup_error_cause error_cause, uint8_t fn) |
| 256 | { |
| 257 | struct osmo_iuup_tnl_prim *itp; |
| 258 | struct iuup_ctrl_nack *nack; |
| 259 | itp = osmo_iuup_tnl_prim_alloc(iui, OSMO_IUUP_TNL_UNITDATA, PRIM_OP_REQUEST, IUUP_MSGB_SIZE); |
| 260 | itp->oph.msg->l2h = msgb_put(itp->oph.msg, sizeof(struct iuup_ctrl_nack)); |
| 261 | nack = (struct iuup_ctrl_nack *) msgb_l2(itp->oph.msg); |
| 262 | *nack = (struct iuup_ctrl_nack){ |
| 263 | .hdr = { |
| 264 | .frame_nr = fn, |
| 265 | .ack_nack = IUUP_AN_NACK, |
| 266 | .pdu_type = IUUP_PDU_T_CONTROL, |
| 267 | .proc_ind = proc_ind, |
| 268 | .mode_version = iui->mode_version, |
| 269 | .payload_crc_hi = 0, |
| 270 | .header_crc = 0, |
| 271 | .payload_crc_lo = 0, |
| 272 | }, |
| 273 | .spare = 0, |
| 274 | .error_cause = error_cause, |
| 275 | }; |
| 276 | nack->hdr.header_crc = osmo_iuup_compute_header_crc(msgb_l2(itp->oph.msg), msgb_l2len(itp->oph.msg)); |
| 277 | return itp; |
| 278 | } |
| 279 | |
| 280 | /* 6.6.2.3.4.1 */ |
| 281 | static struct osmo_iuup_tnl_prim *tnp_ctrl_init_alloc(struct osmo_iuup_instance *iui) |
| 282 | { |
| 283 | struct osmo_iuup_tnl_prim *itp; |
| 284 | struct iuup_pdutype14_hdr *hdr; |
| 285 | struct iuup_ctrl_init_hdr *ihdr; |
| 286 | struct iuup_ctrl_init_rfci_hdr *ihdr_rfci; |
| 287 | struct iuup_ctrl_init_tail *itail; |
| 288 | unsigned int i, j; |
| 289 | uint8_t num_subflows, num_rfci; |
| 290 | uint16_t payload_crc; |
| 291 | struct msgb *msg; |
| 292 | |
| 293 | num_subflows = iui->config.num_subflows; |
| 294 | num_rfci = iui->config.num_rfci; |
| 295 | |
| 296 | itp = osmo_iuup_tnl_prim_alloc(iui, OSMO_IUUP_TNL_UNITDATA, PRIM_OP_REQUEST, IUUP_MSGB_SIZE); |
| 297 | msg = itp->oph.msg; |
| 298 | |
| 299 | msg->l2h = msgb_put(msg, sizeof(*hdr)); |
| 300 | hdr = (struct iuup_pdutype14_hdr *)msgb_l2(msg); |
| 301 | hdr->frame_nr = iui->type14_fn++; |
| 302 | hdr->ack_nack = IUUP_AN_PROCEDURE; |
| 303 | hdr->pdu_type = IUUP_PDU_T_CONTROL; |
| 304 | hdr->proc_ind = IUUP_PROC_INIT; |
| 305 | hdr->mode_version = 0; /* Use here the minimum version required to negotiate */ |
| 306 | hdr->header_crc = osmo_iuup_compute_header_crc(msgb_l2(msg), msgb_l2len(msg)); |
| 307 | |
| 308 | ihdr = (struct iuup_ctrl_init_hdr *)msgb_put(msg, sizeof(*ihdr)); |
| 309 | ihdr->chain_ind = 0; /* this frame is the last frame for the procedure. TODO: support several */ |
| 310 | ihdr->num_subflows_per_rfci = num_subflows; |
| 311 | ihdr->ti = iui->config.IPTIs_present ? 1 : 0; |
| 312 | ihdr->spare = 0; |
| 313 | |
| 314 | /* RFCI + subflow size part: */ |
| 315 | for (i = 0; i < num_rfci; i++) { |
| 316 | bool last = (i+1 == num_rfci); |
| 317 | uint8_t len_size = 1; |
| 318 | for (j = 0; j < num_subflows; j++) { |
| 319 | if (iui->config.subflow_sizes[i][j] > UINT8_MAX) |
| 320 | len_size = 2; |
| 321 | } |
| 322 | ihdr_rfci = (struct iuup_ctrl_init_rfci_hdr *)msgb_put(msg, sizeof(*ihdr_rfci) + len_size * num_subflows); |
| 323 | ihdr_rfci->rfci = i; |
| 324 | ihdr_rfci->li = len_size - 1; |
| 325 | ihdr_rfci->lri = last; |
| 326 | if (len_size == 2) { |
| 327 | uint16_t *buf = (uint16_t *)&ihdr_rfci->subflow_length[0]; |
| 328 | for (j = 0; j < num_subflows; j++) |
| 329 | osmo_store16be(iui->config.subflow_sizes[i][j], buf++); |
| 330 | } else { |
| 331 | for (j = 0; j < num_subflows; j++) |
| 332 | ihdr_rfci->subflow_length[j] = iui->config.subflow_sizes[i][j]; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | if (iui->config.IPTIs_present) { |
| 337 | uint8_t num_bytes = (num_rfci + 1) / 2; |
| 338 | uint8_t *buf = msgb_put(msg, num_bytes); |
| 339 | for (i = 0; i < num_bytes - 1; i++) |
| 340 | buf[i] = iui->config.IPTIs[i*2] << 4 | |
| 341 | (iui->config.IPTIs[i*2 + 1] & 0x0f); |
| 342 | buf[i] = iui->config.IPTIs[i*2] << 4; |
| 343 | if (!(num_rfci & 0x01)) /* is even: */ |
| 344 | buf[i] |= (iui->config.IPTIs[i*2 + 1] & 0x0f); |
| 345 | |
| 346 | } |
| 347 | |
| 348 | itail = (struct iuup_ctrl_init_tail *)msgb_put(msg, sizeof(*itail)); |
| 349 | osmo_store16be(iui->config.supported_versions_mask, &itail->versions_supported); |
| 350 | itail->spare = 0; |
| 351 | itail->data_pdu_type = iui->config.data_pdu_type; |
| 352 | |
| 353 | payload_crc = osmo_iuup_compute_payload_crc(msgb_l2(msg), msgb_l2len(msg)); |
| 354 | hdr->payload_crc_hi = (payload_crc >> 8) & 0x03; |
| 355 | hdr->payload_crc_lo = payload_crc & 0xff; |
| 356 | |
| 357 | |
| 358 | return itp; |
| 359 | } |
| 360 | |
Pau Espin Pedrol | 604eaba | 2022-01-03 16:57:45 +0100 | [diff] [blame] | 361 | static struct osmo_iuup_rnl_prim *irp_init_ind_alloc(struct osmo_iuup_instance *iui) |
| 362 | { |
| 363 | struct osmo_iuup_rnl_prim *irp; |
| 364 | |
| 365 | irp = osmo_iuup_rnl_prim_alloc(iui, OSMO_IUUP_RNL_STATUS, PRIM_OP_INDICATION, IUUP_MSGB_SIZE); |
| 366 | irp->u.status.procedure = IUUP_PROC_INIT; |
| 367 | irp->u.status.u.initialization.mode_version = iui->mode_version; |
| 368 | irp->u.status.u.initialization.data_pdu_type = iui->config.data_pdu_type; |
| 369 | irp->u.status.u.initialization.num_subflows = iui->config.num_subflows; |
| 370 | irp->u.status.u.initialization.num_rfci = iui->config.num_rfci; |
| 371 | memcpy(irp->u.status.u.initialization.subflow_sizes, iui->config.subflow_sizes, |
| 372 | IUUP_MAX_RFCIS * IUUP_MAX_SUBFLOWS * sizeof(iui->config.subflow_sizes[0][0])); |
| 373 | irp->u.status.u.initialization.IPTIs_present = iui->config.IPTIs_present; |
| 374 | if (iui->config.IPTIs_present) |
| 375 | memcpy(irp->u.status.u.initialization.IPTIs, iui->config.IPTIs, |
| 376 | IUUP_MAX_RFCIS * sizeof(iui->config.IPTIs[0])); |
| 377 | return irp; |
| 378 | } |
| 379 | |
Harald Welte | 9fe1f9f | 2018-11-29 13:47:39 +0100 | [diff] [blame] | 380 | /* transform a RNL data primitive into a TNL data primitive (down the stack) */ |
| 381 | static struct osmo_iuup_tnl_prim *rnl_to_tnl_data(struct osmo_iuup_instance *iui, |
| 382 | struct osmo_iuup_rnl_prim *irp) |
| 383 | { |
| 384 | struct osmo_iuup_tnl_prim *itp; |
| 385 | struct osmo_iuup_rnl_data dt; |
| 386 | struct msgb *msg; |
| 387 | uint16_t payload_crc; |
| 388 | struct iuup_pdutype0_hdr *h0; |
| 389 | struct iuup_pdutype1_hdr *h1; |
| 390 | |
| 391 | OSMO_ASSERT(OSMO_PRIM_HDR(&irp->oph) == OSMO_PRIM(OSMO_IUUP_RNL_DATA, PRIM_OP_REQUEST)); |
| 392 | |
| 393 | msg = irp->oph.msg; |
| 394 | dt = irp->u.data; |
| 395 | |
| 396 | /* pull up to the IuUP payload and push a new primitive header in front */ |
| 397 | msgb_pull_to_l3(msg); |
| 398 | |
| 399 | /* push the PDU TYPE 0 / 1 header in front of the payload */ |
| 400 | switch (iui->config.data_pdu_type) { |
| 401 | case 0: |
| 402 | msg->l2h = msgb_push(msg, sizeof(*h0)); |
| 403 | h0 = (struct iuup_pdutype0_hdr *)msg->l2h; |
| 404 | h0->frame_nr = dt.frame_nr; |
| 405 | h0->pdu_type = IUUP_PDU_T_DATA_CRC; |
| 406 | h0->rfci = dt.rfci; |
| 407 | h0->fqc = dt.fqc; |
| 408 | h0->header_crc = osmo_iuup_compute_header_crc(msgb_l2(msg), msgb_l2len(msg)); |
| 409 | payload_crc = osmo_iuup_compute_payload_crc(msgb_l2(msg), msgb_l2len(msg)); |
| 410 | h0->payload_crc_hi = (payload_crc >> 8) & 0x03; |
| 411 | h0->payload_crc_lo = payload_crc & 0xff; |
| 412 | break; |
| 413 | case 1: |
| 414 | msg->l2h = msgb_push(msg, sizeof(*h1)); |
| 415 | h1 = (struct iuup_pdutype1_hdr *)msg->l2h; |
| 416 | h1->frame_nr = dt.frame_nr; |
| 417 | h1->pdu_type = IUUP_PDU_T_DATA_NOCRC; |
| 418 | h1->rfci = dt.rfci; |
| 419 | h1->fqc = dt.fqc; |
| 420 | h1->header_crc = osmo_iuup_compute_header_crc(msgb_l2(msg), msgb_l2len(msg)); |
| 421 | h1->spare = 0; |
| 422 | break; |
| 423 | default: |
| 424 | OSMO_ASSERT(0); |
| 425 | } |
| 426 | |
| 427 | /* Avoid allocating irp out of 8byte-aligned address, Asan is not happy with it */ |
| 428 | itp = (struct osmo_iuup_tnl_prim *) aligned_msgb_push(msg, sizeof(*itp)); |
| 429 | osmo_prim_init(&itp->oph, SAP_IUUP_TNL, OSMO_IUUP_TNL_UNITDATA, PRIM_OP_REQUEST, msg); |
| 430 | |
| 431 | return itp; |
| 432 | } |
| 433 | |
| 434 | /* transform a TNL primitive into a RNL primitive (up the stack) */ |
| 435 | static struct osmo_iuup_rnl_prim *tnl_to_rnl_data(struct osmo_iuup_tnl_prim *itp) |
| 436 | { |
| 437 | struct msgb *msg; |
| 438 | struct iuup_pdutype0_hdr *h0; |
| 439 | struct iuup_pdutype1_hdr *h1; |
| 440 | struct osmo_iuup_rnl_data dt; |
| 441 | struct osmo_iuup_rnl_prim *irp; |
| 442 | |
| 443 | msg = itp->oph.msg; |
| 444 | |
| 445 | OSMO_ASSERT(OSMO_PRIM_HDR(&itp->oph) == OSMO_PRIM(OSMO_IUUP_TNL_UNITDATA, PRIM_OP_INDICATION)); |
| 446 | |
| 447 | switch (iuup_get_pdu_type(msgb_l2(msg))) { |
| 448 | case IUUP_PDU_T_DATA_CRC: |
| 449 | h0 = (struct iuup_pdutype0_hdr *) msgb_l2(msg); |
| 450 | dt.rfci = h0->rfci; |
| 451 | dt.frame_nr = h0->frame_nr; |
| 452 | dt.fqc = h0->fqc; |
| 453 | break; |
| 454 | case IUUP_PDU_T_DATA_NOCRC: |
| 455 | h1 = (struct iuup_pdutype1_hdr *) msgb_l2(msg); |
| 456 | dt.rfci = h1->rfci; |
| 457 | dt.frame_nr = h1->frame_nr; |
| 458 | dt.fqc = h1->fqc; |
| 459 | break; |
| 460 | } |
| 461 | |
| 462 | /* pull up to the IuUP payload and push a new primitive header in front */ |
| 463 | msgb_pull_to_l3(msg); |
| 464 | |
| 465 | /* Avoid allocating irp out of 8byte-aligned address, Asan is not happy with it */ |
| 466 | irp = (struct osmo_iuup_rnl_prim *) aligned_msgb_push(msg, sizeof(*irp)); |
| 467 | osmo_prim_init(&irp->oph, SAP_IUUP_RNL, OSMO_IUUP_RNL_DATA, PRIM_OP_INDICATION, msg); |
| 468 | irp->u.data = dt; |
| 469 | |
| 470 | return irp; |
| 471 | } |
| 472 | |
| 473 | static struct osmo_iuup_rnl_prim *irp_error_event_alloc_c(void *ctx, enum iuup_error_cause cause, enum iuup_error_distance distance) |
| 474 | { |
| 475 | struct osmo_iuup_rnl_prim *irp; |
| 476 | struct msgb *msg; |
| 477 | msg = msgb_alloc_c(ctx, sizeof(*irp), "iuup-tx"); |
| 478 | irp = (struct osmo_iuup_rnl_prim *) msgb_put(msg, sizeof(*irp)); |
| 479 | osmo_prim_init(&irp->oph, SAP_IUUP_RNL, OSMO_IUUP_RNL_STATUS, PRIM_OP_INDICATION, msg); |
| 480 | irp->u.status.procedure = IUUP_PROC_ERR_EVENT; |
| 481 | irp->u.status.u.error_event.cause = cause; |
| 482 | irp->u.status.u.error_event.distance = distance; |
| 483 | return irp; |
| 484 | } |
| 485 | |
| 486 | static struct osmo_iuup_tnl_prim *itp_copy_c(void *ctx, const struct osmo_iuup_tnl_prim *src_itp) |
| 487 | { |
| 488 | struct msgb *msg; |
| 489 | struct osmo_iuup_tnl_prim *dst_itp; |
| 490 | |
| 491 | msg = msgb_copy_c(ctx, src_itp->oph.msg, "iuup-tx-retrans"); |
| 492 | dst_itp = (struct osmo_iuup_tnl_prim *)msgb_data(msg); |
| 493 | dst_itp->oph.msg = msg; |
| 494 | return dst_itp; |
| 495 | } |
| 496 | |
| 497 | static void retransmit_initialization(struct osmo_iuup_instance *iui) |
| 498 | { |
| 499 | struct osmo_iuup_tnl_prim *itp; |
| 500 | iui->fi->T = IUUP_TIMER_INIT; |
| 501 | osmo_timer_schedule(&iui->fi->timer, iui->config.t_init.t_ms / 1000, (iui->config.t_init.t_ms % 1000) * 1000); |
| 502 | itp = itp_copy_c(iui, iui->timer.init.retrans_itp); |
| 503 | iui->transport_prim_cb(&itp->oph, iui->transport_prim_priv); |
| 504 | } |
| 505 | |
| 506 | /* return: whether the last Init was Acked correctly and hence can transition to next state */ |
| 507 | static bool iuup_rx_initialization(struct osmo_iuup_instance *iui, struct osmo_iuup_tnl_prim *itp) |
| 508 | { |
| 509 | struct iuup_pdutype14_hdr *hdr; |
| 510 | struct iuup_ctrl_init_hdr *ihdr; |
| 511 | struct iuup_ctrl_init_rfci_hdr *ihdr_rfci; |
| 512 | struct iuup_ctrl_init_tail *itail; |
| 513 | enum iuup_error_cause err_cause; |
| 514 | uint8_t num_rfci = 0; |
Harald Welte | 29814a5 | 2021-12-24 11:35:57 +0100 | [diff] [blame] | 515 | int i; |
Harald Welte | 9fe1f9f | 2018-11-29 13:47:39 +0100 | [diff] [blame] | 516 | bool is_last; |
| 517 | uint16_t remote_mask, match_mask; |
Pau Espin Pedrol | 604eaba | 2022-01-03 16:57:45 +0100 | [diff] [blame] | 518 | struct osmo_iuup_rnl_prim *irp; |
Harald Welte | 9fe1f9f | 2018-11-29 13:47:39 +0100 | [diff] [blame] | 519 | struct osmo_iuup_tnl_prim *resp; |
| 520 | |
| 521 | /* TODO: whenever we check message boundaries, length, etc. and we fail, send NACK */ |
| 522 | |
| 523 | hdr = (struct iuup_pdutype14_hdr *)msgb_l2(itp->oph.msg); |
| 524 | ihdr = (struct iuup_ctrl_init_hdr *)hdr->payload; |
| 525 | if (ihdr->num_subflows_per_rfci == 0) { |
| 526 | LOGPFSML(iui->fi, LOGL_NOTICE, "Initialization: Unexpected num_subflows=0 received\n"); |
| 527 | err_cause = IUUP_ERR_CAUSE_UNEXPECTED_VALUE; |
| 528 | goto send_nack; |
| 529 | } |
| 530 | ihdr_rfci = (struct iuup_ctrl_init_rfci_hdr *)ihdr->rfci_data; |
| 531 | |
| 532 | do { |
| 533 | uint8_t l_size_bytes = ihdr_rfci->li + 1; |
| 534 | is_last = ihdr_rfci->lri; |
| 535 | if (ihdr_rfci->rfci != num_rfci) { |
| 536 | LOGPFSML(iui->fi, LOGL_NOTICE, "Initialization: Unexpected RFCI %u at position %u received\n", |
| 537 | ihdr_rfci->rfci, num_rfci); |
| 538 | err_cause = IUUP_ERR_CAUSE_UNEXPECTED_RFCI; |
| 539 | goto send_nack; |
| 540 | } |
| 541 | if (l_size_bytes == 2) { |
| 542 | uint16_t *subflow_size = (uint16_t *)ihdr_rfci->subflow_length; |
| 543 | for (i = 0; i < ihdr->num_subflows_per_rfci; i++) { |
| 544 | iui->config.subflow_sizes[ihdr_rfci->rfci][i] = osmo_load16be(subflow_size); |
| 545 | subflow_size++; |
| 546 | } |
| 547 | } else { |
| 548 | uint8_t *subflow_size = ihdr_rfci->subflow_length; |
| 549 | for (i = 0; i < ihdr->num_subflows_per_rfci; i++) { |
Pau Espin Pedrol | d3b016f | 2022-01-05 17:29:06 +0100 | [diff] [blame] | 550 | iui->config.subflow_sizes[ihdr_rfci->rfci][i] = *subflow_size; |
Harald Welte | 9fe1f9f | 2018-11-29 13:47:39 +0100 | [diff] [blame] | 551 | subflow_size++; |
| 552 | } |
| 553 | } |
| 554 | num_rfci++; |
| 555 | ihdr_rfci++; |
| 556 | ihdr_rfci = (struct iuup_ctrl_init_rfci_hdr *)(((uint8_t *)ihdr_rfci) + ihdr->num_subflows_per_rfci * l_size_bytes); |
| 557 | } while (!is_last); |
| 558 | |
| 559 | if (ihdr->ti) { /* Timing information present */ |
| 560 | uint8_t *buf = (uint8_t *)ihdr_rfci; |
| 561 | uint8_t num_bytes = (num_rfci + 1) / 2; |
| 562 | iui->config.IPTIs_present = true; |
| 563 | for (i = 0; i < num_bytes - 1; i++) { |
| 564 | iui->config.IPTIs[i*2] = *buf >> 4; |
| 565 | iui->config.IPTIs[i*2 + 1] = *buf & 0x0f; |
| 566 | buf++; |
| 567 | } |
| 568 | iui->config.IPTIs[i*2] = *buf >> 4; |
| 569 | if (!(num_rfci & 0x01)) /* is even: */ |
| 570 | iui->config.IPTIs[i*2 + 1] = *buf & 0x0f; |
| 571 | buf++; |
| 572 | itail = (struct iuup_ctrl_init_tail *)buf; |
| 573 | } else { |
| 574 | itail = (struct iuup_ctrl_init_tail *)ihdr_rfci; |
| 575 | } |
| 576 | |
| 577 | if (itail->data_pdu_type > 1) { |
| 578 | LOGPFSML(iui->fi, LOGL_NOTICE, "Initialization: Unexpected Data PDU Type %u received\n", itail->data_pdu_type); |
| 579 | err_cause = IUUP_ERR_CAUSE_UNEXPECTED_VALUE; |
| 580 | goto send_nack; |
| 581 | } |
| 582 | |
| 583 | remote_mask = osmo_load16be(&itail->versions_supported); |
| 584 | match_mask = (remote_mask & iui->config.supported_versions_mask); |
| 585 | if (match_mask == 0x0000) { |
| 586 | LOGPFSML(iui->fi, LOGL_NOTICE, |
| 587 | "Initialization: No match in supported versions local=0x%04x vs remote=0x%04x\n", |
| 588 | iui->config.supported_versions_mask, remote_mask); |
| 589 | err_cause = IUUP_ERR_CAUSE_UNEXPECTED_VALUE; |
| 590 | goto send_nack; |
| 591 | } |
| 592 | for (i = 15; i >= 0; i--) { |
| 593 | if (match_mask & (1<<i)) { |
| 594 | iui->mode_version = i; |
| 595 | break; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | iui->config.num_rfci = num_rfci; |
| 600 | iui->config.num_subflows = ihdr->num_subflows_per_rfci; |
| 601 | iui->config.data_pdu_type = itail->data_pdu_type; |
| 602 | |
Pau Espin Pedrol | 604eaba | 2022-01-03 16:57:45 +0100 | [diff] [blame] | 603 | irp = irp_init_ind_alloc(iui); |
| 604 | iui->user_prim_cb(&irp->oph, iui->user_prim_priv); |
| 605 | |
Harald Welte | 9fe1f9f | 2018-11-29 13:47:39 +0100 | [diff] [blame] | 606 | LOGPFSML(iui->fi, LOGL_DEBUG, "Tx Initialization ACK\n"); |
| 607 | resp = itp_ctrl_ack_alloc(iui, IUUP_PROC_INIT, hdr->frame_nr); |
| 608 | iui->transport_prim_cb(&resp->oph, iui->transport_prim_priv); |
| 609 | return ihdr->chain_ind == 0; |
| 610 | send_nack: |
| 611 | LOGPFSML(iui->fi, LOGL_NOTICE, "Tx Initialization NACK cause=%u orig_message=%s\n", |
| 612 | err_cause, osmo_hexdump((const unsigned char *) msgb_l2(itp->oph.msg), msgb_l2len(itp->oph.msg))); |
| 613 | resp = tnp_ctrl_nack_alloc(iui, IUUP_PROC_INIT, err_cause, hdr->frame_nr); |
| 614 | iui->transport_prim_cb(&resp->oph, iui->transport_prim_priv); |
| 615 | return false; |
| 616 | } |
| 617 | |
| 618 | /********************** |
| 619 | * FSM STATE FUNCTIONS |
| 620 | **********************/ |
| 621 | static void iuup_fsm_null(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 622 | { |
| 623 | struct osmo_iuup_instance *iui = fi->priv; |
| 624 | struct osmo_iuup_rnl_prim *user_prim = NULL; |
| 625 | |
| 626 | switch (event) { |
| 627 | case IUUP_FSM_EVT_IUUP_CONFIG_REQ: |
| 628 | user_prim = data; |
| 629 | iui->config = user_prim->u.config; |
| 630 | iui->config.supported_versions_mask &= 0x0003; /* We only support versions 1 and 2 ourselves */ |
| 631 | //TODO: if supported_versions_mask == 0x0000,no supported versions, send error to upper layers |
| 632 | |
| 633 | if (iui->config.transparent) |
| 634 | osmo_fsm_inst_state_chg(fi, IUUP_FSM_ST_TrM_DATA_XFER_READY, 0, 0); |
| 635 | else { |
| 636 | osmo_fsm_inst_state_chg(fi, IUUP_FSM_ST_INIT, 0, 0); |
| 637 | } |
| 638 | break; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | /* transparent mode data transfer */ |
| 643 | static void iuup_fsm_trm_data(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 644 | { |
| 645 | //struct osmo_iuup_instance *iui = fi->priv; |
| 646 | |
| 647 | switch (event) { |
| 648 | case IUUP_FSM_EVT_IUUP_CONFIG_REQ: |
| 649 | osmo_fsm_inst_state_chg(fi, IUUP_FSM_ST_NULL, 0, 0); |
| 650 | break; |
| 651 | case IUUP_FSM_EVT_IUUP_DATA_REQ: |
| 652 | /* Data coming down from RNL (user) towards TNL (transport) */ |
| 653 | break; |
| 654 | case IUUP_FSM_EVT_IUUP_DATA_IND: |
| 655 | /* Data coming up from TNL (transport) towards RNL (user) */ |
| 656 | break; |
| 657 | case IUUP_FSM_EVT_IUUP_UNITDATA_REQ: |
| 658 | case IUUP_FSM_EVT_IUUP_UNITDATA_IND: |
| 659 | case IUUP_FSM_EVT_SSASAR_UNITDATA_REQ: |
| 660 | case IUUP_FSM_EVT_SSASAR_UNITDATA_IND: |
| 661 | /* no state change */ |
| 662 | break; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | static void iuup_fsm_init_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 667 | { |
| 668 | struct osmo_iuup_instance *iui = fi->priv; |
| 669 | |
| 670 | iui->type14_fn = 0; |
| 671 | if (iui->config.active) { |
| 672 | iui->timer.init.n = 0; |
| 673 | iui->timer.init.retrans_itp = tnp_ctrl_init_alloc(iui); |
| 674 | retransmit_initialization(iui); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | static void iuup_fsm_init(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 679 | { |
| 680 | struct osmo_iuup_instance *iui = fi->priv; |
| 681 | struct osmo_iuup_rnl_prim *irp; |
| 682 | struct osmo_iuup_tnl_prim *itp; |
| 683 | |
| 684 | switch (event) { |
| 685 | case IUUP_FSM_EVT_IUUP_CONFIG_REQ: |
| 686 | /* the only permitted 'config req' type is the request to release the instance */ |
| 687 | osmo_fsm_inst_state_chg(fi, IUUP_FSM_ST_NULL, 0, 0); |
| 688 | break; |
| 689 | case IUUP_FSM_EVT_INIT: |
| 690 | itp = data; |
| 691 | if (iuup_rx_initialization(iui, itp)) |
| 692 | osmo_fsm_inst_state_chg(fi, IUUP_FSM_ST_SMpSDU_DATA_XFER_READY, 0, 0); |
| 693 | break; |
| 694 | case IUUP_FSM_EVT_LAST_INIT_ACK: |
| 695 | /* last INIT ACK was received, transition to DATA_XFER_READY state */ |
| 696 | osmo_fsm_inst_state_chg(fi, IUUP_FSM_ST_SMpSDU_DATA_XFER_READY, 0, 0); |
| 697 | break; |
| 698 | case IUUP_FSM_EVT_INIT_NACK: |
| 699 | LOGPFSML(fi, LOGL_NOTICE, "Rx Initialization NACK N=%" PRIu32 "/%" PRIu32 "\n", |
| 700 | iui->timer.init.n, iui->config.t_init.n_max); |
| 701 | osmo_timer_del(&fi->timer); |
| 702 | if (iui->timer.init.n == iui->config.t_init.n_max) { |
| 703 | irp = irp_error_event_alloc_c(iui, IUUP_ERR_CAUSE_INIT_FAILURE_REP_NACK, IUUP_ERR_DIST_SECOND_FWD); |
| 704 | iui->user_prim_cb(&irp->oph, iui->user_prim_priv); |
| 705 | return; |
| 706 | } |
| 707 | iui->timer.init.n++; |
| 708 | retransmit_initialization(iui); |
| 709 | break; |
| 710 | default: |
| 711 | OSMO_ASSERT(false); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | static void iuup_fsm_smpsdu_data(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 716 | { |
| 717 | struct osmo_iuup_instance *iui = fi->priv; |
| 718 | struct osmo_iuup_rnl_prim *irp = NULL; |
| 719 | struct osmo_iuup_tnl_prim *itp = NULL; |
| 720 | |
| 721 | switch (event) { |
| 722 | case IUUP_FSM_EVT_IUUP_CONFIG_REQ: |
| 723 | irp = data; |
| 724 | osmo_fsm_inst_state_chg(fi, IUUP_FSM_ST_NULL, 0, 0); |
| 725 | break; |
| 726 | case IUUP_FSM_EVT_IUUP_DATA_REQ: |
| 727 | /* Data coming down from RNL (user) towards TNL (transport) */ |
| 728 | irp = data; |
| 729 | itp = rnl_to_tnl_data(iui, irp); |
| 730 | iui->transport_prim_cb(&itp->oph, iui->transport_prim_priv); |
| 731 | break; |
| 732 | case IUUP_FSM_EVT_IUUP_DATA_IND: |
| 733 | /* Data coming up from TNL (transport) towards RNL (user) */ |
| 734 | itp = data; |
| 735 | irp = tnl_to_rnl_data(itp); |
| 736 | iui->user_prim_cb(&irp->oph, iui->user_prim_priv); |
| 737 | break; |
| 738 | case IUUP_FSM_EVT_IUUP_UNITDATA_REQ: |
| 739 | case IUUP_FSM_EVT_IUUP_UNITDATA_IND: |
| 740 | case IUUP_FSM_EVT_SSASAR_UNITDATA_REQ: |
| 741 | case IUUP_FSM_EVT_SSASAR_UNITDATA_IND: |
| 742 | /* no state change */ |
| 743 | break; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | static int iuup_fsm_timer_cb(struct osmo_fsm_inst *fi) |
| 748 | { |
| 749 | struct osmo_iuup_instance *iui = fi->priv; |
| 750 | struct osmo_iuup_rnl_prim *irp; |
| 751 | |
| 752 | switch (fi->T) { |
| 753 | case IUUP_TIMER_INIT: |
| 754 | OSMO_ASSERT(fi->state == IUUP_FSM_ST_INIT); |
| 755 | if (iui->timer.init.n == iui->config.t_init.n_max) { |
| 756 | irp = irp_error_event_alloc_c(iui, IUUP_ERR_CAUSE_INIT_FAILURE_NET_TMR, IUUP_ERR_DIST_LOCAL); |
| 757 | iui->user_prim_cb(&irp->oph, iui->user_prim_priv); |
| 758 | return 0; |
| 759 | } |
| 760 | iui->timer.init.n++; |
| 761 | retransmit_initialization(iui); |
| 762 | break; |
| 763 | case IUUP_TIMER_TA: |
| 764 | break; |
| 765 | case IUUP_TIMER_RC: |
| 766 | break; |
| 767 | default: |
| 768 | OSMO_ASSERT(0); |
| 769 | } |
| 770 | return 0; |
| 771 | } |
| 772 | |
| 773 | |
| 774 | static const struct osmo_fsm_state iuup_fsm_states[] = { |
| 775 | [IUUP_FSM_ST_NULL] = { |
| 776 | .in_event_mask = S(IUUP_FSM_EVT_IUUP_CONFIG_REQ), |
| 777 | .out_state_mask = S(IUUP_FSM_ST_INIT) | |
| 778 | S(IUUP_FSM_ST_TrM_DATA_XFER_READY), |
| 779 | .name = "NULL", |
| 780 | .action = iuup_fsm_null, |
| 781 | }, |
| 782 | [IUUP_FSM_ST_TrM_DATA_XFER_READY] = { |
| 783 | .in_event_mask = S(IUUP_FSM_EVT_IUUP_CONFIG_REQ) | |
| 784 | S(IUUP_FSM_EVT_IUUP_STATUS_REQ) | |
| 785 | S(IUUP_FSM_EVT_IUUP_DATA_REQ) | |
| 786 | S(IUUP_FSM_EVT_IUUP_DATA_IND) | |
| 787 | S(IUUP_FSM_EVT_IUUP_UNITDATA_REQ) | |
| 788 | S(IUUP_FSM_EVT_IUUP_UNITDATA_IND) | |
| 789 | S(IUUP_FSM_EVT_SSASAR_UNITDATA_REQ) | |
| 790 | S(IUUP_FSM_EVT_SSASAR_UNITDATA_IND), |
| 791 | .out_state_mask = S(IUUP_FSM_ST_NULL), |
Philipp Maier | bdd7df3 | 2022-01-18 11:58:18 +0100 | [diff] [blame] | 792 | .name = "TrM_Data_Transfer_Ready", |
Harald Welte | 9fe1f9f | 2018-11-29 13:47:39 +0100 | [diff] [blame] | 793 | .action = iuup_fsm_trm_data, |
| 794 | }, |
| 795 | [IUUP_FSM_ST_INIT] = { |
| 796 | .in_event_mask = S(IUUP_FSM_EVT_IUUP_CONFIG_REQ) | |
| 797 | S(IUUP_FSM_EVT_INIT) | |
| 798 | S(IUUP_FSM_EVT_LAST_INIT_ACK) | |
| 799 | S(IUUP_FSM_EVT_INIT_NACK), |
| 800 | .out_state_mask = S(IUUP_FSM_ST_NULL) | |
| 801 | S(IUUP_FSM_ST_SMpSDU_DATA_XFER_READY), |
| 802 | .name = "Initialisation", |
| 803 | .onenter = iuup_fsm_init_on_enter, |
| 804 | .action = iuup_fsm_init, |
| 805 | }, |
| 806 | [IUUP_FSM_ST_SMpSDU_DATA_XFER_READY] = { |
| 807 | .in_event_mask = S(IUUP_FSM_EVT_IUUP_DATA_REQ) | |
| 808 | S(IUUP_FSM_EVT_IUUP_DATA_IND), |
| 809 | .out_state_mask = S(IUUP_FSM_ST_NULL) | |
| 810 | S(IUUP_FSM_ST_INIT), |
Philipp Maier | bdd7df3 | 2022-01-18 11:58:18 +0100 | [diff] [blame] | 811 | .name = "SMpSDU_Data_Transfer_Ready", |
Harald Welte | 9fe1f9f | 2018-11-29 13:47:39 +0100 | [diff] [blame] | 812 | .action = iuup_fsm_smpsdu_data, |
| 813 | }, |
| 814 | }; |
| 815 | |
| 816 | static struct osmo_fsm iuup_fsm = { |
| 817 | .name = "IuUP", |
| 818 | .states = iuup_fsm_states, |
| 819 | .num_states = ARRAY_SIZE(iuup_fsm_states), |
| 820 | .timer_cb = iuup_fsm_timer_cb, |
| 821 | .log_subsys = DLIUUP, |
| 822 | .event_names = iuup_fsm_event_names, |
| 823 | }; |
| 824 | |
| 825 | static int iuup_verify_pdu(const uint8_t *data, unsigned int len) |
| 826 | { |
| 827 | int header_crc_computed, payload_crc_computed; |
| 828 | uint16_t payload_crc; |
| 829 | uint8_t pdu_type = iuup_get_pdu_type(data); |
| 830 | struct iuup_pdutype0_hdr *t0h; |
| 831 | struct iuup_pdutype14_hdr *t14h; |
| 832 | |
| 833 | if (len < 3) |
| 834 | return -EINVAL; |
| 835 | |
| 836 | header_crc_computed = osmo_iuup_compute_header_crc(data, len); |
| 837 | if (iuup_get_hdr_crc(data) != header_crc_computed) { |
Pau Espin Pedrol | 1322755 | 2022-01-10 14:21:19 +0100 | [diff] [blame] | 838 | LOGP(DLIUUP, LOGL_NOTICE, "Header Checksum error: rx 0x%02x vs exp 0x%02x\n", |
Harald Welte | 9fe1f9f | 2018-11-29 13:47:39 +0100 | [diff] [blame] | 839 | iuup_get_hdr_crc(data), header_crc_computed); |
| 840 | return -EIO; |
| 841 | } |
| 842 | switch (pdu_type) { |
| 843 | case IUUP_PDU_T_DATA_NOCRC: |
| 844 | if (len < 4) |
| 845 | return -EINVAL; |
| 846 | break; |
| 847 | case IUUP_PDU_T_DATA_CRC: |
| 848 | t0h = (struct iuup_pdutype0_hdr *) data; |
| 849 | payload_crc = ((uint16_t)t0h->payload_crc_hi << 8) | t0h->payload_crc_lo; |
| 850 | payload_crc_computed = osmo_iuup_compute_payload_crc(data, len); |
| 851 | if (payload_crc != payload_crc_computed) |
Pau Espin Pedrol | 1322755 | 2022-01-10 14:21:19 +0100 | [diff] [blame] | 852 | goto payload_crc_err; |
Harald Welte | 9fe1f9f | 2018-11-29 13:47:39 +0100 | [diff] [blame] | 853 | break; |
| 854 | case IUUP_PDU_T_CONTROL: |
| 855 | t14h = (struct iuup_pdutype14_hdr *) data; |
| 856 | if (t14h->ack_nack == IUUP_AN_PROCEDURE) { |
| 857 | payload_crc = ((uint16_t)t14h->payload_crc_hi << 8) | t14h->payload_crc_lo; |
| 858 | payload_crc_computed = osmo_iuup_compute_payload_crc(data, len); |
| 859 | if (payload_crc != payload_crc_computed) |
Pau Espin Pedrol | 1322755 | 2022-01-10 14:21:19 +0100 | [diff] [blame] | 860 | goto payload_crc_err; |
Harald Welte | 9fe1f9f | 2018-11-29 13:47:39 +0100 | [diff] [blame] | 861 | } |
| 862 | break; |
| 863 | default: |
| 864 | return -EINVAL; |
| 865 | } |
| 866 | return 0; |
Pau Espin Pedrol | 1322755 | 2022-01-10 14:21:19 +0100 | [diff] [blame] | 867 | |
| 868 | payload_crc_err: |
| 869 | LOGP(DLIUUP, LOGL_NOTICE, "Payload Checksum error (pdu type %u): rx 0x%02x vs exp 0x%02x\n", |
| 870 | pdu_type, payload_crc, payload_crc_computed); |
| 871 | return -EIO; |
Harald Welte | 9fe1f9f | 2018-11-29 13:47:39 +0100 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | /* A IuUP TNL SAP primitive from transport (lower layer) */ |
| 875 | int osmo_iuup_tnl_prim_up(struct osmo_iuup_instance *inst, struct osmo_iuup_tnl_prim *itp) |
| 876 | { |
| 877 | struct osmo_prim_hdr *oph = &itp->oph; |
| 878 | struct iuup_pdutype14_hdr *t14h; |
| 879 | int rc = 0; |
| 880 | |
| 881 | OSMO_ASSERT(oph->sap == SAP_IUUP_TNL); |
| 882 | |
| 883 | switch (OSMO_PRIM_HDR(oph)) { |
| 884 | case OSMO_PRIM(OSMO_IUUP_TNL_UNITDATA, PRIM_OP_INDICATION): |
| 885 | if (iuup_verify_pdu(msgb_l2(oph->msg), msgb_l2len(oph->msg)) < 0) { |
| 886 | LOGPFSML(inst->fi, LOGL_NOTICE, "Discarding invalid IuUP PDU: %s\n", |
| 887 | osmo_hexdump((const unsigned char *) msgb_l2(oph->msg), msgb_l2len(oph->msg))); |
| 888 | /* don't return error as the caller is not responsible for the PDU which |
| 889 | * was transmitted from some remote peer */ |
| 890 | return 0; |
| 891 | } |
| 892 | switch (iuup_get_pdu_type(msgb_l2(oph->msg))) { |
| 893 | case IUUP_PDU_T_DATA_CRC: |
| 894 | oph->msg->l3h = msgb_l2(oph->msg) + sizeof(struct iuup_pdutype0_hdr); |
| 895 | rc = osmo_fsm_inst_dispatch(inst->fi, IUUP_FSM_EVT_IUUP_DATA_IND, itp); |
| 896 | break; |
| 897 | case IUUP_PDU_T_DATA_NOCRC: |
| 898 | oph->msg->l3h = msgb_l2(oph->msg) + sizeof(struct iuup_pdutype1_hdr); |
| 899 | rc = osmo_fsm_inst_dispatch(inst->fi, IUUP_FSM_EVT_IUUP_DATA_IND, itp); |
| 900 | break; |
| 901 | case IUUP_PDU_T_CONTROL: |
| 902 | t14h = (struct iuup_pdutype14_hdr *) msgb_l2(oph->msg); |
| 903 | switch (t14h->ack_nack) { |
| 904 | case IUUP_AN_PROCEDURE: |
| 905 | switch (t14h->proc_ind) { |
| 906 | case IUUP_PROC_INIT: |
| 907 | rc = osmo_fsm_inst_dispatch(inst->fi, IUUP_FSM_EVT_INIT, itp); |
| 908 | break; |
| 909 | case IUUP_PROC_RATE_CTRL: |
| 910 | case IUUP_PROC_TIME_ALIGN: |
| 911 | case IUUP_PROC_ERR_EVENT: |
| 912 | LOGPFSML(inst->fi, LOGL_NOTICE, "Received Request for " |
| 913 | "unsupported IuUP procedure %u\n", t14h->proc_ind); |
| 914 | break; |
| 915 | default: |
| 916 | LOGPFSML(inst->fi, LOGL_NOTICE, "Received Request for " |
| 917 | "unknown IuUP procedure %u\n", t14h->proc_ind); |
| 918 | break; |
| 919 | } |
| 920 | break; |
| 921 | case IUUP_AN_ACK: |
| 922 | switch (t14h->proc_ind) { |
| 923 | case IUUP_PROC_INIT: |
| 924 | rc = osmo_fsm_inst_dispatch(inst->fi, |
| 925 | IUUP_FSM_EVT_LAST_INIT_ACK, itp); |
| 926 | break; |
| 927 | default: |
| 928 | LOGPFSML(inst->fi, LOGL_ERROR, "Received ACK for " |
| 929 | "unknown IuUP procedure %u\n", t14h->proc_ind); |
| 930 | break; |
| 931 | } |
| 932 | break; |
| 933 | case IUUP_AN_NACK: |
| 934 | switch (t14h->proc_ind) { |
| 935 | case IUUP_PROC_INIT: |
| 936 | rc = osmo_fsm_inst_dispatch(inst->fi, |
| 937 | IUUP_FSM_EVT_INIT_NACK, itp); |
| 938 | break; |
| 939 | default: |
| 940 | LOGPFSML(inst->fi, LOGL_ERROR, "Received NACK for " |
| 941 | "unknown IuUP procedure %u\n", t14h->proc_ind); |
| 942 | break; |
| 943 | } |
| 944 | break; |
| 945 | default: |
| 946 | LOGPFSML(inst->fi, LOGL_ERROR, "Received unknown IuUP ACK/NACK\n"); |
| 947 | break; |
| 948 | } |
| 949 | break; |
| 950 | default: |
| 951 | LOGPFSML(inst->fi, LOGL_NOTICE, "Received unknown IuUP PDU type %u\n", |
| 952 | iuup_get_pdu_type(msgb_l2(oph->msg))); |
| 953 | break; |
| 954 | } |
| 955 | break; |
| 956 | default: |
| 957 | /* exception: return an error code due to a wrong primitive */ |
| 958 | return -EINVAL; |
| 959 | } |
| 960 | |
| 961 | return rc; |
| 962 | } |
| 963 | |
| 964 | /* A IuUP RNL SAP primitive from user (higher layer) */ |
| 965 | int osmo_iuup_rnl_prim_down(struct osmo_iuup_instance *inst, struct osmo_iuup_rnl_prim *irp) |
| 966 | { |
| 967 | struct osmo_prim_hdr *oph = &irp->oph; |
| 968 | int rc; |
| 969 | |
| 970 | OSMO_ASSERT(oph->sap == SAP_IUUP_RNL); |
| 971 | |
| 972 | switch (OSMO_PRIM_HDR(oph)) { |
| 973 | case OSMO_PRIM(OSMO_IUUP_RNL_CONFIG, PRIM_OP_REQUEST): |
| 974 | rc = osmo_fsm_inst_dispatch(inst->fi, IUUP_FSM_EVT_IUUP_CONFIG_REQ, irp); |
| 975 | msgb_free(irp->oph.msg); |
| 976 | break; |
| 977 | case OSMO_PRIM(OSMO_IUUP_RNL_DATA, PRIM_OP_REQUEST): |
| 978 | rc = osmo_fsm_inst_dispatch(inst->fi, IUUP_FSM_EVT_IUUP_DATA_REQ, irp); |
| 979 | if (rc != 0) |
| 980 | msgb_free(irp->oph.msg); |
| 981 | break; |
| 982 | case OSMO_PRIM(OSMO_IUUP_RNL_STATUS, PRIM_OP_REQUEST): |
| 983 | rc = osmo_fsm_inst_dispatch(inst->fi, IUUP_FSM_EVT_IUUP_STATUS_REQ, irp); |
| 984 | msgb_free(irp->oph.msg); |
| 985 | break; |
| 986 | default: |
| 987 | rc = -EINVAL; |
| 988 | msgb_free(irp->oph.msg); |
| 989 | } |
| 990 | return rc; |
| 991 | } |
| 992 | |
| 993 | struct osmo_iuup_instance *osmo_iuup_instance_alloc(void *ctx, const char *id) |
| 994 | { |
| 995 | struct osmo_iuup_instance *iui; |
| 996 | iui = talloc_zero(ctx, struct osmo_iuup_instance); |
| 997 | if (!iui) |
| 998 | return NULL; |
| 999 | |
| 1000 | iui->fi = osmo_fsm_inst_alloc(&iuup_fsm, NULL, iui, LOGL_DEBUG, id); |
| 1001 | if (!iui->fi) |
| 1002 | goto free_ret; |
| 1003 | |
| 1004 | return iui; |
| 1005 | free_ret: |
| 1006 | talloc_free(iui); |
| 1007 | return NULL; |
| 1008 | } |
| 1009 | |
| 1010 | void osmo_iuup_instance_free(struct osmo_iuup_instance *iui) |
| 1011 | { |
| 1012 | if (!iui) |
| 1013 | return; |
| 1014 | |
| 1015 | if (iui->fi) |
| 1016 | osmo_fsm_inst_free(iui->fi); |
| 1017 | iui->fi = NULL; |
| 1018 | talloc_free(iui); |
| 1019 | } |
| 1020 | |
| 1021 | void osmo_iuup_instance_set_user_prim_cb(struct osmo_iuup_instance *iui, osmo_prim_cb func, void *priv) |
| 1022 | { |
| 1023 | iui->user_prim_cb = func; |
| 1024 | iui->user_prim_priv = priv; |
| 1025 | } |
| 1026 | void osmo_iuup_instance_set_transport_prim_cb(struct osmo_iuup_instance *iui, osmo_prim_cb func, void *priv) |
| 1027 | { |
| 1028 | iui->transport_prim_cb = func; |
| 1029 | iui->transport_prim_priv = priv; |
| 1030 | } |
| 1031 | |
| 1032 | static __attribute__((constructor)) void on_dso_load_iuup_fsm(void) |
| 1033 | { |
| 1034 | OSMO_ASSERT(osmo_fsm_register(&iuup_fsm) == 0); |
| 1035 | } |