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; |
| 82 | enum vlr_ciph ciphering_required; |
| 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); |
| 244 | par->sub_pres_vlr_fsm = sub_pres_vlr_fsm_start(fi, vsub, |
| 245 | PR_ARQ_E_PRES_RES); |
| 246 | return; |
| 247 | } |
| 248 | _proc_arq_vlr_post_pres(fi); |
| 249 | } |
| 250 | |
| 251 | static void _proc_arq_vlr_node2_post_ciph(struct osmo_fsm_inst *fi) |
| 252 | { |
| 253 | struct proc_arq_priv *par = fi->priv; |
| 254 | struct vlr_subscr *vsub = par->vsub; |
| 255 | |
| 256 | LOGPFSM(fi, "%s()\n", __func__); |
| 257 | |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 258 | if (par->is_utran) { |
| 259 | int rc; |
| 260 | rc = par->vlr->ops.tx_common_id(par->msc_conn_ref); |
| 261 | if (rc) |
| 262 | LOGPFSML(fi, LOGL_ERROR, |
| 263 | "Error while sending Common ID (%d)\n", rc); |
| 264 | } |
| 265 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 266 | vsub->conf_by_radio_contact_ind = true; |
| 267 | if (vsub->loc_conf_in_hlr_ind == false) { |
| 268 | /* start Update_Location_Child_VLR. WE use |
| 269 | * Update_HLR_VLR instead, the differences appear |
| 270 | * insignificant for now. */ |
| 271 | par->ul_child_fsm = upd_hlr_vlr_proc_start(fi, vsub, |
| 272 | PR_ARQ_E_UPD_LOC_RES); |
| 273 | osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_UPD_LOC_CHILD, 0, 0); |
| 274 | return; |
| 275 | } |
| 276 | _proc_arq_vlr_node2_post_vlr(fi); |
| 277 | } |
| 278 | |
| 279 | static bool is_ciph_required(struct proc_arq_priv *par) |
| 280 | { |
| 281 | return par->ciphering_required != VLR_CIPH_NONE; |
| 282 | } |
| 283 | |
| 284 | static void _proc_arq_vlr_node2(struct osmo_fsm_inst *fi) |
| 285 | { |
| 286 | struct proc_arq_priv *par = fi->priv; |
| 287 | struct vlr_subscr *vsub = par->vsub; |
| 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 | |
| 296 | if (vlr_set_ciph_mode(vsub->vlr, fi, par->msc_conn_ref, |
| 297 | par->ciphering_required, |
Neels Hofmeyr | 54a706c | 2017-07-18 15:39:27 +0200 | [diff] [blame] | 298 | vsub->vlr->cfg.retrieve_imeisv_ciphered)) { |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 299 | LOGPFSML(fi, LOGL_ERROR, |
| 300 | "Failed to send Ciphering Mode Command\n"); |
| 301 | proc_arq_fsm_done(fi, VLR_PR_ARQ_RES_SYSTEM_FAILURE); |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | par->implicitly_accepted_parq_by_ciphering_cmd = true; |
| 306 | osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_CIPH, 0, 0); |
| 307 | } |
| 308 | |
| 309 | static bool is_auth_required(struct proc_arq_priv *par) |
| 310 | { |
| 311 | /* The cases where the authentication procedure should be used |
| 312 | * are defined in 3GPP TS 33.102 */ |
| 313 | /* For now we use a default value passed in to vlr_lu_fsm(). */ |
| 314 | return par->authentication_required |
| 315 | || (par->ciphering_required != VLR_CIPH_NONE); |
| 316 | } |
| 317 | |
| 318 | /* after the IMSI is known */ |
| 319 | static void proc_arq_vlr_fn_post_imsi(struct osmo_fsm_inst *fi) |
| 320 | { |
| 321 | struct proc_arq_priv *par = fi->priv; |
| 322 | struct vlr_subscr *vsub = par->vsub; |
| 323 | |
| 324 | LOGPFSM(fi, "%s()\n", __func__); |
| 325 | |
| 326 | OSMO_ASSERT(vsub); |
| 327 | |
| 328 | /* TODO: Identity IMEI -> System Failure */ |
| 329 | if (is_auth_required(par)) { |
| 330 | osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_AUTH, |
| 331 | 0, 0); |
| 332 | vsub->auth_fsm = auth_fsm_start(vsub, fi->log_level, fi, |
| 333 | PR_ARQ_E_AUTH_RES, |
| 334 | par->is_r99, |
| 335 | par->is_utran); |
| 336 | } else { |
| 337 | _proc_arq_vlr_node2(fi); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | static void proc_arq_vlr_fn_init(struct osmo_fsm_inst *fi, |
| 342 | uint32_t event, void *data) |
| 343 | { |
| 344 | struct proc_arq_priv *par = fi->priv; |
| 345 | struct vlr_instance *vlr = par->vlr; |
| 346 | struct vlr_subscr *vsub = NULL; |
| 347 | |
| 348 | OSMO_ASSERT(event == PR_ARQ_E_START); |
| 349 | |
| 350 | /* Obtain_Identity_VLR */ |
| 351 | if (!par->by_tmsi) { |
| 352 | /* IMSI was included */ |
| 353 | vsub = vlr_subscr_find_by_imsi(par->vlr, par->imsi); |
| 354 | } else { |
| 355 | /* TMSI was included */ |
| 356 | vsub = vlr_subscr_find_by_tmsi(par->vlr, par->tmsi); |
| 357 | } |
| 358 | if (vsub) { |
Harald Welte | 2483f1b | 2016-06-19 18:06:02 +0200 | [diff] [blame] | 359 | log_set_context(LOG_CTX_VLR_SUBSCR, vsub); |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 360 | if (vsub->proc_arq_fsm && fi != vsub->proc_arq_fsm) { |
| 361 | LOGPFSML(fi, LOGL_ERROR, |
| 362 | "Another proc_arq_fsm is already" |
| 363 | " associated with subscr %s," |
| 364 | " terminating the other FSM.\n", |
| 365 | vlr_subscr_name(vsub)); |
| 366 | proc_arq_fsm_done(vsub->proc_arq_fsm, |
| 367 | VLR_PR_ARQ_RES_SYSTEM_FAILURE); |
| 368 | } |
| 369 | vsub->proc_arq_fsm = fi; |
| 370 | assoc_par_with_subscr(fi, vsub); |
| 371 | proc_arq_vlr_fn_post_imsi(fi); |
| 372 | vlr_subscr_put(vsub); |
| 373 | return; |
| 374 | } |
| 375 | /* No VSUB could be resolved. What now? */ |
| 376 | |
| 377 | if (!par->by_tmsi) { |
| 378 | /* We couldn't find a subscriber even by IMSI, |
| 379 | * Set User Error: Unidentified Subscriber */ |
| 380 | proc_arq_fsm_done(fi, VLR_PR_ARQ_RES_UNIDENT_SUBSCR); |
| 381 | return; |
| 382 | } else { |
| 383 | /* TMSI was included, are we permitted to use it? */ |
| 384 | if (vlr->cfg.parq_retrieve_imsi) { |
| 385 | /* Obtain_IMSI_VLR */ |
| 386 | osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_OBTAIN_IMSI, |
| 387 | vlr_timer(vlr, 3270), 3270); |
| 388 | return; |
| 389 | } else { |
| 390 | /* Set User Error: Unidentified Subscriber */ |
| 391 | proc_arq_fsm_done(fi, VLR_PR_ARQ_RES_UNIDENT_SUBSCR); |
| 392 | return; |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | /* ID REQ(IMSI) has returned */ |
| 398 | static void proc_arq_vlr_fn_w_obt_imsi(struct osmo_fsm_inst *fi, |
| 399 | uint32_t event, void *data) |
| 400 | { |
| 401 | struct proc_arq_priv *par = fi->priv; |
| 402 | struct vlr_instance *vlr = par->vlr; |
| 403 | struct vlr_subscr *vsub; |
| 404 | |
| 405 | OSMO_ASSERT(event == PR_ARQ_E_ID_IMSI); |
| 406 | |
| 407 | vsub = vlr_subscr_find_by_imsi(vlr, par->imsi); |
| 408 | if (!vsub) { |
| 409 | /* Set User Error: Unidentified Subscriber */ |
| 410 | proc_arq_fsm_done(fi, VLR_PR_ARQ_RES_UNIDENT_SUBSCR); |
| 411 | return; |
| 412 | } |
| 413 | assoc_par_with_subscr(fi, vsub); |
| 414 | proc_arq_vlr_fn_post_imsi(fi); |
| 415 | vlr_subscr_put(vsub); |
| 416 | } |
| 417 | |
| 418 | /* Authenticate_VLR has completed */ |
| 419 | static void proc_arq_vlr_fn_w_auth(struct osmo_fsm_inst *fi, |
| 420 | uint32_t event, void *data) |
| 421 | { |
| 422 | enum vlr_auth_fsm_result res; |
| 423 | enum vlr_proc_arq_result ret; |
| 424 | |
| 425 | OSMO_ASSERT(event == PR_ARQ_E_AUTH_RES); |
| 426 | |
| 427 | res = data ? *(enum vlr_auth_fsm_result*)data : -1; |
| 428 | LOGPFSM(fi, "got %s\n", vlr_auth_fsm_result_name(res)); |
| 429 | |
| 430 | switch (res) { |
| 431 | case VLR_AUTH_RES_PASSED: |
| 432 | /* Node 2 */ |
| 433 | _proc_arq_vlr_node2(fi); |
| 434 | return; |
| 435 | case VLR_AUTH_RES_ABORTED: |
| 436 | /* Error */ |
| 437 | ret = VLR_PR_ARQ_RES_UNKNOWN_ERROR; |
| 438 | break; |
| 439 | case VLR_AUTH_RES_UNKNOWN_SUBSCR: |
| 440 | /* Set User Error: Unidentified Subscriber */ |
| 441 | ret = VLR_PR_ARQ_RES_UNIDENT_SUBSCR; |
| 442 | break; |
| 443 | case VLR_AUTH_RES_AUTH_FAILED: |
| 444 | /* Set User Error: Illegal Subscriber */ |
| 445 | ret = VLR_PR_ARQ_RES_ILLEGAL_SUBSCR; |
| 446 | break; |
| 447 | case VLR_AUTH_RES_PROC_ERR: |
| 448 | /* Set User Error: System failure */ |
| 449 | ret = VLR_PR_ARQ_RES_SYSTEM_FAILURE; |
| 450 | break; |
| 451 | default: |
| 452 | LOGPFSML(fi, LOGL_ERROR, "Unexpected vlr_auth_fsm_result value: %d (data=%p)\n", res, data); |
| 453 | ret = VLR_PR_ARQ_RES_UNKNOWN_ERROR; |
| 454 | break; |
| 455 | } |
| 456 | |
| 457 | /* send process_access_req response to caller, enter error state */ |
| 458 | proc_arq_fsm_done(fi, ret); |
| 459 | } |
| 460 | |
| 461 | static void proc_arq_vlr_fn_w_ciph(struct osmo_fsm_inst *fi, |
| 462 | uint32_t event, void *data) |
| 463 | { |
| 464 | struct proc_arq_priv *par = fi->priv; |
| 465 | struct vlr_subscr *vsub = par->vsub; |
| 466 | struct vlr_ciph_result res = { .cause = VLR_CIPH_REJECT }; |
| 467 | |
| 468 | OSMO_ASSERT(event == PR_ARQ_E_CIPH_RES); |
| 469 | |
| 470 | if (!data) |
| 471 | LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: NULL\n"); |
| 472 | else |
| 473 | res = *(struct vlr_ciph_result*)data; |
| 474 | |
| 475 | switch (res.cause) { |
| 476 | case VLR_CIPH_COMPL: |
| 477 | break; |
| 478 | case VLR_CIPH_REJECT: |
| 479 | LOGPFSM(fi, "ciphering rejected\n"); |
| 480 | proc_arq_fsm_done(fi, VLR_PR_ARQ_RES_ILLEGAL_SUBSCR); |
| 481 | return; |
| 482 | default: |
| 483 | LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: %d\n", |
| 484 | res.cause); |
| 485 | proc_arq_fsm_done(fi, VLR_PR_ARQ_RES_ILLEGAL_SUBSCR); |
| 486 | return; |
| 487 | } |
| 488 | |
| 489 | |
| 490 | if (res.imeisv) { |
| 491 | LOGPFSM(fi, "got IMEISV: %s\n", res.imeisv); |
| 492 | vlr_subscr_set_imeisv(vsub, res.imeisv); |
| 493 | } |
| 494 | _proc_arq_vlr_node2_post_ciph(fi); |
| 495 | } |
| 496 | |
| 497 | /* Update_Location_Child_VLR has completed */ |
| 498 | static void proc_arq_vlr_fn_w_upd_loc(struct osmo_fsm_inst *fi, |
| 499 | uint32_t event, void *data) |
| 500 | { |
| 501 | OSMO_ASSERT(event == PR_ARQ_E_UPD_LOC_RES); |
| 502 | |
| 503 | _proc_arq_vlr_node2_post_vlr(fi); |
| 504 | } |
| 505 | |
| 506 | /* Subscriber_Present_VLR has completed */ |
| 507 | static void proc_arq_vlr_fn_w_pres(struct osmo_fsm_inst *fi, |
| 508 | uint32_t event, void *data) |
| 509 | { |
| 510 | OSMO_ASSERT(event == PR_ARQ_E_PRES_RES); |
| 511 | |
| 512 | _proc_arq_vlr_post_pres(fi); |
| 513 | } |
| 514 | |
| 515 | static void proc_arq_vlr_fn_w_trace(struct osmo_fsm_inst *fi, |
| 516 | uint32_t event, void *data) |
| 517 | { |
| 518 | OSMO_ASSERT(event == PR_ARQ_E_TRACE_RES); |
| 519 | |
| 520 | _proc_arq_vlr_post_trace(fi); |
| 521 | } |
| 522 | |
| 523 | /* we have received the ID RESPONSE (IMEI) */ |
| 524 | static void proc_arq_vlr_fn_w_imei(struct osmo_fsm_inst *fi, |
| 525 | uint32_t event, void *data) |
| 526 | { |
| 527 | OSMO_ASSERT(event == PR_ARQ_E_IMEI_RES); |
| 528 | |
| 529 | _proc_arq_vlr_post_imei(fi); |
| 530 | } |
| 531 | |
| 532 | /* MSC tells us that MS has acknowleded TMSI re-allocation */ |
| 533 | static void proc_arq_vlr_fn_w_tmsi(struct osmo_fsm_inst *fi, |
| 534 | uint32_t event, void *data) |
| 535 | { |
| 536 | OSMO_ASSERT(event == PR_ARQ_E_TMSI_ACK); |
| 537 | |
| 538 | /* FIXME: check confirmation? unfreeze? */ |
| 539 | proc_arq_fsm_done(fi, VLR_PR_ARQ_RES_PASSED); |
| 540 | } |
| 541 | |
| 542 | static const struct osmo_fsm_state proc_arq_vlr_states[] = { |
| 543 | [PR_ARQ_S_INIT] = { |
| 544 | .name = OSMO_STRINGIFY(PR_ARQ_S_INIT), |
| 545 | .in_event_mask = S(PR_ARQ_E_START), |
| 546 | .out_state_mask = S(PR_ARQ_S_DONE) | |
| 547 | S(PR_ARQ_S_WAIT_OBTAIN_IMSI) | |
| 548 | S(PR_ARQ_S_WAIT_AUTH) | |
| 549 | S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) | |
| 550 | S(PR_ARQ_S_WAIT_SUB_PRES) | |
| 551 | S(PR_ARQ_S_WAIT_TRACE_SUB) | |
| 552 | S(PR_ARQ_S_WAIT_CHECK_IMEI) | |
| 553 | S(PR_ARQ_S_WAIT_TMSI_ACK), |
| 554 | .action = proc_arq_vlr_fn_init, |
| 555 | }, |
| 556 | [PR_ARQ_S_WAIT_OBTAIN_IMSI] = { |
| 557 | .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_OBTAIN_IMSI), |
| 558 | .in_event_mask = S(PR_ARQ_E_ID_IMSI), |
| 559 | .out_state_mask = S(PR_ARQ_S_DONE) | |
| 560 | S(PR_ARQ_S_WAIT_AUTH) | |
| 561 | S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) | |
| 562 | S(PR_ARQ_S_WAIT_SUB_PRES) | |
| 563 | S(PR_ARQ_S_WAIT_TRACE_SUB) | |
| 564 | S(PR_ARQ_S_WAIT_CHECK_IMEI) | |
| 565 | S(PR_ARQ_S_WAIT_TMSI_ACK), |
| 566 | .action = proc_arq_vlr_fn_w_obt_imsi, |
| 567 | }, |
| 568 | [PR_ARQ_S_WAIT_AUTH] = { |
| 569 | .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_AUTH), |
| 570 | .in_event_mask = S(PR_ARQ_E_AUTH_RES), |
| 571 | .out_state_mask = S(PR_ARQ_S_DONE) | |
| 572 | S(PR_ARQ_S_WAIT_CIPH) | |
| 573 | S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) | |
| 574 | S(PR_ARQ_S_WAIT_SUB_PRES) | |
| 575 | S(PR_ARQ_S_WAIT_TRACE_SUB) | |
| 576 | S(PR_ARQ_S_WAIT_CHECK_IMEI) | |
| 577 | S(PR_ARQ_S_WAIT_TMSI_ACK), |
| 578 | .action = proc_arq_vlr_fn_w_auth, |
| 579 | }, |
| 580 | [PR_ARQ_S_WAIT_CIPH] = { |
| 581 | .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_CIPH), |
| 582 | .in_event_mask = S(PR_ARQ_E_CIPH_RES), |
| 583 | .out_state_mask = S(PR_ARQ_S_DONE) | |
| 584 | S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) | |
| 585 | S(PR_ARQ_S_WAIT_SUB_PRES) | |
| 586 | S(PR_ARQ_S_WAIT_TRACE_SUB) | |
| 587 | S(PR_ARQ_S_WAIT_CHECK_IMEI) | |
| 588 | S(PR_ARQ_S_WAIT_TMSI_ACK), |
| 589 | .action = proc_arq_vlr_fn_w_ciph, |
| 590 | }, |
| 591 | [PR_ARQ_S_WAIT_UPD_LOC_CHILD] = { |
| 592 | .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_UPD_LOC_CHILD), |
| 593 | .in_event_mask = S(PR_ARQ_E_UPD_LOC_RES), |
| 594 | .out_state_mask = S(PR_ARQ_S_DONE) | |
| 595 | S(PR_ARQ_S_WAIT_SUB_PRES) | |
| 596 | S(PR_ARQ_S_WAIT_TRACE_SUB) | |
| 597 | S(PR_ARQ_S_WAIT_CHECK_IMEI) | |
| 598 | S(PR_ARQ_S_WAIT_TMSI_ACK), |
| 599 | .action = proc_arq_vlr_fn_w_upd_loc, |
| 600 | }, |
| 601 | [PR_ARQ_S_WAIT_SUB_PRES] = { |
| 602 | .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_SUB_PRES), |
| 603 | .in_event_mask = S(PR_ARQ_E_PRES_RES), |
| 604 | .out_state_mask = S(PR_ARQ_S_DONE) | |
| 605 | S(PR_ARQ_S_WAIT_TRACE_SUB) | |
| 606 | S(PR_ARQ_S_WAIT_CHECK_IMEI) | |
| 607 | S(PR_ARQ_S_WAIT_TMSI_ACK), |
| 608 | .action = proc_arq_vlr_fn_w_pres, |
| 609 | }, |
| 610 | [PR_ARQ_S_WAIT_TRACE_SUB] = { |
| 611 | .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_TRACE_SUB), |
| 612 | .in_event_mask = S(PR_ARQ_E_TRACE_RES), |
| 613 | .out_state_mask = S(PR_ARQ_S_DONE) | |
| 614 | S(PR_ARQ_S_WAIT_CHECK_IMEI) | |
| 615 | S(PR_ARQ_S_WAIT_TMSI_ACK), |
| 616 | .action = proc_arq_vlr_fn_w_trace, |
| 617 | }, |
| 618 | [PR_ARQ_S_WAIT_CHECK_IMEI] = { |
| 619 | .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_CHECK_IMEI), |
| 620 | .in_event_mask = S(PR_ARQ_E_IMEI_RES), |
| 621 | .out_state_mask = S(PR_ARQ_S_DONE) | |
| 622 | S(PR_ARQ_S_WAIT_TMSI_ACK), |
| 623 | .action = proc_arq_vlr_fn_w_imei, |
| 624 | }, |
| 625 | [PR_ARQ_S_WAIT_TMSI_ACK] = { |
| 626 | .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_TMSI_ACK), |
| 627 | .in_event_mask = S(PR_ARQ_E_TMSI_ACK), |
| 628 | .out_state_mask = S(PR_ARQ_S_DONE), |
| 629 | .action = proc_arq_vlr_fn_w_tmsi, |
| 630 | }, |
| 631 | [PR_ARQ_S_DONE] = { |
| 632 | .name = OSMO_STRINGIFY(PR_ARQ_S_DONE), |
| 633 | .onenter = proc_arq_vlr_dispatch_result, |
| 634 | }, |
| 635 | }; |
| 636 | |
| 637 | static struct osmo_fsm proc_arq_vlr_fsm = { |
| 638 | .name = "Process_Access_Request_VLR", |
| 639 | .states = proc_arq_vlr_states, |
| 640 | .num_states = ARRAY_SIZE(proc_arq_vlr_states), |
| 641 | .allstate_event_mask = 0, |
| 642 | .allstate_action = NULL, |
| 643 | .log_subsys = DVLR, |
| 644 | .event_names = proc_arq_vlr_event_names, |
| 645 | .cleanup = proc_arq_vlr_cleanup, |
| 646 | }; |
| 647 | |
| 648 | void |
| 649 | vlr_proc_acc_req(struct osmo_fsm_inst *parent, |
| 650 | uint32_t parent_event_success, |
| 651 | uint32_t parent_event_failure, |
| 652 | void *parent_event_data, |
| 653 | struct vlr_instance *vlr, void *msc_conn_ref, |
| 654 | enum vlr_parq_type type, const uint8_t *mi_lv, |
| 655 | const struct osmo_location_area_id *lai, |
| 656 | bool authentication_required, |
| 657 | enum vlr_ciph ciphering_required, |
| 658 | bool is_r99, bool is_utran) |
| 659 | { |
| 660 | struct osmo_fsm_inst *fi; |
| 661 | struct proc_arq_priv *par; |
| 662 | char mi_string[GSM48_MI_SIZE]; |
| 663 | uint8_t mi_type; |
| 664 | |
| 665 | fi = osmo_fsm_inst_alloc_child(&proc_arq_vlr_fsm, parent, |
| 666 | parent_event_failure); |
| 667 | if (!fi) |
| 668 | return; |
| 669 | |
| 670 | par = talloc_zero(fi, struct proc_arq_priv); |
| 671 | fi->priv = par; |
| 672 | par->vlr = vlr; |
| 673 | par->msc_conn_ref = msc_conn_ref; |
| 674 | par->type = type; |
| 675 | par->lai = *lai; |
| 676 | par->parent_event_success = parent_event_success; |
| 677 | par->parent_event_failure = parent_event_failure; |
| 678 | par->parent_event_data = parent_event_data; |
| 679 | par->authentication_required = authentication_required; |
| 680 | par->ciphering_required = ciphering_required; |
| 681 | par->is_r99 = is_r99; |
| 682 | par->is_utran = is_utran; |
| 683 | |
| 684 | LOGPFSM(fi, "rev=%s net=%s%s%s\n", |
| 685 | is_r99 ? "R99" : "GSM", |
| 686 | is_utran ? "UTRAN" : "GERAN", |
| 687 | (authentication_required || ciphering_required)? |
| 688 | " Auth" : " (no Auth)", |
| 689 | (authentication_required || ciphering_required)? |
| 690 | (ciphering_required? "+Ciph" : " (no Ciph)") |
| 691 | : ""); |
| 692 | |
Neels Hofmeyr | 84da6b1 | 2016-05-20 21:59:55 +0200 | [diff] [blame] | 693 | if (is_utran && !authentication_required) |
| 694 | LOGPFSML(fi, LOGL_ERROR, |
| 695 | "Authentication off on UTRAN network. Good luck.\n"); |
| 696 | |
Harald Welte | b8b85a1 | 2016-06-17 00:06:42 +0200 | [diff] [blame] | 697 | gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]); |
| 698 | mi_type = mi_lv[1] & GSM_MI_TYPE_MASK; |
| 699 | switch (mi_type) { |
| 700 | case GSM_MI_TYPE_IMSI: |
| 701 | strncpy(par->imsi, mi_string, sizeof(par->imsi)-1); |
| 702 | par->imsi[sizeof(par->imsi)-1] = '\0'; |
| 703 | par->by_tmsi = false; |
| 704 | break; |
| 705 | case GSM_MI_TYPE_TMSI: |
| 706 | par->by_tmsi = true; |
| 707 | par->tmsi = osmo_load32be(mi_lv+2); |
| 708 | break; |
| 709 | case GSM_MI_TYPE_IMEI: |
| 710 | /* TODO: IMEI (emergency call) */ |
| 711 | default: |
| 712 | /* FIXME: directly send reject? */ |
| 713 | proc_arq_fsm_done(fi, VLR_PR_ARQ_RES_UNIDENT_SUBSCR); |
| 714 | return; |
| 715 | } |
| 716 | |
| 717 | osmo_fsm_inst_dispatch(fi, PR_ARQ_E_START, NULL); |
| 718 | } |
| 719 | |
| 720 | /* Gracefully terminate an FSM created by vlr_proc_acc_req() in case of |
| 721 | * external timeout (i.e. from MSC). */ |
| 722 | void vlr_parq_conn_timeout(struct osmo_fsm_inst *fi) |
| 723 | { |
| 724 | if (!fi || fi->state == PR_ARQ_S_DONE) |
| 725 | return; |
| 726 | LOGPFSM(fi, "Connection timed out\n"); |
| 727 | proc_arq_fsm_done(fi, VLR_PR_ARQ_RES_TIMEOUT); |
| 728 | } |
| 729 | |
| 730 | |
| 731 | #if 0 |
| 732 | /*********************************************************************** |
| 733 | * Update_Location_Child_VLR, TS 29.002 Chapter 25.4.4 |
| 734 | ***********************************************************************/ |
| 735 | |
| 736 | enum upd_loc_child_vlr_state { |
| 737 | ULC_S_IDLE, |
| 738 | ULC_S_WAIT_HLR_RESP, |
| 739 | ULC_S_DONE, |
| 740 | }; |
| 741 | |
| 742 | enum upd_loc_child_vlr_event { |
| 743 | ULC_E_START, |
| 744 | }; |
| 745 | |
| 746 | static const struct value_string upd_loc_child_vlr_event_names[] = { |
| 747 | { ULC_E_START, "START" }, |
| 748 | { 0, NULL } |
| 749 | }; |
| 750 | |
| 751 | static void upd_loc_child_f_idle(struct osmo_fsm_inst *fi, uint32_t event, |
| 752 | void *data) |
| 753 | { |
| 754 | OSMO_ASSERT(event == ULC_E_START); |
| 755 | |
| 756 | /* send update location */ |
| 757 | } |
| 758 | |
| 759 | static void upd_loc_child_f_w_hlr(struct osmo_fsm_inst *fi, uint32_t event, |
| 760 | void *data) |
| 761 | { |
| 762 | } |
| 763 | |
| 764 | static const struct osmo_fsm_state upd_loc_child_vlr_states[] = { |
| 765 | [ULC_S_IDLE] = { |
| 766 | .in_event_mask = , |
| 767 | .out_state_mask = S(ULC_S_WAIT_HLR_RESP) | |
| 768 | S(ULC_S_DONE), |
| 769 | .name = "IDLE", |
| 770 | .action = upd_loc_child_f_idle, |
| 771 | }, |
| 772 | [ULC_S_WAIT_HLR_RESP] = { |
| 773 | .in_event_mask = , |
| 774 | .out_state_mask = S(ULC_S_DONE), |
| 775 | .name = "WAIT-HLR-RESP", |
| 776 | .action = upd_loc_child_f_w_hlr, |
| 777 | }, |
| 778 | [ULC_S_DONE] = { |
| 779 | .name = "DONE", |
| 780 | }, |
| 781 | }; |
| 782 | |
| 783 | static struct osmo_fsm upd_loc_child_vlr_fsm = { |
| 784 | .name = "Update_Location_Child_VLR", |
| 785 | .states = upd_loc_child_vlr_states, |
| 786 | .num_states = ARRAY_SIZE(upd_loc_child_vlr_states), |
| 787 | .log_subsys = DVLR, |
| 788 | .event_names = upd_loc_child_vlr_event_names, |
| 789 | }; |
| 790 | #endif |
| 791 | |
| 792 | void vlr_parq_fsm_init(void) |
| 793 | { |
| 794 | //osmo_fsm_register(&upd_loc_child_vlr_fsm); |
| 795 | osmo_fsm_register(&proc_arq_vlr_fsm); |
| 796 | } |