blob: 629625ea4b49759efefce8c01b4125fa314dce06 [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),
Neels Hofmeyr1cb18a22022-10-11 00:18:04 +020043 OSMO_VALUE_STRING(PR_ARQ_E_AUTH_NO_INFO),
Neels Hofmeyr923b6642022-09-28 00:15:45 +020044 OSMO_VALUE_STRING(PR_ARQ_E_AUTH_FAILURE),
Harald Welteb8b85a12016-06-17 00:06:42 +020045 OSMO_VALUE_STRING(PR_ARQ_E_CIPH_RES),
46 OSMO_VALUE_STRING(PR_ARQ_E_UPD_LOC_RES),
47 OSMO_VALUE_STRING(PR_ARQ_E_TRACE_RES),
48 OSMO_VALUE_STRING(PR_ARQ_E_IMEI_RES),
49 OSMO_VALUE_STRING(PR_ARQ_E_PRES_RES),
50 OSMO_VALUE_STRING(PR_ARQ_E_TMSI_ACK),
51 { 0, NULL }
52};
53
54struct proc_arq_priv {
55 struct vlr_instance *vlr;
56 struct vlr_subscr *vsub;
57 void *msc_conn_ref;
58 struct osmo_fsm_inst *ul_child_fsm;
59 struct osmo_fsm_inst *sub_pres_vlr_fsm;
60 uint32_t parent_event_success;
61 uint32_t parent_event_failure;
62 void *parent_event_data;
63
64 enum vlr_parq_type type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010065 enum osmo_cm_service_type cm_service_type;
Neels Hofmeyr15809592018-04-06 02:57:51 +020066 enum gsm48_reject_value result; /*< 0 on success */
Harald Welteb8b85a12016-06-17 00:06:42 +020067 bool by_tmsi;
68 char imsi[16];
69 uint32_t tmsi;
70 struct osmo_location_area_id lai;
71 bool authentication_required;
Neels Hofmeyr2ea72642022-10-10 23:35:47 +020072 /* is_ciphering_to_be_attempted: true when any A5/n > 0 are enabled. Ciphering is allowed, always attempt to get Auth Info from
73 * the HLR. */
Neels Hofmeyrd99a6072022-10-10 23:34:48 +020074 bool is_ciphering_to_be_attempted;
Neels Hofmeyr2ea72642022-10-10 23:35:47 +020075 /* is_ciphering_required: true when A5/0 is disabled. If we cannot get Auth Info from the HLR, reject the
76 * subscriber. */
77 bool is_ciphering_required;
Sylvain Munautda9f37e2019-03-14 11:02:36 +010078 uint8_t key_seq;
Harald Welteb8b85a12016-06-17 00:06:42 +020079 bool is_r99;
80 bool is_utran;
81 bool implicitly_accepted_parq_by_ciphering_cmd;
82};
83
Neels Hofmeyr1035d902018-12-28 21:22:32 +010084static int assoc_par_with_subscr(struct osmo_fsm_inst *fi, struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +020085{
86 struct proc_arq_priv *par = fi->priv;
87 struct vlr_instance *vlr = par->vlr;
88
89 vsub->msc_conn_ref = par->msc_conn_ref;
90 par->vsub = vsub;
91 /* Tell MSC to associate this subscriber with the given
92 * connection */
Neels Hofmeyr1035d902018-12-28 21:22:32 +010093 return vlr->ops.subscr_assoc(par->msc_conn_ref, par->vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +020094}
95
Neels Hofmeyr15809592018-04-06 02:57:51 +020096static const char *vlr_proc_arq_result_name(const struct osmo_fsm_inst *fi)
97{
98 struct proc_arq_priv *par = fi->priv;
99 return par->result? gsm48_reject_value_name(par->result) : "PASSED";
100}
101
Harald Welteb8b85a12016-06-17 00:06:42 +0200102#define proc_arq_fsm_done(fi, res) _proc_arq_fsm_done(fi, res, __FILE__, __LINE__)
103static void _proc_arq_fsm_done(struct osmo_fsm_inst *fi,
Neels Hofmeyr15809592018-04-06 02:57:51 +0200104 enum gsm48_reject_value gsm48_rej,
Harald Welteb8b85a12016-06-17 00:06:42 +0200105 const char *file, int line)
106{
107 struct proc_arq_priv *par = fi->priv;
Neels Hofmeyr15809592018-04-06 02:57:51 +0200108 par->result = gsm48_rej;
109 LOGPFSMSRC(fi, file, line, "proc_arq_fsm_done(%s)\n", vlr_proc_arq_result_name(fi));
Harald Welteb8b85a12016-06-17 00:06:42 +0200110 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_DONE, 0, 0);
111}
112
113static void proc_arq_vlr_dispatch_result(struct osmo_fsm_inst *fi,
114 uint32_t prev_state)
115{
116 struct proc_arq_priv *par = fi->priv;
117 bool success;
118 int rc;
Neels Hofmeyr15809592018-04-06 02:57:51 +0200119 LOGPFSM(fi, "Process Access Request result: %s\n", vlr_proc_arq_result_name(fi));
Harald Welteb8b85a12016-06-17 00:06:42 +0200120
Neels Hofmeyr15809592018-04-06 02:57:51 +0200121 success = (par->result == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200122
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) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100132 rc = par->vlr->ops.tx_cm_serv_acc(par->msc_conn_ref,
133 par->cm_service_type);
Harald Welteb8b85a12016-06-17 00:06:42 +0200134 if (rc) {
135 LOGPFSML(fi, LOGL_ERROR,
136 "Failed to send CM Service Accept\n");
137 success = false;
138 }
139 }
140 if (!success) {
141 rc = par->vlr->ops.tx_cm_serv_rej(par->msc_conn_ref,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100142 par->cm_service_type,
Harald Welteb8b85a12016-06-17 00:06:42 +0200143 par->result);
144 if (rc)
145 LOGPFSML(fi, LOGL_ERROR,
146 "Failed to send CM Service Reject\n");
147 }
148 }
149
150 /* For VLR_PR_ARQ_T_PAGING_RESP, there is nothing to send. The conn_fsm
151 * will start handling pending paging transactions. */
152
153 if (!fi->proc.parent) {
Neels Hofmeyr5b1e0302019-05-06 23:45:09 +0200154 LOGPFSML(fi, LOGL_ERROR, "No parent FSM\n");
Harald Welteb8b85a12016-06-17 00:06:42 +0200155 return;
156 }
157 osmo_fsm_inst_dispatch(fi->proc.parent,
158 success ? par->parent_event_success
159 : par->parent_event_failure,
160 par->parent_event_data);
161}
162
163void proc_arq_vlr_cleanup(struct osmo_fsm_inst *fi,
164 enum osmo_fsm_term_cause cause)
165{
166 struct proc_arq_priv *par = fi->priv;
167 if (par->vsub && par->vsub->proc_arq_fsm == fi)
168 par->vsub->proc_arq_fsm = NULL;
169}
170
171static void _proc_arq_vlr_post_imei(struct osmo_fsm_inst *fi)
172{
173 struct proc_arq_priv *par = fi->priv;
174 struct vlr_subscr *vsub = par->vsub;
175
176 LOGPFSM(fi, "%s()\n", __func__);
177
Neels Hofmeyref9126c2017-07-18 15:38:39 +0200178 /* See 3GPP TS 29.002 Proc_Acc_Req_VLR3. */
Harald Welteb8b85a12016-06-17 00:06:42 +0200179 /* TODO: Identity := IMSI */
180 if (0 /* TODO: TMSI reallocation at access: vlr->cfg.alloc_tmsi_arq */) {
181 vlr_subscr_alloc_tmsi(vsub);
182 /* TODO: forward TMSI to MS, wait for TMSI
183 * REALLOC COMPLETE */
184 /* TODO: Freeze old TMSI */
185 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_TMSI_ACK, 0, 0);
186 return;
187 }
188
Neels Hofmeyr15809592018-04-06 02:57:51 +0200189 proc_arq_fsm_done(fi, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200190}
191
192static void _proc_arq_vlr_post_trace(struct osmo_fsm_inst *fi)
193{
194 struct proc_arq_priv *par = fi->priv;
195 struct vlr_subscr *vsub = par->vsub;
196 struct vlr_instance *vlr = vsub->vlr;
197
198 LOGPFSM(fi, "%s()\n", __func__);
199
200 /* Node 3 */
Neels Hofmeyref9126c2017-07-18 15:38:39 +0200201 /* See 3GPP TS 29.002 Proc_Acc_Req_VLR3. */
Harald Welteb8b85a12016-06-17 00:06:42 +0200202 if (0 /* IMEI check required */) {
203 /* Chck_IMEI_VLR */
204 vlr->ops.tx_id_req(par->msc_conn_ref, GSM_MI_TYPE_IMEI);
205 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_CHECK_IMEI,
206 vlr_timer(vlr, 3270), 3270);
207 } else
208 _proc_arq_vlr_post_imei(fi);
209}
210
211/* After Subscriber_Present_VLR */
212static void _proc_arq_vlr_post_pres(struct osmo_fsm_inst *fi)
213{
214 LOGPFSM(fi, "%s()\n", __func__);
Neels Hofmeyref9126c2017-07-18 15:38:39 +0200215 /* See 3GPP TS 29.002 Proc_Acc_Req_VLR3. */
Harald Welteb8b85a12016-06-17 00:06:42 +0200216 if (0 /* TODO: tracing required */) {
217 /* TODO: Trace_Subscriber_Activity_VLR */
218 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_TRACE_SUB, 0, 0);
219 }
220 _proc_arq_vlr_post_trace(fi);
221}
222
223/* After Update_Location_Child_VLR */
224static void _proc_arq_vlr_node2_post_vlr(struct osmo_fsm_inst *fi)
225{
226 struct proc_arq_priv *par = fi->priv;
227 struct vlr_subscr *vsub = par->vsub;
228
229 LOGPFSM(fi, "%s()\n", __func__);
230
231 if (!vsub->sub_dataconf_by_hlr_ind) {
232 /* Set User Error: Unidentified Subscriber */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200233 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200234 return;
235 }
Neels Hofmeyref9126c2017-07-18 15:38:39 +0200236 /* We don't feature location area specific blocking (yet). */
Harald Welteb8b85a12016-06-17 00:06:42 +0200237 if (0 /* roaming not allowed in LA */) {
238 /* Set User Error: Roaming not allowed in this LA */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200239 proc_arq_fsm_done(fi, GSM48_REJECT_ROAMING_NOT_ALLOWED);
Harald Welteb8b85a12016-06-17 00:06:42 +0200240 return;
241 }
242 vsub->imsi_detached_flag = false;
243 if (vsub->ms_not_reachable_flag) {
244 /* Start Subscriber_Present_VLR */
245 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_SUB_PRES, 0, 0);
Neels Hofmeyr1a5bcd52017-11-18 22:19:55 +0100246 sub_pres_vlr_fsm_start(&par->sub_pres_vlr_fsm, fi, vsub, PR_ARQ_E_PRES_RES);
Harald Welteb8b85a12016-06-17 00:06:42 +0200247 return;
248 }
249 _proc_arq_vlr_post_pres(fi);
250}
251
252static void _proc_arq_vlr_node2_post_ciph(struct osmo_fsm_inst *fi)
253{
254 struct proc_arq_priv *par = fi->priv;
255 struct vlr_subscr *vsub = par->vsub;
Harald Welte544a32f2020-06-21 22:15:53 +0200256 int rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200257
258 LOGPFSM(fi, "%s()\n", __func__);
259
Harald Welte544a32f2020-06-21 22:15:53 +0200260 rc = par->vlr->ops.tx_common_id(par->msc_conn_ref);
261 if (rc)
262 LOGPFSML(fi, LOGL_ERROR, "Error while sending Common ID (%d)\n", rc);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200263
Harald Welteb8b85a12016-06-17 00:06:42 +0200264 vsub->conf_by_radio_contact_ind = true;
265 if (vsub->loc_conf_in_hlr_ind == false) {
266 /* start Update_Location_Child_VLR. WE use
267 * Update_HLR_VLR instead, the differences appear
268 * insignificant for now. */
269 par->ul_child_fsm = upd_hlr_vlr_proc_start(fi, vsub,
270 PR_ARQ_E_UPD_LOC_RES);
271 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_UPD_LOC_CHILD, 0, 0);
272 return;
273 }
274 _proc_arq_vlr_node2_post_vlr(fi);
275}
276
Neels Hofmeyrc48afa12022-10-10 23:47:04 +0200277/* Return true when CipherModeCmd / SecurityModeCmd should be attempted. */
278static bool is_cmc_smc_to_be_attempted(struct proc_arq_priv *par)
Harald Welteb8b85a12016-06-17 00:06:42 +0200279{
Vadim Yanitskiy565ea2b2021-11-28 16:42:58 +0300280 /* UTRAN: always send SecModeCmd, even if ciphering is not required.
281 * GERAN: avoid sending CiphModeCmd if ciphering is not required. */
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200282 return par->is_utran || par->is_ciphering_to_be_attempted;
Harald Welteb8b85a12016-06-17 00:06:42 +0200283}
284
285static void _proc_arq_vlr_node2(struct osmo_fsm_inst *fi)
286{
287 struct proc_arq_priv *par = fi->priv;
288 struct vlr_subscr *vsub = par->vsub;
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100289 bool umts_aka;
Harald Welteb8b85a12016-06-17 00:06:42 +0200290
291 LOGPFSM(fi, "%s()\n", __func__);
292
Neels Hofmeyr1cb18a22022-10-11 00:18:04 +0200293 /* Continue with ciphering, if enabled.
294 * If auth/ciph is optional and the HLR returned no auth info, continue without ciphering. */
295 if (!is_cmc_smc_to_be_attempted(par)
296 || (vsub->sec_ctx == VLR_SEC_CTX_NONE && !par->is_ciphering_required)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200297 _proc_arq_vlr_node2_post_ciph(fi);
298 return;
299 }
300
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100301 switch (vsub->sec_ctx) {
302 case VLR_SEC_CTX_GSM:
303 umts_aka = false;
304 break;
305 case VLR_SEC_CTX_UMTS:
306 umts_aka = true;
307 break;
308 default:
309 LOGPFSML(fi, LOGL_ERROR, "Cannot start ciphering, security context is not established\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200310 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100311 return;
312 }
313
Harald Welteb8b85a12016-06-17 00:06:42 +0200314 if (vlr_set_ciph_mode(vsub->vlr, fi, par->msc_conn_ref,
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100315 umts_aka,
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200316 vsub->vlr->cfg.retrieve_imeisv_ciphered)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200317 LOGPFSML(fi, LOGL_ERROR,
318 "Failed to send Ciphering Mode Command\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200319 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200320 return;
321 }
322
323 par->implicitly_accepted_parq_by_ciphering_cmd = true;
324 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_CIPH, 0, 0);
325}
326
Neels Hofmeyrc48afa12022-10-10 23:47:04 +0200327static bool is_auth_to_be_attempted(struct proc_arq_priv *par)
Harald Welteb8b85a12016-06-17 00:06:42 +0200328{
329 /* The cases where the authentication procedure should be used
330 * are defined in 3GPP TS 33.102 */
331 /* For now we use a default value passed in to vlr_lu_fsm(). */
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100332 return par->authentication_required ||
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200333 (par->is_ciphering_to_be_attempted && !auth_try_reuse_tuple(par->vsub, par->key_seq));
Harald Welteb8b85a12016-06-17 00:06:42 +0200334}
335
336/* after the IMSI is known */
337static void proc_arq_vlr_fn_post_imsi(struct osmo_fsm_inst *fi)
338{
339 struct proc_arq_priv *par = fi->priv;
340 struct vlr_subscr *vsub = par->vsub;
341
342 LOGPFSM(fi, "%s()\n", __func__);
343
344 OSMO_ASSERT(vsub);
345
346 /* TODO: Identity IMEI -> System Failure */
Neels Hofmeyrc48afa12022-10-10 23:47:04 +0200347 if (is_auth_to_be_attempted(par)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200348 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_AUTH,
349 0, 0);
Vadim Yanitskiy3daf0c22020-01-25 06:02:48 +0700350 vsub->auth_fsm = auth_fsm_start(vsub, fi,
Harald Welteb8b85a12016-06-17 00:06:42 +0200351 PR_ARQ_E_AUTH_RES,
Neels Hofmeyr1cb18a22022-10-11 00:18:04 +0200352 PR_ARQ_E_AUTH_NO_INFO,
Neels Hofmeyr66d4ab82022-09-28 12:26:32 +0200353 PR_ARQ_E_AUTH_FAILURE,
Harald Welteb8b85a12016-06-17 00:06:42 +0200354 par->is_r99,
355 par->is_utran);
356 } else {
357 _proc_arq_vlr_node2(fi);
358 }
359}
360
361static void proc_arq_vlr_fn_init(struct osmo_fsm_inst *fi,
362 uint32_t event, void *data)
363{
364 struct proc_arq_priv *par = fi->priv;
365 struct vlr_instance *vlr = par->vlr;
366 struct vlr_subscr *vsub = NULL;
367
368 OSMO_ASSERT(event == PR_ARQ_E_START);
369
370 /* Obtain_Identity_VLR */
371 if (!par->by_tmsi) {
372 /* IMSI was included */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100373 vsub = vlr_subscr_find_by_imsi(par->vlr, par->imsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200374 } else {
375 /* TMSI was included */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100376 vsub = vlr_subscr_find_by_tmsi(par->vlr, par->tmsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200377 }
378 if (vsub) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200379 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200380 if (vsub->proc_arq_fsm && fi != vsub->proc_arq_fsm) {
381 LOGPFSML(fi, LOGL_ERROR,
382 "Another proc_arq_fsm is already"
383 " associated with subscr %s,"
384 " terminating the other FSM.\n",
385 vlr_subscr_name(vsub));
386 proc_arq_fsm_done(vsub->proc_arq_fsm,
Neels Hofmeyr15809592018-04-06 02:57:51 +0200387 GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200388 }
389 vsub->proc_arq_fsm = fi;
Neels Hofmeyr1035d902018-12-28 21:22:32 +0100390 if (assoc_par_with_subscr(fi, vsub) != 0)
391 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
392 else
393 proc_arq_vlr_fn_post_imsi(fi);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100394 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200395 return;
396 }
397 /* No VSUB could be resolved. What now? */
398
399 if (!par->by_tmsi) {
400 /* We couldn't find a subscriber even by IMSI,
401 * Set User Error: Unidentified Subscriber */
Philipp Maier6f4752e2018-05-15 15:23:59 +0200402 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200403 return;
404 } else {
405 /* TMSI was included, are we permitted to use it? */
406 if (vlr->cfg.parq_retrieve_imsi) {
407 /* Obtain_IMSI_VLR */
408 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_OBTAIN_IMSI,
409 vlr_timer(vlr, 3270), 3270);
410 return;
411 } else {
412 /* Set User Error: Unidentified Subscriber */
Philipp Maier6f4752e2018-05-15 15:23:59 +0200413 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200414 return;
415 }
416 }
417}
418
419/* ID REQ(IMSI) has returned */
420static void proc_arq_vlr_fn_w_obt_imsi(struct osmo_fsm_inst *fi,
421 uint32_t event, void *data)
422{
423 struct proc_arq_priv *par = fi->priv;
424 struct vlr_instance *vlr = par->vlr;
425 struct vlr_subscr *vsub;
426
427 OSMO_ASSERT(event == PR_ARQ_E_ID_IMSI);
428
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100429 vsub = vlr_subscr_find_by_imsi(vlr, par->imsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200430 if (!vsub) {
431 /* Set User Error: Unidentified Subscriber */
Philipp Maier6f4752e2018-05-15 15:23:59 +0200432 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200433 return;
434 }
Neels Hofmeyr1035d902018-12-28 21:22:32 +0100435 if (assoc_par_with_subscr(fi, vsub))
436 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
437 else
438 proc_arq_vlr_fn_post_imsi(fi);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100439 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200440}
441
442/* Authenticate_VLR has completed */
443static void proc_arq_vlr_fn_w_auth(struct osmo_fsm_inst *fi,
444 uint32_t event, void *data)
445{
Neels Hofmeyr1cb18a22022-10-11 00:18:04 +0200446 struct proc_arq_priv *par = fi->priv;
Neels Hofmeyr15809592018-04-06 02:57:51 +0200447 enum gsm48_reject_value *cause = data;
Harald Welteb8b85a12016-06-17 00:06:42 +0200448
Neels Hofmeyr923b6642022-09-28 00:15:45 +0200449 switch (event) {
450 case PR_ARQ_E_AUTH_RES:
451 /* Node 2 */
452 _proc_arq_vlr_node2(fi);
453 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200454
Neels Hofmeyr923b6642022-09-28 00:15:45 +0200455 case PR_ARQ_E_AUTH_FAILURE:
Neels Hofmeyr1cb18a22022-10-11 00:18:04 +0200456 proc_arq_fsm_done(fi, cause ? *cause : GSM48_REJECT_NETWORK_FAILURE);
457 return;
458
459 case PR_ARQ_E_AUTH_NO_INFO:
460 /* HLR returned no auth info for the subscriber. Continue only if authentication is optional. */
461 if (par->authentication_required) {
462 proc_arq_fsm_done(fi, cause ? *cause : GSM48_REJECT_NETWORK_FAILURE);
463 return;
464 }
465 LOGPFSML(fi, LOGL_INFO,
466 "Attaching subscriber without auth (auth is optional, and no auth info received from HLR)\n");
467 /* Node 2 */
468 _proc_arq_vlr_node2(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200469 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200470
Neels Hofmeyr923b6642022-09-28 00:15:45 +0200471 default:
472 OSMO_ASSERT(false);
473 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200474}
475
476static void proc_arq_vlr_fn_w_ciph(struct osmo_fsm_inst *fi,
477 uint32_t event, void *data)
478{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100479 enum vlr_ciph_result_cause result = VLR_CIPH_REJECT;
Harald Welteb8b85a12016-06-17 00:06:42 +0200480
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
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100486 result = *(enum vlr_ciph_result_cause*)data;
Harald Welteb8b85a12016-06-17 00:06:42 +0200487
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100488 switch (result) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200489 case VLR_CIPH_COMPL:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100490 _proc_arq_vlr_node2_post_ciph(fi);
491 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200492 case VLR_CIPH_REJECT:
493 LOGPFSM(fi, "ciphering rejected\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200494 proc_arq_fsm_done(fi, GSM48_REJECT_ILLEGAL_MS);
Harald Welteb8b85a12016-06-17 00:06:42 +0200495 return;
496 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100497 LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: %d\n", result);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200498 proc_arq_fsm_done(fi, GSM48_REJECT_ILLEGAL_MS);
Harald Welteb8b85a12016-06-17 00:06:42 +0200499 return;
500 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200501}
502
503/* Update_Location_Child_VLR has completed */
504static void proc_arq_vlr_fn_w_upd_loc(struct osmo_fsm_inst *fi,
505 uint32_t event, void *data)
506{
507 OSMO_ASSERT(event == PR_ARQ_E_UPD_LOC_RES);
508
509 _proc_arq_vlr_node2_post_vlr(fi);
510}
511
512/* Subscriber_Present_VLR has completed */
513static void proc_arq_vlr_fn_w_pres(struct osmo_fsm_inst *fi,
514 uint32_t event, void *data)
515{
516 OSMO_ASSERT(event == PR_ARQ_E_PRES_RES);
517
518 _proc_arq_vlr_post_pres(fi);
519}
520
521static void proc_arq_vlr_fn_w_trace(struct osmo_fsm_inst *fi,
522 uint32_t event, void *data)
523{
524 OSMO_ASSERT(event == PR_ARQ_E_TRACE_RES);
525
526 _proc_arq_vlr_post_trace(fi);
527}
528
529/* we have received the ID RESPONSE (IMEI) */
530static void proc_arq_vlr_fn_w_imei(struct osmo_fsm_inst *fi,
531 uint32_t event, void *data)
532{
533 OSMO_ASSERT(event == PR_ARQ_E_IMEI_RES);
534
535 _proc_arq_vlr_post_imei(fi);
536}
537
538/* MSC tells us that MS has acknowleded TMSI re-allocation */
539static void proc_arq_vlr_fn_w_tmsi(struct osmo_fsm_inst *fi,
540 uint32_t event, void *data)
541{
542 OSMO_ASSERT(event == PR_ARQ_E_TMSI_ACK);
543
544 /* FIXME: check confirmation? unfreeze? */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200545 proc_arq_fsm_done(fi, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200546}
547
548static const struct osmo_fsm_state proc_arq_vlr_states[] = {
549 [PR_ARQ_S_INIT] = {
550 .name = OSMO_STRINGIFY(PR_ARQ_S_INIT),
551 .in_event_mask = S(PR_ARQ_E_START),
552 .out_state_mask = S(PR_ARQ_S_DONE) |
553 S(PR_ARQ_S_WAIT_OBTAIN_IMSI) |
554 S(PR_ARQ_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100555 S(PR_ARQ_S_WAIT_CIPH) |
Harald Welteb8b85a12016-06-17 00:06:42 +0200556 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
557 S(PR_ARQ_S_WAIT_SUB_PRES) |
558 S(PR_ARQ_S_WAIT_TRACE_SUB) |
559 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
560 S(PR_ARQ_S_WAIT_TMSI_ACK),
561 .action = proc_arq_vlr_fn_init,
562 },
563 [PR_ARQ_S_WAIT_OBTAIN_IMSI] = {
564 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_OBTAIN_IMSI),
565 .in_event_mask = S(PR_ARQ_E_ID_IMSI),
566 .out_state_mask = S(PR_ARQ_S_DONE) |
567 S(PR_ARQ_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100568 S(PR_ARQ_S_WAIT_CIPH) |
Harald Welteb8b85a12016-06-17 00:06:42 +0200569 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
570 S(PR_ARQ_S_WAIT_SUB_PRES) |
571 S(PR_ARQ_S_WAIT_TRACE_SUB) |
572 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
573 S(PR_ARQ_S_WAIT_TMSI_ACK),
574 .action = proc_arq_vlr_fn_w_obt_imsi,
575 },
576 [PR_ARQ_S_WAIT_AUTH] = {
577 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_AUTH),
Neels Hofmeyr923b6642022-09-28 00:15:45 +0200578 .in_event_mask = S(PR_ARQ_E_AUTH_RES) |
Neels Hofmeyr1cb18a22022-10-11 00:18:04 +0200579 S(PR_ARQ_E_AUTH_NO_INFO) |
Neels Hofmeyr923b6642022-09-28 00:15:45 +0200580 S(PR_ARQ_E_AUTH_FAILURE),
Harald Welteb8b85a12016-06-17 00:06:42 +0200581 .out_state_mask = S(PR_ARQ_S_DONE) |
582 S(PR_ARQ_S_WAIT_CIPH) |
583 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
584 S(PR_ARQ_S_WAIT_SUB_PRES) |
585 S(PR_ARQ_S_WAIT_TRACE_SUB) |
586 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
587 S(PR_ARQ_S_WAIT_TMSI_ACK),
588 .action = proc_arq_vlr_fn_w_auth,
589 },
590 [PR_ARQ_S_WAIT_CIPH] = {
591 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_CIPH),
592 .in_event_mask = S(PR_ARQ_E_CIPH_RES),
593 .out_state_mask = S(PR_ARQ_S_DONE) |
594 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
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_ciph,
600 },
601 [PR_ARQ_S_WAIT_UPD_LOC_CHILD] = {
602 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_UPD_LOC_CHILD),
603 .in_event_mask = S(PR_ARQ_E_UPD_LOC_RES),
604 .out_state_mask = S(PR_ARQ_S_DONE) |
605 S(PR_ARQ_S_WAIT_SUB_PRES) |
606 S(PR_ARQ_S_WAIT_TRACE_SUB) |
607 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
608 S(PR_ARQ_S_WAIT_TMSI_ACK),
609 .action = proc_arq_vlr_fn_w_upd_loc,
610 },
611 [PR_ARQ_S_WAIT_SUB_PRES] = {
612 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_SUB_PRES),
613 .in_event_mask = S(PR_ARQ_E_PRES_RES),
614 .out_state_mask = S(PR_ARQ_S_DONE) |
615 S(PR_ARQ_S_WAIT_TRACE_SUB) |
616 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
617 S(PR_ARQ_S_WAIT_TMSI_ACK),
618 .action = proc_arq_vlr_fn_w_pres,
619 },
620 [PR_ARQ_S_WAIT_TRACE_SUB] = {
621 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_TRACE_SUB),
622 .in_event_mask = S(PR_ARQ_E_TRACE_RES),
623 .out_state_mask = S(PR_ARQ_S_DONE) |
624 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
625 S(PR_ARQ_S_WAIT_TMSI_ACK),
626 .action = proc_arq_vlr_fn_w_trace,
627 },
628 [PR_ARQ_S_WAIT_CHECK_IMEI] = {
629 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_CHECK_IMEI),
630 .in_event_mask = S(PR_ARQ_E_IMEI_RES),
631 .out_state_mask = S(PR_ARQ_S_DONE) |
632 S(PR_ARQ_S_WAIT_TMSI_ACK),
633 .action = proc_arq_vlr_fn_w_imei,
634 },
635 [PR_ARQ_S_WAIT_TMSI_ACK] = {
636 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_TMSI_ACK),
637 .in_event_mask = S(PR_ARQ_E_TMSI_ACK),
638 .out_state_mask = S(PR_ARQ_S_DONE),
639 .action = proc_arq_vlr_fn_w_tmsi,
640 },
641 [PR_ARQ_S_DONE] = {
642 .name = OSMO_STRINGIFY(PR_ARQ_S_DONE),
643 .onenter = proc_arq_vlr_dispatch_result,
644 },
645};
646
647static struct osmo_fsm proc_arq_vlr_fsm = {
648 .name = "Process_Access_Request_VLR",
649 .states = proc_arq_vlr_states,
650 .num_states = ARRAY_SIZE(proc_arq_vlr_states),
651 .allstate_event_mask = 0,
652 .allstate_action = NULL,
653 .log_subsys = DVLR,
654 .event_names = proc_arq_vlr_event_names,
655 .cleanup = proc_arq_vlr_cleanup,
656};
657
658void
659vlr_proc_acc_req(struct osmo_fsm_inst *parent,
660 uint32_t parent_event_success,
661 uint32_t parent_event_failure,
662 void *parent_event_data,
663 struct vlr_instance *vlr, void *msc_conn_ref,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100664 enum vlr_parq_type type, enum osmo_cm_service_type cm_service_type,
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200665 const struct osmo_mobile_identity *mi,
Harald Welteb8b85a12016-06-17 00:06:42 +0200666 const struct osmo_location_area_id *lai,
667 bool authentication_required,
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200668 bool is_ciphering_to_be_attempted,
Neels Hofmeyr2ea72642022-10-10 23:35:47 +0200669 bool is_ciphering_required,
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100670 uint8_t key_seq,
Harald Welteb8b85a12016-06-17 00:06:42 +0200671 bool is_r99, bool is_utran)
672{
673 struct osmo_fsm_inst *fi;
674 struct proc_arq_priv *par;
Harald Welteb8b85a12016-06-17 00:06:42 +0200675
Neels Hofmeyr2ea72642022-10-10 23:35:47 +0200676 if (is_ciphering_required)
677 OSMO_ASSERT(is_ciphering_to_be_attempted);
678
Harald Welteb8b85a12016-06-17 00:06:42 +0200679 fi = osmo_fsm_inst_alloc_child(&proc_arq_vlr_fsm, parent,
680 parent_event_failure);
681 if (!fi)
682 return;
683
684 par = talloc_zero(fi, struct proc_arq_priv);
685 fi->priv = par;
686 par->vlr = vlr;
687 par->msc_conn_ref = msc_conn_ref;
688 par->type = type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100689 par->cm_service_type = cm_service_type;
Harald Welteb8b85a12016-06-17 00:06:42 +0200690 par->lai = *lai;
691 par->parent_event_success = parent_event_success;
692 par->parent_event_failure = parent_event_failure;
693 par->parent_event_data = parent_event_data;
694 par->authentication_required = authentication_required;
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200695 par->is_ciphering_to_be_attempted = is_ciphering_to_be_attempted;
Neels Hofmeyr2ea72642022-10-10 23:35:47 +0200696 par->is_ciphering_required = is_ciphering_required;
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100697 par->key_seq = key_seq;
Harald Welteb8b85a12016-06-17 00:06:42 +0200698 par->is_r99 = is_r99;
699 par->is_utran = is_utran;
700
701 LOGPFSM(fi, "rev=%s net=%s%s%s\n",
702 is_r99 ? "R99" : "GSM",
703 is_utran ? "UTRAN" : "GERAN",
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200704 (authentication_required || is_ciphering_to_be_attempted) ?
Harald Welteb8b85a12016-06-17 00:06:42 +0200705 " Auth" : " (no Auth)",
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200706 (authentication_required || is_ciphering_to_be_attempted) ?
707 (is_ciphering_to_be_attempted ? "+Ciph" : " (no Ciph)")
Harald Welteb8b85a12016-06-17 00:06:42 +0200708 : "");
709
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200710 if (is_utran && !authentication_required)
711 LOGPFSML(fi, LOGL_ERROR,
712 "Authentication off on UTRAN network. Good luck.\n");
713
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200714 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200715 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200716 OSMO_STRLCPY_ARRAY(par->imsi, mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200717 par->by_tmsi = false;
718 break;
719 case GSM_MI_TYPE_TMSI:
720 par->by_tmsi = true;
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200721 par->tmsi = mi->tmsi;
Harald Welteb8b85a12016-06-17 00:06:42 +0200722 break;
723 case GSM_MI_TYPE_IMEI:
724 /* TODO: IMEI (emergency call) */
725 default:
Philipp Maier6f4752e2018-05-15 15:23:59 +0200726 proc_arq_fsm_done(fi, GSM48_REJECT_INVALID_MANDANTORY_INF);
Harald Welteb8b85a12016-06-17 00:06:42 +0200727 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). */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200735void vlr_parq_cancel(struct osmo_fsm_inst *fi,
736 enum osmo_fsm_term_cause fsm_cause,
737 enum gsm48_reject_value gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200738{
739 if (!fi || fi->state == PR_ARQ_S_DONE)
740 return;
Neels Hofmeyr15809592018-04-06 02:57:51 +0200741 LOGPFSM(fi, "Cancel: %s\n", osmo_fsm_term_cause_name(fsm_cause));
742 proc_arq_fsm_done(fi, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200743}
744
745
746#if 0
747/***********************************************************************
748 * Update_Location_Child_VLR, TS 29.002 Chapter 25.4.4
749 ***********************************************************************/
750
751enum upd_loc_child_vlr_state {
752 ULC_S_IDLE,
753 ULC_S_WAIT_HLR_RESP,
754 ULC_S_DONE,
755};
756
757enum upd_loc_child_vlr_event {
758 ULC_E_START,
759};
760
761static const struct value_string upd_loc_child_vlr_event_names[] = {
762 { ULC_E_START, "START" },
763 { 0, NULL }
764};
765
766static void upd_loc_child_f_idle(struct osmo_fsm_inst *fi, uint32_t event,
767 void *data)
768{
769 OSMO_ASSERT(event == ULC_E_START);
770
771 /* send update location */
772}
773
774static void upd_loc_child_f_w_hlr(struct osmo_fsm_inst *fi, uint32_t event,
775 void *data)
776{
777}
778
779static const struct osmo_fsm_state upd_loc_child_vlr_states[] = {
780 [ULC_S_IDLE] = {
781 .in_event_mask = ,
782 .out_state_mask = S(ULC_S_WAIT_HLR_RESP) |
783 S(ULC_S_DONE),
784 .name = "IDLE",
785 .action = upd_loc_child_f_idle,
786 },
787 [ULC_S_WAIT_HLR_RESP] = {
788 .in_event_mask = ,
789 .out_state_mask = S(ULC_S_DONE),
790 .name = "WAIT-HLR-RESP",
791 .action = upd_loc_child_f_w_hlr,
792 },
793 [ULC_S_DONE] = {
794 .name = "DONE",
795 },
796};
797
798static struct osmo_fsm upd_loc_child_vlr_fsm = {
799 .name = "Update_Location_Child_VLR",
800 .states = upd_loc_child_vlr_states,
801 .num_states = ARRAY_SIZE(upd_loc_child_vlr_states),
802 .log_subsys = DVLR,
803 .event_names = upd_loc_child_vlr_event_names,
804};
805#endif
806
807void vlr_parq_fsm_init(void)
808{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100809 //OSMO_ASSERT(osmo_fsm_register(&upd_loc_child_vlr_fsm) == 0);
810 OSMO_ASSERT(osmo_fsm_register(&proc_arq_vlr_fsm) == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200811}