blob: 48d0d3798f16e7c6ccdcedc84a3482a6377afae2 [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 *
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 *
26 */
27
28
29#include <stdint.h>
30#include <string.h>
31#include <errno.h>
32
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010033#include <osmocom/core/utils.h>
34#include <osmocom/core/msgb.h>
35#include <osmocom/gsm/tlv.h>
36#include <osmocom/gsm/mncc.h>
37#include <osmocom/gsm/protocol/gsm_04_08.h>
38#include <osmocom/gsm/gsm48_ie.h>
Harald Welte1e908662010-03-07 23:39:54 +010039
Harald Welte96e2a002017-06-12 21:44:18 +020040/*! \addtogroup gsm0408
41 * @{
42 */
43
Harald Welte1e908662010-03-07 23:39:54 +010044static const char bcd_num_digits[] = {
45 '0', '1', '2', '3', '4', '5', '6', '7',
46 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
47};
48
Neels Hofmeyr8212fc62019-04-01 14:34:37 +020049/*! Like gsm48_decode_bcd_number2() but with less airtight bounds checking.
Harald Welte96e2a002017-06-12 21:44:18 +020050 * \param[out] Caller-provided output buffer
51 * \param[in] bcd_lv Length-Value portion of to-be-decoded IE
52 * \param[in] h_len Length of an optional heder between L and V portion
53 * \returns - in case of success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +010054int gsm48_decode_bcd_number(char *output, int output_len,
55 const uint8_t *bcd_lv, int h_len)
56{
57 uint8_t in_len = bcd_lv[0];
Neels Hofmeyr83d45312019-04-29 19:15:11 +020058 /* Just assume the input buffer is big enough for the length byte and the following data, so pass in_len + 1 for
59 * the input buffer size. */
60 return gsm48_decode_bcd_number2(output, output_len, bcd_lv, in_len + 1, h_len);
61}
62
63/*! Decode a 'called/calling/connect party BCD number' as in 10.5.4.7.
64 * \param[out] output Caller-provided output buffer.
65 * \param[in] output_len sizeof(output).
66 * \param[in] bcd_lv Length-Value part of to-be-decoded IE.
67 * \param[in] input_len Size of the bcd_lv buffer for bounds checking.
68 * \param[in] h_len Length of an optional header between L and V parts.
69 * \return 0 in case of success, negative on error. Errors checked: no or too little input data, no or too little
70 * output buffer size, IE length exceeds input data size, decoded number exceeds size of the output buffer. The output
71 * is guaranteed to be nul terminated iff output_len > 0.
72 */
73int gsm48_decode_bcd_number2(char *output, size_t output_len,
74 const uint8_t *bcd_lv, size_t input_len,
75 size_t h_len)
76{
77 uint8_t in_len;
Harald Welte1e908662010-03-07 23:39:54 +010078 int i;
Neels Hofmeyr83d45312019-04-29 19:15:11 +020079 if (output_len < 1)
80 return -ENOSPC;
81 *output = '\0';
82 if (input_len < 1)
83 return -EIO;
84 in_len = bcd_lv[0];
85 /* len + 1: the BCD length plus the length byte itself must fit in the input buffer. */
86 if (input_len < in_len + 1)
87 return -EIO;
Harald Welte1e908662010-03-07 23:39:54 +010088
89 for (i = 1 + h_len; i <= in_len; i++) {
90 /* lower nibble */
Harald Welte1e908662010-03-07 23:39:54 +010091 if (output_len <= 1)
92 break;
93 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
Vadim Yanitskiy2cd1dda2019-05-26 00:14:16 +070094 output_len--;
Harald Welte1e908662010-03-07 23:39:54 +010095
96 /* higher nibble */
Harald Welte1e908662010-03-07 23:39:54 +010097 if (output_len <= 1)
98 break;
99 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
Vadim Yanitskiy2cd1dda2019-05-26 00:14:16 +0700100 output_len--;
Harald Welte1e908662010-03-07 23:39:54 +0100101 }
102 if (output_len >= 1)
103 *output++ = '\0';
104
105 return 0;
106}
107
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200108/*! convert a single ASCII character to call-control BCD */
Harald Welte1e908662010-03-07 23:39:54 +0100109static int asc_to_bcd(const char asc)
110{
111 int i;
112
113 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
114 if (bcd_num_digits[i] == asc)
115 return i;
116 }
117 return -EINVAL;
118}
119
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200120/*! convert a ASCII phone number to 'called/calling/connect party BCD number'
Harald Welte96e2a002017-06-12 21:44:18 +0200121 * \param[out] bcd_lv Caller-provided output buffer
122 * \param[in] max_len Maximum Length of \a bcd_lv
123 * \param[in] h_len Length of an optional heder between L and V portion
124 * \param[in] input phone number as 0-terminated ASCII
125 * \returns number of bytes used in \a bcd_lv */
Harald Welte1e908662010-03-07 23:39:54 +0100126int gsm48_encode_bcd_number(uint8_t *bcd_lv, uint8_t max_len,
127 int h_len, const char *input)
128{
129 int in_len = strlen(input);
130 int i;
131 uint8_t *bcd_cur = bcd_lv + 1 + h_len;
132
133 /* two digits per byte, plus type byte */
134 bcd_lv[0] = in_len/2 + h_len;
135 if (in_len % 2)
136 bcd_lv[0]++;
137
138 if (bcd_lv[0] > max_len)
139 return -EIO;
140
141 for (i = 0; i < in_len; i++) {
142 int rc = asc_to_bcd(input[i]);
143 if (rc < 0)
144 return rc;
145 if (i % 2 == 0)
146 *bcd_cur = rc;
147 else
148 *bcd_cur++ |= (rc << 4);
149 }
150 /* append padding nibble in case of odd length */
151 if (i % 2)
152 *bcd_cur++ |= 0xf0;
153
154 /* return how many bytes we used */
155 return (bcd_cur - bcd_lv);
156}
157
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200158/*! Decode TS 04.08 Bearer Capability IE (10.5.4.5)
Harald Welte96e2a002017-06-12 21:44:18 +0200159 * \param[out] Caller-provided memory for decoded output
160 * \[aram[in] LV portion of TS 04.08 Bearer Capability
161 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100162int gsm48_decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
163 const uint8_t *lv)
164{
165 uint8_t in_len = lv[0];
166 int i, s;
167
168 if (in_len < 1)
169 return -EINVAL;
170
171 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
172
173 /* octet 3 */
174 bcap->transfer = lv[1] & 0x07;
175 bcap->mode = (lv[1] & 0x08) >> 3;
176 bcap->coding = (lv[1] & 0x10) >> 4;
177 bcap->radio = (lv[1] & 0x60) >> 5;
178
Harald Weltec8a0b932012-08-24 21:27:26 +0200179 switch (bcap->transfer) {
180 case GSM_MNCC_BCAP_SPEECH:
Harald Welte1e908662010-03-07 23:39:54 +0100181 i = 1;
182 s = 0;
183 while(!(lv[i] & 0x80)) {
184 i++; /* octet 3a etc */
185 if (in_len < i)
186 return 0;
187 bcap->speech_ver[s++] = lv[i] & 0x0f;
188 bcap->speech_ver[s] = -1; /* end of list */
189 if (i == 2) /* octet 3a */
190 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
191 if (s == 7) /* maximum speech versions + end of list */
192 return 0;
193 }
Harald Weltec8a0b932012-08-24 21:27:26 +0200194 break;
195 case GSM_MNCC_BCAP_UNR_DIG:
196 case GSM_MNCC_BCAP_FAX_G3:
197 i = 1;
198 while(!(lv[i] & 0x80)) {
199 i++; /* octet 3a etc */
200 if (in_len < i)
201 return 0;
202 /* ignore them */
203 }
204 /* octet 4: skip */
205 i++;
206 /* octet 5 */
207 i++;
208 if (in_len < i)
209 return 0;
210 bcap->data.rate_adaption = (lv[i] >> 3) & 3;
211 bcap->data.sig_access = lv[i] & 7;
212 while(!(lv[i] & 0x80)) {
213 i++; /* octet 5a etc */
214 if (in_len < i)
215 return 0;
216 /* ignore them */
217 }
218 /* octet 6 */
219 i++;
220 if (in_len < i)
221 return 0;
222 bcap->data.async = lv[i] & 1;
223 if (!(lv[i] & 0x80)) {
224 i++;
225 if (in_len < i)
226 return 0;
227 /* octet 6a */
228 bcap->data.nr_stop_bits = ((lv[i] >> 7) & 1) + 1;
229 if (lv[i] & 0x10)
230 bcap->data.nr_data_bits = 8;
231 else
232 bcap->data.nr_data_bits = 7;
233 bcap->data.user_rate = lv[i] & 0xf;
234
235 if (!(lv[i] & 0x80)) {
236 i++;
237 if (in_len < i)
238 return 0;
239 /* octet 6b */
240 bcap->data.parity = lv[i] & 7;
241 bcap->data.interm_rate = (lv[i] >> 5) & 3;
242
243 /* octet 6c */
244 if (!(lv[i] & 0x80)) {
245 i++;
246 if (in_len < i)
247 return 0;
248 bcap->data.transp = (lv[i] >> 5) & 3;
249 bcap->data.modem_type = lv[i] & 0x1F;
250 }
251 }
252
253 }
254 break;
255 default:
Harald Welte1e908662010-03-07 23:39:54 +0100256 i = 1;
257 while (!(lv[i] & 0x80)) {
258 i++; /* octet 3a etc */
259 if (in_len < i)
260 return 0;
261 /* ignore them */
262 }
263 /* FIXME: implement OCTET 4+ parsing */
Harald Weltec8a0b932012-08-24 21:27:26 +0200264 break;
Harald Welte1e908662010-03-07 23:39:54 +0100265 }
266
267 return 0;
268}
269
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200270/*! Encode TS 04.08 Bearer Capability IE (10.5.4.5)
Harald Welte96e2a002017-06-12 21:44:18 +0200271 * \param[out] msg Message Buffer to which IE is to be appended
272 * \param[in] lv_only Write only LV portion (1) or TLV (0)
273 * \param[in] bcap Decoded Bearer Capability to be encoded
274 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100275int gsm48_encode_bearer_cap(struct msgb *msg, int lv_only,
276 const struct gsm_mncc_bearer_cap *bcap)
277{
278 uint8_t lv[32 + 1];
279 int i = 1, s;
280
281 lv[1] = bcap->transfer;
282 lv[1] |= bcap->mode << 3;
283 lv[1] |= bcap->coding << 4;
284 lv[1] |= bcap->radio << 5;
285
Harald Weltec8a0b932012-08-24 21:27:26 +0200286 switch (bcap->transfer) {
287 case GSM_MNCC_BCAP_SPEECH:
Harald Welte1e908662010-03-07 23:39:54 +0100288 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
289 i++; /* octet 3a etc */
290 lv[i] = bcap->speech_ver[s];
291 if (i == 2) /* octet 3a */
292 lv[i] |= bcap->speech_ctm << 5;
293 }
294 lv[i] |= 0x80; /* last IE of octet 3 etc */
Harald Weltec8a0b932012-08-24 21:27:26 +0200295 break;
296 case GSM48_BCAP_ITCAP_UNR_DIG_INF:
297 case GSM48_BCAP_ITCAP_FAX_G3:
298 lv[i++] |= 0x80; /* last IE of octet 3 etc */
299 /* octet 4 */
300 lv[i++] = 0xb8;
301 /* octet 5 */
302 lv[i++] = 0x80 | ((bcap->data.rate_adaption & 3) << 3)
303 | (bcap->data.sig_access & 7);
304 /* octet 6 */
305 lv[i++] = 0x20 | (bcap->data.async & 1);
306 /* octet 6a */
307 lv[i++] = (bcap->data.user_rate & 0xf) |
308 (bcap->data.nr_data_bits == 8 ? 0x10 : 0x00) |
309 (bcap->data.nr_stop_bits == 2 ? 0x40 : 0x00);
310 /* octet 6b */
311 lv[i++] = (bcap->data.parity & 7) |
312 ((bcap->data.interm_rate & 3) << 5);
313 /* octet 6c */
314 lv[i] = 0x80 | (bcap->data.modem_type & 0x1f);
315 break;
316 default:
317 return -EINVAL;
Harald Welte1e908662010-03-07 23:39:54 +0100318 }
319
320 lv[0] = i;
321 if (lv_only)
322 msgb_lv_put(msg, lv[0], lv+1);
323 else
324 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
325
326 return 0;
327}
328
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200329/*! Decode TS 04.08 Call Control Capabilities IE (10.5.4.5a)
Harald Welte96e2a002017-06-12 21:44:18 +0200330 * \param[out] Caller-provided memory for decoded CC capabilities
331 * \param[in] lv Length-Value of IE
332 * \retursns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100333int gsm48_decode_cccap(struct gsm_mncc_cccap *ccap, const uint8_t *lv)
334{
335 uint8_t in_len = lv[0];
336
337 if (in_len < 1)
338 return -EINVAL;
339
340 /* octet 3 */
341 ccap->dtmf = lv[1] & 0x01;
342 ccap->pcp = (lv[1] & 0x02) >> 1;
343
344 return 0;
345}
346
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200347/*! Encodoe TS 04.08 Call Control Capabilities (10.5.4.5a)
Harald Welte96e2a002017-06-12 21:44:18 +0200348 * \param[out] msg Message Buffer to which to append IE (as TLV)
349 * \param[in] ccap Decoded CC Capabilities to be encoded
350 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100351int gsm48_encode_cccap(struct msgb *msg,
352 const struct gsm_mncc_cccap *ccap)
353{
354 uint8_t lv[2];
355
356 lv[0] = 1;
357 lv[1] = 0;
358 if (ccap->dtmf)
359 lv [1] |= 0x01;
360 if (ccap->pcp)
361 lv [1] |= 0x02;
362
363 msgb_tlv_put(msg, GSM48_IE_CC_CAP, lv[0], lv+1);
364
365 return 0;
366}
367
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200368/*! Decode TS 04.08 Called Party BCD Number IE (10.5.4.7)
Harald Welte96e2a002017-06-12 21:44:18 +0200369 * \param[out] called Caller-provided memory for decoded number
370 * \param[in] lv Length-Value portion of IE
371 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100372int gsm48_decode_called(struct gsm_mncc_number *called,
373 const uint8_t *lv)
374{
375 uint8_t in_len = lv[0];
376
377 if (in_len < 1)
378 return -EINVAL;
379
380 /* octet 3 */
381 called->plan = lv[1] & 0x0f;
382 called->type = (lv[1] & 0x70) >> 4;
383
384 /* octet 4..N */
385 gsm48_decode_bcd_number(called->number, sizeof(called->number), lv, 1);
386
387 return 0;
388}
389
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200390/*! Encode TS 04.08 Called Party IE (10.5.4.7)
Harald Welte96e2a002017-06-12 21:44:18 +0200391 * \param[out] msg Mesage Buffer to which to append IE (as TLV)
392 * \param[in] called MNCC Number to encode/append
393 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100394int gsm48_encode_called(struct msgb *msg,
395 const struct gsm_mncc_number *called)
396{
397 uint8_t lv[18];
398 int ret;
399
400 /* octet 3 */
Sylvain Munaut47ee6932010-09-20 20:59:23 +0200401 lv[1] = 0x80; /* no extension */
402 lv[1] |= called->plan;
Harald Welte1e908662010-03-07 23:39:54 +0100403 lv[1] |= called->type << 4;
404
405 /* octet 4..N, octet 2 */
406 ret = gsm48_encode_bcd_number(lv, sizeof(lv), 1, called->number);
407 if (ret < 0)
408 return ret;
409
410 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
411
412 return 0;
413}
414
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200415/*! Decode TS 04.08 Caller ID
Harald Welte96e2a002017-06-12 21:44:18 +0200416 * \param[out] called Caller-provided memory for decoded number
417 * \param[in] lv Length-Value portion of IE
418 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100419int gsm48_decode_callerid(struct gsm_mncc_number *callerid,
420 const uint8_t *lv)
421{
422 uint8_t in_len = lv[0];
423 int i = 1;
424
425 if (in_len < 1)
426 return -EINVAL;
427
428 /* octet 3 */
429 callerid->plan = lv[1] & 0x0f;
430 callerid->type = (lv[1] & 0x70) >> 4;
431
432 /* octet 3a */
433 if (!(lv[1] & 0x80)) {
434 callerid->screen = lv[2] & 0x03;
435 callerid->present = (lv[2] & 0x60) >> 5;
436 i = 2;
437 }
438
439 /* octet 4..N */
440 gsm48_decode_bcd_number(callerid->number, sizeof(callerid->number), lv, i);
441
442 return 0;
443}
444
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200445/*! Encode TS 04.08 Caller ID IE
Harald Welte96e2a002017-06-12 21:44:18 +0200446 * \param[out] msg Mesage Buffer to which to append IE (as TLV)
447 * \param[in] ie IE Identifier (tag)
448 * \param[in] max_len maximum generated output in bytes
449 * \param[in] callerid MNCC Number to encode/append
450 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100451int gsm48_encode_callerid(struct msgb *msg, int ie, int max_len,
452 const struct gsm_mncc_number *callerid)
453{
454 uint8_t lv[max_len - 1];
455 int h_len = 1;
456 int ret;
457
458 /* octet 3 */
459 lv[1] = callerid->plan;
460 lv[1] |= callerid->type << 4;
461
462 if (callerid->present || callerid->screen) {
463 /* octet 3a */
464 lv[2] = callerid->screen;
465 lv[2] |= callerid->present << 5;
466 lv[2] |= 0x80;
467 h_len++;
468 } else
469 lv[1] |= 0x80;
470
471 /* octet 4..N, octet 2 */
472 ret = gsm48_encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
473 if (ret < 0)
474 return ret;
475
476 msgb_tlv_put(msg, ie, lv[0], lv+1);
477
478 return 0;
479}
480
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200481/*! Decode TS 04.08 Cause IE (10.5.4.11)
Harald Welte96e2a002017-06-12 21:44:18 +0200482 * \param[out] cause Caller-provided memory for output
483 * \param[in] lv LV portion of Cause IE
484 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100485int gsm48_decode_cause(struct gsm_mncc_cause *cause,
486 const uint8_t *lv)
487{
488 uint8_t in_len = lv[0];
489 int i;
490
491 if (in_len < 2)
492 return -EINVAL;
493
494 cause->diag_len = 0;
495
496 /* octet 3 */
497 cause->location = lv[1] & 0x0f;
498 cause->coding = (lv[1] & 0x60) >> 5;
499
500 i = 1;
501 if (!(lv[i] & 0x80)) {
502 i++; /* octet 3a */
503 if (in_len < i+1)
504 return 0;
505 cause->rec = 1;
506 cause->rec_val = lv[i] & 0x7f;
507 }
508 i++;
509
510 /* octet 4 */
511 cause->value = lv[i] & 0x7f;
512 i++;
513
514 if (in_len < i) /* no diag */
515 return 0;
516
517 if (in_len - (i-1) > 32) /* maximum 32 octets */
518 return 0;
519
520 /* octet 5-N */
521 memcpy(cause->diag, lv + i, in_len - (i-1));
522 cause->diag_len = in_len - (i-1);
523
524 return 0;
525}
526
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200527/*! Encode TS 04.08 Cause IE (10.5.4.11)
Harald Welte96e2a002017-06-12 21:44:18 +0200528 * \param[out] msg Message Buffer to which to append IE
529 * \param[in] lv_only Encode as LV (1) or TLV (0)
530 * \param[in] cause Cause value to be encoded
531 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100532int gsm48_encode_cause(struct msgb *msg, int lv_only,
533 const struct gsm_mncc_cause *cause)
534{
535 uint8_t lv[32+4];
536 int i;
537
538 if (cause->diag_len > 32)
539 return -EINVAL;
540
541 /* octet 3 */
542 lv[1] = cause->location;
543 lv[1] |= cause->coding << 5;
544
545 i = 1;
546 if (cause->rec) {
547 i++; /* octet 3a */
548 lv[i] = cause->rec_val;
549 }
550 lv[i] |= 0x80; /* end of octet 3 */
551
552 /* octet 4 */
553 i++;
554 lv[i] = 0x80 | cause->value;
555
556 /* octet 5-N */
557 if (cause->diag_len) {
558 memcpy(lv + i, cause->diag, cause->diag_len);
559 i += cause->diag_len;
560 }
561
562 lv[0] = i;
563 if (lv_only)
564 msgb_lv_put(msg, lv[0], lv+1);
565 else
566 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
567
568 return 0;
569}
570
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200571/*! Decode TS 04.08 Calling Number IE (10.5.4.9) */
Harald Welte1e908662010-03-07 23:39:54 +0100572int gsm48_decode_calling(struct gsm_mncc_number *calling,
573 const uint8_t *lv)
574{
575 return gsm48_decode_callerid(calling, lv);
576}
577
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200578/*! Encode TS 04.08 Calling Number IE (10.5.4.9) */
Harald Welte1e908662010-03-07 23:39:54 +0100579int gsm48_encode_calling(struct msgb *msg,
580 const struct gsm_mncc_number *calling)
581{
582 return gsm48_encode_callerid(msg, GSM48_IE_CALLING_BCD, 14, calling);
583}
584
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200585/*! Decode TS 04.08 Connected Number IE (10.5.4.13) */
Harald Welte1e908662010-03-07 23:39:54 +0100586int gsm48_decode_connected(struct gsm_mncc_number *connected,
587 const uint8_t *lv)
588{
589 return gsm48_decode_callerid(connected, lv);
590}
591
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200592/*! Encode TS 04.08 Connected Number IE (10.5.4.13) */
Harald Welte1e908662010-03-07 23:39:54 +0100593int gsm48_encode_connected(struct msgb *msg,
594 const struct gsm_mncc_number *connected)
595{
596 return gsm48_encode_callerid(msg, GSM48_IE_CONN_BCD, 14, connected);
597}
598
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200599/*! Decode TS 04.08 Redirecting Number IE (10.5.4.21b) */
Harald Welte1e908662010-03-07 23:39:54 +0100600int gsm48_decode_redirecting(struct gsm_mncc_number *redirecting,
601 const uint8_t *lv)
602{
603 return gsm48_decode_callerid(redirecting, lv);
604}
605
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200606/*! Encode TS 04.08 Redirecting Number IE (10.5.4.21b) */
Harald Welte1e908662010-03-07 23:39:54 +0100607int gsm48_encode_redirecting(struct msgb *msg,
608 const struct gsm_mncc_number *redirecting)
609{
610 return gsm48_encode_callerid(msg, GSM48_IE_REDIR_BCD, 19, redirecting);
611}
612
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200613/*! Decode TS 04.08 Facility IE (10.5.4.15) */
Harald Welte1e908662010-03-07 23:39:54 +0100614int gsm48_decode_facility(struct gsm_mncc_facility *facility,
615 const uint8_t *lv)
616{
617 uint8_t in_len = lv[0];
618
619 if (in_len < 1)
620 return -EINVAL;
621
622 if (in_len > sizeof(facility->info))
623 return -EINVAL;
624
625 memcpy(facility->info, lv+1, in_len);
626 facility->len = in_len;
627
628 return 0;
629}
630
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200631/*! Encode TS 04.08 Facility IE (10.5.4.15) */
Harald Welte1e908662010-03-07 23:39:54 +0100632int gsm48_encode_facility(struct msgb *msg, int lv_only,
633 const struct gsm_mncc_facility *facility)
634{
635 uint8_t lv[GSM_MAX_FACILITY + 1];
636
637 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
638 return -EINVAL;
639
640 memcpy(lv+1, facility->info, facility->len);
641 lv[0] = facility->len;
642 if (lv_only)
643 msgb_lv_put(msg, lv[0], lv+1);
644 else
645 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
646
647 return 0;
648}
649
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200650/*! Decode TS 04.08 Notify IE (10.5.4.20) */
Harald Welte1e908662010-03-07 23:39:54 +0100651int gsm48_decode_notify(int *notify, const uint8_t *v)
652{
653 *notify = v[0] & 0x7f;
654
655 return 0;
656}
657
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200658/*! Encode TS 04.08 Notify IE (10.5.4.20) */
Harald Welte1e908662010-03-07 23:39:54 +0100659int gsm48_encode_notify(struct msgb *msg, int notify)
660{
661 msgb_v_put(msg, notify | 0x80);
662
663 return 0;
664}
665
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200666/*! Decode TS 04.08 Signal IE (10.5.4.23) */
Harald Welte1e908662010-03-07 23:39:54 +0100667int gsm48_decode_signal(int *signal, const uint8_t *v)
668{
669 *signal = v[0];
670
671 return 0;
672}
673
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200674/*! Encode TS 04.08 Signal IE (10.5.4.23) */
Harald Welte1e908662010-03-07 23:39:54 +0100675int gsm48_encode_signal(struct msgb *msg, int signal)
676{
677 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
678
679 return 0;
680}
681
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200682/*! Decode TS 04.08 Keypad IE (10.5.4.17) */
Harald Welte1e908662010-03-07 23:39:54 +0100683int gsm48_decode_keypad(int *keypad, const uint8_t *lv)
684{
685 uint8_t in_len = lv[0];
686
687 if (in_len < 1)
688 return -EINVAL;
689
690 *keypad = lv[1] & 0x7f;
691
692 return 0;
693}
694
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200695/*! Encode TS 04.08 Keypad IE (10.5.4.17) */
Harald Welte1e908662010-03-07 23:39:54 +0100696int gsm48_encode_keypad(struct msgb *msg, int keypad)
697{
698 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
699
700 return 0;
701}
702
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200703/*! Decode TS 04.08 Progress IE (10.5.4.21) */
Harald Welte1e908662010-03-07 23:39:54 +0100704int gsm48_decode_progress(struct gsm_mncc_progress *progress,
705 const uint8_t *lv)
706{
707 uint8_t in_len = lv[0];
708
709 if (in_len < 2)
710 return -EINVAL;
711
712 progress->coding = (lv[1] & 0x60) >> 5;
713 progress->location = lv[1] & 0x0f;
714 progress->descr = lv[2] & 0x7f;
715
716 return 0;
717}
718
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200719/*! Encode TS 04.08 Progress IE (10.5.4.21) */
Harald Welte1e908662010-03-07 23:39:54 +0100720int gsm48_encode_progress(struct msgb *msg, int lv_only,
721 const struct gsm_mncc_progress *p)
722{
723 uint8_t lv[3];
724
725 lv[0] = 2;
726 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
727 lv[2] = 0x80 | (p->descr & 0x7f);
728 if (lv_only)
729 msgb_lv_put(msg, lv[0], lv+1);
730 else
731 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
732
733 return 0;
734}
735
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200736/*! Decode TS 04.08 User-User IE (10.5.4.25) */
Harald Welte1e908662010-03-07 23:39:54 +0100737int gsm48_decode_useruser(struct gsm_mncc_useruser *uu,
738 const uint8_t *lv)
739{
740 uint8_t in_len = lv[0];
741 char *info = uu->info;
742 int info_len = sizeof(uu->info);
743 int i;
744
745 if (in_len < 1)
746 return -EINVAL;
747
748 uu->proto = lv[1];
749
750 for (i = 2; i <= in_len; i++) {
751 info_len--;
752 if (info_len <= 1)
753 break;
754 *info++ = lv[i];
755 }
756 if (info_len >= 1)
757 *info++ = '\0';
758
759 return 0;
760}
761
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200762/*! Encode TS 04.08 User-User IE (10.5.4.25) */
Harald Welte1e908662010-03-07 23:39:54 +0100763int gsm48_encode_useruser(struct msgb *msg, int lv_only,
764 const struct gsm_mncc_useruser *uu)
765{
766 uint8_t lv[GSM_MAX_USERUSER + 2];
767
768 if (strlen(uu->info) > GSM_MAX_USERUSER)
769 return -EINVAL;
770
771 lv[0] = 1 + strlen(uu->info);
772 lv[1] = uu->proto;
773 memcpy(lv + 2, uu->info, strlen(uu->info));
774 if (lv_only)
775 msgb_lv_put(msg, lv[0], lv+1);
776 else
777 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
778
779 return 0;
780}
781
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200782/*! Decode TS 04.08 SS Version IE (10.5.4.24) */
Harald Welte1e908662010-03-07 23:39:54 +0100783int gsm48_decode_ssversion(struct gsm_mncc_ssversion *ssv,
784 const uint8_t *lv)
785{
786 uint8_t in_len = lv[0];
787
788 if (in_len < 1 || in_len < sizeof(ssv->info))
789 return -EINVAL;
790
791 memcpy(ssv->info, lv + 1, in_len);
792 ssv->len = in_len;
793
794 return 0;
795}
796
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200797/*! Encode TS 04.08 SS Version IE (10.5.4.24) */
Harald Welte1e908662010-03-07 23:39:54 +0100798int gsm48_encode_ssversion(struct msgb *msg,
799 const struct gsm_mncc_ssversion *ssv)
800{
801 uint8_t lv[GSM_MAX_SSVERSION + 1];
802
803 if (ssv->len > GSM_MAX_SSVERSION)
804 return -EINVAL;
805
806 lv[0] = ssv->len;
807 memcpy(lv + 1, ssv->info, ssv->len);
808 msgb_tlv_put(msg, GSM48_IE_SS_VERS, lv[0], lv+1);
809
810 return 0;
811}
812
813/* decode 'more data' does not require a function, because it has no value */
814
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200815/*! Encode TS 04.08 More Data IE (10.5.4.19) */
Harald Welte1e908662010-03-07 23:39:54 +0100816int gsm48_encode_more(struct msgb *msg)
817{
818 uint8_t *ie;
819
820 ie = msgb_put(msg, 1);
821 ie[0] = GSM48_IE_MORE_DATA;
822
823 return 0;
824}
825
Sylvain Munaut71fd42f2011-09-01 22:05:29 +0200826static int32_t smod(int32_t n, int32_t m)
827{
828 int32_t res;
829
830 res = n % m;
831
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200832 if (res <= 0)
Sylvain Munaut71fd42f2011-09-01 22:05:29 +0200833 res += m;
834
Sylvain Munaut71fd42f2011-09-01 22:05:29 +0200835 return res;
836}
837
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200838/*! Decode TS 04.08 Cell Channel Description IE (10.5.2.1b) and other frequency lists
Pau Espin Pedrold8d0c3e2018-11-15 18:26:01 +0100839 * \param[out] f Caller-provided output memory, an array of 1024 elements
Harald Welte96e2a002017-06-12 21:44:18 +0200840 * \param[in] cd Cell Channel Description IE
841 * \param[in] len Length of \a cd in bytes
842 * \returns 0 on success; negative on error */
Harald Welte1523d702010-08-04 11:46:44 +0200843int gsm48_decode_freq_list(struct gsm_sysinfo_freq *f, uint8_t *cd,
844 uint8_t len, uint8_t mask, uint8_t frqt)
845{
846 int i;
847
848 /* NOTES:
849 *
850 * The Range format uses "SMOD" computation.
851 * e.g. "n SMOD m" equals "((n - 1) % m) + 1"
852 * A cascade of multiple SMOD computations is simpified:
853 * "(n SMOD m) SMOD o" equals "(((n - 1) % m) % o) + 1"
854 *
855 * The Range format uses 16 octets of data in SYSTEM INFORMATION.
856 * When used in dedicated messages, the length can be less.
857 * In this case the ranges are decoded for all frequencies that
858 * fit in the block of given length.
859 */
860
861 /* tabula rasa */
862 for (i = 0; i < 1024; i++)
863 f[i].mask &= ~frqt;
864
865 /* 00..XXX. */
866 if ((cd[0] & 0xc0 & mask) == 0x00) {
867 /* Bit map 0 format */
868 if (len < 16)
869 return -EINVAL;
870 for (i = 1; i <= 124; i++)
871 if ((cd[15 - ((i-1) >> 3)] & (1 << ((i-1) & 7))))
872 f[i].mask |= frqt;
873
874 return 0;
875 }
876
877 /* 10..0XX. */
878 if ((cd[0] & 0xc8 & mask) == 0x80) {
879 /* Range 1024 format */
880 uint16_t w[17]; /* 1..16 */
881 struct gsm48_range_1024 *r = (struct gsm48_range_1024 *)cd;
882
883 if (len < 2)
884 return -EINVAL;
885 memset(w, 0, sizeof(w));
886 if (r->f0)
887 f[0].mask |= frqt;
888 w[1] = (r->w1_hi << 8) | r->w1_lo;
889 if (len >= 4)
890 w[2] = (r->w2_hi << 1) | r->w2_lo;
891 if (len >= 5)
892 w[3] = (r->w3_hi << 2) | r->w3_lo;
893 if (len >= 6)
894 w[4] = (r->w4_hi << 2) | r->w4_lo;
895 if (len >= 7)
896 w[5] = (r->w5_hi << 2) | r->w5_lo;
897 if (len >= 8)
898 w[6] = (r->w6_hi << 2) | r->w6_lo;
899 if (len >= 9)
900 w[7] = (r->w7_hi << 2) | r->w7_lo;
901 if (len >= 10)
902 w[8] = (r->w8_hi << 1) | r->w8_lo;
903 if (len >= 10)
904 w[9] = r->w9;
905 if (len >= 11)
906 w[10] = r->w10;
907 if (len >= 12)
908 w[11] = (r->w11_hi << 6) | r->w11_lo;
909 if (len >= 13)
910 w[12] = (r->w12_hi << 5) | r->w12_lo;
911 if (len >= 14)
912 w[13] = (r->w13_hi << 4) | r->w13_lo;
913 if (len >= 15)
914 w[14] = (r->w14_hi << 3) | r->w14_lo;
915 if (len >= 16)
916 w[15] = (r->w15_hi << 2) | r->w15_lo;
917 if (len >= 16)
918 w[16] = r->w16;
919 if (w[1])
920 f[w[1]].mask |= frqt;
921 if (w[2])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200922 f[smod(w[1] - 512 + w[2], 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200923 if (w[3])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200924 f[smod(w[1] + w[3], 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200925 if (w[4])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200926 f[smod(w[1] - 512 + smod(w[2] - 256 + w[4], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200927 if (w[5])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200928 f[smod(w[1] + smod(w[3] - 256 + w[5], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200929 if (w[6])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200930 f[smod(w[1] - 512 + smod(w[2] + w[6], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200931 if (w[7])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200932 f[smod(w[1] + smod(w[3] + w[7], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200933 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200934 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 +0200935 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200936 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 +0200937 if (w[10])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200938 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 +0200939 if (w[11])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200940 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 +0200941 if (w[12])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200942 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 +0200943 if (w[13])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200944 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 +0200945 if (w[14])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200946 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 +0200947 if (w[15])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200948 f[smod(w[1] + smod(w[3] + smod(w[7] + w[15], 255), 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200949 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200950 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 +0200951
952 return 0;
953 }
954 /* 10..100. */
955 if ((cd[0] & 0xce & mask) == 0x88) {
956 /* Range 512 format */
957 uint16_t w[18]; /* 1..17 */
958 struct gsm48_range_512 *r = (struct gsm48_range_512 *)cd;
959
960 if (len < 4)
961 return -EINVAL;
962 memset(w, 0, sizeof(w));
963 w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
964 w[1] = (r->w1_hi << 2) | r->w1_lo;
965 if (len >= 5)
966 w[2] = (r->w2_hi << 2) | r->w2_lo;
967 if (len >= 6)
968 w[3] = (r->w3_hi << 2) | r->w3_lo;
969 if (len >= 7)
970 w[4] = (r->w4_hi << 1) | r->w4_lo;
971 if (len >= 7)
972 w[5] = r->w5;
973 if (len >= 8)
974 w[6] = r->w6;
975 if (len >= 9)
976 w[7] = (r->w7_hi << 6) | r->w7_lo;
977 if (len >= 10)
978 w[8] = (r->w8_hi << 4) | r->w8_lo;
979 if (len >= 11)
980 w[9] = (r->w9_hi << 2) | r->w9_lo;
981 if (len >= 11)
982 w[10] = r->w10;
983 if (len >= 12)
984 w[11] = r->w11;
985 if (len >= 13)
986 w[12] = (r->w12_hi << 4) | r->w12_lo;
987 if (len >= 14)
988 w[13] = (r->w13_hi << 2) | r->w13_lo;
989 if (len >= 14)
990 w[14] = r->w14;
991 if (len >= 15)
992 w[15] = r->w15;
993 if (len >= 16)
994 w[16] = (r->w16_hi << 3) | r->w16_lo;
995 if (len >= 16)
996 w[17] = r->w17;
997 f[w[0]].mask |= frqt;
998 if (w[1])
999 f[(w[0] + w[1]) % 1024].mask |= frqt;
1000 if (w[2])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001001 f[(w[0] + smod(w[1] - 256 + w[2], 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001002 if (w[3])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001003 f[(w[0] + smod(w[1] + w[3], 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001004 if (w[4])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001005 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 +02001006 if (w[5])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001007 f[(w[0] + smod(w[1] + smod(w[3] - 128 + w[5], 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001008 if (w[6])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001009 f[(w[0] + smod(w[1] - 256 + smod(w[2] + w[6], 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001010 if (w[7])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001011 f[(w[0] + smod(w[1] + smod(w[3] + w[7], 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001012 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001013 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 +02001014 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001015 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 +02001016 if (w[10])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001017 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 +02001018 if (w[11])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001019 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 +02001020 if (w[12])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001021 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 +02001022 if (w[13])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001023 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 +02001024 if (w[14])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001025 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 +02001026 if (w[15])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001027 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 +02001028 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001029 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 +02001030 if (w[17])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001031 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 +02001032
1033 return 0;
1034 }
1035 /* 10..101. */
1036 if ((cd[0] & 0xce & mask) == 0x8a) {
1037 /* Range 256 format */
1038 uint16_t w[22]; /* 1..21 */
1039 struct gsm48_range_256 *r = (struct gsm48_range_256 *)cd;
1040
1041 if (len < 4)
1042 return -EINVAL;
1043 memset(w, 0, sizeof(w));
1044 w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
1045 w[1] = (r->w1_hi << 1) | r->w1_lo;
1046 if (len >= 4)
1047 w[2] = r->w2;
1048 if (len >= 5)
1049 w[3] = r->w3;
1050 if (len >= 6)
1051 w[4] = (r->w4_hi << 5) | r->w4_lo;
1052 if (len >= 7)
1053 w[5] = (r->w5_hi << 3) | r->w5_lo;
1054 if (len >= 8)
1055 w[6] = (r->w6_hi << 1) | r->w6_lo;
1056 if (len >= 8)
1057 w[7] = r->w7;
1058 if (len >= 9)
1059 w[8] = (r->w8_hi << 4) | r->w8_lo;
1060 if (len >= 10)
1061 w[9] = (r->w9_hi << 1) | r->w9_lo;
1062 if (len >= 10)
1063 w[10] = r->w10;
1064 if (len >= 11)
1065 w[11] = (r->w11_hi << 3) | r->w11_lo;
1066 if (len >= 11)
1067 w[12] = r->w12;
1068 if (len >= 12)
1069 w[13] = r->w13;
1070 if (len >= 13)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001071 w[14] = (r->w14_hi << 2) | r->w14_lo;
Harald Welte1523d702010-08-04 11:46:44 +02001072 if (len >= 13)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001073 w[15] = r->w15;
Harald Welte1523d702010-08-04 11:46:44 +02001074 if (len >= 14)
1075 w[16] = (r->w16_hi << 3) | r->w16_lo;
1076 if (len >= 14)
1077 w[17] = r->w17;
1078 if (len >= 15)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001079 w[18] = (r->w18_hi << 3) | r->w18_lo;
Harald Welte1523d702010-08-04 11:46:44 +02001080 if (len >= 15)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001081 w[19] = r->w19;
Harald Welte1523d702010-08-04 11:46:44 +02001082 if (len >= 16)
1083 w[20] = (r->w20_hi << 3) | r->w20_lo;
1084 if (len >= 16)
1085 w[21] = r->w21;
1086 f[w[0]].mask |= frqt;
1087 if (w[1])
1088 f[(w[0] + w[1]) % 1024].mask |= frqt;
1089 if (w[2])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001090 f[(w[0] + smod(w[1] - 128 + w[2], 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001091 if (w[3])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001092 f[(w[0] + smod(w[1] + w[3], 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001093 if (w[4])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001094 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 +02001095 if (w[5])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001096 f[(w[0] + smod(w[1] + smod(w[3] - 64 + w[5], 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001097 if (w[6])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001098 f[(w[0] + smod(w[1] - 128 + smod(w[2] + w[6], 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001099 if (w[7])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001100 f[(w[0] + smod(w[1] + smod(w[3] + w[7], 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001101 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001102 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 +02001103 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001104 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 +02001105 if (w[10])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001106 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 +02001107 if (w[11])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001108 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 +02001109 if (w[12])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001110 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 +02001111 if (w[13])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001112 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 +02001113 if (w[14])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001114 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 +02001115 if (w[15])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001116 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 +02001117 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001118 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 +02001119 if (w[17])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001120 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 +02001121 if (w[18])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001122 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 +02001123 if (w[19])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001124 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 +02001125 if (w[20])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001126 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 +02001127 if (w[21])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001128 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 +02001129
1130 return 0;
1131 }
1132 /* 10..110. */
1133 if ((cd[0] & 0xce & mask) == 0x8c) {
1134 /* Range 128 format */
1135 uint16_t w[29]; /* 1..28 */
1136 struct gsm48_range_128 *r = (struct gsm48_range_128 *)cd;
1137
1138 if (len < 3)
1139 return -EINVAL;
1140 memset(w, 0, sizeof(w));
1141 w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
1142 w[1] = r->w1;
1143 if (len >= 4)
1144 w[2] = r->w2;
1145 if (len >= 5)
1146 w[3] = (r->w3_hi << 4) | r->w3_lo;
1147 if (len >= 6)
1148 w[4] = (r->w4_hi << 1) | r->w4_lo;
1149 if (len >= 6)
1150 w[5] = r->w5;
1151 if (len >= 7)
1152 w[6] = (r->w6_hi << 3) | r->w6_lo;
1153 if (len >= 7)
1154 w[7] = r->w7;
1155 if (len >= 8)
1156 w[8] = r->w8;
1157 if (len >= 8)
1158 w[9] = r->w9;
1159 if (len >= 9)
1160 w[10] = r->w10;
1161 if (len >= 9)
1162 w[11] = r->w11;
1163 if (len >= 10)
1164 w[12] = r->w12;
1165 if (len >= 10)
1166 w[13] = r->w13;
1167 if (len >= 11)
1168 w[14] = r->w14;
1169 if (len >= 11)
1170 w[15] = r->w15;
1171 if (len >= 12)
1172 w[16] = r->w16;
1173 if (len >= 12)
1174 w[17] = r->w17;
1175 if (len >= 13)
1176 w[18] = (r->w18_hi << 1) | r->w18_lo;
1177 if (len >= 13)
1178 w[19] = r->w19;
1179 if (len >= 13)
1180 w[20] = r->w20;
1181 if (len >= 14)
1182 w[21] = (r->w21_hi << 2) | r->w21_lo;
1183 if (len >= 14)
1184 w[22] = r->w22;
1185 if (len >= 14)
1186 w[23] = r->w23;
1187 if (len >= 15)
1188 w[24] = r->w24;
1189 if (len >= 15)
1190 w[25] = r->w25;
1191 if (len >= 16)
1192 w[26] = (r->w26_hi << 1) | r->w26_lo;
1193 if (len >= 16)
1194 w[27] = r->w27;
1195 if (len >= 16)
1196 w[28] = r->w28;
1197 f[w[0]].mask |= frqt;
1198 if (w[1])
1199 f[(w[0] + w[1]) % 1024].mask |= frqt;
1200 if (w[2])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001201 f[(w[0] + smod(w[1] - 64 + w[2], 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001202 if (w[3])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001203 f[(w[0] + smod(w[1] + w[3], 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001204 if (w[4])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001205 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 +02001206 if (w[5])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001207 f[(w[0] + smod(w[1] + smod(w[3] - 32 + w[5], 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001208 if (w[6])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001209 f[(w[0] + smod(w[1] - 64 + smod(w[2] + w[6], 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001210 if (w[7])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001211 f[(w[0] + smod(w[1] + smod(w[3] + w[7], 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001212 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001213 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 +02001214 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001215 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 +02001216 if (w[10])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001217 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 +02001218 if (w[11])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001219 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 +02001220 if (w[12])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001221 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 +02001222 if (w[13])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001223 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 +02001224 if (w[14])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001225 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 +02001226 if (w[15])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001227 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 +02001228 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001229 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 +02001230 if (w[17])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001231 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 +02001232 if (w[18])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001233 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 +02001234 if (w[19])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001235 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 +02001236 if (w[20])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001237 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 +02001238 if (w[21])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001239 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 +02001240 if (w[22])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001241 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 +02001242 if (w[23])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001243 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 +02001244 if (w[24])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001245 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 +02001246 if (w[25])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001247 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 +02001248 if (w[26])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001249 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 +02001250 if (w[27])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001251 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 +02001252 if (w[28])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001253 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 +02001254
1255 return 0;
1256 }
1257 /* 10..111. */
1258 if ((cd[0] & 0xce & mask) == 0x8e) {
1259 /* Variable bitmap format (can be any length >= 3) */
1260 uint16_t orig = 0;
1261 struct gsm48_var_bit *r = (struct gsm48_var_bit *)cd;
1262
1263 if (len < 3)
1264 return -EINVAL;
1265 orig = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
1266 f[orig].mask |= frqt;
1267 for (i = 1; 2 + (i >> 3) < len; i++)
1268 if ((cd[2 + (i >> 3)] & (0x80 >> (i & 7))))
1269 f[(orig + i) % 1024].mask |= frqt;
1270
1271 return 0;
1272 }
1273
1274 return 0;
1275}
Harald Welte96e2a002017-06-12 21:44:18 +02001276/*! @} */