blob: 049f5dc67a0947d749213d37efa41bbd22667763 [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];
58 int i;
59
60 for (i = 1 + h_len; i <= in_len; i++) {
61 /* lower nibble */
62 output_len--;
63 if (output_len <= 1)
64 break;
65 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
66
67 /* higher nibble */
68 output_len--;
69 if (output_len <= 1)
70 break;
71 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
72 }
73 if (output_len >= 1)
74 *output++ = '\0';
75
76 return 0;
77}
78
Neels Hofmeyr8212fc62019-04-01 14:34:37 +020079/*! Decode a 'called/calling/connect party BCD number' as in 10.5.4.7.
80 * \param[out] output Caller-provided output buffer.
81 * \param[in] output_len sizeof(output).
82 * \param[in] bcd_lv Length-Value part of to-be-decoded IE.
83 * \param[in] input_len Size of the buffer to read the IE from.
84 * \param[in] h_len Length of an optional header between L and V parts.
85 * \return 0 in case of success, negative on error. Errors checked: no or too little input data, no or too little
86 * output buffer size, IE length exceeds input data size, decoded number exceeds size of the output buffer. The output
87 * is guaranteed to be nul terminated iff output_len > 0.
88 */
89int gsm48_decode_bcd_number2(char *output, size_t output_len,
90 const uint8_t *bcd_lv, size_t input_len,
91 size_t h_len)
92{
93 uint8_t len;
94 if (output_len < 1)
95 return -ENOSPC;
96 *output = '\0';
97 if (input_len < 1)
98 return -EIO;
99 len = bcd_lv[0];
100 if (input_len < len)
101 return -EIO;
102 return gsm48_decode_bcd_number(output, output_len, bcd_lv, h_len);
103}
104
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200105/*! convert a single ASCII character to call-control BCD */
Harald Welte1e908662010-03-07 23:39:54 +0100106static int asc_to_bcd(const char asc)
107{
108 int i;
109
110 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
111 if (bcd_num_digits[i] == asc)
112 return i;
113 }
114 return -EINVAL;
115}
116
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200117/*! convert a ASCII phone number to 'called/calling/connect party BCD number'
Harald Welte96e2a002017-06-12 21:44:18 +0200118 * \param[out] bcd_lv Caller-provided output buffer
119 * \param[in] max_len Maximum Length of \a bcd_lv
120 * \param[in] h_len Length of an optional heder between L and V portion
121 * \param[in] input phone number as 0-terminated ASCII
122 * \returns number of bytes used in \a bcd_lv */
Harald Welte1e908662010-03-07 23:39:54 +0100123int gsm48_encode_bcd_number(uint8_t *bcd_lv, uint8_t max_len,
124 int h_len, const char *input)
125{
126 int in_len = strlen(input);
127 int i;
128 uint8_t *bcd_cur = bcd_lv + 1 + h_len;
129
130 /* two digits per byte, plus type byte */
131 bcd_lv[0] = in_len/2 + h_len;
132 if (in_len % 2)
133 bcd_lv[0]++;
134
135 if (bcd_lv[0] > max_len)
136 return -EIO;
137
138 for (i = 0; i < in_len; i++) {
139 int rc = asc_to_bcd(input[i]);
140 if (rc < 0)
141 return rc;
142 if (i % 2 == 0)
143 *bcd_cur = rc;
144 else
145 *bcd_cur++ |= (rc << 4);
146 }
147 /* append padding nibble in case of odd length */
148 if (i % 2)
149 *bcd_cur++ |= 0xf0;
150
151 /* return how many bytes we used */
152 return (bcd_cur - bcd_lv);
153}
154
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200155/*! Decode TS 04.08 Bearer Capability IE (10.5.4.5)
Harald Welte96e2a002017-06-12 21:44:18 +0200156 * \param[out] Caller-provided memory for decoded output
157 * \[aram[in] LV portion of TS 04.08 Bearer Capability
158 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100159int gsm48_decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
160 const uint8_t *lv)
161{
162 uint8_t in_len = lv[0];
163 int i, s;
164
165 if (in_len < 1)
166 return -EINVAL;
167
168 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
169
170 /* octet 3 */
171 bcap->transfer = lv[1] & 0x07;
172 bcap->mode = (lv[1] & 0x08) >> 3;
173 bcap->coding = (lv[1] & 0x10) >> 4;
174 bcap->radio = (lv[1] & 0x60) >> 5;
175
Harald Weltec8a0b932012-08-24 21:27:26 +0200176 switch (bcap->transfer) {
177 case GSM_MNCC_BCAP_SPEECH:
Harald Welte1e908662010-03-07 23:39:54 +0100178 i = 1;
179 s = 0;
180 while(!(lv[i] & 0x80)) {
181 i++; /* octet 3a etc */
182 if (in_len < i)
183 return 0;
184 bcap->speech_ver[s++] = lv[i] & 0x0f;
185 bcap->speech_ver[s] = -1; /* end of list */
186 if (i == 2) /* octet 3a */
187 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
188 if (s == 7) /* maximum speech versions + end of list */
189 return 0;
190 }
Harald Weltec8a0b932012-08-24 21:27:26 +0200191 break;
192 case GSM_MNCC_BCAP_UNR_DIG:
193 case GSM_MNCC_BCAP_FAX_G3:
194 i = 1;
195 while(!(lv[i] & 0x80)) {
196 i++; /* octet 3a etc */
197 if (in_len < i)
198 return 0;
199 /* ignore them */
200 }
201 /* octet 4: skip */
202 i++;
203 /* octet 5 */
204 i++;
205 if (in_len < i)
206 return 0;
207 bcap->data.rate_adaption = (lv[i] >> 3) & 3;
208 bcap->data.sig_access = lv[i] & 7;
209 while(!(lv[i] & 0x80)) {
210 i++; /* octet 5a etc */
211 if (in_len < i)
212 return 0;
213 /* ignore them */
214 }
215 /* octet 6 */
216 i++;
217 if (in_len < i)
218 return 0;
219 bcap->data.async = lv[i] & 1;
220 if (!(lv[i] & 0x80)) {
221 i++;
222 if (in_len < i)
223 return 0;
224 /* octet 6a */
225 bcap->data.nr_stop_bits = ((lv[i] >> 7) & 1) + 1;
226 if (lv[i] & 0x10)
227 bcap->data.nr_data_bits = 8;
228 else
229 bcap->data.nr_data_bits = 7;
230 bcap->data.user_rate = lv[i] & 0xf;
231
232 if (!(lv[i] & 0x80)) {
233 i++;
234 if (in_len < i)
235 return 0;
236 /* octet 6b */
237 bcap->data.parity = lv[i] & 7;
238 bcap->data.interm_rate = (lv[i] >> 5) & 3;
239
240 /* octet 6c */
241 if (!(lv[i] & 0x80)) {
242 i++;
243 if (in_len < i)
244 return 0;
245 bcap->data.transp = (lv[i] >> 5) & 3;
246 bcap->data.modem_type = lv[i] & 0x1F;
247 }
248 }
249
250 }
251 break;
252 default:
Harald Welte1e908662010-03-07 23:39:54 +0100253 i = 1;
254 while (!(lv[i] & 0x80)) {
255 i++; /* octet 3a etc */
256 if (in_len < i)
257 return 0;
258 /* ignore them */
259 }
260 /* FIXME: implement OCTET 4+ parsing */
Harald Weltec8a0b932012-08-24 21:27:26 +0200261 break;
Harald Welte1e908662010-03-07 23:39:54 +0100262 }
263
264 return 0;
265}
266
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200267/*! Encode TS 04.08 Bearer Capability IE (10.5.4.5)
Harald Welte96e2a002017-06-12 21:44:18 +0200268 * \param[out] msg Message Buffer to which IE is to be appended
269 * \param[in] lv_only Write only LV portion (1) or TLV (0)
270 * \param[in] bcap Decoded Bearer Capability to be encoded
271 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100272int gsm48_encode_bearer_cap(struct msgb *msg, int lv_only,
273 const struct gsm_mncc_bearer_cap *bcap)
274{
275 uint8_t lv[32 + 1];
276 int i = 1, s;
277
278 lv[1] = bcap->transfer;
279 lv[1] |= bcap->mode << 3;
280 lv[1] |= bcap->coding << 4;
281 lv[1] |= bcap->radio << 5;
282
Harald Weltec8a0b932012-08-24 21:27:26 +0200283 switch (bcap->transfer) {
284 case GSM_MNCC_BCAP_SPEECH:
Harald Welte1e908662010-03-07 23:39:54 +0100285 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
286 i++; /* octet 3a etc */
287 lv[i] = bcap->speech_ver[s];
288 if (i == 2) /* octet 3a */
289 lv[i] |= bcap->speech_ctm << 5;
290 }
291 lv[i] |= 0x80; /* last IE of octet 3 etc */
Harald Weltec8a0b932012-08-24 21:27:26 +0200292 break;
293 case GSM48_BCAP_ITCAP_UNR_DIG_INF:
294 case GSM48_BCAP_ITCAP_FAX_G3:
295 lv[i++] |= 0x80; /* last IE of octet 3 etc */
296 /* octet 4 */
297 lv[i++] = 0xb8;
298 /* octet 5 */
299 lv[i++] = 0x80 | ((bcap->data.rate_adaption & 3) << 3)
300 | (bcap->data.sig_access & 7);
301 /* octet 6 */
302 lv[i++] = 0x20 | (bcap->data.async & 1);
303 /* octet 6a */
304 lv[i++] = (bcap->data.user_rate & 0xf) |
305 (bcap->data.nr_data_bits == 8 ? 0x10 : 0x00) |
306 (bcap->data.nr_stop_bits == 2 ? 0x40 : 0x00);
307 /* octet 6b */
308 lv[i++] = (bcap->data.parity & 7) |
309 ((bcap->data.interm_rate & 3) << 5);
310 /* octet 6c */
311 lv[i] = 0x80 | (bcap->data.modem_type & 0x1f);
312 break;
313 default:
314 return -EINVAL;
Harald Welte1e908662010-03-07 23:39:54 +0100315 }
316
317 lv[0] = i;
318 if (lv_only)
319 msgb_lv_put(msg, lv[0], lv+1);
320 else
321 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
322
323 return 0;
324}
325
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200326/*! Decode TS 04.08 Call Control Capabilities IE (10.5.4.5a)
Harald Welte96e2a002017-06-12 21:44:18 +0200327 * \param[out] Caller-provided memory for decoded CC capabilities
328 * \param[in] lv Length-Value of IE
329 * \retursns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100330int gsm48_decode_cccap(struct gsm_mncc_cccap *ccap, const uint8_t *lv)
331{
332 uint8_t in_len = lv[0];
333
334 if (in_len < 1)
335 return -EINVAL;
336
337 /* octet 3 */
338 ccap->dtmf = lv[1] & 0x01;
339 ccap->pcp = (lv[1] & 0x02) >> 1;
340
341 return 0;
342}
343
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200344/*! Encodoe TS 04.08 Call Control Capabilities (10.5.4.5a)
Harald Welte96e2a002017-06-12 21:44:18 +0200345 * \param[out] msg Message Buffer to which to append IE (as TLV)
346 * \param[in] ccap Decoded CC Capabilities to be encoded
347 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100348int gsm48_encode_cccap(struct msgb *msg,
349 const struct gsm_mncc_cccap *ccap)
350{
351 uint8_t lv[2];
352
353 lv[0] = 1;
354 lv[1] = 0;
355 if (ccap->dtmf)
356 lv [1] |= 0x01;
357 if (ccap->pcp)
358 lv [1] |= 0x02;
359
360 msgb_tlv_put(msg, GSM48_IE_CC_CAP, lv[0], lv+1);
361
362 return 0;
363}
364
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200365/*! Decode TS 04.08 Called Party BCD Number IE (10.5.4.7)
Harald Welte96e2a002017-06-12 21:44:18 +0200366 * \param[out] called Caller-provided memory for decoded number
367 * \param[in] lv Length-Value portion of IE
368 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100369int gsm48_decode_called(struct gsm_mncc_number *called,
370 const uint8_t *lv)
371{
372 uint8_t in_len = lv[0];
373
374 if (in_len < 1)
375 return -EINVAL;
376
377 /* octet 3 */
378 called->plan = lv[1] & 0x0f;
379 called->type = (lv[1] & 0x70) >> 4;
380
381 /* octet 4..N */
382 gsm48_decode_bcd_number(called->number, sizeof(called->number), lv, 1);
383
384 return 0;
385}
386
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200387/*! Encode TS 04.08 Called Party IE (10.5.4.7)
Harald Welte96e2a002017-06-12 21:44:18 +0200388 * \param[out] msg Mesage Buffer to which to append IE (as TLV)
389 * \param[in] called MNCC Number to encode/append
390 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100391int gsm48_encode_called(struct msgb *msg,
392 const struct gsm_mncc_number *called)
393{
394 uint8_t lv[18];
395 int ret;
396
397 /* octet 3 */
Sylvain Munaut47ee6932010-09-20 20:59:23 +0200398 lv[1] = 0x80; /* no extension */
399 lv[1] |= called->plan;
Harald Welte1e908662010-03-07 23:39:54 +0100400 lv[1] |= called->type << 4;
401
402 /* octet 4..N, octet 2 */
403 ret = gsm48_encode_bcd_number(lv, sizeof(lv), 1, called->number);
404 if (ret < 0)
405 return ret;
406
407 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
408
409 return 0;
410}
411
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200412/*! Decode TS 04.08 Caller ID
Harald Welte96e2a002017-06-12 21:44:18 +0200413 * \param[out] called Caller-provided memory for decoded number
414 * \param[in] lv Length-Value portion of IE
415 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100416int gsm48_decode_callerid(struct gsm_mncc_number *callerid,
417 const uint8_t *lv)
418{
419 uint8_t in_len = lv[0];
420 int i = 1;
421
422 if (in_len < 1)
423 return -EINVAL;
424
425 /* octet 3 */
426 callerid->plan = lv[1] & 0x0f;
427 callerid->type = (lv[1] & 0x70) >> 4;
428
429 /* octet 3a */
430 if (!(lv[1] & 0x80)) {
431 callerid->screen = lv[2] & 0x03;
432 callerid->present = (lv[2] & 0x60) >> 5;
433 i = 2;
434 }
435
436 /* octet 4..N */
437 gsm48_decode_bcd_number(callerid->number, sizeof(callerid->number), lv, i);
438
439 return 0;
440}
441
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200442/*! Encode TS 04.08 Caller ID IE
Harald Welte96e2a002017-06-12 21:44:18 +0200443 * \param[out] msg Mesage Buffer to which to append IE (as TLV)
444 * \param[in] ie IE Identifier (tag)
445 * \param[in] max_len maximum generated output in bytes
446 * \param[in] callerid MNCC Number to encode/append
447 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100448int gsm48_encode_callerid(struct msgb *msg, int ie, int max_len,
449 const struct gsm_mncc_number *callerid)
450{
451 uint8_t lv[max_len - 1];
452 int h_len = 1;
453 int ret;
454
455 /* octet 3 */
456 lv[1] = callerid->plan;
457 lv[1] |= callerid->type << 4;
458
459 if (callerid->present || callerid->screen) {
460 /* octet 3a */
461 lv[2] = callerid->screen;
462 lv[2] |= callerid->present << 5;
463 lv[2] |= 0x80;
464 h_len++;
465 } else
466 lv[1] |= 0x80;
467
468 /* octet 4..N, octet 2 */
469 ret = gsm48_encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
470 if (ret < 0)
471 return ret;
472
473 msgb_tlv_put(msg, ie, lv[0], lv+1);
474
475 return 0;
476}
477
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200478/*! Decode TS 04.08 Cause IE (10.5.4.11)
Harald Welte96e2a002017-06-12 21:44:18 +0200479 * \param[out] cause Caller-provided memory for output
480 * \param[in] lv LV portion of Cause IE
481 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100482int gsm48_decode_cause(struct gsm_mncc_cause *cause,
483 const uint8_t *lv)
484{
485 uint8_t in_len = lv[0];
486 int i;
487
488 if (in_len < 2)
489 return -EINVAL;
490
491 cause->diag_len = 0;
492
493 /* octet 3 */
494 cause->location = lv[1] & 0x0f;
495 cause->coding = (lv[1] & 0x60) >> 5;
496
497 i = 1;
498 if (!(lv[i] & 0x80)) {
499 i++; /* octet 3a */
500 if (in_len < i+1)
501 return 0;
502 cause->rec = 1;
503 cause->rec_val = lv[i] & 0x7f;
504 }
505 i++;
506
507 /* octet 4 */
508 cause->value = lv[i] & 0x7f;
509 i++;
510
511 if (in_len < i) /* no diag */
512 return 0;
513
514 if (in_len - (i-1) > 32) /* maximum 32 octets */
515 return 0;
516
517 /* octet 5-N */
518 memcpy(cause->diag, lv + i, in_len - (i-1));
519 cause->diag_len = in_len - (i-1);
520
521 return 0;
522}
523
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200524/*! Encode TS 04.08 Cause IE (10.5.4.11)
Harald Welte96e2a002017-06-12 21:44:18 +0200525 * \param[out] msg Message Buffer to which to append IE
526 * \param[in] lv_only Encode as LV (1) or TLV (0)
527 * \param[in] cause Cause value to be encoded
528 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100529int gsm48_encode_cause(struct msgb *msg, int lv_only,
530 const struct gsm_mncc_cause *cause)
531{
532 uint8_t lv[32+4];
533 int i;
534
535 if (cause->diag_len > 32)
536 return -EINVAL;
537
538 /* octet 3 */
539 lv[1] = cause->location;
540 lv[1] |= cause->coding << 5;
541
542 i = 1;
543 if (cause->rec) {
544 i++; /* octet 3a */
545 lv[i] = cause->rec_val;
546 }
547 lv[i] |= 0x80; /* end of octet 3 */
548
549 /* octet 4 */
550 i++;
551 lv[i] = 0x80 | cause->value;
552
553 /* octet 5-N */
554 if (cause->diag_len) {
555 memcpy(lv + i, cause->diag, cause->diag_len);
556 i += cause->diag_len;
557 }
558
559 lv[0] = i;
560 if (lv_only)
561 msgb_lv_put(msg, lv[0], lv+1);
562 else
563 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
564
565 return 0;
566}
567
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200568/*! Decode TS 04.08 Calling Number IE (10.5.4.9) */
Harald Welte1e908662010-03-07 23:39:54 +0100569int gsm48_decode_calling(struct gsm_mncc_number *calling,
570 const uint8_t *lv)
571{
572 return gsm48_decode_callerid(calling, lv);
573}
574
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200575/*! Encode TS 04.08 Calling Number IE (10.5.4.9) */
Harald Welte1e908662010-03-07 23:39:54 +0100576int gsm48_encode_calling(struct msgb *msg,
577 const struct gsm_mncc_number *calling)
578{
579 return gsm48_encode_callerid(msg, GSM48_IE_CALLING_BCD, 14, calling);
580}
581
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200582/*! Decode TS 04.08 Connected Number IE (10.5.4.13) */
Harald Welte1e908662010-03-07 23:39:54 +0100583int gsm48_decode_connected(struct gsm_mncc_number *connected,
584 const uint8_t *lv)
585{
586 return gsm48_decode_callerid(connected, lv);
587}
588
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200589/*! Encode TS 04.08 Connected Number IE (10.5.4.13) */
Harald Welte1e908662010-03-07 23:39:54 +0100590int gsm48_encode_connected(struct msgb *msg,
591 const struct gsm_mncc_number *connected)
592{
593 return gsm48_encode_callerid(msg, GSM48_IE_CONN_BCD, 14, connected);
594}
595
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200596/*! Decode TS 04.08 Redirecting Number IE (10.5.4.21b) */
Harald Welte1e908662010-03-07 23:39:54 +0100597int gsm48_decode_redirecting(struct gsm_mncc_number *redirecting,
598 const uint8_t *lv)
599{
600 return gsm48_decode_callerid(redirecting, lv);
601}
602
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200603/*! Encode TS 04.08 Redirecting Number IE (10.5.4.21b) */
Harald Welte1e908662010-03-07 23:39:54 +0100604int gsm48_encode_redirecting(struct msgb *msg,
605 const struct gsm_mncc_number *redirecting)
606{
607 return gsm48_encode_callerid(msg, GSM48_IE_REDIR_BCD, 19, redirecting);
608}
609
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200610/*! Decode TS 04.08 Facility IE (10.5.4.15) */
Harald Welte1e908662010-03-07 23:39:54 +0100611int gsm48_decode_facility(struct gsm_mncc_facility *facility,
612 const uint8_t *lv)
613{
614 uint8_t in_len = lv[0];
615
616 if (in_len < 1)
617 return -EINVAL;
618
619 if (in_len > sizeof(facility->info))
620 return -EINVAL;
621
622 memcpy(facility->info, lv+1, in_len);
623 facility->len = in_len;
624
625 return 0;
626}
627
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200628/*! Encode TS 04.08 Facility IE (10.5.4.15) */
Harald Welte1e908662010-03-07 23:39:54 +0100629int gsm48_encode_facility(struct msgb *msg, int lv_only,
630 const struct gsm_mncc_facility *facility)
631{
632 uint8_t lv[GSM_MAX_FACILITY + 1];
633
634 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
635 return -EINVAL;
636
637 memcpy(lv+1, facility->info, facility->len);
638 lv[0] = facility->len;
639 if (lv_only)
640 msgb_lv_put(msg, lv[0], lv+1);
641 else
642 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
643
644 return 0;
645}
646
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200647/*! Decode TS 04.08 Notify IE (10.5.4.20) */
Harald Welte1e908662010-03-07 23:39:54 +0100648int gsm48_decode_notify(int *notify, const uint8_t *v)
649{
650 *notify = v[0] & 0x7f;
651
652 return 0;
653}
654
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200655/*! Encode TS 04.08 Notify IE (10.5.4.20) */
Harald Welte1e908662010-03-07 23:39:54 +0100656int gsm48_encode_notify(struct msgb *msg, int notify)
657{
658 msgb_v_put(msg, notify | 0x80);
659
660 return 0;
661}
662
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200663/*! Decode TS 04.08 Signal IE (10.5.4.23) */
Harald Welte1e908662010-03-07 23:39:54 +0100664int gsm48_decode_signal(int *signal, const uint8_t *v)
665{
666 *signal = v[0];
667
668 return 0;
669}
670
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200671/*! Encode TS 04.08 Signal IE (10.5.4.23) */
Harald Welte1e908662010-03-07 23:39:54 +0100672int gsm48_encode_signal(struct msgb *msg, int signal)
673{
674 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
675
676 return 0;
677}
678
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200679/*! Decode TS 04.08 Keypad IE (10.5.4.17) */
Harald Welte1e908662010-03-07 23:39:54 +0100680int gsm48_decode_keypad(int *keypad, const uint8_t *lv)
681{
682 uint8_t in_len = lv[0];
683
684 if (in_len < 1)
685 return -EINVAL;
686
687 *keypad = lv[1] & 0x7f;
688
689 return 0;
690}
691
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200692/*! Encode TS 04.08 Keypad IE (10.5.4.17) */
Harald Welte1e908662010-03-07 23:39:54 +0100693int gsm48_encode_keypad(struct msgb *msg, int keypad)
694{
695 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
696
697 return 0;
698}
699
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200700/*! Decode TS 04.08 Progress IE (10.5.4.21) */
Harald Welte1e908662010-03-07 23:39:54 +0100701int gsm48_decode_progress(struct gsm_mncc_progress *progress,
702 const uint8_t *lv)
703{
704 uint8_t in_len = lv[0];
705
706 if (in_len < 2)
707 return -EINVAL;
708
709 progress->coding = (lv[1] & 0x60) >> 5;
710 progress->location = lv[1] & 0x0f;
711 progress->descr = lv[2] & 0x7f;
712
713 return 0;
714}
715
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200716/*! Encode TS 04.08 Progress IE (10.5.4.21) */
Harald Welte1e908662010-03-07 23:39:54 +0100717int gsm48_encode_progress(struct msgb *msg, int lv_only,
718 const struct gsm_mncc_progress *p)
719{
720 uint8_t lv[3];
721
722 lv[0] = 2;
723 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
724 lv[2] = 0x80 | (p->descr & 0x7f);
725 if (lv_only)
726 msgb_lv_put(msg, lv[0], lv+1);
727 else
728 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
729
730 return 0;
731}
732
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200733/*! Decode TS 04.08 User-User IE (10.5.4.25) */
Harald Welte1e908662010-03-07 23:39:54 +0100734int gsm48_decode_useruser(struct gsm_mncc_useruser *uu,
735 const uint8_t *lv)
736{
737 uint8_t in_len = lv[0];
738 char *info = uu->info;
739 int info_len = sizeof(uu->info);
740 int i;
741
742 if (in_len < 1)
743 return -EINVAL;
744
745 uu->proto = lv[1];
746
747 for (i = 2; i <= in_len; i++) {
748 info_len--;
749 if (info_len <= 1)
750 break;
751 *info++ = lv[i];
752 }
753 if (info_len >= 1)
754 *info++ = '\0';
755
756 return 0;
757}
758
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200759/*! Encode TS 04.08 User-User IE (10.5.4.25) */
Harald Welte1e908662010-03-07 23:39:54 +0100760int gsm48_encode_useruser(struct msgb *msg, int lv_only,
761 const struct gsm_mncc_useruser *uu)
762{
763 uint8_t lv[GSM_MAX_USERUSER + 2];
764
765 if (strlen(uu->info) > GSM_MAX_USERUSER)
766 return -EINVAL;
767
768 lv[0] = 1 + strlen(uu->info);
769 lv[1] = uu->proto;
770 memcpy(lv + 2, uu->info, strlen(uu->info));
771 if (lv_only)
772 msgb_lv_put(msg, lv[0], lv+1);
773 else
774 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
775
776 return 0;
777}
778
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200779/*! Decode TS 04.08 SS Version IE (10.5.4.24) */
Harald Welte1e908662010-03-07 23:39:54 +0100780int gsm48_decode_ssversion(struct gsm_mncc_ssversion *ssv,
781 const uint8_t *lv)
782{
783 uint8_t in_len = lv[0];
784
785 if (in_len < 1 || in_len < sizeof(ssv->info))
786 return -EINVAL;
787
788 memcpy(ssv->info, lv + 1, in_len);
789 ssv->len = in_len;
790
791 return 0;
792}
793
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200794/*! Encode TS 04.08 SS Version IE (10.5.4.24) */
Harald Welte1e908662010-03-07 23:39:54 +0100795int gsm48_encode_ssversion(struct msgb *msg,
796 const struct gsm_mncc_ssversion *ssv)
797{
798 uint8_t lv[GSM_MAX_SSVERSION + 1];
799
800 if (ssv->len > GSM_MAX_SSVERSION)
801 return -EINVAL;
802
803 lv[0] = ssv->len;
804 memcpy(lv + 1, ssv->info, ssv->len);
805 msgb_tlv_put(msg, GSM48_IE_SS_VERS, lv[0], lv+1);
806
807 return 0;
808}
809
810/* decode 'more data' does not require a function, because it has no value */
811
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200812/*! Encode TS 04.08 More Data IE (10.5.4.19) */
Harald Welte1e908662010-03-07 23:39:54 +0100813int gsm48_encode_more(struct msgb *msg)
814{
815 uint8_t *ie;
816
817 ie = msgb_put(msg, 1);
818 ie[0] = GSM48_IE_MORE_DATA;
819
820 return 0;
821}
822
Sylvain Munaut71fd42f2011-09-01 22:05:29 +0200823static int32_t smod(int32_t n, int32_t m)
824{
825 int32_t res;
826
827 res = n % m;
828
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200829 if (res <= 0)
Sylvain Munaut71fd42f2011-09-01 22:05:29 +0200830 res += m;
831
Sylvain Munaut71fd42f2011-09-01 22:05:29 +0200832 return res;
833}
834
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200835/*! Decode TS 04.08 Cell Channel Description IE (10.5.2.1b) and other frequency lists
Pau Espin Pedrold8d0c3e2018-11-15 18:26:01 +0100836 * \param[out] f Caller-provided output memory, an array of 1024 elements
Harald Welte96e2a002017-06-12 21:44:18 +0200837 * \param[in] cd Cell Channel Description IE
838 * \param[in] len Length of \a cd in bytes
839 * \returns 0 on success; negative on error */
Harald Welte1523d702010-08-04 11:46:44 +0200840int gsm48_decode_freq_list(struct gsm_sysinfo_freq *f, uint8_t *cd,
841 uint8_t len, uint8_t mask, uint8_t frqt)
842{
843 int i;
844
845 /* NOTES:
846 *
847 * The Range format uses "SMOD" computation.
848 * e.g. "n SMOD m" equals "((n - 1) % m) + 1"
849 * A cascade of multiple SMOD computations is simpified:
850 * "(n SMOD m) SMOD o" equals "(((n - 1) % m) % o) + 1"
851 *
852 * The Range format uses 16 octets of data in SYSTEM INFORMATION.
853 * When used in dedicated messages, the length can be less.
854 * In this case the ranges are decoded for all frequencies that
855 * fit in the block of given length.
856 */
857
858 /* tabula rasa */
859 for (i = 0; i < 1024; i++)
860 f[i].mask &= ~frqt;
861
862 /* 00..XXX. */
863 if ((cd[0] & 0xc0 & mask) == 0x00) {
864 /* Bit map 0 format */
865 if (len < 16)
866 return -EINVAL;
867 for (i = 1; i <= 124; i++)
868 if ((cd[15 - ((i-1) >> 3)] & (1 << ((i-1) & 7))))
869 f[i].mask |= frqt;
870
871 return 0;
872 }
873
874 /* 10..0XX. */
875 if ((cd[0] & 0xc8 & mask) == 0x80) {
876 /* Range 1024 format */
877 uint16_t w[17]; /* 1..16 */
878 struct gsm48_range_1024 *r = (struct gsm48_range_1024 *)cd;
879
880 if (len < 2)
881 return -EINVAL;
882 memset(w, 0, sizeof(w));
883 if (r->f0)
884 f[0].mask |= frqt;
885 w[1] = (r->w1_hi << 8) | r->w1_lo;
886 if (len >= 4)
887 w[2] = (r->w2_hi << 1) | r->w2_lo;
888 if (len >= 5)
889 w[3] = (r->w3_hi << 2) | r->w3_lo;
890 if (len >= 6)
891 w[4] = (r->w4_hi << 2) | r->w4_lo;
892 if (len >= 7)
893 w[5] = (r->w5_hi << 2) | r->w5_lo;
894 if (len >= 8)
895 w[6] = (r->w6_hi << 2) | r->w6_lo;
896 if (len >= 9)
897 w[7] = (r->w7_hi << 2) | r->w7_lo;
898 if (len >= 10)
899 w[8] = (r->w8_hi << 1) | r->w8_lo;
900 if (len >= 10)
901 w[9] = r->w9;
902 if (len >= 11)
903 w[10] = r->w10;
904 if (len >= 12)
905 w[11] = (r->w11_hi << 6) | r->w11_lo;
906 if (len >= 13)
907 w[12] = (r->w12_hi << 5) | r->w12_lo;
908 if (len >= 14)
909 w[13] = (r->w13_hi << 4) | r->w13_lo;
910 if (len >= 15)
911 w[14] = (r->w14_hi << 3) | r->w14_lo;
912 if (len >= 16)
913 w[15] = (r->w15_hi << 2) | r->w15_lo;
914 if (len >= 16)
915 w[16] = r->w16;
916 if (w[1])
917 f[w[1]].mask |= frqt;
918 if (w[2])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200919 f[smod(w[1] - 512 + w[2], 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200920 if (w[3])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200921 f[smod(w[1] + w[3], 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200922 if (w[4])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200923 f[smod(w[1] - 512 + smod(w[2] - 256 + w[4], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200924 if (w[5])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200925 f[smod(w[1] + smod(w[3] - 256 + w[5], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200926 if (w[6])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200927 f[smod(w[1] - 512 + smod(w[2] + w[6], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200928 if (w[7])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200929 f[smod(w[1] + smod(w[3] + w[7], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200930 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200931 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 +0200932 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200933 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 +0200934 if (w[10])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200935 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 +0200936 if (w[11])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200937 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 +0200938 if (w[12])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200939 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 +0200940 if (w[13])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200941 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 +0200942 if (w[14])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200943 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 +0200944 if (w[15])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200945 f[smod(w[1] + smod(w[3] + smod(w[7] + w[15], 255), 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200946 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200947 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 +0200948
949 return 0;
950 }
951 /* 10..100. */
952 if ((cd[0] & 0xce & mask) == 0x88) {
953 /* Range 512 format */
954 uint16_t w[18]; /* 1..17 */
955 struct gsm48_range_512 *r = (struct gsm48_range_512 *)cd;
956
957 if (len < 4)
958 return -EINVAL;
959 memset(w, 0, sizeof(w));
960 w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
961 w[1] = (r->w1_hi << 2) | r->w1_lo;
962 if (len >= 5)
963 w[2] = (r->w2_hi << 2) | r->w2_lo;
964 if (len >= 6)
965 w[3] = (r->w3_hi << 2) | r->w3_lo;
966 if (len >= 7)
967 w[4] = (r->w4_hi << 1) | r->w4_lo;
968 if (len >= 7)
969 w[5] = r->w5;
970 if (len >= 8)
971 w[6] = r->w6;
972 if (len >= 9)
973 w[7] = (r->w7_hi << 6) | r->w7_lo;
974 if (len >= 10)
975 w[8] = (r->w8_hi << 4) | r->w8_lo;
976 if (len >= 11)
977 w[9] = (r->w9_hi << 2) | r->w9_lo;
978 if (len >= 11)
979 w[10] = r->w10;
980 if (len >= 12)
981 w[11] = r->w11;
982 if (len >= 13)
983 w[12] = (r->w12_hi << 4) | r->w12_lo;
984 if (len >= 14)
985 w[13] = (r->w13_hi << 2) | r->w13_lo;
986 if (len >= 14)
987 w[14] = r->w14;
988 if (len >= 15)
989 w[15] = r->w15;
990 if (len >= 16)
991 w[16] = (r->w16_hi << 3) | r->w16_lo;
992 if (len >= 16)
993 w[17] = r->w17;
994 f[w[0]].mask |= frqt;
995 if (w[1])
996 f[(w[0] + w[1]) % 1024].mask |= frqt;
997 if (w[2])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200998 f[(w[0] + smod(w[1] - 256 + w[2], 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200999 if (w[3])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001000 f[(w[0] + smod(w[1] + w[3], 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001001 if (w[4])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001002 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 +02001003 if (w[5])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001004 f[(w[0] + smod(w[1] + smod(w[3] - 128 + w[5], 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001005 if (w[6])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001006 f[(w[0] + smod(w[1] - 256 + smod(w[2] + w[6], 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001007 if (w[7])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001008 f[(w[0] + smod(w[1] + smod(w[3] + w[7], 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001009 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001010 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 +02001011 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001012 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 +02001013 if (w[10])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001014 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 +02001015 if (w[11])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001016 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 +02001017 if (w[12])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001018 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 +02001019 if (w[13])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001020 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 +02001021 if (w[14])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001022 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 +02001023 if (w[15])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001024 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 +02001025 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001026 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 +02001027 if (w[17])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001028 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 +02001029
1030 return 0;
1031 }
1032 /* 10..101. */
1033 if ((cd[0] & 0xce & mask) == 0x8a) {
1034 /* Range 256 format */
1035 uint16_t w[22]; /* 1..21 */
1036 struct gsm48_range_256 *r = (struct gsm48_range_256 *)cd;
1037
1038 if (len < 4)
1039 return -EINVAL;
1040 memset(w, 0, sizeof(w));
1041 w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
1042 w[1] = (r->w1_hi << 1) | r->w1_lo;
1043 if (len >= 4)
1044 w[2] = r->w2;
1045 if (len >= 5)
1046 w[3] = r->w3;
1047 if (len >= 6)
1048 w[4] = (r->w4_hi << 5) | r->w4_lo;
1049 if (len >= 7)
1050 w[5] = (r->w5_hi << 3) | r->w5_lo;
1051 if (len >= 8)
1052 w[6] = (r->w6_hi << 1) | r->w6_lo;
1053 if (len >= 8)
1054 w[7] = r->w7;
1055 if (len >= 9)
1056 w[8] = (r->w8_hi << 4) | r->w8_lo;
1057 if (len >= 10)
1058 w[9] = (r->w9_hi << 1) | r->w9_lo;
1059 if (len >= 10)
1060 w[10] = r->w10;
1061 if (len >= 11)
1062 w[11] = (r->w11_hi << 3) | r->w11_lo;
1063 if (len >= 11)
1064 w[12] = r->w12;
1065 if (len >= 12)
1066 w[13] = r->w13;
1067 if (len >= 13)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001068 w[14] = (r->w14_hi << 2) | r->w14_lo;
Harald Welte1523d702010-08-04 11:46:44 +02001069 if (len >= 13)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001070 w[15] = r->w15;
Harald Welte1523d702010-08-04 11:46:44 +02001071 if (len >= 14)
1072 w[16] = (r->w16_hi << 3) | r->w16_lo;
1073 if (len >= 14)
1074 w[17] = r->w17;
1075 if (len >= 15)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001076 w[18] = (r->w18_hi << 3) | r->w18_lo;
Harald Welte1523d702010-08-04 11:46:44 +02001077 if (len >= 15)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001078 w[19] = r->w19;
Harald Welte1523d702010-08-04 11:46:44 +02001079 if (len >= 16)
1080 w[20] = (r->w20_hi << 3) | r->w20_lo;
1081 if (len >= 16)
1082 w[21] = r->w21;
1083 f[w[0]].mask |= frqt;
1084 if (w[1])
1085 f[(w[0] + w[1]) % 1024].mask |= frqt;
1086 if (w[2])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001087 f[(w[0] + smod(w[1] - 128 + w[2], 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001088 if (w[3])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001089 f[(w[0] + smod(w[1] + w[3], 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001090 if (w[4])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001091 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 +02001092 if (w[5])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001093 f[(w[0] + smod(w[1] + smod(w[3] - 64 + w[5], 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001094 if (w[6])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001095 f[(w[0] + smod(w[1] - 128 + smod(w[2] + w[6], 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001096 if (w[7])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001097 f[(w[0] + smod(w[1] + smod(w[3] + w[7], 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001098 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001099 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 +02001100 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001101 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 +02001102 if (w[10])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001103 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 +02001104 if (w[11])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001105 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 +02001106 if (w[12])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001107 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 +02001108 if (w[13])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001109 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 +02001110 if (w[14])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001111 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 +02001112 if (w[15])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001113 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 +02001114 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001115 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 +02001116 if (w[17])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001117 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 +02001118 if (w[18])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001119 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 +02001120 if (w[19])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001121 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 +02001122 if (w[20])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001123 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 +02001124 if (w[21])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001125 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 +02001126
1127 return 0;
1128 }
1129 /* 10..110. */
1130 if ((cd[0] & 0xce & mask) == 0x8c) {
1131 /* Range 128 format */
1132 uint16_t w[29]; /* 1..28 */
1133 struct gsm48_range_128 *r = (struct gsm48_range_128 *)cd;
1134
1135 if (len < 3)
1136 return -EINVAL;
1137 memset(w, 0, sizeof(w));
1138 w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
1139 w[1] = r->w1;
1140 if (len >= 4)
1141 w[2] = r->w2;
1142 if (len >= 5)
1143 w[3] = (r->w3_hi << 4) | r->w3_lo;
1144 if (len >= 6)
1145 w[4] = (r->w4_hi << 1) | r->w4_lo;
1146 if (len >= 6)
1147 w[5] = r->w5;
1148 if (len >= 7)
1149 w[6] = (r->w6_hi << 3) | r->w6_lo;
1150 if (len >= 7)
1151 w[7] = r->w7;
1152 if (len >= 8)
1153 w[8] = r->w8;
1154 if (len >= 8)
1155 w[9] = r->w9;
1156 if (len >= 9)
1157 w[10] = r->w10;
1158 if (len >= 9)
1159 w[11] = r->w11;
1160 if (len >= 10)
1161 w[12] = r->w12;
1162 if (len >= 10)
1163 w[13] = r->w13;
1164 if (len >= 11)
1165 w[14] = r->w14;
1166 if (len >= 11)
1167 w[15] = r->w15;
1168 if (len >= 12)
1169 w[16] = r->w16;
1170 if (len >= 12)
1171 w[17] = r->w17;
1172 if (len >= 13)
1173 w[18] = (r->w18_hi << 1) | r->w18_lo;
1174 if (len >= 13)
1175 w[19] = r->w19;
1176 if (len >= 13)
1177 w[20] = r->w20;
1178 if (len >= 14)
1179 w[21] = (r->w21_hi << 2) | r->w21_lo;
1180 if (len >= 14)
1181 w[22] = r->w22;
1182 if (len >= 14)
1183 w[23] = r->w23;
1184 if (len >= 15)
1185 w[24] = r->w24;
1186 if (len >= 15)
1187 w[25] = r->w25;
1188 if (len >= 16)
1189 w[26] = (r->w26_hi << 1) | r->w26_lo;
1190 if (len >= 16)
1191 w[27] = r->w27;
1192 if (len >= 16)
1193 w[28] = r->w28;
1194 f[w[0]].mask |= frqt;
1195 if (w[1])
1196 f[(w[0] + w[1]) % 1024].mask |= frqt;
1197 if (w[2])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001198 f[(w[0] + smod(w[1] - 64 + w[2], 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001199 if (w[3])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001200 f[(w[0] + smod(w[1] + w[3], 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001201 if (w[4])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001202 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 +02001203 if (w[5])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001204 f[(w[0] + smod(w[1] + smod(w[3] - 32 + w[5], 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001205 if (w[6])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001206 f[(w[0] + smod(w[1] - 64 + smod(w[2] + w[6], 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001207 if (w[7])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001208 f[(w[0] + smod(w[1] + smod(w[3] + w[7], 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001209 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001210 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 +02001211 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001212 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 +02001213 if (w[10])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001214 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 +02001215 if (w[11])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001216 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 +02001217 if (w[12])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001218 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 +02001219 if (w[13])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001220 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 +02001221 if (w[14])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001222 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 +02001223 if (w[15])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001224 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 +02001225 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001226 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 +02001227 if (w[17])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001228 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 +02001229 if (w[18])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001230 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 +02001231 if (w[19])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001232 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 +02001233 if (w[20])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001234 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 +02001235 if (w[21])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001236 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 +02001237 if (w[22])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001238 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 +02001239 if (w[23])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001240 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 +02001241 if (w[24])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001242 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 +02001243 if (w[25])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001244 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 +02001245 if (w[26])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001246 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 +02001247 if (w[27])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001248 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 +02001249 if (w[28])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001250 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 +02001251
1252 return 0;
1253 }
1254 /* 10..111. */
1255 if ((cd[0] & 0xce & mask) == 0x8e) {
1256 /* Variable bitmap format (can be any length >= 3) */
1257 uint16_t orig = 0;
1258 struct gsm48_var_bit *r = (struct gsm48_var_bit *)cd;
1259
1260 if (len < 3)
1261 return -EINVAL;
1262 orig = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
1263 f[orig].mask |= frqt;
1264 for (i = 1; 2 + (i >> 3) < len; i++)
1265 if ((cd[2 + (i >> 3)] & (0x80 >> (i & 7))))
1266 f[(orig + i) % 1024].mask |= frqt;
1267
1268 return 0;
1269 }
1270
1271 return 0;
1272}
Harald Welte96e2a002017-06-12 21:44:18 +02001273/*! @} */