Jacob Erlbeck | 9114bee | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 1 | /* 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 Welte | 6e68808 | 2014-08-24 17:38:18 +0200 | [diff] [blame] | 21 | #include <osmocom/gsm/gsm48.h> |
| 22 | |
Jacob Erlbeck | 9114bee | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 23 | #include <openbsc/gprs_gb_parse.h> |
| 24 | |
| 25 | #include <openbsc/gprs_utils.h> |
| 26 | |
| 27 | #include <openbsc/gsm_04_08_gprs.h> |
Jacob Erlbeck | 9114bee | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 28 | #include <openbsc/debug.h> |
| 29 | |
| 30 | #include <osmocom/gprs/gprs_bssgp.h> |
| 31 | |
| 32 | /* TODO: Move shift functions to libosmocore */ |
| 33 | |
| 34 | int 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 | |
| 48 | fail: |
| 49 | *data += *data_len; |
| 50 | *data_len = 0; |
| 51 | return -1; |
| 52 | } |
| 53 | |
| 54 | int 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 | |
| 78 | fail: |
| 79 | *data += *data_len; |
| 80 | *data_len = 0; |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | int 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 | |
| 112 | fail: |
| 113 | *data += *data_len; |
| 114 | *data_len = 0; |
| 115 | return -1; |
| 116 | } |
| 117 | |
| 118 | int 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 | |
| 142 | fail: |
| 143 | *data += *data_len; |
| 144 | *data_len = 0; |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | static 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 | |
| 188 | static 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 | |
| 222 | static 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; |
Jacob Erlbeck | 9114bee | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 227 | int power_off; |
| 228 | |
| 229 | parse_ctx->llc_msg_name = "DETACH_REQ"; |
| 230 | |
| 231 | /* Skip spare half octet */ |
| 232 | /* Get Detach type */ |
| 233 | if (v_fixed_shift(&data, &data_len, 1, &value) <= 0) |
| 234 | /* invalid */ |
| 235 | return 0; |
| 236 | |
Jacob Erlbeck | 9114bee | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 237 | power_off = *value & 0x08 ? 1 : 0; |
| 238 | |
| 239 | if (!parse_ctx->to_bss) { |
| 240 | /* Mobile originated */ |
| 241 | |
| 242 | if (power_off) |
| 243 | parse_ctx->invalidate_tlli = 1; |
| 244 | |
| 245 | /* Get P-TMSI (Mobile identity), see GSM 24.008, 9.4.5.2 */ |
| 246 | if (tlv_match(&data, &data_len, |
| 247 | GSM48_IE_GMM_ALLOC_PTMSI, &value, &value_len) > 0) |
| 248 | { |
| 249 | if (gprs_is_mi_tmsi(value, value_len)) |
| 250 | parse_ctx->ptmsi_enc = value; |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | return 1; |
| 255 | } |
| 256 | |
| 257 | static int gprs_gb_parse_gmm_ra_upd_req(uint8_t *data, size_t data_len, |
| 258 | struct gprs_gb_parse_context *parse_ctx) |
| 259 | { |
| 260 | uint8_t *value; |
| 261 | |
| 262 | parse_ctx->llc_msg_name = "RA_UPD_REQ"; |
| 263 | |
| 264 | /* Skip Update type */ |
| 265 | /* Skip GPRS ciphering key sequence number */ |
| 266 | v_fixed_shift(&data, &data_len, 1, NULL); |
| 267 | |
| 268 | if (v_fixed_shift(&data, &data_len, 6, &value) <= 0) |
| 269 | return 0; |
| 270 | |
| 271 | parse_ctx->old_raid_enc = value; |
| 272 | |
| 273 | return 1; |
| 274 | } |
| 275 | |
| 276 | static int gprs_gb_parse_gmm_ra_upd_ack(uint8_t *data, size_t data_len, |
| 277 | struct gprs_gb_parse_context *parse_ctx) |
| 278 | { |
| 279 | uint8_t *value; |
| 280 | size_t value_len; |
| 281 | |
| 282 | parse_ctx->llc_msg_name = "RA_UPD_ACK"; |
| 283 | |
| 284 | /* Skip Force to standby */ |
| 285 | /* Skip Update result */ |
| 286 | /* Skip Periodic RA update timer */ |
| 287 | v_fixed_shift(&data, &data_len, 2, NULL); |
| 288 | |
| 289 | if (v_fixed_shift(&data, &data_len, 6, &value) <= 0) |
| 290 | return 0; |
| 291 | |
| 292 | parse_ctx->raid_enc = value; |
| 293 | |
| 294 | /* Skip P-TMSI signature (P-TMSI signature, opt, TV, length 4) */ |
| 295 | tv_fixed_match(&data, &data_len, GSM48_IE_GMM_PTMSI_SIG, 3, NULL); |
| 296 | |
| 297 | /* Allocated P-TMSI (Mobile identity, opt, TLV, length 7) */ |
| 298 | if (tlv_match(&data, &data_len, GSM48_IE_GMM_ALLOC_PTMSI, |
| 299 | &value, &value_len) > 0 && |
| 300 | gprs_is_mi_tmsi(value, value_len)) |
| 301 | parse_ctx->new_ptmsi_enc = value; |
| 302 | |
| 303 | return 1; |
| 304 | } |
| 305 | |
| 306 | static int gprs_gb_parse_gmm_ptmsi_reall_cmd(uint8_t *data, size_t data_len, |
| 307 | struct gprs_gb_parse_context *parse_ctx) |
| 308 | { |
| 309 | uint8_t *value; |
| 310 | size_t value_len; |
| 311 | |
| 312 | parse_ctx->llc_msg_name = "PTMSI_REALL_CMD"; |
| 313 | |
| 314 | LOGP(DLLC, LOGL_NOTICE, |
| 315 | "Got P-TMSI Reallocation Command which is not covered by unit tests yet.\n"); |
| 316 | |
| 317 | /* Allocated P-TMSI */ |
| 318 | if (lv_shift(&data, &data_len, &value, &value_len) > 0 && |
| 319 | gprs_is_mi_tmsi(value, value_len)) |
| 320 | parse_ctx->new_ptmsi_enc = value; |
| 321 | |
| 322 | if (v_fixed_shift(&data, &data_len, 6, &value) <= 0) |
| 323 | return 0; |
| 324 | |
| 325 | parse_ctx->raid_enc = value; |
| 326 | |
| 327 | return 1; |
| 328 | } |
| 329 | |
| 330 | static int gprs_gb_parse_gmm_id_resp(uint8_t *data, size_t data_len, |
| 331 | struct gprs_gb_parse_context *parse_ctx) |
| 332 | { |
| 333 | uint8_t *value; |
| 334 | size_t value_len; |
| 335 | |
| 336 | parse_ctx->llc_msg_name = "ID_RESP"; |
| 337 | |
| 338 | /* Mobile identity, Mobile identity 10.5.1.4, M LV 2-10 */ |
| 339 | if (lv_shift(&data, &data_len, &value, &value_len) <= 0 || |
| 340 | value_len < 1 || value_len > 9) |
| 341 | /* invalid */ |
| 342 | return 0; |
| 343 | |
| 344 | if (gprs_is_mi_tmsi(value, value_len)) { |
| 345 | parse_ctx->ptmsi_enc = value; |
| 346 | } else if (gprs_is_mi_imsi(value, value_len)) { |
| 347 | parse_ctx->imsi = value; |
| 348 | parse_ctx->imsi_len = value_len; |
| 349 | } |
| 350 | |
| 351 | return 1; |
| 352 | } |
| 353 | |
| 354 | static int gprs_gb_parse_gsm_act_pdp_req(uint8_t *data, size_t data_len, |
| 355 | struct gprs_gb_parse_context *parse_ctx) |
| 356 | { |
| 357 | ssize_t old_len; |
| 358 | uint8_t *value; |
| 359 | size_t value_len; |
| 360 | |
| 361 | parse_ctx->llc_msg_name = "ACT_PDP_REQ"; |
| 362 | |
| 363 | /* Skip Requested NSAPI */ |
| 364 | /* Skip Requested LLC SAPI */ |
| 365 | v_fixed_shift(&data, &data_len, 2, NULL); |
| 366 | |
| 367 | /* Skip Requested QoS (support 04.08 and 24.008) */ |
| 368 | if (lv_shift(&data, &data_len, NULL, &value_len) <= 0 || |
| 369 | value_len < 4 || value_len > 14) |
| 370 | /* invalid */ |
| 371 | return 0;; |
| 372 | |
| 373 | /* Skip Requested PDP address */ |
| 374 | if (lv_shift(&data, &data_len, NULL, &value_len) <= 0 || |
| 375 | value_len < 2 || value_len > 18) |
| 376 | /* invalid */ |
| 377 | return 0; |
| 378 | |
| 379 | /* Access point name */ |
| 380 | old_len = tlv_match(&data, &data_len, |
| 381 | GSM48_IE_GSM_APN, &value, &value_len); |
| 382 | |
| 383 | if (old_len > 0 && value_len >=1 && value_len <= 100) { |
| 384 | parse_ctx->apn_ie = data - old_len; |
| 385 | parse_ctx->apn_ie_len = old_len; |
| 386 | } |
| 387 | |
| 388 | return 1; |
| 389 | } |
| 390 | |
| 391 | int gprs_gb_parse_dtap(uint8_t *data, size_t data_len, |
| 392 | struct gprs_gb_parse_context *parse_ctx) |
| 393 | { |
| 394 | struct gsm48_hdr *g48h; |
| 395 | |
| 396 | if (v_fixed_shift(&data, &data_len, sizeof(*g48h), (uint8_t **)&g48h) <= 0) |
| 397 | return 0; |
| 398 | |
| 399 | parse_ctx->g48_hdr = g48h; |
| 400 | |
| 401 | if ((g48h->proto_discr & 0x0f) != GSM48_PDISC_MM_GPRS && |
| 402 | (g48h->proto_discr & 0x0f) != GSM48_PDISC_SM_GPRS) |
| 403 | return 1; |
| 404 | |
| 405 | switch (g48h->msg_type) { |
| 406 | case GSM48_MT_GMM_ATTACH_REQ: |
| 407 | return gprs_gb_parse_gmm_attach_req(data, data_len, parse_ctx); |
| 408 | |
| 409 | case GSM48_MT_GMM_ATTACH_ACK: |
| 410 | return gprs_gb_parse_gmm_attach_ack(data, data_len, parse_ctx); |
| 411 | |
| 412 | case GSM48_MT_GMM_RA_UPD_REQ: |
| 413 | return gprs_gb_parse_gmm_ra_upd_req(data, data_len, parse_ctx); |
| 414 | |
| 415 | case GSM48_MT_GMM_RA_UPD_ACK: |
| 416 | return gprs_gb_parse_gmm_ra_upd_ack(data, data_len, parse_ctx); |
| 417 | |
| 418 | case GSM48_MT_GMM_PTMSI_REALL_CMD: |
| 419 | return gprs_gb_parse_gmm_ptmsi_reall_cmd(data, data_len, parse_ctx); |
| 420 | |
| 421 | case GSM48_MT_GSM_ACT_PDP_REQ: |
| 422 | return gprs_gb_parse_gsm_act_pdp_req(data, data_len, parse_ctx); |
| 423 | |
| 424 | case GSM48_MT_GMM_ID_RESP: |
| 425 | return gprs_gb_parse_gmm_id_resp(data, data_len, parse_ctx); |
| 426 | |
| 427 | case GSM48_MT_GMM_DETACH_REQ: |
| 428 | return gprs_gb_parse_gmm_detach_req(data, data_len, parse_ctx); |
| 429 | |
| 430 | case GSM48_MT_GMM_DETACH_ACK: |
| 431 | parse_ctx->llc_msg_name = "DETACH_ACK"; |
| 432 | parse_ctx->invalidate_tlli = 1; |
| 433 | break; |
| 434 | |
| 435 | default: |
| 436 | break; |
| 437 | }; |
| 438 | |
| 439 | return 1; |
| 440 | } |
| 441 | |
| 442 | int gprs_gb_parse_llc(uint8_t *llc, size_t llc_len, |
| 443 | struct gprs_gb_parse_context *parse_ctx) |
| 444 | { |
| 445 | struct gprs_llc_hdr_parsed *ghp = &parse_ctx->llc_hdr_parsed; |
| 446 | int rc; |
| 447 | int fcs; |
| 448 | |
| 449 | /* parse LLC */ |
| 450 | rc = gprs_llc_hdr_parse(ghp, llc, llc_len); |
| 451 | gprs_llc_hdr_dump(ghp); |
| 452 | if (rc != 0) { |
| 453 | LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n"); |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | fcs = gprs_llc_fcs(llc, ghp->crc_length); |
| 458 | LOGP(DLLC, LOGL_DEBUG, "Got LLC message, CRC: %06x (computed %06x)\n", |
| 459 | ghp->fcs, fcs); |
| 460 | |
| 461 | if (!ghp->data) |
| 462 | return 0; |
| 463 | |
| 464 | if (ghp->sapi != GPRS_SAPI_GMM) |
| 465 | return 1; |
| 466 | |
| 467 | if (ghp->cmd != GPRS_LLC_UI) |
| 468 | return 1; |
| 469 | |
| 470 | if (ghp->is_encrypted) { |
| 471 | parse_ctx->need_decryption = 1; |
| 472 | return 0; |
| 473 | } |
| 474 | |
| 475 | return gprs_gb_parse_dtap(ghp->data, ghp->data_len, parse_ctx); |
| 476 | } |
| 477 | |
| 478 | int gprs_gb_parse_bssgp(uint8_t *bssgp, size_t bssgp_len, |
| 479 | struct gprs_gb_parse_context *parse_ctx) |
| 480 | { |
| 481 | struct bssgp_normal_hdr *bgph; |
| 482 | struct bssgp_ud_hdr *budh = NULL; |
| 483 | struct tlv_parsed *tp = &parse_ctx->bssgp_tp; |
| 484 | uint8_t pdu_type; |
| 485 | uint8_t *data; |
| 486 | size_t data_len; |
| 487 | int rc; |
| 488 | |
| 489 | if (bssgp_len < sizeof(struct bssgp_normal_hdr)) |
| 490 | return 0; |
| 491 | |
| 492 | bgph = (struct bssgp_normal_hdr *)bssgp; |
| 493 | pdu_type = bgph->pdu_type; |
| 494 | |
| 495 | if (pdu_type == BSSGP_PDUT_UL_UNITDATA || |
| 496 | pdu_type == BSSGP_PDUT_DL_UNITDATA) { |
| 497 | if (bssgp_len < sizeof(struct bssgp_ud_hdr)) |
| 498 | return 0; |
| 499 | budh = (struct bssgp_ud_hdr *)bssgp; |
| 500 | bgph = NULL; |
| 501 | data = budh->data; |
| 502 | data_len = bssgp_len - sizeof(*budh); |
| 503 | } else { |
| 504 | data = bgph->data; |
| 505 | data_len = bssgp_len - sizeof(*bgph); |
| 506 | } |
| 507 | |
| 508 | if (bssgp_tlv_parse(tp, data, data_len) < 0) |
| 509 | return 0; |
| 510 | |
| 511 | parse_ctx->pdu_type = pdu_type; |
| 512 | parse_ctx->bud_hdr = budh; |
| 513 | parse_ctx->bgp_hdr = bgph; |
| 514 | parse_ctx->bssgp_data = data; |
| 515 | parse_ctx->bssgp_data_len = data_len; |
| 516 | |
| 517 | if (budh) |
| 518 | parse_ctx->tlli_enc = (uint8_t *)&budh->tlli; |
| 519 | |
| 520 | if (TLVP_PRESENT(tp, BSSGP_IE_ROUTEING_AREA)) |
| 521 | parse_ctx->bssgp_raid_enc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_ROUTEING_AREA); |
| 522 | |
| 523 | if (TLVP_PRESENT(tp, BSSGP_IE_CELL_ID)) |
| 524 | parse_ctx->bssgp_raid_enc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_CELL_ID); |
| 525 | |
| 526 | if (TLVP_PRESENT(tp, BSSGP_IE_IMSI)) { |
| 527 | parse_ctx->imsi = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_IMSI); |
| 528 | parse_ctx->imsi_len = TLVP_LEN(tp, BSSGP_IE_IMSI); |
| 529 | } |
| 530 | |
Jacob Erlbeck | 4b663ac | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 531 | if (TLVP_PRESENT(tp, BSSGP_IE_TLLI)) { |
| 532 | if (parse_ctx->tlli_enc) |
| 533 | /* This is TLLI old, don't confuse it with TLLI current */ |
| 534 | parse_ctx->old_tlli_enc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_TLLI); |
| 535 | else |
| 536 | parse_ctx->tlli_enc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_TLLI); |
| 537 | } |
Jacob Erlbeck | 9114bee | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 538 | |
| 539 | if (TLVP_PRESENT(tp, BSSGP_IE_TMSI) && pdu_type == BSSGP_PDUT_PAGING_PS) |
| 540 | parse_ctx->ptmsi_enc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_TMSI); |
| 541 | |
| 542 | if (TLVP_PRESENT(tp, BSSGP_IE_LLC_PDU)) { |
| 543 | uint8_t *llc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_LLC_PDU); |
| 544 | size_t llc_len = TLVP_LEN(tp, BSSGP_IE_LLC_PDU); |
| 545 | |
| 546 | rc = gprs_gb_parse_llc(llc, llc_len, parse_ctx); |
| 547 | if (!rc) |
| 548 | return 0; |
| 549 | |
| 550 | parse_ctx->llc = llc; |
| 551 | parse_ctx->llc_len = llc_len; |
| 552 | } |
| 553 | |
| 554 | if (parse_ctx->tlli_enc) { |
| 555 | uint32_t tmp_tlli; |
| 556 | memcpy(&tmp_tlli, parse_ctx->tlli_enc, sizeof(tmp_tlli)); |
| 557 | parse_ctx->tlli = ntohl(tmp_tlli); |
| 558 | } |
| 559 | |
| 560 | return 1; |
| 561 | } |
| 562 | |
| 563 | void gprs_gb_log_parse_context(struct gprs_gb_parse_context *parse_ctx, |
| 564 | const char *default_msg_name) |
| 565 | { |
| 566 | const char *msg_name = default_msg_name; |
| 567 | const char *sep = ""; |
| 568 | |
| 569 | if (!parse_ctx->tlli_enc && |
| 570 | !parse_ctx->ptmsi_enc && |
| 571 | !parse_ctx->new_ptmsi_enc && |
| 572 | !parse_ctx->imsi) |
| 573 | return; |
| 574 | |
| 575 | if (parse_ctx->llc_msg_name) |
| 576 | msg_name = parse_ctx->llc_msg_name; |
| 577 | |
| 578 | LOGP(DGPRS, LOGL_DEBUG, "%s: Got", msg_name); |
| 579 | |
| 580 | if (parse_ctx->tlli_enc) { |
| 581 | LOGP(DGPRS, LOGL_DEBUG, "%s TLLI %08x", sep, parse_ctx->tlli); |
| 582 | sep = ","; |
| 583 | } |
| 584 | |
Jacob Erlbeck | 4b663ac | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 585 | if (parse_ctx->old_tlli_enc) { |
| 586 | LOGP(DGPRS, LOGL_DEBUG, "%s old TLLI %02x%02x%02x%02x", sep, |
| 587 | parse_ctx->old_tlli_enc[0], |
| 588 | parse_ctx->old_tlli_enc[1], |
| 589 | parse_ctx->old_tlli_enc[2], |
| 590 | parse_ctx->old_tlli_enc[3]); |
| 591 | sep = ","; |
| 592 | } |
| 593 | |
Jacob Erlbeck | 9114bee | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 594 | if (parse_ctx->bssgp_raid_enc) { |
| 595 | struct gprs_ra_id raid; |
| 596 | gsm48_parse_ra(&raid, parse_ctx->bssgp_raid_enc); |
| 597 | LOGP(DGPRS, LOGL_DEBUG, "%s BSSGP RAID %u-%u-%u-%u", sep, |
| 598 | raid.mcc, raid.mnc, raid.lac, raid.rac); |
| 599 | sep = ","; |
| 600 | } |
| 601 | |
| 602 | if (parse_ctx->raid_enc) { |
| 603 | struct gprs_ra_id raid; |
| 604 | gsm48_parse_ra(&raid, parse_ctx->raid_enc); |
| 605 | LOGP(DGPRS, LOGL_DEBUG, "%s RAID %u-%u-%u-%u", sep, |
| 606 | raid.mcc, raid.mnc, raid.lac, raid.rac); |
| 607 | sep = ","; |
| 608 | } |
| 609 | |
| 610 | if (parse_ctx->old_raid_enc) { |
| 611 | struct gprs_ra_id raid; |
| 612 | gsm48_parse_ra(&raid, parse_ctx->old_raid_enc); |
| 613 | LOGP(DGPRS, LOGL_DEBUG, "%s old RAID %u-%u-%u-%u", sep, |
| 614 | raid.mcc, raid.mnc, raid.lac, raid.rac); |
| 615 | sep = ","; |
| 616 | } |
| 617 | |
| 618 | if (parse_ctx->ptmsi_enc) { |
| 619 | uint32_t ptmsi = GSM_RESERVED_TMSI; |
| 620 | int ok; |
| 621 | ok = gprs_parse_mi_tmsi(parse_ctx->ptmsi_enc, GSM48_TMSI_LEN, &ptmsi); |
| 622 | LOGP(DGPRS, LOGL_DEBUG, "%s PTMSI %08x%s", |
| 623 | sep, ptmsi, ok ? "" : " (parse error)"); |
| 624 | sep = ","; |
| 625 | } |
| 626 | |
| 627 | if (parse_ctx->new_ptmsi_enc) { |
| 628 | uint32_t new_ptmsi = GSM_RESERVED_TMSI; |
| 629 | int ok; |
| 630 | ok = gprs_parse_mi_tmsi(parse_ctx->new_ptmsi_enc, GSM48_TMSI_LEN, |
| 631 | &new_ptmsi); |
| 632 | LOGP(DGPRS, LOGL_DEBUG, "%s new PTMSI %08x%s", |
| 633 | sep, new_ptmsi, ok ? "" : " (parse error)"); |
| 634 | sep = ","; |
| 635 | } |
| 636 | |
| 637 | if (parse_ctx->imsi) { |
| 638 | char mi_buf[200]; |
| 639 | mi_buf[0] = '\0'; |
| 640 | gsm48_mi_to_string(mi_buf, sizeof(mi_buf), |
| 641 | parse_ctx->imsi, parse_ctx->imsi_len); |
| 642 | LOGP(DGPRS, LOGL_DEBUG, "%s IMSI %s", |
| 643 | sep, mi_buf); |
| 644 | sep = ","; |
| 645 | } |
| 646 | if (parse_ctx->invalidate_tlli) { |
| 647 | LOGP(DGPRS, LOGL_DEBUG, "%s invalidate", sep); |
| 648 | sep = ","; |
| 649 | } |
| 650 | |
| 651 | LOGP(DGPRS, LOGL_DEBUG, "\n"); |
| 652 | } |
| 653 | |