blob: 6d40bec4d77619f4e949d602656107aabf3cd667 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file gsm48_ie.c
2 * GSM Mobile Radio Interface Layer 3 messages.
3 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0. */
4/*
5 * (C) 2008 by Harald Welte <laforge@gnumonks.org>
Harald Welteb1ac2b92010-04-09 07:50:18 +02006 * (C) 2009-2010 by Andreas Eversberg
Harald Welte1e908662010-03-07 23:39:54 +01007 *
8 * All Rights Reserved
9 *
Harald Weltee08da972017-11-13 01:00:26 +090010 * SPDX-License-Identifier: GPL-2.0+
11 *
Harald Welte1e908662010-03-07 23:39:54 +010012 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
Harald Welte1e908662010-03-07 23:39:54 +010022 */
23
24
25#include <stdint.h>
26#include <string.h>
27#include <errno.h>
28
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010029#include <osmocom/core/utils.h>
30#include <osmocom/core/msgb.h>
31#include <osmocom/gsm/tlv.h>
32#include <osmocom/gsm/mncc.h>
Philipp Maiere36be562020-11-12 11:33:54 +010033#include <osmocom/core/bitvec.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010034#include <osmocom/gsm/protocol/gsm_04_08.h>
35#include <osmocom/gsm/gsm48_ie.h>
Harald Welte1e908662010-03-07 23:39:54 +010036
Harald Welte96e2a002017-06-12 21:44:18 +020037/*! \addtogroup gsm0408
38 * @{
39 */
40
Harald Welte1e908662010-03-07 23:39:54 +010041static const char bcd_num_digits[] = {
42 '0', '1', '2', '3', '4', '5', '6', '7',
43 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
44};
45
Neels Hofmeyr8212fc62019-04-01 14:34:37 +020046/*! Like gsm48_decode_bcd_number2() but with less airtight bounds checking.
Harald Welte96e2a002017-06-12 21:44:18 +020047 * \param[out] Caller-provided output buffer
48 * \param[in] bcd_lv Length-Value portion of to-be-decoded IE
49 * \param[in] h_len Length of an optional heder between L and V portion
50 * \returns - in case of success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +010051int gsm48_decode_bcd_number(char *output, int output_len,
52 const uint8_t *bcd_lv, int h_len)
53{
54 uint8_t in_len = bcd_lv[0];
Neels Hofmeyr83d45312019-04-29 19:15:11 +020055 /* Just assume the input buffer is big enough for the length byte and the following data, so pass in_len + 1 for
56 * the input buffer size. */
57 return gsm48_decode_bcd_number2(output, output_len, bcd_lv, in_len + 1, h_len);
58}
59
60/*! Decode a 'called/calling/connect party BCD number' as in 10.5.4.7.
61 * \param[out] output Caller-provided output buffer.
62 * \param[in] output_len sizeof(output).
63 * \param[in] bcd_lv Length-Value part of to-be-decoded IE.
64 * \param[in] input_len Size of the bcd_lv buffer for bounds checking.
65 * \param[in] h_len Length of an optional header between L and V parts.
Vadim Yanitskiy71940872019-05-26 00:49:57 +070066 * \return 0 in case of success, negative on error.
67 *
68 * Errors checked:
69 * - no or too little input data (-EIO),
Vadim Yanitskiye4799f52019-05-26 00:55:20 +070070 * - IE length exceeds input data size (-EINVAL),
Vadim Yanitskiy71940872019-05-26 00:49:57 +070071 * - no or too little output buffer size (-ENOSPC),
72 * - decoded number exceeds size of the output buffer (-ENOSPC).
73 *
74 * The output is guaranteed to be nul terminated iff output_len > 0.
Neels Hofmeyr83d45312019-04-29 19:15:11 +020075 */
76int gsm48_decode_bcd_number2(char *output, size_t output_len,
77 const uint8_t *bcd_lv, size_t input_len,
78 size_t h_len)
79{
80 uint8_t in_len;
Harald Welte1e908662010-03-07 23:39:54 +010081 int i;
Oliver Smith186f8782019-06-06 16:11:32 +020082 bool truncated = false;
Neels Hofmeyr83d45312019-04-29 19:15:11 +020083 if (output_len < 1)
84 return -ENOSPC;
85 *output = '\0';
86 if (input_len < 1)
87 return -EIO;
88 in_len = bcd_lv[0];
89 /* len + 1: the BCD length plus the length byte itself must fit in the input buffer. */
90 if (input_len < in_len + 1)
Vadim Yanitskiye4799f52019-05-26 00:55:20 +070091 return -EINVAL;
Harald Welte1e908662010-03-07 23:39:54 +010092
93 for (i = 1 + h_len; i <= in_len; i++) {
94 /* lower nibble */
Oliver Smith186f8782019-06-06 16:11:32 +020095 if (output_len <= 1) {
96 truncated = true;
Harald Welte1e908662010-03-07 23:39:54 +010097 break;
Oliver Smith186f8782019-06-06 16:11:32 +020098 }
Harald Welte1e908662010-03-07 23:39:54 +010099 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
Vadim Yanitskiy2cd1dda2019-05-26 00:14:16 +0700100 output_len--;
Harald Welte1e908662010-03-07 23:39:54 +0100101
102 /* higher nibble */
Oliver Smith186f8782019-06-06 16:11:32 +0200103 if (output_len <= 1) {
104 /* not truncated if there is exactly one 0xf ('\0') higher nibble remaining */
105 if (i == in_len && (bcd_lv[i] & 0xf0) == 0xf0) {
106 break;
107 }
108
109 truncated = true;
Harald Welte1e908662010-03-07 23:39:54 +0100110 break;
Oliver Smith186f8782019-06-06 16:11:32 +0200111 }
Harald Welte1e908662010-03-07 23:39:54 +0100112 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
Vadim Yanitskiy2cd1dda2019-05-26 00:14:16 +0700113 output_len--;
Harald Welte1e908662010-03-07 23:39:54 +0100114 }
115 if (output_len >= 1)
116 *output++ = '\0';
117
Vadim Yanitskiy71940872019-05-26 00:49:57 +0700118 /* Indicate whether the output was truncated */
Oliver Smith186f8782019-06-06 16:11:32 +0200119 if (truncated)
Vadim Yanitskiy71940872019-05-26 00:49:57 +0700120 return -ENOSPC;
121
Harald Welte1e908662010-03-07 23:39:54 +0100122 return 0;
123}
124
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200125/*! convert a single ASCII character to call-control BCD */
Harald Welte1e908662010-03-07 23:39:54 +0100126static int asc_to_bcd(const char asc)
127{
128 int i;
129
130 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
131 if (bcd_num_digits[i] == asc)
132 return i;
133 }
134 return -EINVAL;
135}
136
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200137/*! convert a ASCII phone number to 'called/calling/connect party BCD number'
Harald Welte96e2a002017-06-12 21:44:18 +0200138 * \param[out] bcd_lv Caller-provided output buffer
139 * \param[in] max_len Maximum Length of \a bcd_lv
140 * \param[in] h_len Length of an optional heder between L and V portion
141 * \param[in] input phone number as 0-terminated ASCII
Vadim Yanitskiy1dc82642019-05-27 00:53:54 +0700142 * \returns number of bytes used in \a bcd_lv
143 *
144 * Depending on a context (e.g. called or calling party BCD number), the
145 * optional header between L and V parts can contain TON (Type Of Number),
146 * NPI (Numbering Plan Indication), presentation or screening indicator.
147 * NOTE: it is up to the caller to initialize this header!
148 */
Harald Welte1e908662010-03-07 23:39:54 +0100149int gsm48_encode_bcd_number(uint8_t *bcd_lv, uint8_t max_len,
150 int h_len, const char *input)
151{
152 int in_len = strlen(input);
153 int i;
154 uint8_t *bcd_cur = bcd_lv + 1 + h_len;
155
156 /* two digits per byte, plus type byte */
157 bcd_lv[0] = in_len/2 + h_len;
158 if (in_len % 2)
159 bcd_lv[0]++;
160
161 if (bcd_lv[0] > max_len)
162 return -EIO;
163
164 for (i = 0; i < in_len; i++) {
165 int rc = asc_to_bcd(input[i]);
166 if (rc < 0)
167 return rc;
168 if (i % 2 == 0)
169 *bcd_cur = rc;
170 else
171 *bcd_cur++ |= (rc << 4);
172 }
173 /* append padding nibble in case of odd length */
174 if (i % 2)
175 *bcd_cur++ |= 0xf0;
176
177 /* return how many bytes we used */
178 return (bcd_cur - bcd_lv);
179}
180
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200181/*! Decode TS 04.08 Bearer Capability IE (10.5.4.5)
Harald Welte96e2a002017-06-12 21:44:18 +0200182 * \param[out] Caller-provided memory for decoded output
183 * \[aram[in] LV portion of TS 04.08 Bearer Capability
184 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100185int gsm48_decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
186 const uint8_t *lv)
187{
188 uint8_t in_len = lv[0];
189 int i, s;
190
191 if (in_len < 1)
192 return -EINVAL;
193
194 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
195
196 /* octet 3 */
197 bcap->transfer = lv[1] & 0x07;
198 bcap->mode = (lv[1] & 0x08) >> 3;
199 bcap->coding = (lv[1] & 0x10) >> 4;
200 bcap->radio = (lv[1] & 0x60) >> 5;
201
Harald Weltec8a0b932012-08-24 21:27:26 +0200202 switch (bcap->transfer) {
203 case GSM_MNCC_BCAP_SPEECH:
Harald Welte1e908662010-03-07 23:39:54 +0100204 i = 1;
205 s = 0;
206 while(!(lv[i] & 0x80)) {
207 i++; /* octet 3a etc */
208 if (in_len < i)
209 return 0;
210 bcap->speech_ver[s++] = lv[i] & 0x0f;
211 bcap->speech_ver[s] = -1; /* end of list */
212 if (i == 2) /* octet 3a */
213 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
214 if (s == 7) /* maximum speech versions + end of list */
215 return 0;
216 }
Harald Weltec8a0b932012-08-24 21:27:26 +0200217 break;
218 case GSM_MNCC_BCAP_UNR_DIG:
219 case GSM_MNCC_BCAP_FAX_G3:
220 i = 1;
221 while(!(lv[i] & 0x80)) {
222 i++; /* octet 3a etc */
223 if (in_len < i)
224 return 0;
225 /* ignore them */
226 }
227 /* octet 4: skip */
228 i++;
229 /* octet 5 */
230 i++;
231 if (in_len < i)
232 return 0;
233 bcap->data.rate_adaption = (lv[i] >> 3) & 3;
234 bcap->data.sig_access = lv[i] & 7;
235 while(!(lv[i] & 0x80)) {
236 i++; /* octet 5a etc */
237 if (in_len < i)
238 return 0;
239 /* ignore them */
240 }
241 /* octet 6 */
242 i++;
243 if (in_len < i)
244 return 0;
245 bcap->data.async = lv[i] & 1;
246 if (!(lv[i] & 0x80)) {
247 i++;
248 if (in_len < i)
249 return 0;
250 /* octet 6a */
251 bcap->data.nr_stop_bits = ((lv[i] >> 7) & 1) + 1;
252 if (lv[i] & 0x10)
253 bcap->data.nr_data_bits = 8;
254 else
255 bcap->data.nr_data_bits = 7;
256 bcap->data.user_rate = lv[i] & 0xf;
257
258 if (!(lv[i] & 0x80)) {
259 i++;
260 if (in_len < i)
261 return 0;
262 /* octet 6b */
263 bcap->data.parity = lv[i] & 7;
264 bcap->data.interm_rate = (lv[i] >> 5) & 3;
265
266 /* octet 6c */
267 if (!(lv[i] & 0x80)) {
268 i++;
269 if (in_len < i)
270 return 0;
271 bcap->data.transp = (lv[i] >> 5) & 3;
272 bcap->data.modem_type = lv[i] & 0x1F;
273 }
274 }
275
276 }
277 break;
278 default:
Harald Welte1e908662010-03-07 23:39:54 +0100279 i = 1;
280 while (!(lv[i] & 0x80)) {
281 i++; /* octet 3a etc */
282 if (in_len < i)
283 return 0;
284 /* ignore them */
285 }
286 /* FIXME: implement OCTET 4+ parsing */
Harald Weltec8a0b932012-08-24 21:27:26 +0200287 break;
Harald Welte1e908662010-03-07 23:39:54 +0100288 }
289
290 return 0;
291}
292
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200293/*! Encode TS 04.08 Bearer Capability IE (10.5.4.5)
Harald Welte96e2a002017-06-12 21:44:18 +0200294 * \param[out] msg Message Buffer to which IE is to be appended
295 * \param[in] lv_only Write only LV portion (1) or TLV (0)
296 * \param[in] bcap Decoded Bearer Capability to be encoded
297 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100298int gsm48_encode_bearer_cap(struct msgb *msg, int lv_only,
299 const struct gsm_mncc_bearer_cap *bcap)
300{
301 uint8_t lv[32 + 1];
302 int i = 1, s;
303
304 lv[1] = bcap->transfer;
305 lv[1] |= bcap->mode << 3;
306 lv[1] |= bcap->coding << 4;
307 lv[1] |= bcap->radio << 5;
308
Harald Weltec8a0b932012-08-24 21:27:26 +0200309 switch (bcap->transfer) {
310 case GSM_MNCC_BCAP_SPEECH:
Harald Welte1e908662010-03-07 23:39:54 +0100311 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
312 i++; /* octet 3a etc */
313 lv[i] = bcap->speech_ver[s];
314 if (i == 2) /* octet 3a */
315 lv[i] |= bcap->speech_ctm << 5;
316 }
317 lv[i] |= 0x80; /* last IE of octet 3 etc */
Harald Weltec8a0b932012-08-24 21:27:26 +0200318 break;
319 case GSM48_BCAP_ITCAP_UNR_DIG_INF:
320 case GSM48_BCAP_ITCAP_FAX_G3:
321 lv[i++] |= 0x80; /* last IE of octet 3 etc */
322 /* octet 4 */
323 lv[i++] = 0xb8;
324 /* octet 5 */
325 lv[i++] = 0x80 | ((bcap->data.rate_adaption & 3) << 3)
326 | (bcap->data.sig_access & 7);
327 /* octet 6 */
328 lv[i++] = 0x20 | (bcap->data.async & 1);
329 /* octet 6a */
330 lv[i++] = (bcap->data.user_rate & 0xf) |
331 (bcap->data.nr_data_bits == 8 ? 0x10 : 0x00) |
332 (bcap->data.nr_stop_bits == 2 ? 0x40 : 0x00);
333 /* octet 6b */
334 lv[i++] = (bcap->data.parity & 7) |
335 ((bcap->data.interm_rate & 3) << 5);
336 /* octet 6c */
337 lv[i] = 0x80 | (bcap->data.modem_type & 0x1f);
338 break;
339 default:
340 return -EINVAL;
Harald Welte1e908662010-03-07 23:39:54 +0100341 }
342
343 lv[0] = i;
344 if (lv_only)
345 msgb_lv_put(msg, lv[0], lv+1);
346 else
347 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
348
349 return 0;
350}
351
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200352/*! Decode TS 04.08 Call Control Capabilities IE (10.5.4.5a)
Harald Welte96e2a002017-06-12 21:44:18 +0200353 * \param[out] Caller-provided memory for decoded CC capabilities
354 * \param[in] lv Length-Value of IE
355 * \retursns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100356int gsm48_decode_cccap(struct gsm_mncc_cccap *ccap, const uint8_t *lv)
357{
358 uint8_t in_len = lv[0];
359
360 if (in_len < 1)
361 return -EINVAL;
362
363 /* octet 3 */
364 ccap->dtmf = lv[1] & 0x01;
365 ccap->pcp = (lv[1] & 0x02) >> 1;
366
367 return 0;
368}
369
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200370/*! Encodoe TS 04.08 Call Control Capabilities (10.5.4.5a)
Harald Welte96e2a002017-06-12 21:44:18 +0200371 * \param[out] msg Message Buffer to which to append IE (as TLV)
372 * \param[in] ccap Decoded CC Capabilities to be encoded
373 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100374int gsm48_encode_cccap(struct msgb *msg,
375 const struct gsm_mncc_cccap *ccap)
376{
377 uint8_t lv[2];
378
379 lv[0] = 1;
380 lv[1] = 0;
381 if (ccap->dtmf)
382 lv [1] |= 0x01;
383 if (ccap->pcp)
384 lv [1] |= 0x02;
385
386 msgb_tlv_put(msg, GSM48_IE_CC_CAP, lv[0], lv+1);
387
388 return 0;
389}
390
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200391/*! Decode TS 04.08 Called Party BCD Number IE (10.5.4.7)
Harald Welte96e2a002017-06-12 21:44:18 +0200392 * \param[out] called Caller-provided memory for decoded number
393 * \param[in] lv Length-Value portion of IE
394 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100395int gsm48_decode_called(struct gsm_mncc_number *called,
396 const uint8_t *lv)
397{
398 uint8_t in_len = lv[0];
399
400 if (in_len < 1)
401 return -EINVAL;
402
403 /* octet 3 */
404 called->plan = lv[1] & 0x0f;
405 called->type = (lv[1] & 0x70) >> 4;
406
407 /* octet 4..N */
408 gsm48_decode_bcd_number(called->number, sizeof(called->number), lv, 1);
409
410 return 0;
411}
412
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200413/*! Encode TS 04.08 Called Party IE (10.5.4.7)
Harald Welte96e2a002017-06-12 21:44:18 +0200414 * \param[out] msg Mesage Buffer to which to append IE (as TLV)
415 * \param[in] called MNCC Number to encode/append
416 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100417int gsm48_encode_called(struct msgb *msg,
418 const struct gsm_mncc_number *called)
419{
420 uint8_t lv[18];
421 int ret;
422
423 /* octet 3 */
Sylvain Munaut47ee6932010-09-20 20:59:23 +0200424 lv[1] = 0x80; /* no extension */
425 lv[1] |= called->plan;
Harald Welte1e908662010-03-07 23:39:54 +0100426 lv[1] |= called->type << 4;
427
428 /* octet 4..N, octet 2 */
429 ret = gsm48_encode_bcd_number(lv, sizeof(lv), 1, called->number);
430 if (ret < 0)
431 return ret;
432
433 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
434
435 return 0;
436}
437
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200438/*! Decode TS 04.08 Caller ID
Harald Welte96e2a002017-06-12 21:44:18 +0200439 * \param[out] called Caller-provided memory for decoded number
440 * \param[in] lv Length-Value portion of IE
441 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100442int gsm48_decode_callerid(struct gsm_mncc_number *callerid,
443 const uint8_t *lv)
444{
445 uint8_t in_len = lv[0];
446 int i = 1;
447
448 if (in_len < 1)
449 return -EINVAL;
450
451 /* octet 3 */
452 callerid->plan = lv[1] & 0x0f;
453 callerid->type = (lv[1] & 0x70) >> 4;
454
455 /* octet 3a */
456 if (!(lv[1] & 0x80)) {
457 callerid->screen = lv[2] & 0x03;
458 callerid->present = (lv[2] & 0x60) >> 5;
459 i = 2;
460 }
461
462 /* octet 4..N */
463 gsm48_decode_bcd_number(callerid->number, sizeof(callerid->number), lv, i);
464
465 return 0;
466}
467
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200468/*! Encode TS 04.08 Caller ID IE
Harald Welte96e2a002017-06-12 21:44:18 +0200469 * \param[out] msg Mesage Buffer to which to append IE (as TLV)
470 * \param[in] ie IE Identifier (tag)
471 * \param[in] max_len maximum generated output in bytes
472 * \param[in] callerid MNCC Number to encode/append
473 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100474int gsm48_encode_callerid(struct msgb *msg, int ie, int max_len,
475 const struct gsm_mncc_number *callerid)
476{
477 uint8_t lv[max_len - 1];
478 int h_len = 1;
479 int ret;
480
481 /* octet 3 */
482 lv[1] = callerid->plan;
483 lv[1] |= callerid->type << 4;
484
485 if (callerid->present || callerid->screen) {
486 /* octet 3a */
487 lv[2] = callerid->screen;
488 lv[2] |= callerid->present << 5;
489 lv[2] |= 0x80;
490 h_len++;
491 } else
492 lv[1] |= 0x80;
493
494 /* octet 4..N, octet 2 */
495 ret = gsm48_encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
496 if (ret < 0)
497 return ret;
498
499 msgb_tlv_put(msg, ie, lv[0], lv+1);
500
501 return 0;
502}
503
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200504/*! Decode TS 04.08 Cause IE (10.5.4.11)
Harald Welte96e2a002017-06-12 21:44:18 +0200505 * \param[out] cause Caller-provided memory for output
506 * \param[in] lv LV portion of Cause IE
507 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100508int gsm48_decode_cause(struct gsm_mncc_cause *cause,
509 const uint8_t *lv)
510{
511 uint8_t in_len = lv[0];
512 int i;
513
514 if (in_len < 2)
515 return -EINVAL;
516
517 cause->diag_len = 0;
518
519 /* octet 3 */
520 cause->location = lv[1] & 0x0f;
521 cause->coding = (lv[1] & 0x60) >> 5;
522
523 i = 1;
524 if (!(lv[i] & 0x80)) {
525 i++; /* octet 3a */
526 if (in_len < i+1)
527 return 0;
528 cause->rec = 1;
529 cause->rec_val = lv[i] & 0x7f;
530 }
531 i++;
532
533 /* octet 4 */
534 cause->value = lv[i] & 0x7f;
535 i++;
536
537 if (in_len < i) /* no diag */
538 return 0;
539
540 if (in_len - (i-1) > 32) /* maximum 32 octets */
541 return 0;
542
543 /* octet 5-N */
544 memcpy(cause->diag, lv + i, in_len - (i-1));
545 cause->diag_len = in_len - (i-1);
546
547 return 0;
548}
549
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200550/*! Encode TS 04.08 Cause IE (10.5.4.11)
Harald Welte96e2a002017-06-12 21:44:18 +0200551 * \param[out] msg Message Buffer to which to append IE
552 * \param[in] lv_only Encode as LV (1) or TLV (0)
553 * \param[in] cause Cause value to be encoded
554 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100555int gsm48_encode_cause(struct msgb *msg, int lv_only,
556 const struct gsm_mncc_cause *cause)
557{
558 uint8_t lv[32+4];
559 int i;
560
561 if (cause->diag_len > 32)
562 return -EINVAL;
563
564 /* octet 3 */
565 lv[1] = cause->location;
566 lv[1] |= cause->coding << 5;
567
568 i = 1;
569 if (cause->rec) {
570 i++; /* octet 3a */
571 lv[i] = cause->rec_val;
572 }
573 lv[i] |= 0x80; /* end of octet 3 */
574
575 /* octet 4 */
576 i++;
577 lv[i] = 0x80 | cause->value;
578
579 /* octet 5-N */
580 if (cause->diag_len) {
581 memcpy(lv + i, cause->diag, cause->diag_len);
582 i += cause->diag_len;
583 }
584
585 lv[0] = i;
586 if (lv_only)
587 msgb_lv_put(msg, lv[0], lv+1);
588 else
589 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
590
591 return 0;
592}
593
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200594/*! Decode TS 04.08 Calling Number IE (10.5.4.9) */
Harald Welte1e908662010-03-07 23:39:54 +0100595int gsm48_decode_calling(struct gsm_mncc_number *calling,
596 const uint8_t *lv)
597{
598 return gsm48_decode_callerid(calling, lv);
599}
600
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200601/*! Encode TS 04.08 Calling Number IE (10.5.4.9) */
Harald Welte1e908662010-03-07 23:39:54 +0100602int gsm48_encode_calling(struct msgb *msg,
603 const struct gsm_mncc_number *calling)
604{
605 return gsm48_encode_callerid(msg, GSM48_IE_CALLING_BCD, 14, calling);
606}
607
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200608/*! Decode TS 04.08 Connected Number IE (10.5.4.13) */
Harald Welte1e908662010-03-07 23:39:54 +0100609int gsm48_decode_connected(struct gsm_mncc_number *connected,
610 const uint8_t *lv)
611{
612 return gsm48_decode_callerid(connected, lv);
613}
614
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200615/*! Encode TS 04.08 Connected Number IE (10.5.4.13) */
Harald Welte1e908662010-03-07 23:39:54 +0100616int gsm48_encode_connected(struct msgb *msg,
617 const struct gsm_mncc_number *connected)
618{
619 return gsm48_encode_callerid(msg, GSM48_IE_CONN_BCD, 14, connected);
620}
621
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200622/*! Decode TS 04.08 Redirecting Number IE (10.5.4.21b) */
Harald Welte1e908662010-03-07 23:39:54 +0100623int gsm48_decode_redirecting(struct gsm_mncc_number *redirecting,
624 const uint8_t *lv)
625{
626 return gsm48_decode_callerid(redirecting, lv);
627}
628
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200629/*! Encode TS 04.08 Redirecting Number IE (10.5.4.21b) */
Harald Welte1e908662010-03-07 23:39:54 +0100630int gsm48_encode_redirecting(struct msgb *msg,
631 const struct gsm_mncc_number *redirecting)
632{
633 return gsm48_encode_callerid(msg, GSM48_IE_REDIR_BCD, 19, redirecting);
634}
635
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200636/*! Decode TS 04.08 Facility IE (10.5.4.15) */
Harald Welte1e908662010-03-07 23:39:54 +0100637int gsm48_decode_facility(struct gsm_mncc_facility *facility,
638 const uint8_t *lv)
639{
640 uint8_t in_len = lv[0];
641
642 if (in_len < 1)
643 return -EINVAL;
644
645 if (in_len > sizeof(facility->info))
646 return -EINVAL;
647
648 memcpy(facility->info, lv+1, in_len);
649 facility->len = in_len;
650
651 return 0;
652}
653
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200654/*! Encode TS 04.08 Facility IE (10.5.4.15) */
Harald Welte1e908662010-03-07 23:39:54 +0100655int gsm48_encode_facility(struct msgb *msg, int lv_only,
656 const struct gsm_mncc_facility *facility)
657{
658 uint8_t lv[GSM_MAX_FACILITY + 1];
659
660 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
661 return -EINVAL;
662
663 memcpy(lv+1, facility->info, facility->len);
664 lv[0] = facility->len;
665 if (lv_only)
666 msgb_lv_put(msg, lv[0], lv+1);
667 else
668 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
669
670 return 0;
671}
672
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200673/*! Decode TS 04.08 Notify IE (10.5.4.20) */
Harald Welte1e908662010-03-07 23:39:54 +0100674int gsm48_decode_notify(int *notify, const uint8_t *v)
675{
676 *notify = v[0] & 0x7f;
677
678 return 0;
679}
680
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200681/*! Encode TS 04.08 Notify IE (10.5.4.20) */
Harald Welte1e908662010-03-07 23:39:54 +0100682int gsm48_encode_notify(struct msgb *msg, int notify)
683{
684 msgb_v_put(msg, notify | 0x80);
685
686 return 0;
687}
688
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200689/*! Decode TS 04.08 Signal IE (10.5.4.23) */
Harald Welte1e908662010-03-07 23:39:54 +0100690int gsm48_decode_signal(int *signal, const uint8_t *v)
691{
692 *signal = v[0];
693
694 return 0;
695}
696
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200697/*! Encode TS 04.08 Signal IE (10.5.4.23) */
Harald Welte1e908662010-03-07 23:39:54 +0100698int gsm48_encode_signal(struct msgb *msg, int signal)
699{
700 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
701
702 return 0;
703}
704
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200705/*! Decode TS 04.08 Keypad IE (10.5.4.17) */
Harald Welte1e908662010-03-07 23:39:54 +0100706int gsm48_decode_keypad(int *keypad, const uint8_t *lv)
707{
708 uint8_t in_len = lv[0];
709
710 if (in_len < 1)
711 return -EINVAL;
712
713 *keypad = lv[1] & 0x7f;
714
715 return 0;
716}
717
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200718/*! Encode TS 04.08 Keypad IE (10.5.4.17) */
Harald Welte1e908662010-03-07 23:39:54 +0100719int gsm48_encode_keypad(struct msgb *msg, int keypad)
720{
721 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
722
723 return 0;
724}
725
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200726/*! Decode TS 04.08 Progress IE (10.5.4.21) */
Harald Welte1e908662010-03-07 23:39:54 +0100727int gsm48_decode_progress(struct gsm_mncc_progress *progress,
728 const uint8_t *lv)
729{
730 uint8_t in_len = lv[0];
731
732 if (in_len < 2)
733 return -EINVAL;
734
735 progress->coding = (lv[1] & 0x60) >> 5;
736 progress->location = lv[1] & 0x0f;
737 progress->descr = lv[2] & 0x7f;
738
739 return 0;
740}
741
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200742/*! Encode TS 04.08 Progress IE (10.5.4.21) */
Harald Welte1e908662010-03-07 23:39:54 +0100743int gsm48_encode_progress(struct msgb *msg, int lv_only,
744 const struct gsm_mncc_progress *p)
745{
746 uint8_t lv[3];
747
748 lv[0] = 2;
749 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
750 lv[2] = 0x80 | (p->descr & 0x7f);
751 if (lv_only)
752 msgb_lv_put(msg, lv[0], lv+1);
753 else
754 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
755
756 return 0;
757}
758
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200759/*! Decode TS 04.08 User-User IE (10.5.4.25) */
Harald Welte1e908662010-03-07 23:39:54 +0100760int gsm48_decode_useruser(struct gsm_mncc_useruser *uu,
761 const uint8_t *lv)
762{
763 uint8_t in_len = lv[0];
764 char *info = uu->info;
765 int info_len = sizeof(uu->info);
766 int i;
767
768 if (in_len < 1)
769 return -EINVAL;
770
771 uu->proto = lv[1];
772
773 for (i = 2; i <= in_len; i++) {
774 info_len--;
775 if (info_len <= 1)
776 break;
777 *info++ = lv[i];
778 }
779 if (info_len >= 1)
780 *info++ = '\0';
781
782 return 0;
783}
784
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200785/*! Encode TS 04.08 User-User IE (10.5.4.25) */
Harald Welte1e908662010-03-07 23:39:54 +0100786int gsm48_encode_useruser(struct msgb *msg, int lv_only,
787 const struct gsm_mncc_useruser *uu)
788{
789 uint8_t lv[GSM_MAX_USERUSER + 2];
790
791 if (strlen(uu->info) > GSM_MAX_USERUSER)
792 return -EINVAL;
793
794 lv[0] = 1 + strlen(uu->info);
795 lv[1] = uu->proto;
796 memcpy(lv + 2, uu->info, strlen(uu->info));
797 if (lv_only)
798 msgb_lv_put(msg, lv[0], lv+1);
799 else
800 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
801
802 return 0;
803}
804
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200805/*! Decode TS 04.08 SS Version IE (10.5.4.24) */
Harald Welte1e908662010-03-07 23:39:54 +0100806int gsm48_decode_ssversion(struct gsm_mncc_ssversion *ssv,
807 const uint8_t *lv)
808{
809 uint8_t in_len = lv[0];
810
811 if (in_len < 1 || in_len < sizeof(ssv->info))
812 return -EINVAL;
813
814 memcpy(ssv->info, lv + 1, in_len);
815 ssv->len = in_len;
816
817 return 0;
818}
819
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200820/*! Encode TS 04.08 SS Version IE (10.5.4.24) */
Harald Welte1e908662010-03-07 23:39:54 +0100821int gsm48_encode_ssversion(struct msgb *msg,
822 const struct gsm_mncc_ssversion *ssv)
823{
824 uint8_t lv[GSM_MAX_SSVERSION + 1];
825
826 if (ssv->len > GSM_MAX_SSVERSION)
827 return -EINVAL;
828
829 lv[0] = ssv->len;
830 memcpy(lv + 1, ssv->info, ssv->len);
831 msgb_tlv_put(msg, GSM48_IE_SS_VERS, lv[0], lv+1);
832
833 return 0;
834}
835
836/* decode 'more data' does not require a function, because it has no value */
837
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200838/*! Encode TS 04.08 More Data IE (10.5.4.19) */
Harald Welte1e908662010-03-07 23:39:54 +0100839int gsm48_encode_more(struct msgb *msg)
840{
841 uint8_t *ie;
842
843 ie = msgb_put(msg, 1);
844 ie[0] = GSM48_IE_MORE_DATA;
845
846 return 0;
847}
848
Sylvain Munaut71fd42f2011-09-01 22:05:29 +0200849static int32_t smod(int32_t n, int32_t m)
850{
851 int32_t res;
852
853 res = n % m;
854
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200855 if (res <= 0)
Sylvain Munaut71fd42f2011-09-01 22:05:29 +0200856 res += m;
857
Sylvain Munaut71fd42f2011-09-01 22:05:29 +0200858 return res;
859}
860
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200861/*! Decode TS 04.08 Cell Channel Description IE (10.5.2.1b) and other frequency lists
Pau Espin Pedrold8d0c3e2018-11-15 18:26:01 +0100862 * \param[out] f Caller-provided output memory, an array of 1024 elements
Harald Welte96e2a002017-06-12 21:44:18 +0200863 * \param[in] cd Cell Channel Description IE
864 * \param[in] len Length of \a cd in bytes
865 * \returns 0 on success; negative on error */
Harald Welte1523d702010-08-04 11:46:44 +0200866int gsm48_decode_freq_list(struct gsm_sysinfo_freq *f, uint8_t *cd,
867 uint8_t len, uint8_t mask, uint8_t frqt)
868{
869 int i;
870
871 /* NOTES:
872 *
873 * The Range format uses "SMOD" computation.
874 * e.g. "n SMOD m" equals "((n - 1) % m) + 1"
875 * A cascade of multiple SMOD computations is simpified:
876 * "(n SMOD m) SMOD o" equals "(((n - 1) % m) % o) + 1"
877 *
878 * The Range format uses 16 octets of data in SYSTEM INFORMATION.
879 * When used in dedicated messages, the length can be less.
880 * In this case the ranges are decoded for all frequencies that
881 * fit in the block of given length.
882 */
883
884 /* tabula rasa */
885 for (i = 0; i < 1024; i++)
886 f[i].mask &= ~frqt;
887
888 /* 00..XXX. */
889 if ((cd[0] & 0xc0 & mask) == 0x00) {
890 /* Bit map 0 format */
891 if (len < 16)
892 return -EINVAL;
893 for (i = 1; i <= 124; i++)
894 if ((cd[15 - ((i-1) >> 3)] & (1 << ((i-1) & 7))))
895 f[i].mask |= frqt;
896
897 return 0;
898 }
899
900 /* 10..0XX. */
901 if ((cd[0] & 0xc8 & mask) == 0x80) {
902 /* Range 1024 format */
903 uint16_t w[17]; /* 1..16 */
904 struct gsm48_range_1024 *r = (struct gsm48_range_1024 *)cd;
905
906 if (len < 2)
907 return -EINVAL;
908 memset(w, 0, sizeof(w));
909 if (r->f0)
910 f[0].mask |= frqt;
911 w[1] = (r->w1_hi << 8) | r->w1_lo;
912 if (len >= 4)
913 w[2] = (r->w2_hi << 1) | r->w2_lo;
914 if (len >= 5)
915 w[3] = (r->w3_hi << 2) | r->w3_lo;
916 if (len >= 6)
917 w[4] = (r->w4_hi << 2) | r->w4_lo;
918 if (len >= 7)
919 w[5] = (r->w5_hi << 2) | r->w5_lo;
920 if (len >= 8)
921 w[6] = (r->w6_hi << 2) | r->w6_lo;
922 if (len >= 9)
923 w[7] = (r->w7_hi << 2) | r->w7_lo;
924 if (len >= 10)
925 w[8] = (r->w8_hi << 1) | r->w8_lo;
926 if (len >= 10)
927 w[9] = r->w9;
928 if (len >= 11)
929 w[10] = r->w10;
930 if (len >= 12)
931 w[11] = (r->w11_hi << 6) | r->w11_lo;
932 if (len >= 13)
933 w[12] = (r->w12_hi << 5) | r->w12_lo;
934 if (len >= 14)
935 w[13] = (r->w13_hi << 4) | r->w13_lo;
936 if (len >= 15)
937 w[14] = (r->w14_hi << 3) | r->w14_lo;
938 if (len >= 16)
939 w[15] = (r->w15_hi << 2) | r->w15_lo;
940 if (len >= 16)
941 w[16] = r->w16;
942 if (w[1])
943 f[w[1]].mask |= frqt;
944 if (w[2])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200945 f[smod(w[1] - 512 + w[2], 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200946 if (w[3])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200947 f[smod(w[1] + w[3], 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200948 if (w[4])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200949 f[smod(w[1] - 512 + smod(w[2] - 256 + w[4], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200950 if (w[5])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200951 f[smod(w[1] + smod(w[3] - 256 + w[5], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200952 if (w[6])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200953 f[smod(w[1] - 512 + smod(w[2] + w[6], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200954 if (w[7])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200955 f[smod(w[1] + smod(w[3] + w[7], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200956 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200957 f[smod(w[1] - 512 + smod(w[2] - 256 + smod(w[4] - 128 + w[8] , 255), 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200958 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200959 f[smod(w[1] + smod(w[3] - 256 + smod(w[5] - 128 + w[9] , 255), 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200960 if (w[10])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200961 f[smod(w[1] - 512 + smod(w[2] + smod(w[6] - 128 + w[10], 255), 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200962 if (w[11])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200963 f[smod(w[1] + smod(w[3] + smod(w[7] - 128 + w[11], 255), 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200964 if (w[12])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200965 f[smod(w[1] - 512 + smod(w[2] - 256 + smod(w[4] + w[12], 255), 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200966 if (w[13])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200967 f[smod(w[1] + smod(w[3] - 256 + smod(w[5] + w[13], 255), 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200968 if (w[14])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200969 f[smod(w[1] - 512 + smod(w[2] + smod(w[6] + w[14], 255), 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200970 if (w[15])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200971 f[smod(w[1] + smod(w[3] + smod(w[7] + w[15], 255), 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200972 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200973 f[smod(w[1] - 512 + smod(w[2] - 256 + smod(w[4] - 128 + smod(w[8] - 64 + w[16], 127), 255), 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200974
975 return 0;
976 }
977 /* 10..100. */
978 if ((cd[0] & 0xce & mask) == 0x88) {
979 /* Range 512 format */
980 uint16_t w[18]; /* 1..17 */
981 struct gsm48_range_512 *r = (struct gsm48_range_512 *)cd;
982
983 if (len < 4)
984 return -EINVAL;
985 memset(w, 0, sizeof(w));
986 w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
987 w[1] = (r->w1_hi << 2) | r->w1_lo;
988 if (len >= 5)
989 w[2] = (r->w2_hi << 2) | r->w2_lo;
990 if (len >= 6)
991 w[3] = (r->w3_hi << 2) | r->w3_lo;
992 if (len >= 7)
993 w[4] = (r->w4_hi << 1) | r->w4_lo;
994 if (len >= 7)
995 w[5] = r->w5;
996 if (len >= 8)
997 w[6] = r->w6;
998 if (len >= 9)
999 w[7] = (r->w7_hi << 6) | r->w7_lo;
1000 if (len >= 10)
1001 w[8] = (r->w8_hi << 4) | r->w8_lo;
1002 if (len >= 11)
1003 w[9] = (r->w9_hi << 2) | r->w9_lo;
1004 if (len >= 11)
1005 w[10] = r->w10;
1006 if (len >= 12)
1007 w[11] = r->w11;
1008 if (len >= 13)
1009 w[12] = (r->w12_hi << 4) | r->w12_lo;
1010 if (len >= 14)
1011 w[13] = (r->w13_hi << 2) | r->w13_lo;
1012 if (len >= 14)
1013 w[14] = r->w14;
1014 if (len >= 15)
1015 w[15] = r->w15;
1016 if (len >= 16)
1017 w[16] = (r->w16_hi << 3) | r->w16_lo;
1018 if (len >= 16)
1019 w[17] = r->w17;
1020 f[w[0]].mask |= frqt;
1021 if (w[1])
1022 f[(w[0] + w[1]) % 1024].mask |= frqt;
1023 if (w[2])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001024 f[(w[0] + smod(w[1] - 256 + w[2], 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001025 if (w[3])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001026 f[(w[0] + smod(w[1] + w[3], 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001027 if (w[4])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001028 f[(w[0] + smod(w[1] - 256 + smod(w[2] - 128 + w[4], 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001029 if (w[5])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001030 f[(w[0] + smod(w[1] + smod(w[3] - 128 + w[5], 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001031 if (w[6])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001032 f[(w[0] + smod(w[1] - 256 + smod(w[2] + w[6], 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001033 if (w[7])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001034 f[(w[0] + smod(w[1] + smod(w[3] + w[7], 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001035 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001036 f[(w[0] + smod(w[1] - 256 + smod(w[2] - 128 + smod(w[4] - 64 + w[8] , 127), 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001037 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001038 f[(w[0] + smod(w[1] + smod(w[3] - 128 + smod(w[5] - 64 + w[9] , 127), 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001039 if (w[10])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001040 f[(w[0] + smod(w[1] - 256 + smod(w[2] + smod(w[6] - 64 + w[10], 127), 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001041 if (w[11])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001042 f[(w[0] + smod(w[1] + smod(w[3] + smod(w[7] - 64 + w[11], 127), 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001043 if (w[12])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001044 f[(w[0] + smod(w[1] - 256 + smod(w[2] - 128 + smod(w[4] + w[12], 127), 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001045 if (w[13])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001046 f[(w[0] + smod(w[1] + smod(w[3] - 128 + smod(w[5] + w[13], 127), 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001047 if (w[14])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001048 f[(w[0] + smod(w[1] - 256 + smod(w[2] + smod(w[6] + w[14], 127), 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001049 if (w[15])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001050 f[(w[0] + smod(w[1] + smod(w[3] + smod(w[7] + w[15], 127), 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001051 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001052 f[(w[0] + smod(w[1] - 256 + smod(w[2] - 128 + smod(w[4] - 64 + smod(w[8] - 32 + w[16], 63), 127), 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001053 if (w[17])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001054 f[(w[0] + smod(w[1] + smod(w[3] - 128 + smod(w[5] - 64 + smod(w[9] - 32 + w[17], 63), 127), 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001055
1056 return 0;
1057 }
1058 /* 10..101. */
1059 if ((cd[0] & 0xce & mask) == 0x8a) {
1060 /* Range 256 format */
1061 uint16_t w[22]; /* 1..21 */
1062 struct gsm48_range_256 *r = (struct gsm48_range_256 *)cd;
1063
1064 if (len < 4)
1065 return -EINVAL;
1066 memset(w, 0, sizeof(w));
1067 w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
1068 w[1] = (r->w1_hi << 1) | r->w1_lo;
1069 if (len >= 4)
1070 w[2] = r->w2;
1071 if (len >= 5)
1072 w[3] = r->w3;
1073 if (len >= 6)
1074 w[4] = (r->w4_hi << 5) | r->w4_lo;
1075 if (len >= 7)
1076 w[5] = (r->w5_hi << 3) | r->w5_lo;
1077 if (len >= 8)
1078 w[6] = (r->w6_hi << 1) | r->w6_lo;
1079 if (len >= 8)
1080 w[7] = r->w7;
1081 if (len >= 9)
1082 w[8] = (r->w8_hi << 4) | r->w8_lo;
1083 if (len >= 10)
1084 w[9] = (r->w9_hi << 1) | r->w9_lo;
1085 if (len >= 10)
1086 w[10] = r->w10;
1087 if (len >= 11)
1088 w[11] = (r->w11_hi << 3) | r->w11_lo;
1089 if (len >= 11)
1090 w[12] = r->w12;
1091 if (len >= 12)
1092 w[13] = r->w13;
1093 if (len >= 13)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001094 w[14] = (r->w14_hi << 2) | r->w14_lo;
Harald Welte1523d702010-08-04 11:46:44 +02001095 if (len >= 13)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001096 w[15] = r->w15;
Harald Welte1523d702010-08-04 11:46:44 +02001097 if (len >= 14)
1098 w[16] = (r->w16_hi << 3) | r->w16_lo;
1099 if (len >= 14)
1100 w[17] = r->w17;
1101 if (len >= 15)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001102 w[18] = (r->w18_hi << 3) | r->w18_lo;
Harald Welte1523d702010-08-04 11:46:44 +02001103 if (len >= 15)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001104 w[19] = r->w19;
Harald Welte1523d702010-08-04 11:46:44 +02001105 if (len >= 16)
1106 w[20] = (r->w20_hi << 3) | r->w20_lo;
1107 if (len >= 16)
1108 w[21] = r->w21;
1109 f[w[0]].mask |= frqt;
1110 if (w[1])
1111 f[(w[0] + w[1]) % 1024].mask |= frqt;
1112 if (w[2])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001113 f[(w[0] + smod(w[1] - 128 + w[2], 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001114 if (w[3])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001115 f[(w[0] + smod(w[1] + w[3], 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001116 if (w[4])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001117 f[(w[0] + smod(w[1] - 128 + smod(w[2] - 64 + w[4], 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001118 if (w[5])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001119 f[(w[0] + smod(w[1] + smod(w[3] - 64 + w[5], 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001120 if (w[6])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001121 f[(w[0] + smod(w[1] - 128 + smod(w[2] + w[6], 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001122 if (w[7])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001123 f[(w[0] + smod(w[1] + smod(w[3] + w[7], 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001124 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001125 f[(w[0] + smod(w[1] - 128 + smod(w[2] - 64 + smod(w[4] - 32 + w[8] , 63), 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001126 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001127 f[(w[0] + smod(w[1] + smod(w[3] - 64 + smod(w[5] - 32 + w[9] , 63), 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001128 if (w[10])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001129 f[(w[0] + smod(w[1] - 128 + smod(w[2] + smod(w[6] - 32 + w[10], 63), 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001130 if (w[11])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001131 f[(w[0] + smod(w[1] + smod(w[3] + smod(w[7] - 32 + w[11], 63), 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001132 if (w[12])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001133 f[(w[0] + smod(w[1] - 128 + smod(w[2] - 64 + smod(w[4] + w[12], 63), 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001134 if (w[13])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001135 f[(w[0] + smod(w[1] + smod(w[3] - 64 + smod(w[5] + w[13], 63), 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001136 if (w[14])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001137 f[(w[0] + smod(w[1] - 128 + smod(w[2] + smod(w[6] + w[14], 63), 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001138 if (w[15])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001139 f[(w[0] + smod(w[1] + smod(w[3] + smod(w[7] + w[15], 63), 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001140 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001141 f[(w[0] + smod(w[1] - 128 + smod(w[2] - 64 + smod(w[4] - 32 + smod(w[8] - 16 + w[16], 31), 63), 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001142 if (w[17])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001143 f[(w[0] + smod(w[1] + smod(w[3] - 64 + smod(w[5] - 32 + smod(w[9] - 16 + w[17], 31), 63), 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001144 if (w[18])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001145 f[(w[0] + smod(w[1] - 128 + smod(w[2] + smod(w[6] - 32 + smod(w[10] - 16 + w[18], 31), 63), 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001146 if (w[19])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001147 f[(w[0] + smod(w[1] + smod(w[3] + smod(w[7] - 32 + smod(w[11] - 16 + w[19], 31), 63), 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001148 if (w[20])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001149 f[(w[0] + smod(w[1] - 128 + smod(w[2] - 64 + smod(w[4] + smod(w[12] - 16 + w[20], 31), 63), 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001150 if (w[21])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001151 f[(w[0] + smod(w[1] + smod(w[3] - 64 + smod(w[5] + smod(w[13] - 16 + w[21], 31), 63), 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001152
1153 return 0;
1154 }
1155 /* 10..110. */
1156 if ((cd[0] & 0xce & mask) == 0x8c) {
1157 /* Range 128 format */
1158 uint16_t w[29]; /* 1..28 */
1159 struct gsm48_range_128 *r = (struct gsm48_range_128 *)cd;
1160
1161 if (len < 3)
1162 return -EINVAL;
1163 memset(w, 0, sizeof(w));
1164 w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
1165 w[1] = r->w1;
1166 if (len >= 4)
1167 w[2] = r->w2;
1168 if (len >= 5)
1169 w[3] = (r->w3_hi << 4) | r->w3_lo;
1170 if (len >= 6)
1171 w[4] = (r->w4_hi << 1) | r->w4_lo;
1172 if (len >= 6)
1173 w[5] = r->w5;
1174 if (len >= 7)
1175 w[6] = (r->w6_hi << 3) | r->w6_lo;
1176 if (len >= 7)
1177 w[7] = r->w7;
1178 if (len >= 8)
1179 w[8] = r->w8;
1180 if (len >= 8)
1181 w[9] = r->w9;
1182 if (len >= 9)
1183 w[10] = r->w10;
1184 if (len >= 9)
1185 w[11] = r->w11;
1186 if (len >= 10)
1187 w[12] = r->w12;
1188 if (len >= 10)
1189 w[13] = r->w13;
1190 if (len >= 11)
1191 w[14] = r->w14;
1192 if (len >= 11)
1193 w[15] = r->w15;
1194 if (len >= 12)
1195 w[16] = r->w16;
1196 if (len >= 12)
1197 w[17] = r->w17;
1198 if (len >= 13)
1199 w[18] = (r->w18_hi << 1) | r->w18_lo;
1200 if (len >= 13)
1201 w[19] = r->w19;
1202 if (len >= 13)
1203 w[20] = r->w20;
1204 if (len >= 14)
1205 w[21] = (r->w21_hi << 2) | r->w21_lo;
1206 if (len >= 14)
1207 w[22] = r->w22;
1208 if (len >= 14)
1209 w[23] = r->w23;
1210 if (len >= 15)
1211 w[24] = r->w24;
1212 if (len >= 15)
1213 w[25] = r->w25;
1214 if (len >= 16)
1215 w[26] = (r->w26_hi << 1) | r->w26_lo;
1216 if (len >= 16)
1217 w[27] = r->w27;
1218 if (len >= 16)
1219 w[28] = r->w28;
1220 f[w[0]].mask |= frqt;
1221 if (w[1])
1222 f[(w[0] + w[1]) % 1024].mask |= frqt;
1223 if (w[2])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001224 f[(w[0] + smod(w[1] - 64 + w[2], 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001225 if (w[3])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001226 f[(w[0] + smod(w[1] + w[3], 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001227 if (w[4])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001228 f[(w[0] + smod(w[1] - 64 + smod(w[2] - 32 + w[4], 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001229 if (w[5])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001230 f[(w[0] + smod(w[1] + smod(w[3] - 32 + w[5], 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001231 if (w[6])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001232 f[(w[0] + smod(w[1] - 64 + smod(w[2] + w[6], 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001233 if (w[7])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001234 f[(w[0] + smod(w[1] + smod(w[3] + w[7], 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001235 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001236 f[(w[0] + smod(w[1] - 64 + smod(w[2] - 32 + smod(w[4] - 16 + w[8] , 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001237 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001238 f[(w[0] + smod(w[1] + smod(w[3] - 32 + smod(w[5] - 16 + w[9] , 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001239 if (w[10])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001240 f[(w[0] + smod(w[1] - 64 + smod(w[2] + smod(w[6] - 16 + w[10], 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001241 if (w[11])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001242 f[(w[0] + smod(w[1] + smod(w[3] + smod(w[7] - 16 + w[11], 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001243 if (w[12])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001244 f[(w[0] + smod(w[1] - 64 + smod(w[2] - 32 + smod(w[4] + w[12], 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001245 if (w[13])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001246 f[(w[0] + smod(w[1] + smod(w[3] - 32 + smod(w[5] + w[13], 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001247 if (w[14])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001248 f[(w[0] + smod(w[1] - 64 + smod(w[2] + smod(w[6] + w[14], 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001249 if (w[15])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001250 f[(w[0] + smod(w[1] + smod(w[3] + smod(w[7] + w[15], 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001251 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001252 f[(w[0] + smod(w[1] - 64 + smod(w[2] - 32 + smod(w[4] - 16 + smod(w[8] - 8 + w[16], 15), 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001253 if (w[17])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001254 f[(w[0] + smod(w[1] + smod(w[3] - 32 + smod(w[5] - 16 + smod(w[9] - 8 + w[17], 15), 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001255 if (w[18])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001256 f[(w[0] + smod(w[1] - 64 + smod(w[2] + smod(w[6] - 16 + smod(w[10] - 8 + w[18], 15), 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001257 if (w[19])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001258 f[(w[0] + smod(w[1] + smod(w[3] + smod(w[7] - 16 + smod(w[11] - 8 + w[19], 15), 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001259 if (w[20])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001260 f[(w[0] + smod(w[1] - 64 + smod(w[2] - 32 + smod(w[4] + smod(w[12] - 8 + w[20], 15), 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001261 if (w[21])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001262 f[(w[0] + smod(w[1] + smod(w[3] - 32 + smod(w[5] + smod(w[13] - 8 + w[21], 15), 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001263 if (w[22])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001264 f[(w[0] + smod(w[1] - 64 + smod(w[2] + smod(w[6] + smod(w[14] - 8 + w[22], 15), 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001265 if (w[23])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001266 f[(w[0] + smod(w[1] + smod(w[3] + smod(w[7] + smod(w[15] - 8 + w[23], 15), 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001267 if (w[24])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001268 f[(w[0] + smod(w[1] - 64 + smod(w[2] - 32 + smod(w[4] - 16 + smod(w[8] + w[24], 15), 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001269 if (w[25])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001270 f[(w[0] + smod(w[1] + smod(w[3] - 32 + smod(w[5] - 16 + smod(w[9] + w[25], 15), 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001271 if (w[26])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001272 f[(w[0] + smod(w[1] - 64 + smod(w[2] + smod(w[6] - 16 + smod(w[10] + w[26], 15), 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001273 if (w[27])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001274 f[(w[0] + smod(w[1] + smod(w[3] + smod(w[7] - 16 + smod(w[11] + w[27], 15), 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001275 if (w[28])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001276 f[(w[0] + smod(w[1] - 64 + smod(w[2] - 32 + smod(w[4] + smod(w[12] + w[28], 15), 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001277
1278 return 0;
1279 }
1280 /* 10..111. */
1281 if ((cd[0] & 0xce & mask) == 0x8e) {
1282 /* Variable bitmap format (can be any length >= 3) */
1283 uint16_t orig = 0;
1284 struct gsm48_var_bit *r = (struct gsm48_var_bit *)cd;
1285
1286 if (len < 3)
1287 return -EINVAL;
1288 orig = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
1289 f[orig].mask |= frqt;
1290 for (i = 1; 2 + (i >> 3) < len; i++)
1291 if ((cd[2 + (i >> 3)] & (0x80 >> (i & 7))))
1292 f[(orig + i) % 1024].mask |= frqt;
1293
1294 return 0;
1295 }
1296
1297 return 0;
1298}
Philipp Maiere36be562020-11-12 11:33:54 +01001299
1300/*! Decode 3GPP TS 24.008 Mobile Station Classmark 3 (10.5.1.7).
1301 * \param[out] classmark3_out user provided memory to store decoded classmark3.
1302 * \param[in] classmark3 pointer to memory that contains the raw classmark bits.
1303 * \param[in] classmark3_len length in bytes of the memory where classmark3 points to.
1304 * \returns 0 on success; negative on error. */
1305int gsm48_decode_classmark3(struct gsm48_classmark3 *classmark3_out,
1306 const uint8_t *classmark3, size_t classmark3_len)
1307{
1308 struct bitvec bv;
1309 uint8_t data[255];
1310 struct gsm48_classmark3 *cm3 = classmark3_out;
1311
1312 /* if cm3 gets extended by spec, it will be truncated, but 255 bytes
1313 * should be more than enough. */
1314 if (classmark3_len > sizeof(data))
1315 classmark3_len = sizeof(data);
1316
1317 memset(&bv, 0, sizeof(bv));
1318 memset(data, 0, sizeof(data));
1319 memset(classmark3_out, 0, sizeof(*classmark3_out));
1320
1321 memcpy(data, classmark3, classmark3_len);
1322 bv.data = (uint8_t*) data;
1323 bv.data_len = sizeof(data);
1324
1325 /* Parse bit vector, see also: 3GPP TS 24.008, section 10.5.1.7 */
1326 bitvec_get_uint(&bv, 1);
1327 cm3->mult_band_supp = bitvec_get_uint(&bv, 3);
1328 switch (cm3->mult_band_supp) {
1329 case 0x00:
1330 cm3->a5_bits = bitvec_get_uint(&bv, 4);
1331 break;
1332 case 0x05:
1333 case 0x06:
1334 cm3->a5_bits = bitvec_get_uint(&bv, 4);
1335 cm3->assoc_radio_cap_2 = bitvec_get_uint(&bv, 4);
1336 cm3->assoc_radio_cap_1 = bitvec_get_uint(&bv, 4);
1337 break;
1338 case 0x01:
1339 case 0x02:
1340 case 0x04:
1341 cm3->a5_bits = bitvec_get_uint(&bv, 4);
1342 bitvec_get_uint(&bv, 4);
1343 cm3->assoc_radio_cap_1 = bitvec_get_uint(&bv, 4);
1344 break;
1345 default:
1346 return -1;
1347 }
1348
1349 cm3->r_support.present = bitvec_get_uint(&bv, 1);
1350 if (cm3->r_support.present)
1351 cm3->r_support.r_gsm_assoc_radio_cap = bitvec_get_uint(&bv, 3);
1352
1353 cm3->hscsd_mult_slot_cap.present = bitvec_get_uint(&bv, 1);
1354 if (cm3->hscsd_mult_slot_cap.present)
1355 cm3->hscsd_mult_slot_cap.mslot_class = bitvec_get_uint(&bv, 5);
1356
1357 cm3->ucs2_treatment = bitvec_get_uint(&bv, 1);
1358 cm3->extended_meas_cap = bitvec_get_uint(&bv, 1);
1359
1360 cm3->ms_meas_cap.present = bitvec_get_uint(&bv, 1);
1361 if (cm3->ms_meas_cap.present) {
1362 cm3->ms_meas_cap.sms_value = bitvec_get_uint(&bv, 4);
1363 cm3->ms_meas_cap.sm_value = bitvec_get_uint(&bv, 4);
1364 }
1365
1366 cm3->ms_pos_method_cap.present = bitvec_get_uint(&bv, 1);
1367 if (cm3->ms_pos_method_cap.present)
1368 cm3->ms_pos_method_cap.method = bitvec_get_uint(&bv, 5);
1369
1370 cm3->ecsd_multislot_cap.present = bitvec_get_uint(&bv, 1);
1371 if (cm3->ecsd_multislot_cap.present)
1372 cm3->ecsd_multislot_cap.mslot_class = bitvec_get_uint(&bv, 5);
1373
1374 cm3->psk8_struct.present = bitvec_get_uint(&bv, 1);
1375 if (cm3->psk8_struct.present) {
1376 cm3->psk8_struct.mod_cap = bitvec_get_uint(&bv, 1);
1377
1378 cm3->psk8_struct.rf_pwr_cap_1.present = bitvec_get_uint(&bv, 1);
1379 if (cm3->psk8_struct.rf_pwr_cap_1.present) {
1380 cm3->psk8_struct.rf_pwr_cap_1.value =
1381 bitvec_get_uint(&bv, 2);
1382 }
1383
1384 cm3->psk8_struct.rf_pwr_cap_2.present = bitvec_get_uint(&bv, 1);
1385 if (cm3->psk8_struct.rf_pwr_cap_2.present) {
1386 cm3->psk8_struct.rf_pwr_cap_2.value =
1387 bitvec_get_uint(&bv, 2);
1388 }
1389 }
1390
1391 cm3->gsm_400_bands_supp.present = bitvec_get_uint(&bv, 1);
1392 if (cm3->gsm_400_bands_supp.present) {
1393 cm3->gsm_400_bands_supp.value = bitvec_get_uint(&bv, 2);
1394 if (cm3->gsm_400_bands_supp.value == 0x00)
1395 return -1;
1396 cm3->gsm_400_bands_supp.assoc_radio_cap =
1397 bitvec_get_uint(&bv, 4);
1398 }
1399
1400 cm3->gsm_850_assoc_radio_cap.present = bitvec_get_uint(&bv, 1);
1401 if (cm3->gsm_850_assoc_radio_cap.present)
1402 cm3->gsm_850_assoc_radio_cap.value = bitvec_get_uint(&bv, 4);
1403
1404 cm3->gsm_1900_assoc_radio_cap.present = bitvec_get_uint(&bv, 1);
1405 if (cm3->gsm_1900_assoc_radio_cap.present)
1406 cm3->gsm_1900_assoc_radio_cap.value = bitvec_get_uint(&bv, 4);
1407
1408 cm3->umts_fdd_rat_cap = bitvec_get_uint(&bv, 1);
1409 cm3->umts_tdd_rat_cap = bitvec_get_uint(&bv, 1);
1410 cm3->cdma200_rat_cap = bitvec_get_uint(&bv, 1);
1411
1412 cm3->dtm_gprs_multislot_cap.present = bitvec_get_uint(&bv, 1);
1413 if (cm3->dtm_gprs_multislot_cap.present) {
1414 cm3->dtm_gprs_multislot_cap.mslot_class = bitvec_get_uint(&bv, 2);
1415 cm3->dtm_gprs_multislot_cap.single_slot_dtm =
1416 bitvec_get_uint(&bv, 1);
1417 cm3->dtm_gprs_multislot_cap.dtm_egprs_multislot_cap.present =
1418 bitvec_get_uint(&bv, 1);
1419 if (cm3->dtm_gprs_multislot_cap.dtm_egprs_multislot_cap.present)
1420 cm3->dtm_gprs_multislot_cap.dtm_egprs_multislot_cap.
1421 mslot_class = bitvec_get_uint(&bv, 2);
1422 }
1423
1424 /* Release 4 starts here. */
1425 cm3->single_band_supp.present = bitvec_get_uint(&bv, 1);
1426 if (cm3->single_band_supp.present)
1427 cm3->single_band_supp.value = bitvec_get_uint(&bv, 4);
1428
1429 cm3->gsm_750_assoc_radio_cap.present = bitvec_get_uint(&bv, 1);
1430 if (cm3->gsm_750_assoc_radio_cap.present)
1431 cm3->gsm_750_assoc_radio_cap.value = bitvec_get_uint(&bv, 4);
1432
1433 cm3->umts_1_28_mcps_tdd_rat_cap = bitvec_get_uint(&bv, 1);
1434 cm3->geran_feature_package = bitvec_get_uint(&bv, 1);
1435
1436 cm3->extended_dtm_gprs_multislot_cap.present = bitvec_get_uint(&bv, 1);
1437 if (cm3->extended_dtm_gprs_multislot_cap.present) {
1438 cm3->extended_dtm_gprs_multislot_cap.mslot_class =
1439 bitvec_get_uint(&bv, 2);
1440 cm3->extended_dtm_gprs_multislot_cap.
1441 extended_dtm_egprs_multislot_cap.present =
1442 bitvec_get_uint(&bv, 1);
1443 if (cm3->extended_dtm_gprs_multislot_cap.
1444 extended_dtm_egprs_multislot_cap.present)
1445 cm3->extended_dtm_gprs_multislot_cap.
1446 extended_dtm_egprs_multislot_cap.mslot_class =
1447 bitvec_get_uint(&bv, 2);
1448 }
1449
1450 /* Release 5 starts here */
1451 cm3->high_multislot_cap.present = bitvec_get_uint(&bv, 1);
1452 if (cm3->high_multislot_cap.present)
1453 cm3->high_multislot_cap.value = bitvec_get_uint(&bv, 2);
1454
1455 /* This used to be the GERAN Iu mode support bit, but the newer spec
1456 * releases say that it should not be used (always zero), however
1457 * we will just ignore tha state of this bit. */
1458 bitvec_get_uint(&bv, 1);
1459
1460 cm3->geran_feature_package_2 = bitvec_get_uint(&bv, 1);
1461 cm3->gmsk_multislot_power_prof = bitvec_get_uint(&bv, 2);
1462 cm3->psk8_multislot_power_prof = bitvec_get_uint(&bv, 2);
1463
1464 /* Release 6 starts here */
1465 cm3->t_gsm_400_bands_supp.present = bitvec_get_uint(&bv, 1);
1466 if (cm3->t_gsm_400_bands_supp.present) {
1467 cm3->t_gsm_400_bands_supp.value = bitvec_get_uint(&bv, 2);
1468 cm3->t_gsm_400_bands_supp.assoc_radio_cap =
1469 bitvec_get_uint(&bv, 4);
1470 }
1471
1472 /* This used to be T-GSM 900 associated radio capability, but the
1473 * newer spec releases say that this bit should not be used, but if
1474 * it is used by some MS anyway we must assume that there is data
1475 * we have to override. */
1476 if (bitvec_get_uint(&bv, 1))
1477 bitvec_get_uint(&bv, 4);
1478
1479 cm3->dl_advanced_rx_perf = bitvec_get_uint(&bv, 2);
1480 cm3->dtm_enhancements_cap = bitvec_get_uint(&bv, 1);
1481
1482 cm3->dtm_gprs_high_multislot_cap.present = bitvec_get_uint(&bv, 1);
1483 if (cm3->dtm_gprs_high_multislot_cap.present) {
1484 cm3->dtm_gprs_high_multislot_cap.mslot_class =
1485 bitvec_get_uint(&bv, 3);
1486 cm3->dtm_gprs_high_multislot_cap.offset_required =
1487 bitvec_get_uint(&bv, 1);
1488 cm3->dtm_gprs_high_multislot_cap.dtm_egprs_high_multislot_cap.
1489 present = bitvec_get_uint(&bv, 1);
1490 if (cm3->dtm_gprs_high_multislot_cap.
1491 dtm_egprs_high_multislot_cap.present)
1492 cm3->dtm_gprs_high_multislot_cap.
1493 dtm_egprs_high_multislot_cap.mslot_class =
1494 bitvec_get_uint(&bv, 3);
1495 }
1496
1497 cm3->repeated_acch_capability = bitvec_get_uint(&bv, 1);
1498
1499 /* Release 7 starts here */
1500 cm3->gsm_710_assoc_radio_cap.present = bitvec_get_uint(&bv, 1);
1501 if (cm3->gsm_710_assoc_radio_cap.present)
1502 cm3->gsm_710_assoc_radio_cap.value = bitvec_get_uint(&bv, 4);
1503
1504 cm3->t_gsm_810_assoc_radio_cap.present = bitvec_get_uint(&bv, 1);
1505 if (cm3->t_gsm_810_assoc_radio_cap.present)
1506 cm3->t_gsm_810_assoc_radio_cap.value = bitvec_get_uint(&bv, 4);
1507
1508 cm3->ciphering_mode_setting_cap = bitvec_get_uint(&bv, 1);
1509 cm3->add_pos_cap = bitvec_get_uint(&bv, 1);
1510
1511 /* Release 8 starts here */
1512 cm3->e_utra_fdd_supp = bitvec_get_uint(&bv, 1);
1513 cm3->e_utra_tdd_supp = bitvec_get_uint(&bv, 1);
1514 cm3->e_utra_meas_rep_supp = bitvec_get_uint(&bv, 1);
1515 cm3->prio_resel_supp = bitvec_get_uint(&bv, 1);
1516
1517 /* Release 9 starts here */
1518 cm3->utra_csg_cells_rep = bitvec_get_uint(&bv, 1);
1519
1520 cm3->vamos_level = bitvec_get_uint(&bv, 2);
1521
1522 /* Release 10 starts here */
1523 cm3->tighter_capability = bitvec_get_uint(&bv, 2);
1524 cm3->sel_ciph_dl_sacch = bitvec_get_uint(&bv, 1);
1525
1526 /* Release 11 starts here */
1527 cm3->cs_ps_srvcc_geran_utra = bitvec_get_uint(&bv, 2);
1528 cm3->cs_ps_srvcc_geran_eutra = bitvec_get_uint(&bv, 2);
1529
1530 cm3->geran_net_sharing = bitvec_get_uint(&bv, 1);
1531 cm3->e_utra_wb_rsrq_meas_supp = bitvec_get_uint(&bv, 1);
1532
1533 /* Release 12 starts here */
1534 cm3->er_band_support = bitvec_get_uint(&bv, 1);
1535 cm3->utra_mult_band_ind_supp = bitvec_get_uint(&bv, 1);
1536 cm3->e_utra_mult_band_ind_supp = bitvec_get_uint(&bv, 1);
1537 cm3->extended_tsc_set_cap_supp = bitvec_get_uint(&bv, 1);
1538
1539 /* Late addition of a release 11 feature */
1540 cm3->extended_earfcn_val_range = bitvec_get_uint(&bv, 1);
1541
1542 return 0;
1543}
Harald Welte96e2a002017-06-12 21:44:18 +02001544/*! @} */