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 | |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 32 | static 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 Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 41 | if (gprs_shift_lv(&data, &data_len, NULL, &value_len) <= 0 || |
Jacob Erlbeck | abb964f | 2014-09-29 12:36:45 +0200 | [diff] [blame] | 42 | value_len < 1 || value_len > 8) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 43 | /* invalid */ |
Jacob Erlbeck | e8e0f0f | 2014-09-29 12:45:36 +0200 | [diff] [blame] | 44 | return 0; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 45 | |
| 46 | /* Skip Attach type */ |
| 47 | /* Skip Ciphering key sequence number */ |
| 48 | /* Skip DRX parameter */ |
Jacob Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 49 | gprs_shift_v_fixed(&data, &data_len, 3, NULL); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 50 | |
| 51 | /* Get Mobile identity */ |
Jacob Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 52 | if (gprs_shift_lv(&data, &data_len, &value, &value_len) <= 0 || |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 53 | value_len < 5 || value_len > 8) |
| 54 | /* invalid */ |
| 55 | return 0; |
| 56 | |
| 57 | if (gprs_is_mi_tmsi(value, value_len)) { |
Jacob Erlbeck | 17608c6 | 2014-10-02 16:14:47 +0200 | [diff] [blame] | 58 | parse_ctx->ptmsi_enc = value + 1; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 59 | } else if (gprs_is_mi_imsi(value, value_len)) { |
| 60 | parse_ctx->imsi = value; |
| 61 | parse_ctx->imsi_len = value_len; |
| 62 | } |
| 63 | |
Jacob Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 64 | if (gprs_shift_v_fixed(&data, &data_len, 6, &value) <= 0) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 65 | return 0; |
| 66 | |
| 67 | parse_ctx->old_raid_enc = value; |
| 68 | |
| 69 | return 1; |
| 70 | } |
| 71 | |
| 72 | static 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 Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 85 | gprs_shift_v_fixed(&data, &data_len, 3, NULL); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 86 | |
Jacob Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 87 | if (gprs_shift_v_fixed(&data, &data_len, 6, &value) <= 0) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 88 | return 0; |
| 89 | |
| 90 | parse_ctx->raid_enc = value; |
| 91 | |
| 92 | /* Skip P-TMSI signature (P-TMSI signature, opt, TV, length 4) */ |
Jacob Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 93 | gprs_match_tv_fixed(&data, &data_len, GSM48_IE_GMM_PTMSI_SIG, 3, NULL); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 94 | |
| 95 | /* Skip Negotiated READY timer value (GPRS timer, opt, TV, length 2) */ |
Jacob Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 96 | gprs_match_tv_fixed(&data, &data_len, GSM48_IE_GMM_TIMER_READY, 1, NULL); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 97 | |
| 98 | /* Allocated P-TMSI (Mobile identity, opt, TLV, length 7) */ |
Jacob Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 99 | if (gprs_match_tlv(&data, &data_len, GSM48_IE_GMM_ALLOC_PTMSI, |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 100 | &value, &value_len) > 0 && |
| 101 | gprs_is_mi_tmsi(value, value_len)) |
Jacob Erlbeck | 17608c6 | 2014-10-02 16:14:47 +0200 | [diff] [blame] | 102 | parse_ctx->new_ptmsi_enc = value + 1; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 103 | return 1; |
| 104 | } |
| 105 | |
Jacob Erlbeck | 538bee0 | 2014-09-22 10:42:05 +0200 | [diff] [blame] | 106 | static 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 Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 114 | if (gprs_shift_v_fixed(&data, &data_len, 1, &value) <= 0) |
Jacob Erlbeck | 538bee0 | 2014-09-22 10:42:05 +0200 | [diff] [blame] | 115 | return 0; |
| 116 | |
| 117 | parse_ctx->invalidate_tlli = 1; |
| 118 | |
| 119 | return 1; |
| 120 | } |
| 121 | |
| 122 | |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 123 | static 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 Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 128 | int detach_type; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 129 | int power_off; |
| 130 | |
| 131 | parse_ctx->llc_msg_name = "DETACH_REQ"; |
| 132 | |
| 133 | /* Skip spare half octet */ |
| 134 | /* Get Detach type */ |
Jacob Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 135 | if (gprs_shift_v_fixed(&data, &data_len, 1, &value) <= 0) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 136 | /* invalid */ |
| 137 | return 0; |
| 138 | |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 139 | detach_type = *value & 0x07; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 140 | power_off = *value & 0x08 ? 1 : 0; |
| 141 | |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 142 | 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 Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 147 | /* 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 Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 153 | if (gprs_match_tlv(&data, &data_len, |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 154 | GSM48_IE_GMM_ALLOC_PTMSI, &value, &value_len) > 0) |
| 155 | { |
| 156 | if (gprs_is_mi_tmsi(value, value_len)) |
Jacob Erlbeck | 17608c6 | 2014-10-02 16:14:47 +0200 | [diff] [blame] | 157 | parse_ctx->ptmsi_enc = value + 1; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
| 161 | return 1; |
| 162 | } |
| 163 | |
| 164 | static 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 Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 173 | gprs_shift_v_fixed(&data, &data_len, 1, NULL); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 174 | |
Jacob Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 175 | if (gprs_shift_v_fixed(&data, &data_len, 6, &value) <= 0) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 176 | return 0; |
| 177 | |
| 178 | parse_ctx->old_raid_enc = value; |
| 179 | |
| 180 | return 1; |
| 181 | } |
| 182 | |
Jacob Erlbeck | 4581f29 | 2014-09-16 12:16:58 +0200 | [diff] [blame] | 183 | static 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 Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 193 | if (gprs_shift_v_fixed(&data, &data_len, 1, &value) <= 0) |
Jacob Erlbeck | 4581f29 | 2014-09-16 12:16:58 +0200 | [diff] [blame] | 194 | return 0; |
| 195 | |
| 196 | cause = value[0]; |
| 197 | |
| 198 | /* Force to standby, 1/2 */ |
| 199 | /* spare bits, 1/2 */ |
Jacob Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 200 | if (gprs_shift_v_fixed(&data, &data_len, 1, &value) <= 0) |
Jacob Erlbeck | 4581f29 | 2014-09-16 12:16:58 +0200 | [diff] [blame] | 201 | 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 Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 213 | static 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 Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 224 | gprs_shift_v_fixed(&data, &data_len, 2, NULL); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 225 | |
Jacob Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 226 | if (gprs_shift_v_fixed(&data, &data_len, 6, &value) <= 0) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 227 | return 0; |
| 228 | |
| 229 | parse_ctx->raid_enc = value; |
| 230 | |
| 231 | /* Skip P-TMSI signature (P-TMSI signature, opt, TV, length 4) */ |
Jacob Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 232 | gprs_match_tv_fixed(&data, &data_len, GSM48_IE_GMM_PTMSI_SIG, 3, NULL); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 233 | |
| 234 | /* Allocated P-TMSI (Mobile identity, opt, TLV, length 7) */ |
Jacob Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 235 | if (gprs_match_tlv(&data, &data_len, GSM48_IE_GMM_ALLOC_PTMSI, |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 236 | &value, &value_len) > 0 && |
| 237 | gprs_is_mi_tmsi(value, value_len)) |
Jacob Erlbeck | 17608c6 | 2014-10-02 16:14:47 +0200 | [diff] [blame] | 238 | parse_ctx->new_ptmsi_enc = value + 1; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 239 | |
| 240 | return 1; |
| 241 | } |
| 242 | |
| 243 | static 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 Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 255 | if (gprs_shift_lv(&data, &data_len, &value, &value_len) > 0 && |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 256 | gprs_is_mi_tmsi(value, value_len)) |
Jacob Erlbeck | 17608c6 | 2014-10-02 16:14:47 +0200 | [diff] [blame] | 257 | parse_ctx->new_ptmsi_enc = value + 1; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 258 | |
Jacob Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 259 | if (gprs_shift_v_fixed(&data, &data_len, 6, &value) <= 0) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 260 | return 0; |
| 261 | |
| 262 | parse_ctx->raid_enc = value; |
| 263 | |
| 264 | return 1; |
| 265 | } |
| 266 | |
| 267 | static 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 Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 276 | if (gprs_shift_lv(&data, &data_len, &value, &value_len) <= 0 || |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 277 | value_len < 1 || value_len > 9) |
| 278 | /* invalid */ |
| 279 | return 0; |
| 280 | |
| 281 | if (gprs_is_mi_tmsi(value, value_len)) { |
Jacob Erlbeck | 17608c6 | 2014-10-02 16:14:47 +0200 | [diff] [blame] | 282 | parse_ctx->ptmsi_enc = value + 1; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 283 | } 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 | |
| 291 | static 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 Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 302 | gprs_shift_v_fixed(&data, &data_len, 2, NULL); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 303 | |
| 304 | /* Skip Requested QoS (support 04.08 and 24.008) */ |
Jacob Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 305 | if (gprs_shift_lv(&data, &data_len, NULL, &value_len) <= 0 || |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 306 | value_len < 4 || value_len > 14) |
| 307 | /* invalid */ |
Jacob Erlbeck | e8e0f0f | 2014-09-29 12:45:36 +0200 | [diff] [blame] | 308 | return 0; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 309 | |
| 310 | /* Skip Requested PDP address */ |
Jacob Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 311 | if (gprs_shift_lv(&data, &data_len, NULL, &value_len) <= 0 || |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 312 | value_len < 2 || value_len > 18) |
| 313 | /* invalid */ |
| 314 | return 0; |
| 315 | |
| 316 | /* Access point name */ |
Jacob Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 317 | old_len = gprs_match_tlv(&data, &data_len, |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 318 | 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 | |
| 328 | int 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 Erlbeck | dce4501 | 2014-12-11 11:01:46 +0100 | [diff] [blame] | 333 | if (gprs_shift_v_fixed(&data, &data_len, sizeof(*g48h), (uint8_t **)&g48h) <= 0) |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 334 | 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 Erlbeck | 538bee0 | 2014-09-22 10:42:05 +0200 | [diff] [blame] | 346 | case GSM48_MT_GMM_ATTACH_REJ: |
| 347 | return gprs_gb_parse_gmm_attach_rej(data, data_len, parse_ctx); |
| 348 | |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 349 | 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 Erlbeck | 4581f29 | 2014-09-16 12:16:58 +0200 | [diff] [blame] | 355 | case GSM48_MT_GMM_RA_UPD_REJ: |
| 356 | return gprs_gb_parse_gmm_ra_upd_rej(data, data_len, parse_ctx); |
| 357 | |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 358 | 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 | |
| 385 | int 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 | |
| 421 | int 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 Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 451 | 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 Erlbeck | c23ac20 | 2014-10-09 12:04:56 +0200 | [diff] [blame] | 457 | if (bssgp_tlv_parse(tp, data, data_len) < 0) |
| 458 | return 0; |
| 459 | |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 460 | 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 Erlbeck | 85a9f54 | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 474 | 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 Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 481 | |
| 482 | if (TLVP_PRESENT(tp, BSSGP_IE_TMSI) && pdu_type == BSSGP_PDUT_PAGING_PS) |
Jacob Erlbeck | d5f24fd | 2014-09-30 13:49:43 +0200 | [diff] [blame] | 483 | parse_ctx->bssgp_ptmsi_enc = (uint8_t *)TLVP_VAL(tp, BSSGP_IE_TMSI); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 484 | |
| 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 Erlbeck | 550898a | 2014-09-11 15:22:18 +0200 | [diff] [blame] | 503 | 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 Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 507 | return 1; |
| 508 | } |
| 509 | |
Jacob Erlbeck | 42272c1 | 2014-10-09 12:16:17 +0200 | [diff] [blame] | 510 | void gprs_gb_log_parse_context(int log_level, |
| 511 | struct gprs_gb_parse_context *parse_ctx, |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 512 | const char *default_msg_name) |
| 513 | { |
Jacob Erlbeck | c23ac20 | 2014-10-09 12:04:56 +0200 | [diff] [blame] | 514 | const char *msg_name; |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 515 | const char *sep = ""; |
| 516 | |
| 517 | if (!parse_ctx->tlli_enc && |
| 518 | !parse_ctx->ptmsi_enc && |
| 519 | !parse_ctx->new_ptmsi_enc && |
Jacob Erlbeck | d5f24fd | 2014-09-30 13:49:43 +0200 | [diff] [blame] | 520 | !parse_ctx->bssgp_ptmsi_enc && |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 521 | !parse_ctx->imsi) |
| 522 | return; |
| 523 | |
Jacob Erlbeck | c23ac20 | 2014-10-09 12:04:56 +0200 | [diff] [blame] | 524 | msg_name = gprs_gb_message_name(parse_ctx, default_msg_name); |
| 525 | |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 526 | if (parse_ctx->llc_msg_name) |
| 527 | msg_name = parse_ctx->llc_msg_name; |
| 528 | |
Jacob Erlbeck | 42272c1 | 2014-10-09 12:16:17 +0200 | [diff] [blame] | 529 | LOGP(DGPRS, log_level, "%s: Got", msg_name); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 530 | |
| 531 | if (parse_ctx->tlli_enc) { |
Jacob Erlbeck | 42272c1 | 2014-10-09 12:16:17 +0200 | [diff] [blame] | 532 | LOGPC(DGPRS, log_level, "%s TLLI %08x", sep, parse_ctx->tlli); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 533 | sep = ","; |
| 534 | } |
| 535 | |
Jacob Erlbeck | 85a9f54 | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 536 | if (parse_ctx->old_tlli_enc) { |
Jacob Erlbeck | 42272c1 | 2014-10-09 12:16:17 +0200 | [diff] [blame] | 537 | LOGPC(DGPRS, log_level, "%s old TLLI %02x%02x%02x%02x", sep, |
Jacob Erlbeck | 85a9f54 | 2014-08-21 15:07:11 +0200 | [diff] [blame] | 538 | 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 Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 545 | if (parse_ctx->bssgp_raid_enc) { |
| 546 | struct gprs_ra_id raid; |
| 547 | gsm48_parse_ra(&raid, parse_ctx->bssgp_raid_enc); |
Jacob Erlbeck | 42272c1 | 2014-10-09 12:16:17 +0200 | [diff] [blame] | 548 | LOGPC(DGPRS, log_level, "%s BSSGP RAID %u-%u-%u-%u", sep, |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 549 | 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 Erlbeck | 42272c1 | 2014-10-09 12:16:17 +0200 | [diff] [blame] | 556 | LOGPC(DGPRS, log_level, "%s RAID %u-%u-%u-%u", sep, |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 557 | 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 Erlbeck | 42272c1 | 2014-10-09 12:16:17 +0200 | [diff] [blame] | 564 | LOGPC(DGPRS, log_level, "%s old RAID %u-%u-%u-%u", sep, |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 565 | raid.mcc, raid.mnc, raid.lac, raid.rac); |
| 566 | sep = ","; |
| 567 | } |
| 568 | |
Jacob Erlbeck | d5f24fd | 2014-09-30 13:49:43 +0200 | [diff] [blame] | 569 | 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 Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 576 | if (parse_ctx->ptmsi_enc) { |
| 577 | uint32_t ptmsi = GSM_RESERVED_TMSI; |
Jacob Erlbeck | 17608c6 | 2014-10-02 16:14:47 +0200 | [diff] [blame] | 578 | gprs_parse_tmsi(parse_ctx->ptmsi_enc, &ptmsi); |
Jacob Erlbeck | 42272c1 | 2014-10-09 12:16:17 +0200 | [diff] [blame] | 579 | LOGPC(DGPRS, log_level, "%s PTMSI %08x", sep, ptmsi); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 580 | sep = ","; |
| 581 | } |
| 582 | |
| 583 | if (parse_ctx->new_ptmsi_enc) { |
| 584 | uint32_t new_ptmsi = GSM_RESERVED_TMSI; |
Jacob Erlbeck | 17608c6 | 2014-10-02 16:14:47 +0200 | [diff] [blame] | 585 | gprs_parse_tmsi(parse_ctx->new_ptmsi_enc, &new_ptmsi); |
Jacob Erlbeck | 42272c1 | 2014-10-09 12:16:17 +0200 | [diff] [blame] | 586 | LOGPC(DGPRS, log_level, "%s new PTMSI %08x", sep, new_ptmsi); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 587 | 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 Erlbeck | 42272c1 | 2014-10-09 12:16:17 +0200 | [diff] [blame] | 595 | LOGPC(DGPRS, log_level, "%s IMSI %s", |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 596 | sep, mi_buf); |
| 597 | sep = ","; |
| 598 | } |
| 599 | if (parse_ctx->invalidate_tlli) { |
Jacob Erlbeck | 42272c1 | 2014-10-09 12:16:17 +0200 | [diff] [blame] | 600 | LOGPC(DGPRS, log_level, "%s invalidate", sep); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 601 | sep = ","; |
| 602 | } |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 603 | if (parse_ctx->await_reattach) { |
Jacob Erlbeck | 42272c1 | 2014-10-09 12:16:17 +0200 | [diff] [blame] | 604 | LOGPC(DGPRS, log_level, "%s re-attach", sep); |
Jacob Erlbeck | f9038d9 | 2014-09-12 15:09:56 +0200 | [diff] [blame] | 605 | sep = ","; |
| 606 | } |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 607 | |
Jacob Erlbeck | 42272c1 | 2014-10-09 12:16:17 +0200 | [diff] [blame] | 608 | LOGPC(DGPRS, log_level, "\n"); |
Jacob Erlbeck | cf11e93 | 2014-08-19 12:21:01 +0200 | [diff] [blame] | 609 | } |
| 610 | |
Jacob Erlbeck | c23ac20 | 2014-10-09 12:04:56 +0200 | [diff] [blame] | 611 | const 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 | } |