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