blob: 59f931b2bf5d059aa3eff0cb3b7ae34f2909735b [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.
Vadim Yanitskiy71940872019-05-26 00:49:57 +070069 * \return 0 in case of success, negative on error.
70 *
71 * Errors checked:
72 * - no or too little input data (-EIO),
Vadim Yanitskiye4799f52019-05-26 00:55:20 +070073 * - IE length exceeds input data size (-EINVAL),
Vadim Yanitskiy71940872019-05-26 00:49:57 +070074 * - no or too little output buffer size (-ENOSPC),
75 * - decoded number exceeds size of the output buffer (-ENOSPC).
76 *
77 * The output is guaranteed to be nul terminated iff output_len > 0.
Neels Hofmeyr83d45312019-04-29 19:15:11 +020078 */
79int gsm48_decode_bcd_number2(char *output, size_t output_len,
80 const uint8_t *bcd_lv, size_t input_len,
81 size_t h_len)
82{
83 uint8_t in_len;
Harald Welte1e908662010-03-07 23:39:54 +010084 int i;
Neels Hofmeyr83d45312019-04-29 19:15:11 +020085 if (output_len < 1)
86 return -ENOSPC;
87 *output = '\0';
88 if (input_len < 1)
89 return -EIO;
90 in_len = bcd_lv[0];
91 /* len + 1: the BCD length plus the length byte itself must fit in the input buffer. */
92 if (input_len < in_len + 1)
Vadim Yanitskiye4799f52019-05-26 00:55:20 +070093 return -EINVAL;
Harald Welte1e908662010-03-07 23:39:54 +010094
95 for (i = 1 + h_len; i <= in_len; i++) {
96 /* lower nibble */
Harald Welte1e908662010-03-07 23:39:54 +010097 if (output_len <= 1)
98 break;
99 *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 */
Harald Welte1e908662010-03-07 23:39:54 +0100103 if (output_len <= 1)
104 break;
105 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
Vadim Yanitskiy2cd1dda2019-05-26 00:14:16 +0700106 output_len--;
Harald Welte1e908662010-03-07 23:39:54 +0100107 }
108 if (output_len >= 1)
109 *output++ = '\0';
110
Vadim Yanitskiy71940872019-05-26 00:49:57 +0700111 /* Indicate whether the output was truncated */
112 if (i < in_len)
113 return -ENOSPC;
114
Harald Welte1e908662010-03-07 23:39:54 +0100115 return 0;
116}
117
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200118/*! convert a single ASCII character to call-control BCD */
Harald Welte1e908662010-03-07 23:39:54 +0100119static int asc_to_bcd(const char asc)
120{
121 int i;
122
123 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
124 if (bcd_num_digits[i] == asc)
125 return i;
126 }
127 return -EINVAL;
128}
129
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200130/*! convert a ASCII phone number to 'called/calling/connect party BCD number'
Harald Welte96e2a002017-06-12 21:44:18 +0200131 * \param[out] bcd_lv Caller-provided output buffer
132 * \param[in] max_len Maximum Length of \a bcd_lv
133 * \param[in] h_len Length of an optional heder between L and V portion
134 * \param[in] input phone number as 0-terminated ASCII
Vadim Yanitskiy1dc82642019-05-27 00:53:54 +0700135 * \returns number of bytes used in \a bcd_lv
136 *
137 * Depending on a context (e.g. called or calling party BCD number), the
138 * optional header between L and V parts can contain TON (Type Of Number),
139 * NPI (Numbering Plan Indication), presentation or screening indicator.
140 * NOTE: it is up to the caller to initialize this header!
141 */
Harald Welte1e908662010-03-07 23:39:54 +0100142int gsm48_encode_bcd_number(uint8_t *bcd_lv, uint8_t max_len,
143 int h_len, const char *input)
144{
145 int in_len = strlen(input);
146 int i;
147 uint8_t *bcd_cur = bcd_lv + 1 + h_len;
148
149 /* two digits per byte, plus type byte */
150 bcd_lv[0] = in_len/2 + h_len;
151 if (in_len % 2)
152 bcd_lv[0]++;
153
154 if (bcd_lv[0] > max_len)
155 return -EIO;
156
157 for (i = 0; i < in_len; i++) {
158 int rc = asc_to_bcd(input[i]);
159 if (rc < 0)
160 return rc;
161 if (i % 2 == 0)
162 *bcd_cur = rc;
163 else
164 *bcd_cur++ |= (rc << 4);
165 }
166 /* append padding nibble in case of odd length */
167 if (i % 2)
168 *bcd_cur++ |= 0xf0;
169
170 /* return how many bytes we used */
171 return (bcd_cur - bcd_lv);
172}
173
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200174/*! Decode TS 04.08 Bearer Capability IE (10.5.4.5)
Harald Welte96e2a002017-06-12 21:44:18 +0200175 * \param[out] Caller-provided memory for decoded output
176 * \[aram[in] LV portion of TS 04.08 Bearer Capability
177 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100178int gsm48_decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
179 const uint8_t *lv)
180{
181 uint8_t in_len = lv[0];
182 int i, s;
183
184 if (in_len < 1)
185 return -EINVAL;
186
187 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
188
189 /* octet 3 */
190 bcap->transfer = lv[1] & 0x07;
191 bcap->mode = (lv[1] & 0x08) >> 3;
192 bcap->coding = (lv[1] & 0x10) >> 4;
193 bcap->radio = (lv[1] & 0x60) >> 5;
194
Harald Weltec8a0b932012-08-24 21:27:26 +0200195 switch (bcap->transfer) {
196 case GSM_MNCC_BCAP_SPEECH:
Harald Welte1e908662010-03-07 23:39:54 +0100197 i = 1;
198 s = 0;
199 while(!(lv[i] & 0x80)) {
200 i++; /* octet 3a etc */
201 if (in_len < i)
202 return 0;
203 bcap->speech_ver[s++] = lv[i] & 0x0f;
204 bcap->speech_ver[s] = -1; /* end of list */
205 if (i == 2) /* octet 3a */
206 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
207 if (s == 7) /* maximum speech versions + end of list */
208 return 0;
209 }
Harald Weltec8a0b932012-08-24 21:27:26 +0200210 break;
211 case GSM_MNCC_BCAP_UNR_DIG:
212 case GSM_MNCC_BCAP_FAX_G3:
213 i = 1;
214 while(!(lv[i] & 0x80)) {
215 i++; /* octet 3a etc */
216 if (in_len < i)
217 return 0;
218 /* ignore them */
219 }
220 /* octet 4: skip */
221 i++;
222 /* octet 5 */
223 i++;
224 if (in_len < i)
225 return 0;
226 bcap->data.rate_adaption = (lv[i] >> 3) & 3;
227 bcap->data.sig_access = lv[i] & 7;
228 while(!(lv[i] & 0x80)) {
229 i++; /* octet 5a etc */
230 if (in_len < i)
231 return 0;
232 /* ignore them */
233 }
234 /* octet 6 */
235 i++;
236 if (in_len < i)
237 return 0;
238 bcap->data.async = lv[i] & 1;
239 if (!(lv[i] & 0x80)) {
240 i++;
241 if (in_len < i)
242 return 0;
243 /* octet 6a */
244 bcap->data.nr_stop_bits = ((lv[i] >> 7) & 1) + 1;
245 if (lv[i] & 0x10)
246 bcap->data.nr_data_bits = 8;
247 else
248 bcap->data.nr_data_bits = 7;
249 bcap->data.user_rate = lv[i] & 0xf;
250
251 if (!(lv[i] & 0x80)) {
252 i++;
253 if (in_len < i)
254 return 0;
255 /* octet 6b */
256 bcap->data.parity = lv[i] & 7;
257 bcap->data.interm_rate = (lv[i] >> 5) & 3;
258
259 /* octet 6c */
260 if (!(lv[i] & 0x80)) {
261 i++;
262 if (in_len < i)
263 return 0;
264 bcap->data.transp = (lv[i] >> 5) & 3;
265 bcap->data.modem_type = lv[i] & 0x1F;
266 }
267 }
268
269 }
270 break;
271 default:
Harald Welte1e908662010-03-07 23:39:54 +0100272 i = 1;
273 while (!(lv[i] & 0x80)) {
274 i++; /* octet 3a etc */
275 if (in_len < i)
276 return 0;
277 /* ignore them */
278 }
279 /* FIXME: implement OCTET 4+ parsing */
Harald Weltec8a0b932012-08-24 21:27:26 +0200280 break;
Harald Welte1e908662010-03-07 23:39:54 +0100281 }
282
283 return 0;
284}
285
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200286/*! Encode TS 04.08 Bearer Capability IE (10.5.4.5)
Harald Welte96e2a002017-06-12 21:44:18 +0200287 * \param[out] msg Message Buffer to which IE is to be appended
288 * \param[in] lv_only Write only LV portion (1) or TLV (0)
289 * \param[in] bcap Decoded Bearer Capability to be encoded
290 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100291int gsm48_encode_bearer_cap(struct msgb *msg, int lv_only,
292 const struct gsm_mncc_bearer_cap *bcap)
293{
294 uint8_t lv[32 + 1];
295 int i = 1, s;
296
297 lv[1] = bcap->transfer;
298 lv[1] |= bcap->mode << 3;
299 lv[1] |= bcap->coding << 4;
300 lv[1] |= bcap->radio << 5;
301
Harald Weltec8a0b932012-08-24 21:27:26 +0200302 switch (bcap->transfer) {
303 case GSM_MNCC_BCAP_SPEECH:
Harald Welte1e908662010-03-07 23:39:54 +0100304 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
305 i++; /* octet 3a etc */
306 lv[i] = bcap->speech_ver[s];
307 if (i == 2) /* octet 3a */
308 lv[i] |= bcap->speech_ctm << 5;
309 }
310 lv[i] |= 0x80; /* last IE of octet 3 etc */
Harald Weltec8a0b932012-08-24 21:27:26 +0200311 break;
312 case GSM48_BCAP_ITCAP_UNR_DIG_INF:
313 case GSM48_BCAP_ITCAP_FAX_G3:
314 lv[i++] |= 0x80; /* last IE of octet 3 etc */
315 /* octet 4 */
316 lv[i++] = 0xb8;
317 /* octet 5 */
318 lv[i++] = 0x80 | ((bcap->data.rate_adaption & 3) << 3)
319 | (bcap->data.sig_access & 7);
320 /* octet 6 */
321 lv[i++] = 0x20 | (bcap->data.async & 1);
322 /* octet 6a */
323 lv[i++] = (bcap->data.user_rate & 0xf) |
324 (bcap->data.nr_data_bits == 8 ? 0x10 : 0x00) |
325 (bcap->data.nr_stop_bits == 2 ? 0x40 : 0x00);
326 /* octet 6b */
327 lv[i++] = (bcap->data.parity & 7) |
328 ((bcap->data.interm_rate & 3) << 5);
329 /* octet 6c */
330 lv[i] = 0x80 | (bcap->data.modem_type & 0x1f);
331 break;
332 default:
333 return -EINVAL;
Harald Welte1e908662010-03-07 23:39:54 +0100334 }
335
336 lv[0] = i;
337 if (lv_only)
338 msgb_lv_put(msg, lv[0], lv+1);
339 else
340 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
341
342 return 0;
343}
344
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200345/*! Decode TS 04.08 Call Control Capabilities IE (10.5.4.5a)
Harald Welte96e2a002017-06-12 21:44:18 +0200346 * \param[out] Caller-provided memory for decoded CC capabilities
347 * \param[in] lv Length-Value of IE
348 * \retursns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100349int gsm48_decode_cccap(struct gsm_mncc_cccap *ccap, const uint8_t *lv)
350{
351 uint8_t in_len = lv[0];
352
353 if (in_len < 1)
354 return -EINVAL;
355
356 /* octet 3 */
357 ccap->dtmf = lv[1] & 0x01;
358 ccap->pcp = (lv[1] & 0x02) >> 1;
359
360 return 0;
361}
362
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200363/*! Encodoe TS 04.08 Call Control Capabilities (10.5.4.5a)
Harald Welte96e2a002017-06-12 21:44:18 +0200364 * \param[out] msg Message Buffer to which to append IE (as TLV)
365 * \param[in] ccap Decoded CC Capabilities to be encoded
366 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100367int gsm48_encode_cccap(struct msgb *msg,
368 const struct gsm_mncc_cccap *ccap)
369{
370 uint8_t lv[2];
371
372 lv[0] = 1;
373 lv[1] = 0;
374 if (ccap->dtmf)
375 lv [1] |= 0x01;
376 if (ccap->pcp)
377 lv [1] |= 0x02;
378
379 msgb_tlv_put(msg, GSM48_IE_CC_CAP, lv[0], lv+1);
380
381 return 0;
382}
383
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200384/*! Decode TS 04.08 Called Party BCD Number IE (10.5.4.7)
Harald Welte96e2a002017-06-12 21:44:18 +0200385 * \param[out] called Caller-provided memory for decoded number
386 * \param[in] lv Length-Value portion of IE
387 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100388int gsm48_decode_called(struct gsm_mncc_number *called,
389 const uint8_t *lv)
390{
391 uint8_t in_len = lv[0];
392
393 if (in_len < 1)
394 return -EINVAL;
395
396 /* octet 3 */
397 called->plan = lv[1] & 0x0f;
398 called->type = (lv[1] & 0x70) >> 4;
399
400 /* octet 4..N */
401 gsm48_decode_bcd_number(called->number, sizeof(called->number), lv, 1);
402
403 return 0;
404}
405
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200406/*! Encode TS 04.08 Called Party IE (10.5.4.7)
Harald Welte96e2a002017-06-12 21:44:18 +0200407 * \param[out] msg Mesage Buffer to which to append IE (as TLV)
408 * \param[in] called MNCC Number to encode/append
409 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100410int gsm48_encode_called(struct msgb *msg,
411 const struct gsm_mncc_number *called)
412{
413 uint8_t lv[18];
414 int ret;
415
416 /* octet 3 */
Sylvain Munaut47ee6932010-09-20 20:59:23 +0200417 lv[1] = 0x80; /* no extension */
418 lv[1] |= called->plan;
Harald Welte1e908662010-03-07 23:39:54 +0100419 lv[1] |= called->type << 4;
420
421 /* octet 4..N, octet 2 */
422 ret = gsm48_encode_bcd_number(lv, sizeof(lv), 1, called->number);
423 if (ret < 0)
424 return ret;
425
426 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
427
428 return 0;
429}
430
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200431/*! Decode TS 04.08 Caller ID
Harald Welte96e2a002017-06-12 21:44:18 +0200432 * \param[out] called Caller-provided memory for decoded number
433 * \param[in] lv Length-Value portion of IE
434 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100435int gsm48_decode_callerid(struct gsm_mncc_number *callerid,
436 const uint8_t *lv)
437{
438 uint8_t in_len = lv[0];
439 int i = 1;
440
441 if (in_len < 1)
442 return -EINVAL;
443
444 /* octet 3 */
445 callerid->plan = lv[1] & 0x0f;
446 callerid->type = (lv[1] & 0x70) >> 4;
447
448 /* octet 3a */
449 if (!(lv[1] & 0x80)) {
450 callerid->screen = lv[2] & 0x03;
451 callerid->present = (lv[2] & 0x60) >> 5;
452 i = 2;
453 }
454
455 /* octet 4..N */
456 gsm48_decode_bcd_number(callerid->number, sizeof(callerid->number), lv, i);
457
458 return 0;
459}
460
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200461/*! Encode TS 04.08 Caller ID IE
Harald Welte96e2a002017-06-12 21:44:18 +0200462 * \param[out] msg Mesage Buffer to which to append IE (as TLV)
463 * \param[in] ie IE Identifier (tag)
464 * \param[in] max_len maximum generated output in bytes
465 * \param[in] callerid MNCC Number to encode/append
466 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100467int gsm48_encode_callerid(struct msgb *msg, int ie, int max_len,
468 const struct gsm_mncc_number *callerid)
469{
470 uint8_t lv[max_len - 1];
471 int h_len = 1;
472 int ret;
473
474 /* octet 3 */
475 lv[1] = callerid->plan;
476 lv[1] |= callerid->type << 4;
477
478 if (callerid->present || callerid->screen) {
479 /* octet 3a */
480 lv[2] = callerid->screen;
481 lv[2] |= callerid->present << 5;
482 lv[2] |= 0x80;
483 h_len++;
484 } else
485 lv[1] |= 0x80;
486
487 /* octet 4..N, octet 2 */
488 ret = gsm48_encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
489 if (ret < 0)
490 return ret;
491
492 msgb_tlv_put(msg, ie, lv[0], lv+1);
493
494 return 0;
495}
496
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200497/*! Decode TS 04.08 Cause IE (10.5.4.11)
Harald Welte96e2a002017-06-12 21:44:18 +0200498 * \param[out] cause Caller-provided memory for output
499 * \param[in] lv LV portion of Cause IE
500 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100501int gsm48_decode_cause(struct gsm_mncc_cause *cause,
502 const uint8_t *lv)
503{
504 uint8_t in_len = lv[0];
505 int i;
506
507 if (in_len < 2)
508 return -EINVAL;
509
510 cause->diag_len = 0;
511
512 /* octet 3 */
513 cause->location = lv[1] & 0x0f;
514 cause->coding = (lv[1] & 0x60) >> 5;
515
516 i = 1;
517 if (!(lv[i] & 0x80)) {
518 i++; /* octet 3a */
519 if (in_len < i+1)
520 return 0;
521 cause->rec = 1;
522 cause->rec_val = lv[i] & 0x7f;
523 }
524 i++;
525
526 /* octet 4 */
527 cause->value = lv[i] & 0x7f;
528 i++;
529
530 if (in_len < i) /* no diag */
531 return 0;
532
533 if (in_len - (i-1) > 32) /* maximum 32 octets */
534 return 0;
535
536 /* octet 5-N */
537 memcpy(cause->diag, lv + i, in_len - (i-1));
538 cause->diag_len = in_len - (i-1);
539
540 return 0;
541}
542
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200543/*! Encode TS 04.08 Cause IE (10.5.4.11)
Harald Welte96e2a002017-06-12 21:44:18 +0200544 * \param[out] msg Message Buffer to which to append IE
545 * \param[in] lv_only Encode as LV (1) or TLV (0)
546 * \param[in] cause Cause value to be encoded
547 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100548int gsm48_encode_cause(struct msgb *msg, int lv_only,
549 const struct gsm_mncc_cause *cause)
550{
551 uint8_t lv[32+4];
552 int i;
553
554 if (cause->diag_len > 32)
555 return -EINVAL;
556
557 /* octet 3 */
558 lv[1] = cause->location;
559 lv[1] |= cause->coding << 5;
560
561 i = 1;
562 if (cause->rec) {
563 i++; /* octet 3a */
564 lv[i] = cause->rec_val;
565 }
566 lv[i] |= 0x80; /* end of octet 3 */
567
568 /* octet 4 */
569 i++;
570 lv[i] = 0x80 | cause->value;
571
572 /* octet 5-N */
573 if (cause->diag_len) {
574 memcpy(lv + i, cause->diag, cause->diag_len);
575 i += cause->diag_len;
576 }
577
578 lv[0] = i;
579 if (lv_only)
580 msgb_lv_put(msg, lv[0], lv+1);
581 else
582 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
583
584 return 0;
585}
586
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200587/*! Decode TS 04.08 Calling Number IE (10.5.4.9) */
Harald Welte1e908662010-03-07 23:39:54 +0100588int gsm48_decode_calling(struct gsm_mncc_number *calling,
589 const uint8_t *lv)
590{
591 return gsm48_decode_callerid(calling, lv);
592}
593
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200594/*! Encode TS 04.08 Calling Number IE (10.5.4.9) */
Harald Welte1e908662010-03-07 23:39:54 +0100595int gsm48_encode_calling(struct msgb *msg,
596 const struct gsm_mncc_number *calling)
597{
598 return gsm48_encode_callerid(msg, GSM48_IE_CALLING_BCD, 14, calling);
599}
600
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200601/*! Decode TS 04.08 Connected Number IE (10.5.4.13) */
Harald Welte1e908662010-03-07 23:39:54 +0100602int gsm48_decode_connected(struct gsm_mncc_number *connected,
603 const uint8_t *lv)
604{
605 return gsm48_decode_callerid(connected, lv);
606}
607
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200608/*! Encode TS 04.08 Connected Number IE (10.5.4.13) */
Harald Welte1e908662010-03-07 23:39:54 +0100609int gsm48_encode_connected(struct msgb *msg,
610 const struct gsm_mncc_number *connected)
611{
612 return gsm48_encode_callerid(msg, GSM48_IE_CONN_BCD, 14, connected);
613}
614
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200615/*! Decode TS 04.08 Redirecting Number IE (10.5.4.21b) */
Harald Welte1e908662010-03-07 23:39:54 +0100616int gsm48_decode_redirecting(struct gsm_mncc_number *redirecting,
617 const uint8_t *lv)
618{
619 return gsm48_decode_callerid(redirecting, lv);
620}
621
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200622/*! Encode TS 04.08 Redirecting Number IE (10.5.4.21b) */
Harald Welte1e908662010-03-07 23:39:54 +0100623int gsm48_encode_redirecting(struct msgb *msg,
624 const struct gsm_mncc_number *redirecting)
625{
626 return gsm48_encode_callerid(msg, GSM48_IE_REDIR_BCD, 19, redirecting);
627}
628
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200629/*! Decode TS 04.08 Facility IE (10.5.4.15) */
Harald Welte1e908662010-03-07 23:39:54 +0100630int gsm48_decode_facility(struct gsm_mncc_facility *facility,
631 const uint8_t *lv)
632{
633 uint8_t in_len = lv[0];
634
635 if (in_len < 1)
636 return -EINVAL;
637
638 if (in_len > sizeof(facility->info))
639 return -EINVAL;
640
641 memcpy(facility->info, lv+1, in_len);
642 facility->len = in_len;
643
644 return 0;
645}
646
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200647/*! Encode TS 04.08 Facility IE (10.5.4.15) */
Harald Welte1e908662010-03-07 23:39:54 +0100648int gsm48_encode_facility(struct msgb *msg, int lv_only,
649 const struct gsm_mncc_facility *facility)
650{
651 uint8_t lv[GSM_MAX_FACILITY + 1];
652
653 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
654 return -EINVAL;
655
656 memcpy(lv+1, facility->info, facility->len);
657 lv[0] = facility->len;
658 if (lv_only)
659 msgb_lv_put(msg, lv[0], lv+1);
660 else
661 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
662
663 return 0;
664}
665
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200666/*! Decode TS 04.08 Notify IE (10.5.4.20) */
Harald Welte1e908662010-03-07 23:39:54 +0100667int gsm48_decode_notify(int *notify, const uint8_t *v)
668{
669 *notify = v[0] & 0x7f;
670
671 return 0;
672}
673
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200674/*! Encode TS 04.08 Notify IE (10.5.4.20) */
Harald Welte1e908662010-03-07 23:39:54 +0100675int gsm48_encode_notify(struct msgb *msg, int notify)
676{
677 msgb_v_put(msg, notify | 0x80);
678
679 return 0;
680}
681
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200682/*! Decode TS 04.08 Signal IE (10.5.4.23) */
Harald Welte1e908662010-03-07 23:39:54 +0100683int gsm48_decode_signal(int *signal, const uint8_t *v)
684{
685 *signal = v[0];
686
687 return 0;
688}
689
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200690/*! Encode TS 04.08 Signal IE (10.5.4.23) */
Harald Welte1e908662010-03-07 23:39:54 +0100691int gsm48_encode_signal(struct msgb *msg, int signal)
692{
693 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
694
695 return 0;
696}
697
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200698/*! Decode TS 04.08 Keypad IE (10.5.4.17) */
Harald Welte1e908662010-03-07 23:39:54 +0100699int gsm48_decode_keypad(int *keypad, const uint8_t *lv)
700{
701 uint8_t in_len = lv[0];
702
703 if (in_len < 1)
704 return -EINVAL;
705
706 *keypad = lv[1] & 0x7f;
707
708 return 0;
709}
710
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200711/*! Encode TS 04.08 Keypad IE (10.5.4.17) */
Harald Welte1e908662010-03-07 23:39:54 +0100712int gsm48_encode_keypad(struct msgb *msg, int keypad)
713{
714 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
715
716 return 0;
717}
718
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200719/*! Decode TS 04.08 Progress IE (10.5.4.21) */
Harald Welte1e908662010-03-07 23:39:54 +0100720int gsm48_decode_progress(struct gsm_mncc_progress *progress,
721 const uint8_t *lv)
722{
723 uint8_t in_len = lv[0];
724
725 if (in_len < 2)
726 return -EINVAL;
727
728 progress->coding = (lv[1] & 0x60) >> 5;
729 progress->location = lv[1] & 0x0f;
730 progress->descr = lv[2] & 0x7f;
731
732 return 0;
733}
734
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200735/*! Encode TS 04.08 Progress IE (10.5.4.21) */
Harald Welte1e908662010-03-07 23:39:54 +0100736int gsm48_encode_progress(struct msgb *msg, int lv_only,
737 const struct gsm_mncc_progress *p)
738{
739 uint8_t lv[3];
740
741 lv[0] = 2;
742 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
743 lv[2] = 0x80 | (p->descr & 0x7f);
744 if (lv_only)
745 msgb_lv_put(msg, lv[0], lv+1);
746 else
747 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
748
749 return 0;
750}
751
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200752/*! Decode TS 04.08 User-User IE (10.5.4.25) */
Harald Welte1e908662010-03-07 23:39:54 +0100753int gsm48_decode_useruser(struct gsm_mncc_useruser *uu,
754 const uint8_t *lv)
755{
756 uint8_t in_len = lv[0];
757 char *info = uu->info;
758 int info_len = sizeof(uu->info);
759 int i;
760
761 if (in_len < 1)
762 return -EINVAL;
763
764 uu->proto = lv[1];
765
766 for (i = 2; i <= in_len; i++) {
767 info_len--;
768 if (info_len <= 1)
769 break;
770 *info++ = lv[i];
771 }
772 if (info_len >= 1)
773 *info++ = '\0';
774
775 return 0;
776}
777
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200778/*! Encode TS 04.08 User-User IE (10.5.4.25) */
Harald Welte1e908662010-03-07 23:39:54 +0100779int gsm48_encode_useruser(struct msgb *msg, int lv_only,
780 const struct gsm_mncc_useruser *uu)
781{
782 uint8_t lv[GSM_MAX_USERUSER + 2];
783
784 if (strlen(uu->info) > GSM_MAX_USERUSER)
785 return -EINVAL;
786
787 lv[0] = 1 + strlen(uu->info);
788 lv[1] = uu->proto;
789 memcpy(lv + 2, uu->info, strlen(uu->info));
790 if (lv_only)
791 msgb_lv_put(msg, lv[0], lv+1);
792 else
793 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
794
795 return 0;
796}
797
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200798/*! Decode TS 04.08 SS Version IE (10.5.4.24) */
Harald Welte1e908662010-03-07 23:39:54 +0100799int gsm48_decode_ssversion(struct gsm_mncc_ssversion *ssv,
800 const uint8_t *lv)
801{
802 uint8_t in_len = lv[0];
803
804 if (in_len < 1 || in_len < sizeof(ssv->info))
805 return -EINVAL;
806
807 memcpy(ssv->info, lv + 1, in_len);
808 ssv->len = in_len;
809
810 return 0;
811}
812
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200813/*! Encode TS 04.08 SS Version IE (10.5.4.24) */
Harald Welte1e908662010-03-07 23:39:54 +0100814int gsm48_encode_ssversion(struct msgb *msg,
815 const struct gsm_mncc_ssversion *ssv)
816{
817 uint8_t lv[GSM_MAX_SSVERSION + 1];
818
819 if (ssv->len > GSM_MAX_SSVERSION)
820 return -EINVAL;
821
822 lv[0] = ssv->len;
823 memcpy(lv + 1, ssv->info, ssv->len);
824 msgb_tlv_put(msg, GSM48_IE_SS_VERS, lv[0], lv+1);
825
826 return 0;
827}
828
829/* decode 'more data' does not require a function, because it has no value */
830
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200831/*! Encode TS 04.08 More Data IE (10.5.4.19) */
Harald Welte1e908662010-03-07 23:39:54 +0100832int gsm48_encode_more(struct msgb *msg)
833{
834 uint8_t *ie;
835
836 ie = msgb_put(msg, 1);
837 ie[0] = GSM48_IE_MORE_DATA;
838
839 return 0;
840}
841
Sylvain Munaut71fd42f2011-09-01 22:05:29 +0200842static int32_t smod(int32_t n, int32_t m)
843{
844 int32_t res;
845
846 res = n % m;
847
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200848 if (res <= 0)
Sylvain Munaut71fd42f2011-09-01 22:05:29 +0200849 res += m;
850
Sylvain Munaut71fd42f2011-09-01 22:05:29 +0200851 return res;
852}
853
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200854/*! Decode TS 04.08 Cell Channel Description IE (10.5.2.1b) and other frequency lists
Pau Espin Pedrold8d0c3e2018-11-15 18:26:01 +0100855 * \param[out] f Caller-provided output memory, an array of 1024 elements
Harald Welte96e2a002017-06-12 21:44:18 +0200856 * \param[in] cd Cell Channel Description IE
857 * \param[in] len Length of \a cd in bytes
858 * \returns 0 on success; negative on error */
Harald Welte1523d702010-08-04 11:46:44 +0200859int gsm48_decode_freq_list(struct gsm_sysinfo_freq *f, uint8_t *cd,
860 uint8_t len, uint8_t mask, uint8_t frqt)
861{
862 int i;
863
864 /* NOTES:
865 *
866 * The Range format uses "SMOD" computation.
867 * e.g. "n SMOD m" equals "((n - 1) % m) + 1"
868 * A cascade of multiple SMOD computations is simpified:
869 * "(n SMOD m) SMOD o" equals "(((n - 1) % m) % o) + 1"
870 *
871 * The Range format uses 16 octets of data in SYSTEM INFORMATION.
872 * When used in dedicated messages, the length can be less.
873 * In this case the ranges are decoded for all frequencies that
874 * fit in the block of given length.
875 */
876
877 /* tabula rasa */
878 for (i = 0; i < 1024; i++)
879 f[i].mask &= ~frqt;
880
881 /* 00..XXX. */
882 if ((cd[0] & 0xc0 & mask) == 0x00) {
883 /* Bit map 0 format */
884 if (len < 16)
885 return -EINVAL;
886 for (i = 1; i <= 124; i++)
887 if ((cd[15 - ((i-1) >> 3)] & (1 << ((i-1) & 7))))
888 f[i].mask |= frqt;
889
890 return 0;
891 }
892
893 /* 10..0XX. */
894 if ((cd[0] & 0xc8 & mask) == 0x80) {
895 /* Range 1024 format */
896 uint16_t w[17]; /* 1..16 */
897 struct gsm48_range_1024 *r = (struct gsm48_range_1024 *)cd;
898
899 if (len < 2)
900 return -EINVAL;
901 memset(w, 0, sizeof(w));
902 if (r->f0)
903 f[0].mask |= frqt;
904 w[1] = (r->w1_hi << 8) | r->w1_lo;
905 if (len >= 4)
906 w[2] = (r->w2_hi << 1) | r->w2_lo;
907 if (len >= 5)
908 w[3] = (r->w3_hi << 2) | r->w3_lo;
909 if (len >= 6)
910 w[4] = (r->w4_hi << 2) | r->w4_lo;
911 if (len >= 7)
912 w[5] = (r->w5_hi << 2) | r->w5_lo;
913 if (len >= 8)
914 w[6] = (r->w6_hi << 2) | r->w6_lo;
915 if (len >= 9)
916 w[7] = (r->w7_hi << 2) | r->w7_lo;
917 if (len >= 10)
918 w[8] = (r->w8_hi << 1) | r->w8_lo;
919 if (len >= 10)
920 w[9] = r->w9;
921 if (len >= 11)
922 w[10] = r->w10;
923 if (len >= 12)
924 w[11] = (r->w11_hi << 6) | r->w11_lo;
925 if (len >= 13)
926 w[12] = (r->w12_hi << 5) | r->w12_lo;
927 if (len >= 14)
928 w[13] = (r->w13_hi << 4) | r->w13_lo;
929 if (len >= 15)
930 w[14] = (r->w14_hi << 3) | r->w14_lo;
931 if (len >= 16)
932 w[15] = (r->w15_hi << 2) | r->w15_lo;
933 if (len >= 16)
934 w[16] = r->w16;
935 if (w[1])
936 f[w[1]].mask |= frqt;
937 if (w[2])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200938 f[smod(w[1] - 512 + w[2], 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200939 if (w[3])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200940 f[smod(w[1] + w[3], 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200941 if (w[4])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200942 f[smod(w[1] - 512 + smod(w[2] - 256 + w[4], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200943 if (w[5])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200944 f[smod(w[1] + smod(w[3] - 256 + w[5], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200945 if (w[6])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200946 f[smod(w[1] - 512 + smod(w[2] + w[6], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200947 if (w[7])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200948 f[smod(w[1] + smod(w[3] + w[7], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200949 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200950 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 +0200951 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200952 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 +0200953 if (w[10])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200954 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 +0200955 if (w[11])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200956 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 +0200957 if (w[12])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200958 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 +0200959 if (w[13])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200960 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 +0200961 if (w[14])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200962 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 +0200963 if (w[15])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200964 f[smod(w[1] + smod(w[3] + smod(w[7] + w[15], 255), 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200965 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200966 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 +0200967
968 return 0;
969 }
970 /* 10..100. */
971 if ((cd[0] & 0xce & mask) == 0x88) {
972 /* Range 512 format */
973 uint16_t w[18]; /* 1..17 */
974 struct gsm48_range_512 *r = (struct gsm48_range_512 *)cd;
975
976 if (len < 4)
977 return -EINVAL;
978 memset(w, 0, sizeof(w));
979 w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
980 w[1] = (r->w1_hi << 2) | r->w1_lo;
981 if (len >= 5)
982 w[2] = (r->w2_hi << 2) | r->w2_lo;
983 if (len >= 6)
984 w[3] = (r->w3_hi << 2) | r->w3_lo;
985 if (len >= 7)
986 w[4] = (r->w4_hi << 1) | r->w4_lo;
987 if (len >= 7)
988 w[5] = r->w5;
989 if (len >= 8)
990 w[6] = r->w6;
991 if (len >= 9)
992 w[7] = (r->w7_hi << 6) | r->w7_lo;
993 if (len >= 10)
994 w[8] = (r->w8_hi << 4) | r->w8_lo;
995 if (len >= 11)
996 w[9] = (r->w9_hi << 2) | r->w9_lo;
997 if (len >= 11)
998 w[10] = r->w10;
999 if (len >= 12)
1000 w[11] = r->w11;
1001 if (len >= 13)
1002 w[12] = (r->w12_hi << 4) | r->w12_lo;
1003 if (len >= 14)
1004 w[13] = (r->w13_hi << 2) | r->w13_lo;
1005 if (len >= 14)
1006 w[14] = r->w14;
1007 if (len >= 15)
1008 w[15] = r->w15;
1009 if (len >= 16)
1010 w[16] = (r->w16_hi << 3) | r->w16_lo;
1011 if (len >= 16)
1012 w[17] = r->w17;
1013 f[w[0]].mask |= frqt;
1014 if (w[1])
1015 f[(w[0] + w[1]) % 1024].mask |= frqt;
1016 if (w[2])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001017 f[(w[0] + smod(w[1] - 256 + w[2], 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001018 if (w[3])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001019 f[(w[0] + smod(w[1] + w[3], 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001020 if (w[4])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001021 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 +02001022 if (w[5])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001023 f[(w[0] + smod(w[1] + smod(w[3] - 128 + w[5], 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001024 if (w[6])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001025 f[(w[0] + smod(w[1] - 256 + smod(w[2] + w[6], 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001026 if (w[7])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001027 f[(w[0] + smod(w[1] + smod(w[3] + w[7], 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001028 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001029 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 +02001030 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001031 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 +02001032 if (w[10])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001033 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 +02001034 if (w[11])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001035 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 +02001036 if (w[12])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001037 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 +02001038 if (w[13])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001039 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 +02001040 if (w[14])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001041 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 +02001042 if (w[15])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001043 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 +02001044 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001045 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 +02001046 if (w[17])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001047 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 +02001048
1049 return 0;
1050 }
1051 /* 10..101. */
1052 if ((cd[0] & 0xce & mask) == 0x8a) {
1053 /* Range 256 format */
1054 uint16_t w[22]; /* 1..21 */
1055 struct gsm48_range_256 *r = (struct gsm48_range_256 *)cd;
1056
1057 if (len < 4)
1058 return -EINVAL;
1059 memset(w, 0, sizeof(w));
1060 w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
1061 w[1] = (r->w1_hi << 1) | r->w1_lo;
1062 if (len >= 4)
1063 w[2] = r->w2;
1064 if (len >= 5)
1065 w[3] = r->w3;
1066 if (len >= 6)
1067 w[4] = (r->w4_hi << 5) | r->w4_lo;
1068 if (len >= 7)
1069 w[5] = (r->w5_hi << 3) | r->w5_lo;
1070 if (len >= 8)
1071 w[6] = (r->w6_hi << 1) | r->w6_lo;
1072 if (len >= 8)
1073 w[7] = r->w7;
1074 if (len >= 9)
1075 w[8] = (r->w8_hi << 4) | r->w8_lo;
1076 if (len >= 10)
1077 w[9] = (r->w9_hi << 1) | r->w9_lo;
1078 if (len >= 10)
1079 w[10] = r->w10;
1080 if (len >= 11)
1081 w[11] = (r->w11_hi << 3) | r->w11_lo;
1082 if (len >= 11)
1083 w[12] = r->w12;
1084 if (len >= 12)
1085 w[13] = r->w13;
1086 if (len >= 13)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001087 w[14] = (r->w14_hi << 2) | r->w14_lo;
Harald Welte1523d702010-08-04 11:46:44 +02001088 if (len >= 13)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001089 w[15] = r->w15;
Harald Welte1523d702010-08-04 11:46:44 +02001090 if (len >= 14)
1091 w[16] = (r->w16_hi << 3) | r->w16_lo;
1092 if (len >= 14)
1093 w[17] = r->w17;
1094 if (len >= 15)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001095 w[18] = (r->w18_hi << 3) | r->w18_lo;
Harald Welte1523d702010-08-04 11:46:44 +02001096 if (len >= 15)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001097 w[19] = r->w19;
Harald Welte1523d702010-08-04 11:46:44 +02001098 if (len >= 16)
1099 w[20] = (r->w20_hi << 3) | r->w20_lo;
1100 if (len >= 16)
1101 w[21] = r->w21;
1102 f[w[0]].mask |= frqt;
1103 if (w[1])
1104 f[(w[0] + w[1]) % 1024].mask |= frqt;
1105 if (w[2])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001106 f[(w[0] + smod(w[1] - 128 + w[2], 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001107 if (w[3])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001108 f[(w[0] + smod(w[1] + w[3], 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001109 if (w[4])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001110 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 +02001111 if (w[5])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001112 f[(w[0] + smod(w[1] + smod(w[3] - 64 + w[5], 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001113 if (w[6])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001114 f[(w[0] + smod(w[1] - 128 + smod(w[2] + w[6], 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001115 if (w[7])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001116 f[(w[0] + smod(w[1] + smod(w[3] + w[7], 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001117 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001118 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 +02001119 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001120 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 +02001121 if (w[10])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001122 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 +02001123 if (w[11])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001124 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 +02001125 if (w[12])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001126 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 +02001127 if (w[13])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001128 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 +02001129 if (w[14])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001130 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 +02001131 if (w[15])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001132 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 +02001133 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001134 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 +02001135 if (w[17])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001136 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 +02001137 if (w[18])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001138 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 +02001139 if (w[19])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001140 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 +02001141 if (w[20])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001142 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 +02001143 if (w[21])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001144 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 +02001145
1146 return 0;
1147 }
1148 /* 10..110. */
1149 if ((cd[0] & 0xce & mask) == 0x8c) {
1150 /* Range 128 format */
1151 uint16_t w[29]; /* 1..28 */
1152 struct gsm48_range_128 *r = (struct gsm48_range_128 *)cd;
1153
1154 if (len < 3)
1155 return -EINVAL;
1156 memset(w, 0, sizeof(w));
1157 w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
1158 w[1] = r->w1;
1159 if (len >= 4)
1160 w[2] = r->w2;
1161 if (len >= 5)
1162 w[3] = (r->w3_hi << 4) | r->w3_lo;
1163 if (len >= 6)
1164 w[4] = (r->w4_hi << 1) | r->w4_lo;
1165 if (len >= 6)
1166 w[5] = r->w5;
1167 if (len >= 7)
1168 w[6] = (r->w6_hi << 3) | r->w6_lo;
1169 if (len >= 7)
1170 w[7] = r->w7;
1171 if (len >= 8)
1172 w[8] = r->w8;
1173 if (len >= 8)
1174 w[9] = r->w9;
1175 if (len >= 9)
1176 w[10] = r->w10;
1177 if (len >= 9)
1178 w[11] = r->w11;
1179 if (len >= 10)
1180 w[12] = r->w12;
1181 if (len >= 10)
1182 w[13] = r->w13;
1183 if (len >= 11)
1184 w[14] = r->w14;
1185 if (len >= 11)
1186 w[15] = r->w15;
1187 if (len >= 12)
1188 w[16] = r->w16;
1189 if (len >= 12)
1190 w[17] = r->w17;
1191 if (len >= 13)
1192 w[18] = (r->w18_hi << 1) | r->w18_lo;
1193 if (len >= 13)
1194 w[19] = r->w19;
1195 if (len >= 13)
1196 w[20] = r->w20;
1197 if (len >= 14)
1198 w[21] = (r->w21_hi << 2) | r->w21_lo;
1199 if (len >= 14)
1200 w[22] = r->w22;
1201 if (len >= 14)
1202 w[23] = r->w23;
1203 if (len >= 15)
1204 w[24] = r->w24;
1205 if (len >= 15)
1206 w[25] = r->w25;
1207 if (len >= 16)
1208 w[26] = (r->w26_hi << 1) | r->w26_lo;
1209 if (len >= 16)
1210 w[27] = r->w27;
1211 if (len >= 16)
1212 w[28] = r->w28;
1213 f[w[0]].mask |= frqt;
1214 if (w[1])
1215 f[(w[0] + w[1]) % 1024].mask |= frqt;
1216 if (w[2])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001217 f[(w[0] + smod(w[1] - 64 + w[2], 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001218 if (w[3])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001219 f[(w[0] + smod(w[1] + w[3], 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001220 if (w[4])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001221 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 +02001222 if (w[5])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001223 f[(w[0] + smod(w[1] + smod(w[3] - 32 + w[5], 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001224 if (w[6])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001225 f[(w[0] + smod(w[1] - 64 + smod(w[2] + w[6], 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001226 if (w[7])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001227 f[(w[0] + smod(w[1] + smod(w[3] + w[7], 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001228 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001229 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 +02001230 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001231 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 +02001232 if (w[10])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001233 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 +02001234 if (w[11])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001235 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 +02001236 if (w[12])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001237 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 +02001238 if (w[13])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001239 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 +02001240 if (w[14])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001241 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 +02001242 if (w[15])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001243 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 +02001244 if (w[16])
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] - 8 + w[16], 15), 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001246 if (w[17])
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] - 8 + w[17], 15), 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001248 if (w[18])
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] - 8 + w[18], 15), 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001250 if (w[19])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001251 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 +02001252 if (w[20])
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] - 8 + w[20], 15), 31), 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001254 if (w[21])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001255 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 +02001256 if (w[22])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001257 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 +02001258 if (w[23])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001259 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 +02001260 if (w[24])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001261 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 +02001262 if (w[25])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001263 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 +02001264 if (w[26])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001265 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 +02001266 if (w[27])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001267 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 +02001268 if (w[28])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001269 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 +02001270
1271 return 0;
1272 }
1273 /* 10..111. */
1274 if ((cd[0] & 0xce & mask) == 0x8e) {
1275 /* Variable bitmap format (can be any length >= 3) */
1276 uint16_t orig = 0;
1277 struct gsm48_var_bit *r = (struct gsm48_var_bit *)cd;
1278
1279 if (len < 3)
1280 return -EINVAL;
1281 orig = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
1282 f[orig].mask |= frqt;
1283 for (i = 1; 2 + (i >> 3) < len; i++)
1284 if ((cd[2 + (i >> 3)] & (0x80 >> (i & 7))))
1285 f[(orig + i) % 1024].mask |= frqt;
1286
1287 return 0;
1288 }
1289
1290 return 0;
1291}
Harald Welte96e2a002017-06-12 21:44:18 +02001292/*! @} */