Harald Welte | a183a6e | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1 | /* Osmocom Visitor Location Register (VLR) Autentication FSM */ |
| 2 | |
| 3 | /* (C) 2016 by Harald Welte <laforge@gnumonks.org> |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU Affero General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU Affero General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Affero General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | |
| 23 | #include <osmocom/core/fsm.h> |
| 24 | #include <osmocom/core/utils.h> |
| 25 | #include <osmocom/gsm/gsup.h> |
| 26 | #include <openbsc/vlr.h> |
| 27 | #include <openbsc/debug.h> |
| 28 | |
| 29 | #include "vlr_core.h" |
| 30 | #include "vlr_auth_fsm.h" |
| 31 | |
| 32 | #define S(x) (1 << (x)) |
| 33 | |
| 34 | static const struct value_string fsm_auth_event_names[] = { |
| 35 | OSMO_VALUE_STRING(VLR_AUTH_E_START), |
| 36 | OSMO_VALUE_STRING(VLR_AUTH_E_HLR_SAI_ACK), |
| 37 | OSMO_VALUE_STRING(VLR_AUTH_E_HLR_SAI_NACK), |
| 38 | OSMO_VALUE_STRING(VLR_AUTH_E_HLR_SAI_ABORT), |
| 39 | OSMO_VALUE_STRING(VLR_AUTH_E_MS_AUTH_RESP), |
| 40 | OSMO_VALUE_STRING(VLR_AUTH_E_MS_AUTH_FAIL), |
| 41 | OSMO_VALUE_STRING(VLR_AUTH_E_MS_ID_IMSI), |
| 42 | { 0, NULL } |
| 43 | }; |
| 44 | |
| 45 | const struct value_string vlr_auth_fsm_result_names[] = { |
| 46 | OSMO_VALUE_STRING(VLR_AUTH_RES_ABORTED), |
| 47 | OSMO_VALUE_STRING(VLR_AUTH_RES_UNKNOWN_SUBSCR), |
| 48 | OSMO_VALUE_STRING(VLR_AUTH_RES_PROC_ERR), |
| 49 | OSMO_VALUE_STRING(VLR_AUTH_RES_AUTH_FAILED), |
| 50 | OSMO_VALUE_STRING(VLR_AUTH_RES_PASSED), |
| 51 | {0, NULL} |
| 52 | }; |
| 53 | |
| 54 | /* private state of the auth_fsm_instance */ |
| 55 | struct auth_fsm_priv { |
| 56 | struct vlr_subscr *vsub; |
| 57 | bool by_imsi; |
| 58 | bool is_r99; |
| 59 | bool is_utran; |
| 60 | bool auth_requested; |
| 61 | |
| 62 | int auth_tuple_max_use_count; /* see vlr->cfg instead */ |
| 63 | }; |
| 64 | |
| 65 | /*********************************************************************** |
| 66 | * Utility functions |
| 67 | ***********************************************************************/ |
| 68 | |
| 69 | /* Always use either vlr_subscr_get_auth_tuple() or vlr_subscr_has_auth_tuple() |
| 70 | * instead, to ensure proper use count. |
| 71 | * Return an auth tuple with the lowest use_count among the auth tuples. If |
| 72 | * max_use_count >= 0, return NULL if all available auth tuples have a use |
| 73 | * count > max_use_count. If max_use_count is negative, return a currently |
| 74 | * least used auth tuple without enforcing a maximum use count. If there are |
| 75 | * no auth tuples, return NULL. |
| 76 | */ |
| 77 | static struct gsm_auth_tuple * |
| 78 | _vlr_subscr_next_auth_tuple(struct vlr_subscr *vsub, int max_use_count) |
| 79 | { |
| 80 | unsigned int count; |
| 81 | unsigned int idx; |
| 82 | struct gsm_auth_tuple *at = NULL; |
| 83 | unsigned int key_seq = GSM_KEY_SEQ_INVAL; |
| 84 | |
| 85 | if (!vsub) |
| 86 | return NULL; |
| 87 | |
| 88 | if (vsub->last_tuple) |
| 89 | key_seq = vsub->last_tuple->key_seq; |
| 90 | |
| 91 | if (key_seq == GSM_KEY_SEQ_INVAL) |
| 92 | /* Start with 0 after increment modulo array size */ |
| 93 | idx = ARRAY_SIZE(vsub->auth_tuples) - 1; |
| 94 | else |
| 95 | idx = key_seq; |
| 96 | |
| 97 | for (count = ARRAY_SIZE(vsub->auth_tuples); count > 0; count--) { |
| 98 | idx = (idx + 1) % ARRAY_SIZE(vsub->auth_tuples); |
| 99 | |
| 100 | if (vsub->auth_tuples[idx].key_seq == GSM_KEY_SEQ_INVAL) |
| 101 | continue; |
| 102 | |
| 103 | if (!at || vsub->auth_tuples[idx].use_count < at->use_count) |
| 104 | at = &vsub->auth_tuples[idx]; |
| 105 | } |
| 106 | |
| 107 | if (!at || (max_use_count >= 0 && at->use_count > max_use_count)) |
| 108 | return NULL; |
| 109 | |
| 110 | return at; |
| 111 | } |
| 112 | |
| 113 | /* Return an auth tuple and increment its use count. */ |
| 114 | static struct gsm_auth_tuple * |
| 115 | vlr_subscr_get_auth_tuple(struct vlr_subscr *vsub, int max_use_count) |
| 116 | { |
| 117 | struct gsm_auth_tuple *at = _vlr_subscr_next_auth_tuple(vsub, |
| 118 | max_use_count); |
| 119 | if (!at) |
| 120 | return NULL; |
| 121 | at->use_count++; |
| 122 | return at; |
| 123 | } |
| 124 | |
| 125 | /* Return whether an auth tuple with the given max_use_count is available. */ |
| 126 | static bool vlr_subscr_has_auth_tuple(struct vlr_subscr *vsub, |
| 127 | int max_use_count) |
| 128 | { |
| 129 | return _vlr_subscr_next_auth_tuple(vsub, max_use_count) != NULL; |
| 130 | } |
| 131 | |
| 132 | static bool check_auth_resp(struct vlr_subscr *vsub, bool is_r99, |
| 133 | bool is_utran, const uint8_t *res, |
| 134 | uint8_t res_len) |
| 135 | { |
| 136 | struct gsm_auth_tuple *at = vsub->last_tuple; |
| 137 | struct osmo_auth_vector *vec = &at->vec; |
| 138 | bool check_umts; |
| 139 | OSMO_ASSERT(at); |
| 140 | |
| 141 | LOGVSUBP(LOGL_DEBUG, vsub, "received res: %s\n", |
| 142 | osmo_hexdump(res, res_len)); |
| 143 | |
| 144 | /* RES must be present and at least 32bit */ |
| 145 | if (!res || res_len < sizeof(vec->sres)) { |
| 146 | LOGVSUBP(LOGL_NOTICE, vsub, "AUTH RES missing or too short " |
| 147 | "(%u)\n", res_len); |
| 148 | goto out_false; |
| 149 | } |
| 150 | |
| 151 | check_umts = false; |
| 152 | if (is_r99 && (vec->auth_types & OSMO_AUTH_TYPE_UMTS)) { |
| 153 | check_umts = true; |
| 154 | /* We have a R99 capable UE and have a UMTS AKA capable USIM. |
| 155 | * However, the ME may still choose to only perform GSM AKA, as |
| 156 | * long as the bearer is GERAN */ |
| 157 | if (res_len != vec->res_len) { |
| 158 | if (is_utran) { |
| 159 | LOGVSUBP(LOGL_NOTICE, vsub, |
| 160 | "AUTH via UTRAN but " |
| 161 | "res_len(%u) != vec->res_len(%u)\n", |
| 162 | res_len, vec->res_len); |
| 163 | goto out_false; |
| 164 | } |
| 165 | check_umts = false; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if (check_umts) { |
| 170 | if (res_len != vec->res_len |
| 171 | || memcmp(res, vec->res, res_len)) { |
| 172 | LOGVSUBP(LOGL_INFO, vsub, "UMTS AUTH failure:" |
| 173 | " mismatching res (expected res=%s)\n", |
| 174 | osmo_hexdump(vec->res, vec->res_len)); |
| 175 | goto out_false; |
| 176 | } |
| 177 | |
| 178 | LOGVSUBP(LOGL_INFO, vsub, "AUTH established UMTS security" |
| 179 | " context\n"); |
| 180 | vsub->sec_ctx = VLR_SEC_CTX_UMTS; |
| 181 | return true; |
| 182 | } else { |
| 183 | if (res_len != sizeof(vec->sres) |
| 184 | || memcmp(res, vec->sres, sizeof(vec->sres))) { |
| 185 | LOGVSUBP(LOGL_INFO, vsub, "GSM AUTH failure:" |
| 186 | " mismatching sres (expected sres=%s)\n", |
| 187 | osmo_hexdump(vec->sres, sizeof(vec->sres))); |
| 188 | goto out_false; |
| 189 | } |
| 190 | |
| 191 | LOGVSUBP(LOGL_INFO, vsub, "AUTH established GSM security" |
| 192 | " context\n"); |
| 193 | vsub->sec_ctx = VLR_SEC_CTX_GSM; |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | out_false: |
| 198 | vsub->sec_ctx = VLR_SEC_CTX_NONE; |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | static void auth_fsm_onenter_failed(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 203 | { |
| 204 | struct auth_fsm_priv *afp = fi->priv; |
| 205 | struct vlr_subscr *vsub = afp->vsub; |
| 206 | |
| 207 | /* If authentication hasn't even started, e.g. the HLR sent no auth |
| 208 | * info, then we also don't need to tell the HLR about an auth failure. |
| 209 | */ |
| 210 | if (afp->auth_requested) |
| 211 | vlr_subscr_tx_auth_fail_rep(vsub); |
| 212 | } |
| 213 | |
| 214 | static bool is_umts_auth(struct auth_fsm_priv *afp, |
| 215 | uint32_t auth_types) |
| 216 | { |
| 217 | if (!afp->is_r99) |
| 218 | return false; |
| 219 | if (!(auth_types & OSMO_AUTH_TYPE_UMTS)) |
| 220 | return false; |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | /* Terminate the Auth FSM Instance and notify parent */ |
| 225 | static void auth_fsm_term(struct osmo_fsm_inst *fi, enum vlr_auth_fsm_result res) |
| 226 | { |
| 227 | struct auth_fsm_priv *afp = fi->priv; |
| 228 | struct vlr_subscr *vsub = afp->vsub; |
| 229 | |
| 230 | LOGPFSM(fi, "Authentication terminating with result %s\n", |
| 231 | vlr_auth_fsm_result_name(res)); |
| 232 | |
| 233 | /* Do one final state transition (mostly for logging purpose) */ |
| 234 | if (res == VLR_AUTH_RES_PASSED) |
| 235 | osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_AUTHENTICATED, 0, 0); |
| 236 | else |
| 237 | osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_AUTH_FAILED, 0, 0); |
| 238 | |
| 239 | /* return the result to the parent FSM */ |
| 240 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, &res); |
| 241 | vsub->auth_fsm = NULL; |
| 242 | } |
| 243 | |
| 244 | /* back-end function transmitting authentication. Caller ensures we have valid |
| 245 | * tuple */ |
| 246 | static int _vlr_subscr_authenticate(struct osmo_fsm_inst *fi) |
| 247 | { |
| 248 | struct auth_fsm_priv *afp = fi->priv; |
| 249 | struct vlr_subscr *vsub = afp->vsub; |
| 250 | struct gsm_auth_tuple *at; |
| 251 | |
| 252 | /* Caller ensures we have vectors available */ |
| 253 | at = vlr_subscr_get_auth_tuple(vsub, afp->auth_tuple_max_use_count); |
| 254 | if (!at) { |
| 255 | LOGPFSML(fi, LOGL_ERROR, "A previous check ensured that an" |
| 256 | " auth tuple was available, but now there is in fact" |
| 257 | " none.\n"); |
| 258 | auth_fsm_term(fi, VLR_AUTH_RES_PROC_ERR); |
| 259 | return -1; |
| 260 | } |
| 261 | |
| 262 | LOGPFSM(fi, "got auth tuple: use_count=%d key_seq=%d\n", |
| 263 | at->use_count, at->key_seq); |
| 264 | |
| 265 | OSMO_ASSERT(at); |
| 266 | |
| 267 | /* Transmit auth req to subscriber */ |
| 268 | afp->auth_requested = true; |
| 269 | vsub->last_tuple = at; |
| 270 | vsub->vlr->ops.tx_auth_req(vsub->msc_conn_ref, at, |
| 271 | is_umts_auth(afp, at->vec.auth_types)); |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | /*********************************************************************** |
| 276 | * FSM State Action functions |
| 277 | ***********************************************************************/ |
| 278 | |
| 279 | /* Initial State of TS 23.018 AUT_VLR */ |
| 280 | static void auth_fsm_needs_auth(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 281 | { |
| 282 | struct auth_fsm_priv *afp = fi->priv; |
| 283 | struct vlr_subscr *vsub = afp->vsub; |
| 284 | |
| 285 | OSMO_ASSERT(event == VLR_AUTH_E_START); |
| 286 | |
| 287 | /* Start off with the default max_use_count, possibly change that if we |
| 288 | * need to re-use an old tuple. */ |
| 289 | afp->auth_tuple_max_use_count = vsub->vlr->cfg.auth_tuple_max_use_count; |
| 290 | |
| 291 | /* Check if we have vectors available */ |
| 292 | if (!vlr_subscr_has_auth_tuple(vsub, afp->auth_tuple_max_use_count)) { |
| 293 | /* Obtain_Authentication_Sets_VLR */ |
| 294 | vlr_subscr_req_sai(vsub, NULL, NULL); |
| 295 | osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_NEEDS_AUTH_WAIT_AI, |
| 296 | GSM_29002_TIMER_M, 0); |
| 297 | } else { |
| 298 | /* go straight ahead with sending auth request */ |
| 299 | osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_WAIT_RESP, |
| 300 | vlr_timer(vsub->vlr, 3260), 3260); |
| 301 | _vlr_subscr_authenticate(fi); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /* Waiting for Authentication Info from HLR */ |
| 306 | static void auth_fsm_wait_ai(struct osmo_fsm_inst *fi, uint32_t event, |
| 307 | void *data) |
| 308 | { |
| 309 | struct auth_fsm_priv *afp = fi->priv; |
| 310 | struct vlr_subscr *vsub = afp->vsub; |
| 311 | struct osmo_gsup_message *gsup = data; |
| 312 | |
| 313 | if (event == VLR_AUTH_E_HLR_SAI_NACK) |
| 314 | LOGPFSM(fi, "GSUP: rx Auth Info Error cause: %d: %s\n", |
| 315 | gsup->cause, |
| 316 | get_value_string(gsm48_gmm_cause_names, gsup->cause)); |
| 317 | |
| 318 | /* We are in what corresponds to the |
| 319 | * Wait_For_Authentication_Sets state of TS 23.018 OAS_VLR */ |
| 320 | if ((event == VLR_AUTH_E_HLR_SAI_ACK && !gsup->num_auth_vectors) |
| 321 | || (event == VLR_AUTH_E_HLR_SAI_NACK && |
| 322 | gsup->cause != GMM_CAUSE_IMSI_UNKNOWN) |
| 323 | || (event == VLR_AUTH_E_HLR_SAI_ABORT)) { |
| 324 | if (vsub->vlr->cfg.auth_reuse_old_sets_on_error |
| 325 | && vlr_subscr_has_auth_tuple(vsub, -1)) { |
| 326 | /* To re-use an old tuple, disable the max_use_count |
| 327 | * constraint. */ |
| 328 | afp->auth_tuple_max_use_count = -1; |
| 329 | goto pass; |
| 330 | } |
| 331 | /* result = procedure error */ |
| 332 | auth_fsm_term(fi, VLR_AUTH_RES_PROC_ERR); |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | switch (event) { |
| 337 | case VLR_AUTH_E_HLR_SAI_ACK: |
| 338 | vlr_subscr_update_tuples(vsub, gsup); |
| 339 | goto pass; |
| 340 | break; |
| 341 | case VLR_AUTH_E_HLR_SAI_NACK: |
| 342 | auth_fsm_term(fi, |
| 343 | gsup->cause == GMM_CAUSE_IMSI_UNKNOWN? |
| 344 | VLR_AUTH_RES_UNKNOWN_SUBSCR |
| 345 | : VLR_AUTH_RES_PROC_ERR); |
| 346 | break; |
| 347 | } |
| 348 | |
| 349 | return; |
| 350 | pass: |
| 351 | osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_WAIT_RESP, |
| 352 | vlr_timer(vsub->vlr, 3260), 3260); |
| 353 | _vlr_subscr_authenticate(fi); |
| 354 | } |
| 355 | |
| 356 | /* Waiting for Authentication Response from MS */ |
| 357 | static void auth_fsm_wait_auth_resp(struct osmo_fsm_inst *fi, uint32_t event, |
| 358 | void *data) |
| 359 | { |
| 360 | struct auth_fsm_priv *afp = fi->priv; |
| 361 | struct vlr_subscr *vsub = afp->vsub; |
| 362 | struct vlr_instance *vlr = vsub->vlr; |
| 363 | struct vlr_auth_resp_par *par = data; |
| 364 | int rc; |
| 365 | |
| 366 | switch (event) { |
| 367 | case VLR_AUTH_E_MS_AUTH_RESP: |
| 368 | rc = check_auth_resp(vsub, par->is_r99, par->is_utran, |
| 369 | par->res, par->res_len); |
| 370 | if (rc == false) { |
| 371 | if (!afp->by_imsi) { |
| 372 | vlr->ops.tx_id_req(vsub->msc_conn_ref, |
| 373 | GSM_MI_TYPE_IMSI); |
| 374 | osmo_fsm_inst_state_chg(fi, |
| 375 | VLR_SUB_AS_WAIT_ID_IMSI, |
| 376 | vlr_timer(vlr, 3270), 3270); |
| 377 | } else { |
| 378 | auth_fsm_term(fi, VLR_AUTH_RES_AUTH_FAILED); |
| 379 | } |
| 380 | } else { |
| 381 | auth_fsm_term(fi, VLR_AUTH_RES_PASSED); |
| 382 | } |
| 383 | break; |
| 384 | case VLR_AUTH_E_MS_AUTH_FAIL: |
| 385 | if (par->auts) { |
| 386 | /* First failure, start re-sync attempt */ |
| 387 | vlr_subscr_req_sai(vsub, par->auts, |
| 388 | vsub->last_tuple->vec.rand); |
| 389 | osmo_fsm_inst_state_chg(fi, |
| 390 | VLR_SUB_AS_NEEDS_AUTH_WAIT_SAI_RESYNC, |
| 391 | GSM_29002_TIMER_M, 0); |
| 392 | } else |
| 393 | auth_fsm_term(fi, VLR_AUTH_RES_AUTH_FAILED); |
| 394 | break; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | /* Waiting for Authentication Info from HLR (resync case) */ |
| 399 | static void auth_fsm_wait_ai_resync(struct osmo_fsm_inst *fi, |
| 400 | uint32_t event, void *data) |
| 401 | { |
| 402 | struct auth_fsm_priv *afp = fi->priv; |
| 403 | struct vlr_subscr *vsub = afp->vsub; |
| 404 | struct osmo_gsup_message *gsup = data; |
| 405 | |
| 406 | /* We are in what corresponds to the |
| 407 | * Wait_For_Authentication_Sets state of TS 23.018 OAS_VLR */ |
| 408 | if ((event == VLR_AUTH_E_HLR_SAI_ACK && !gsup->num_auth_vectors) || |
| 409 | (event == VLR_AUTH_E_HLR_SAI_NACK && |
| 410 | gsup->cause != GMM_CAUSE_IMSI_UNKNOWN) || |
| 411 | (event == VLR_AUTH_E_HLR_SAI_ABORT)) { |
| 412 | /* result = procedure error */ |
| 413 | auth_fsm_term(fi, VLR_AUTH_RES_PROC_ERR); |
| 414 | } |
| 415 | switch (event) { |
| 416 | case VLR_AUTH_E_HLR_SAI_ACK: |
| 417 | vlr_subscr_update_tuples(vsub, gsup); |
| 418 | goto pass; |
| 419 | break; |
| 420 | case VLR_AUTH_E_HLR_SAI_NACK: |
| 421 | auth_fsm_term(fi, |
| 422 | gsup->cause == GMM_CAUSE_IMSI_UNKNOWN? |
| 423 | VLR_AUTH_RES_UNKNOWN_SUBSCR |
| 424 | : VLR_AUTH_RES_PROC_ERR); |
| 425 | break; |
| 426 | } |
| 427 | |
| 428 | return; |
| 429 | pass: |
| 430 | osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_WAIT_RESP_RESYNC, |
| 431 | vlr_timer(vsub->vlr, 3260), 3260); |
| 432 | _vlr_subscr_authenticate(fi); |
| 433 | } |
| 434 | |
| 435 | /* Waiting for AUTH RESP from MS (re-sync case) */ |
| 436 | static void auth_fsm_wait_auth_resp_resync(struct osmo_fsm_inst *fi, |
| 437 | uint32_t event, void *data) |
| 438 | { |
| 439 | struct auth_fsm_priv *afp = fi->priv; |
| 440 | struct vlr_subscr *vsub = afp->vsub; |
| 441 | struct vlr_auth_resp_par *par = data; |
| 442 | struct vlr_instance *vlr = vsub->vlr; |
| 443 | int rc; |
| 444 | |
| 445 | switch (event) { |
| 446 | case VLR_AUTH_E_MS_AUTH_RESP: |
| 447 | rc = check_auth_resp(vsub, par->is_r99, par->is_utran, |
| 448 | par->res, par->res_len); |
| 449 | if (rc == false) { |
| 450 | if (!afp->by_imsi) { |
| 451 | vlr->ops.tx_id_req(vsub->msc_conn_ref, |
| 452 | GSM_MI_TYPE_IMSI); |
| 453 | osmo_fsm_inst_state_chg(fi, |
| 454 | VLR_SUB_AS_WAIT_ID_IMSI, |
| 455 | vlr_timer(vlr, 3270), 3270); |
| 456 | } else { |
| 457 | /* Result = Aborted */ |
| 458 | auth_fsm_term(fi, VLR_AUTH_RES_ABORTED); |
| 459 | } |
| 460 | } else { |
| 461 | /* Result = Pass */ |
| 462 | auth_fsm_term(fi, VLR_AUTH_RES_PASSED); |
| 463 | } |
| 464 | break; |
| 465 | case VLR_AUTH_E_MS_AUTH_FAIL: |
| 466 | /* Second failure: Result = Fail */ |
| 467 | auth_fsm_term(fi, VLR_AUTH_RES_AUTH_FAILED); |
| 468 | break; |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | /* AUT_VLR waiting for Obtain_IMSI_VLR result */ |
| 473 | static void auth_fsm_wait_imsi(struct osmo_fsm_inst *fi, uint32_t event, |
| 474 | void *data) |
| 475 | { |
| 476 | struct auth_fsm_priv *afp = fi->priv; |
| 477 | struct vlr_subscr *vsub = afp->vsub; |
| 478 | const char *mi_string = data; |
| 479 | |
| 480 | switch (event) { |
| 481 | case VLR_AUTH_E_MS_ID_IMSI: |
| 482 | if (vsub->imsi[0] |
| 483 | && !vlr_subscr_matches_imsi(vsub, mi_string)) { |
| 484 | LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:" |
| 485 | " %s\n", mi_string); |
| 486 | } else { |
| 487 | strncpy(vsub->imsi, mi_string, sizeof(vsub->imsi)); |
| 488 | vsub->imsi[sizeof(vsub->imsi)-1] = '\0'; |
| 489 | } |
| 490 | /* retry with identity=IMSI */ |
| 491 | afp->by_imsi = true; |
| 492 | osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_NEEDS_AUTH, 0, 0); |
| 493 | osmo_fsm_inst_dispatch(fi, VLR_AUTH_E_START, NULL); |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | static const struct osmo_fsm_state auth_fsm_states[] = { |
| 499 | [VLR_SUB_AS_NEEDS_AUTH] = { |
| 500 | .name = OSMO_STRINGIFY(VLR_SUB_AS_NEEDS_AUTH), |
| 501 | .in_event_mask = S(VLR_AUTH_E_START), |
| 502 | .out_state_mask = S(VLR_SUB_AS_NEEDS_AUTH_WAIT_AI) | |
| 503 | S(VLR_SUB_AS_WAIT_RESP), |
| 504 | .action = auth_fsm_needs_auth, |
| 505 | }, |
| 506 | [VLR_SUB_AS_NEEDS_AUTH_WAIT_AI] = { |
| 507 | .name = OSMO_STRINGIFY(VLR_SUB_AS_NEEDS_AUTH_WAIT_AI), |
| 508 | .in_event_mask = S(VLR_AUTH_E_HLR_SAI_ACK) | |
| 509 | S(VLR_AUTH_E_HLR_SAI_NACK), |
| 510 | .out_state_mask = S(VLR_SUB_AS_AUTH_FAILED) | |
| 511 | S(VLR_SUB_AS_WAIT_RESP), |
| 512 | .action = auth_fsm_wait_ai, |
| 513 | }, |
| 514 | [VLR_SUB_AS_WAIT_RESP] = { |
| 515 | .name = OSMO_STRINGIFY(VLR_SUB_AS_WAIT_RESP), |
| 516 | .in_event_mask = S(VLR_AUTH_E_MS_AUTH_RESP) | |
| 517 | S(VLR_AUTH_E_MS_AUTH_FAIL), |
| 518 | .out_state_mask = S(VLR_SUB_AS_WAIT_ID_IMSI) | |
| 519 | S(VLR_SUB_AS_AUTH_FAILED) | |
| 520 | S(VLR_SUB_AS_AUTHENTICATED) | |
| 521 | S(VLR_SUB_AS_NEEDS_AUTH_WAIT_SAI_RESYNC), |
| 522 | .action = auth_fsm_wait_auth_resp, |
| 523 | }, |
| 524 | [VLR_SUB_AS_NEEDS_AUTH_WAIT_SAI_RESYNC] = { |
| 525 | .name = OSMO_STRINGIFY(VLR_SUB_AS_NEEDS_AUTH_WAIT_SAI_RESYNC), |
| 526 | .in_event_mask = S(VLR_AUTH_E_HLR_SAI_ACK) | |
| 527 | S(VLR_AUTH_E_HLR_SAI_NACK), |
| 528 | .out_state_mask = S(VLR_SUB_AS_AUTH_FAILED) | |
| 529 | S(VLR_SUB_AS_WAIT_RESP_RESYNC), |
| 530 | .action = auth_fsm_wait_ai_resync, |
| 531 | }, |
| 532 | [VLR_SUB_AS_WAIT_RESP_RESYNC] = { |
| 533 | .name = OSMO_STRINGIFY(VLR_SUB_AS_WAIT_RESP_RESYNC), |
| 534 | .in_event_mask = S(VLR_AUTH_E_MS_AUTH_RESP) | |
| 535 | S(VLR_AUTH_E_MS_AUTH_FAIL), |
| 536 | .out_state_mask = S(VLR_SUB_AS_AUTH_FAILED) | |
| 537 | S(VLR_SUB_AS_AUTHENTICATED), |
| 538 | .action = auth_fsm_wait_auth_resp_resync, |
| 539 | }, |
| 540 | [VLR_SUB_AS_WAIT_ID_IMSI] = { |
| 541 | .name = OSMO_STRINGIFY(VLR_SUB_AS_WAIT_ID_IMSI), |
| 542 | .in_event_mask = S(VLR_AUTH_E_MS_ID_IMSI), |
| 543 | .out_state_mask = S(VLR_SUB_AS_NEEDS_AUTH), |
| 544 | .action = auth_fsm_wait_imsi, |
| 545 | }, |
| 546 | [VLR_SUB_AS_AUTHENTICATED] = { |
| 547 | .name = OSMO_STRINGIFY(VLR_SUB_AS_AUTHENTICATED), |
| 548 | .in_event_mask = 0, |
| 549 | .out_state_mask = 0, |
| 550 | }, |
| 551 | [VLR_SUB_AS_AUTH_FAILED] = { |
| 552 | .name = OSMO_STRINGIFY(VLR_SUB_AS_AUTH_FAILED), |
| 553 | .in_event_mask = 0, |
| 554 | .out_state_mask = 0, |
| 555 | .onenter = auth_fsm_onenter_failed, |
| 556 | }, |
| 557 | }; |
| 558 | |
| 559 | struct osmo_fsm vlr_auth_fsm = { |
| 560 | .name = "VLR_Authenticate", |
| 561 | .states = auth_fsm_states, |
| 562 | .num_states = ARRAY_SIZE(auth_fsm_states), |
| 563 | .allstate_event_mask = 0, |
| 564 | .allstate_action = NULL, |
| 565 | .log_subsys = DVLR, |
| 566 | .event_names = fsm_auth_event_names, |
| 567 | }; |
| 568 | |
| 569 | /*********************************************************************** |
| 570 | * User API (for SGSN/MSC code) |
| 571 | ***********************************************************************/ |
| 572 | |
| 573 | /* MSC->VLR: Start Procedure Authenticate_VLR (TS 23.012 Ch. 4.1.2.2) */ |
| 574 | struct osmo_fsm_inst *auth_fsm_start(struct vlr_subscr *vsub, |
| 575 | uint32_t log_level, |
| 576 | struct osmo_fsm_inst *parent, |
| 577 | uint32_t parent_term_event, |
| 578 | bool is_r99, |
| 579 | bool is_utran) |
| 580 | { |
| 581 | struct osmo_fsm_inst *fi; |
| 582 | struct auth_fsm_priv *afp; |
| 583 | |
| 584 | fi = osmo_fsm_inst_alloc_child(&vlr_auth_fsm, parent, |
| 585 | parent_term_event); |
| 586 | |
| 587 | |
| 588 | afp = talloc_zero(fi, struct auth_fsm_priv); |
| 589 | if (!afp) { |
| 590 | osmo_fsm_inst_dispatch(parent, parent_term_event, 0); |
| 591 | return NULL; |
| 592 | } |
| 593 | |
| 594 | afp->vsub = vsub; |
| 595 | if (vsub->imsi[0]) |
| 596 | afp->by_imsi = true; |
| 597 | afp->is_r99 = is_r99; |
| 598 | afp->is_utran = is_utran; |
| 599 | fi->priv = afp; |
| 600 | vsub->auth_fsm = fi; |
| 601 | |
| 602 | osmo_fsm_inst_dispatch(fi, VLR_AUTH_E_START, NULL); |
| 603 | |
| 604 | return fi; |
| 605 | } |