blob: 688698db684d700039fcf4ef78931649d66f9007 [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
135 * \returns number of bytes used in \a bcd_lv */
Harald Welte1e908662010-03-07 23:39:54 +0100136int gsm48_encode_bcd_number(uint8_t *bcd_lv, uint8_t max_len,
137 int h_len, const char *input)
138{
139 int in_len = strlen(input);
140 int i;
141 uint8_t *bcd_cur = bcd_lv + 1 + h_len;
142
143 /* two digits per byte, plus type byte */
144 bcd_lv[0] = in_len/2 + h_len;
145 if (in_len % 2)
146 bcd_lv[0]++;
147
148 if (bcd_lv[0] > max_len)
149 return -EIO;
150
151 for (i = 0; i < in_len; i++) {
152 int rc = asc_to_bcd(input[i]);
153 if (rc < 0)
154 return rc;
155 if (i % 2 == 0)
156 *bcd_cur = rc;
157 else
158 *bcd_cur++ |= (rc << 4);
159 }
160 /* append padding nibble in case of odd length */
161 if (i % 2)
162 *bcd_cur++ |= 0xf0;
163
164 /* return how many bytes we used */
165 return (bcd_cur - bcd_lv);
166}
167
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200168/*! Decode TS 04.08 Bearer Capability IE (10.5.4.5)
Harald Welte96e2a002017-06-12 21:44:18 +0200169 * \param[out] Caller-provided memory for decoded output
170 * \[aram[in] LV portion of TS 04.08 Bearer Capability
171 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100172int gsm48_decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
173 const uint8_t *lv)
174{
175 uint8_t in_len = lv[0];
176 int i, s;
177
178 if (in_len < 1)
179 return -EINVAL;
180
181 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
182
183 /* octet 3 */
184 bcap->transfer = lv[1] & 0x07;
185 bcap->mode = (lv[1] & 0x08) >> 3;
186 bcap->coding = (lv[1] & 0x10) >> 4;
187 bcap->radio = (lv[1] & 0x60) >> 5;
188
Harald Weltec8a0b932012-08-24 21:27:26 +0200189 switch (bcap->transfer) {
190 case GSM_MNCC_BCAP_SPEECH:
Harald Welte1e908662010-03-07 23:39:54 +0100191 i = 1;
192 s = 0;
193 while(!(lv[i] & 0x80)) {
194 i++; /* octet 3a etc */
195 if (in_len < i)
196 return 0;
197 bcap->speech_ver[s++] = lv[i] & 0x0f;
198 bcap->speech_ver[s] = -1; /* end of list */
199 if (i == 2) /* octet 3a */
200 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
201 if (s == 7) /* maximum speech versions + end of list */
202 return 0;
203 }
Harald Weltec8a0b932012-08-24 21:27:26 +0200204 break;
205 case GSM_MNCC_BCAP_UNR_DIG:
206 case GSM_MNCC_BCAP_FAX_G3:
207 i = 1;
208 while(!(lv[i] & 0x80)) {
209 i++; /* octet 3a etc */
210 if (in_len < i)
211 return 0;
212 /* ignore them */
213 }
214 /* octet 4: skip */
215 i++;
216 /* octet 5 */
217 i++;
218 if (in_len < i)
219 return 0;
220 bcap->data.rate_adaption = (lv[i] >> 3) & 3;
221 bcap->data.sig_access = lv[i] & 7;
222 while(!(lv[i] & 0x80)) {
223 i++; /* octet 5a etc */
224 if (in_len < i)
225 return 0;
226 /* ignore them */
227 }
228 /* octet 6 */
229 i++;
230 if (in_len < i)
231 return 0;
232 bcap->data.async = lv[i] & 1;
233 if (!(lv[i] & 0x80)) {
234 i++;
235 if (in_len < i)
236 return 0;
237 /* octet 6a */
238 bcap->data.nr_stop_bits = ((lv[i] >> 7) & 1) + 1;
239 if (lv[i] & 0x10)
240 bcap->data.nr_data_bits = 8;
241 else
242 bcap->data.nr_data_bits = 7;
243 bcap->data.user_rate = lv[i] & 0xf;
244
245 if (!(lv[i] & 0x80)) {
246 i++;
247 if (in_len < i)
248 return 0;
249 /* octet 6b */
250 bcap->data.parity = lv[i] & 7;
251 bcap->data.interm_rate = (lv[i] >> 5) & 3;
252
253 /* octet 6c */
254 if (!(lv[i] & 0x80)) {
255 i++;
256 if (in_len < i)
257 return 0;
258 bcap->data.transp = (lv[i] >> 5) & 3;
259 bcap->data.modem_type = lv[i] & 0x1F;
260 }
261 }
262
263 }
264 break;
265 default:
Harald Welte1e908662010-03-07 23:39:54 +0100266 i = 1;
267 while (!(lv[i] & 0x80)) {
268 i++; /* octet 3a etc */
269 if (in_len < i)
270 return 0;
271 /* ignore them */
272 }
273 /* FIXME: implement OCTET 4+ parsing */
Harald Weltec8a0b932012-08-24 21:27:26 +0200274 break;
Harald Welte1e908662010-03-07 23:39:54 +0100275 }
276
277 return 0;
278}
279
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200280/*! Encode TS 04.08 Bearer Capability IE (10.5.4.5)
Harald Welte96e2a002017-06-12 21:44:18 +0200281 * \param[out] msg Message Buffer to which IE is to be appended
282 * \param[in] lv_only Write only LV portion (1) or TLV (0)
283 * \param[in] bcap Decoded Bearer Capability to be encoded
284 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100285int gsm48_encode_bearer_cap(struct msgb *msg, int lv_only,
286 const struct gsm_mncc_bearer_cap *bcap)
287{
288 uint8_t lv[32 + 1];
289 int i = 1, s;
290
291 lv[1] = bcap->transfer;
292 lv[1] |= bcap->mode << 3;
293 lv[1] |= bcap->coding << 4;
294 lv[1] |= bcap->radio << 5;
295
Harald Weltec8a0b932012-08-24 21:27:26 +0200296 switch (bcap->transfer) {
297 case GSM_MNCC_BCAP_SPEECH:
Harald Welte1e908662010-03-07 23:39:54 +0100298 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
299 i++; /* octet 3a etc */
300 lv[i] = bcap->speech_ver[s];
301 if (i == 2) /* octet 3a */
302 lv[i] |= bcap->speech_ctm << 5;
303 }
304 lv[i] |= 0x80; /* last IE of octet 3 etc */
Harald Weltec8a0b932012-08-24 21:27:26 +0200305 break;
306 case GSM48_BCAP_ITCAP_UNR_DIG_INF:
307 case GSM48_BCAP_ITCAP_FAX_G3:
308 lv[i++] |= 0x80; /* last IE of octet 3 etc */
309 /* octet 4 */
310 lv[i++] = 0xb8;
311 /* octet 5 */
312 lv[i++] = 0x80 | ((bcap->data.rate_adaption & 3) << 3)
313 | (bcap->data.sig_access & 7);
314 /* octet 6 */
315 lv[i++] = 0x20 | (bcap->data.async & 1);
316 /* octet 6a */
317 lv[i++] = (bcap->data.user_rate & 0xf) |
318 (bcap->data.nr_data_bits == 8 ? 0x10 : 0x00) |
319 (bcap->data.nr_stop_bits == 2 ? 0x40 : 0x00);
320 /* octet 6b */
321 lv[i++] = (bcap->data.parity & 7) |
322 ((bcap->data.interm_rate & 3) << 5);
323 /* octet 6c */
324 lv[i] = 0x80 | (bcap->data.modem_type & 0x1f);
325 break;
326 default:
327 return -EINVAL;
Harald Welte1e908662010-03-07 23:39:54 +0100328 }
329
330 lv[0] = i;
331 if (lv_only)
332 msgb_lv_put(msg, lv[0], lv+1);
333 else
334 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
335
336 return 0;
337}
338
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200339/*! Decode TS 04.08 Call Control Capabilities IE (10.5.4.5a)
Harald Welte96e2a002017-06-12 21:44:18 +0200340 * \param[out] Caller-provided memory for decoded CC capabilities
341 * \param[in] lv Length-Value of IE
342 * \retursns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100343int gsm48_decode_cccap(struct gsm_mncc_cccap *ccap, const uint8_t *lv)
344{
345 uint8_t in_len = lv[0];
346
347 if (in_len < 1)
348 return -EINVAL;
349
350 /* octet 3 */
351 ccap->dtmf = lv[1] & 0x01;
352 ccap->pcp = (lv[1] & 0x02) >> 1;
353
354 return 0;
355}
356
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200357/*! Encodoe TS 04.08 Call Control Capabilities (10.5.4.5a)
Harald Welte96e2a002017-06-12 21:44:18 +0200358 * \param[out] msg Message Buffer to which to append IE (as TLV)
359 * \param[in] ccap Decoded CC Capabilities to be encoded
360 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100361int gsm48_encode_cccap(struct msgb *msg,
362 const struct gsm_mncc_cccap *ccap)
363{
364 uint8_t lv[2];
365
366 lv[0] = 1;
367 lv[1] = 0;
368 if (ccap->dtmf)
369 lv [1] |= 0x01;
370 if (ccap->pcp)
371 lv [1] |= 0x02;
372
373 msgb_tlv_put(msg, GSM48_IE_CC_CAP, lv[0], lv+1);
374
375 return 0;
376}
377
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200378/*! Decode TS 04.08 Called Party BCD Number IE (10.5.4.7)
Harald Welte96e2a002017-06-12 21:44:18 +0200379 * \param[out] called Caller-provided memory for decoded number
380 * \param[in] lv Length-Value portion of IE
381 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100382int gsm48_decode_called(struct gsm_mncc_number *called,
383 const uint8_t *lv)
384{
385 uint8_t in_len = lv[0];
386
387 if (in_len < 1)
388 return -EINVAL;
389
390 /* octet 3 */
391 called->plan = lv[1] & 0x0f;
392 called->type = (lv[1] & 0x70) >> 4;
393
394 /* octet 4..N */
395 gsm48_decode_bcd_number(called->number, sizeof(called->number), lv, 1);
396
397 return 0;
398}
399
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200400/*! Encode TS 04.08 Called Party IE (10.5.4.7)
Harald Welte96e2a002017-06-12 21:44:18 +0200401 * \param[out] msg Mesage Buffer to which to append IE (as TLV)
402 * \param[in] called MNCC Number to encode/append
403 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100404int gsm48_encode_called(struct msgb *msg,
405 const struct gsm_mncc_number *called)
406{
407 uint8_t lv[18];
408 int ret;
409
410 /* octet 3 */
Sylvain Munaut47ee6932010-09-20 20:59:23 +0200411 lv[1] = 0x80; /* no extension */
412 lv[1] |= called->plan;
Harald Welte1e908662010-03-07 23:39:54 +0100413 lv[1] |= called->type << 4;
414
415 /* octet 4..N, octet 2 */
416 ret = gsm48_encode_bcd_number(lv, sizeof(lv), 1, called->number);
417 if (ret < 0)
418 return ret;
419
420 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
421
422 return 0;
423}
424
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200425/*! Decode TS 04.08 Caller ID
Harald Welte96e2a002017-06-12 21:44:18 +0200426 * \param[out] called Caller-provided memory for decoded number
427 * \param[in] lv Length-Value portion of IE
428 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100429int gsm48_decode_callerid(struct gsm_mncc_number *callerid,
430 const uint8_t *lv)
431{
432 uint8_t in_len = lv[0];
433 int i = 1;
434
435 if (in_len < 1)
436 return -EINVAL;
437
438 /* octet 3 */
439 callerid->plan = lv[1] & 0x0f;
440 callerid->type = (lv[1] & 0x70) >> 4;
441
442 /* octet 3a */
443 if (!(lv[1] & 0x80)) {
444 callerid->screen = lv[2] & 0x03;
445 callerid->present = (lv[2] & 0x60) >> 5;
446 i = 2;
447 }
448
449 /* octet 4..N */
450 gsm48_decode_bcd_number(callerid->number, sizeof(callerid->number), lv, i);
451
452 return 0;
453}
454
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200455/*! Encode TS 04.08 Caller ID IE
Harald Welte96e2a002017-06-12 21:44:18 +0200456 * \param[out] msg Mesage Buffer to which to append IE (as TLV)
457 * \param[in] ie IE Identifier (tag)
458 * \param[in] max_len maximum generated output in bytes
459 * \param[in] callerid MNCC Number to encode/append
460 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100461int gsm48_encode_callerid(struct msgb *msg, int ie, int max_len,
462 const struct gsm_mncc_number *callerid)
463{
464 uint8_t lv[max_len - 1];
465 int h_len = 1;
466 int ret;
467
468 /* octet 3 */
469 lv[1] = callerid->plan;
470 lv[1] |= callerid->type << 4;
471
472 if (callerid->present || callerid->screen) {
473 /* octet 3a */
474 lv[2] = callerid->screen;
475 lv[2] |= callerid->present << 5;
476 lv[2] |= 0x80;
477 h_len++;
478 } else
479 lv[1] |= 0x80;
480
481 /* octet 4..N, octet 2 */
482 ret = gsm48_encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
483 if (ret < 0)
484 return ret;
485
486 msgb_tlv_put(msg, ie, lv[0], lv+1);
487
488 return 0;
489}
490
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200491/*! Decode TS 04.08 Cause IE (10.5.4.11)
Harald Welte96e2a002017-06-12 21:44:18 +0200492 * \param[out] cause Caller-provided memory for output
493 * \param[in] lv LV portion of Cause IE
494 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100495int gsm48_decode_cause(struct gsm_mncc_cause *cause,
496 const uint8_t *lv)
497{
498 uint8_t in_len = lv[0];
499 int i;
500
501 if (in_len < 2)
502 return -EINVAL;
503
504 cause->diag_len = 0;
505
506 /* octet 3 */
507 cause->location = lv[1] & 0x0f;
508 cause->coding = (lv[1] & 0x60) >> 5;
509
510 i = 1;
511 if (!(lv[i] & 0x80)) {
512 i++; /* octet 3a */
513 if (in_len < i+1)
514 return 0;
515 cause->rec = 1;
516 cause->rec_val = lv[i] & 0x7f;
517 }
518 i++;
519
520 /* octet 4 */
521 cause->value = lv[i] & 0x7f;
522 i++;
523
524 if (in_len < i) /* no diag */
525 return 0;
526
527 if (in_len - (i-1) > 32) /* maximum 32 octets */
528 return 0;
529
530 /* octet 5-N */
531 memcpy(cause->diag, lv + i, in_len - (i-1));
532 cause->diag_len = in_len - (i-1);
533
534 return 0;
535}
536
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200537/*! Encode TS 04.08 Cause IE (10.5.4.11)
Harald Welte96e2a002017-06-12 21:44:18 +0200538 * \param[out] msg Message Buffer to which to append IE
539 * \param[in] lv_only Encode as LV (1) or TLV (0)
540 * \param[in] cause Cause value to be encoded
541 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100542int gsm48_encode_cause(struct msgb *msg, int lv_only,
543 const struct gsm_mncc_cause *cause)
544{
545 uint8_t lv[32+4];
546 int i;
547
548 if (cause->diag_len > 32)
549 return -EINVAL;
550
551 /* octet 3 */
552 lv[1] = cause->location;
553 lv[1] |= cause->coding << 5;
554
555 i = 1;
556 if (cause->rec) {
557 i++; /* octet 3a */
558 lv[i] = cause->rec_val;
559 }
560 lv[i] |= 0x80; /* end of octet 3 */
561
562 /* octet 4 */
563 i++;
564 lv[i] = 0x80 | cause->value;
565
566 /* octet 5-N */
567 if (cause->diag_len) {
568 memcpy(lv + i, cause->diag, cause->diag_len);
569 i += cause->diag_len;
570 }
571
572 lv[0] = i;
573 if (lv_only)
574 msgb_lv_put(msg, lv[0], lv+1);
575 else
576 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
577
578 return 0;
579}
580
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200581/*! Decode TS 04.08 Calling Number IE (10.5.4.9) */
Harald Welte1e908662010-03-07 23:39:54 +0100582int gsm48_decode_calling(struct gsm_mncc_number *calling,
583 const uint8_t *lv)
584{
585 return gsm48_decode_callerid(calling, lv);
586}
587
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200588/*! Encode TS 04.08 Calling Number IE (10.5.4.9) */
Harald Welte1e908662010-03-07 23:39:54 +0100589int gsm48_encode_calling(struct msgb *msg,
590 const struct gsm_mncc_number *calling)
591{
592 return gsm48_encode_callerid(msg, GSM48_IE_CALLING_BCD, 14, calling);
593}
594
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200595/*! Decode TS 04.08 Connected Number IE (10.5.4.13) */
Harald Welte1e908662010-03-07 23:39:54 +0100596int gsm48_decode_connected(struct gsm_mncc_number *connected,
597 const uint8_t *lv)
598{
599 return gsm48_decode_callerid(connected, lv);
600}
601
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200602/*! Encode TS 04.08 Connected Number IE (10.5.4.13) */
Harald Welte1e908662010-03-07 23:39:54 +0100603int gsm48_encode_connected(struct msgb *msg,
604 const struct gsm_mncc_number *connected)
605{
606 return gsm48_encode_callerid(msg, GSM48_IE_CONN_BCD, 14, connected);
607}
608
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200609/*! Decode TS 04.08 Redirecting Number IE (10.5.4.21b) */
Harald Welte1e908662010-03-07 23:39:54 +0100610int gsm48_decode_redirecting(struct gsm_mncc_number *redirecting,
611 const uint8_t *lv)
612{
613 return gsm48_decode_callerid(redirecting, lv);
614}
615
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200616/*! Encode TS 04.08 Redirecting Number IE (10.5.4.21b) */
Harald Welte1e908662010-03-07 23:39:54 +0100617int gsm48_encode_redirecting(struct msgb *msg,
618 const struct gsm_mncc_number *redirecting)
619{
620 return gsm48_encode_callerid(msg, GSM48_IE_REDIR_BCD, 19, redirecting);
621}
622
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200623/*! Decode TS 04.08 Facility IE (10.5.4.15) */
Harald Welte1e908662010-03-07 23:39:54 +0100624int gsm48_decode_facility(struct gsm_mncc_facility *facility,
625 const uint8_t *lv)
626{
627 uint8_t in_len = lv[0];
628
629 if (in_len < 1)
630 return -EINVAL;
631
632 if (in_len > sizeof(facility->info))
633 return -EINVAL;
634
635 memcpy(facility->info, lv+1, in_len);
636 facility->len = in_len;
637
638 return 0;
639}
640
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200641/*! Encode TS 04.08 Facility IE (10.5.4.15) */
Harald Welte1e908662010-03-07 23:39:54 +0100642int gsm48_encode_facility(struct msgb *msg, int lv_only,
643 const struct gsm_mncc_facility *facility)
644{
645 uint8_t lv[GSM_MAX_FACILITY + 1];
646
647 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
648 return -EINVAL;
649
650 memcpy(lv+1, facility->info, facility->len);
651 lv[0] = facility->len;
652 if (lv_only)
653 msgb_lv_put(msg, lv[0], lv+1);
654 else
655 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
656
657 return 0;
658}
659
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200660/*! Decode TS 04.08 Notify IE (10.5.4.20) */
Harald Welte1e908662010-03-07 23:39:54 +0100661int gsm48_decode_notify(int *notify, const uint8_t *v)
662{
663 *notify = v[0] & 0x7f;
664
665 return 0;
666}
667
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200668/*! Encode TS 04.08 Notify IE (10.5.4.20) */
Harald Welte1e908662010-03-07 23:39:54 +0100669int gsm48_encode_notify(struct msgb *msg, int notify)
670{
671 msgb_v_put(msg, notify | 0x80);
672
673 return 0;
674}
675
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200676/*! Decode TS 04.08 Signal IE (10.5.4.23) */
Harald Welte1e908662010-03-07 23:39:54 +0100677int gsm48_decode_signal(int *signal, const uint8_t *v)
678{
679 *signal = v[0];
680
681 return 0;
682}
683
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200684/*! Encode TS 04.08 Signal IE (10.5.4.23) */
Harald Welte1e908662010-03-07 23:39:54 +0100685int gsm48_encode_signal(struct msgb *msg, int signal)
686{
687 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
688
689 return 0;
690}
691
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200692/*! Decode TS 04.08 Keypad IE (10.5.4.17) */
Harald Welte1e908662010-03-07 23:39:54 +0100693int gsm48_decode_keypad(int *keypad, const uint8_t *lv)
694{
695 uint8_t in_len = lv[0];
696
697 if (in_len < 1)
698 return -EINVAL;
699
700 *keypad = lv[1] & 0x7f;
701
702 return 0;
703}
704
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200705/*! Encode TS 04.08 Keypad IE (10.5.4.17) */
Harald Welte1e908662010-03-07 23:39:54 +0100706int gsm48_encode_keypad(struct msgb *msg, int keypad)
707{
708 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
709
710 return 0;
711}
712
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200713/*! Decode TS 04.08 Progress IE (10.5.4.21) */
Harald Welte1e908662010-03-07 23:39:54 +0100714int gsm48_decode_progress(struct gsm_mncc_progress *progress,
715 const uint8_t *lv)
716{
717 uint8_t in_len = lv[0];
718
719 if (in_len < 2)
720 return -EINVAL;
721
722 progress->coding = (lv[1] & 0x60) >> 5;
723 progress->location = lv[1] & 0x0f;
724 progress->descr = lv[2] & 0x7f;
725
726 return 0;
727}
728
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200729/*! Encode TS 04.08 Progress IE (10.5.4.21) */
Harald Welte1e908662010-03-07 23:39:54 +0100730int gsm48_encode_progress(struct msgb *msg, int lv_only,
731 const struct gsm_mncc_progress *p)
732{
733 uint8_t lv[3];
734
735 lv[0] = 2;
736 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
737 lv[2] = 0x80 | (p->descr & 0x7f);
738 if (lv_only)
739 msgb_lv_put(msg, lv[0], lv+1);
740 else
741 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
742
743 return 0;
744}
745
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200746/*! Decode TS 04.08 User-User IE (10.5.4.25) */
Harald Welte1e908662010-03-07 23:39:54 +0100747int gsm48_decode_useruser(struct gsm_mncc_useruser *uu,
748 const uint8_t *lv)
749{
750 uint8_t in_len = lv[0];
751 char *info = uu->info;
752 int info_len = sizeof(uu->info);
753 int i;
754
755 if (in_len < 1)
756 return -EINVAL;
757
758 uu->proto = lv[1];
759
760 for (i = 2; i <= in_len; i++) {
761 info_len--;
762 if (info_len <= 1)
763 break;
764 *info++ = lv[i];
765 }
766 if (info_len >= 1)
767 *info++ = '\0';
768
769 return 0;
770}
771
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200772/*! Encode TS 04.08 User-User IE (10.5.4.25) */
Harald Welte1e908662010-03-07 23:39:54 +0100773int gsm48_encode_useruser(struct msgb *msg, int lv_only,
774 const struct gsm_mncc_useruser *uu)
775{
776 uint8_t lv[GSM_MAX_USERUSER + 2];
777
778 if (strlen(uu->info) > GSM_MAX_USERUSER)
779 return -EINVAL;
780
781 lv[0] = 1 + strlen(uu->info);
782 lv[1] = uu->proto;
783 memcpy(lv + 2, uu->info, strlen(uu->info));
784 if (lv_only)
785 msgb_lv_put(msg, lv[0], lv+1);
786 else
787 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
788
789 return 0;
790}
791
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200792/*! Decode TS 04.08 SS Version IE (10.5.4.24) */
Harald Welte1e908662010-03-07 23:39:54 +0100793int gsm48_decode_ssversion(struct gsm_mncc_ssversion *ssv,
794 const uint8_t *lv)
795{
796 uint8_t in_len = lv[0];
797
798 if (in_len < 1 || in_len < sizeof(ssv->info))
799 return -EINVAL;
800
801 memcpy(ssv->info, lv + 1, in_len);
802 ssv->len = in_len;
803
804 return 0;
805}
806
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200807/*! Encode TS 04.08 SS Version IE (10.5.4.24) */
Harald Welte1e908662010-03-07 23:39:54 +0100808int gsm48_encode_ssversion(struct msgb *msg,
809 const struct gsm_mncc_ssversion *ssv)
810{
811 uint8_t lv[GSM_MAX_SSVERSION + 1];
812
813 if (ssv->len > GSM_MAX_SSVERSION)
814 return -EINVAL;
815
816 lv[0] = ssv->len;
817 memcpy(lv + 1, ssv->info, ssv->len);
818 msgb_tlv_put(msg, GSM48_IE_SS_VERS, lv[0], lv+1);
819
820 return 0;
821}
822
823/* decode 'more data' does not require a function, because it has no value */
824
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200825/*! Encode TS 04.08 More Data IE (10.5.4.19) */
Harald Welte1e908662010-03-07 23:39:54 +0100826int gsm48_encode_more(struct msgb *msg)
827{
828 uint8_t *ie;
829
830 ie = msgb_put(msg, 1);
831 ie[0] = GSM48_IE_MORE_DATA;
832
833 return 0;
834}
835
Sylvain Munaut71fd42f2011-09-01 22:05:29 +0200836static int32_t smod(int32_t n, int32_t m)
837{
838 int32_t res;
839
840 res = n % m;
841
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200842 if (res <= 0)
Sylvain Munaut71fd42f2011-09-01 22:05:29 +0200843 res += m;
844
Sylvain Munaut71fd42f2011-09-01 22:05:29 +0200845 return res;
846}
847
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200848/*! Decode TS 04.08 Cell Channel Description IE (10.5.2.1b) and other frequency lists
Pau Espin Pedrold8d0c3e2018-11-15 18:26:01 +0100849 * \param[out] f Caller-provided output memory, an array of 1024 elements
Harald Welte96e2a002017-06-12 21:44:18 +0200850 * \param[in] cd Cell Channel Description IE
851 * \param[in] len Length of \a cd in bytes
852 * \returns 0 on success; negative on error */
Harald Welte1523d702010-08-04 11:46:44 +0200853int gsm48_decode_freq_list(struct gsm_sysinfo_freq *f, uint8_t *cd,
854 uint8_t len, uint8_t mask, uint8_t frqt)
855{
856 int i;
857
858 /* NOTES:
859 *
860 * The Range format uses "SMOD" computation.
861 * e.g. "n SMOD m" equals "((n - 1) % m) + 1"
862 * A cascade of multiple SMOD computations is simpified:
863 * "(n SMOD m) SMOD o" equals "(((n - 1) % m) % o) + 1"
864 *
865 * The Range format uses 16 octets of data in SYSTEM INFORMATION.
866 * When used in dedicated messages, the length can be less.
867 * In this case the ranges are decoded for all frequencies that
868 * fit in the block of given length.
869 */
870
871 /* tabula rasa */
872 for (i = 0; i < 1024; i++)
873 f[i].mask &= ~frqt;
874
875 /* 00..XXX. */
876 if ((cd[0] & 0xc0 & mask) == 0x00) {
877 /* Bit map 0 format */
878 if (len < 16)
879 return -EINVAL;
880 for (i = 1; i <= 124; i++)
881 if ((cd[15 - ((i-1) >> 3)] & (1 << ((i-1) & 7))))
882 f[i].mask |= frqt;
883
884 return 0;
885 }
886
887 /* 10..0XX. */
888 if ((cd[0] & 0xc8 & mask) == 0x80) {
889 /* Range 1024 format */
890 uint16_t w[17]; /* 1..16 */
891 struct gsm48_range_1024 *r = (struct gsm48_range_1024 *)cd;
892
893 if (len < 2)
894 return -EINVAL;
895 memset(w, 0, sizeof(w));
896 if (r->f0)
897 f[0].mask |= frqt;
898 w[1] = (r->w1_hi << 8) | r->w1_lo;
899 if (len >= 4)
900 w[2] = (r->w2_hi << 1) | r->w2_lo;
901 if (len >= 5)
902 w[3] = (r->w3_hi << 2) | r->w3_lo;
903 if (len >= 6)
904 w[4] = (r->w4_hi << 2) | r->w4_lo;
905 if (len >= 7)
906 w[5] = (r->w5_hi << 2) | r->w5_lo;
907 if (len >= 8)
908 w[6] = (r->w6_hi << 2) | r->w6_lo;
909 if (len >= 9)
910 w[7] = (r->w7_hi << 2) | r->w7_lo;
911 if (len >= 10)
912 w[8] = (r->w8_hi << 1) | r->w8_lo;
913 if (len >= 10)
914 w[9] = r->w9;
915 if (len >= 11)
916 w[10] = r->w10;
917 if (len >= 12)
918 w[11] = (r->w11_hi << 6) | r->w11_lo;
919 if (len >= 13)
920 w[12] = (r->w12_hi << 5) | r->w12_lo;
921 if (len >= 14)
922 w[13] = (r->w13_hi << 4) | r->w13_lo;
923 if (len >= 15)
924 w[14] = (r->w14_hi << 3) | r->w14_lo;
925 if (len >= 16)
926 w[15] = (r->w15_hi << 2) | r->w15_lo;
927 if (len >= 16)
928 w[16] = r->w16;
929 if (w[1])
930 f[w[1]].mask |= frqt;
931 if (w[2])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200932 f[smod(w[1] - 512 + w[2], 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200933 if (w[3])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200934 f[smod(w[1] + w[3], 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200935 if (w[4])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200936 f[smod(w[1] - 512 + smod(w[2] - 256 + w[4], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200937 if (w[5])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200938 f[smod(w[1] + smod(w[3] - 256 + w[5], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200939 if (w[6])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200940 f[smod(w[1] - 512 + smod(w[2] + w[6], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200941 if (w[7])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200942 f[smod(w[1] + smod(w[3] + w[7], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200943 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200944 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 +0200945 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200946 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 +0200947 if (w[10])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200948 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 +0200949 if (w[11])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200950 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 +0200951 if (w[12])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200952 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 +0200953 if (w[13])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200954 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 +0200955 if (w[14])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200956 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 +0200957 if (w[15])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200958 f[smod(w[1] + smod(w[3] + smod(w[7] + w[15], 255), 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200959 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200960 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 +0200961
962 return 0;
963 }
964 /* 10..100. */
965 if ((cd[0] & 0xce & mask) == 0x88) {
966 /* Range 512 format */
967 uint16_t w[18]; /* 1..17 */
968 struct gsm48_range_512 *r = (struct gsm48_range_512 *)cd;
969
970 if (len < 4)
971 return -EINVAL;
972 memset(w, 0, sizeof(w));
973 w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
974 w[1] = (r->w1_hi << 2) | r->w1_lo;
975 if (len >= 5)
976 w[2] = (r->w2_hi << 2) | r->w2_lo;
977 if (len >= 6)
978 w[3] = (r->w3_hi << 2) | r->w3_lo;
979 if (len >= 7)
980 w[4] = (r->w4_hi << 1) | r->w4_lo;
981 if (len >= 7)
982 w[5] = r->w5;
983 if (len >= 8)
984 w[6] = r->w6;
985 if (len >= 9)
986 w[7] = (r->w7_hi << 6) | r->w7_lo;
987 if (len >= 10)
988 w[8] = (r->w8_hi << 4) | r->w8_lo;
989 if (len >= 11)
990 w[9] = (r->w9_hi << 2) | r->w9_lo;
991 if (len >= 11)
992 w[10] = r->w10;
993 if (len >= 12)
994 w[11] = r->w11;
995 if (len >= 13)
996 w[12] = (r->w12_hi << 4) | r->w12_lo;
997 if (len >= 14)
998 w[13] = (r->w13_hi << 2) | r->w13_lo;
999 if (len >= 14)
1000 w[14] = r->w14;
1001 if (len >= 15)
1002 w[15] = r->w15;
1003 if (len >= 16)
1004 w[16] = (r->w16_hi << 3) | r->w16_lo;
1005 if (len >= 16)
1006 w[17] = r->w17;
1007 f[w[0]].mask |= frqt;
1008 if (w[1])
1009 f[(w[0] + w[1]) % 1024].mask |= frqt;
1010 if (w[2])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001011 f[(w[0] + smod(w[1] - 256 + w[2], 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001012 if (w[3])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001013 f[(w[0] + smod(w[1] + w[3], 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001014 if (w[4])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001015 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 +02001016 if (w[5])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001017 f[(w[0] + smod(w[1] + smod(w[3] - 128 + w[5], 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001018 if (w[6])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001019 f[(w[0] + smod(w[1] - 256 + smod(w[2] + w[6], 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001020 if (w[7])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001021 f[(w[0] + smod(w[1] + smod(w[3] + w[7], 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001022 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001023 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 +02001024 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001025 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 +02001026 if (w[10])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001027 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 +02001028 if (w[11])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001029 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 +02001030 if (w[12])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001031 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 +02001032 if (w[13])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001033 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 +02001034 if (w[14])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001035 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 +02001036 if (w[15])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001037 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 +02001038 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001039 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 +02001040 if (w[17])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001041 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 +02001042
1043 return 0;
1044 }
1045 /* 10..101. */
1046 if ((cd[0] & 0xce & mask) == 0x8a) {
1047 /* Range 256 format */
1048 uint16_t w[22]; /* 1..21 */
1049 struct gsm48_range_256 *r = (struct gsm48_range_256 *)cd;
1050
1051 if (len < 4)
1052 return -EINVAL;
1053 memset(w, 0, sizeof(w));
1054 w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
1055 w[1] = (r->w1_hi << 1) | r->w1_lo;
1056 if (len >= 4)
1057 w[2] = r->w2;
1058 if (len >= 5)
1059 w[3] = r->w3;
1060 if (len >= 6)
1061 w[4] = (r->w4_hi << 5) | r->w4_lo;
1062 if (len >= 7)
1063 w[5] = (r->w5_hi << 3) | r->w5_lo;
1064 if (len >= 8)
1065 w[6] = (r->w6_hi << 1) | r->w6_lo;
1066 if (len >= 8)
1067 w[7] = r->w7;
1068 if (len >= 9)
1069 w[8] = (r->w8_hi << 4) | r->w8_lo;
1070 if (len >= 10)
1071 w[9] = (r->w9_hi << 1) | r->w9_lo;
1072 if (len >= 10)
1073 w[10] = r->w10;
1074 if (len >= 11)
1075 w[11] = (r->w11_hi << 3) | r->w11_lo;
1076 if (len >= 11)
1077 w[12] = r->w12;
1078 if (len >= 12)
1079 w[13] = r->w13;
1080 if (len >= 13)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001081 w[14] = (r->w14_hi << 2) | r->w14_lo;
Harald Welte1523d702010-08-04 11:46:44 +02001082 if (len >= 13)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001083 w[15] = r->w15;
Harald Welte1523d702010-08-04 11:46:44 +02001084 if (len >= 14)
1085 w[16] = (r->w16_hi << 3) | r->w16_lo;
1086 if (len >= 14)
1087 w[17] = r->w17;
1088 if (len >= 15)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001089 w[18] = (r->w18_hi << 3) | r->w18_lo;
Harald Welte1523d702010-08-04 11:46:44 +02001090 if (len >= 15)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001091 w[19] = r->w19;
Harald Welte1523d702010-08-04 11:46:44 +02001092 if (len >= 16)
1093 w[20] = (r->w20_hi << 3) | r->w20_lo;
1094 if (len >= 16)
1095 w[21] = r->w21;
1096 f[w[0]].mask |= frqt;
1097 if (w[1])
1098 f[(w[0] + w[1]) % 1024].mask |= frqt;
1099 if (w[2])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001100 f[(w[0] + smod(w[1] - 128 + w[2], 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001101 if (w[3])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001102 f[(w[0] + smod(w[1] + w[3], 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001103 if (w[4])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001104 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 +02001105 if (w[5])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001106 f[(w[0] + smod(w[1] + smod(w[3] - 64 + w[5], 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001107 if (w[6])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001108 f[(w[0] + smod(w[1] - 128 + smod(w[2] + w[6], 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001109 if (w[7])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001110 f[(w[0] + smod(w[1] + smod(w[3] + w[7], 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001111 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001112 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 +02001113 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001114 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 +02001115 if (w[10])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001116 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 +02001117 if (w[11])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001118 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 +02001119 if (w[12])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001120 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 +02001121 if (w[13])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001122 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 +02001123 if (w[14])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001124 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 +02001125 if (w[15])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001126 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 +02001127 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001128 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 +02001129 if (w[17])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001130 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 +02001131 if (w[18])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001132 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 +02001133 if (w[19])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001134 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 +02001135 if (w[20])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001136 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 +02001137 if (w[21])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001138 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 +02001139
1140 return 0;
1141 }
1142 /* 10..110. */
1143 if ((cd[0] & 0xce & mask) == 0x8c) {
1144 /* Range 128 format */
1145 uint16_t w[29]; /* 1..28 */
1146 struct gsm48_range_128 *r = (struct gsm48_range_128 *)cd;
1147
1148 if (len < 3)
1149 return -EINVAL;
1150 memset(w, 0, sizeof(w));
1151 w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
1152 w[1] = r->w1;
1153 if (len >= 4)
1154 w[2] = r->w2;
1155 if (len >= 5)
1156 w[3] = (r->w3_hi << 4) | r->w3_lo;
1157 if (len >= 6)
1158 w[4] = (r->w4_hi << 1) | r->w4_lo;
1159 if (len >= 6)
1160 w[5] = r->w5;
1161 if (len >= 7)
1162 w[6] = (r->w6_hi << 3) | r->w6_lo;
1163 if (len >= 7)
1164 w[7] = r->w7;
1165 if (len >= 8)
1166 w[8] = r->w8;
1167 if (len >= 8)
1168 w[9] = r->w9;
1169 if (len >= 9)
1170 w[10] = r->w10;
1171 if (len >= 9)
1172 w[11] = r->w11;
1173 if (len >= 10)
1174 w[12] = r->w12;
1175 if (len >= 10)
1176 w[13] = r->w13;
1177 if (len >= 11)
1178 w[14] = r->w14;
1179 if (len >= 11)
1180 w[15] = r->w15;
1181 if (len >= 12)
1182 w[16] = r->w16;
1183 if (len >= 12)
1184 w[17] = r->w17;
1185 if (len >= 13)
1186 w[18] = (r->w18_hi << 1) | r->w18_lo;
1187 if (len >= 13)
1188 w[19] = r->w19;
1189 if (len >= 13)
1190 w[20] = r->w20;
1191 if (len >= 14)
1192 w[21] = (r->w21_hi << 2) | r->w21_lo;
1193 if (len >= 14)
1194 w[22] = r->w22;
1195 if (len >= 14)
1196 w[23] = r->w23;
1197 if (len >= 15)
1198 w[24] = r->w24;
1199 if (len >= 15)
1200 w[25] = r->w25;
1201 if (len >= 16)
1202 w[26] = (r->w26_hi << 1) | r->w26_lo;
1203 if (len >= 16)
1204 w[27] = r->w27;
1205 if (len >= 16)
1206 w[28] = r->w28;
1207 f[w[0]].mask |= frqt;
1208 if (w[1])
1209 f[(w[0] + w[1]) % 1024].mask |= frqt;
1210 if (w[2])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001211 f[(w[0] + smod(w[1] - 64 + w[2], 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001212 if (w[3])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001213 f[(w[0] + smod(w[1] + w[3], 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001214 if (w[4])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001215 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 +02001216 if (w[5])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001217 f[(w[0] + smod(w[1] + smod(w[3] - 32 + w[5], 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001218 if (w[6])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001219 f[(w[0] + smod(w[1] - 64 + smod(w[2] + w[6], 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001220 if (w[7])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001221 f[(w[0] + smod(w[1] + smod(w[3] + w[7], 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001222 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001223 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 +02001224 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001225 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 +02001226 if (w[10])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001227 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 +02001228 if (w[11])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001229 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 +02001230 if (w[12])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001231 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 +02001232 if (w[13])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001233 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 +02001234 if (w[14])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001235 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 +02001236 if (w[15])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001237 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 +02001238 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001239 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 +02001240 if (w[17])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001241 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 +02001242 if (w[18])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001243 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 +02001244 if (w[19])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001245 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 +02001246 if (w[20])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001247 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 +02001248 if (w[21])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001249 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 +02001250 if (w[22])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001251 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 +02001252 if (w[23])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001253 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 +02001254 if (w[24])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001255 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 +02001256 if (w[25])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001257 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 +02001258 if (w[26])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001259 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 +02001260 if (w[27])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001261 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 +02001262 if (w[28])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001263 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 +02001264
1265 return 0;
1266 }
1267 /* 10..111. */
1268 if ((cd[0] & 0xce & mask) == 0x8e) {
1269 /* Variable bitmap format (can be any length >= 3) */
1270 uint16_t orig = 0;
1271 struct gsm48_var_bit *r = (struct gsm48_var_bit *)cd;
1272
1273 if (len < 3)
1274 return -EINVAL;
1275 orig = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
1276 f[orig].mask |= frqt;
1277 for (i = 1; 2 + (i >> 3) < len; i++)
1278 if ((cd[2 + (i >> 3)] & (0x80 >> (i & 7))))
1279 f[(orig + i) % 1024].mask |= frqt;
1280
1281 return 0;
1282 }
1283
1284 return 0;
1285}
Harald Welte96e2a002017-06-12 21:44:18 +02001286/*! @} */