blob: ef4a4d108514385a8f25d44a75c4f0cdf7350342 [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
32/* TODO: Move shift functions to libosmocore */
33
34int v_fixed_shift(uint8_t **data, size_t *data_len,
35 size_t len, uint8_t **value)
36{
37 if (len > *data_len)
38 goto fail;
39
40 if (value)
41 *value = *data;
42
43 *data += len;
44 *data_len -= len;
45
46 return len;
47
48fail:
49 *data += *data_len;
50 *data_len = 0;
51 return -1;
52}
53
54int tv_fixed_match(uint8_t **data, size_t *data_len,
55 uint8_t tag, size_t len,
56 uint8_t **value)
57{
58 size_t ie_len;
59
60 if (*data_len == 0)
61 goto fail;
62
63 if ((*data)[0] != tag)
64 return 0;
65
66 if (len > *data_len - 1)
67 goto fail;
68
69 if (value)
70 *value = *data + 1;
71
72 ie_len = len + 1;
73 *data += ie_len;
74 *data_len -= ie_len;
75
76 return ie_len;
77
78fail:
79 *data += *data_len;
80 *data_len = 0;
81 return -1;
82}
83
84int tlv_match(uint8_t **data, size_t *data_len,
85 uint8_t tag, uint8_t **value, size_t *value_len)
86{
87 size_t len;
88 size_t ie_len;
89
90 if (*data_len < 2)
91 goto fail;
92
93 if ((*data)[0] != tag)
94 return 0;
95
96 len = (*data)[1];
97 if (len > *data_len - 2)
98 goto fail;
99
100 if (value)
101 *value = *data + 2;
102 if (value_len)
103 *value_len = len;
104
105 ie_len = len + 2;
106
107 *data += ie_len;
108 *data_len -= ie_len;
109
110 return ie_len;
111
112fail:
113 *data += *data_len;
114 *data_len = 0;
115 return -1;
116}
117
118int lv_shift(uint8_t **data, size_t *data_len,
119 uint8_t **value, size_t *value_len)
120{
121 size_t len;
122 size_t ie_len;
123
124 if (*data_len < 1)
125 goto fail;
126
127 len = (*data)[0];
128 if (len > *data_len - 1)
129 goto fail;
130
131 if (value)
132 *value = *data + 1;
133 if (value_len)
134 *value_len = len;
135
136 ie_len = len + 1;
137 *data += ie_len;
138 *data_len -= ie_len;
139
140 return ie_len;
141
142fail:
143 *data += *data_len;
144 *data_len = 0;
145 return -1;
146}
147
148static int gprs_gb_parse_gmm_attach_req(uint8_t *data, size_t data_len,
149 struct gprs_gb_parse_context *parse_ctx)
150{
151 uint8_t *value;
152 size_t value_len;
153
154 parse_ctx->llc_msg_name = "ATTACH_REQ";
155
156 /* Skip MS network capability */
157 if (lv_shift(&data, &data_len, NULL, &value_len) <= 0 ||
158 value_len < 1 || value_len > 2)
159 /* invalid */
160 return 0;;
161
162 /* Skip Attach type */
163 /* Skip Ciphering key sequence number */
164 /* Skip DRX parameter */
165 v_fixed_shift(&data, &data_len, 3, NULL);
166
167 /* Get Mobile identity */
168 if (lv_shift(&data, &data_len, &value, &value_len) <= 0 ||
169 value_len < 5 || value_len > 8)
170 /* invalid */
171 return 0;
172
173 if (gprs_is_mi_tmsi(value, value_len)) {
174 parse_ctx->ptmsi_enc = value;
175 } else if (gprs_is_mi_imsi(value, value_len)) {
176 parse_ctx->imsi = value;
177 parse_ctx->imsi_len = value_len;
178 }
179
180 if (v_fixed_shift(&data, &data_len, 6, &value) <= 0)
181 return 0;
182
183 parse_ctx->old_raid_enc = value;
184
185 return 1;
186}
187
188static int gprs_gb_parse_gmm_attach_ack(uint8_t *data, size_t data_len,
189 struct gprs_gb_parse_context *parse_ctx)
190{
191 uint8_t *value;
192 size_t value_len;
193
194 parse_ctx->llc_msg_name = "ATTACH_ACK";
195
196 /* Skip Attach result */
197 /* Skip Force to standby */
198 /* Skip Periodic RA update timer */
199 /* Skip Radio priority for SMS */
200 /* Skip Spare half octet */
201 v_fixed_shift(&data, &data_len, 3, NULL);
202
203 if (v_fixed_shift(&data, &data_len, 6, &value) <= 0)
204 return 0;
205
206 parse_ctx->raid_enc = value;
207
208 /* Skip P-TMSI signature (P-TMSI signature, opt, TV, length 4) */
209 tv_fixed_match(&data, &data_len, GSM48_IE_GMM_PTMSI_SIG, 3, NULL);
210
211 /* Skip Negotiated READY timer value (GPRS timer, opt, TV, length 2) */
212 tv_fixed_match(&data, &data_len, GSM48_IE_GMM_TIMER_READY, 1, NULL);
213
214 /* Allocated P-TMSI (Mobile identity, opt, TLV, length 7) */
215 if (tlv_match(&data, &data_len, GSM48_IE_GMM_ALLOC_PTMSI,
216 &value, &value_len) > 0 &&
217 gprs_is_mi_tmsi(value, value_len))
218 parse_ctx->new_ptmsi_enc = value;
219 return 1;
220}
221
222static int gprs_gb_parse_gmm_detach_req(uint8_t *data, size_t data_len,
223 struct gprs_gb_parse_context *parse_ctx)
224{
225 uint8_t *value;
226 size_t value_len;
227 int detach_type;
228 int power_off;
229
230 parse_ctx->llc_msg_name = "DETACH_REQ";
231
232 /* Skip spare half octet */
233 /* Get Detach type */
234 if (v_fixed_shift(&data, &data_len, 1, &value) <= 0)
235 /* invalid */
236 return 0;
237
238 detach_type = *value & 0x07;
239 power_off = *value & 0x08 ? 1 : 0;
240
241 if (!parse_ctx->to_bss) {
242 /* Mobile originated */
243
244 if (power_off)
245 parse_ctx->invalidate_tlli = 1;
246
247 /* Get P-TMSI (Mobile identity), see GSM 24.008, 9.4.5.2 */
248 if (tlv_match(&data, &data_len,
249 GSM48_IE_GMM_ALLOC_PTMSI, &value, &value_len) > 0)
250 {
251 if (gprs_is_mi_tmsi(value, value_len))
252 parse_ctx->ptmsi_enc = value;
253 }
254 }
255
256 return 1;
257}
258
259static int gprs_gb_parse_gmm_ra_upd_req(uint8_t *data, size_t data_len,
260 struct gprs_gb_parse_context *parse_ctx)
261{
262 uint8_t *value;
263
264 parse_ctx->llc_msg_name = "RA_UPD_REQ";
265
266 /* Skip Update type */
267 /* Skip GPRS ciphering key sequence number */
268 v_fixed_shift(&data, &data_len, 1, NULL);
269
270 if (v_fixed_shift(&data, &data_len, 6, &value) <= 0)
271 return 0;
272
273 parse_ctx->old_raid_enc = value;
274
275 return 1;
276}
277
278static int gprs_gb_parse_gmm_ra_upd_ack(uint8_t *data, size_t data_len,
279 struct gprs_gb_parse_context *parse_ctx)
280{
281 uint8_t *value;
282 size_t value_len;
283
284 parse_ctx->llc_msg_name = "RA_UPD_ACK";
285
286 /* Skip Force to standby */
287 /* Skip Update result */
288 /* Skip Periodic RA update timer */
289 v_fixed_shift(&data, &data_len, 2, NULL);
290
291 if (v_fixed_shift(&data, &data_len, 6, &value) <= 0)
292 return 0;
293
294 parse_ctx->raid_enc = value;
295
296 /* Skip P-TMSI signature (P-TMSI signature, opt, TV, length 4) */
297 tv_fixed_match(&data, &data_len, GSM48_IE_GMM_PTMSI_SIG, 3, NULL);
298
299 /* Allocated P-TMSI (Mobile identity, opt, TLV, length 7) */
300 if (tlv_match(&data, &data_len, GSM48_IE_GMM_ALLOC_PTMSI,
301 &value, &value_len) > 0 &&
302 gprs_is_mi_tmsi(value, value_len))
303 parse_ctx->new_ptmsi_enc = value;
304
305 return 1;
306}
307
308static int gprs_gb_parse_gmm_ptmsi_reall_cmd(uint8_t *data, size_t data_len,
309 struct gprs_gb_parse_context *parse_ctx)
310{
311 uint8_t *value;
312 size_t value_len;
313
314 parse_ctx->llc_msg_name = "PTMSI_REALL_CMD";
315
316 LOGP(DLLC, LOGL_NOTICE,
317 "Got P-TMSI Reallocation Command which is not covered by unit tests yet.\n");
318
319 /* Allocated P-TMSI */
320 if (lv_shift(&data, &data_len, &value, &value_len) > 0 &&
321 gprs_is_mi_tmsi(value, value_len))
322 parse_ctx->new_ptmsi_enc = value;
323
324 if (v_fixed_shift(&data, &data_len, 6, &value) <= 0)
325 return 0;
326
327 parse_ctx->raid_enc = value;
328
329 return 1;
330}
331
332static int gprs_gb_parse_gmm_id_resp(uint8_t *data, size_t data_len,
333 struct gprs_gb_parse_context *parse_ctx)
334{
335 uint8_t *value;
336 size_t value_len;
337
338 parse_ctx->llc_msg_name = "ID_RESP";
339
340 /* Mobile identity, Mobile identity 10.5.1.4, M LV 2-10 */
341 if (lv_shift(&data, &data_len, &value, &value_len) <= 0 ||
342 value_len < 1 || value_len > 9)
343 /* invalid */
344 return 0;
345
346 if (gprs_is_mi_tmsi(value, value_len)) {
347 parse_ctx->ptmsi_enc = value;
348 } else if (gprs_is_mi_imsi(value, value_len)) {
349 parse_ctx->imsi = value;
350 parse_ctx->imsi_len = value_len;
351 }
352
353 return 1;
354}
355
356static int gprs_gb_parse_gsm_act_pdp_req(uint8_t *data, size_t data_len,
357 struct gprs_gb_parse_context *parse_ctx)
358{
359 ssize_t old_len;
360 uint8_t *value;
361 size_t value_len;
362
363 parse_ctx->llc_msg_name = "ACT_PDP_REQ";
364
365 /* Skip Requested NSAPI */
366 /* Skip Requested LLC SAPI */
367 v_fixed_shift(&data, &data_len, 2, NULL);
368
369 /* Skip Requested QoS (support 04.08 and 24.008) */
370 if (lv_shift(&data, &data_len, NULL, &value_len) <= 0 ||
371 value_len < 4 || value_len > 14)
372 /* invalid */
373 return 0;;
374
375 /* Skip Requested PDP address */
376 if (lv_shift(&data, &data_len, NULL, &value_len) <= 0 ||
377 value_len < 2 || value_len > 18)
378 /* invalid */
379 return 0;
380
381 /* Access point name */
382 old_len = tlv_match(&data, &data_len,
383 GSM48_IE_GSM_APN, &value, &value_len);
384
385 if (old_len > 0 && value_len >=1 && value_len <= 100) {
386 parse_ctx->apn_ie = data - old_len;
387 parse_ctx->apn_ie_len = old_len;
388 }
389
390 return 1;
391}
392
393int gprs_gb_parse_dtap(uint8_t *data, size_t data_len,
394 struct gprs_gb_parse_context *parse_ctx)
395{
396 struct gsm48_hdr *g48h;
397
398 if (v_fixed_shift(&data, &data_len, sizeof(*g48h), (uint8_t **)&g48h) <= 0)
399 return 0;
400
401 parse_ctx->g48_hdr = g48h;
402
403 if ((g48h->proto_discr & 0x0f) != GSM48_PDISC_MM_GPRS &&
404 (g48h->proto_discr & 0x0f) != GSM48_PDISC_SM_GPRS)
405 return 1;
406
407 switch (g48h->msg_type) {
408 case GSM48_MT_GMM_ATTACH_REQ:
409 return gprs_gb_parse_gmm_attach_req(data, data_len, parse_ctx);
410
411 case GSM48_MT_GMM_ATTACH_ACK:
412 return gprs_gb_parse_gmm_attach_ack(data, data_len, parse_ctx);
413
414 case GSM48_MT_GMM_RA_UPD_REQ:
415 return gprs_gb_parse_gmm_ra_upd_req(data, data_len, parse_ctx);
416
417 case GSM48_MT_GMM_RA_UPD_ACK:
418 return gprs_gb_parse_gmm_ra_upd_ack(data, data_len, parse_ctx);
419
420 case GSM48_MT_GMM_PTMSI_REALL_CMD:
421 return gprs_gb_parse_gmm_ptmsi_reall_cmd(data, data_len, parse_ctx);
422
423 case GSM48_MT_GSM_ACT_PDP_REQ:
424 return gprs_gb_parse_gsm_act_pdp_req(data, data_len, parse_ctx);
425
426 case GSM48_MT_GMM_ID_RESP:
427 return gprs_gb_parse_gmm_id_resp(data, data_len, parse_ctx);
428
429 case GSM48_MT_GMM_DETACH_REQ:
430 return gprs_gb_parse_gmm_detach_req(data, data_len, parse_ctx);
431
432 case GSM48_MT_GMM_DETACH_ACK:
433 parse_ctx->llc_msg_name = "DETACH_ACK";
434 parse_ctx->invalidate_tlli = 1;
435 break;
436
437 default:
438 break;
439 };
440
441 return 1;
442}
443
444int gprs_gb_parse_llc(uint8_t *llc, size_t llc_len,
445 struct gprs_gb_parse_context *parse_ctx)
446{
447 struct gprs_llc_hdr_parsed *ghp = &parse_ctx->llc_hdr_parsed;
448 int rc;
449 int fcs;
450
451 /* parse LLC */
452 rc = gprs_llc_hdr_parse(ghp, llc, llc_len);
453 gprs_llc_hdr_dump(ghp);
454 if (rc != 0) {
455 LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n");
456 return 0;
457 }
458
459 fcs = gprs_llc_fcs(llc, ghp->crc_length);
460 LOGP(DLLC, LOGL_DEBUG, "Got LLC message, CRC: %06x (computed %06x)\n",
461 ghp->fcs, fcs);
462
463 if (!ghp->data)
464 return 0;
465
466 if (ghp->sapi != GPRS_SAPI_GMM)
467 return 1;
468
469 if (ghp->cmd != GPRS_LLC_UI)
470 return 1;
471
472 if (ghp->is_encrypted) {
473 parse_ctx->need_decryption = 1;
474 return 0;
475 }
476
477 return gprs_gb_parse_dtap(ghp->data, ghp->data_len, parse_ctx);
478}
479
480int gprs_gb_parse_bssgp(uint8_t *bssgp, size_t bssgp_len,
481 struct gprs_gb_parse_context *parse_ctx)
482{
483 struct bssgp_normal_hdr *bgph;
484 struct bssgp_ud_hdr *budh = NULL;
485 struct tlv_parsed *tp = &parse_ctx->bssgp_tp;
486 uint8_t pdu_type;
487 uint8_t *data;
488 size_t data_len;
489 int rc;
490
491 if (bssgp_len < sizeof(struct bssgp_normal_hdr))
492 return 0;
493
494 bgph = (struct bssgp_normal_hdr *)bssgp;
495 pdu_type = bgph->pdu_type;
496
497 if (pdu_type == BSSGP_PDUT_UL_UNITDATA ||
498 pdu_type == BSSGP_PDUT_DL_UNITDATA) {
499 if (bssgp_len < sizeof(struct bssgp_ud_hdr))
500 return 0;
501 budh = (struct bssgp_ud_hdr *)bssgp;
502 bgph = NULL;
503 data = budh->data;
504 data_len = bssgp_len - sizeof(*budh);
505 } else {
506 data = bgph->data;
507 data_len = bssgp_len - sizeof(*bgph);
508 }
509
510 if (bssgp_tlv_parse(tp, data, data_len) < 0)
511 return 0;
512
513 parse_ctx->pdu_type = pdu_type;
514 parse_ctx->bud_hdr = budh;
515 parse_ctx->bgp_hdr = bgph;
516 parse_ctx->bssgp_data = data;
517 parse_ctx->bssgp_data_len = data_len;
518
519 if (budh)
520 parse_ctx->tlli_enc = (uint8_t *)&budh->tlli;
521
522 if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA))
523 parse_ctx->bssgp_raid_enc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA);
524
525 if (TLVP_PRESENT(tp, BSSGP_IE_CELL_ID))
526 parse_ctx->bssgp_raid_enc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_CELL_ID);
527
528 if (TLVP_PRESENT(tp, BSSGP_IE_IMSI)) {
529 parse_ctx->imsi = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_IMSI);
530 parse_ctx->imsi_len = TLVP_LEN(tp, BSSGP_IE_IMSI);
531 }
532
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200533 if (TLVP_PRESENT(tp, BSSGP_IE_TLLI)) {
534 if (parse_ctx->tlli_enc)
535 /* This is TLLI old, don't confuse it with TLLI current */
536 parse_ctx->old_tlli_enc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_TLLI);
537 else
538 parse_ctx->tlli_enc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_TLLI);
539 }
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200540
541 if (TLVP_PRESENT(tp, BSSGP_IE_TMSI) && pdu_type == BSSGP_PDUT_PAGING_PS)
542 parse_ctx->ptmsi_enc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_TMSI);
543
544 if (TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) {
545 uint8_t *llc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_LLC_PDU);
546 size_t llc_len = TLVP_LEN(tp, BSSGP_IE_LLC_PDU);
547
548 rc = gprs_gb_parse_llc(llc, llc_len, parse_ctx);
549 if (!rc)
550 return 0;
551
552 parse_ctx->llc = llc;
553 parse_ctx->llc_len = llc_len;
554 }
555
556 if (parse_ctx->tlli_enc) {
557 uint32_t tmp_tlli;
558 memcpy(&tmp_tlli, parse_ctx->tlli_enc, sizeof(tmp_tlli));
559 parse_ctx->tlli = ntohl(tmp_tlli);
560 }
561
562 return 1;
563}
564
565void gprs_gb_log_parse_context(struct gprs_gb_parse_context *parse_ctx,
566 const char *default_msg_name)
567{
568 const char *msg_name = default_msg_name;
569 const char *sep = "";
570
571 if (!parse_ctx->tlli_enc &&
572 !parse_ctx->ptmsi_enc &&
573 !parse_ctx->new_ptmsi_enc &&
574 !parse_ctx->imsi)
575 return;
576
577 if (parse_ctx->llc_msg_name)
578 msg_name = parse_ctx->llc_msg_name;
579
580 LOGP(DGPRS, LOGL_DEBUG, "%s: Got", msg_name);
581
582 if (parse_ctx->tlli_enc) {
583 LOGP(DGPRS, LOGL_DEBUG, "%s TLLI %08x", sep, parse_ctx->tlli);
584 sep = ",";
585 }
586
Jacob Erlbeck4b663ac2014-08-21 15:07:11 +0200587 if (parse_ctx->old_tlli_enc) {
588 LOGP(DGPRS, LOGL_DEBUG, "%s old TLLI %02x%02x%02x%02x", sep,
589 parse_ctx->old_tlli_enc[0],
590 parse_ctx->old_tlli_enc[1],
591 parse_ctx->old_tlli_enc[2],
592 parse_ctx->old_tlli_enc[3]);
593 sep = ",";
594 }
595
Jacob Erlbeck9114bee2014-08-19 12:21:01 +0200596 if (parse_ctx->bssgp_raid_enc) {
597 struct gprs_ra_id raid;
598 gsm48_parse_ra(&raid, parse_ctx->bssgp_raid_enc);
599 LOGP(DGPRS, LOGL_DEBUG, "%s BSSGP RAID %u-%u-%u-%u", sep,
600 raid.mcc, raid.mnc, raid.lac, raid.rac);
601 sep = ",";
602 }
603
604 if (parse_ctx->raid_enc) {
605 struct gprs_ra_id raid;
606 gsm48_parse_ra(&raid, parse_ctx->raid_enc);
607 LOGP(DGPRS, LOGL_DEBUG, "%s RAID %u-%u-%u-%u", sep,
608 raid.mcc, raid.mnc, raid.lac, raid.rac);
609 sep = ",";
610 }
611
612 if (parse_ctx->old_raid_enc) {
613 struct gprs_ra_id raid;
614 gsm48_parse_ra(&raid, parse_ctx->old_raid_enc);
615 LOGP(DGPRS, LOGL_DEBUG, "%s old RAID %u-%u-%u-%u", sep,
616 raid.mcc, raid.mnc, raid.lac, raid.rac);
617 sep = ",";
618 }
619
620 if (parse_ctx->ptmsi_enc) {
621 uint32_t ptmsi = GSM_RESERVED_TMSI;
622 int ok;
623 ok = gprs_parse_mi_tmsi(parse_ctx->ptmsi_enc, GSM48_TMSI_LEN, &ptmsi);
624 LOGP(DGPRS, LOGL_DEBUG, "%s PTMSI %08x%s",
625 sep, ptmsi, ok ? "" : " (parse error)");
626 sep = ",";
627 }
628
629 if (parse_ctx->new_ptmsi_enc) {
630 uint32_t new_ptmsi = GSM_RESERVED_TMSI;
631 int ok;
632 ok = gprs_parse_mi_tmsi(parse_ctx->new_ptmsi_enc, GSM48_TMSI_LEN,
633 &new_ptmsi);
634 LOGP(DGPRS, LOGL_DEBUG, "%s new PTMSI %08x%s",
635 sep, new_ptmsi, ok ? "" : " (parse error)");
636 sep = ",";
637 }
638
639 if (parse_ctx->imsi) {
640 char mi_buf[200];
641 mi_buf[0] = '\0';
642 gsm48_mi_to_string(mi_buf, sizeof(mi_buf),
643 parse_ctx->imsi, parse_ctx->imsi_len);
644 LOGP(DGPRS, LOGL_DEBUG, "%s IMSI %s",
645 sep, mi_buf);
646 sep = ",";
647 }
648 if (parse_ctx->invalidate_tlli) {
649 LOGP(DGPRS, LOGL_DEBUG, "%s invalidate", sep);
650 sep = ",";
651 }
652
653 LOGP(DGPRS, LOGL_DEBUG, "\n");
654}
655