blob: 159e533e0a9a50b0039b73364107ea1b83375c90 [file] [log] [blame]
Harald Welte9fe1f9f2018-11-29 13:47:39 +01001/*! \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 */
38const 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 */
46const struct osmo_crc16gen_code iuup_data_crc_code = {
47 .bits = 10,
48 .poly = 563,
49 .init = 0,
50 .remainder = 0,
51};
52
53static 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
67int 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
94int 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
115struct 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
121struct 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
140enum 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
147enum 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
162static const struct value_string iuup_fsm_event_names[] = {
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" },
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
178static inline uint8_t iuup_get_pdu_type(const uint8_t *data)
179{
180 return data[0] >> 4;
181}
182
183static 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))
192static 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 */
199static inline __attribute__((assume_aligned(PTR_ALIGNMENT_BYTES)))
200unsigned 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
208struct 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
219struct 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 */
231static 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 */
255static 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 */
281static 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
361/* transform a RNL data primitive into a TNL data primitive (down the stack) */
362static struct osmo_iuup_tnl_prim *rnl_to_tnl_data(struct osmo_iuup_instance *iui,
363 struct osmo_iuup_rnl_prim *irp)
364{
365 struct osmo_iuup_tnl_prim *itp;
366 struct osmo_iuup_rnl_data dt;
367 struct msgb *msg;
368 uint16_t payload_crc;
369 struct iuup_pdutype0_hdr *h0;
370 struct iuup_pdutype1_hdr *h1;
371
372 OSMO_ASSERT(OSMO_PRIM_HDR(&irp->oph) == OSMO_PRIM(OSMO_IUUP_RNL_DATA, PRIM_OP_REQUEST));
373
374 msg = irp->oph.msg;
375 dt = irp->u.data;
376
377 /* pull up to the IuUP payload and push a new primitive header in front */
378 msgb_pull_to_l3(msg);
379
380 /* push the PDU TYPE 0 / 1 header in front of the payload */
381 switch (iui->config.data_pdu_type) {
382 case 0:
383 msg->l2h = msgb_push(msg, sizeof(*h0));
384 h0 = (struct iuup_pdutype0_hdr *)msg->l2h;
385 h0->frame_nr = dt.frame_nr;
386 h0->pdu_type = IUUP_PDU_T_DATA_CRC;
387 h0->rfci = dt.rfci;
388 h0->fqc = dt.fqc;
389 h0->header_crc = osmo_iuup_compute_header_crc(msgb_l2(msg), msgb_l2len(msg));
390 payload_crc = osmo_iuup_compute_payload_crc(msgb_l2(msg), msgb_l2len(msg));
391 h0->payload_crc_hi = (payload_crc >> 8) & 0x03;
392 h0->payload_crc_lo = payload_crc & 0xff;
393 break;
394 case 1:
395 msg->l2h = msgb_push(msg, sizeof(*h1));
396 h1 = (struct iuup_pdutype1_hdr *)msg->l2h;
397 h1->frame_nr = dt.frame_nr;
398 h1->pdu_type = IUUP_PDU_T_DATA_NOCRC;
399 h1->rfci = dt.rfci;
400 h1->fqc = dt.fqc;
401 h1->header_crc = osmo_iuup_compute_header_crc(msgb_l2(msg), msgb_l2len(msg));
402 h1->spare = 0;
403 break;
404 default:
405 OSMO_ASSERT(0);
406 }
407
408 /* Avoid allocating irp out of 8byte-aligned address, Asan is not happy with it */
409 itp = (struct osmo_iuup_tnl_prim *) aligned_msgb_push(msg, sizeof(*itp));
410 osmo_prim_init(&itp->oph, SAP_IUUP_TNL, OSMO_IUUP_TNL_UNITDATA, PRIM_OP_REQUEST, msg);
411
412 return itp;
413}
414
415/* transform a TNL primitive into a RNL primitive (up the stack) */
416static struct osmo_iuup_rnl_prim *tnl_to_rnl_data(struct osmo_iuup_tnl_prim *itp)
417{
418 struct msgb *msg;
419 struct iuup_pdutype0_hdr *h0;
420 struct iuup_pdutype1_hdr *h1;
421 struct osmo_iuup_rnl_data dt;
422 struct osmo_iuup_rnl_prim *irp;
423
424 msg = itp->oph.msg;
425
426 OSMO_ASSERT(OSMO_PRIM_HDR(&itp->oph) == OSMO_PRIM(OSMO_IUUP_TNL_UNITDATA, PRIM_OP_INDICATION));
427
428 switch (iuup_get_pdu_type(msgb_l2(msg))) {
429 case IUUP_PDU_T_DATA_CRC:
430 h0 = (struct iuup_pdutype0_hdr *) msgb_l2(msg);
431 dt.rfci = h0->rfci;
432 dt.frame_nr = h0->frame_nr;
433 dt.fqc = h0->fqc;
434 break;
435 case IUUP_PDU_T_DATA_NOCRC:
436 h1 = (struct iuup_pdutype1_hdr *) msgb_l2(msg);
437 dt.rfci = h1->rfci;
438 dt.frame_nr = h1->frame_nr;
439 dt.fqc = h1->fqc;
440 break;
441 }
442
443 /* pull up to the IuUP payload and push a new primitive header in front */
444 msgb_pull_to_l3(msg);
445
446 /* Avoid allocating irp out of 8byte-aligned address, Asan is not happy with it */
447 irp = (struct osmo_iuup_rnl_prim *) aligned_msgb_push(msg, sizeof(*irp));
448 osmo_prim_init(&irp->oph, SAP_IUUP_RNL, OSMO_IUUP_RNL_DATA, PRIM_OP_INDICATION, msg);
449 irp->u.data = dt;
450
451 return irp;
452}
453
454static struct osmo_iuup_rnl_prim *irp_error_event_alloc_c(void *ctx, enum iuup_error_cause cause, enum iuup_error_distance distance)
455{
456 struct osmo_iuup_rnl_prim *irp;
457 struct msgb *msg;
458 msg = msgb_alloc_c(ctx, sizeof(*irp), "iuup-tx");
459 irp = (struct osmo_iuup_rnl_prim *) msgb_put(msg, sizeof(*irp));
460 osmo_prim_init(&irp->oph, SAP_IUUP_RNL, OSMO_IUUP_RNL_STATUS, PRIM_OP_INDICATION, msg);
461 irp->u.status.procedure = IUUP_PROC_ERR_EVENT;
462 irp->u.status.u.error_event.cause = cause;
463 irp->u.status.u.error_event.distance = distance;
464 return irp;
465}
466
467static struct osmo_iuup_tnl_prim *itp_copy_c(void *ctx, const struct osmo_iuup_tnl_prim *src_itp)
468{
469 struct msgb *msg;
470 struct osmo_iuup_tnl_prim *dst_itp;
471
472 msg = msgb_copy_c(ctx, src_itp->oph.msg, "iuup-tx-retrans");
473 dst_itp = (struct osmo_iuup_tnl_prim *)msgb_data(msg);
474 dst_itp->oph.msg = msg;
475 return dst_itp;
476}
477
478static void retransmit_initialization(struct osmo_iuup_instance *iui)
479{
480 struct osmo_iuup_tnl_prim *itp;
481 iui->fi->T = IUUP_TIMER_INIT;
482 osmo_timer_schedule(&iui->fi->timer, iui->config.t_init.t_ms / 1000, (iui->config.t_init.t_ms % 1000) * 1000);
483 itp = itp_copy_c(iui, iui->timer.init.retrans_itp);
484 iui->transport_prim_cb(&itp->oph, iui->transport_prim_priv);
485}
486
487/* return: whether the last Init was Acked correctly and hence can transition to next state */
488static bool iuup_rx_initialization(struct osmo_iuup_instance *iui, struct osmo_iuup_tnl_prim *itp)
489{
490 struct iuup_pdutype14_hdr *hdr;
491 struct iuup_ctrl_init_hdr *ihdr;
492 struct iuup_ctrl_init_rfci_hdr *ihdr_rfci;
493 struct iuup_ctrl_init_tail *itail;
494 enum iuup_error_cause err_cause;
495 uint8_t num_rfci = 0;
496 unsigned int i;
497 bool is_last;
498 uint16_t remote_mask, match_mask;
499 struct osmo_iuup_tnl_prim *resp;
500
501 /* TODO: whenever we check message boundaries, length, etc. and we fail, send NACK */
502
503 hdr = (struct iuup_pdutype14_hdr *)msgb_l2(itp->oph.msg);
504 ihdr = (struct iuup_ctrl_init_hdr *)hdr->payload;
505 if (ihdr->num_subflows_per_rfci == 0) {
506 LOGPFSML(iui->fi, LOGL_NOTICE, "Initialization: Unexpected num_subflows=0 received\n");
507 err_cause = IUUP_ERR_CAUSE_UNEXPECTED_VALUE;
508 goto send_nack;
509 }
510 ihdr_rfci = (struct iuup_ctrl_init_rfci_hdr *)ihdr->rfci_data;
511
512 do {
513 uint8_t l_size_bytes = ihdr_rfci->li + 1;
514 is_last = ihdr_rfci->lri;
515 if (ihdr_rfci->rfci != num_rfci) {
516 LOGPFSML(iui->fi, LOGL_NOTICE, "Initialization: Unexpected RFCI %u at position %u received\n",
517 ihdr_rfci->rfci, num_rfci);
518 err_cause = IUUP_ERR_CAUSE_UNEXPECTED_RFCI;
519 goto send_nack;
520 }
521 if (l_size_bytes == 2) {
522 uint16_t *subflow_size = (uint16_t *)ihdr_rfci->subflow_length;
523 for (i = 0; i < ihdr->num_subflows_per_rfci; i++) {
524 iui->config.subflow_sizes[ihdr_rfci->rfci][i] = osmo_load16be(subflow_size);
525 subflow_size++;
526 }
527 } else {
528 uint8_t *subflow_size = ihdr_rfci->subflow_length;
529 for (i = 0; i < ihdr->num_subflows_per_rfci; i++) {
530 iui->config.subflow_sizes[ihdr_rfci->rfci][i] = osmo_load16be(subflow_size);
531 subflow_size++;
532 }
533 }
534 num_rfci++;
535 ihdr_rfci++;
536 ihdr_rfci = (struct iuup_ctrl_init_rfci_hdr *)(((uint8_t *)ihdr_rfci) + ihdr->num_subflows_per_rfci * l_size_bytes);
537 } while (!is_last);
538
539 if (ihdr->ti) { /* Timing information present */
540 uint8_t *buf = (uint8_t *)ihdr_rfci;
541 uint8_t num_bytes = (num_rfci + 1) / 2;
542 iui->config.IPTIs_present = true;
543 for (i = 0; i < num_bytes - 1; i++) {
544 iui->config.IPTIs[i*2] = *buf >> 4;
545 iui->config.IPTIs[i*2 + 1] = *buf & 0x0f;
546 buf++;
547 }
548 iui->config.IPTIs[i*2] = *buf >> 4;
549 if (!(num_rfci & 0x01)) /* is even: */
550 iui->config.IPTIs[i*2 + 1] = *buf & 0x0f;
551 buf++;
552 itail = (struct iuup_ctrl_init_tail *)buf;
553 } else {
554 itail = (struct iuup_ctrl_init_tail *)ihdr_rfci;
555 }
556
557 if (itail->data_pdu_type > 1) {
558 LOGPFSML(iui->fi, LOGL_NOTICE, "Initialization: Unexpected Data PDU Type %u received\n", itail->data_pdu_type);
559 err_cause = IUUP_ERR_CAUSE_UNEXPECTED_VALUE;
560 goto send_nack;
561 }
562
563 remote_mask = osmo_load16be(&itail->versions_supported);
564 match_mask = (remote_mask & iui->config.supported_versions_mask);
565 if (match_mask == 0x0000) {
566 LOGPFSML(iui->fi, LOGL_NOTICE,
567 "Initialization: No match in supported versions local=0x%04x vs remote=0x%04x\n",
568 iui->config.supported_versions_mask, remote_mask);
569 err_cause = IUUP_ERR_CAUSE_UNEXPECTED_VALUE;
570 goto send_nack;
571 }
572 for (i = 15; i >= 0; i--) {
573 if (match_mask & (1<<i)) {
574 iui->mode_version = i;
575 break;
576 }
577 }
578
579 iui->config.num_rfci = num_rfci;
580 iui->config.num_subflows = ihdr->num_subflows_per_rfci;
581 iui->config.data_pdu_type = itail->data_pdu_type;
582
583 LOGPFSML(iui->fi, LOGL_DEBUG, "Tx Initialization ACK\n");
584 resp = itp_ctrl_ack_alloc(iui, IUUP_PROC_INIT, hdr->frame_nr);
585 iui->transport_prim_cb(&resp->oph, iui->transport_prim_priv);
586 return ihdr->chain_ind == 0;
587send_nack:
588 LOGPFSML(iui->fi, LOGL_NOTICE, "Tx Initialization NACK cause=%u orig_message=%s\n",
589 err_cause, osmo_hexdump((const unsigned char *) msgb_l2(itp->oph.msg), msgb_l2len(itp->oph.msg)));
590 resp = tnp_ctrl_nack_alloc(iui, IUUP_PROC_INIT, err_cause, hdr->frame_nr);
591 iui->transport_prim_cb(&resp->oph, iui->transport_prim_priv);
592 return false;
593}
594
595/**********************
596 * FSM STATE FUNCTIONS
597 **********************/
598static void iuup_fsm_null(struct osmo_fsm_inst *fi, uint32_t event, void *data)
599{
600 struct osmo_iuup_instance *iui = fi->priv;
601 struct osmo_iuup_rnl_prim *user_prim = NULL;
602
603 switch (event) {
604 case IUUP_FSM_EVT_IUUP_CONFIG_REQ:
605 user_prim = data;
606 iui->config = user_prim->u.config;
607 iui->config.supported_versions_mask &= 0x0003; /* We only support versions 1 and 2 ourselves */
608 //TODO: if supported_versions_mask == 0x0000,no supported versions, send error to upper layers
609
610 if (iui->config.transparent)
611 osmo_fsm_inst_state_chg(fi, IUUP_FSM_ST_TrM_DATA_XFER_READY, 0, 0);
612 else {
613 osmo_fsm_inst_state_chg(fi, IUUP_FSM_ST_INIT, 0, 0);
614 }
615 break;
616 }
617}
618
619/* transparent mode data transfer */
620static void iuup_fsm_trm_data(struct osmo_fsm_inst *fi, uint32_t event, void *data)
621{
622 //struct osmo_iuup_instance *iui = fi->priv;
623
624 switch (event) {
625 case IUUP_FSM_EVT_IUUP_CONFIG_REQ:
626 osmo_fsm_inst_state_chg(fi, IUUP_FSM_ST_NULL, 0, 0);
627 break;
628 case IUUP_FSM_EVT_IUUP_DATA_REQ:
629 /* Data coming down from RNL (user) towards TNL (transport) */
630 break;
631 case IUUP_FSM_EVT_IUUP_DATA_IND:
632 /* Data coming up from TNL (transport) towards RNL (user) */
633 break;
634 case IUUP_FSM_EVT_IUUP_UNITDATA_REQ:
635 case IUUP_FSM_EVT_IUUP_UNITDATA_IND:
636 case IUUP_FSM_EVT_SSASAR_UNITDATA_REQ:
637 case IUUP_FSM_EVT_SSASAR_UNITDATA_IND:
638 /* no state change */
639 break;
640 }
641}
642
643static void iuup_fsm_init_on_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
644{
645 struct osmo_iuup_instance *iui = fi->priv;
646
647 iui->type14_fn = 0;
648 if (iui->config.active) {
649 iui->timer.init.n = 0;
650 iui->timer.init.retrans_itp = tnp_ctrl_init_alloc(iui);
651 retransmit_initialization(iui);
652 }
653}
654
655static void iuup_fsm_init(struct osmo_fsm_inst *fi, uint32_t event, void *data)
656{
657 struct osmo_iuup_instance *iui = fi->priv;
658 struct osmo_iuup_rnl_prim *irp;
659 struct osmo_iuup_tnl_prim *itp;
660
661 switch (event) {
662 case IUUP_FSM_EVT_IUUP_CONFIG_REQ:
663 /* the only permitted 'config req' type is the request to release the instance */
664 osmo_fsm_inst_state_chg(fi, IUUP_FSM_ST_NULL, 0, 0);
665 break;
666 case IUUP_FSM_EVT_INIT:
667 itp = data;
668 if (iuup_rx_initialization(iui, itp))
669 osmo_fsm_inst_state_chg(fi, IUUP_FSM_ST_SMpSDU_DATA_XFER_READY, 0, 0);
670 break;
671 case IUUP_FSM_EVT_LAST_INIT_ACK:
672 /* last INIT ACK was received, transition to DATA_XFER_READY state */
673 osmo_fsm_inst_state_chg(fi, IUUP_FSM_ST_SMpSDU_DATA_XFER_READY, 0, 0);
674 break;
675 case IUUP_FSM_EVT_INIT_NACK:
676 LOGPFSML(fi, LOGL_NOTICE, "Rx Initialization NACK N=%" PRIu32 "/%" PRIu32 "\n",
677 iui->timer.init.n, iui->config.t_init.n_max);
678 osmo_timer_del(&fi->timer);
679 if (iui->timer.init.n == iui->config.t_init.n_max) {
680 irp = irp_error_event_alloc_c(iui, IUUP_ERR_CAUSE_INIT_FAILURE_REP_NACK, IUUP_ERR_DIST_SECOND_FWD);
681 iui->user_prim_cb(&irp->oph, iui->user_prim_priv);
682 return;
683 }
684 iui->timer.init.n++;
685 retransmit_initialization(iui);
686 break;
687 default:
688 OSMO_ASSERT(false);
689 }
690}
691
692static void iuup_fsm_smpsdu_data(struct osmo_fsm_inst *fi, uint32_t event, void *data)
693{
694 struct osmo_iuup_instance *iui = fi->priv;
695 struct osmo_iuup_rnl_prim *irp = NULL;
696 struct osmo_iuup_tnl_prim *itp = NULL;
697
698 switch (event) {
699 case IUUP_FSM_EVT_IUUP_CONFIG_REQ:
700 irp = data;
701 osmo_fsm_inst_state_chg(fi, IUUP_FSM_ST_NULL, 0, 0);
702 break;
703 case IUUP_FSM_EVT_IUUP_DATA_REQ:
704 /* Data coming down from RNL (user) towards TNL (transport) */
705 irp = data;
706 itp = rnl_to_tnl_data(iui, irp);
707 iui->transport_prim_cb(&itp->oph, iui->transport_prim_priv);
708 break;
709 case IUUP_FSM_EVT_IUUP_DATA_IND:
710 /* Data coming up from TNL (transport) towards RNL (user) */
711 itp = data;
712 irp = tnl_to_rnl_data(itp);
713 iui->user_prim_cb(&irp->oph, iui->user_prim_priv);
714 break;
715 case IUUP_FSM_EVT_IUUP_UNITDATA_REQ:
716 case IUUP_FSM_EVT_IUUP_UNITDATA_IND:
717 case IUUP_FSM_EVT_SSASAR_UNITDATA_REQ:
718 case IUUP_FSM_EVT_SSASAR_UNITDATA_IND:
719 /* no state change */
720 break;
721 }
722}
723
724static int iuup_fsm_timer_cb(struct osmo_fsm_inst *fi)
725{
726 struct osmo_iuup_instance *iui = fi->priv;
727 struct osmo_iuup_rnl_prim *irp;
728
729 switch (fi->T) {
730 case IUUP_TIMER_INIT:
731 OSMO_ASSERT(fi->state == IUUP_FSM_ST_INIT);
732 if (iui->timer.init.n == iui->config.t_init.n_max) {
733 irp = irp_error_event_alloc_c(iui, IUUP_ERR_CAUSE_INIT_FAILURE_NET_TMR, IUUP_ERR_DIST_LOCAL);
734 iui->user_prim_cb(&irp->oph, iui->user_prim_priv);
735 return 0;
736 }
737 iui->timer.init.n++;
738 retransmit_initialization(iui);
739 break;
740 case IUUP_TIMER_TA:
741 break;
742 case IUUP_TIMER_RC:
743 break;
744 default:
745 OSMO_ASSERT(0);
746 }
747 return 0;
748}
749
750
751static const struct osmo_fsm_state iuup_fsm_states[] = {
752 [IUUP_FSM_ST_NULL] = {
753 .in_event_mask = S(IUUP_FSM_EVT_IUUP_CONFIG_REQ),
754 .out_state_mask = S(IUUP_FSM_ST_INIT) |
755 S(IUUP_FSM_ST_TrM_DATA_XFER_READY),
756 .name = "NULL",
757 .action = iuup_fsm_null,
758 },
759 [IUUP_FSM_ST_TrM_DATA_XFER_READY] = {
760 .in_event_mask = S(IUUP_FSM_EVT_IUUP_CONFIG_REQ) |
761 S(IUUP_FSM_EVT_IUUP_STATUS_REQ) |
762 S(IUUP_FSM_EVT_IUUP_DATA_REQ) |
763 S(IUUP_FSM_EVT_IUUP_DATA_IND) |
764 S(IUUP_FSM_EVT_IUUP_UNITDATA_REQ) |
765 S(IUUP_FSM_EVT_IUUP_UNITDATA_IND) |
766 S(IUUP_FSM_EVT_SSASAR_UNITDATA_REQ) |
767 S(IUUP_FSM_EVT_SSASAR_UNITDATA_IND),
768 .out_state_mask = S(IUUP_FSM_ST_NULL),
769 .name = "TrM Data Transfer Ready",
770 .action = iuup_fsm_trm_data,
771 },
772 [IUUP_FSM_ST_INIT] = {
773 .in_event_mask = S(IUUP_FSM_EVT_IUUP_CONFIG_REQ) |
774 S(IUUP_FSM_EVT_INIT) |
775 S(IUUP_FSM_EVT_LAST_INIT_ACK) |
776 S(IUUP_FSM_EVT_INIT_NACK),
777 .out_state_mask = S(IUUP_FSM_ST_NULL) |
778 S(IUUP_FSM_ST_SMpSDU_DATA_XFER_READY),
779 .name = "Initialisation",
780 .onenter = iuup_fsm_init_on_enter,
781 .action = iuup_fsm_init,
782 },
783 [IUUP_FSM_ST_SMpSDU_DATA_XFER_READY] = {
784 .in_event_mask = S(IUUP_FSM_EVT_IUUP_DATA_REQ) |
785 S(IUUP_FSM_EVT_IUUP_DATA_IND),
786 .out_state_mask = S(IUUP_FSM_ST_NULL) |
787 S(IUUP_FSM_ST_INIT),
788 .name = "SMpSDU Data Transfer Ready",
789 .action = iuup_fsm_smpsdu_data,
790 },
791};
792
793static struct osmo_fsm iuup_fsm = {
794 .name = "IuUP",
795 .states = iuup_fsm_states,
796 .num_states = ARRAY_SIZE(iuup_fsm_states),
797 .timer_cb = iuup_fsm_timer_cb,
798 .log_subsys = DLIUUP,
799 .event_names = iuup_fsm_event_names,
800};
801
802static int iuup_verify_pdu(const uint8_t *data, unsigned int len)
803{
804 int header_crc_computed, payload_crc_computed;
805 uint16_t payload_crc;
806 uint8_t pdu_type = iuup_get_pdu_type(data);
807 struct iuup_pdutype0_hdr *t0h;
808 struct iuup_pdutype14_hdr *t14h;
809
810 if (len < 3)
811 return -EINVAL;
812
813 header_crc_computed = osmo_iuup_compute_header_crc(data, len);
814 if (iuup_get_hdr_crc(data) != header_crc_computed) {
815 LOGP(DLIUUP, LOGL_NOTICE, "Checksum error: rx 0x%02x vs exp 0x%02x\n",
816 iuup_get_hdr_crc(data), header_crc_computed);
817 return -EIO;
818 }
819 switch (pdu_type) {
820 case IUUP_PDU_T_DATA_NOCRC:
821 if (len < 4)
822 return -EINVAL;
823 break;
824 case IUUP_PDU_T_DATA_CRC:
825 t0h = (struct iuup_pdutype0_hdr *) data;
826 payload_crc = ((uint16_t)t0h->payload_crc_hi << 8) | t0h->payload_crc_lo;
827 payload_crc_computed = osmo_iuup_compute_payload_crc(data, len);
828 if (payload_crc != payload_crc_computed)
829 return -EIO;
830 break;
831 case IUUP_PDU_T_CONTROL:
832 t14h = (struct iuup_pdutype14_hdr *) data;
833 if (t14h->ack_nack == IUUP_AN_PROCEDURE) {
834 payload_crc = ((uint16_t)t14h->payload_crc_hi << 8) | t14h->payload_crc_lo;
835 payload_crc_computed = osmo_iuup_compute_payload_crc(data, len);
836 if (payload_crc != payload_crc_computed)
837 return -EIO;
838 }
839 break;
840 default:
841 return -EINVAL;
842 }
843 return 0;
844}
845
846/* A IuUP TNL SAP primitive from transport (lower layer) */
847int osmo_iuup_tnl_prim_up(struct osmo_iuup_instance *inst, struct osmo_iuup_tnl_prim *itp)
848{
849 struct osmo_prim_hdr *oph = &itp->oph;
850 struct iuup_pdutype14_hdr *t14h;
851 int rc = 0;
852
853 OSMO_ASSERT(oph->sap == SAP_IUUP_TNL);
854
855 switch (OSMO_PRIM_HDR(oph)) {
856 case OSMO_PRIM(OSMO_IUUP_TNL_UNITDATA, PRIM_OP_INDICATION):
857 if (iuup_verify_pdu(msgb_l2(oph->msg), msgb_l2len(oph->msg)) < 0) {
858 LOGPFSML(inst->fi, LOGL_NOTICE, "Discarding invalid IuUP PDU: %s\n",
859 osmo_hexdump((const unsigned char *) msgb_l2(oph->msg), msgb_l2len(oph->msg)));
860 /* don't return error as the caller is not responsible for the PDU which
861 * was transmitted from some remote peer */
862 return 0;
863 }
864 switch (iuup_get_pdu_type(msgb_l2(oph->msg))) {
865 case IUUP_PDU_T_DATA_CRC:
866 oph->msg->l3h = msgb_l2(oph->msg) + sizeof(struct iuup_pdutype0_hdr);
867 rc = osmo_fsm_inst_dispatch(inst->fi, IUUP_FSM_EVT_IUUP_DATA_IND, itp);
868 break;
869 case IUUP_PDU_T_DATA_NOCRC:
870 oph->msg->l3h = msgb_l2(oph->msg) + sizeof(struct iuup_pdutype1_hdr);
871 rc = osmo_fsm_inst_dispatch(inst->fi, IUUP_FSM_EVT_IUUP_DATA_IND, itp);
872 break;
873 case IUUP_PDU_T_CONTROL:
874 t14h = (struct iuup_pdutype14_hdr *) msgb_l2(oph->msg);
875 switch (t14h->ack_nack) {
876 case IUUP_AN_PROCEDURE:
877 switch (t14h->proc_ind) {
878 case IUUP_PROC_INIT:
879 rc = osmo_fsm_inst_dispatch(inst->fi, IUUP_FSM_EVT_INIT, itp);
880 break;
881 case IUUP_PROC_RATE_CTRL:
882 case IUUP_PROC_TIME_ALIGN:
883 case IUUP_PROC_ERR_EVENT:
884 LOGPFSML(inst->fi, LOGL_NOTICE, "Received Request for "
885 "unsupported IuUP procedure %u\n", t14h->proc_ind);
886 break;
887 default:
888 LOGPFSML(inst->fi, LOGL_NOTICE, "Received Request for "
889 "unknown IuUP procedure %u\n", t14h->proc_ind);
890 break;
891 }
892 break;
893 case IUUP_AN_ACK:
894 switch (t14h->proc_ind) {
895 case IUUP_PROC_INIT:
896 rc = osmo_fsm_inst_dispatch(inst->fi,
897 IUUP_FSM_EVT_LAST_INIT_ACK, itp);
898 break;
899 default:
900 LOGPFSML(inst->fi, LOGL_ERROR, "Received ACK for "
901 "unknown IuUP procedure %u\n", t14h->proc_ind);
902 break;
903 }
904 break;
905 case IUUP_AN_NACK:
906 switch (t14h->proc_ind) {
907 case IUUP_PROC_INIT:
908 rc = osmo_fsm_inst_dispatch(inst->fi,
909 IUUP_FSM_EVT_INIT_NACK, itp);
910 break;
911 default:
912 LOGPFSML(inst->fi, LOGL_ERROR, "Received NACK for "
913 "unknown IuUP procedure %u\n", t14h->proc_ind);
914 break;
915 }
916 break;
917 default:
918 LOGPFSML(inst->fi, LOGL_ERROR, "Received unknown IuUP ACK/NACK\n");
919 break;
920 }
921 break;
922 default:
923 LOGPFSML(inst->fi, LOGL_NOTICE, "Received unknown IuUP PDU type %u\n",
924 iuup_get_pdu_type(msgb_l2(oph->msg)));
925 break;
926 }
927 break;
928 default:
929 /* exception: return an error code due to a wrong primitive */
930 return -EINVAL;
931 }
932
933 return rc;
934}
935
936/* A IuUP RNL SAP primitive from user (higher layer) */
937int osmo_iuup_rnl_prim_down(struct osmo_iuup_instance *inst, struct osmo_iuup_rnl_prim *irp)
938{
939 struct osmo_prim_hdr *oph = &irp->oph;
940 int rc;
941
942 OSMO_ASSERT(oph->sap == SAP_IUUP_RNL);
943
944 switch (OSMO_PRIM_HDR(oph)) {
945 case OSMO_PRIM(OSMO_IUUP_RNL_CONFIG, PRIM_OP_REQUEST):
946 rc = osmo_fsm_inst_dispatch(inst->fi, IUUP_FSM_EVT_IUUP_CONFIG_REQ, irp);
947 msgb_free(irp->oph.msg);
948 break;
949 case OSMO_PRIM(OSMO_IUUP_RNL_DATA, PRIM_OP_REQUEST):
950 rc = osmo_fsm_inst_dispatch(inst->fi, IUUP_FSM_EVT_IUUP_DATA_REQ, irp);
951 if (rc != 0)
952 msgb_free(irp->oph.msg);
953 break;
954 case OSMO_PRIM(OSMO_IUUP_RNL_STATUS, PRIM_OP_REQUEST):
955 rc = osmo_fsm_inst_dispatch(inst->fi, IUUP_FSM_EVT_IUUP_STATUS_REQ, irp);
956 msgb_free(irp->oph.msg);
957 break;
958 default:
959 rc = -EINVAL;
960 msgb_free(irp->oph.msg);
961 }
962 return rc;
963}
964
965struct osmo_iuup_instance *osmo_iuup_instance_alloc(void *ctx, const char *id)
966{
967 struct osmo_iuup_instance *iui;
968 iui = talloc_zero(ctx, struct osmo_iuup_instance);
969 if (!iui)
970 return NULL;
971
972 iui->fi = osmo_fsm_inst_alloc(&iuup_fsm, NULL, iui, LOGL_DEBUG, id);
973 if (!iui->fi)
974 goto free_ret;
975
976 return iui;
977free_ret:
978 talloc_free(iui);
979 return NULL;
980}
981
982void osmo_iuup_instance_free(struct osmo_iuup_instance *iui)
983{
984 if (!iui)
985 return;
986
987 if (iui->fi)
988 osmo_fsm_inst_free(iui->fi);
989 iui->fi = NULL;
990 talloc_free(iui);
991}
992
993void osmo_iuup_instance_set_user_prim_cb(struct osmo_iuup_instance *iui, osmo_prim_cb func, void *priv)
994{
995 iui->user_prim_cb = func;
996 iui->user_prim_priv = priv;
997}
998void osmo_iuup_instance_set_transport_prim_cb(struct osmo_iuup_instance *iui, osmo_prim_cb func, void *priv)
999{
1000 iui->transport_prim_cb = func;
1001 iui->transport_prim_priv = priv;
1002}
1003
1004static __attribute__((constructor)) void on_dso_load_iuup_fsm(void)
1005{
1006 OSMO_ASSERT(osmo_fsm_register(&iuup_fsm) == 0);
1007}