blob: 105acbaf705a430f9b4c5d20b5b62da4b18e51e0 [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 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
24 */
25
26
27#include <stdint.h>
28#include <string.h>
29#include <errno.h>
30
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010031#include <osmocom/core/utils.h>
32#include <osmocom/core/msgb.h>
33#include <osmocom/gsm/tlv.h>
34#include <osmocom/gsm/mncc.h>
35#include <osmocom/gsm/protocol/gsm_04_08.h>
36#include <osmocom/gsm/gsm48_ie.h>
Harald Welte1e908662010-03-07 23:39:54 +010037
Harald Welte96e2a002017-06-12 21:44:18 +020038/*! \addtogroup gsm0408
39 * @{
40 */
41
Harald Welte1e908662010-03-07 23:39:54 +010042static const char bcd_num_digits[] = {
43 '0', '1', '2', '3', '4', '5', '6', '7',
44 '8', '9', '*', '#', 'a', 'b', 'c', '\0'
45};
46
Neels Hofmeyr87e45502017-06-20 00:17:59 +020047/*! decode a 'called/calling/connect party BCD number' as in 10.5.4.7
Harald Welte96e2a002017-06-12 21:44:18 +020048 * \param[out] Caller-provided output buffer
49 * \param[in] bcd_lv Length-Value portion of to-be-decoded IE
50 * \param[in] h_len Length of an optional heder between L and V portion
51 * \returns - in case of success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +010052int gsm48_decode_bcd_number(char *output, int output_len,
53 const uint8_t *bcd_lv, int h_len)
54{
55 uint8_t in_len = bcd_lv[0];
56 int i;
57
58 for (i = 1 + h_len; i <= in_len; i++) {
59 /* lower nibble */
60 output_len--;
61 if (output_len <= 1)
62 break;
63 *output++ = bcd_num_digits[bcd_lv[i] & 0xf];
64
65 /* higher nibble */
66 output_len--;
67 if (output_len <= 1)
68 break;
69 *output++ = bcd_num_digits[bcd_lv[i] >> 4];
70 }
71 if (output_len >= 1)
72 *output++ = '\0';
73
74 return 0;
75}
76
Neels Hofmeyr87e45502017-06-20 00:17:59 +020077/*! convert a single ASCII character to call-control BCD */
Harald Welte1e908662010-03-07 23:39:54 +010078static int asc_to_bcd(const char asc)
79{
80 int i;
81
82 for (i = 0; i < ARRAY_SIZE(bcd_num_digits); i++) {
83 if (bcd_num_digits[i] == asc)
84 return i;
85 }
86 return -EINVAL;
87}
88
Neels Hofmeyr87e45502017-06-20 00:17:59 +020089/*! convert a ASCII phone number to 'called/calling/connect party BCD number'
Harald Welte96e2a002017-06-12 21:44:18 +020090 * \param[out] bcd_lv Caller-provided output buffer
91 * \param[in] max_len Maximum Length of \a bcd_lv
92 * \param[in] h_len Length of an optional heder between L and V portion
93 * \param[in] input phone number as 0-terminated ASCII
94 * \returns number of bytes used in \a bcd_lv */
Harald Welte1e908662010-03-07 23:39:54 +010095int gsm48_encode_bcd_number(uint8_t *bcd_lv, uint8_t max_len,
96 int h_len, const char *input)
97{
98 int in_len = strlen(input);
99 int i;
100 uint8_t *bcd_cur = bcd_lv + 1 + h_len;
101
102 /* two digits per byte, plus type byte */
103 bcd_lv[0] = in_len/2 + h_len;
104 if (in_len % 2)
105 bcd_lv[0]++;
106
107 if (bcd_lv[0] > max_len)
108 return -EIO;
109
110 for (i = 0; i < in_len; i++) {
111 int rc = asc_to_bcd(input[i]);
112 if (rc < 0)
113 return rc;
114 if (i % 2 == 0)
115 *bcd_cur = rc;
116 else
117 *bcd_cur++ |= (rc << 4);
118 }
119 /* append padding nibble in case of odd length */
120 if (i % 2)
121 *bcd_cur++ |= 0xf0;
122
123 /* return how many bytes we used */
124 return (bcd_cur - bcd_lv);
125}
126
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200127/*! Decode TS 04.08 Bearer Capability IE (10.5.4.5)
Harald Welte96e2a002017-06-12 21:44:18 +0200128 * \param[out] Caller-provided memory for decoded output
129 * \[aram[in] LV portion of TS 04.08 Bearer Capability
130 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100131int gsm48_decode_bearer_cap(struct gsm_mncc_bearer_cap *bcap,
132 const uint8_t *lv)
133{
134 uint8_t in_len = lv[0];
135 int i, s;
136
137 if (in_len < 1)
138 return -EINVAL;
139
140 bcap->speech_ver[0] = -1; /* end of list, of maximum 7 values */
141
142 /* octet 3 */
143 bcap->transfer = lv[1] & 0x07;
144 bcap->mode = (lv[1] & 0x08) >> 3;
145 bcap->coding = (lv[1] & 0x10) >> 4;
146 bcap->radio = (lv[1] & 0x60) >> 5;
147
Harald Weltec8a0b932012-08-24 21:27:26 +0200148 switch (bcap->transfer) {
149 case GSM_MNCC_BCAP_SPEECH:
Harald Welte1e908662010-03-07 23:39:54 +0100150 i = 1;
151 s = 0;
152 while(!(lv[i] & 0x80)) {
153 i++; /* octet 3a etc */
154 if (in_len < i)
155 return 0;
156 bcap->speech_ver[s++] = lv[i] & 0x0f;
157 bcap->speech_ver[s] = -1; /* end of list */
158 if (i == 2) /* octet 3a */
159 bcap->speech_ctm = (lv[i] & 0x20) >> 5;
160 if (s == 7) /* maximum speech versions + end of list */
161 return 0;
162 }
Harald Weltec8a0b932012-08-24 21:27:26 +0200163 break;
164 case GSM_MNCC_BCAP_UNR_DIG:
165 case GSM_MNCC_BCAP_FAX_G3:
166 i = 1;
167 while(!(lv[i] & 0x80)) {
168 i++; /* octet 3a etc */
169 if (in_len < i)
170 return 0;
171 /* ignore them */
172 }
173 /* octet 4: skip */
174 i++;
175 /* octet 5 */
176 i++;
177 if (in_len < i)
178 return 0;
179 bcap->data.rate_adaption = (lv[i] >> 3) & 3;
180 bcap->data.sig_access = lv[i] & 7;
181 while(!(lv[i] & 0x80)) {
182 i++; /* octet 5a etc */
183 if (in_len < i)
184 return 0;
185 /* ignore them */
186 }
187 /* octet 6 */
188 i++;
189 if (in_len < i)
190 return 0;
191 bcap->data.async = lv[i] & 1;
192 if (!(lv[i] & 0x80)) {
193 i++;
194 if (in_len < i)
195 return 0;
196 /* octet 6a */
197 bcap->data.nr_stop_bits = ((lv[i] >> 7) & 1) + 1;
198 if (lv[i] & 0x10)
199 bcap->data.nr_data_bits = 8;
200 else
201 bcap->data.nr_data_bits = 7;
202 bcap->data.user_rate = lv[i] & 0xf;
203
204 if (!(lv[i] & 0x80)) {
205 i++;
206 if (in_len < i)
207 return 0;
208 /* octet 6b */
209 bcap->data.parity = lv[i] & 7;
210 bcap->data.interm_rate = (lv[i] >> 5) & 3;
211
212 /* octet 6c */
213 if (!(lv[i] & 0x80)) {
214 i++;
215 if (in_len < i)
216 return 0;
217 bcap->data.transp = (lv[i] >> 5) & 3;
218 bcap->data.modem_type = lv[i] & 0x1F;
219 }
220 }
221
222 }
223 break;
224 default:
Harald Welte1e908662010-03-07 23:39:54 +0100225 i = 1;
226 while (!(lv[i] & 0x80)) {
227 i++; /* octet 3a etc */
228 if (in_len < i)
229 return 0;
230 /* ignore them */
231 }
232 /* FIXME: implement OCTET 4+ parsing */
Harald Weltec8a0b932012-08-24 21:27:26 +0200233 break;
Harald Welte1e908662010-03-07 23:39:54 +0100234 }
235
236 return 0;
237}
238
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200239/*! Encode TS 04.08 Bearer Capability IE (10.5.4.5)
Harald Welte96e2a002017-06-12 21:44:18 +0200240 * \param[out] msg Message Buffer to which IE is to be appended
241 * \param[in] lv_only Write only LV portion (1) or TLV (0)
242 * \param[in] bcap Decoded Bearer Capability to be encoded
243 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100244int gsm48_encode_bearer_cap(struct msgb *msg, int lv_only,
245 const struct gsm_mncc_bearer_cap *bcap)
246{
247 uint8_t lv[32 + 1];
248 int i = 1, s;
249
250 lv[1] = bcap->transfer;
251 lv[1] |= bcap->mode << 3;
252 lv[1] |= bcap->coding << 4;
253 lv[1] |= bcap->radio << 5;
254
Harald Weltec8a0b932012-08-24 21:27:26 +0200255 switch (bcap->transfer) {
256 case GSM_MNCC_BCAP_SPEECH:
Harald Welte1e908662010-03-07 23:39:54 +0100257 for (s = 0; bcap->speech_ver[s] >= 0; s++) {
258 i++; /* octet 3a etc */
259 lv[i] = bcap->speech_ver[s];
260 if (i == 2) /* octet 3a */
261 lv[i] |= bcap->speech_ctm << 5;
262 }
263 lv[i] |= 0x80; /* last IE of octet 3 etc */
Harald Weltec8a0b932012-08-24 21:27:26 +0200264 break;
265 case GSM48_BCAP_ITCAP_UNR_DIG_INF:
266 case GSM48_BCAP_ITCAP_FAX_G3:
267 lv[i++] |= 0x80; /* last IE of octet 3 etc */
268 /* octet 4 */
269 lv[i++] = 0xb8;
270 /* octet 5 */
271 lv[i++] = 0x80 | ((bcap->data.rate_adaption & 3) << 3)
272 | (bcap->data.sig_access & 7);
273 /* octet 6 */
274 lv[i++] = 0x20 | (bcap->data.async & 1);
275 /* octet 6a */
276 lv[i++] = (bcap->data.user_rate & 0xf) |
277 (bcap->data.nr_data_bits == 8 ? 0x10 : 0x00) |
278 (bcap->data.nr_stop_bits == 2 ? 0x40 : 0x00);
279 /* octet 6b */
280 lv[i++] = (bcap->data.parity & 7) |
281 ((bcap->data.interm_rate & 3) << 5);
282 /* octet 6c */
283 lv[i] = 0x80 | (bcap->data.modem_type & 0x1f);
284 break;
285 default:
286 return -EINVAL;
Harald Welte1e908662010-03-07 23:39:54 +0100287 }
288
289 lv[0] = i;
290 if (lv_only)
291 msgb_lv_put(msg, lv[0], lv+1);
292 else
293 msgb_tlv_put(msg, GSM48_IE_BEARER_CAP, lv[0], lv+1);
294
295 return 0;
296}
297
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200298/*! Decode TS 04.08 Call Control Capabilities IE (10.5.4.5a)
Harald Welte96e2a002017-06-12 21:44:18 +0200299 * \param[out] Caller-provided memory for decoded CC capabilities
300 * \param[in] lv Length-Value of IE
301 * \retursns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100302int gsm48_decode_cccap(struct gsm_mncc_cccap *ccap, const uint8_t *lv)
303{
304 uint8_t in_len = lv[0];
305
306 if (in_len < 1)
307 return -EINVAL;
308
309 /* octet 3 */
310 ccap->dtmf = lv[1] & 0x01;
311 ccap->pcp = (lv[1] & 0x02) >> 1;
312
313 return 0;
314}
315
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200316/*! Encodoe TS 04.08 Call Control Capabilities (10.5.4.5a)
Harald Welte96e2a002017-06-12 21:44:18 +0200317 * \param[out] msg Message Buffer to which to append IE (as TLV)
318 * \param[in] ccap Decoded CC Capabilities to be encoded
319 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100320int gsm48_encode_cccap(struct msgb *msg,
321 const struct gsm_mncc_cccap *ccap)
322{
323 uint8_t lv[2];
324
325 lv[0] = 1;
326 lv[1] = 0;
327 if (ccap->dtmf)
328 lv [1] |= 0x01;
329 if (ccap->pcp)
330 lv [1] |= 0x02;
331
332 msgb_tlv_put(msg, GSM48_IE_CC_CAP, lv[0], lv+1);
333
334 return 0;
335}
336
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200337/*! Decode TS 04.08 Called Party BCD Number IE (10.5.4.7)
Harald Welte96e2a002017-06-12 21:44:18 +0200338 * \param[out] called Caller-provided memory for decoded number
339 * \param[in] lv Length-Value portion of IE
340 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100341int gsm48_decode_called(struct gsm_mncc_number *called,
342 const uint8_t *lv)
343{
344 uint8_t in_len = lv[0];
345
346 if (in_len < 1)
347 return -EINVAL;
348
349 /* octet 3 */
350 called->plan = lv[1] & 0x0f;
351 called->type = (lv[1] & 0x70) >> 4;
352
353 /* octet 4..N */
354 gsm48_decode_bcd_number(called->number, sizeof(called->number), lv, 1);
355
356 return 0;
357}
358
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200359/*! Encode TS 04.08 Called Party IE (10.5.4.7)
Harald Welte96e2a002017-06-12 21:44:18 +0200360 * \param[out] msg Mesage Buffer to which to append IE (as TLV)
361 * \param[in] called MNCC Number to encode/append
362 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100363int gsm48_encode_called(struct msgb *msg,
364 const struct gsm_mncc_number *called)
365{
366 uint8_t lv[18];
367 int ret;
368
369 /* octet 3 */
Sylvain Munaut47ee6932010-09-20 20:59:23 +0200370 lv[1] = 0x80; /* no extension */
371 lv[1] |= called->plan;
Harald Welte1e908662010-03-07 23:39:54 +0100372 lv[1] |= called->type << 4;
373
374 /* octet 4..N, octet 2 */
375 ret = gsm48_encode_bcd_number(lv, sizeof(lv), 1, called->number);
376 if (ret < 0)
377 return ret;
378
379 msgb_tlv_put(msg, GSM48_IE_CALLED_BCD, lv[0], lv+1);
380
381 return 0;
382}
383
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200384/*! Decode TS 04.08 Caller ID
Harald Welte96e2a002017-06-12 21:44:18 +0200385 * \param[out] called Caller-provided memory for decoded number
386 * \param[in] lv Length-Value portion of IE
387 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100388int gsm48_decode_callerid(struct gsm_mncc_number *callerid,
389 const uint8_t *lv)
390{
391 uint8_t in_len = lv[0];
392 int i = 1;
393
394 if (in_len < 1)
395 return -EINVAL;
396
397 /* octet 3 */
398 callerid->plan = lv[1] & 0x0f;
399 callerid->type = (lv[1] & 0x70) >> 4;
400
401 /* octet 3a */
402 if (!(lv[1] & 0x80)) {
403 callerid->screen = lv[2] & 0x03;
404 callerid->present = (lv[2] & 0x60) >> 5;
405 i = 2;
406 }
407
408 /* octet 4..N */
409 gsm48_decode_bcd_number(callerid->number, sizeof(callerid->number), lv, i);
410
411 return 0;
412}
413
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200414/*! Encode TS 04.08 Caller ID IE
Harald Welte96e2a002017-06-12 21:44:18 +0200415 * \param[out] msg Mesage Buffer to which to append IE (as TLV)
416 * \param[in] ie IE Identifier (tag)
417 * \param[in] max_len maximum generated output in bytes
418 * \param[in] callerid MNCC Number to encode/append
419 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100420int gsm48_encode_callerid(struct msgb *msg, int ie, int max_len,
421 const struct gsm_mncc_number *callerid)
422{
423 uint8_t lv[max_len - 1];
424 int h_len = 1;
425 int ret;
426
427 /* octet 3 */
428 lv[1] = callerid->plan;
429 lv[1] |= callerid->type << 4;
430
431 if (callerid->present || callerid->screen) {
432 /* octet 3a */
433 lv[2] = callerid->screen;
434 lv[2] |= callerid->present << 5;
435 lv[2] |= 0x80;
436 h_len++;
437 } else
438 lv[1] |= 0x80;
439
440 /* octet 4..N, octet 2 */
441 ret = gsm48_encode_bcd_number(lv, sizeof(lv), h_len, callerid->number);
442 if (ret < 0)
443 return ret;
444
445 msgb_tlv_put(msg, ie, lv[0], lv+1);
446
447 return 0;
448}
449
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200450/*! Decode TS 04.08 Cause IE (10.5.4.11)
Harald Welte96e2a002017-06-12 21:44:18 +0200451 * \param[out] cause Caller-provided memory for output
452 * \param[in] lv LV portion of Cause IE
453 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100454int gsm48_decode_cause(struct gsm_mncc_cause *cause,
455 const uint8_t *lv)
456{
457 uint8_t in_len = lv[0];
458 int i;
459
460 if (in_len < 2)
461 return -EINVAL;
462
463 cause->diag_len = 0;
464
465 /* octet 3 */
466 cause->location = lv[1] & 0x0f;
467 cause->coding = (lv[1] & 0x60) >> 5;
468
469 i = 1;
470 if (!(lv[i] & 0x80)) {
471 i++; /* octet 3a */
472 if (in_len < i+1)
473 return 0;
474 cause->rec = 1;
475 cause->rec_val = lv[i] & 0x7f;
476 }
477 i++;
478
479 /* octet 4 */
480 cause->value = lv[i] & 0x7f;
481 i++;
482
483 if (in_len < i) /* no diag */
484 return 0;
485
486 if (in_len - (i-1) > 32) /* maximum 32 octets */
487 return 0;
488
489 /* octet 5-N */
490 memcpy(cause->diag, lv + i, in_len - (i-1));
491 cause->diag_len = in_len - (i-1);
492
493 return 0;
494}
495
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200496/*! Encode TS 04.08 Cause IE (10.5.4.11)
Harald Welte96e2a002017-06-12 21:44:18 +0200497 * \param[out] msg Message Buffer to which to append IE
498 * \param[in] lv_only Encode as LV (1) or TLV (0)
499 * \param[in] cause Cause value to be encoded
500 * \returns 0 on success; negative on error */
Harald Welte1e908662010-03-07 23:39:54 +0100501int gsm48_encode_cause(struct msgb *msg, int lv_only,
502 const struct gsm_mncc_cause *cause)
503{
504 uint8_t lv[32+4];
505 int i;
506
507 if (cause->diag_len > 32)
508 return -EINVAL;
509
510 /* octet 3 */
511 lv[1] = cause->location;
512 lv[1] |= cause->coding << 5;
513
514 i = 1;
515 if (cause->rec) {
516 i++; /* octet 3a */
517 lv[i] = cause->rec_val;
518 }
519 lv[i] |= 0x80; /* end of octet 3 */
520
521 /* octet 4 */
522 i++;
523 lv[i] = 0x80 | cause->value;
524
525 /* octet 5-N */
526 if (cause->diag_len) {
527 memcpy(lv + i, cause->diag, cause->diag_len);
528 i += cause->diag_len;
529 }
530
531 lv[0] = i;
532 if (lv_only)
533 msgb_lv_put(msg, lv[0], lv+1);
534 else
535 msgb_tlv_put(msg, GSM48_IE_CAUSE, lv[0], lv+1);
536
537 return 0;
538}
539
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200540/*! Decode TS 04.08 Calling Number IE (10.5.4.9) */
Harald Welte1e908662010-03-07 23:39:54 +0100541int gsm48_decode_calling(struct gsm_mncc_number *calling,
542 const uint8_t *lv)
543{
544 return gsm48_decode_callerid(calling, lv);
545}
546
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200547/*! Encode TS 04.08 Calling Number IE (10.5.4.9) */
Harald Welte1e908662010-03-07 23:39:54 +0100548int gsm48_encode_calling(struct msgb *msg,
549 const struct gsm_mncc_number *calling)
550{
551 return gsm48_encode_callerid(msg, GSM48_IE_CALLING_BCD, 14, calling);
552}
553
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200554/*! Decode TS 04.08 Connected Number IE (10.5.4.13) */
Harald Welte1e908662010-03-07 23:39:54 +0100555int gsm48_decode_connected(struct gsm_mncc_number *connected,
556 const uint8_t *lv)
557{
558 return gsm48_decode_callerid(connected, lv);
559}
560
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200561/*! Encode TS 04.08 Connected Number IE (10.5.4.13) */
Harald Welte1e908662010-03-07 23:39:54 +0100562int gsm48_encode_connected(struct msgb *msg,
563 const struct gsm_mncc_number *connected)
564{
565 return gsm48_encode_callerid(msg, GSM48_IE_CONN_BCD, 14, connected);
566}
567
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200568/*! Decode TS 04.08 Redirecting Number IE (10.5.4.21b) */
Harald Welte1e908662010-03-07 23:39:54 +0100569int gsm48_decode_redirecting(struct gsm_mncc_number *redirecting,
570 const uint8_t *lv)
571{
572 return gsm48_decode_callerid(redirecting, lv);
573}
574
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200575/*! Encode TS 04.08 Redirecting Number IE (10.5.4.21b) */
Harald Welte1e908662010-03-07 23:39:54 +0100576int gsm48_encode_redirecting(struct msgb *msg,
577 const struct gsm_mncc_number *redirecting)
578{
579 return gsm48_encode_callerid(msg, GSM48_IE_REDIR_BCD, 19, redirecting);
580}
581
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200582/*! Decode TS 04.08 Facility IE (10.5.4.15) */
Harald Welte1e908662010-03-07 23:39:54 +0100583int gsm48_decode_facility(struct gsm_mncc_facility *facility,
584 const uint8_t *lv)
585{
586 uint8_t in_len = lv[0];
587
588 if (in_len < 1)
589 return -EINVAL;
590
591 if (in_len > sizeof(facility->info))
592 return -EINVAL;
593
594 memcpy(facility->info, lv+1, in_len);
595 facility->len = in_len;
596
597 return 0;
598}
599
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200600/*! Encode TS 04.08 Facility IE (10.5.4.15) */
Harald Welte1e908662010-03-07 23:39:54 +0100601int gsm48_encode_facility(struct msgb *msg, int lv_only,
602 const struct gsm_mncc_facility *facility)
603{
604 uint8_t lv[GSM_MAX_FACILITY + 1];
605
606 if (facility->len < 1 || facility->len > GSM_MAX_FACILITY)
607 return -EINVAL;
608
609 memcpy(lv+1, facility->info, facility->len);
610 lv[0] = facility->len;
611 if (lv_only)
612 msgb_lv_put(msg, lv[0], lv+1);
613 else
614 msgb_tlv_put(msg, GSM48_IE_FACILITY, lv[0], lv+1);
615
616 return 0;
617}
618
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200619/*! Decode TS 04.08 Notify IE (10.5.4.20) */
Harald Welte1e908662010-03-07 23:39:54 +0100620int gsm48_decode_notify(int *notify, const uint8_t *v)
621{
622 *notify = v[0] & 0x7f;
623
624 return 0;
625}
626
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200627/*! Encode TS 04.08 Notify IE (10.5.4.20) */
Harald Welte1e908662010-03-07 23:39:54 +0100628int gsm48_encode_notify(struct msgb *msg, int notify)
629{
630 msgb_v_put(msg, notify | 0x80);
631
632 return 0;
633}
634
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200635/*! Decode TS 04.08 Signal IE (10.5.4.23) */
Harald Welte1e908662010-03-07 23:39:54 +0100636int gsm48_decode_signal(int *signal, const uint8_t *v)
637{
638 *signal = v[0];
639
640 return 0;
641}
642
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200643/*! Encode TS 04.08 Signal IE (10.5.4.23) */
Harald Welte1e908662010-03-07 23:39:54 +0100644int gsm48_encode_signal(struct msgb *msg, int signal)
645{
646 msgb_tv_put(msg, GSM48_IE_SIGNAL, signal);
647
648 return 0;
649}
650
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200651/*! Decode TS 04.08 Keypad IE (10.5.4.17) */
Harald Welte1e908662010-03-07 23:39:54 +0100652int gsm48_decode_keypad(int *keypad, const uint8_t *lv)
653{
654 uint8_t in_len = lv[0];
655
656 if (in_len < 1)
657 return -EINVAL;
658
659 *keypad = lv[1] & 0x7f;
660
661 return 0;
662}
663
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200664/*! Encode TS 04.08 Keypad IE (10.5.4.17) */
Harald Welte1e908662010-03-07 23:39:54 +0100665int gsm48_encode_keypad(struct msgb *msg, int keypad)
666{
667 msgb_tv_put(msg, GSM48_IE_KPD_FACILITY, keypad);
668
669 return 0;
670}
671
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200672/*! Decode TS 04.08 Progress IE (10.5.4.21) */
Harald Welte1e908662010-03-07 23:39:54 +0100673int gsm48_decode_progress(struct gsm_mncc_progress *progress,
674 const uint8_t *lv)
675{
676 uint8_t in_len = lv[0];
677
678 if (in_len < 2)
679 return -EINVAL;
680
681 progress->coding = (lv[1] & 0x60) >> 5;
682 progress->location = lv[1] & 0x0f;
683 progress->descr = lv[2] & 0x7f;
684
685 return 0;
686}
687
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200688/*! Encode TS 04.08 Progress IE (10.5.4.21) */
Harald Welte1e908662010-03-07 23:39:54 +0100689int gsm48_encode_progress(struct msgb *msg, int lv_only,
690 const struct gsm_mncc_progress *p)
691{
692 uint8_t lv[3];
693
694 lv[0] = 2;
695 lv[1] = 0x80 | ((p->coding & 0x3) << 5) | (p->location & 0xf);
696 lv[2] = 0x80 | (p->descr & 0x7f);
697 if (lv_only)
698 msgb_lv_put(msg, lv[0], lv+1);
699 else
700 msgb_tlv_put(msg, GSM48_IE_PROGR_IND, lv[0], lv+1);
701
702 return 0;
703}
704
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200705/*! Decode TS 04.08 User-User IE (10.5.4.25) */
Harald Welte1e908662010-03-07 23:39:54 +0100706int gsm48_decode_useruser(struct gsm_mncc_useruser *uu,
707 const uint8_t *lv)
708{
709 uint8_t in_len = lv[0];
710 char *info = uu->info;
711 int info_len = sizeof(uu->info);
712 int i;
713
714 if (in_len < 1)
715 return -EINVAL;
716
717 uu->proto = lv[1];
718
719 for (i = 2; i <= in_len; i++) {
720 info_len--;
721 if (info_len <= 1)
722 break;
723 *info++ = lv[i];
724 }
725 if (info_len >= 1)
726 *info++ = '\0';
727
728 return 0;
729}
730
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200731/*! Encode TS 04.08 User-User IE (10.5.4.25) */
Harald Welte1e908662010-03-07 23:39:54 +0100732int gsm48_encode_useruser(struct msgb *msg, int lv_only,
733 const struct gsm_mncc_useruser *uu)
734{
735 uint8_t lv[GSM_MAX_USERUSER + 2];
736
737 if (strlen(uu->info) > GSM_MAX_USERUSER)
738 return -EINVAL;
739
740 lv[0] = 1 + strlen(uu->info);
741 lv[1] = uu->proto;
742 memcpy(lv + 2, uu->info, strlen(uu->info));
743 if (lv_only)
744 msgb_lv_put(msg, lv[0], lv+1);
745 else
746 msgb_tlv_put(msg, GSM48_IE_USER_USER, lv[0], lv+1);
747
748 return 0;
749}
750
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200751/*! Decode TS 04.08 SS Version IE (10.5.4.24) */
Harald Welte1e908662010-03-07 23:39:54 +0100752int gsm48_decode_ssversion(struct gsm_mncc_ssversion *ssv,
753 const uint8_t *lv)
754{
755 uint8_t in_len = lv[0];
756
757 if (in_len < 1 || in_len < sizeof(ssv->info))
758 return -EINVAL;
759
760 memcpy(ssv->info, lv + 1, in_len);
761 ssv->len = in_len;
762
763 return 0;
764}
765
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200766/*! Encode TS 04.08 SS Version IE (10.5.4.24) */
Harald Welte1e908662010-03-07 23:39:54 +0100767int gsm48_encode_ssversion(struct msgb *msg,
768 const struct gsm_mncc_ssversion *ssv)
769{
770 uint8_t lv[GSM_MAX_SSVERSION + 1];
771
772 if (ssv->len > GSM_MAX_SSVERSION)
773 return -EINVAL;
774
775 lv[0] = ssv->len;
776 memcpy(lv + 1, ssv->info, ssv->len);
777 msgb_tlv_put(msg, GSM48_IE_SS_VERS, lv[0], lv+1);
778
779 return 0;
780}
781
782/* decode 'more data' does not require a function, because it has no value */
783
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200784/*! Encode TS 04.08 More Data IE (10.5.4.19) */
Harald Welte1e908662010-03-07 23:39:54 +0100785int gsm48_encode_more(struct msgb *msg)
786{
787 uint8_t *ie;
788
789 ie = msgb_put(msg, 1);
790 ie[0] = GSM48_IE_MORE_DATA;
791
792 return 0;
793}
794
Sylvain Munaut71fd42f2011-09-01 22:05:29 +0200795static int32_t smod(int32_t n, int32_t m)
796{
797 int32_t res;
798
799 res = n % m;
800
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200801 if (res <= 0)
Sylvain Munaut71fd42f2011-09-01 22:05:29 +0200802 res += m;
803
Sylvain Munaut71fd42f2011-09-01 22:05:29 +0200804 return res;
805}
806
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200807/*! Decode TS 04.08 Cell Channel Description IE (10.5.2.1b) and other frequency lists
Harald Welte96e2a002017-06-12 21:44:18 +0200808 * \param[out] f Caller-provided output memory
809 * \param[in] cd Cell Channel Description IE
810 * \param[in] len Length of \a cd in bytes
811 * \returns 0 on success; negative on error */
Harald Welte1523d702010-08-04 11:46:44 +0200812int gsm48_decode_freq_list(struct gsm_sysinfo_freq *f, uint8_t *cd,
813 uint8_t len, uint8_t mask, uint8_t frqt)
814{
815 int i;
816
817 /* NOTES:
818 *
819 * The Range format uses "SMOD" computation.
820 * e.g. "n SMOD m" equals "((n - 1) % m) + 1"
821 * A cascade of multiple SMOD computations is simpified:
822 * "(n SMOD m) SMOD o" equals "(((n - 1) % m) % o) + 1"
823 *
824 * The Range format uses 16 octets of data in SYSTEM INFORMATION.
825 * When used in dedicated messages, the length can be less.
826 * In this case the ranges are decoded for all frequencies that
827 * fit in the block of given length.
828 */
829
830 /* tabula rasa */
831 for (i = 0; i < 1024; i++)
832 f[i].mask &= ~frqt;
833
834 /* 00..XXX. */
835 if ((cd[0] & 0xc0 & mask) == 0x00) {
836 /* Bit map 0 format */
837 if (len < 16)
838 return -EINVAL;
839 for (i = 1; i <= 124; i++)
840 if ((cd[15 - ((i-1) >> 3)] & (1 << ((i-1) & 7))))
841 f[i].mask |= frqt;
842
843 return 0;
844 }
845
846 /* 10..0XX. */
847 if ((cd[0] & 0xc8 & mask) == 0x80) {
848 /* Range 1024 format */
849 uint16_t w[17]; /* 1..16 */
850 struct gsm48_range_1024 *r = (struct gsm48_range_1024 *)cd;
851
852 if (len < 2)
853 return -EINVAL;
854 memset(w, 0, sizeof(w));
855 if (r->f0)
856 f[0].mask |= frqt;
857 w[1] = (r->w1_hi << 8) | r->w1_lo;
858 if (len >= 4)
859 w[2] = (r->w2_hi << 1) | r->w2_lo;
860 if (len >= 5)
861 w[3] = (r->w3_hi << 2) | r->w3_lo;
862 if (len >= 6)
863 w[4] = (r->w4_hi << 2) | r->w4_lo;
864 if (len >= 7)
865 w[5] = (r->w5_hi << 2) | r->w5_lo;
866 if (len >= 8)
867 w[6] = (r->w6_hi << 2) | r->w6_lo;
868 if (len >= 9)
869 w[7] = (r->w7_hi << 2) | r->w7_lo;
870 if (len >= 10)
871 w[8] = (r->w8_hi << 1) | r->w8_lo;
872 if (len >= 10)
873 w[9] = r->w9;
874 if (len >= 11)
875 w[10] = r->w10;
876 if (len >= 12)
877 w[11] = (r->w11_hi << 6) | r->w11_lo;
878 if (len >= 13)
879 w[12] = (r->w12_hi << 5) | r->w12_lo;
880 if (len >= 14)
881 w[13] = (r->w13_hi << 4) | r->w13_lo;
882 if (len >= 15)
883 w[14] = (r->w14_hi << 3) | r->w14_lo;
884 if (len >= 16)
885 w[15] = (r->w15_hi << 2) | r->w15_lo;
886 if (len >= 16)
887 w[16] = r->w16;
888 if (w[1])
889 f[w[1]].mask |= frqt;
890 if (w[2])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200891 f[smod(w[1] - 512 + w[2], 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200892 if (w[3])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200893 f[smod(w[1] + w[3], 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200894 if (w[4])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200895 f[smod(w[1] - 512 + smod(w[2] - 256 + w[4], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200896 if (w[5])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200897 f[smod(w[1] + smod(w[3] - 256 + w[5], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200898 if (w[6])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200899 f[smod(w[1] - 512 + smod(w[2] + w[6], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200900 if (w[7])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200901 f[smod(w[1] + smod(w[3] + w[7], 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200902 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200903 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 +0200904 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200905 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 +0200906 if (w[10])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200907 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 +0200908 if (w[11])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200909 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 +0200910 if (w[12])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200911 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 +0200912 if (w[13])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200913 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 +0200914 if (w[14])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200915 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 +0200916 if (w[15])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200917 f[smod(w[1] + smod(w[3] + smod(w[7] + w[15], 255), 511), 1023)].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200918 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200919 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 +0200920
921 return 0;
922 }
923 /* 10..100. */
924 if ((cd[0] & 0xce & mask) == 0x88) {
925 /* Range 512 format */
926 uint16_t w[18]; /* 1..17 */
927 struct gsm48_range_512 *r = (struct gsm48_range_512 *)cd;
928
929 if (len < 4)
930 return -EINVAL;
931 memset(w, 0, sizeof(w));
932 w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
933 w[1] = (r->w1_hi << 2) | r->w1_lo;
934 if (len >= 5)
935 w[2] = (r->w2_hi << 2) | r->w2_lo;
936 if (len >= 6)
937 w[3] = (r->w3_hi << 2) | r->w3_lo;
938 if (len >= 7)
939 w[4] = (r->w4_hi << 1) | r->w4_lo;
940 if (len >= 7)
941 w[5] = r->w5;
942 if (len >= 8)
943 w[6] = r->w6;
944 if (len >= 9)
945 w[7] = (r->w7_hi << 6) | r->w7_lo;
946 if (len >= 10)
947 w[8] = (r->w8_hi << 4) | r->w8_lo;
948 if (len >= 11)
949 w[9] = (r->w9_hi << 2) | r->w9_lo;
950 if (len >= 11)
951 w[10] = r->w10;
952 if (len >= 12)
953 w[11] = r->w11;
954 if (len >= 13)
955 w[12] = (r->w12_hi << 4) | r->w12_lo;
956 if (len >= 14)
957 w[13] = (r->w13_hi << 2) | r->w13_lo;
958 if (len >= 14)
959 w[14] = r->w14;
960 if (len >= 15)
961 w[15] = r->w15;
962 if (len >= 16)
963 w[16] = (r->w16_hi << 3) | r->w16_lo;
964 if (len >= 16)
965 w[17] = r->w17;
966 f[w[0]].mask |= frqt;
967 if (w[1])
968 f[(w[0] + w[1]) % 1024].mask |= frqt;
969 if (w[2])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200970 f[(w[0] + smod(w[1] - 256 + w[2], 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200971 if (w[3])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200972 f[(w[0] + smod(w[1] + w[3], 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200973 if (w[4])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200974 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 +0200975 if (w[5])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200976 f[(w[0] + smod(w[1] + smod(w[3] - 128 + w[5], 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200977 if (w[6])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200978 f[(w[0] + smod(w[1] - 256 + smod(w[2] + w[6], 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200979 if (w[7])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200980 f[(w[0] + smod(w[1] + smod(w[3] + w[7], 255), 511)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +0200981 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200982 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 +0200983 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200984 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 +0200985 if (w[10])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200986 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 +0200987 if (w[11])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200988 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 +0200989 if (w[12])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200990 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 +0200991 if (w[13])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200992 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 +0200993 if (w[14])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200994 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 +0200995 if (w[15])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200996 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 +0200997 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +0200998 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 +0200999 if (w[17])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001000 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 +02001001
1002 return 0;
1003 }
1004 /* 10..101. */
1005 if ((cd[0] & 0xce & mask) == 0x8a) {
1006 /* Range 256 format */
1007 uint16_t w[22]; /* 1..21 */
1008 struct gsm48_range_256 *r = (struct gsm48_range_256 *)cd;
1009
1010 if (len < 4)
1011 return -EINVAL;
1012 memset(w, 0, sizeof(w));
1013 w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
1014 w[1] = (r->w1_hi << 1) | r->w1_lo;
1015 if (len >= 4)
1016 w[2] = r->w2;
1017 if (len >= 5)
1018 w[3] = r->w3;
1019 if (len >= 6)
1020 w[4] = (r->w4_hi << 5) | r->w4_lo;
1021 if (len >= 7)
1022 w[5] = (r->w5_hi << 3) | r->w5_lo;
1023 if (len >= 8)
1024 w[6] = (r->w6_hi << 1) | r->w6_lo;
1025 if (len >= 8)
1026 w[7] = r->w7;
1027 if (len >= 9)
1028 w[8] = (r->w8_hi << 4) | r->w8_lo;
1029 if (len >= 10)
1030 w[9] = (r->w9_hi << 1) | r->w9_lo;
1031 if (len >= 10)
1032 w[10] = r->w10;
1033 if (len >= 11)
1034 w[11] = (r->w11_hi << 3) | r->w11_lo;
1035 if (len >= 11)
1036 w[12] = r->w12;
1037 if (len >= 12)
1038 w[13] = r->w13;
1039 if (len >= 13)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001040 w[14] = (r->w14_hi << 2) | r->w14_lo;
Harald Welte1523d702010-08-04 11:46:44 +02001041 if (len >= 13)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001042 w[15] = r->w15;
Harald Welte1523d702010-08-04 11:46:44 +02001043 if (len >= 14)
1044 w[16] = (r->w16_hi << 3) | r->w16_lo;
1045 if (len >= 14)
1046 w[17] = r->w17;
1047 if (len >= 15)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001048 w[18] = (r->w18_hi << 3) | r->w18_lo;
Harald Welte1523d702010-08-04 11:46:44 +02001049 if (len >= 15)
Jacob Erlbeck85bc5492014-01-13 14:21:23 +01001050 w[19] = r->w19;
Harald Welte1523d702010-08-04 11:46:44 +02001051 if (len >= 16)
1052 w[20] = (r->w20_hi << 3) | r->w20_lo;
1053 if (len >= 16)
1054 w[21] = r->w21;
1055 f[w[0]].mask |= frqt;
1056 if (w[1])
1057 f[(w[0] + w[1]) % 1024].mask |= frqt;
1058 if (w[2])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001059 f[(w[0] + smod(w[1] - 128 + w[2], 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001060 if (w[3])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001061 f[(w[0] + smod(w[1] + w[3], 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001062 if (w[4])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001063 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 +02001064 if (w[5])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001065 f[(w[0] + smod(w[1] + smod(w[3] - 64 + w[5], 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001066 if (w[6])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001067 f[(w[0] + smod(w[1] - 128 + smod(w[2] + w[6], 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001068 if (w[7])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001069 f[(w[0] + smod(w[1] + smod(w[3] + w[7], 127), 255)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001070 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001071 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 +02001072 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001073 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 +02001074 if (w[10])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001075 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 +02001076 if (w[11])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001077 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 +02001078 if (w[12])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001079 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 +02001080 if (w[13])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001081 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 +02001082 if (w[14])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001083 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 +02001084 if (w[15])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001085 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 +02001086 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001087 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 +02001088 if (w[17])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001089 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 +02001090 if (w[18])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001091 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 +02001092 if (w[19])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001093 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 +02001094 if (w[20])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001095 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 +02001096 if (w[21])
Sylvain Munaut71fd42f2011-09-01 22:05:29 +02001097 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 +02001098
1099 return 0;
1100 }
1101 /* 10..110. */
1102 if ((cd[0] & 0xce & mask) == 0x8c) {
1103 /* Range 128 format */
1104 uint16_t w[29]; /* 1..28 */
1105 struct gsm48_range_128 *r = (struct gsm48_range_128 *)cd;
1106
1107 if (len < 3)
1108 return -EINVAL;
1109 memset(w, 0, sizeof(w));
1110 w[0] = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
1111 w[1] = r->w1;
1112 if (len >= 4)
1113 w[2] = r->w2;
1114 if (len >= 5)
1115 w[3] = (r->w3_hi << 4) | r->w3_lo;
1116 if (len >= 6)
1117 w[4] = (r->w4_hi << 1) | r->w4_lo;
1118 if (len >= 6)
1119 w[5] = r->w5;
1120 if (len >= 7)
1121 w[6] = (r->w6_hi << 3) | r->w6_lo;
1122 if (len >= 7)
1123 w[7] = r->w7;
1124 if (len >= 8)
1125 w[8] = r->w8;
1126 if (len >= 8)
1127 w[9] = r->w9;
1128 if (len >= 9)
1129 w[10] = r->w10;
1130 if (len >= 9)
1131 w[11] = r->w11;
1132 if (len >= 10)
1133 w[12] = r->w12;
1134 if (len >= 10)
1135 w[13] = r->w13;
1136 if (len >= 11)
1137 w[14] = r->w14;
1138 if (len >= 11)
1139 w[15] = r->w15;
1140 if (len >= 12)
1141 w[16] = r->w16;
1142 if (len >= 12)
1143 w[17] = r->w17;
1144 if (len >= 13)
1145 w[18] = (r->w18_hi << 1) | r->w18_lo;
1146 if (len >= 13)
1147 w[19] = r->w19;
1148 if (len >= 13)
1149 w[20] = r->w20;
1150 if (len >= 14)
1151 w[21] = (r->w21_hi << 2) | r->w21_lo;
1152 if (len >= 14)
1153 w[22] = r->w22;
1154 if (len >= 14)
1155 w[23] = r->w23;
1156 if (len >= 15)
1157 w[24] = r->w24;
1158 if (len >= 15)
1159 w[25] = r->w25;
1160 if (len >= 16)
1161 w[26] = (r->w26_hi << 1) | r->w26_lo;
1162 if (len >= 16)
1163 w[27] = r->w27;
1164 if (len >= 16)
1165 w[28] = r->w28;
1166 f[w[0]].mask |= frqt;
1167 if (w[1])
1168 f[(w[0] + w[1]) % 1024].mask |= frqt;
1169 if (w[2])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001170 f[(w[0] + smod(w[1] - 64 + w[2], 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001171 if (w[3])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001172 f[(w[0] + smod(w[1] + w[3], 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001173 if (w[4])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001174 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 +02001175 if (w[5])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001176 f[(w[0] + smod(w[1] + smod(w[3] - 32 + w[5], 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001177 if (w[6])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001178 f[(w[0] + smod(w[1] - 64 + smod(w[2] + w[6], 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001179 if (w[7])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001180 f[(w[0] + smod(w[1] + smod(w[3] + w[7], 63), 127)) % 1024].mask |= frqt;
Harald Welte1523d702010-08-04 11:46:44 +02001181 if (w[8])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001182 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 +02001183 if (w[9])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001184 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 +02001185 if (w[10])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001186 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 +02001187 if (w[11])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001188 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 +02001189 if (w[12])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001190 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 +02001191 if (w[13])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001192 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 +02001193 if (w[14])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001194 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 +02001195 if (w[15])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001196 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 +02001197 if (w[16])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001198 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 +02001199 if (w[17])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001200 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 +02001201 if (w[18])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001202 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 +02001203 if (w[19])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001204 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 +02001205 if (w[20])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001206 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 +02001207 if (w[21])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001208 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 +02001209 if (w[22])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001210 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 +02001211 if (w[23])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001212 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 +02001213 if (w[24])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001214 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 +02001215 if (w[25])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001216 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 +02001217 if (w[26])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001218 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 +02001219 if (w[27])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001220 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 +02001221 if (w[28])
Andreas.Eversbergeaac0cf2011-09-02 20:12:19 +02001222 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 +02001223
1224 return 0;
1225 }
1226 /* 10..111. */
1227 if ((cd[0] & 0xce & mask) == 0x8e) {
1228 /* Variable bitmap format (can be any length >= 3) */
1229 uint16_t orig = 0;
1230 struct gsm48_var_bit *r = (struct gsm48_var_bit *)cd;
1231
1232 if (len < 3)
1233 return -EINVAL;
1234 orig = (r->orig_arfcn_hi << 9) | (r->orig_arfcn_mid << 1) | r->orig_arfcn_lo;
1235 f[orig].mask |= frqt;
1236 for (i = 1; 2 + (i >> 3) < len; i++)
1237 if ((cd[2 + (i >> 3)] & (0x80 >> (i & 7))))
1238 f[(orig + i) % 1024].mask |= frqt;
1239
1240 return 0;
1241 }
1242
1243 return 0;
1244}
Harald Welte96e2a002017-06-12 21:44:18 +02001245/*! @} */