Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 1 | /* Osmocom Visitor Location Register (VLR): Access Request FSMs */ |
| 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 | #include <osmocom/core/fsm.h> |
| 23 | #include <osmocom/gsm/gsup.h> |
Max | 43b01b0 | 2017-09-15 11:22:30 +0200 | [diff] [blame] | 24 | #include <osmocom/gsm/gsm48.h> |
Neels Hofmeyr | 9084396 | 2017-09-04 15:04:35 +0200 | [diff] [blame] | 25 | #include <osmocom/msc/vlr.h> |
| 26 | #include <osmocom/msc/debug.h> |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 27 | |
| 28 | #include "vlr_core.h" |
| 29 | #include "vlr_auth_fsm.h" |
| 30 | #include "vlr_lu_fsm.h" |
| 31 | #include "vlr_access_req_fsm.h" |
| 32 | |
| 33 | #define S(x) (1 << (x)) |
| 34 | |
| 35 | /*********************************************************************** |
| 36 | * Process_Access_Request_VLR, TS 29.002 Chapter 25.4.2 |
| 37 | ***********************************************************************/ |
| 38 | |
| 39 | const struct value_string vlr_proc_arq_result_names[] = { |
| 40 | OSMO_VALUE_STRING(VLR_PR_ARQ_RES_NONE), |
| 41 | OSMO_VALUE_STRING(VLR_PR_ARQ_RES_SYSTEM_FAILURE), |
| 42 | OSMO_VALUE_STRING(VLR_PR_ARQ_RES_ILLEGAL_SUBSCR), |
| 43 | OSMO_VALUE_STRING(VLR_PR_ARQ_RES_UNIDENT_SUBSCR), |
| 44 | OSMO_VALUE_STRING(VLR_PR_ARQ_RES_ROAMING_NOTALLOWED), |
| 45 | OSMO_VALUE_STRING(VLR_PR_ARQ_RES_ILLEGAL_EQUIP), |
| 46 | OSMO_VALUE_STRING(VLR_PR_ARQ_RES_UNKNOWN_ERROR), |
| 47 | OSMO_VALUE_STRING(VLR_PR_ARQ_RES_TIMEOUT), |
| 48 | OSMO_VALUE_STRING(VLR_PR_ARQ_RES_PASSED), |
| 49 | { 0, NULL } |
| 50 | }; |
| 51 | |
| 52 | static const struct value_string proc_arq_vlr_event_names[] = { |
| 53 | OSMO_VALUE_STRING(PR_ARQ_E_START), |
| 54 | OSMO_VALUE_STRING(PR_ARQ_E_ID_IMSI), |
| 55 | OSMO_VALUE_STRING(PR_ARQ_E_AUTH_RES), |
| 56 | OSMO_VALUE_STRING(PR_ARQ_E_CIPH_RES), |
| 57 | OSMO_VALUE_STRING(PR_ARQ_E_UPD_LOC_RES), |
| 58 | OSMO_VALUE_STRING(PR_ARQ_E_TRACE_RES), |
| 59 | OSMO_VALUE_STRING(PR_ARQ_E_IMEI_RES), |
| 60 | OSMO_VALUE_STRING(PR_ARQ_E_PRES_RES), |
| 61 | OSMO_VALUE_STRING(PR_ARQ_E_TMSI_ACK), |
| 62 | { 0, NULL } |
| 63 | }; |
| 64 | |
| 65 | struct proc_arq_priv { |
| 66 | struct vlr_instance *vlr; |
| 67 | struct vlr_subscr *vsub; |
| 68 | void *msc_conn_ref; |
| 69 | struct osmo_fsm_inst *ul_child_fsm; |
| 70 | struct osmo_fsm_inst *sub_pres_vlr_fsm; |
| 71 | uint32_t parent_event_success; |
| 72 | uint32_t parent_event_failure; |
| 73 | void *parent_event_data; |
| 74 | |
| 75 | enum vlr_parq_type type; |
| 76 | enum vlr_proc_arq_result result; |
| 77 | bool by_tmsi; |
| 78 | char imsi[16]; |
| 79 | uint32_t tmsi; |
| 80 | struct osmo_location_area_id lai; |
| 81 | bool authentication_required; |
Harald Welte | 71c51df | 2017-12-23 18:51:48 +0100 | [diff] [blame] | 82 | bool ciphering_required; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 83 | bool is_r99; |
| 84 | bool is_utran; |
| 85 | bool implicitly_accepted_parq_by_ciphering_cmd; |
| 86 | }; |
| 87 | |
| 88 | static void assoc_par_with_subscr(struct osmo_fsm_inst *fi, struct vlr_subscr *vsub) |
| 89 | { |
| 90 | struct proc_arq_priv *par = fi->priv; |
| 91 | struct vlr_instance *vlr = par->vlr; |
| 92 | |
| 93 | vsub->msc_conn_ref = par->msc_conn_ref; |
| 94 | par->vsub = vsub; |
| 95 | /* Tell MSC to associate this subscriber with the given |
| 96 | * connection */ |
| 97 | vlr->ops.subscr_assoc(par->msc_conn_ref, par->vsub); |
| 98 | } |
| 99 | |
| 100 | #define proc_arq_fsm_done(fi, res) _proc_arq_fsm_done(fi, res, __FILE__, __LINE__) |
| 101 | static void _proc_arq_fsm_done(struct osmo_fsm_inst *fi, |
| 102 | enum vlr_proc_arq_result res, |
| 103 | const char *file, int line) |
| 104 | { |
| 105 | struct proc_arq_priv *par = fi->priv; |
| 106 | LOGPFSMSRC(fi, file, line, "proc_arq_fsm_done(%s)\n", |
| 107 | vlr_proc_arq_result_name(res)); |
| 108 | par->result = res; |
| 109 | osmo_fsm_inst_state_chg(fi, PR_ARQ_S_DONE, 0, 0); |
| 110 | } |
| 111 | |
| 112 | static void proc_arq_vlr_dispatch_result(struct osmo_fsm_inst *fi, |
| 113 | uint32_t prev_state) |
| 114 | { |
| 115 | struct proc_arq_priv *par = fi->priv; |
| 116 | bool success; |
| 117 | int rc; |
| 118 | LOGPFSM(fi, "Process Access Request result: %s\n", |
| 119 | vlr_proc_arq_result_name(par->result)); |
| 120 | |
| 121 | success = (par->result == VLR_PR_ARQ_RES_PASSED); |
| 122 | |
| 123 | /* It would be logical to first dispatch the success event to the |
| 124 | * parent FSM, but that could start actions that send messages to the |
| 125 | * MS. Rather send the CM Service Accept message first and then signal |
| 126 | * success. Since messages are handled synchronously, the success event |
| 127 | * will be processed before we handle new incoming data from the MS. */ |
| 128 | |
| 129 | if (par->type == VLR_PR_ARQ_T_CM_SERV_REQ) { |
| 130 | if (success |
| 131 | && !par->implicitly_accepted_parq_by_ciphering_cmd) { |
| 132 | rc = par->vlr->ops.tx_cm_serv_acc(par->msc_conn_ref); |
| 133 | if (rc) { |
| 134 | LOGPFSML(fi, LOGL_ERROR, |
| 135 | "Failed to send CM Service Accept\n"); |
| 136 | success = false; |
| 137 | } |
| 138 | } |
| 139 | if (!success) { |
| 140 | rc = par->vlr->ops.tx_cm_serv_rej(par->msc_conn_ref, |
| 141 | par->result); |
| 142 | if (rc) |
| 143 | LOGPFSML(fi, LOGL_ERROR, |
| 144 | "Failed to send CM Service Reject\n"); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /* For VLR_PR_ARQ_T_PAGING_RESP, there is nothing to send. The conn_fsm |
| 149 | * will start handling pending paging transactions. */ |
| 150 | |
| 151 | if (!fi->proc.parent) { |
| 152 | LOGPFSML(fi, LOGL_ERROR, "No parent FSM"); |
| 153 | return; |
| 154 | } |
| 155 | osmo_fsm_inst_dispatch(fi->proc.parent, |
| 156 | success ? par->parent_event_success |
| 157 | : par->parent_event_failure, |
| 158 | par->parent_event_data); |
| 159 | } |
| 160 | |
| 161 | void proc_arq_vlr_cleanup(struct osmo_fsm_inst *fi, |
| 162 | enum osmo_fsm_term_cause cause) |
| 163 | { |
| 164 | struct proc_arq_priv *par = fi->priv; |
| 165 | if (par->vsub && par->vsub->proc_arq_fsm == fi) |
| 166 | par->vsub->proc_arq_fsm = NULL; |
| 167 | } |
| 168 | |
| 169 | static void _proc_arq_vlr_post_imei(struct osmo_fsm_inst *fi) |
| 170 | { |
| 171 | struct proc_arq_priv *par = fi->priv; |
| 172 | struct vlr_subscr *vsub = par->vsub; |
| 173 | |
| 174 | LOGPFSM(fi, "%s()\n", __func__); |
| 175 | |
Neels Hofmeyr | ef9126c | 2017-07-18 15:38:39 +0200 | [diff] [blame] | 176 | /* See 3GPP TS 29.002 Proc_Acc_Req_VLR3. */ |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 177 | /* TODO: Identity := IMSI */ |
| 178 | if (0 /* TODO: TMSI reallocation at access: vlr->cfg.alloc_tmsi_arq */) { |
| 179 | vlr_subscr_alloc_tmsi(vsub); |
| 180 | /* TODO: forward TMSI to MS, wait for TMSI |
| 181 | * REALLOC COMPLETE */ |
| 182 | /* TODO: Freeze old TMSI */ |
| 183 | osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_TMSI_ACK, 0, 0); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | proc_arq_fsm_done(fi, VLR_PR_ARQ_RES_PASSED); |
| 188 | } |
| 189 | |
| 190 | static void _proc_arq_vlr_post_trace(struct osmo_fsm_inst *fi) |
| 191 | { |
| 192 | struct proc_arq_priv *par = fi->priv; |
| 193 | struct vlr_subscr *vsub = par->vsub; |
| 194 | struct vlr_instance *vlr = vsub->vlr; |
| 195 | |
| 196 | LOGPFSM(fi, "%s()\n", __func__); |
| 197 | |
| 198 | /* Node 3 */ |
Neels Hofmeyr | ef9126c | 2017-07-18 15:38:39 +0200 | [diff] [blame] | 199 | /* See 3GPP TS 29.002 Proc_Acc_Req_VLR3. */ |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 200 | if (0 /* IMEI check required */) { |
| 201 | /* Chck_IMEI_VLR */ |
| 202 | vlr->ops.tx_id_req(par->msc_conn_ref, GSM_MI_TYPE_IMEI); |
| 203 | osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_CHECK_IMEI, |
| 204 | vlr_timer(vlr, 3270), 3270); |
| 205 | } else |
| 206 | _proc_arq_vlr_post_imei(fi); |
| 207 | } |
| 208 | |
| 209 | /* After Subscriber_Present_VLR */ |
| 210 | static void _proc_arq_vlr_post_pres(struct osmo_fsm_inst *fi) |
| 211 | { |
| 212 | LOGPFSM(fi, "%s()\n", __func__); |
Neels Hofmeyr | ef9126c | 2017-07-18 15:38:39 +0200 | [diff] [blame] | 213 | /* See 3GPP TS 29.002 Proc_Acc_Req_VLR3. */ |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 214 | if (0 /* TODO: tracing required */) { |
| 215 | /* TODO: Trace_Subscriber_Activity_VLR */ |
| 216 | osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_TRACE_SUB, 0, 0); |
| 217 | } |
| 218 | _proc_arq_vlr_post_trace(fi); |
| 219 | } |
| 220 | |
| 221 | /* After Update_Location_Child_VLR */ |
| 222 | static void _proc_arq_vlr_node2_post_vlr(struct osmo_fsm_inst *fi) |
| 223 | { |
| 224 | struct proc_arq_priv *par = fi->priv; |
| 225 | struct vlr_subscr *vsub = par->vsub; |
| 226 | |
| 227 | LOGPFSM(fi, "%s()\n", __func__); |
| 228 | |
| 229 | if (!vsub->sub_dataconf_by_hlr_ind) { |
| 230 | /* Set User Error: Unidentified Subscriber */ |
| 231 | proc_arq_fsm_done(fi, VLR_PR_ARQ_RES_UNIDENT_SUBSCR); |
| 232 | return; |
| 233 | } |
Neels Hofmeyr | ef9126c | 2017-07-18 15:38:39 +0200 | [diff] [blame] | 234 | /* We don't feature location area specific blocking (yet). */ |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 235 | if (0 /* roaming not allowed in LA */) { |
| 236 | /* Set User Error: Roaming not allowed in this LA */ |
| 237 | proc_arq_fsm_done(fi, VLR_PR_ARQ_RES_ROAMING_NOTALLOWED); |
| 238 | return; |
| 239 | } |
| 240 | vsub->imsi_detached_flag = false; |
| 241 | if (vsub->ms_not_reachable_flag) { |
| 242 | /* Start Subscriber_Present_VLR */ |
| 243 | osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_SUB_PRES, 0, 0); |
Neels Hofmeyr | 1a5bcd5 | 2017-11-18 22:19:55 +0100 | [diff] [blame] | 244 | sub_pres_vlr_fsm_start(&par->sub_pres_vlr_fsm, fi, vsub, PR_ARQ_E_PRES_RES); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 245 | return; |
| 246 | } |
| 247 | _proc_arq_vlr_post_pres(fi); |
| 248 | } |
| 249 | |
| 250 | static void _proc_arq_vlr_node2_post_ciph(struct osmo_fsm_inst *fi) |
| 251 | { |
| 252 | struct proc_arq_priv *par = fi->priv; |
| 253 | struct vlr_subscr *vsub = par->vsub; |
| 254 | |
| 255 | LOGPFSM(fi, "%s()\n", __func__); |
| 256 | |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 257 | if (par->is_utran) { |
| 258 | int rc; |
| 259 | rc = par->vlr->ops.tx_common_id(par->msc_conn_ref); |
| 260 | if (rc) |
| 261 | LOGPFSML(fi, LOGL_ERROR, |
| 262 | "Error while sending Common ID (%d)\n", rc); |
| 263 | } |
| 264 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 265 | vsub->conf_by_radio_contact_ind = true; |
| 266 | if (vsub->loc_conf_in_hlr_ind == false) { |
| 267 | /* start Update_Location_Child_VLR. WE use |
| 268 | * Update_HLR_VLR instead, the differences appear |
| 269 | * insignificant for now. */ |
| 270 | par->ul_child_fsm = upd_hlr_vlr_proc_start(fi, vsub, |
| 271 | PR_ARQ_E_UPD_LOC_RES); |
| 272 | osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_UPD_LOC_CHILD, 0, 0); |
| 273 | return; |
| 274 | } |
| 275 | _proc_arq_vlr_node2_post_vlr(fi); |
| 276 | } |
| 277 | |
| 278 | static bool is_ciph_required(struct proc_arq_priv *par) |
| 279 | { |
Harald Welte | 71c51df | 2017-12-23 18:51:48 +0100 | [diff] [blame] | 280 | return par->ciphering_required; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static void _proc_arq_vlr_node2(struct osmo_fsm_inst *fi) |
| 284 | { |
| 285 | struct proc_arq_priv *par = fi->priv; |
| 286 | struct vlr_subscr *vsub = par->vsub; |
Neels Hofmeyr | 7795a19 | 2018-03-10 00:26:36 +0100 | [diff] [blame] | 287 | bool umts_aka; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 288 | |
| 289 | LOGPFSM(fi, "%s()\n", __func__); |
| 290 | |
| 291 | if (!is_ciph_required(par)) { |
| 292 | _proc_arq_vlr_node2_post_ciph(fi); |
| 293 | return; |
| 294 | } |
| 295 | |
Neels Hofmeyr | 7795a19 | 2018-03-10 00:26:36 +0100 | [diff] [blame] | 296 | switch (vsub->sec_ctx) { |
| 297 | case VLR_SEC_CTX_GSM: |
| 298 | umts_aka = false; |
| 299 | break; |
| 300 | case VLR_SEC_CTX_UMTS: |
| 301 | umts_aka = true; |
| 302 | break; |
| 303 | default: |
| 304 | LOGPFSML(fi, LOGL_ERROR, "Cannot start ciphering, security context is not established\n"); |
| 305 | proc_arq_fsm_done(fi, VLR_PR_ARQ_RES_SYSTEM_FAILURE); |
| 306 | return; |
| 307 | } |
| 308 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 309 | if (vlr_set_ciph_mode(vsub->vlr, fi, par->msc_conn_ref, |
| 310 | par->ciphering_required, |
Neels Hofmeyr | 7795a19 | 2018-03-10 00:26:36 +0100 | [diff] [blame] | 311 | umts_aka, |
Neels Hofmeyr | 54a706c | 2017-07-18 15:39:27 +0200 | [diff] [blame] | 312 | vsub->vlr->cfg.retrieve_imeisv_ciphered)) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 313 | LOGPFSML(fi, LOGL_ERROR, |
| 314 | "Failed to send Ciphering Mode Command\n"); |
| 315 | proc_arq_fsm_done(fi, VLR_PR_ARQ_RES_SYSTEM_FAILURE); |
| 316 | return; |
| 317 | } |
| 318 | |
| 319 | par->implicitly_accepted_parq_by_ciphering_cmd = true; |
| 320 | osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_CIPH, 0, 0); |
| 321 | } |
| 322 | |
| 323 | static bool is_auth_required(struct proc_arq_priv *par) |
| 324 | { |
| 325 | /* The cases where the authentication procedure should be used |
| 326 | * are defined in 3GPP TS 33.102 */ |
| 327 | /* For now we use a default value passed in to vlr_lu_fsm(). */ |
Harald Welte | 71c51df | 2017-12-23 18:51:48 +0100 | [diff] [blame] | 328 | return par->authentication_required || par->ciphering_required; |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /* after the IMSI is known */ |
| 332 | static void proc_arq_vlr_fn_post_imsi(struct osmo_fsm_inst *fi) |
| 333 | { |
| 334 | struct proc_arq_priv *par = fi->priv; |
| 335 | struct vlr_subscr *vsub = par->vsub; |
| 336 | |
| 337 | LOGPFSM(fi, "%s()\n", __func__); |
| 338 | |
| 339 | OSMO_ASSERT(vsub); |
| 340 | |
| 341 | /* TODO: Identity IMEI -> System Failure */ |
| 342 | if (is_auth_required(par)) { |
| 343 | osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_AUTH, |
| 344 | 0, 0); |
| 345 | vsub->auth_fsm = auth_fsm_start(vsub, fi->log_level, fi, |
| 346 | PR_ARQ_E_AUTH_RES, |
| 347 | par->is_r99, |
| 348 | par->is_utran); |
| 349 | } else { |
| 350 | _proc_arq_vlr_node2(fi); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | static void proc_arq_vlr_fn_init(struct osmo_fsm_inst *fi, |
| 355 | uint32_t event, void *data) |
| 356 | { |
| 357 | struct proc_arq_priv *par = fi->priv; |
| 358 | struct vlr_instance *vlr = par->vlr; |
| 359 | struct vlr_subscr *vsub = NULL; |
| 360 | |
| 361 | OSMO_ASSERT(event == PR_ARQ_E_START); |
| 362 | |
| 363 | /* Obtain_Identity_VLR */ |
| 364 | if (!par->by_tmsi) { |
| 365 | /* IMSI was included */ |
| 366 | vsub = vlr_subscr_find_by_imsi(par->vlr, par->imsi); |
| 367 | } else { |
| 368 | /* TMSI was included */ |
| 369 | vsub = vlr_subscr_find_by_tmsi(par->vlr, par->tmsi); |
| 370 | } |
| 371 | if (vsub) { |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 372 | log_set_context(LOG_CTX_VLR_SUBSCR, vsub); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 373 | if (vsub->proc_arq_fsm && fi != vsub->proc_arq_fsm) { |
| 374 | LOGPFSML(fi, LOGL_ERROR, |
| 375 | "Another proc_arq_fsm is already" |
| 376 | " associated with subscr %s," |
| 377 | " terminating the other FSM.\n", |
| 378 | vlr_subscr_name(vsub)); |
| 379 | proc_arq_fsm_done(vsub->proc_arq_fsm, |
| 380 | VLR_PR_ARQ_RES_SYSTEM_FAILURE); |
| 381 | } |
| 382 | vsub->proc_arq_fsm = fi; |
| 383 | assoc_par_with_subscr(fi, vsub); |
| 384 | proc_arq_vlr_fn_post_imsi(fi); |
| 385 | vlr_subscr_put(vsub); |
| 386 | return; |
| 387 | } |
| 388 | /* No VSUB could be resolved. What now? */ |
| 389 | |
| 390 | if (!par->by_tmsi) { |
| 391 | /* We couldn't find a subscriber even by IMSI, |
| 392 | * Set User Error: Unidentified Subscriber */ |
| 393 | proc_arq_fsm_done(fi, VLR_PR_ARQ_RES_UNIDENT_SUBSCR); |
| 394 | return; |
| 395 | } else { |
| 396 | /* TMSI was included, are we permitted to use it? */ |
| 397 | if (vlr->cfg.parq_retrieve_imsi) { |
| 398 | /* Obtain_IMSI_VLR */ |
| 399 | osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_OBTAIN_IMSI, |
| 400 | vlr_timer(vlr, 3270), 3270); |
| 401 | return; |
| 402 | } else { |
| 403 | /* Set User Error: Unidentified Subscriber */ |
| 404 | proc_arq_fsm_done(fi, VLR_PR_ARQ_RES_UNIDENT_SUBSCR); |
| 405 | return; |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | /* ID REQ(IMSI) has returned */ |
| 411 | static void proc_arq_vlr_fn_w_obt_imsi(struct osmo_fsm_inst *fi, |
| 412 | uint32_t event, void *data) |
| 413 | { |
| 414 | struct proc_arq_priv *par = fi->priv; |
| 415 | struct vlr_instance *vlr = par->vlr; |
| 416 | struct vlr_subscr *vsub; |
| 417 | |
| 418 | OSMO_ASSERT(event == PR_ARQ_E_ID_IMSI); |
| 419 | |
| 420 | vsub = vlr_subscr_find_by_imsi(vlr, par->imsi); |
| 421 | if (!vsub) { |
| 422 | /* Set User Error: Unidentified Subscriber */ |
| 423 | proc_arq_fsm_done(fi, VLR_PR_ARQ_RES_UNIDENT_SUBSCR); |
| 424 | return; |
| 425 | } |
| 426 | assoc_par_with_subscr(fi, vsub); |
| 427 | proc_arq_vlr_fn_post_imsi(fi); |
| 428 | vlr_subscr_put(vsub); |
| 429 | } |
| 430 | |
| 431 | /* Authenticate_VLR has completed */ |
| 432 | static void proc_arq_vlr_fn_w_auth(struct osmo_fsm_inst *fi, |
| 433 | uint32_t event, void *data) |
| 434 | { |
| 435 | enum vlr_auth_fsm_result res; |
| 436 | enum vlr_proc_arq_result ret; |
| 437 | |
| 438 | OSMO_ASSERT(event == PR_ARQ_E_AUTH_RES); |
| 439 | |
| 440 | res = data ? *(enum vlr_auth_fsm_result*)data : -1; |
| 441 | LOGPFSM(fi, "got %s\n", vlr_auth_fsm_result_name(res)); |
| 442 | |
| 443 | switch (res) { |
| 444 | case VLR_AUTH_RES_PASSED: |
| 445 | /* Node 2 */ |
| 446 | _proc_arq_vlr_node2(fi); |
| 447 | return; |
| 448 | case VLR_AUTH_RES_ABORTED: |
| 449 | /* Error */ |
| 450 | ret = VLR_PR_ARQ_RES_UNKNOWN_ERROR; |
| 451 | break; |
| 452 | case VLR_AUTH_RES_UNKNOWN_SUBSCR: |
| 453 | /* Set User Error: Unidentified Subscriber */ |
| 454 | ret = VLR_PR_ARQ_RES_UNIDENT_SUBSCR; |
| 455 | break; |
| 456 | case VLR_AUTH_RES_AUTH_FAILED: |
| 457 | /* Set User Error: Illegal Subscriber */ |
| 458 | ret = VLR_PR_ARQ_RES_ILLEGAL_SUBSCR; |
| 459 | break; |
| 460 | case VLR_AUTH_RES_PROC_ERR: |
| 461 | /* Set User Error: System failure */ |
| 462 | ret = VLR_PR_ARQ_RES_SYSTEM_FAILURE; |
| 463 | break; |
| 464 | default: |
| 465 | LOGPFSML(fi, LOGL_ERROR, "Unexpected vlr_auth_fsm_result value: %d (data=%p)\n", res, data); |
| 466 | ret = VLR_PR_ARQ_RES_UNKNOWN_ERROR; |
| 467 | break; |
| 468 | } |
| 469 | |
| 470 | /* send process_access_req response to caller, enter error state */ |
| 471 | proc_arq_fsm_done(fi, ret); |
| 472 | } |
| 473 | |
| 474 | static void proc_arq_vlr_fn_w_ciph(struct osmo_fsm_inst *fi, |
| 475 | uint32_t event, void *data) |
| 476 | { |
| 477 | struct proc_arq_priv *par = fi->priv; |
| 478 | struct vlr_subscr *vsub = par->vsub; |
| 479 | struct vlr_ciph_result res = { .cause = VLR_CIPH_REJECT }; |
| 480 | |
| 481 | OSMO_ASSERT(event == PR_ARQ_E_CIPH_RES); |
| 482 | |
| 483 | if (!data) |
| 484 | LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: NULL\n"); |
| 485 | else |
| 486 | res = *(struct vlr_ciph_result*)data; |
| 487 | |
| 488 | switch (res.cause) { |
| 489 | case VLR_CIPH_COMPL: |
| 490 | break; |
| 491 | case VLR_CIPH_REJECT: |
| 492 | LOGPFSM(fi, "ciphering rejected\n"); |
| 493 | proc_arq_fsm_done(fi, VLR_PR_ARQ_RES_ILLEGAL_SUBSCR); |
| 494 | return; |
| 495 | default: |
| 496 | LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: %d\n", |
| 497 | res.cause); |
| 498 | proc_arq_fsm_done(fi, VLR_PR_ARQ_RES_ILLEGAL_SUBSCR); |
| 499 | return; |
| 500 | } |
| 501 | |
| 502 | |
Neels Hofmeyr | fa10eda | 2018-03-13 01:22:01 +0100 | [diff] [blame] | 503 | if (*res.imeisv) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 504 | LOGPFSM(fi, "got IMEISV: %s\n", res.imeisv); |
| 505 | vlr_subscr_set_imeisv(vsub, res.imeisv); |
| 506 | } |
| 507 | _proc_arq_vlr_node2_post_ciph(fi); |
| 508 | } |
| 509 | |
| 510 | /* Update_Location_Child_VLR has completed */ |
| 511 | static void proc_arq_vlr_fn_w_upd_loc(struct osmo_fsm_inst *fi, |
| 512 | uint32_t event, void *data) |
| 513 | { |
| 514 | OSMO_ASSERT(event == PR_ARQ_E_UPD_LOC_RES); |
| 515 | |
| 516 | _proc_arq_vlr_node2_post_vlr(fi); |
| 517 | } |
| 518 | |
| 519 | /* Subscriber_Present_VLR has completed */ |
| 520 | static void proc_arq_vlr_fn_w_pres(struct osmo_fsm_inst *fi, |
| 521 | uint32_t event, void *data) |
| 522 | { |
| 523 | OSMO_ASSERT(event == PR_ARQ_E_PRES_RES); |
| 524 | |
| 525 | _proc_arq_vlr_post_pres(fi); |
| 526 | } |
| 527 | |
| 528 | static void proc_arq_vlr_fn_w_trace(struct osmo_fsm_inst *fi, |
| 529 | uint32_t event, void *data) |
| 530 | { |
| 531 | OSMO_ASSERT(event == PR_ARQ_E_TRACE_RES); |
| 532 | |
| 533 | _proc_arq_vlr_post_trace(fi); |
| 534 | } |
| 535 | |
| 536 | /* we have received the ID RESPONSE (IMEI) */ |
| 537 | static void proc_arq_vlr_fn_w_imei(struct osmo_fsm_inst *fi, |
| 538 | uint32_t event, void *data) |
| 539 | { |
| 540 | OSMO_ASSERT(event == PR_ARQ_E_IMEI_RES); |
| 541 | |
| 542 | _proc_arq_vlr_post_imei(fi); |
| 543 | } |
| 544 | |
| 545 | /* MSC tells us that MS has acknowleded TMSI re-allocation */ |
| 546 | static void proc_arq_vlr_fn_w_tmsi(struct osmo_fsm_inst *fi, |
| 547 | uint32_t event, void *data) |
| 548 | { |
| 549 | OSMO_ASSERT(event == PR_ARQ_E_TMSI_ACK); |
| 550 | |
| 551 | /* FIXME: check confirmation? unfreeze? */ |
| 552 | proc_arq_fsm_done(fi, VLR_PR_ARQ_RES_PASSED); |
| 553 | } |
| 554 | |
| 555 | static const struct osmo_fsm_state proc_arq_vlr_states[] = { |
| 556 | [PR_ARQ_S_INIT] = { |
| 557 | .name = OSMO_STRINGIFY(PR_ARQ_S_INIT), |
| 558 | .in_event_mask = S(PR_ARQ_E_START), |
| 559 | .out_state_mask = S(PR_ARQ_S_DONE) | |
| 560 | S(PR_ARQ_S_WAIT_OBTAIN_IMSI) | |
| 561 | S(PR_ARQ_S_WAIT_AUTH) | |
| 562 | S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) | |
| 563 | S(PR_ARQ_S_WAIT_SUB_PRES) | |
| 564 | S(PR_ARQ_S_WAIT_TRACE_SUB) | |
| 565 | S(PR_ARQ_S_WAIT_CHECK_IMEI) | |
| 566 | S(PR_ARQ_S_WAIT_TMSI_ACK), |
| 567 | .action = proc_arq_vlr_fn_init, |
| 568 | }, |
| 569 | [PR_ARQ_S_WAIT_OBTAIN_IMSI] = { |
| 570 | .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_OBTAIN_IMSI), |
| 571 | .in_event_mask = S(PR_ARQ_E_ID_IMSI), |
| 572 | .out_state_mask = S(PR_ARQ_S_DONE) | |
| 573 | S(PR_ARQ_S_WAIT_AUTH) | |
| 574 | S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) | |
| 575 | S(PR_ARQ_S_WAIT_SUB_PRES) | |
| 576 | S(PR_ARQ_S_WAIT_TRACE_SUB) | |
| 577 | S(PR_ARQ_S_WAIT_CHECK_IMEI) | |
| 578 | S(PR_ARQ_S_WAIT_TMSI_ACK), |
| 579 | .action = proc_arq_vlr_fn_w_obt_imsi, |
| 580 | }, |
| 581 | [PR_ARQ_S_WAIT_AUTH] = { |
| 582 | .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_AUTH), |
| 583 | .in_event_mask = S(PR_ARQ_E_AUTH_RES), |
| 584 | .out_state_mask = S(PR_ARQ_S_DONE) | |
| 585 | S(PR_ARQ_S_WAIT_CIPH) | |
| 586 | S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) | |
| 587 | S(PR_ARQ_S_WAIT_SUB_PRES) | |
| 588 | S(PR_ARQ_S_WAIT_TRACE_SUB) | |
| 589 | S(PR_ARQ_S_WAIT_CHECK_IMEI) | |
| 590 | S(PR_ARQ_S_WAIT_TMSI_ACK), |
| 591 | .action = proc_arq_vlr_fn_w_auth, |
| 592 | }, |
| 593 | [PR_ARQ_S_WAIT_CIPH] = { |
| 594 | .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_CIPH), |
| 595 | .in_event_mask = S(PR_ARQ_E_CIPH_RES), |
| 596 | .out_state_mask = S(PR_ARQ_S_DONE) | |
| 597 | S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) | |
| 598 | S(PR_ARQ_S_WAIT_SUB_PRES) | |
| 599 | S(PR_ARQ_S_WAIT_TRACE_SUB) | |
| 600 | S(PR_ARQ_S_WAIT_CHECK_IMEI) | |
| 601 | S(PR_ARQ_S_WAIT_TMSI_ACK), |
| 602 | .action = proc_arq_vlr_fn_w_ciph, |
| 603 | }, |
| 604 | [PR_ARQ_S_WAIT_UPD_LOC_CHILD] = { |
| 605 | .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_UPD_LOC_CHILD), |
| 606 | .in_event_mask = S(PR_ARQ_E_UPD_LOC_RES), |
| 607 | .out_state_mask = S(PR_ARQ_S_DONE) | |
| 608 | S(PR_ARQ_S_WAIT_SUB_PRES) | |
| 609 | S(PR_ARQ_S_WAIT_TRACE_SUB) | |
| 610 | S(PR_ARQ_S_WAIT_CHECK_IMEI) | |
| 611 | S(PR_ARQ_S_WAIT_TMSI_ACK), |
| 612 | .action = proc_arq_vlr_fn_w_upd_loc, |
| 613 | }, |
| 614 | [PR_ARQ_S_WAIT_SUB_PRES] = { |
| 615 | .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_SUB_PRES), |
| 616 | .in_event_mask = S(PR_ARQ_E_PRES_RES), |
| 617 | .out_state_mask = S(PR_ARQ_S_DONE) | |
| 618 | S(PR_ARQ_S_WAIT_TRACE_SUB) | |
| 619 | S(PR_ARQ_S_WAIT_CHECK_IMEI) | |
| 620 | S(PR_ARQ_S_WAIT_TMSI_ACK), |
| 621 | .action = proc_arq_vlr_fn_w_pres, |
| 622 | }, |
| 623 | [PR_ARQ_S_WAIT_TRACE_SUB] = { |
| 624 | .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_TRACE_SUB), |
| 625 | .in_event_mask = S(PR_ARQ_E_TRACE_RES), |
| 626 | .out_state_mask = S(PR_ARQ_S_DONE) | |
| 627 | S(PR_ARQ_S_WAIT_CHECK_IMEI) | |
| 628 | S(PR_ARQ_S_WAIT_TMSI_ACK), |
| 629 | .action = proc_arq_vlr_fn_w_trace, |
| 630 | }, |
| 631 | [PR_ARQ_S_WAIT_CHECK_IMEI] = { |
| 632 | .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_CHECK_IMEI), |
| 633 | .in_event_mask = S(PR_ARQ_E_IMEI_RES), |
| 634 | .out_state_mask = S(PR_ARQ_S_DONE) | |
| 635 | S(PR_ARQ_S_WAIT_TMSI_ACK), |
| 636 | .action = proc_arq_vlr_fn_w_imei, |
| 637 | }, |
| 638 | [PR_ARQ_S_WAIT_TMSI_ACK] = { |
| 639 | .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_TMSI_ACK), |
| 640 | .in_event_mask = S(PR_ARQ_E_TMSI_ACK), |
| 641 | .out_state_mask = S(PR_ARQ_S_DONE), |
| 642 | .action = proc_arq_vlr_fn_w_tmsi, |
| 643 | }, |
| 644 | [PR_ARQ_S_DONE] = { |
| 645 | .name = OSMO_STRINGIFY(PR_ARQ_S_DONE), |
| 646 | .onenter = proc_arq_vlr_dispatch_result, |
| 647 | }, |
| 648 | }; |
| 649 | |
| 650 | static struct osmo_fsm proc_arq_vlr_fsm = { |
| 651 | .name = "Process_Access_Request_VLR", |
| 652 | .states = proc_arq_vlr_states, |
| 653 | .num_states = ARRAY_SIZE(proc_arq_vlr_states), |
| 654 | .allstate_event_mask = 0, |
| 655 | .allstate_action = NULL, |
| 656 | .log_subsys = DVLR, |
| 657 | .event_names = proc_arq_vlr_event_names, |
| 658 | .cleanup = proc_arq_vlr_cleanup, |
| 659 | }; |
| 660 | |
| 661 | void |
| 662 | vlr_proc_acc_req(struct osmo_fsm_inst *parent, |
| 663 | uint32_t parent_event_success, |
| 664 | uint32_t parent_event_failure, |
| 665 | void *parent_event_data, |
| 666 | struct vlr_instance *vlr, void *msc_conn_ref, |
| 667 | enum vlr_parq_type type, const uint8_t *mi_lv, |
| 668 | const struct osmo_location_area_id *lai, |
| 669 | bool authentication_required, |
Harald Welte | 71c51df | 2017-12-23 18:51:48 +0100 | [diff] [blame] | 670 | bool ciphering_required, |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 671 | bool is_r99, bool is_utran) |
| 672 | { |
| 673 | struct osmo_fsm_inst *fi; |
| 674 | struct proc_arq_priv *par; |
| 675 | char mi_string[GSM48_MI_SIZE]; |
| 676 | uint8_t mi_type; |
| 677 | |
| 678 | fi = osmo_fsm_inst_alloc_child(&proc_arq_vlr_fsm, parent, |
| 679 | parent_event_failure); |
| 680 | if (!fi) |
| 681 | return; |
| 682 | |
| 683 | par = talloc_zero(fi, struct proc_arq_priv); |
| 684 | fi->priv = par; |
| 685 | par->vlr = vlr; |
| 686 | par->msc_conn_ref = msc_conn_ref; |
| 687 | par->type = type; |
| 688 | par->lai = *lai; |
| 689 | par->parent_event_success = parent_event_success; |
| 690 | par->parent_event_failure = parent_event_failure; |
| 691 | par->parent_event_data = parent_event_data; |
| 692 | par->authentication_required = authentication_required; |
| 693 | par->ciphering_required = ciphering_required; |
| 694 | par->is_r99 = is_r99; |
| 695 | par->is_utran = is_utran; |
| 696 | |
| 697 | LOGPFSM(fi, "rev=%s net=%s%s%s\n", |
| 698 | is_r99 ? "R99" : "GSM", |
| 699 | is_utran ? "UTRAN" : "GERAN", |
| 700 | (authentication_required || ciphering_required)? |
| 701 | " Auth" : " (no Auth)", |
| 702 | (authentication_required || ciphering_required)? |
| 703 | (ciphering_required? "+Ciph" : " (no Ciph)") |
| 704 | : ""); |
| 705 | |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 706 | if (is_utran && !authentication_required) |
| 707 | LOGPFSML(fi, LOGL_ERROR, |
| 708 | "Authentication off on UTRAN network. Good luck.\n"); |
| 709 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 710 | gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]); |
| 711 | mi_type = mi_lv[1] & GSM_MI_TYPE_MASK; |
| 712 | switch (mi_type) { |
| 713 | case GSM_MI_TYPE_IMSI: |
| 714 | strncpy(par->imsi, mi_string, sizeof(par->imsi)-1); |
| 715 | par->imsi[sizeof(par->imsi)-1] = '\0'; |
| 716 | par->by_tmsi = false; |
| 717 | break; |
| 718 | case GSM_MI_TYPE_TMSI: |
| 719 | par->by_tmsi = true; |
| 720 | par->tmsi = osmo_load32be(mi_lv+2); |
| 721 | break; |
| 722 | case GSM_MI_TYPE_IMEI: |
| 723 | /* TODO: IMEI (emergency call) */ |
| 724 | default: |
| 725 | /* FIXME: directly send reject? */ |
| 726 | proc_arq_fsm_done(fi, VLR_PR_ARQ_RES_UNIDENT_SUBSCR); |
| 727 | return; |
| 728 | } |
| 729 | |
| 730 | osmo_fsm_inst_dispatch(fi, PR_ARQ_E_START, NULL); |
| 731 | } |
| 732 | |
| 733 | /* Gracefully terminate an FSM created by vlr_proc_acc_req() in case of |
| 734 | * external timeout (i.e. from MSC). */ |
| 735 | void vlr_parq_conn_timeout(struct osmo_fsm_inst *fi) |
| 736 | { |
| 737 | if (!fi || fi->state == PR_ARQ_S_DONE) |
| 738 | return; |
| 739 | LOGPFSM(fi, "Connection timed out\n"); |
| 740 | proc_arq_fsm_done(fi, VLR_PR_ARQ_RES_TIMEOUT); |
| 741 | } |
| 742 | |
| 743 | |
| 744 | #if 0 |
| 745 | /*********************************************************************** |
| 746 | * Update_Location_Child_VLR, TS 29.002 Chapter 25.4.4 |
| 747 | ***********************************************************************/ |
| 748 | |
| 749 | enum upd_loc_child_vlr_state { |
| 750 | ULC_S_IDLE, |
| 751 | ULC_S_WAIT_HLR_RESP, |
| 752 | ULC_S_DONE, |
| 753 | }; |
| 754 | |
| 755 | enum upd_loc_child_vlr_event { |
| 756 | ULC_E_START, |
| 757 | }; |
| 758 | |
| 759 | static const struct value_string upd_loc_child_vlr_event_names[] = { |
| 760 | { ULC_E_START, "START" }, |
| 761 | { 0, NULL } |
| 762 | }; |
| 763 | |
| 764 | static void upd_loc_child_f_idle(struct osmo_fsm_inst *fi, uint32_t event, |
| 765 | void *data) |
| 766 | { |
| 767 | OSMO_ASSERT(event == ULC_E_START); |
| 768 | |
| 769 | /* send update location */ |
| 770 | } |
| 771 | |
| 772 | static void upd_loc_child_f_w_hlr(struct osmo_fsm_inst *fi, uint32_t event, |
| 773 | void *data) |
| 774 | { |
| 775 | } |
| 776 | |
| 777 | static const struct osmo_fsm_state upd_loc_child_vlr_states[] = { |
| 778 | [ULC_S_IDLE] = { |
| 779 | .in_event_mask = , |
| 780 | .out_state_mask = S(ULC_S_WAIT_HLR_RESP) | |
| 781 | S(ULC_S_DONE), |
| 782 | .name = "IDLE", |
| 783 | .action = upd_loc_child_f_idle, |
| 784 | }, |
| 785 | [ULC_S_WAIT_HLR_RESP] = { |
| 786 | .in_event_mask = , |
| 787 | .out_state_mask = S(ULC_S_DONE), |
| 788 | .name = "WAIT-HLR-RESP", |
| 789 | .action = upd_loc_child_f_w_hlr, |
| 790 | }, |
| 791 | [ULC_S_DONE] = { |
| 792 | .name = "DONE", |
| 793 | }, |
| 794 | }; |
| 795 | |
| 796 | static struct osmo_fsm upd_loc_child_vlr_fsm = { |
| 797 | .name = "Update_Location_Child_VLR", |
| 798 | .states = upd_loc_child_vlr_states, |
| 799 | .num_states = ARRAY_SIZE(upd_loc_child_vlr_states), |
| 800 | .log_subsys = DVLR, |
| 801 | .event_names = upd_loc_child_vlr_event_names, |
| 802 | }; |
| 803 | #endif |
| 804 | |
| 805 | void vlr_parq_fsm_init(void) |
| 806 | { |
| 807 | //osmo_fsm_register(&upd_loc_child_vlr_fsm); |
| 808 | osmo_fsm_register(&proc_arq_vlr_fsm); |
| 809 | } |