1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
/*
* (C) 2012 by Harald Welte <laforge@gnumonks.org>
* All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <osmocom/gsm/protocol/gsm_04_08.h>
#include <osmocom/gsm/gsm48_ie.h>
#include <osmocom/gsm/gsm48.h>
#include <osmocom/gsm/mncc.h>
#include <osmocom/core/backtrace.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/msgb.h>
static const uint8_t csd_9600_v110_lv[] = { 0x07, 0xa1, 0xb8, 0x89, 0x21, 0x15, 0x63, 0x80 };
static const struct gsm_mncc_bearer_cap bcap_csd_9600_v110 = {
.transfer = GSM48_BCAP_ITCAP_UNR_DIG_INF,
.mode = GSM48_BCAP_TMOD_CIRCUIT,
.coding = GSM48_BCAP_CODING_GSM_STD,
.radio = GSM48_BCAP_RRQ_FR_ONLY,
.speech_ver[0]= -1,
.data = {
.rate_adaption = GSM48_BCAP_RA_V110_X30,
.sig_access = GSM48_BCAP_SA_I440_I450,
.async = 1,
.nr_stop_bits = 1,
.nr_data_bits = 8,
.user_rate = GSM48_BCAP_UR_9600,
.parity = GSM48_BCAP_PAR_NONE,
.interm_rate = GSM48_BCAP_IR_16k,
.transp = GSM48_BCAP_TR_TRANSP,
.modem_type = GSM48_BCAP_MT_NONE,
},
};
static const uint8_t speech_all_lv[] = { 0x06, 0x60, 0x04, 0x02, 0x00, 0x05, 0x81 };
static const struct gsm_mncc_bearer_cap bcap_speech_all = {
.transfer = GSM48_BCAP_ITCAP_SPEECH,
.mode = GSM48_BCAP_TMOD_CIRCUIT,
.coding = GSM48_BCAP_CODING_GSM_STD,
.radio = GSM48_BCAP_RRQ_DUAL_FR,
.speech_ver = {
4, 2, 0, 5, 1, -1,
},
};
struct bcap_test {
const uint8_t *lv;
const struct gsm_mncc_bearer_cap *bc;
const char *name;
};
static const struct bcap_test bcap_tests[] = {
{ csd_9600_v110_lv, &bcap_csd_9600_v110, "CSD 9600/V.110/transparent" },
{ speech_all_lv, &bcap_speech_all, "Speech, all codecs" },
};
static int test_bearer_cap()
{
struct gsm_mncc_bearer_cap bc;
int i, rc;
for (i = 0; i < ARRAY_SIZE(bcap_tests); i++) {
struct msgb *msg = msgb_alloc(100, "test");
int lv_len;
memset(&bc, 0, sizeof(bc));
/* test decoding */
rc = gsm48_decode_bearer_cap(&bc, bcap_tests[i].lv);
if (rc < 0) {
fprintf(stderr, "Error decoding %s\n",
bcap_tests[i].name);
return rc;
}
if (memcmp(&bc, bcap_tests[i].bc, sizeof(bc))) {
fprintf(stderr, "Incorrect decoded result of %s:\n",
bcap_tests[i].name);
fprintf(stderr, " should: %s\n",
osmo_hexdump((uint8_t *) bcap_tests[i].bc, sizeof(bc)));
fprintf(stderr, " is: %s\n",
osmo_hexdump((uint8_t *) &bc, sizeof(bc)));
return -1;
}
/* also test re-encode? */
rc = gsm48_encode_bearer_cap(msg, 1, &bc);
if (rc < 0) {
fprintf(stderr, "Error encoding %s\n",
bcap_tests[i].name);
return rc;
}
lv_len = bcap_tests[i].lv[0]+1;
if (memcmp(msg->data, bcap_tests[i].lv, lv_len)) {
fprintf(stderr, "Incorrect encoded result of %s:\n",
bcap_tests[i].name);
fprintf(stderr, " should: %s\n",
osmo_hexdump(bcap_tests[i].lv, lv_len));
fprintf(stderr, " is: %s\n",
osmo_hexdump(msg->data, msg->len));
return -1;
}
printf("Test `%s' passed\n", bcap_tests[i].name);
msgb_free(msg);
}
return 0;
}
static inline void dump_ra(const struct gprs_ra_id *raid)
{
printf("%s%s\n", osmo_rai_name(raid), raid->mnc_3_digits ? " (3-digit MNC)" : "");
}
static inline void check_ra(const struct gprs_ra_id *raid)
{
struct gsm48_ra_id ra;
struct gprs_ra_id raid0 = {
.mnc = 0,
.mcc = 0,
.lac = 0,
.rac = 0,
};
gsm48_encode_ra(&ra, raid);
printf("Constructed RA:\n");
gsm48_parse_ra(&raid0, (const uint8_t *)&ra);
dump_ra(raid);
printf("MCC+MNC in BCD: %s\n", osmo_hexdump(ra.digits, sizeof(ra.digits)));
dump_ra(&raid0);
printf("RA test...");
if (raid->mnc != raid0.mnc || raid->mcc != raid0.mcc || raid->lac != raid0.lac || raid->rac != raid0.rac
|| (raid->mnc_3_digits || raid->mnc > 99) != raid0.mnc_3_digits)
printf("FAIL\n");
else
printf("passed\n");
}
static inline void check_lai(const struct gprs_ra_id *raid)
{
int rc;
struct gsm48_loc_area_id lai = {};
struct gprs_ra_id decoded = {};
struct gprs_ra_id _laid = *raid;
struct gprs_ra_id *laid = &_laid;
laid->rac = 0;
printf("- gsm48_generate_lai() from "); dump_ra(laid);
gsm48_generate_lai(&lai, laid->mcc, laid->mnc, laid->lac);
printf(" Encoded %s\n", osmo_hexdump((unsigned char*)&lai, sizeof(lai)));
rc = gsm48_decode_lai(&lai, &decoded.mcc, &decoded.mnc, &decoded.lac);
if (rc) {
printf(" gsm48_decode_lai() returned %d --> FAIL\n", rc);
return;
}
printf(" gsm48_decode_lai() gives "); dump_ra(&decoded);
if (decoded.mcc == laid->mcc
&& decoded.mnc == laid->mnc
&& decoded.lac == laid->lac)
printf(" passed\n");
else
printf(" FAIL\n");
}
static inline void dump_lai(const struct osmo_location_area_id *lai)
{
printf("%s%s\n", osmo_lai_name(lai), lai->plmn.mnc_3_digits ? " (3-digit MNC)" : "");
}
static inline void check_lai2(const struct gprs_ra_id *raid)
{
struct gsm48_loc_area_id lai = {};
struct osmo_location_area_id decoded = {};
struct osmo_location_area_id laid = {
.plmn = {
.mcc = raid->mcc,
.mnc = raid->mnc,
.mnc_3_digits = raid->mnc_3_digits,
},
.lac = raid->lac,
};
printf("- gsm48_generate_lai2() from "); dump_lai(&laid);
gsm48_generate_lai2(&lai, &laid);
printf(" Encoded %s\n", osmo_hexdump((unsigned char*)&lai, sizeof(lai)));
gsm48_decode_lai2(&lai, &decoded);
printf(" gsm48_decode_lai2() gives "); dump_lai(&decoded);
if (decoded.plmn.mcc == laid.plmn.mcc
&& decoded.plmn.mnc == laid.plmn.mnc
&& decoded.lac == laid.lac
&& decoded.plmn.mnc_3_digits == (laid.plmn.mnc_3_digits || laid.plmn.mnc > 99))
printf(" passed\n");
else
printf(" FAIL\n");
}
static struct gprs_ra_id test_ra_cap_items[] = {
{
.mcc = 77,
.mnc = 121,
.lac = 666,
.rac = 5,
},
{
.mcc = 84,
.mnc = 98,
.lac = 11,
.rac = 89,
},
{
.mcc = 0,
.mnc = 0,
.lac = 0,
.rac = 0,
.mnc_3_digits = false,
/* expecting 000-00, BCD = 00 f0 00 */
},
{
.mcc = 0,
.mnc = 0,
.lac = 0,
.rac = 0,
.mnc_3_digits = true,
/* expecting 000-000, BCD = 00 00 00 */
},
{
.mcc = 999,
.mnc = 999,
.lac = 65535,
.rac = 255,
},
{
.mcc = 1,
.mnc = 2,
.lac = 23,
.rac = 42,
.mnc_3_digits = false,
/* expecting 001-02, BCD = 00 f1 20 */
},
{
.mcc = 1,
.mnc = 2,
.lac = 23,
.rac = 42,
.mnc_3_digits = true,
/* expecting 001-002, BCD = 00 21 00 */
},
{
.mcc = 12,
.mnc = 34,
.lac = 56,
.rac = 78,
.mnc_3_digits = false,
/* expecting 012-34, BCD = 10 f2 43 */
},
{
.mcc = 12,
.mnc = 34,
.lac = 23,
.rac = 42,
.mnc_3_digits = true,
/* expecting 012-034, BCD = 10 42 30 */
},
{
.mcc = 123,
.mnc = 456,
.lac = 23,
.rac = 42,
.mnc_3_digits = false,
/* expecting 123-456, BCD = 21 63 54 (false flag has no effect) */
},
{
.mcc = 123,
.mnc = 456,
.lac = 23,
.rac = 42,
.mnc_3_digits = true,
/* expecting 123-456, BCD = 21 63 54 (same) */
},
};
static void test_ra_cap(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(test_ra_cap_items); i++)
check_ra(&test_ra_cap_items[i]);
}
static void test_lai_encode_decode(void)
{
int i;
for (i = 0; i < ARRAY_SIZE(test_ra_cap_items); i++) {
check_lai(&test_ra_cap_items[i]);
check_lai2(&test_ra_cap_items[i]);
}
}
static void test_mid_from_tmsi(void)
{
static const uint8_t res[] = { 0x17, 0x05, 0xf4, 0xaa, 0xbb, 0xcc, 0xdd };
uint32_t tmsi = 0xAABBCCDD;
uint8_t buf[3 + sizeof(uint32_t)];
printf("Simple TMSI encoding test....");
memset(&buf, 0xFE, sizeof(buf));
gsm48_generate_mid_from_tmsi(buf, tmsi);
OSMO_ASSERT(memcmp(buf, res, sizeof(res)) == 0);
printf("passed\n");
}
static void test_mid_from_imsi(void)
{
char *imsi = "901700000004620";
uint8_t buf[10], len;
printf("Simple IMSI encoding test....");
len = gsm48_generate_mid_from_imsi(buf, imsi);
printf("passed: [%u] %s\n", len, osmo_hexdump(buf, len));
}
int main(int argc, char **argv)
{
test_bearer_cap();
test_mid_from_tmsi();
test_mid_from_imsi();
test_ra_cap();
test_lai_encode_decode();
return EXIT_SUCCESS;
}
|