blob: 5f4795f9a7f3ba92a0026aa9dbe983de9a589f5a [file] [log] [blame]
Harald Welteb8b85a12016-06-17 00:06:42 +02001/* 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>
Max43b01b02017-09-15 11:22:30 +020024#include <osmocom/gsm/gsm48.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020025#include <osmocom/msc/vlr.h>
26#include <osmocom/msc/debug.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020027
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
Harald Welteb8b85a12016-06-17 00:06:42 +020039static const struct value_string proc_arq_vlr_event_names[] = {
40 OSMO_VALUE_STRING(PR_ARQ_E_START),
41 OSMO_VALUE_STRING(PR_ARQ_E_ID_IMSI),
42 OSMO_VALUE_STRING(PR_ARQ_E_AUTH_RES),
43 OSMO_VALUE_STRING(PR_ARQ_E_CIPH_RES),
44 OSMO_VALUE_STRING(PR_ARQ_E_UPD_LOC_RES),
45 OSMO_VALUE_STRING(PR_ARQ_E_TRACE_RES),
46 OSMO_VALUE_STRING(PR_ARQ_E_IMEI_RES),
47 OSMO_VALUE_STRING(PR_ARQ_E_PRES_RES),
48 OSMO_VALUE_STRING(PR_ARQ_E_TMSI_ACK),
49 { 0, NULL }
50};
51
52struct proc_arq_priv {
53 struct vlr_instance *vlr;
54 struct vlr_subscr *vsub;
55 void *msc_conn_ref;
56 struct osmo_fsm_inst *ul_child_fsm;
57 struct osmo_fsm_inst *sub_pres_vlr_fsm;
58 uint32_t parent_event_success;
59 uint32_t parent_event_failure;
60 void *parent_event_data;
61
62 enum vlr_parq_type type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010063 enum osmo_cm_service_type cm_service_type;
Neels Hofmeyr15809592018-04-06 02:57:51 +020064 enum gsm48_reject_value result; /*< 0 on success */
Harald Welteb8b85a12016-06-17 00:06:42 +020065 bool by_tmsi;
66 char imsi[16];
67 uint32_t tmsi;
68 struct osmo_location_area_id lai;
69 bool authentication_required;
Neels Hofmeyr2ea72642022-10-10 23:35:47 +020070 /* is_ciphering_to_be_attempted: true when any A5/n > 0 are enabled. Ciphering is allowed, always attempt to get Auth Info from
71 * the HLR. */
Neels Hofmeyrd99a6072022-10-10 23:34:48 +020072 bool is_ciphering_to_be_attempted;
Neels Hofmeyr2ea72642022-10-10 23:35:47 +020073 /* is_ciphering_required: true when A5/0 is disabled. If we cannot get Auth Info from the HLR, reject the
74 * subscriber. */
75 bool is_ciphering_required;
Sylvain Munautda9f37e2019-03-14 11:02:36 +010076 uint8_t key_seq;
Harald Welteb8b85a12016-06-17 00:06:42 +020077 bool is_r99;
78 bool is_utran;
79 bool implicitly_accepted_parq_by_ciphering_cmd;
80};
81
Neels Hofmeyr1035d902018-12-28 21:22:32 +010082static int assoc_par_with_subscr(struct osmo_fsm_inst *fi, struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +020083{
84 struct proc_arq_priv *par = fi->priv;
85 struct vlr_instance *vlr = par->vlr;
86
87 vsub->msc_conn_ref = par->msc_conn_ref;
88 par->vsub = vsub;
89 /* Tell MSC to associate this subscriber with the given
90 * connection */
Neels Hofmeyr1035d902018-12-28 21:22:32 +010091 return vlr->ops.subscr_assoc(par->msc_conn_ref, par->vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +020092}
93
Neels Hofmeyr15809592018-04-06 02:57:51 +020094static const char *vlr_proc_arq_result_name(const struct osmo_fsm_inst *fi)
95{
96 struct proc_arq_priv *par = fi->priv;
97 return par->result? gsm48_reject_value_name(par->result) : "PASSED";
98}
99
Harald Welteb8b85a12016-06-17 00:06:42 +0200100#define proc_arq_fsm_done(fi, res) _proc_arq_fsm_done(fi, res, __FILE__, __LINE__)
101static void _proc_arq_fsm_done(struct osmo_fsm_inst *fi,
Neels Hofmeyr15809592018-04-06 02:57:51 +0200102 enum gsm48_reject_value gsm48_rej,
Harald Welteb8b85a12016-06-17 00:06:42 +0200103 const char *file, int line)
104{
105 struct proc_arq_priv *par = fi->priv;
Neels Hofmeyr15809592018-04-06 02:57:51 +0200106 par->result = gsm48_rej;
107 LOGPFSMSRC(fi, file, line, "proc_arq_fsm_done(%s)\n", vlr_proc_arq_result_name(fi));
Harald Welteb8b85a12016-06-17 00:06:42 +0200108 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_DONE, 0, 0);
109}
110
111static void proc_arq_vlr_dispatch_result(struct osmo_fsm_inst *fi,
112 uint32_t prev_state)
113{
114 struct proc_arq_priv *par = fi->priv;
115 bool success;
116 int rc;
Neels Hofmeyr15809592018-04-06 02:57:51 +0200117 LOGPFSM(fi, "Process Access Request result: %s\n", vlr_proc_arq_result_name(fi));
Harald Welteb8b85a12016-06-17 00:06:42 +0200118
Neels Hofmeyr15809592018-04-06 02:57:51 +0200119 success = (par->result == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200120
121 /* It would be logical to first dispatch the success event to the
122 * parent FSM, but that could start actions that send messages to the
123 * MS. Rather send the CM Service Accept message first and then signal
124 * success. Since messages are handled synchronously, the success event
125 * will be processed before we handle new incoming data from the MS. */
126
127 if (par->type == VLR_PR_ARQ_T_CM_SERV_REQ) {
128 if (success
129 && !par->implicitly_accepted_parq_by_ciphering_cmd) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100130 rc = par->vlr->ops.tx_cm_serv_acc(par->msc_conn_ref,
131 par->cm_service_type);
Harald Welteb8b85a12016-06-17 00:06:42 +0200132 if (rc) {
133 LOGPFSML(fi, LOGL_ERROR,
134 "Failed to send CM Service Accept\n");
135 success = false;
136 }
137 }
138 if (!success) {
139 rc = par->vlr->ops.tx_cm_serv_rej(par->msc_conn_ref,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100140 par->cm_service_type,
Harald Welteb8b85a12016-06-17 00:06:42 +0200141 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) {
Neels Hofmeyr5b1e0302019-05-06 23:45:09 +0200152 LOGPFSML(fi, LOGL_ERROR, "No parent FSM\n");
Harald Welteb8b85a12016-06-17 00:06:42 +0200153 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
161void 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
169static 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 Hofmeyref9126c2017-07-18 15:38:39 +0200176 /* See 3GPP TS 29.002 Proc_Acc_Req_VLR3. */
Harald Welteb8b85a12016-06-17 00:06:42 +0200177 /* 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
Neels Hofmeyr15809592018-04-06 02:57:51 +0200187 proc_arq_fsm_done(fi, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200188}
189
190static 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 Hofmeyref9126c2017-07-18 15:38:39 +0200199 /* See 3GPP TS 29.002 Proc_Acc_Req_VLR3. */
Harald Welteb8b85a12016-06-17 00:06:42 +0200200 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 */
210static void _proc_arq_vlr_post_pres(struct osmo_fsm_inst *fi)
211{
212 LOGPFSM(fi, "%s()\n", __func__);
Neels Hofmeyref9126c2017-07-18 15:38:39 +0200213 /* See 3GPP TS 29.002 Proc_Acc_Req_VLR3. */
Harald Welteb8b85a12016-06-17 00:06:42 +0200214 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 */
222static 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 */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200231 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200232 return;
233 }
Neels Hofmeyref9126c2017-07-18 15:38:39 +0200234 /* We don't feature location area specific blocking (yet). */
Harald Welteb8b85a12016-06-17 00:06:42 +0200235 if (0 /* roaming not allowed in LA */) {
236 /* Set User Error: Roaming not allowed in this LA */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200237 proc_arq_fsm_done(fi, GSM48_REJECT_ROAMING_NOT_ALLOWED);
Harald Welteb8b85a12016-06-17 00:06:42 +0200238 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 Hofmeyr1a5bcd52017-11-18 22:19:55 +0100244 sub_pres_vlr_fsm_start(&par->sub_pres_vlr_fsm, fi, vsub, PR_ARQ_E_PRES_RES);
Harald Welteb8b85a12016-06-17 00:06:42 +0200245 return;
246 }
247 _proc_arq_vlr_post_pres(fi);
248}
249
250static 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;
Harald Welte544a32f2020-06-21 22:15:53 +0200254 int rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200255
256 LOGPFSM(fi, "%s()\n", __func__);
257
Harald Welte544a32f2020-06-21 22:15:53 +0200258 rc = par->vlr->ops.tx_common_id(par->msc_conn_ref);
259 if (rc)
260 LOGPFSML(fi, LOGL_ERROR, "Error while sending Common ID (%d)\n", rc);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200261
Harald Welteb8b85a12016-06-17 00:06:42 +0200262 vsub->conf_by_radio_contact_ind = true;
263 if (vsub->loc_conf_in_hlr_ind == false) {
264 /* start Update_Location_Child_VLR. WE use
265 * Update_HLR_VLR instead, the differences appear
266 * insignificant for now. */
267 par->ul_child_fsm = upd_hlr_vlr_proc_start(fi, vsub,
268 PR_ARQ_E_UPD_LOC_RES);
269 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_UPD_LOC_CHILD, 0, 0);
270 return;
271 }
272 _proc_arq_vlr_node2_post_vlr(fi);
273}
274
Neels Hofmeyrc48afa12022-10-10 23:47:04 +0200275/* Return true when CipherModeCmd / SecurityModeCmd should be attempted. */
276static bool is_cmc_smc_to_be_attempted(struct proc_arq_priv *par)
Harald Welteb8b85a12016-06-17 00:06:42 +0200277{
Vadim Yanitskiy565ea2b2021-11-28 16:42:58 +0300278 /* UTRAN: always send SecModeCmd, even if ciphering is not required.
279 * GERAN: avoid sending CiphModeCmd if ciphering is not required. */
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200280 return par->is_utran || par->is_ciphering_to_be_attempted;
Harald Welteb8b85a12016-06-17 00:06:42 +0200281}
282
283static 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 Hofmeyr7795a192018-03-10 00:26:36 +0100287 bool umts_aka;
Harald Welteb8b85a12016-06-17 00:06:42 +0200288
289 LOGPFSM(fi, "%s()\n", __func__);
290
Neels Hofmeyrc48afa12022-10-10 23:47:04 +0200291 if (!is_cmc_smc_to_be_attempted(par)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200292 _proc_arq_vlr_node2_post_ciph(fi);
293 return;
294 }
295
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100296 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");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200305 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100306 return;
307 }
308
Harald Welteb8b85a12016-06-17 00:06:42 +0200309 if (vlr_set_ciph_mode(vsub->vlr, fi, par->msc_conn_ref,
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100310 umts_aka,
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200311 vsub->vlr->cfg.retrieve_imeisv_ciphered)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200312 LOGPFSML(fi, LOGL_ERROR,
313 "Failed to send Ciphering Mode Command\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200314 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200315 return;
316 }
317
318 par->implicitly_accepted_parq_by_ciphering_cmd = true;
319 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_CIPH, 0, 0);
320}
321
Neels Hofmeyrc48afa12022-10-10 23:47:04 +0200322static bool is_auth_to_be_attempted(struct proc_arq_priv *par)
Harald Welteb8b85a12016-06-17 00:06:42 +0200323{
324 /* The cases where the authentication procedure should be used
325 * are defined in 3GPP TS 33.102 */
326 /* For now we use a default value passed in to vlr_lu_fsm(). */
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100327 return par->authentication_required ||
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200328 (par->is_ciphering_to_be_attempted && !auth_try_reuse_tuple(par->vsub, par->key_seq));
Harald Welteb8b85a12016-06-17 00:06:42 +0200329}
330
331/* after the IMSI is known */
332static 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 */
Neels Hofmeyrc48afa12022-10-10 23:47:04 +0200342 if (is_auth_to_be_attempted(par)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200343 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_AUTH,
344 0, 0);
Vadim Yanitskiy3daf0c22020-01-25 06:02:48 +0700345 vsub->auth_fsm = auth_fsm_start(vsub, fi,
Harald Welteb8b85a12016-06-17 00:06:42 +0200346 PR_ARQ_E_AUTH_RES,
347 par->is_r99,
348 par->is_utran);
349 } else {
350 _proc_arq_vlr_node2(fi);
351 }
352}
353
354static 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 */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100366 vsub = vlr_subscr_find_by_imsi(par->vlr, par->imsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200367 } else {
368 /* TMSI was included */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100369 vsub = vlr_subscr_find_by_tmsi(par->vlr, par->tmsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200370 }
371 if (vsub) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200372 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200373 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,
Neels Hofmeyr15809592018-04-06 02:57:51 +0200380 GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200381 }
382 vsub->proc_arq_fsm = fi;
Neels Hofmeyr1035d902018-12-28 21:22:32 +0100383 if (assoc_par_with_subscr(fi, vsub) != 0)
384 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
385 else
386 proc_arq_vlr_fn_post_imsi(fi);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100387 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200388 return;
389 }
390 /* No VSUB could be resolved. What now? */
391
392 if (!par->by_tmsi) {
393 /* We couldn't find a subscriber even by IMSI,
394 * Set User Error: Unidentified Subscriber */
Philipp Maier6f4752e2018-05-15 15:23:59 +0200395 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200396 return;
397 } else {
398 /* TMSI was included, are we permitted to use it? */
399 if (vlr->cfg.parq_retrieve_imsi) {
400 /* Obtain_IMSI_VLR */
401 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_OBTAIN_IMSI,
402 vlr_timer(vlr, 3270), 3270);
403 return;
404 } else {
405 /* Set User Error: Unidentified Subscriber */
Philipp Maier6f4752e2018-05-15 15:23:59 +0200406 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200407 return;
408 }
409 }
410}
411
412/* ID REQ(IMSI) has returned */
413static void proc_arq_vlr_fn_w_obt_imsi(struct osmo_fsm_inst *fi,
414 uint32_t event, void *data)
415{
416 struct proc_arq_priv *par = fi->priv;
417 struct vlr_instance *vlr = par->vlr;
418 struct vlr_subscr *vsub;
419
420 OSMO_ASSERT(event == PR_ARQ_E_ID_IMSI);
421
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100422 vsub = vlr_subscr_find_by_imsi(vlr, par->imsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200423 if (!vsub) {
424 /* Set User Error: Unidentified Subscriber */
Philipp Maier6f4752e2018-05-15 15:23:59 +0200425 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200426 return;
427 }
Neels Hofmeyr1035d902018-12-28 21:22:32 +0100428 if (assoc_par_with_subscr(fi, vsub))
429 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
430 else
431 proc_arq_vlr_fn_post_imsi(fi);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100432 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200433}
434
435/* Authenticate_VLR has completed */
436static void proc_arq_vlr_fn_w_auth(struct osmo_fsm_inst *fi,
437 uint32_t event, void *data)
438{
Neels Hofmeyr15809592018-04-06 02:57:51 +0200439 enum gsm48_reject_value *cause = data;
Harald Welteb8b85a12016-06-17 00:06:42 +0200440
441 OSMO_ASSERT(event == PR_ARQ_E_AUTH_RES);
442
Neels Hofmeyr15809592018-04-06 02:57:51 +0200443 if (!cause || *cause) {
444 proc_arq_fsm_done(fi, cause? *cause : GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200445 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200446 }
447
Neels Hofmeyr15809592018-04-06 02:57:51 +0200448 /* Node 2 */
449 _proc_arq_vlr_node2(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200450}
451
452static void proc_arq_vlr_fn_w_ciph(struct osmo_fsm_inst *fi,
453 uint32_t event, void *data)
454{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100455 enum vlr_ciph_result_cause result = VLR_CIPH_REJECT;
Harald Welteb8b85a12016-06-17 00:06:42 +0200456
457 OSMO_ASSERT(event == PR_ARQ_E_CIPH_RES);
458
459 if (!data)
460 LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: NULL\n");
461 else
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100462 result = *(enum vlr_ciph_result_cause*)data;
Harald Welteb8b85a12016-06-17 00:06:42 +0200463
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100464 switch (result) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200465 case VLR_CIPH_COMPL:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100466 _proc_arq_vlr_node2_post_ciph(fi);
467 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200468 case VLR_CIPH_REJECT:
469 LOGPFSM(fi, "ciphering rejected\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200470 proc_arq_fsm_done(fi, GSM48_REJECT_ILLEGAL_MS);
Harald Welteb8b85a12016-06-17 00:06:42 +0200471 return;
472 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100473 LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: %d\n", result);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200474 proc_arq_fsm_done(fi, GSM48_REJECT_ILLEGAL_MS);
Harald Welteb8b85a12016-06-17 00:06:42 +0200475 return;
476 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200477}
478
479/* Update_Location_Child_VLR has completed */
480static void proc_arq_vlr_fn_w_upd_loc(struct osmo_fsm_inst *fi,
481 uint32_t event, void *data)
482{
483 OSMO_ASSERT(event == PR_ARQ_E_UPD_LOC_RES);
484
485 _proc_arq_vlr_node2_post_vlr(fi);
486}
487
488/* Subscriber_Present_VLR has completed */
489static void proc_arq_vlr_fn_w_pres(struct osmo_fsm_inst *fi,
490 uint32_t event, void *data)
491{
492 OSMO_ASSERT(event == PR_ARQ_E_PRES_RES);
493
494 _proc_arq_vlr_post_pres(fi);
495}
496
497static void proc_arq_vlr_fn_w_trace(struct osmo_fsm_inst *fi,
498 uint32_t event, void *data)
499{
500 OSMO_ASSERT(event == PR_ARQ_E_TRACE_RES);
501
502 _proc_arq_vlr_post_trace(fi);
503}
504
505/* we have received the ID RESPONSE (IMEI) */
506static void proc_arq_vlr_fn_w_imei(struct osmo_fsm_inst *fi,
507 uint32_t event, void *data)
508{
509 OSMO_ASSERT(event == PR_ARQ_E_IMEI_RES);
510
511 _proc_arq_vlr_post_imei(fi);
512}
513
514/* MSC tells us that MS has acknowleded TMSI re-allocation */
515static void proc_arq_vlr_fn_w_tmsi(struct osmo_fsm_inst *fi,
516 uint32_t event, void *data)
517{
518 OSMO_ASSERT(event == PR_ARQ_E_TMSI_ACK);
519
520 /* FIXME: check confirmation? unfreeze? */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200521 proc_arq_fsm_done(fi, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200522}
523
524static const struct osmo_fsm_state proc_arq_vlr_states[] = {
525 [PR_ARQ_S_INIT] = {
526 .name = OSMO_STRINGIFY(PR_ARQ_S_INIT),
527 .in_event_mask = S(PR_ARQ_E_START),
528 .out_state_mask = S(PR_ARQ_S_DONE) |
529 S(PR_ARQ_S_WAIT_OBTAIN_IMSI) |
530 S(PR_ARQ_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100531 S(PR_ARQ_S_WAIT_CIPH) |
Harald Welteb8b85a12016-06-17 00:06:42 +0200532 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
533 S(PR_ARQ_S_WAIT_SUB_PRES) |
534 S(PR_ARQ_S_WAIT_TRACE_SUB) |
535 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
536 S(PR_ARQ_S_WAIT_TMSI_ACK),
537 .action = proc_arq_vlr_fn_init,
538 },
539 [PR_ARQ_S_WAIT_OBTAIN_IMSI] = {
540 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_OBTAIN_IMSI),
541 .in_event_mask = S(PR_ARQ_E_ID_IMSI),
542 .out_state_mask = S(PR_ARQ_S_DONE) |
543 S(PR_ARQ_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100544 S(PR_ARQ_S_WAIT_CIPH) |
Harald Welteb8b85a12016-06-17 00:06:42 +0200545 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
546 S(PR_ARQ_S_WAIT_SUB_PRES) |
547 S(PR_ARQ_S_WAIT_TRACE_SUB) |
548 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
549 S(PR_ARQ_S_WAIT_TMSI_ACK),
550 .action = proc_arq_vlr_fn_w_obt_imsi,
551 },
552 [PR_ARQ_S_WAIT_AUTH] = {
553 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_AUTH),
554 .in_event_mask = S(PR_ARQ_E_AUTH_RES),
555 .out_state_mask = S(PR_ARQ_S_DONE) |
556 S(PR_ARQ_S_WAIT_CIPH) |
557 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
558 S(PR_ARQ_S_WAIT_SUB_PRES) |
559 S(PR_ARQ_S_WAIT_TRACE_SUB) |
560 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
561 S(PR_ARQ_S_WAIT_TMSI_ACK),
562 .action = proc_arq_vlr_fn_w_auth,
563 },
564 [PR_ARQ_S_WAIT_CIPH] = {
565 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_CIPH),
566 .in_event_mask = S(PR_ARQ_E_CIPH_RES),
567 .out_state_mask = S(PR_ARQ_S_DONE) |
568 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
569 S(PR_ARQ_S_WAIT_SUB_PRES) |
570 S(PR_ARQ_S_WAIT_TRACE_SUB) |
571 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
572 S(PR_ARQ_S_WAIT_TMSI_ACK),
573 .action = proc_arq_vlr_fn_w_ciph,
574 },
575 [PR_ARQ_S_WAIT_UPD_LOC_CHILD] = {
576 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_UPD_LOC_CHILD),
577 .in_event_mask = S(PR_ARQ_E_UPD_LOC_RES),
578 .out_state_mask = S(PR_ARQ_S_DONE) |
579 S(PR_ARQ_S_WAIT_SUB_PRES) |
580 S(PR_ARQ_S_WAIT_TRACE_SUB) |
581 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
582 S(PR_ARQ_S_WAIT_TMSI_ACK),
583 .action = proc_arq_vlr_fn_w_upd_loc,
584 },
585 [PR_ARQ_S_WAIT_SUB_PRES] = {
586 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_SUB_PRES),
587 .in_event_mask = S(PR_ARQ_E_PRES_RES),
588 .out_state_mask = S(PR_ARQ_S_DONE) |
589 S(PR_ARQ_S_WAIT_TRACE_SUB) |
590 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
591 S(PR_ARQ_S_WAIT_TMSI_ACK),
592 .action = proc_arq_vlr_fn_w_pres,
593 },
594 [PR_ARQ_S_WAIT_TRACE_SUB] = {
595 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_TRACE_SUB),
596 .in_event_mask = S(PR_ARQ_E_TRACE_RES),
597 .out_state_mask = S(PR_ARQ_S_DONE) |
598 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
599 S(PR_ARQ_S_WAIT_TMSI_ACK),
600 .action = proc_arq_vlr_fn_w_trace,
601 },
602 [PR_ARQ_S_WAIT_CHECK_IMEI] = {
603 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_CHECK_IMEI),
604 .in_event_mask = S(PR_ARQ_E_IMEI_RES),
605 .out_state_mask = S(PR_ARQ_S_DONE) |
606 S(PR_ARQ_S_WAIT_TMSI_ACK),
607 .action = proc_arq_vlr_fn_w_imei,
608 },
609 [PR_ARQ_S_WAIT_TMSI_ACK] = {
610 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_TMSI_ACK),
611 .in_event_mask = S(PR_ARQ_E_TMSI_ACK),
612 .out_state_mask = S(PR_ARQ_S_DONE),
613 .action = proc_arq_vlr_fn_w_tmsi,
614 },
615 [PR_ARQ_S_DONE] = {
616 .name = OSMO_STRINGIFY(PR_ARQ_S_DONE),
617 .onenter = proc_arq_vlr_dispatch_result,
618 },
619};
620
621static struct osmo_fsm proc_arq_vlr_fsm = {
622 .name = "Process_Access_Request_VLR",
623 .states = proc_arq_vlr_states,
624 .num_states = ARRAY_SIZE(proc_arq_vlr_states),
625 .allstate_event_mask = 0,
626 .allstate_action = NULL,
627 .log_subsys = DVLR,
628 .event_names = proc_arq_vlr_event_names,
629 .cleanup = proc_arq_vlr_cleanup,
630};
631
632void
633vlr_proc_acc_req(struct osmo_fsm_inst *parent,
634 uint32_t parent_event_success,
635 uint32_t parent_event_failure,
636 void *parent_event_data,
637 struct vlr_instance *vlr, void *msc_conn_ref,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100638 enum vlr_parq_type type, enum osmo_cm_service_type cm_service_type,
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200639 const struct osmo_mobile_identity *mi,
Harald Welteb8b85a12016-06-17 00:06:42 +0200640 const struct osmo_location_area_id *lai,
641 bool authentication_required,
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200642 bool is_ciphering_to_be_attempted,
Neels Hofmeyr2ea72642022-10-10 23:35:47 +0200643 bool is_ciphering_required,
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100644 uint8_t key_seq,
Harald Welteb8b85a12016-06-17 00:06:42 +0200645 bool is_r99, bool is_utran)
646{
647 struct osmo_fsm_inst *fi;
648 struct proc_arq_priv *par;
Harald Welteb8b85a12016-06-17 00:06:42 +0200649
Neels Hofmeyr2ea72642022-10-10 23:35:47 +0200650 if (is_ciphering_required)
651 OSMO_ASSERT(is_ciphering_to_be_attempted);
652
Harald Welteb8b85a12016-06-17 00:06:42 +0200653 fi = osmo_fsm_inst_alloc_child(&proc_arq_vlr_fsm, parent,
654 parent_event_failure);
655 if (!fi)
656 return;
657
658 par = talloc_zero(fi, struct proc_arq_priv);
659 fi->priv = par;
660 par->vlr = vlr;
661 par->msc_conn_ref = msc_conn_ref;
662 par->type = type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100663 par->cm_service_type = cm_service_type;
Harald Welteb8b85a12016-06-17 00:06:42 +0200664 par->lai = *lai;
665 par->parent_event_success = parent_event_success;
666 par->parent_event_failure = parent_event_failure;
667 par->parent_event_data = parent_event_data;
668 par->authentication_required = authentication_required;
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200669 par->is_ciphering_to_be_attempted = is_ciphering_to_be_attempted;
Neels Hofmeyr2ea72642022-10-10 23:35:47 +0200670 par->is_ciphering_required = is_ciphering_required;
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100671 par->key_seq = key_seq;
Harald Welteb8b85a12016-06-17 00:06:42 +0200672 par->is_r99 = is_r99;
673 par->is_utran = is_utran;
674
675 LOGPFSM(fi, "rev=%s net=%s%s%s\n",
676 is_r99 ? "R99" : "GSM",
677 is_utran ? "UTRAN" : "GERAN",
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200678 (authentication_required || is_ciphering_to_be_attempted) ?
Harald Welteb8b85a12016-06-17 00:06:42 +0200679 " Auth" : " (no Auth)",
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200680 (authentication_required || is_ciphering_to_be_attempted) ?
681 (is_ciphering_to_be_attempted ? "+Ciph" : " (no Ciph)")
Harald Welteb8b85a12016-06-17 00:06:42 +0200682 : "");
683
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200684 if (is_utran && !authentication_required)
685 LOGPFSML(fi, LOGL_ERROR,
686 "Authentication off on UTRAN network. Good luck.\n");
687
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200688 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200689 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200690 OSMO_STRLCPY_ARRAY(par->imsi, mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200691 par->by_tmsi = false;
692 break;
693 case GSM_MI_TYPE_TMSI:
694 par->by_tmsi = true;
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200695 par->tmsi = mi->tmsi;
Harald Welteb8b85a12016-06-17 00:06:42 +0200696 break;
697 case GSM_MI_TYPE_IMEI:
698 /* TODO: IMEI (emergency call) */
699 default:
Philipp Maier6f4752e2018-05-15 15:23:59 +0200700 proc_arq_fsm_done(fi, GSM48_REJECT_INVALID_MANDANTORY_INF);
Harald Welteb8b85a12016-06-17 00:06:42 +0200701 return;
702 }
703
704 osmo_fsm_inst_dispatch(fi, PR_ARQ_E_START, NULL);
705}
706
707/* Gracefully terminate an FSM created by vlr_proc_acc_req() in case of
708 * external timeout (i.e. from MSC). */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200709void vlr_parq_cancel(struct osmo_fsm_inst *fi,
710 enum osmo_fsm_term_cause fsm_cause,
711 enum gsm48_reject_value gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200712{
713 if (!fi || fi->state == PR_ARQ_S_DONE)
714 return;
Neels Hofmeyr15809592018-04-06 02:57:51 +0200715 LOGPFSM(fi, "Cancel: %s\n", osmo_fsm_term_cause_name(fsm_cause));
716 proc_arq_fsm_done(fi, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200717}
718
719
720#if 0
721/***********************************************************************
722 * Update_Location_Child_VLR, TS 29.002 Chapter 25.4.4
723 ***********************************************************************/
724
725enum upd_loc_child_vlr_state {
726 ULC_S_IDLE,
727 ULC_S_WAIT_HLR_RESP,
728 ULC_S_DONE,
729};
730
731enum upd_loc_child_vlr_event {
732 ULC_E_START,
733};
734
735static const struct value_string upd_loc_child_vlr_event_names[] = {
736 { ULC_E_START, "START" },
737 { 0, NULL }
738};
739
740static void upd_loc_child_f_idle(struct osmo_fsm_inst *fi, uint32_t event,
741 void *data)
742{
743 OSMO_ASSERT(event == ULC_E_START);
744
745 /* send update location */
746}
747
748static void upd_loc_child_f_w_hlr(struct osmo_fsm_inst *fi, uint32_t event,
749 void *data)
750{
751}
752
753static const struct osmo_fsm_state upd_loc_child_vlr_states[] = {
754 [ULC_S_IDLE] = {
755 .in_event_mask = ,
756 .out_state_mask = S(ULC_S_WAIT_HLR_RESP) |
757 S(ULC_S_DONE),
758 .name = "IDLE",
759 .action = upd_loc_child_f_idle,
760 },
761 [ULC_S_WAIT_HLR_RESP] = {
762 .in_event_mask = ,
763 .out_state_mask = S(ULC_S_DONE),
764 .name = "WAIT-HLR-RESP",
765 .action = upd_loc_child_f_w_hlr,
766 },
767 [ULC_S_DONE] = {
768 .name = "DONE",
769 },
770};
771
772static struct osmo_fsm upd_loc_child_vlr_fsm = {
773 .name = "Update_Location_Child_VLR",
774 .states = upd_loc_child_vlr_states,
775 .num_states = ARRAY_SIZE(upd_loc_child_vlr_states),
776 .log_subsys = DVLR,
777 .event_names = upd_loc_child_vlr_event_names,
778};
779#endif
780
781void vlr_parq_fsm_init(void)
782{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100783 //OSMO_ASSERT(osmo_fsm_register(&upd_loc_child_vlr_fsm) == 0);
784 OSMO_ASSERT(osmo_fsm_register(&proc_arq_vlr_fsm) == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200785}