blob: 6096854071e89c36b04e9494957eecad4a515c83 [file] [log] [blame]
Jacob Erlbeck9114bee2014-08-19 12:21:01 +02001/* GPRS Gb message parser */
2
3/* (C) 2014 by On-Waves
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
Harald Welte6e688082014-08-24 17:38:18 +020021#include <osmocom/gsm/gsm48.h>
22
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020023#include <openbsc/gprs_gb_parse.h>
24
25#include <openbsc/gprs_utils.h>
26
27#include <openbsc/gsm_04_08_gprs.h>
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020028#include <openbsc/debug.h>
29
30#include <osmocom/gprs/gprs_bssgp.h>
31
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020032static int gprs_gb_parse_gmm_attach_req(uint8_t *data, size_t data_len,
33 struct gprs_gb_parse_context *parse_ctx)
34{
35 uint8_t *value;
36 size_t value_len;
37
38 parse_ctx->llc_msg_name = "ATTACH_REQ";
39
40 /* Skip MS network capability */
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +010041 if (gprs_shift_lv(&data, &data_len, NULL, &value_len) <= 0 ||
Jacob Erlbeckc9cd15f2014-09-29 12:36:45 +020042 value_len < 1 || value_len > 8)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020043 /* invalid */
Jacob Erlbeckf349bae2014-09-29 12:45:36 +020044 return 0;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020045
46 /* Skip Attach type */
47 /* Skip Ciphering key sequence number */
48 /* Skip DRX parameter */
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +010049 gprs_shift_v_fixed(&data, &data_len, 3, NULL);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020050
51 /* Get Mobile identity */
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +010052 if (gprs_shift_lv(&data, &data_len, &value, &value_len) <= 0 ||
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020053 value_len < 5 || value_len > 8)
54 /* invalid */
55 return 0;
56
57 if (gprs_is_mi_tmsi(value, value_len)) {
Jacob Erlbeck49389172014-10-02 16:14:47 +020058 parse_ctx->ptmsi_enc = value + 1;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020059 } else if (gprs_is_mi_imsi(value, value_len)) {
60 parse_ctx->imsi = value;
61 parse_ctx->imsi_len = value_len;
62 }
63
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +010064 if (gprs_shift_v_fixed(&data, &data_len, 6, &value) <= 0)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020065 return 0;
66
67 parse_ctx->old_raid_enc = value;
68
69 return 1;
70}
71
72static int gprs_gb_parse_gmm_attach_ack(uint8_t *data, size_t data_len,
73 struct gprs_gb_parse_context *parse_ctx)
74{
75 uint8_t *value;
76 size_t value_len;
77
78 parse_ctx->llc_msg_name = "ATTACH_ACK";
79
80 /* Skip Attach result */
81 /* Skip Force to standby */
82 /* Skip Periodic RA update timer */
83 /* Skip Radio priority for SMS */
84 /* Skip Spare half octet */
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +010085 gprs_shift_v_fixed(&data, &data_len, 3, NULL);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020086
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +010087 if (gprs_shift_v_fixed(&data, &data_len, 6, &value) <= 0)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020088 return 0;
89
90 parse_ctx->raid_enc = value;
91
92 /* Skip P-TMSI signature (P-TMSI signature, opt, TV, length 4) */
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +010093 gprs_match_tv_fixed(&data, &data_len, GSM48_IE_GMM_PTMSI_SIG, 3, NULL);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020094
95 /* Skip Negotiated READY timer value (GPRS timer, opt, TV, length 2) */
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +010096 gprs_match_tv_fixed(&data, &data_len, GSM48_IE_GMM_TIMER_READY, 1, NULL);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +020097
98 /* Allocated P-TMSI (Mobile identity, opt, TLV, length 7) */
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +010099 if (gprs_match_tlv(&data, &data_len, GSM48_IE_GMM_ALLOC_PTMSI,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200100 &value, &value_len) > 0 &&
101 gprs_is_mi_tmsi(value, value_len))
Jacob Erlbeck49389172014-10-02 16:14:47 +0200102 parse_ctx->new_ptmsi_enc = value + 1;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200103 return 1;
104}
105
Jacob Erlbeck9c65c812014-09-22 10:42:05 +0200106static int gprs_gb_parse_gmm_attach_rej(uint8_t *data, size_t data_len,
107 struct gprs_gb_parse_context *parse_ctx)
108{
109 uint8_t *value;
110
111 parse_ctx->llc_msg_name = "ATTACH_REJ";
112
113 /* GMM cause */
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100114 if (gprs_shift_v_fixed(&data, &data_len, 1, &value) <= 0)
Jacob Erlbeck9c65c812014-09-22 10:42:05 +0200115 return 0;
116
117 parse_ctx->invalidate_tlli = 1;
118
119 return 1;
120}
121
122
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200123static int gprs_gb_parse_gmm_detach_req(uint8_t *data, size_t data_len,
124 struct gprs_gb_parse_context *parse_ctx)
125{
126 uint8_t *value;
127 size_t value_len;
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200128 int detach_type;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200129 int power_off;
130
131 parse_ctx->llc_msg_name = "DETACH_REQ";
132
133 /* Skip spare half octet */
134 /* Get Detach type */
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100135 if (gprs_shift_v_fixed(&data, &data_len, 1, &value) <= 0)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200136 /* invalid */
137 return 0;
138
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200139 detach_type = *value & 0x07;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200140 power_off = *value & 0x08 ? 1 : 0;
141
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200142 if (parse_ctx->to_bss) {
143 /* Network originated */
144 if (detach_type == GPRS_DET_T_MT_REATT_REQ)
145 parse_ctx->await_reattach = 1;
146 } else {
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200147 /* Mobile originated */
148
149 if (power_off)
150 parse_ctx->invalidate_tlli = 1;
151
152 /* Get P-TMSI (Mobile identity), see GSM 24.008, 9.4.5.2 */
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100153 if (gprs_match_tlv(&data, &data_len,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200154 GSM48_IE_GMM_ALLOC_PTMSI, &value, &value_len) > 0)
155 {
156 if (gprs_is_mi_tmsi(value, value_len))
Jacob Erlbeck49389172014-10-02 16:14:47 +0200157 parse_ctx->ptmsi_enc = value + 1;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200158 }
159 }
160
161 return 1;
162}
163
164static int gprs_gb_parse_gmm_ra_upd_req(uint8_t *data, size_t data_len,
165 struct gprs_gb_parse_context *parse_ctx)
166{
167 uint8_t *value;
168
169 parse_ctx->llc_msg_name = "RA_UPD_REQ";
170
171 /* Skip Update type */
172 /* Skip GPRS ciphering key sequence number */
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100173 gprs_shift_v_fixed(&data, &data_len, 1, NULL);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200174
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100175 if (gprs_shift_v_fixed(&data, &data_len, 6, &value) <= 0)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200176 return 0;
177
178 parse_ctx->old_raid_enc = value;
179
180 return 1;
181}
182
Jacob Erlbeck85e5c8f2014-09-16 12:16:58 +0200183static int gprs_gb_parse_gmm_ra_upd_rej(uint8_t *data, size_t data_len,
184 struct gprs_gb_parse_context *parse_ctx)
185{
186 uint8_t *value;
187 uint8_t cause;
188 int force_standby;
189
190 parse_ctx->llc_msg_name = "RA_UPD_REJ";
191
192 /* GMM cause */
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100193 if (gprs_shift_v_fixed(&data, &data_len, 1, &value) <= 0)
Jacob Erlbeck85e5c8f2014-09-16 12:16:58 +0200194 return 0;
195
196 cause = value[0];
197
198 /* Force to standby, 1/2 */
199 /* spare bits, 1/2 */
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100200 if (gprs_shift_v_fixed(&data, &data_len, 1, &value) <= 0)
Jacob Erlbeck85e5c8f2014-09-16 12:16:58 +0200201 return 0;
202
203 force_standby = (value[0] & 0x07) == 0x01;
204
205 if (cause == GMM_CAUSE_IMPL_DETACHED && !force_standby)
206 parse_ctx->await_reattach = 1;
207
208 parse_ctx->invalidate_tlli = 1;
209
210 return 1;
211}
212
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200213static int gprs_gb_parse_gmm_ra_upd_ack(uint8_t *data, size_t data_len,
214 struct gprs_gb_parse_context *parse_ctx)
215{
216 uint8_t *value;
217 size_t value_len;
218
219 parse_ctx->llc_msg_name = "RA_UPD_ACK";
220
221 /* Skip Force to standby */
222 /* Skip Update result */
223 /* Skip Periodic RA update timer */
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100224 gprs_shift_v_fixed(&data, &data_len, 2, NULL);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200225
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100226 if (gprs_shift_v_fixed(&data, &data_len, 6, &value) <= 0)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200227 return 0;
228
229 parse_ctx->raid_enc = value;
230
231 /* Skip P-TMSI signature (P-TMSI signature, opt, TV, length 4) */
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100232 gprs_match_tv_fixed(&data, &data_len, GSM48_IE_GMM_PTMSI_SIG, 3, NULL);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200233
234 /* Allocated P-TMSI (Mobile identity, opt, TLV, length 7) */
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100235 if (gprs_match_tlv(&data, &data_len, GSM48_IE_GMM_ALLOC_PTMSI,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200236 &value, &value_len) > 0 &&
237 gprs_is_mi_tmsi(value, value_len))
Jacob Erlbeck49389172014-10-02 16:14:47 +0200238 parse_ctx->new_ptmsi_enc = value + 1;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200239
240 return 1;
241}
242
243static int gprs_gb_parse_gmm_ptmsi_reall_cmd(uint8_t *data, size_t data_len,
244 struct gprs_gb_parse_context *parse_ctx)
245{
246 uint8_t *value;
247 size_t value_len;
248
249 parse_ctx->llc_msg_name = "PTMSI_REALL_CMD";
250
251 LOGP(DLLC, LOGL_NOTICE,
252 "Got P-TMSI Reallocation Command which is not covered by unit tests yet.\n");
253
254 /* Allocated P-TMSI */
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100255 if (gprs_shift_lv(&data, &data_len, &value, &value_len) > 0 &&
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200256 gprs_is_mi_tmsi(value, value_len))
Jacob Erlbeck49389172014-10-02 16:14:47 +0200257 parse_ctx->new_ptmsi_enc = value + 1;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200258
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100259 if (gprs_shift_v_fixed(&data, &data_len, 6, &value) <= 0)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200260 return 0;
261
262 parse_ctx->raid_enc = value;
263
264 return 1;
265}
266
267static int gprs_gb_parse_gmm_id_resp(uint8_t *data, size_t data_len,
268 struct gprs_gb_parse_context *parse_ctx)
269{
270 uint8_t *value;
271 size_t value_len;
272
273 parse_ctx->llc_msg_name = "ID_RESP";
274
275 /* Mobile identity, Mobile identity 10.5.1.4, M LV 2-10 */
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100276 if (gprs_shift_lv(&data, &data_len, &value, &value_len) <= 0 ||
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200277 value_len < 1 || value_len > 9)
278 /* invalid */
279 return 0;
280
281 if (gprs_is_mi_tmsi(value, value_len)) {
Jacob Erlbeck49389172014-10-02 16:14:47 +0200282 parse_ctx->ptmsi_enc = value + 1;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200283 } else if (gprs_is_mi_imsi(value, value_len)) {
284 parse_ctx->imsi = value;
285 parse_ctx->imsi_len = value_len;
286 }
287
288 return 1;
289}
290
291static int gprs_gb_parse_gsm_act_pdp_req(uint8_t *data, size_t data_len,
292 struct gprs_gb_parse_context *parse_ctx)
293{
294 ssize_t old_len;
295 uint8_t *value;
296 size_t value_len;
297
298 parse_ctx->llc_msg_name = "ACT_PDP_REQ";
299
300 /* Skip Requested NSAPI */
301 /* Skip Requested LLC SAPI */
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100302 gprs_shift_v_fixed(&data, &data_len, 2, NULL);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200303
304 /* Skip Requested QoS (support 04.08 and 24.008) */
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100305 if (gprs_shift_lv(&data, &data_len, NULL, &value_len) <= 0 ||
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200306 value_len < 4 || value_len > 14)
307 /* invalid */
Jacob Erlbeckf349bae2014-09-29 12:45:36 +0200308 return 0;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200309
310 /* Skip Requested PDP address */
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100311 if (gprs_shift_lv(&data, &data_len, NULL, &value_len) <= 0 ||
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200312 value_len < 2 || value_len > 18)
313 /* invalid */
314 return 0;
315
316 /* Access point name */
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100317 old_len = gprs_match_tlv(&data, &data_len,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200318 GSM48_IE_GSM_APN, &value, &value_len);
319
320 if (old_len > 0 && value_len >=1 && value_len <= 100) {
321 parse_ctx->apn_ie = data - old_len;
322 parse_ctx->apn_ie_len = old_len;
323 }
324
325 return 1;
326}
327
328int gprs_gb_parse_dtap(uint8_t *data, size_t data_len,
329 struct gprs_gb_parse_context *parse_ctx)
330{
331 struct gsm48_hdr *g48h;
332
Jacob Erlbeckdcfd4562014-12-11 11:01:46 +0100333 if (gprs_shift_v_fixed(&data, &data_len, sizeof(*g48h), (uint8_t **)&g48h) <= 0)
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200334 return 0;
335
336 parse_ctx->g48_hdr = g48h;
337
338 if ((g48h->proto_discr & 0x0f) != GSM48_PDISC_MM_GPRS &&
339 (g48h->proto_discr & 0x0f) != GSM48_PDISC_SM_GPRS)
340 return 1;
341
342 switch (g48h->msg_type) {
343 case GSM48_MT_GMM_ATTACH_REQ:
344 return gprs_gb_parse_gmm_attach_req(data, data_len, parse_ctx);
345
Jacob Erlbeck9c65c812014-09-22 10:42:05 +0200346 case GSM48_MT_GMM_ATTACH_REJ:
347 return gprs_gb_parse_gmm_attach_rej(data, data_len, parse_ctx);
348
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200349 case GSM48_MT_GMM_ATTACH_ACK:
350 return gprs_gb_parse_gmm_attach_ack(data, data_len, parse_ctx);
351
352 case GSM48_MT_GMM_RA_UPD_REQ:
353 return gprs_gb_parse_gmm_ra_upd_req(data, data_len, parse_ctx);
354
Jacob Erlbeck85e5c8f2014-09-16 12:16:58 +0200355 case GSM48_MT_GMM_RA_UPD_REJ:
356 return gprs_gb_parse_gmm_ra_upd_rej(data, data_len, parse_ctx);
357
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200358 case GSM48_MT_GMM_RA_UPD_ACK:
359 return gprs_gb_parse_gmm_ra_upd_ack(data, data_len, parse_ctx);
360
361 case GSM48_MT_GMM_PTMSI_REALL_CMD:
362 return gprs_gb_parse_gmm_ptmsi_reall_cmd(data, data_len, parse_ctx);
363
364 case GSM48_MT_GSM_ACT_PDP_REQ:
365 return gprs_gb_parse_gsm_act_pdp_req(data, data_len, parse_ctx);
366
367 case GSM48_MT_GMM_ID_RESP:
368 return gprs_gb_parse_gmm_id_resp(data, data_len, parse_ctx);
369
370 case GSM48_MT_GMM_DETACH_REQ:
371 return gprs_gb_parse_gmm_detach_req(data, data_len, parse_ctx);
372
373 case GSM48_MT_GMM_DETACH_ACK:
374 parse_ctx->llc_msg_name = "DETACH_ACK";
375 parse_ctx->invalidate_tlli = 1;
376 break;
377
378 default:
379 break;
380 };
381
382 return 1;
383}
384
385int gprs_gb_parse_llc(uint8_t *llc, size_t llc_len,
386 struct gprs_gb_parse_context *parse_ctx)
387{
388 struct gprs_llc_hdr_parsed *ghp = &parse_ctx->llc_hdr_parsed;
389 int rc;
390 int fcs;
391
392 /* parse LLC */
393 rc = gprs_llc_hdr_parse(ghp, llc, llc_len);
394 gprs_llc_hdr_dump(ghp);
395 if (rc != 0) {
396 LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n");
397 return 0;
398 }
399
400 fcs = gprs_llc_fcs(llc, ghp->crc_length);
401 LOGP(DLLC, LOGL_DEBUG, "Got LLC message, CRC: %06x (computed %06x)\n",
402 ghp->fcs, fcs);
403
404 if (!ghp->data)
405 return 0;
406
407 if (ghp->sapi != GPRS_SAPI_GMM)
408 return 1;
409
410 if (ghp->cmd != GPRS_LLC_UI)
411 return 1;
412
413 if (ghp->is_encrypted) {
414 parse_ctx->need_decryption = 1;
415 return 0;
416 }
417
418 return gprs_gb_parse_dtap(ghp->data, ghp->data_len, parse_ctx);
419}
420
421int gprs_gb_parse_bssgp(uint8_t *bssgp, size_t bssgp_len,
422 struct gprs_gb_parse_context *parse_ctx)
423{
424 struct bssgp_normal_hdr *bgph;
425 struct bssgp_ud_hdr *budh = NULL;
426 struct tlv_parsed *tp = &parse_ctx->bssgp_tp;
427 uint8_t pdu_type;
428 uint8_t *data;
429 size_t data_len;
430 int rc;
431
432 if (bssgp_len < sizeof(struct bssgp_normal_hdr))
433 return 0;
434
435 bgph = (struct bssgp_normal_hdr *)bssgp;
436 pdu_type = bgph->pdu_type;
437
438 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
439 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
440 if (bssgp_len < sizeof(struct bssgp_ud_hdr))
441 return 0;
442 budh = (struct bssgp_ud_hdr *)bssgp;
443 bgph = NULL;
444 data = budh->data;
445 data_len = bssgp_len - sizeof(*budh);
446 } else {
447 data = bgph->data;
448 data_len = bssgp_len - sizeof(*bgph);
449 }
450
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200451 parse_ctx->pdu_type = pdu_type;
452 parse_ctx->bud_hdr = budh;
453 parse_ctx->bgp_hdr = bgph;
454 parse_ctx->bssgp_data = data;
455 parse_ctx->bssgp_data_len = data_len;
456
Jacob Erlbeck9b071352014-10-09 12:04:56 +0200457 if (bssgp_tlv_parse(tp, data, data_len) < 0)
458 return 0;
459
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200460 if (budh)
461 parse_ctx->tlli_enc = (uint8_t *)&budh->tlli;
462
463 if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA))
464 parse_ctx->bssgp_raid_enc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA);
465
466 if (TLVP_PRESENT(tp, BSSGP_IE_CELL_ID))
467 parse_ctx->bssgp_raid_enc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_CELL_ID);
468
469 if (TLVP_PRESENT(tp, BSSGP_IE_IMSI)) {
470 parse_ctx->imsi = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_IMSI);
471 parse_ctx->imsi_len = TLVP_LEN(tp, BSSGP_IE_IMSI);
472 }
473
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200474 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI)) {
475 if (parse_ctx->tlli_enc)
476 /* This is TLLI old, don't confuse it with TLLI current */
477 parse_ctx->old_tlli_enc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_TLLI);
478 else
479 parse_ctx->tlli_enc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_TLLI);
480 }
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200481
482 if (TLVP_PRESENT(tp, BSSGP_IE_TMSI) && pdu_type == BSSGP_PDUT_PAGING_PS)
Jacob Erlbeckc37ef6c2014-09-30 13:49:43 +0200483 parse_ctx->bssgp_ptmsi_enc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_TMSI);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200484
485 if (TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) {
486 uint8_t *llc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
487 size_t llc_len = TLVP_LEN(tp, BSSGP_IE_LLC_PDU);
488
489 rc = gprs_gb_parse_llc(llc, llc_len, parse_ctx);
490 if (!rc)
491 return 0;
492
493 parse_ctx->llc = llc;
494 parse_ctx->llc_len = llc_len;
495 }
496
497 if (parse_ctx->tlli_enc) {
498 uint32_t tmp_tlli;
499 memcpy(&tmp_tlli, parse_ctx->tlli_enc, sizeof(tmp_tlli));
500 parse_ctx->tlli = ntohl(tmp_tlli);
501 }
502
Jacob Erlbeck948c07f2014-09-11 15:22:18 +0200503 if (parse_ctx->bssgp_raid_enc && parse_ctx->old_raid_enc &&
504 memcmp(parse_ctx->bssgp_raid_enc, parse_ctx->old_raid_enc, 6) != 0)
505 parse_ctx->old_raid_is_foreign = 1;
506
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200507 return 1;
508}
509
Jacob Erlbeck1c407aa2014-10-09 12:16:17 +0200510void gprs_gb_log_parse_context(int log_level,
511 struct gprs_gb_parse_context *parse_ctx,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200512 const char *default_msg_name)
513{
Jacob Erlbeck9b071352014-10-09 12:04:56 +0200514 const char *msg_name;
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200515 const char *sep = "";
516
517 if (!parse_ctx->tlli_enc &&
518 !parse_ctx->ptmsi_enc &&
519 !parse_ctx->new_ptmsi_enc &&
Jacob Erlbeckc37ef6c2014-09-30 13:49:43 +0200520 !parse_ctx->bssgp_ptmsi_enc &&
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200521 !parse_ctx->imsi)
522 return;
523
Jacob Erlbeck9b071352014-10-09 12:04:56 +0200524 msg_name = gprs_gb_message_name(parse_ctx, default_msg_name);
525
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200526 if (parse_ctx->llc_msg_name)
527 msg_name = parse_ctx->llc_msg_name;
528
Jacob Erlbeck1c407aa2014-10-09 12:16:17 +0200529 LOGP(DGPRS, log_level, "%s: Got", msg_name);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200530
531 if (parse_ctx->tlli_enc) {
Jacob Erlbeck1c407aa2014-10-09 12:16:17 +0200532 LOGPC(DGPRS, log_level, "%s TLLI %08x", sep, parse_ctx->tlli);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200533 sep = ",";
534 }
535
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200536 if (parse_ctx->old_tlli_enc) {
Jacob Erlbeck1c407aa2014-10-09 12:16:17 +0200537 LOGPC(DGPRS, log_level, "%s old TLLI %02x%02x%02x%02x", sep,
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200538 parse_ctx->old_tlli_enc[0],
539 parse_ctx->old_tlli_enc[1],
540 parse_ctx->old_tlli_enc[2],
541 parse_ctx->old_tlli_enc[3]);
542 sep = ",";
543 }
544
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200545 if (parse_ctx->bssgp_raid_enc) {
546 struct gprs_ra_id raid;
547 gsm48_parse_ra(&raid, parse_ctx->bssgp_raid_enc);
Jacob Erlbeck1c407aa2014-10-09 12:16:17 +0200548 LOGPC(DGPRS, log_level, "%s BSSGP RAID %u-%u-%u-%u", sep,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200549 raid.mcc, raid.mnc, raid.lac, raid.rac);
550 sep = ",";
551 }
552
553 if (parse_ctx->raid_enc) {
554 struct gprs_ra_id raid;
555 gsm48_parse_ra(&raid, parse_ctx->raid_enc);
Jacob Erlbeck1c407aa2014-10-09 12:16:17 +0200556 LOGPC(DGPRS, log_level, "%s RAID %u-%u-%u-%u", sep,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200557 raid.mcc, raid.mnc, raid.lac, raid.rac);
558 sep = ",";
559 }
560
561 if (parse_ctx->old_raid_enc) {
562 struct gprs_ra_id raid;
563 gsm48_parse_ra(&raid, parse_ctx->old_raid_enc);
Jacob Erlbeck1c407aa2014-10-09 12:16:17 +0200564 LOGPC(DGPRS, log_level, "%s old RAID %u-%u-%u-%u", sep,
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200565 raid.mcc, raid.mnc, raid.lac, raid.rac);
566 sep = ",";
567 }
568
Jacob Erlbeckc37ef6c2014-09-30 13:49:43 +0200569 if (parse_ctx->bssgp_ptmsi_enc) {
570 uint32_t ptmsi = GSM_RESERVED_TMSI;
571 gprs_parse_tmsi(parse_ctx->bssgp_ptmsi_enc, &ptmsi);
572 LOGPC(DGPRS, log_level, "%s BSSGP PTMSI %08x", sep, ptmsi);
573 sep = ",";
574 }
575
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200576 if (parse_ctx->ptmsi_enc) {
577 uint32_t ptmsi = GSM_RESERVED_TMSI;
Jacob Erlbeck49389172014-10-02 16:14:47 +0200578 gprs_parse_tmsi(parse_ctx->ptmsi_enc, &ptmsi);
Jacob Erlbeck1c407aa2014-10-09 12:16:17 +0200579 LOGPC(DGPRS, log_level, "%s PTMSI %08x", sep, ptmsi);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200580 sep = ",";
581 }
582
583 if (parse_ctx->new_ptmsi_enc) {
584 uint32_t new_ptmsi = GSM_RESERVED_TMSI;
Jacob Erlbeck49389172014-10-02 16:14:47 +0200585 gprs_parse_tmsi(parse_ctx->new_ptmsi_enc, &new_ptmsi);
Jacob Erlbeck1c407aa2014-10-09 12:16:17 +0200586 LOGPC(DGPRS, log_level, "%s new PTMSI %08x", sep, new_ptmsi);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200587 sep = ",";
588 }
589
590 if (parse_ctx->imsi) {
591 char mi_buf[200];
592 mi_buf[0] = '\0';
593 gsm48_mi_to_string(mi_buf, sizeof(mi_buf),
594 parse_ctx->imsi, parse_ctx->imsi_len);
Jacob Erlbeck1c407aa2014-10-09 12:16:17 +0200595 LOGPC(DGPRS, log_level, "%s IMSI %s",
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200596 sep, mi_buf);
597 sep = ",";
598 }
599 if (parse_ctx->invalidate_tlli) {
Jacob Erlbeck1c407aa2014-10-09 12:16:17 +0200600 LOGPC(DGPRS, log_level, "%s invalidate", sep);
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200601 sep = ",";
602 }
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200603 if (parse_ctx->await_reattach) {
Jacob Erlbeck1c407aa2014-10-09 12:16:17 +0200604 LOGPC(DGPRS, log_level, "%s re-attach", sep);
Jacob Erlbeck7430da62014-09-12 15:09:56 +0200605 sep = ",";
606 }
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200607
Jacob Erlbeck1c407aa2014-10-09 12:16:17 +0200608 LOGPC(DGPRS, log_level, "\n");
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200609}
610
Jacob Erlbeck9b071352014-10-09 12:04:56 +0200611const char *gprs_gb_message_name(const struct gprs_gb_parse_context *parse_ctx,
612 const char *default_msg_name)
613{
614 if (parse_ctx->llc_msg_name)
615 return parse_ctx->llc_msg_name;
616
617 if (parse_ctx->g48_hdr)
618 return "GMM";
619
620 if (parse_ctx->llc)
621 return "LLC";
622
623 if (parse_ctx->bud_hdr)
624 return "BSSGP-UNITDATA";
625
626 if (parse_ctx->bgp_hdr)
627 return "BSSGP";
628
629 return "unknown";
630}