blob: a26377bb9342e6e8286787560c2a7b90b5e951b9 [file] [log] [blame]
Holger Hans Peter Freytherd11290b2013-10-26 17:32:04 +02001/* decoding
2 *
3 * Copyright (C) 2012 Ivan Klyuchnikov
4 * Copyright (C) 2012 Andreas Eversberg <jolly@eversberg.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20#include <decoding.h>
21#include <rlc.h>
22#include <gprs_debug.h>
23
Jacob Erlbeck4abc6862015-12-08 15:14:05 +010024extern "C" {
25#include <osmocom/core/utils.h>
26}
27
Holger Hans Peter Freytherd11290b2013-10-26 17:32:04 +020028#include <arpa/inet.h>
29
30#include <errno.h>
31#include <string.h>
32
Jacob Erlbeck4abc6862015-12-08 15:14:05 +010033#define LENGTH_TO_END 255
34/*
35 * \returns num extensions fields (num frames == offset) on success,
36 * -errno otherwise.
37 */
38static int parse_extensions_egprs(const uint8_t *data, unsigned int data_len,
39 unsigned int *offs,
40 bool is_last_block,
41 Decoding::RlcData *chunks, unsigned int chunks_size)
42{
43 const struct rlc_li_field_egprs *li;
44 uint8_t e;
45 unsigned int num_chunks = 0;
46 // unsigned int data_area = 0;
47
48 e = 0;
49 while (!e) {
50 if (*offs > data_len) {
51 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA LI extended, "
52 "but no more data\n");
53 return -EINVAL;
54 }
55
56 /* get new E */
57 li = (struct rlc_li_field_egprs *)&data[*offs];
58 e = li->e;
59 *offs += 1;
60
61 if (!chunks)
62 continue;
63
64 if (num_chunks == chunks_size) {
65 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA LI extended, "
66 "but no more chunks possible\n");
67 return -ENOSPC;
68 }
69 if (li->li == 0 && num_chunks == 0 && li->e == 0) {
70 /* TS 44.060, table 10.4.14a.1, row 2a */
71 chunks[num_chunks].length = 0;
72 chunks[num_chunks].is_complete = true;
73 } else if (li->li == 0 && num_chunks == 0 && li->e == 1) {
74 /* TS 44.060, table 10.4.14a.1, row 4 */
75 // chunks[num_chunks].length = data_len - *offs - data_area;
76 chunks[num_chunks].length = LENGTH_TO_END;
77 chunks[num_chunks].is_complete = is_last_block;
78 } else if (li->li == 127 && li->e == 1) {
79 /* TS 44.060, table 10.4.14a.1, row 3 & 5 */
80 /* only filling bytes left */
81 break;
82 } else if (li->li > 0) {
83 /* TS 44.060, table 10.4.14a.1, row 1 & 2b */
84 chunks[num_chunks].length = li->li;
85 chunks[num_chunks].is_complete = true;
86 } else {
87 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA LI contains "
88 "invalid extension octet: LI=%d, E=%d, count=%d\n",
89 li->li, li->e, num_chunks);
90 return -EINVAL;
91 }
92
93 num_chunks += 1;
94
95 if (e == 1) {
96 /* There is space after the last chunk, add a final one */
97 if (num_chunks == chunks_size) {
98 LOGP(DRLCMACUL, LOGL_NOTICE,
99 "UL DATA LI possibly extended, "
100 "but no more chunks possible\n");
101 return -ENOSPC;
102 }
103
104 // chunks[num_chunks].length = data_len - *offs - data_area;
105 chunks[num_chunks].length = LENGTH_TO_END;
106 chunks[num_chunks].is_complete = is_last_block;
107 // data_area += chunks[num_chunks].length;
108 num_chunks += 1;
109 }
110 }
111
112 return num_chunks;
113}
114
115static int parse_extensions_gprs(const uint8_t *data, unsigned int data_len,
116 unsigned int *offs,
117 bool is_last_block,
118 Decoding::RlcData *chunks, unsigned int chunks_size)
119{
120 const struct rlc_li_field *li;
121 uint8_t m, e;
122 unsigned int num_chunks = 0;
123 // unsigned int data_area = 0;
124
125 e = 0;
126 while (!e) {
127 if (*offs > data_len) {
128 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA LI extended, "
129 "but no more data\n");
130 return -EINVAL;
131 }
132
133 /* get new E */
134 li = (const struct rlc_li_field *)&data[*offs];
135 e = li->e;
136 m = li->m;
137 *offs += 1;
138
139 if (li->li == 0) {
140 /* TS 44.060, 10.4.14, par 6 */
141 e = 1;
142 m = 0;
143 }
144
145 /* TS 44.060, table 10.4.13.1 */
146 if (m == 0 && e == 0) {
147 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA "
148 "ignored, because M='0' and E='0'.\n");
149 return 0;
150 }
151
152 if (!chunks)
153 continue;
154
155 if (num_chunks == chunks_size) {
156 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA LI extended, "
157 "but no more chunks possible\n");
158 return -ENOSPC;
159 }
160
161 if (li->li == 0)
162 /* e is 1 here */
163 // chunks[num_chunks].length = data_len - *offs - data_area;
164 chunks[num_chunks].length = LENGTH_TO_END;
165 else
166 chunks[num_chunks].length = li->li;
167
168 chunks[num_chunks].is_complete = li->li || is_last_block;
169
170 // data_area += chunks[num_chunks].length;
171 num_chunks += 1;
172
173 if (e == 1 && m == 1) {
174 if (num_chunks == chunks_size) {
175 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA LI extended, "
176 "but no more chunks possible\n");
177 return -ENOSPC;
178 }
179 /* TS 44.060, 10.4.13.1, row 4 */
180 // chunks[num_chunks].length = data_len - *offs - data_area;
181 chunks[num_chunks].length = LENGTH_TO_END;
182 chunks[num_chunks].is_complete = is_last_block;
183 num_chunks += 1;
184 }
185 }
186
187 return num_chunks;
188}
189
190int Decoding::rlc_data_from_ul_data(
191 const struct gprs_rlc_ul_data_block_info *rdbi, GprsCodingScheme cs,
192 const uint8_t *data, RlcData *chunks, unsigned int chunks_size,
193 uint32_t *tlli)
194{
195 uint8_t e;
196 unsigned int data_len = rdbi->data_len;
197 unsigned int num_chunks = 0, i;
198 unsigned int offs = 0;
199 bool is_last_block = (rdbi->cv == 0);
200
201 if (!chunks)
202 chunks_size = 0;
203
204 e = rdbi->e;
205 if (e) {
206 if (chunks_size > 0) {
207 chunks[num_chunks].offset = offs;
208 chunks[num_chunks].length = LENGTH_TO_END;
209 chunks[num_chunks].is_complete = is_last_block;
210 num_chunks += 1;
211 } else if (chunks) {
212 LOGP(DRLCMACUL, LOGL_NOTICE, "No extension, "
213 "but no more chunks possible\n");
214 return -ENOSPC;
215 }
216 } else if (cs.isEgprs()) {
217 /* if E is not set (LI follows), EGPRS */
218 num_chunks = parse_extensions_egprs(data, data_len, &offs,
219 is_last_block,
220 chunks, chunks_size);
221 } else {
222 /* if E is not set (LI follows), GPRS */
223 num_chunks = parse_extensions_gprs(data, data_len, &offs,
224 is_last_block,
225 chunks, chunks_size);
226 }
227
228 /* TLLI */
229 if (rdbi->ti) {
230 uint32_t tlli_enc;
231 if (offs + 4 > data_len) {
232 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA TLLI out of block "
233 "border\n");
234 return -EINVAL;
235 }
236
237 memcpy(&tlli_enc, data + offs, sizeof(tlli_enc));
238 if (cs.isGprs())
239 /* The TLLI is encoded in big endian for GPRS (see
240 * TS 44.060, figure 10.2.2.1, note) */
241 *tlli = be32toh(tlli_enc);
242 else
243 /* The TLLI is encoded in little endian for EGPRS (see
244 * TS 44.060, figure 10.3a.2.1, note 2) */
245 *tlli = le32toh(tlli_enc);
246
247 offs += sizeof(tlli_enc);
248 } else {
249 *tlli = 0;
250 }
251
252 /* PFI */
253 if (rdbi->pi) {
254 LOGP(DRLCMACUL, LOGL_ERROR, "ERROR: PFI not supported, "
255 "please disable in SYSTEM INFORMATION\n");
256 return -ENOTSUP;
257
258 /* TODO: Skip all extensions with E=0 (see TS 44.060, 10.4.11 */
259 }
260
261 /* LLC */
262 for (i = 0; i < num_chunks; i++) {
263 chunks[i].offset = offs;
264 if (chunks[i].length == LENGTH_TO_END) {
265 if (offs == data_len) {
266 /* There is no place for an additional chunk,
267 * so drop it (this may happen with EGPRS since
268 * there is no M flag. */
269 num_chunks -= 1;
270 break;;
271 }
272 chunks[i].length = data_len - offs;
273 }
274 offs += chunks[i].length;
275 if (offs > data_len) {
276 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA out of block "
277 "border, chunk idx: %d, size: %d\n",
278 i, chunks[i].length);
279 return -EINVAL;
280 }
281 }
282
283 return num_chunks;
284}
Holger Hans Peter Freytherd11290b2013-10-26 17:32:04 +0200285
286int Decoding::tlli_from_ul_data(const uint8_t *data, uint8_t len,
287 uint32_t *tlli)
288{
289 struct rlc_ul_header *rh = (struct rlc_ul_header *)data;
290 struct rlc_li_field *li;
291 uint8_t e;
292 uint32_t _tlli;
293
294 if (!rh->ti)
295 return -EINVAL;
296
297 data += 3;
298 len -= 3;
299 e = rh->e;
300 /* if E is not set (LI follows) */
301 while (!e) {
302 if (!len) {
303 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA LI extended, "
304 "but no more data\n");
305 return -EINVAL;
306 }
307 /* get new E */
308 li = (struct rlc_li_field *)data;
309 if (li->e == 0) /* if LI==0, E is interpreted as '1' */
310 e = 1;
311 else
312 e = li->e;
313 data++;
314 len--;
315 }
316 if (len < 4) {
317 LOGP(DRLCMACUL, LOGL_NOTICE, "UL DATA TLLI out of frame "
318 "border\n");
319 return -EINVAL;
320 }
321 memcpy(&_tlli, data, 4);
322 *tlli = ntohl(_tlli);
323
324 return 0;
325}
326
Holger Hans Peter Freytherfcbc7022013-10-26 17:38:37 +0200327uint8_t Decoding::get_ms_class_by_capability(MS_Radio_Access_capability_t *cap)
328{
329 int i;
330
331 for (i = 0; i < cap->Count_MS_RA_capability_value; i++) {
332 if (!cap->MS_RA_capability_value[i].u.Content.Exist_Multislot_capability)
333 continue;
334 if (!cap->MS_RA_capability_value[i].u.Content.Multislot_capability.Exist_GPRS_multislot_class)
335 continue;
336 return cap->MS_RA_capability_value[i].u.Content.Multislot_capability.GPRS_multislot_class;
337 }
338
339 return 0;
340}
341
Jacob Erlbeckc3c58042015-09-28 17:55:32 +0200342uint8_t Decoding::get_egprs_ms_class_by_capability(MS_Radio_Access_capability_t *cap)
343{
344 int i;
345
346 for (i = 0; i < cap->Count_MS_RA_capability_value; i++) {
347 if (!cap->MS_RA_capability_value[i].u.Content.Exist_Multislot_capability)
348 continue;
349 if (!cap->MS_RA_capability_value[i].u.Content.Multislot_capability.Exist_EGPRS_multislot_class)
350 continue;
351 return cap->MS_RA_capability_value[i].u.Content.Multislot_capability.EGPRS_multislot_class;
352 }
353
354 return 0;
355}
356
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +0100357/**
358 * show_rbb needs to be an array with 65 elements
Daniel Willmann5e94cd42013-12-11 20:11:16 +0100359 * The index of the array is the bit position in the rbb
360 * (show_rbb[63] relates to BSN ssn-1)
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +0100361 */
362void Decoding::extract_rbb(const uint8_t *rbb, char *show_rbb)
363{
Daniel Willmann5e94cd42013-12-11 20:11:16 +0100364 for (int i = 0; i < 64; i++) {
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +0100365 uint8_t bit;
366
Daniel Willmann5e94cd42013-12-11 20:11:16 +0100367 bit = !!(rbb[i/8] & (1<<(7-i%8)));
Daniel Willmann6f7cb2c2013-12-11 14:25:20 +0100368 show_rbb[i] = bit ? 'R' : 'I';
Holger Hans Peter Freytherdf6b4f52013-11-24 17:05:48 +0100369 }
370
371 show_rbb[64] = '\0';
372}
Jacob Erlbeck4abc6862015-12-08 15:14:05 +0100373
374int Decoding::rlc_parse_ul_data_header(struct gprs_rlc_ul_header_egprs *rlc,
375 const uint8_t *data, GprsCodingScheme cs)
376{
377 const struct gprs_rlc_ul_header_egprs_3 *egprs3;
378 const struct rlc_ul_header *gprs;
379 unsigned int e_ti_header;
380 unsigned int cur_bit = 0;
381 unsigned int data_len = 0;
382
383 rlc->cs = cs;
384
385 data_len = cs.maxDataBlockBytes();
386
387 switch(cs.headerTypeData()) {
388 case GprsCodingScheme::HEADER_GPRS_DATA:
389 gprs = static_cast<struct rlc_ul_header *>
390 ((void *)data);
391 rlc->r = gprs->r;
392 rlc->si = gprs->si;
393 rlc->tfi = gprs->tfi;
394 rlc->cps = 0;
395 rlc->rsb = 0;
396
397 rlc->num_data_blocks = 1;
398 rlc->block_info[0].cv = gprs->cv;
399 rlc->block_info[0].pi = gprs->pi;
400 rlc->block_info[0].bsn = gprs->bsn;
401 rlc->block_info[0].e = gprs->e;
402 rlc->block_info[0].ti = gprs->ti;
403 rlc->block_info[0].spb = 0;
404
405 cur_bit += 3 * 8;
406
407 rlc->data_offs_bits[0] = cur_bit;
408 rlc->block_info[0].data_len = data_len;
409 cur_bit += data_len * 8;
410
411 break;
412 case GprsCodingScheme::HEADER_EGPRS_DATA_TYPE_3:
413 egprs3 = static_cast<struct gprs_rlc_ul_header_egprs_3 *>
414 ((void *)data);
415 rlc->r = egprs3->r;
416 rlc->si = egprs3->si;
417 rlc->tfi = (egprs3->tfi_a << 0) | (egprs3->tfi_b << 2);
418 rlc->cps = (egprs3->cps_a << 0) | (egprs3->cps_b << 2);
419 rlc->rsb = egprs3->rsb;
420
421 rlc->num_data_blocks = 1;
422 rlc->block_info[0].cv = egprs3->cv;
423 rlc->block_info[0].pi = egprs3->pi;
424 rlc->block_info[0].spb = egprs3->spb;
425 rlc->block_info[0].bsn =
426 (egprs3->bsn1_a << 0) | (egprs3->bsn1_b << 5);
427
428 cur_bit += 3 * 8 + 7;
429
430 e_ti_header = (data[3] + (data[4] << 8)) >> 7;
431 rlc->block_info[0].e = !!(e_ti_header & 0x01);
432 rlc->block_info[0].ti = !!(e_ti_header & 0x02);
433 cur_bit += 2;
434
435 rlc->data_offs_bits[0] = cur_bit;
436 rlc->block_info[0].data_len = data_len;
437 cur_bit += data_len * 8;
438
439 break;
440
441 case GprsCodingScheme::HEADER_EGPRS_DATA_TYPE_1:
442 case GprsCodingScheme::HEADER_EGPRS_DATA_TYPE_2:
443 /* TODO: Support both header types */
444 /* fall through */
445 default:
446 LOGP(DRLCMACDL, LOGL_ERROR,
447 "Decoding of uplink %s data blocks not yet supported.\n",
448 cs.name());
449 return -ENOTSUP;
450 };
451
452 return cur_bit;
453}
454
455/**
456 * \brief Copy LSB bitstream RLC data block to byte aligned buffer.
457 *
458 * Note that the bitstream is encoded in LSB first order, so the two octets
459 * 654321xx xxxxxx87 contain the octet 87654321 starting at bit position 3
460 * (LSB has bit position 1). This is a different order than the one used by
461 * CSN.1.
462 *
463 * \param data_block_idx The block index, 0..1 for header type 1, 0 otherwise
464 * \param src A pointer to the start of the RLC block (incl. the header)
465 * \param buffer A data area of a least the size of the RLC block
466 * \returns the number of bytes copied
467 */
468unsigned int Decoding::rlc_copy_to_aligned_buffer(
469 const struct gprs_rlc_ul_header_egprs *rlc,
470 unsigned int data_block_idx,
471 const uint8_t *src, uint8_t *buffer)
472{
473 unsigned int hdr_bytes;
474 unsigned int extra_bits;
475 unsigned int i;
476
477 uint8_t c, last_c;
478 uint8_t *dst;
479 const struct gprs_rlc_ul_data_block_info *rdbi;
480
481 OSMO_ASSERT(data_block_idx < rlc->num_data_blocks);
482 rdbi = &rlc->block_info[data_block_idx];
483
484 hdr_bytes = rlc->data_offs_bits[data_block_idx] >> 3;
485 extra_bits = (rlc->data_offs_bits[data_block_idx] & 7);
486
487 if (extra_bits == 0) {
488 /* It is aligned already */
489 memmove(buffer, src + hdr_bytes, rdbi->data_len);
490 return rdbi->data_len;
491 }
492
493 dst = buffer;
494 src = src + hdr_bytes;
495 last_c = *(src++);
496
497 for (i = 0; i < rdbi->data_len; i++) {
498 c = src[i];
499 *(dst++) = (last_c >> extra_bits) | (c << (8 - extra_bits));
500 last_c = c;
501 }
502
503 return rdbi->data_len;
504}
505
506/**
507 * \brief Get a pointer to byte aligned RLC data.
508 *
509 * Since the RLC data may not be byte aligned to the RLC block data such that a
510 * single RLC data byte is spread over two RLC block bytes, this function
511 * eventually uses the provided buffer as data storage.
512 *
513 * \param src A pointer to the start of the RLC block (incl. the header)
514 * \param buffer A data area of a least the size of the RLC block
515 * \returns A pointer to the RLC data start within src if it is aligned, and
516 * buffer otherwise.
517 */
518const uint8_t *Decoding::rlc_get_data_aligned(
519 const struct gprs_rlc_ul_header_egprs *rlc,
520 unsigned int data_block_idx,
521 const uint8_t *src, uint8_t *buffer)
522{
523 unsigned int hdr_bytes;
524 unsigned int extra_bits;
525
526 OSMO_ASSERT(data_block_idx < ARRAY_SIZE(rlc->data_offs_bits));
527
528 hdr_bytes = rlc->data_offs_bits[data_block_idx] >> 3;
529 extra_bits = (rlc->data_offs_bits[data_block_idx] & 7);
530
531 if (extra_bits == 0)
532 /* It is aligned already, return a pointer that refers to the
533 * original data. */
534 return src + hdr_bytes;
535
536 Decoding::rlc_copy_to_aligned_buffer(rlc, data_block_idx, src, buffer);
537 return buffer;
538}