Harald Welte | b8b85a1 | 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> |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 26 | #include <osmocom/msc/vlr.h> |
| 27 | #include <osmocom/msc/debug.h> |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 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 | |
Neels Hofmeyr | 33f5341 | 2017-10-29 02:11:18 +0100 | [diff] [blame] | 62 | int auth_tuple_max_reuse_count; /* see vlr->cfg instead */ |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 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 |
Neels Hofmeyr | 33f5341 | 2017-10-29 02:11:18 +0100 | [diff] [blame] | 72 | * max_reuse_count >= 0, return NULL if all available auth tuples have a use |
| 73 | * count > max_reuse_count. If max_reuse_count is negative, return a currently |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 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 * |
Neels Hofmeyr | 33f5341 | 2017-10-29 02:11:18 +0100 | [diff] [blame] | 78 | _vlr_subscr_next_auth_tuple(struct vlr_subscr *vsub, int max_reuse_count) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 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 | |
Neels Hofmeyr | 33f5341 | 2017-10-29 02:11:18 +0100 | [diff] [blame] | 107 | if (!at || (max_reuse_count >= 0 && at->use_count > max_reuse_count)) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 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 * |
Neels Hofmeyr | 33f5341 | 2017-10-29 02:11:18 +0100 | [diff] [blame] | 115 | vlr_subscr_get_auth_tuple(struct vlr_subscr *vsub, int max_reuse_count) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 116 | { |
| 117 | struct gsm_auth_tuple *at = _vlr_subscr_next_auth_tuple(vsub, |
Neels Hofmeyr | 33f5341 | 2017-10-29 02:11:18 +0100 | [diff] [blame] | 118 | max_reuse_count); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 119 | if (!at) |
| 120 | return NULL; |
| 121 | at->use_count++; |
| 122 | return at; |
| 123 | } |
| 124 | |
Neels Hofmeyr | 33f5341 | 2017-10-29 02:11:18 +0100 | [diff] [blame] | 125 | /* Return whether an auth tuple with a matching use_count is available. */ |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 126 | static bool vlr_subscr_has_auth_tuple(struct vlr_subscr *vsub, |
Neels Hofmeyr | 33f5341 | 2017-10-29 02:11:18 +0100 | [diff] [blame] | 127 | int max_reuse_count) |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 128 | { |
Neels Hofmeyr | 33f5341 | 2017-10-29 02:11:18 +0100 | [diff] [blame] | 129 | return _vlr_subscr_next_auth_tuple(vsub, max_reuse_count) != NULL; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 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 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 214 | /* Terminate the Auth FSM Instance and notify parent */ |
| 215 | static void auth_fsm_term(struct osmo_fsm_inst *fi, enum vlr_auth_fsm_result res) |
| 216 | { |
| 217 | struct auth_fsm_priv *afp = fi->priv; |
| 218 | struct vlr_subscr *vsub = afp->vsub; |
| 219 | |
| 220 | LOGPFSM(fi, "Authentication terminating with result %s\n", |
| 221 | vlr_auth_fsm_result_name(res)); |
| 222 | |
| 223 | /* Do one final state transition (mostly for logging purpose) */ |
| 224 | if (res == VLR_AUTH_RES_PASSED) |
| 225 | osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_AUTHENTICATED, 0, 0); |
| 226 | else |
| 227 | osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_AUTH_FAILED, 0, 0); |
| 228 | |
| 229 | /* return the result to the parent FSM */ |
| 230 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, &res); |
| 231 | vsub->auth_fsm = NULL; |
| 232 | } |
| 233 | |
| 234 | /* back-end function transmitting authentication. Caller ensures we have valid |
| 235 | * tuple */ |
| 236 | static int _vlr_subscr_authenticate(struct osmo_fsm_inst *fi) |
| 237 | { |
| 238 | struct auth_fsm_priv *afp = fi->priv; |
| 239 | struct vlr_subscr *vsub = afp->vsub; |
| 240 | struct gsm_auth_tuple *at; |
| 241 | |
| 242 | /* Caller ensures we have vectors available */ |
Neels Hofmeyr | 33f5341 | 2017-10-29 02:11:18 +0100 | [diff] [blame] | 243 | at = vlr_subscr_get_auth_tuple(vsub, afp->auth_tuple_max_reuse_count); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 244 | if (!at) { |
| 245 | LOGPFSML(fi, LOGL_ERROR, "A previous check ensured that an" |
| 246 | " auth tuple was available, but now there is in fact" |
| 247 | " none.\n"); |
| 248 | auth_fsm_term(fi, VLR_AUTH_RES_PROC_ERR); |
| 249 | return -1; |
| 250 | } |
| 251 | |
| 252 | LOGPFSM(fi, "got auth tuple: use_count=%d key_seq=%d\n", |
| 253 | at->use_count, at->key_seq); |
| 254 | |
| 255 | OSMO_ASSERT(at); |
| 256 | |
| 257 | /* Transmit auth req to subscriber */ |
| 258 | afp->auth_requested = true; |
| 259 | vsub->last_tuple = at; |
| 260 | vsub->vlr->ops.tx_auth_req(vsub->msc_conn_ref, at, |
Neels Hofmeyr | e3d72d7 | 2017-12-18 02:06:44 +0100 | [diff] [blame] | 261 | vlr_use_umts_aka(&at->vec, afp->is_r99)); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | /*********************************************************************** |
| 266 | * FSM State Action functions |
| 267 | ***********************************************************************/ |
| 268 | |
| 269 | /* Initial State of TS 23.018 AUT_VLR */ |
| 270 | static void auth_fsm_needs_auth(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 271 | { |
| 272 | struct auth_fsm_priv *afp = fi->priv; |
| 273 | struct vlr_subscr *vsub = afp->vsub; |
| 274 | |
| 275 | OSMO_ASSERT(event == VLR_AUTH_E_START); |
| 276 | |
Neels Hofmeyr | 33f5341 | 2017-10-29 02:11:18 +0100 | [diff] [blame] | 277 | /* Start off with the default max_reuse_count, possibly change that if we |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 278 | * need to re-use an old tuple. */ |
Neels Hofmeyr | 33f5341 | 2017-10-29 02:11:18 +0100 | [diff] [blame] | 279 | afp->auth_tuple_max_reuse_count = vsub->vlr->cfg.auth_tuple_max_reuse_count; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 280 | |
| 281 | /* Check if we have vectors available */ |
Neels Hofmeyr | 33f5341 | 2017-10-29 02:11:18 +0100 | [diff] [blame] | 282 | if (!vlr_subscr_has_auth_tuple(vsub, afp->auth_tuple_max_reuse_count)) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 283 | /* Obtain_Authentication_Sets_VLR */ |
| 284 | vlr_subscr_req_sai(vsub, NULL, NULL); |
| 285 | osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_NEEDS_AUTH_WAIT_AI, |
| 286 | GSM_29002_TIMER_M, 0); |
| 287 | } else { |
| 288 | /* go straight ahead with sending auth request */ |
| 289 | osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_WAIT_RESP, |
| 290 | vlr_timer(vsub->vlr, 3260), 3260); |
| 291 | _vlr_subscr_authenticate(fi); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /* Waiting for Authentication Info from HLR */ |
| 296 | static void auth_fsm_wait_ai(struct osmo_fsm_inst *fi, uint32_t event, |
| 297 | void *data) |
| 298 | { |
| 299 | struct auth_fsm_priv *afp = fi->priv; |
| 300 | struct vlr_subscr *vsub = afp->vsub; |
| 301 | struct osmo_gsup_message *gsup = data; |
| 302 | |
| 303 | if (event == VLR_AUTH_E_HLR_SAI_NACK) |
| 304 | LOGPFSM(fi, "GSUP: rx Auth Info Error cause: %d: %s\n", |
| 305 | gsup->cause, |
| 306 | get_value_string(gsm48_gmm_cause_names, gsup->cause)); |
| 307 | |
| 308 | /* We are in what corresponds to the |
| 309 | * Wait_For_Authentication_Sets state of TS 23.018 OAS_VLR */ |
| 310 | if ((event == VLR_AUTH_E_HLR_SAI_ACK && !gsup->num_auth_vectors) |
| 311 | || (event == VLR_AUTH_E_HLR_SAI_NACK && |
| 312 | gsup->cause != GMM_CAUSE_IMSI_UNKNOWN) |
| 313 | || (event == VLR_AUTH_E_HLR_SAI_ABORT)) { |
| 314 | if (vsub->vlr->cfg.auth_reuse_old_sets_on_error |
| 315 | && vlr_subscr_has_auth_tuple(vsub, -1)) { |
Neels Hofmeyr | 33f5341 | 2017-10-29 02:11:18 +0100 | [diff] [blame] | 316 | /* To re-use an old tuple, disable the max_reuse_count |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 317 | * constraint. */ |
Neels Hofmeyr | 33f5341 | 2017-10-29 02:11:18 +0100 | [diff] [blame] | 318 | afp->auth_tuple_max_reuse_count = -1; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 319 | goto pass; |
| 320 | } |
| 321 | /* result = procedure error */ |
| 322 | auth_fsm_term(fi, VLR_AUTH_RES_PROC_ERR); |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | switch (event) { |
| 327 | case VLR_AUTH_E_HLR_SAI_ACK: |
| 328 | vlr_subscr_update_tuples(vsub, gsup); |
| 329 | goto pass; |
| 330 | break; |
| 331 | case VLR_AUTH_E_HLR_SAI_NACK: |
| 332 | auth_fsm_term(fi, |
| 333 | gsup->cause == GMM_CAUSE_IMSI_UNKNOWN? |
| 334 | VLR_AUTH_RES_UNKNOWN_SUBSCR |
| 335 | : VLR_AUTH_RES_PROC_ERR); |
| 336 | break; |
| 337 | } |
| 338 | |
| 339 | return; |
| 340 | pass: |
| 341 | osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_WAIT_RESP, |
| 342 | vlr_timer(vsub->vlr, 3260), 3260); |
| 343 | _vlr_subscr_authenticate(fi); |
| 344 | } |
| 345 | |
| 346 | /* Waiting for Authentication Response from MS */ |
| 347 | static void auth_fsm_wait_auth_resp(struct osmo_fsm_inst *fi, uint32_t event, |
| 348 | void *data) |
| 349 | { |
| 350 | struct auth_fsm_priv *afp = fi->priv; |
| 351 | struct vlr_subscr *vsub = afp->vsub; |
| 352 | struct vlr_instance *vlr = vsub->vlr; |
| 353 | struct vlr_auth_resp_par *par = data; |
| 354 | int rc; |
| 355 | |
| 356 | switch (event) { |
| 357 | case VLR_AUTH_E_MS_AUTH_RESP: |
| 358 | rc = check_auth_resp(vsub, par->is_r99, par->is_utran, |
| 359 | par->res, par->res_len); |
| 360 | if (rc == false) { |
| 361 | if (!afp->by_imsi) { |
| 362 | vlr->ops.tx_id_req(vsub->msc_conn_ref, |
| 363 | GSM_MI_TYPE_IMSI); |
| 364 | osmo_fsm_inst_state_chg(fi, |
| 365 | VLR_SUB_AS_WAIT_ID_IMSI, |
| 366 | vlr_timer(vlr, 3270), 3270); |
| 367 | } else { |
| 368 | auth_fsm_term(fi, VLR_AUTH_RES_AUTH_FAILED); |
| 369 | } |
| 370 | } else { |
| 371 | auth_fsm_term(fi, VLR_AUTH_RES_PASSED); |
| 372 | } |
| 373 | break; |
| 374 | case VLR_AUTH_E_MS_AUTH_FAIL: |
| 375 | if (par->auts) { |
| 376 | /* First failure, start re-sync attempt */ |
| 377 | vlr_subscr_req_sai(vsub, par->auts, |
| 378 | vsub->last_tuple->vec.rand); |
| 379 | osmo_fsm_inst_state_chg(fi, |
| 380 | VLR_SUB_AS_NEEDS_AUTH_WAIT_SAI_RESYNC, |
| 381 | GSM_29002_TIMER_M, 0); |
| 382 | } else |
| 383 | auth_fsm_term(fi, VLR_AUTH_RES_AUTH_FAILED); |
| 384 | break; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /* Waiting for Authentication Info from HLR (resync case) */ |
| 389 | static void auth_fsm_wait_ai_resync(struct osmo_fsm_inst *fi, |
| 390 | uint32_t event, void *data) |
| 391 | { |
| 392 | struct auth_fsm_priv *afp = fi->priv; |
| 393 | struct vlr_subscr *vsub = afp->vsub; |
| 394 | struct osmo_gsup_message *gsup = data; |
| 395 | |
| 396 | /* We are in what corresponds to the |
| 397 | * Wait_For_Authentication_Sets state of TS 23.018 OAS_VLR */ |
| 398 | if ((event == VLR_AUTH_E_HLR_SAI_ACK && !gsup->num_auth_vectors) || |
| 399 | (event == VLR_AUTH_E_HLR_SAI_NACK && |
| 400 | gsup->cause != GMM_CAUSE_IMSI_UNKNOWN) || |
| 401 | (event == VLR_AUTH_E_HLR_SAI_ABORT)) { |
| 402 | /* result = procedure error */ |
| 403 | auth_fsm_term(fi, VLR_AUTH_RES_PROC_ERR); |
| 404 | } |
| 405 | switch (event) { |
| 406 | case VLR_AUTH_E_HLR_SAI_ACK: |
| 407 | vlr_subscr_update_tuples(vsub, gsup); |
| 408 | goto pass; |
| 409 | break; |
| 410 | case VLR_AUTH_E_HLR_SAI_NACK: |
| 411 | auth_fsm_term(fi, |
| 412 | gsup->cause == GMM_CAUSE_IMSI_UNKNOWN? |
| 413 | VLR_AUTH_RES_UNKNOWN_SUBSCR |
| 414 | : VLR_AUTH_RES_PROC_ERR); |
| 415 | break; |
| 416 | } |
| 417 | |
| 418 | return; |
| 419 | pass: |
| 420 | osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_WAIT_RESP_RESYNC, |
| 421 | vlr_timer(vsub->vlr, 3260), 3260); |
| 422 | _vlr_subscr_authenticate(fi); |
| 423 | } |
| 424 | |
| 425 | /* Waiting for AUTH RESP from MS (re-sync case) */ |
| 426 | static void auth_fsm_wait_auth_resp_resync(struct osmo_fsm_inst *fi, |
| 427 | uint32_t event, void *data) |
| 428 | { |
| 429 | struct auth_fsm_priv *afp = fi->priv; |
| 430 | struct vlr_subscr *vsub = afp->vsub; |
| 431 | struct vlr_auth_resp_par *par = data; |
| 432 | struct vlr_instance *vlr = vsub->vlr; |
| 433 | int rc; |
| 434 | |
| 435 | switch (event) { |
| 436 | case VLR_AUTH_E_MS_AUTH_RESP: |
| 437 | rc = check_auth_resp(vsub, par->is_r99, par->is_utran, |
| 438 | par->res, par->res_len); |
| 439 | if (rc == false) { |
| 440 | if (!afp->by_imsi) { |
| 441 | vlr->ops.tx_id_req(vsub->msc_conn_ref, |
| 442 | GSM_MI_TYPE_IMSI); |
| 443 | osmo_fsm_inst_state_chg(fi, |
| 444 | VLR_SUB_AS_WAIT_ID_IMSI, |
| 445 | vlr_timer(vlr, 3270), 3270); |
| 446 | } else { |
| 447 | /* Result = Aborted */ |
| 448 | auth_fsm_term(fi, VLR_AUTH_RES_ABORTED); |
| 449 | } |
| 450 | } else { |
| 451 | /* Result = Pass */ |
| 452 | auth_fsm_term(fi, VLR_AUTH_RES_PASSED); |
| 453 | } |
| 454 | break; |
| 455 | case VLR_AUTH_E_MS_AUTH_FAIL: |
| 456 | /* Second failure: Result = Fail */ |
| 457 | auth_fsm_term(fi, VLR_AUTH_RES_AUTH_FAILED); |
| 458 | break; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | /* AUT_VLR waiting for Obtain_IMSI_VLR result */ |
| 463 | static void auth_fsm_wait_imsi(struct osmo_fsm_inst *fi, uint32_t event, |
| 464 | void *data) |
| 465 | { |
| 466 | struct auth_fsm_priv *afp = fi->priv; |
| 467 | struct vlr_subscr *vsub = afp->vsub; |
| 468 | const char *mi_string = data; |
| 469 | |
| 470 | switch (event) { |
| 471 | case VLR_AUTH_E_MS_ID_IMSI: |
| 472 | if (vsub->imsi[0] |
| 473 | && !vlr_subscr_matches_imsi(vsub, mi_string)) { |
| 474 | LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:" |
| 475 | " %s\n", mi_string); |
| 476 | } else { |
| 477 | strncpy(vsub->imsi, mi_string, sizeof(vsub->imsi)); |
| 478 | vsub->imsi[sizeof(vsub->imsi)-1] = '\0'; |
| 479 | } |
| 480 | /* retry with identity=IMSI */ |
| 481 | afp->by_imsi = true; |
| 482 | osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_NEEDS_AUTH, 0, 0); |
| 483 | osmo_fsm_inst_dispatch(fi, VLR_AUTH_E_START, NULL); |
| 484 | break; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | static const struct osmo_fsm_state auth_fsm_states[] = { |
| 489 | [VLR_SUB_AS_NEEDS_AUTH] = { |
| 490 | .name = OSMO_STRINGIFY(VLR_SUB_AS_NEEDS_AUTH), |
| 491 | .in_event_mask = S(VLR_AUTH_E_START), |
| 492 | .out_state_mask = S(VLR_SUB_AS_NEEDS_AUTH_WAIT_AI) | |
| 493 | S(VLR_SUB_AS_WAIT_RESP), |
| 494 | .action = auth_fsm_needs_auth, |
| 495 | }, |
| 496 | [VLR_SUB_AS_NEEDS_AUTH_WAIT_AI] = { |
| 497 | .name = OSMO_STRINGIFY(VLR_SUB_AS_NEEDS_AUTH_WAIT_AI), |
| 498 | .in_event_mask = S(VLR_AUTH_E_HLR_SAI_ACK) | |
| 499 | S(VLR_AUTH_E_HLR_SAI_NACK), |
| 500 | .out_state_mask = S(VLR_SUB_AS_AUTH_FAILED) | |
| 501 | S(VLR_SUB_AS_WAIT_RESP), |
| 502 | .action = auth_fsm_wait_ai, |
| 503 | }, |
| 504 | [VLR_SUB_AS_WAIT_RESP] = { |
| 505 | .name = OSMO_STRINGIFY(VLR_SUB_AS_WAIT_RESP), |
| 506 | .in_event_mask = S(VLR_AUTH_E_MS_AUTH_RESP) | |
| 507 | S(VLR_AUTH_E_MS_AUTH_FAIL), |
| 508 | .out_state_mask = S(VLR_SUB_AS_WAIT_ID_IMSI) | |
| 509 | S(VLR_SUB_AS_AUTH_FAILED) | |
| 510 | S(VLR_SUB_AS_AUTHENTICATED) | |
| 511 | S(VLR_SUB_AS_NEEDS_AUTH_WAIT_SAI_RESYNC), |
| 512 | .action = auth_fsm_wait_auth_resp, |
| 513 | }, |
| 514 | [VLR_SUB_AS_NEEDS_AUTH_WAIT_SAI_RESYNC] = { |
| 515 | .name = OSMO_STRINGIFY(VLR_SUB_AS_NEEDS_AUTH_WAIT_SAI_RESYNC), |
| 516 | .in_event_mask = S(VLR_AUTH_E_HLR_SAI_ACK) | |
| 517 | S(VLR_AUTH_E_HLR_SAI_NACK), |
| 518 | .out_state_mask = S(VLR_SUB_AS_AUTH_FAILED) | |
| 519 | S(VLR_SUB_AS_WAIT_RESP_RESYNC), |
| 520 | .action = auth_fsm_wait_ai_resync, |
| 521 | }, |
| 522 | [VLR_SUB_AS_WAIT_RESP_RESYNC] = { |
| 523 | .name = OSMO_STRINGIFY(VLR_SUB_AS_WAIT_RESP_RESYNC), |
| 524 | .in_event_mask = S(VLR_AUTH_E_MS_AUTH_RESP) | |
| 525 | S(VLR_AUTH_E_MS_AUTH_FAIL), |
| 526 | .out_state_mask = S(VLR_SUB_AS_AUTH_FAILED) | |
| 527 | S(VLR_SUB_AS_AUTHENTICATED), |
| 528 | .action = auth_fsm_wait_auth_resp_resync, |
| 529 | }, |
| 530 | [VLR_SUB_AS_WAIT_ID_IMSI] = { |
| 531 | .name = OSMO_STRINGIFY(VLR_SUB_AS_WAIT_ID_IMSI), |
| 532 | .in_event_mask = S(VLR_AUTH_E_MS_ID_IMSI), |
| 533 | .out_state_mask = S(VLR_SUB_AS_NEEDS_AUTH), |
| 534 | .action = auth_fsm_wait_imsi, |
| 535 | }, |
| 536 | [VLR_SUB_AS_AUTHENTICATED] = { |
| 537 | .name = OSMO_STRINGIFY(VLR_SUB_AS_AUTHENTICATED), |
| 538 | .in_event_mask = 0, |
| 539 | .out_state_mask = 0, |
| 540 | }, |
| 541 | [VLR_SUB_AS_AUTH_FAILED] = { |
| 542 | .name = OSMO_STRINGIFY(VLR_SUB_AS_AUTH_FAILED), |
| 543 | .in_event_mask = 0, |
| 544 | .out_state_mask = 0, |
| 545 | .onenter = auth_fsm_onenter_failed, |
| 546 | }, |
| 547 | }; |
| 548 | |
| 549 | struct osmo_fsm vlr_auth_fsm = { |
| 550 | .name = "VLR_Authenticate", |
| 551 | .states = auth_fsm_states, |
| 552 | .num_states = ARRAY_SIZE(auth_fsm_states), |
| 553 | .allstate_event_mask = 0, |
| 554 | .allstate_action = NULL, |
| 555 | .log_subsys = DVLR, |
| 556 | .event_names = fsm_auth_event_names, |
| 557 | }; |
| 558 | |
| 559 | /*********************************************************************** |
| 560 | * User API (for SGSN/MSC code) |
| 561 | ***********************************************************************/ |
| 562 | |
| 563 | /* MSC->VLR: Start Procedure Authenticate_VLR (TS 23.012 Ch. 4.1.2.2) */ |
| 564 | struct osmo_fsm_inst *auth_fsm_start(struct vlr_subscr *vsub, |
| 565 | uint32_t log_level, |
| 566 | struct osmo_fsm_inst *parent, |
| 567 | uint32_t parent_term_event, |
| 568 | bool is_r99, |
| 569 | bool is_utran) |
| 570 | { |
| 571 | struct osmo_fsm_inst *fi; |
| 572 | struct auth_fsm_priv *afp; |
| 573 | |
| 574 | fi = osmo_fsm_inst_alloc_child(&vlr_auth_fsm, parent, |
| 575 | parent_term_event); |
Neels Hofmeyr | c698ab9 | 2017-11-07 13:23:13 +0100 | [diff] [blame] | 576 | if (!fi) { |
| 577 | osmo_fsm_inst_dispatch(parent, parent_term_event, 0); |
| 578 | return NULL; |
| 579 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 580 | |
| 581 | afp = talloc_zero(fi, struct auth_fsm_priv); |
| 582 | if (!afp) { |
| 583 | osmo_fsm_inst_dispatch(parent, parent_term_event, 0); |
| 584 | return NULL; |
| 585 | } |
| 586 | |
| 587 | afp->vsub = vsub; |
| 588 | if (vsub->imsi[0]) |
| 589 | afp->by_imsi = true; |
| 590 | afp->is_r99 = is_r99; |
| 591 | afp->is_utran = is_utran; |
| 592 | fi->priv = afp; |
| 593 | vsub->auth_fsm = fi; |
| 594 | |
| 595 | osmo_fsm_inst_dispatch(fi, VLR_AUTH_E_START, NULL); |
| 596 | |
| 597 | return fi; |
| 598 | } |