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; |
Neels Hofmeyr | 11d2ce3 | 2018-03-10 00:25:20 +0100 | [diff] [blame] | 139 | bool res_is_umts_aka; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 140 | OSMO_ASSERT(at); |
| 141 | |
Neels Hofmeyr | a9099bc | 2018-03-10 04:22:50 +0100 | [diff] [blame] | 142 | LOGVSUBP(LOGL_DEBUG, vsub, "AUTH on %s received %s: %s (%u bytes)\n", |
| 143 | is_utran ? "UTRAN" : "GERAN", |
| 144 | is_utran ? "RES" : "SRES/RES", |
| 145 | osmo_hexdump_nospc(res, res_len), res_len); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 146 | |
| 147 | /* RES must be present and at least 32bit */ |
Neels Hofmeyr | 11d2ce3 | 2018-03-10 00:25:20 +0100 | [diff] [blame] | 148 | if (!res || !res_len) { |
| 149 | LOGVSUBP(LOGL_NOTICE, vsub, "AUTH SRES/RES missing\n"); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 150 | goto out_false; |
| 151 | } |
| 152 | |
Neels Hofmeyr | 11d2ce3 | 2018-03-10 00:25:20 +0100 | [diff] [blame] | 153 | /* We're deciding the UMTS AKA-ness of the response by the RES size. So let's make sure we can't |
| 154 | * mix them up by size. On UTRAN, we expect full length RES always, no way to mix up there. */ |
| 155 | if (!is_utran && vec->res_len == sizeof(vec->sres)) |
| 156 | LOGVSUBP(LOGL_ERROR, vsub, "Unforeseen situation: UMTS AKA's RES length" |
| 157 | " equals the size of SRES: %u -- this code wants to differentiate" |
| 158 | " the two by their size, which won't work properly now.\n", vec->res_len); |
| 159 | |
| 160 | /* RES must be either vec->res_len (UMTS AKA) or sizeof(sres) (GSM AKA) */ |
| 161 | if (res_len == vec->res_len) |
| 162 | res_is_umts_aka = true; |
| 163 | else if (res_len == sizeof(vec->sres)) |
| 164 | res_is_umts_aka = false; |
| 165 | else { |
| 166 | if (is_utran) |
| 167 | LOGVSUBP(LOGL_NOTICE, vsub, "AUTH RES has invalid length: %u." |
| 168 | " Expected %u (UMTS AKA)\n", |
| 169 | res_len, vec->res_len); |
| 170 | else |
| 171 | LOGVSUBP(LOGL_NOTICE, vsub, "AUTH SRES/RES has invalid length: %u." |
| 172 | " Expected either %zu (GSM AKA) or %u (UMTS AKA)\n", |
| 173 | res_len, sizeof(vec->sres), vec->res_len); |
| 174 | goto out_false; |
| 175 | } |
| 176 | |
| 177 | check_umts = (is_r99 |
| 178 | && (vec->auth_types & OSMO_AUTH_TYPE_UMTS) |
| 179 | && res_is_umts_aka); |
| 180 | |
| 181 | /* Even on an R99 capable MS with a UMTS AKA capable USIM, |
| 182 | * the MS may still choose to only perform GSM AKA, as |
| 183 | * long as the bearer is GERAN -- never on UTRAN: */ |
| 184 | if (is_utran && !check_umts) { |
| 185 | LOGVSUBP(LOGL_ERROR, vsub, |
| 186 | "AUTH via UTRAN, cannot allow GSM AKA" |
| 187 | " (MS is %sR99 capable, vec has %sUMTS AKA tokens, res_len=%u is %s)\n", |
| 188 | is_r99 ? "" : "NOT ", |
| 189 | (vec->auth_types & OSMO_AUTH_TYPE_UMTS) ? "" : "NO ", |
| 190 | res_len, (res_len == vec->res_len)? "valid" : "INVALID on UTRAN"); |
| 191 | goto out_false; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | if (check_umts) { |
| 195 | if (res_len != vec->res_len |
| 196 | || memcmp(res, vec->res, res_len)) { |
| 197 | LOGVSUBP(LOGL_INFO, vsub, "UMTS AUTH failure:" |
| 198 | " mismatching res (expected res=%s)\n", |
| 199 | osmo_hexdump(vec->res, vec->res_len)); |
| 200 | goto out_false; |
| 201 | } |
| 202 | |
| 203 | LOGVSUBP(LOGL_INFO, vsub, "AUTH established UMTS security" |
| 204 | " context\n"); |
| 205 | vsub->sec_ctx = VLR_SEC_CTX_UMTS; |
| 206 | return true; |
| 207 | } else { |
| 208 | if (res_len != sizeof(vec->sres) |
| 209 | || memcmp(res, vec->sres, sizeof(vec->sres))) { |
| 210 | LOGVSUBP(LOGL_INFO, vsub, "GSM AUTH failure:" |
| 211 | " mismatching sres (expected sres=%s)\n", |
| 212 | osmo_hexdump(vec->sres, sizeof(vec->sres))); |
| 213 | goto out_false; |
| 214 | } |
| 215 | |
| 216 | LOGVSUBP(LOGL_INFO, vsub, "AUTH established GSM security" |
| 217 | " context\n"); |
| 218 | vsub->sec_ctx = VLR_SEC_CTX_GSM; |
| 219 | return true; |
| 220 | } |
| 221 | |
| 222 | out_false: |
| 223 | vsub->sec_ctx = VLR_SEC_CTX_NONE; |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | static void auth_fsm_onenter_failed(struct osmo_fsm_inst *fi, uint32_t prev_state) |
| 228 | { |
| 229 | struct auth_fsm_priv *afp = fi->priv; |
| 230 | struct vlr_subscr *vsub = afp->vsub; |
| 231 | |
| 232 | /* If authentication hasn't even started, e.g. the HLR sent no auth |
| 233 | * info, then we also don't need to tell the HLR about an auth failure. |
| 234 | */ |
Max | 770fbd2 | 2018-01-24 12:48:33 +0100 | [diff] [blame] | 235 | if (afp->auth_requested) { |
| 236 | int rc = vlr_subscr_tx_auth_fail_rep(vsub); |
| 237 | if (rc < 0) |
| 238 | LOGVSUBP(LOGL_ERROR, vsub, "Failed to communicate AUTH failure to HLR\n"); |
| 239 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 240 | } |
| 241 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 242 | /* Terminate the Auth FSM Instance and notify parent */ |
| 243 | static void auth_fsm_term(struct osmo_fsm_inst *fi, enum vlr_auth_fsm_result res) |
| 244 | { |
| 245 | struct auth_fsm_priv *afp = fi->priv; |
| 246 | struct vlr_subscr *vsub = afp->vsub; |
| 247 | |
| 248 | LOGPFSM(fi, "Authentication terminating with result %s\n", |
| 249 | vlr_auth_fsm_result_name(res)); |
| 250 | |
| 251 | /* Do one final state transition (mostly for logging purpose) */ |
| 252 | if (res == VLR_AUTH_RES_PASSED) |
| 253 | osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_AUTHENTICATED, 0, 0); |
| 254 | else |
| 255 | osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_AUTH_FAILED, 0, 0); |
| 256 | |
| 257 | /* return the result to the parent FSM */ |
| 258 | osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, &res); |
| 259 | vsub->auth_fsm = NULL; |
| 260 | } |
| 261 | |
| 262 | /* back-end function transmitting authentication. Caller ensures we have valid |
| 263 | * tuple */ |
| 264 | static int _vlr_subscr_authenticate(struct osmo_fsm_inst *fi) |
| 265 | { |
| 266 | struct auth_fsm_priv *afp = fi->priv; |
| 267 | struct vlr_subscr *vsub = afp->vsub; |
| 268 | struct gsm_auth_tuple *at; |
| 269 | |
| 270 | /* Caller ensures we have vectors available */ |
Neels Hofmeyr | 33f5341 | 2017-10-29 02:11:18 +0100 | [diff] [blame] | 271 | 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] | 272 | if (!at) { |
| 273 | LOGPFSML(fi, LOGL_ERROR, "A previous check ensured that an" |
| 274 | " auth tuple was available, but now there is in fact" |
| 275 | " none.\n"); |
| 276 | auth_fsm_term(fi, VLR_AUTH_RES_PROC_ERR); |
| 277 | return -1; |
| 278 | } |
| 279 | |
| 280 | LOGPFSM(fi, "got auth tuple: use_count=%d key_seq=%d\n", |
| 281 | at->use_count, at->key_seq); |
| 282 | |
| 283 | OSMO_ASSERT(at); |
| 284 | |
| 285 | /* Transmit auth req to subscriber */ |
| 286 | afp->auth_requested = true; |
| 287 | vsub->last_tuple = at; |
| 288 | vsub->vlr->ops.tx_auth_req(vsub->msc_conn_ref, at, |
Neels Hofmeyr | e3d72d7 | 2017-12-18 02:06:44 +0100 | [diff] [blame] | 289 | vlr_use_umts_aka(&at->vec, afp->is_r99)); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | /*********************************************************************** |
| 294 | * FSM State Action functions |
| 295 | ***********************************************************************/ |
| 296 | |
| 297 | /* Initial State of TS 23.018 AUT_VLR */ |
| 298 | static void auth_fsm_needs_auth(struct osmo_fsm_inst *fi, uint32_t event, void *data) |
| 299 | { |
| 300 | struct auth_fsm_priv *afp = fi->priv; |
| 301 | struct vlr_subscr *vsub = afp->vsub; |
| 302 | |
| 303 | OSMO_ASSERT(event == VLR_AUTH_E_START); |
| 304 | |
Neels Hofmeyr | 33f5341 | 2017-10-29 02:11:18 +0100 | [diff] [blame] | 305 | /* 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] | 306 | * need to re-use an old tuple. */ |
Neels Hofmeyr | 33f5341 | 2017-10-29 02:11:18 +0100 | [diff] [blame] | 307 | 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] | 308 | |
| 309 | /* Check if we have vectors available */ |
Neels Hofmeyr | 33f5341 | 2017-10-29 02:11:18 +0100 | [diff] [blame] | 310 | 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] | 311 | /* Obtain_Authentication_Sets_VLR */ |
Max | 770fbd2 | 2018-01-24 12:48:33 +0100 | [diff] [blame] | 312 | int rc = vlr_subscr_req_sai(vsub, NULL, NULL); |
| 313 | if (rc < 0) |
| 314 | LOGPFSM(fi, "Failed to request Authentication Sets from VLR\n"); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 315 | osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_NEEDS_AUTH_WAIT_AI, |
| 316 | GSM_29002_TIMER_M, 0); |
| 317 | } else { |
| 318 | /* go straight ahead with sending auth request */ |
| 319 | osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_WAIT_RESP, |
| 320 | vlr_timer(vsub->vlr, 3260), 3260); |
| 321 | _vlr_subscr_authenticate(fi); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | /* Waiting for Authentication Info from HLR */ |
| 326 | static void auth_fsm_wait_ai(struct osmo_fsm_inst *fi, uint32_t event, |
| 327 | void *data) |
| 328 | { |
| 329 | struct auth_fsm_priv *afp = fi->priv; |
| 330 | struct vlr_subscr *vsub = afp->vsub; |
| 331 | struct osmo_gsup_message *gsup = data; |
| 332 | |
| 333 | if (event == VLR_AUTH_E_HLR_SAI_NACK) |
| 334 | LOGPFSM(fi, "GSUP: rx Auth Info Error cause: %d: %s\n", |
| 335 | gsup->cause, |
| 336 | get_value_string(gsm48_gmm_cause_names, gsup->cause)); |
| 337 | |
| 338 | /* We are in what corresponds to the |
| 339 | * Wait_For_Authentication_Sets state of TS 23.018 OAS_VLR */ |
| 340 | if ((event == VLR_AUTH_E_HLR_SAI_ACK && !gsup->num_auth_vectors) |
| 341 | || (event == VLR_AUTH_E_HLR_SAI_NACK && |
| 342 | gsup->cause != GMM_CAUSE_IMSI_UNKNOWN) |
| 343 | || (event == VLR_AUTH_E_HLR_SAI_ABORT)) { |
| 344 | if (vsub->vlr->cfg.auth_reuse_old_sets_on_error |
| 345 | && vlr_subscr_has_auth_tuple(vsub, -1)) { |
Neels Hofmeyr | 33f5341 | 2017-10-29 02:11:18 +0100 | [diff] [blame] | 346 | /* To re-use an old tuple, disable the max_reuse_count |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 347 | * constraint. */ |
Neels Hofmeyr | 33f5341 | 2017-10-29 02:11:18 +0100 | [diff] [blame] | 348 | afp->auth_tuple_max_reuse_count = -1; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 349 | goto pass; |
| 350 | } |
| 351 | /* result = procedure error */ |
| 352 | auth_fsm_term(fi, VLR_AUTH_RES_PROC_ERR); |
| 353 | return; |
| 354 | } |
| 355 | |
| 356 | switch (event) { |
| 357 | case VLR_AUTH_E_HLR_SAI_ACK: |
| 358 | vlr_subscr_update_tuples(vsub, gsup); |
| 359 | goto pass; |
| 360 | break; |
| 361 | case VLR_AUTH_E_HLR_SAI_NACK: |
| 362 | auth_fsm_term(fi, |
| 363 | gsup->cause == GMM_CAUSE_IMSI_UNKNOWN? |
| 364 | VLR_AUTH_RES_UNKNOWN_SUBSCR |
| 365 | : VLR_AUTH_RES_PROC_ERR); |
| 366 | break; |
| 367 | } |
| 368 | |
| 369 | return; |
| 370 | pass: |
| 371 | osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_WAIT_RESP, |
| 372 | vlr_timer(vsub->vlr, 3260), 3260); |
| 373 | _vlr_subscr_authenticate(fi); |
| 374 | } |
| 375 | |
| 376 | /* Waiting for Authentication Response from MS */ |
| 377 | static void auth_fsm_wait_auth_resp(struct osmo_fsm_inst *fi, uint32_t event, |
| 378 | void *data) |
| 379 | { |
| 380 | struct auth_fsm_priv *afp = fi->priv; |
| 381 | struct vlr_subscr *vsub = afp->vsub; |
| 382 | struct vlr_instance *vlr = vsub->vlr; |
| 383 | struct vlr_auth_resp_par *par = data; |
| 384 | int rc; |
| 385 | |
| 386 | switch (event) { |
| 387 | case VLR_AUTH_E_MS_AUTH_RESP: |
| 388 | rc = check_auth_resp(vsub, par->is_r99, par->is_utran, |
| 389 | par->res, par->res_len); |
| 390 | if (rc == false) { |
| 391 | if (!afp->by_imsi) { |
| 392 | vlr->ops.tx_id_req(vsub->msc_conn_ref, |
| 393 | GSM_MI_TYPE_IMSI); |
| 394 | osmo_fsm_inst_state_chg(fi, |
| 395 | VLR_SUB_AS_WAIT_ID_IMSI, |
| 396 | vlr_timer(vlr, 3270), 3270); |
| 397 | } else { |
| 398 | auth_fsm_term(fi, VLR_AUTH_RES_AUTH_FAILED); |
| 399 | } |
| 400 | } else { |
| 401 | auth_fsm_term(fi, VLR_AUTH_RES_PASSED); |
| 402 | } |
| 403 | break; |
| 404 | case VLR_AUTH_E_MS_AUTH_FAIL: |
| 405 | if (par->auts) { |
| 406 | /* First failure, start re-sync attempt */ |
Max | 770fbd2 | 2018-01-24 12:48:33 +0100 | [diff] [blame] | 407 | rc = vlr_subscr_req_sai(vsub, par->auts, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 408 | vsub->last_tuple->vec.rand); |
| 409 | osmo_fsm_inst_state_chg(fi, |
| 410 | VLR_SUB_AS_NEEDS_AUTH_WAIT_SAI_RESYNC, |
| 411 | GSM_29002_TIMER_M, 0); |
| 412 | } else |
| 413 | auth_fsm_term(fi, VLR_AUTH_RES_AUTH_FAILED); |
| 414 | break; |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | /* Waiting for Authentication Info from HLR (resync case) */ |
| 419 | static void auth_fsm_wait_ai_resync(struct osmo_fsm_inst *fi, |
| 420 | uint32_t event, void *data) |
| 421 | { |
| 422 | struct auth_fsm_priv *afp = fi->priv; |
| 423 | struct vlr_subscr *vsub = afp->vsub; |
| 424 | struct osmo_gsup_message *gsup = data; |
| 425 | |
| 426 | /* We are in what corresponds to the |
| 427 | * Wait_For_Authentication_Sets state of TS 23.018 OAS_VLR */ |
| 428 | if ((event == VLR_AUTH_E_HLR_SAI_ACK && !gsup->num_auth_vectors) || |
| 429 | (event == VLR_AUTH_E_HLR_SAI_NACK && |
| 430 | gsup->cause != GMM_CAUSE_IMSI_UNKNOWN) || |
| 431 | (event == VLR_AUTH_E_HLR_SAI_ABORT)) { |
| 432 | /* result = procedure error */ |
| 433 | auth_fsm_term(fi, VLR_AUTH_RES_PROC_ERR); |
| 434 | } |
| 435 | switch (event) { |
| 436 | case VLR_AUTH_E_HLR_SAI_ACK: |
| 437 | vlr_subscr_update_tuples(vsub, gsup); |
| 438 | goto pass; |
| 439 | break; |
| 440 | case VLR_AUTH_E_HLR_SAI_NACK: |
| 441 | auth_fsm_term(fi, |
| 442 | gsup->cause == GMM_CAUSE_IMSI_UNKNOWN? |
| 443 | VLR_AUTH_RES_UNKNOWN_SUBSCR |
| 444 | : VLR_AUTH_RES_PROC_ERR); |
| 445 | break; |
| 446 | } |
| 447 | |
| 448 | return; |
| 449 | pass: |
| 450 | osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_WAIT_RESP_RESYNC, |
| 451 | vlr_timer(vsub->vlr, 3260), 3260); |
| 452 | _vlr_subscr_authenticate(fi); |
| 453 | } |
| 454 | |
| 455 | /* Waiting for AUTH RESP from MS (re-sync case) */ |
| 456 | static void auth_fsm_wait_auth_resp_resync(struct osmo_fsm_inst *fi, |
| 457 | uint32_t event, void *data) |
| 458 | { |
| 459 | struct auth_fsm_priv *afp = fi->priv; |
| 460 | struct vlr_subscr *vsub = afp->vsub; |
| 461 | struct vlr_auth_resp_par *par = data; |
| 462 | struct vlr_instance *vlr = vsub->vlr; |
| 463 | int rc; |
| 464 | |
| 465 | switch (event) { |
| 466 | case VLR_AUTH_E_MS_AUTH_RESP: |
| 467 | rc = check_auth_resp(vsub, par->is_r99, par->is_utran, |
| 468 | par->res, par->res_len); |
| 469 | if (rc == false) { |
| 470 | if (!afp->by_imsi) { |
| 471 | vlr->ops.tx_id_req(vsub->msc_conn_ref, |
| 472 | GSM_MI_TYPE_IMSI); |
| 473 | osmo_fsm_inst_state_chg(fi, |
| 474 | VLR_SUB_AS_WAIT_ID_IMSI, |
| 475 | vlr_timer(vlr, 3270), 3270); |
| 476 | } else { |
| 477 | /* Result = Aborted */ |
| 478 | auth_fsm_term(fi, VLR_AUTH_RES_ABORTED); |
| 479 | } |
| 480 | } else { |
| 481 | /* Result = Pass */ |
| 482 | auth_fsm_term(fi, VLR_AUTH_RES_PASSED); |
| 483 | } |
| 484 | break; |
| 485 | case VLR_AUTH_E_MS_AUTH_FAIL: |
| 486 | /* Second failure: Result = Fail */ |
| 487 | auth_fsm_term(fi, VLR_AUTH_RES_AUTH_FAILED); |
| 488 | break; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | /* AUT_VLR waiting for Obtain_IMSI_VLR result */ |
| 493 | static void auth_fsm_wait_imsi(struct osmo_fsm_inst *fi, uint32_t event, |
| 494 | void *data) |
| 495 | { |
| 496 | struct auth_fsm_priv *afp = fi->priv; |
| 497 | struct vlr_subscr *vsub = afp->vsub; |
| 498 | const char *mi_string = data; |
| 499 | |
| 500 | switch (event) { |
| 501 | case VLR_AUTH_E_MS_ID_IMSI: |
| 502 | if (vsub->imsi[0] |
| 503 | && !vlr_subscr_matches_imsi(vsub, mi_string)) { |
| 504 | LOGVSUBP(LOGL_ERROR, vsub, "IMSI in ID RESP differs:" |
| 505 | " %s\n", mi_string); |
| 506 | } else { |
| 507 | strncpy(vsub->imsi, mi_string, sizeof(vsub->imsi)); |
| 508 | vsub->imsi[sizeof(vsub->imsi)-1] = '\0'; |
| 509 | } |
| 510 | /* retry with identity=IMSI */ |
| 511 | afp->by_imsi = true; |
| 512 | osmo_fsm_inst_state_chg(fi, VLR_SUB_AS_NEEDS_AUTH, 0, 0); |
| 513 | osmo_fsm_inst_dispatch(fi, VLR_AUTH_E_START, NULL); |
| 514 | break; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | static const struct osmo_fsm_state auth_fsm_states[] = { |
| 519 | [VLR_SUB_AS_NEEDS_AUTH] = { |
| 520 | .name = OSMO_STRINGIFY(VLR_SUB_AS_NEEDS_AUTH), |
| 521 | .in_event_mask = S(VLR_AUTH_E_START), |
| 522 | .out_state_mask = S(VLR_SUB_AS_NEEDS_AUTH_WAIT_AI) | |
| 523 | S(VLR_SUB_AS_WAIT_RESP), |
| 524 | .action = auth_fsm_needs_auth, |
| 525 | }, |
| 526 | [VLR_SUB_AS_NEEDS_AUTH_WAIT_AI] = { |
| 527 | .name = OSMO_STRINGIFY(VLR_SUB_AS_NEEDS_AUTH_WAIT_AI), |
| 528 | .in_event_mask = S(VLR_AUTH_E_HLR_SAI_ACK) | |
| 529 | S(VLR_AUTH_E_HLR_SAI_NACK), |
| 530 | .out_state_mask = S(VLR_SUB_AS_AUTH_FAILED) | |
| 531 | S(VLR_SUB_AS_WAIT_RESP), |
| 532 | .action = auth_fsm_wait_ai, |
| 533 | }, |
| 534 | [VLR_SUB_AS_WAIT_RESP] = { |
| 535 | .name = OSMO_STRINGIFY(VLR_SUB_AS_WAIT_RESP), |
| 536 | .in_event_mask = S(VLR_AUTH_E_MS_AUTH_RESP) | |
| 537 | S(VLR_AUTH_E_MS_AUTH_FAIL), |
| 538 | .out_state_mask = S(VLR_SUB_AS_WAIT_ID_IMSI) | |
| 539 | S(VLR_SUB_AS_AUTH_FAILED) | |
| 540 | S(VLR_SUB_AS_AUTHENTICATED) | |
| 541 | S(VLR_SUB_AS_NEEDS_AUTH_WAIT_SAI_RESYNC), |
| 542 | .action = auth_fsm_wait_auth_resp, |
| 543 | }, |
| 544 | [VLR_SUB_AS_NEEDS_AUTH_WAIT_SAI_RESYNC] = { |
| 545 | .name = OSMO_STRINGIFY(VLR_SUB_AS_NEEDS_AUTH_WAIT_SAI_RESYNC), |
| 546 | .in_event_mask = S(VLR_AUTH_E_HLR_SAI_ACK) | |
| 547 | S(VLR_AUTH_E_HLR_SAI_NACK), |
| 548 | .out_state_mask = S(VLR_SUB_AS_AUTH_FAILED) | |
| 549 | S(VLR_SUB_AS_WAIT_RESP_RESYNC), |
| 550 | .action = auth_fsm_wait_ai_resync, |
| 551 | }, |
| 552 | [VLR_SUB_AS_WAIT_RESP_RESYNC] = { |
| 553 | .name = OSMO_STRINGIFY(VLR_SUB_AS_WAIT_RESP_RESYNC), |
| 554 | .in_event_mask = S(VLR_AUTH_E_MS_AUTH_RESP) | |
| 555 | S(VLR_AUTH_E_MS_AUTH_FAIL), |
| 556 | .out_state_mask = S(VLR_SUB_AS_AUTH_FAILED) | |
| 557 | S(VLR_SUB_AS_AUTHENTICATED), |
| 558 | .action = auth_fsm_wait_auth_resp_resync, |
| 559 | }, |
| 560 | [VLR_SUB_AS_WAIT_ID_IMSI] = { |
| 561 | .name = OSMO_STRINGIFY(VLR_SUB_AS_WAIT_ID_IMSI), |
| 562 | .in_event_mask = S(VLR_AUTH_E_MS_ID_IMSI), |
| 563 | .out_state_mask = S(VLR_SUB_AS_NEEDS_AUTH), |
| 564 | .action = auth_fsm_wait_imsi, |
| 565 | }, |
| 566 | [VLR_SUB_AS_AUTHENTICATED] = { |
| 567 | .name = OSMO_STRINGIFY(VLR_SUB_AS_AUTHENTICATED), |
| 568 | .in_event_mask = 0, |
| 569 | .out_state_mask = 0, |
| 570 | }, |
| 571 | [VLR_SUB_AS_AUTH_FAILED] = { |
| 572 | .name = OSMO_STRINGIFY(VLR_SUB_AS_AUTH_FAILED), |
| 573 | .in_event_mask = 0, |
| 574 | .out_state_mask = 0, |
| 575 | .onenter = auth_fsm_onenter_failed, |
| 576 | }, |
| 577 | }; |
| 578 | |
| 579 | struct osmo_fsm vlr_auth_fsm = { |
| 580 | .name = "VLR_Authenticate", |
| 581 | .states = auth_fsm_states, |
| 582 | .num_states = ARRAY_SIZE(auth_fsm_states), |
| 583 | .allstate_event_mask = 0, |
| 584 | .allstate_action = NULL, |
| 585 | .log_subsys = DVLR, |
| 586 | .event_names = fsm_auth_event_names, |
| 587 | }; |
| 588 | |
| 589 | /*********************************************************************** |
| 590 | * User API (for SGSN/MSC code) |
| 591 | ***********************************************************************/ |
| 592 | |
| 593 | /* MSC->VLR: Start Procedure Authenticate_VLR (TS 23.012 Ch. 4.1.2.2) */ |
| 594 | struct osmo_fsm_inst *auth_fsm_start(struct vlr_subscr *vsub, |
| 595 | uint32_t log_level, |
| 596 | struct osmo_fsm_inst *parent, |
| 597 | uint32_t parent_term_event, |
| 598 | bool is_r99, |
| 599 | bool is_utran) |
| 600 | { |
| 601 | struct osmo_fsm_inst *fi; |
| 602 | struct auth_fsm_priv *afp; |
| 603 | |
| 604 | fi = osmo_fsm_inst_alloc_child(&vlr_auth_fsm, parent, |
| 605 | parent_term_event); |
Neels Hofmeyr | c698ab9 | 2017-11-07 13:23:13 +0100 | [diff] [blame] | 606 | if (!fi) { |
| 607 | osmo_fsm_inst_dispatch(parent, parent_term_event, 0); |
| 608 | return NULL; |
| 609 | } |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 610 | |
| 611 | afp = talloc_zero(fi, struct auth_fsm_priv); |
| 612 | if (!afp) { |
| 613 | osmo_fsm_inst_dispatch(parent, parent_term_event, 0); |
| 614 | return NULL; |
| 615 | } |
| 616 | |
| 617 | afp->vsub = vsub; |
| 618 | if (vsub->imsi[0]) |
| 619 | afp->by_imsi = true; |
| 620 | afp->is_r99 = is_r99; |
| 621 | afp->is_utran = is_utran; |
| 622 | fi->priv = afp; |
| 623 | vsub->auth_fsm = fi; |
| 624 | |
| 625 | osmo_fsm_inst_dispatch(fi, VLR_AUTH_E_START, NULL); |
| 626 | |
| 627 | return fi; |
| 628 | } |