blob: bcf589e1a90c82ec6f369f9a2e6d8b9f22f21932 [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 Hofmeyr923b6642022-09-28 00:15:45 +020043 OSMO_VALUE_STRING(PR_ARQ_E_AUTH_FAILURE),
Harald Welteb8b85a12016-06-17 00:06:42 +020044 OSMO_VALUE_STRING(PR_ARQ_E_CIPH_RES),
45 OSMO_VALUE_STRING(PR_ARQ_E_UPD_LOC_RES),
46 OSMO_VALUE_STRING(PR_ARQ_E_TRACE_RES),
47 OSMO_VALUE_STRING(PR_ARQ_E_IMEI_RES),
48 OSMO_VALUE_STRING(PR_ARQ_E_PRES_RES),
49 OSMO_VALUE_STRING(PR_ARQ_E_TMSI_ACK),
50 { 0, NULL }
51};
52
53struct proc_arq_priv {
54 struct vlr_instance *vlr;
55 struct vlr_subscr *vsub;
56 void *msc_conn_ref;
57 struct osmo_fsm_inst *ul_child_fsm;
58 struct osmo_fsm_inst *sub_pres_vlr_fsm;
59 uint32_t parent_event_success;
60 uint32_t parent_event_failure;
61 void *parent_event_data;
62
63 enum vlr_parq_type type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010064 enum osmo_cm_service_type cm_service_type;
Neels Hofmeyr15809592018-04-06 02:57:51 +020065 enum gsm48_reject_value result; /*< 0 on success */
Harald Welteb8b85a12016-06-17 00:06:42 +020066 bool by_tmsi;
67 char imsi[16];
68 uint32_t tmsi;
69 struct osmo_location_area_id lai;
70 bool authentication_required;
Neels Hofmeyr2ea72642022-10-10 23:35:47 +020071 /* is_ciphering_to_be_attempted: true when any A5/n > 0 are enabled. Ciphering is allowed, always attempt to get Auth Info from
72 * the HLR. */
Neels Hofmeyrd99a6072022-10-10 23:34:48 +020073 bool is_ciphering_to_be_attempted;
Neels Hofmeyr2ea72642022-10-10 23:35:47 +020074 /* is_ciphering_required: true when A5/0 is disabled. If we cannot get Auth Info from the HLR, reject the
75 * subscriber. */
76 bool is_ciphering_required;
Sylvain Munautda9f37e2019-03-14 11:02:36 +010077 uint8_t key_seq;
Harald Welteb8b85a12016-06-17 00:06:42 +020078 bool is_r99;
79 bool is_utran;
80 bool implicitly_accepted_parq_by_ciphering_cmd;
81};
82
Neels Hofmeyr1035d902018-12-28 21:22:32 +010083static int assoc_par_with_subscr(struct osmo_fsm_inst *fi, struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +020084{
85 struct proc_arq_priv *par = fi->priv;
86 struct vlr_instance *vlr = par->vlr;
87
88 vsub->msc_conn_ref = par->msc_conn_ref;
89 par->vsub = vsub;
90 /* Tell MSC to associate this subscriber with the given
91 * connection */
Neels Hofmeyr1035d902018-12-28 21:22:32 +010092 return vlr->ops.subscr_assoc(par->msc_conn_ref, par->vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +020093}
94
Neels Hofmeyr15809592018-04-06 02:57:51 +020095static const char *vlr_proc_arq_result_name(const struct osmo_fsm_inst *fi)
96{
97 struct proc_arq_priv *par = fi->priv;
98 return par->result? gsm48_reject_value_name(par->result) : "PASSED";
99}
100
Harald Welteb8b85a12016-06-17 00:06:42 +0200101#define proc_arq_fsm_done(fi, res) _proc_arq_fsm_done(fi, res, __FILE__, __LINE__)
102static void _proc_arq_fsm_done(struct osmo_fsm_inst *fi,
Neels Hofmeyr15809592018-04-06 02:57:51 +0200103 enum gsm48_reject_value gsm48_rej,
Harald Welteb8b85a12016-06-17 00:06:42 +0200104 const char *file, int line)
105{
106 struct proc_arq_priv *par = fi->priv;
Neels Hofmeyr15809592018-04-06 02:57:51 +0200107 par->result = gsm48_rej;
108 LOGPFSMSRC(fi, file, line, "proc_arq_fsm_done(%s)\n", vlr_proc_arq_result_name(fi));
Harald Welteb8b85a12016-06-17 00:06:42 +0200109 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_DONE, 0, 0);
110}
111
112static 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;
Neels Hofmeyr15809592018-04-06 02:57:51 +0200118 LOGPFSM(fi, "Process Access Request result: %s\n", vlr_proc_arq_result_name(fi));
Harald Welteb8b85a12016-06-17 00:06:42 +0200119
Neels Hofmeyr15809592018-04-06 02:57:51 +0200120 success = (par->result == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200121
122 /* It would be logical to first dispatch the success event to the
123 * parent FSM, but that could start actions that send messages to the
124 * MS. Rather send the CM Service Accept message first and then signal
125 * success. Since messages are handled synchronously, the success event
126 * will be processed before we handle new incoming data from the MS. */
127
128 if (par->type == VLR_PR_ARQ_T_CM_SERV_REQ) {
129 if (success
130 && !par->implicitly_accepted_parq_by_ciphering_cmd) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100131 rc = par->vlr->ops.tx_cm_serv_acc(par->msc_conn_ref,
132 par->cm_service_type);
Harald Welteb8b85a12016-06-17 00:06:42 +0200133 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,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100141 par->cm_service_type,
Harald Welteb8b85a12016-06-17 00:06:42 +0200142 par->result);
143 if (rc)
144 LOGPFSML(fi, LOGL_ERROR,
145 "Failed to send CM Service Reject\n");
146 }
147 }
148
149 /* For VLR_PR_ARQ_T_PAGING_RESP, there is nothing to send. The conn_fsm
150 * will start handling pending paging transactions. */
151
152 if (!fi->proc.parent) {
Neels Hofmeyr5b1e0302019-05-06 23:45:09 +0200153 LOGPFSML(fi, LOGL_ERROR, "No parent FSM\n");
Harald Welteb8b85a12016-06-17 00:06:42 +0200154 return;
155 }
156 osmo_fsm_inst_dispatch(fi->proc.parent,
157 success ? par->parent_event_success
158 : par->parent_event_failure,
159 par->parent_event_data);
160}
161
162void proc_arq_vlr_cleanup(struct osmo_fsm_inst *fi,
163 enum osmo_fsm_term_cause cause)
164{
165 struct proc_arq_priv *par = fi->priv;
166 if (par->vsub && par->vsub->proc_arq_fsm == fi)
167 par->vsub->proc_arq_fsm = NULL;
168}
169
170static void _proc_arq_vlr_post_imei(struct osmo_fsm_inst *fi)
171{
172 struct proc_arq_priv *par = fi->priv;
173 struct vlr_subscr *vsub = par->vsub;
174
175 LOGPFSM(fi, "%s()\n", __func__);
176
Neels Hofmeyref9126c2017-07-18 15:38:39 +0200177 /* See 3GPP TS 29.002 Proc_Acc_Req_VLR3. */
Harald Welteb8b85a12016-06-17 00:06:42 +0200178 /* TODO: Identity := IMSI */
179 if (0 /* TODO: TMSI reallocation at access: vlr->cfg.alloc_tmsi_arq */) {
180 vlr_subscr_alloc_tmsi(vsub);
181 /* TODO: forward TMSI to MS, wait for TMSI
182 * REALLOC COMPLETE */
183 /* TODO: Freeze old TMSI */
184 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_TMSI_ACK, 0, 0);
185 return;
186 }
187
Neels Hofmeyr15809592018-04-06 02:57:51 +0200188 proc_arq_fsm_done(fi, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200189}
190
191static void _proc_arq_vlr_post_trace(struct osmo_fsm_inst *fi)
192{
193 struct proc_arq_priv *par = fi->priv;
194 struct vlr_subscr *vsub = par->vsub;
195 struct vlr_instance *vlr = vsub->vlr;
196
197 LOGPFSM(fi, "%s()\n", __func__);
198
199 /* Node 3 */
Neels Hofmeyref9126c2017-07-18 15:38:39 +0200200 /* See 3GPP TS 29.002 Proc_Acc_Req_VLR3. */
Harald Welteb8b85a12016-06-17 00:06:42 +0200201 if (0 /* IMEI check required */) {
202 /* Chck_IMEI_VLR */
203 vlr->ops.tx_id_req(par->msc_conn_ref, GSM_MI_TYPE_IMEI);
204 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_CHECK_IMEI,
205 vlr_timer(vlr, 3270), 3270);
206 } else
207 _proc_arq_vlr_post_imei(fi);
208}
209
210/* After Subscriber_Present_VLR */
211static void _proc_arq_vlr_post_pres(struct osmo_fsm_inst *fi)
212{
213 LOGPFSM(fi, "%s()\n", __func__);
Neels Hofmeyref9126c2017-07-18 15:38:39 +0200214 /* See 3GPP TS 29.002 Proc_Acc_Req_VLR3. */
Harald Welteb8b85a12016-06-17 00:06:42 +0200215 if (0 /* TODO: tracing required */) {
216 /* TODO: Trace_Subscriber_Activity_VLR */
217 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_TRACE_SUB, 0, 0);
218 }
219 _proc_arq_vlr_post_trace(fi);
220}
221
222/* After Update_Location_Child_VLR */
223static void _proc_arq_vlr_node2_post_vlr(struct osmo_fsm_inst *fi)
224{
225 struct proc_arq_priv *par = fi->priv;
226 struct vlr_subscr *vsub = par->vsub;
227
228 LOGPFSM(fi, "%s()\n", __func__);
229
230 if (!vsub->sub_dataconf_by_hlr_ind) {
231 /* Set User Error: Unidentified Subscriber */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200232 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200233 return;
234 }
Neels Hofmeyref9126c2017-07-18 15:38:39 +0200235 /* We don't feature location area specific blocking (yet). */
Harald Welteb8b85a12016-06-17 00:06:42 +0200236 if (0 /* roaming not allowed in LA */) {
237 /* Set User Error: Roaming not allowed in this LA */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200238 proc_arq_fsm_done(fi, GSM48_REJECT_ROAMING_NOT_ALLOWED);
Harald Welteb8b85a12016-06-17 00:06:42 +0200239 return;
240 }
241 vsub->imsi_detached_flag = false;
242 if (vsub->ms_not_reachable_flag) {
243 /* Start Subscriber_Present_VLR */
244 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_SUB_PRES, 0, 0);
Neels Hofmeyr1a5bcd52017-11-18 22:19:55 +0100245 sub_pres_vlr_fsm_start(&par->sub_pres_vlr_fsm, fi, vsub, PR_ARQ_E_PRES_RES);
Harald Welteb8b85a12016-06-17 00:06:42 +0200246 return;
247 }
248 _proc_arq_vlr_post_pres(fi);
249}
250
251static 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;
Harald Welte544a32f2020-06-21 22:15:53 +0200255 int rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200256
257 LOGPFSM(fi, "%s()\n", __func__);
258
Harald Welte544a32f2020-06-21 22:15:53 +0200259 rc = par->vlr->ops.tx_common_id(par->msc_conn_ref);
260 if (rc)
261 LOGPFSML(fi, LOGL_ERROR, "Error while sending Common ID (%d)\n", rc);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200262
Harald Welteb8b85a12016-06-17 00:06:42 +0200263 vsub->conf_by_radio_contact_ind = true;
264 if (vsub->loc_conf_in_hlr_ind == false) {
265 /* start Update_Location_Child_VLR. WE use
266 * Update_HLR_VLR instead, the differences appear
267 * insignificant for now. */
268 par->ul_child_fsm = upd_hlr_vlr_proc_start(fi, vsub,
269 PR_ARQ_E_UPD_LOC_RES);
270 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_UPD_LOC_CHILD, 0, 0);
271 return;
272 }
273 _proc_arq_vlr_node2_post_vlr(fi);
274}
275
Neels Hofmeyrc48afa12022-10-10 23:47:04 +0200276/* Return true when CipherModeCmd / SecurityModeCmd should be attempted. */
277static bool is_cmc_smc_to_be_attempted(struct proc_arq_priv *par)
Harald Welteb8b85a12016-06-17 00:06:42 +0200278{
Vadim Yanitskiy565ea2b2021-11-28 16:42:58 +0300279 /* UTRAN: always send SecModeCmd, even if ciphering is not required.
280 * GERAN: avoid sending CiphModeCmd if ciphering is not required. */
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200281 return par->is_utran || par->is_ciphering_to_be_attempted;
Harald Welteb8b85a12016-06-17 00:06:42 +0200282}
283
284static 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;
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100288 bool umts_aka;
Harald Welteb8b85a12016-06-17 00:06:42 +0200289
290 LOGPFSM(fi, "%s()\n", __func__);
291
Neels Hofmeyrc48afa12022-10-10 23:47:04 +0200292 if (!is_cmc_smc_to_be_attempted(par)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200293 _proc_arq_vlr_node2_post_ciph(fi);
294 return;
295 }
296
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100297 switch (vsub->sec_ctx) {
298 case VLR_SEC_CTX_GSM:
299 umts_aka = false;
300 break;
301 case VLR_SEC_CTX_UMTS:
302 umts_aka = true;
303 break;
304 default:
305 LOGPFSML(fi, LOGL_ERROR, "Cannot start ciphering, security context is not established\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200306 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100307 return;
308 }
309
Harald Welteb8b85a12016-06-17 00:06:42 +0200310 if (vlr_set_ciph_mode(vsub->vlr, fi, par->msc_conn_ref,
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100311 umts_aka,
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200312 vsub->vlr->cfg.retrieve_imeisv_ciphered)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200313 LOGPFSML(fi, LOGL_ERROR,
314 "Failed to send Ciphering Mode Command\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200315 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200316 return;
317 }
318
319 par->implicitly_accepted_parq_by_ciphering_cmd = true;
320 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_CIPH, 0, 0);
321}
322
Neels Hofmeyrc48afa12022-10-10 23:47:04 +0200323static bool is_auth_to_be_attempted(struct proc_arq_priv *par)
Harald Welteb8b85a12016-06-17 00:06:42 +0200324{
325 /* The cases where the authentication procedure should be used
326 * are defined in 3GPP TS 33.102 */
327 /* For now we use a default value passed in to vlr_lu_fsm(). */
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100328 return par->authentication_required ||
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200329 (par->is_ciphering_to_be_attempted && !auth_try_reuse_tuple(par->vsub, par->key_seq));
Harald Welteb8b85a12016-06-17 00:06:42 +0200330}
331
332/* after the IMSI is known */
333static void proc_arq_vlr_fn_post_imsi(struct osmo_fsm_inst *fi)
334{
335 struct proc_arq_priv *par = fi->priv;
336 struct vlr_subscr *vsub = par->vsub;
337
338 LOGPFSM(fi, "%s()\n", __func__);
339
340 OSMO_ASSERT(vsub);
341
342 /* TODO: Identity IMEI -> System Failure */
Neels Hofmeyrc48afa12022-10-10 23:47:04 +0200343 if (is_auth_to_be_attempted(par)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200344 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_AUTH,
345 0, 0);
Vadim Yanitskiy3daf0c22020-01-25 06:02:48 +0700346 vsub->auth_fsm = auth_fsm_start(vsub, fi,
Harald Welteb8b85a12016-06-17 00:06:42 +0200347 PR_ARQ_E_AUTH_RES,
Neels Hofmeyr923b6642022-09-28 00:15:45 +0200348 PR_ARQ_E_AUTH_FAILURE,
Harald Welteb8b85a12016-06-17 00:06:42 +0200349 par->is_r99,
350 par->is_utran);
351 } else {
352 _proc_arq_vlr_node2(fi);
353 }
354}
355
356static void proc_arq_vlr_fn_init(struct osmo_fsm_inst *fi,
357 uint32_t event, void *data)
358{
359 struct proc_arq_priv *par = fi->priv;
360 struct vlr_instance *vlr = par->vlr;
361 struct vlr_subscr *vsub = NULL;
362
363 OSMO_ASSERT(event == PR_ARQ_E_START);
364
365 /* Obtain_Identity_VLR */
366 if (!par->by_tmsi) {
367 /* IMSI was included */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100368 vsub = vlr_subscr_find_by_imsi(par->vlr, par->imsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200369 } else {
370 /* TMSI was included */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100371 vsub = vlr_subscr_find_by_tmsi(par->vlr, par->tmsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200372 }
373 if (vsub) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200374 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200375 if (vsub->proc_arq_fsm && fi != vsub->proc_arq_fsm) {
376 LOGPFSML(fi, LOGL_ERROR,
377 "Another proc_arq_fsm is already"
378 " associated with subscr %s,"
379 " terminating the other FSM.\n",
380 vlr_subscr_name(vsub));
381 proc_arq_fsm_done(vsub->proc_arq_fsm,
Neels Hofmeyr15809592018-04-06 02:57:51 +0200382 GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200383 }
384 vsub->proc_arq_fsm = fi;
Neels Hofmeyr1035d902018-12-28 21:22:32 +0100385 if (assoc_par_with_subscr(fi, vsub) != 0)
386 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
387 else
388 proc_arq_vlr_fn_post_imsi(fi);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100389 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200390 return;
391 }
392 /* No VSUB could be resolved. What now? */
393
394 if (!par->by_tmsi) {
395 /* We couldn't find a subscriber even by IMSI,
396 * Set User Error: Unidentified Subscriber */
Philipp Maier6f4752e2018-05-15 15:23:59 +0200397 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200398 return;
399 } else {
400 /* TMSI was included, are we permitted to use it? */
401 if (vlr->cfg.parq_retrieve_imsi) {
402 /* Obtain_IMSI_VLR */
403 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_OBTAIN_IMSI,
404 vlr_timer(vlr, 3270), 3270);
405 return;
406 } else {
407 /* Set User Error: Unidentified Subscriber */
Philipp Maier6f4752e2018-05-15 15:23:59 +0200408 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200409 return;
410 }
411 }
412}
413
414/* ID REQ(IMSI) has returned */
415static void proc_arq_vlr_fn_w_obt_imsi(struct osmo_fsm_inst *fi,
416 uint32_t event, void *data)
417{
418 struct proc_arq_priv *par = fi->priv;
419 struct vlr_instance *vlr = par->vlr;
420 struct vlr_subscr *vsub;
421
422 OSMO_ASSERT(event == PR_ARQ_E_ID_IMSI);
423
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100424 vsub = vlr_subscr_find_by_imsi(vlr, par->imsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200425 if (!vsub) {
426 /* Set User Error: Unidentified Subscriber */
Philipp Maier6f4752e2018-05-15 15:23:59 +0200427 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200428 return;
429 }
Neels Hofmeyr1035d902018-12-28 21:22:32 +0100430 if (assoc_par_with_subscr(fi, vsub))
431 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
432 else
433 proc_arq_vlr_fn_post_imsi(fi);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100434 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200435}
436
437/* Authenticate_VLR has completed */
438static void proc_arq_vlr_fn_w_auth(struct osmo_fsm_inst *fi,
439 uint32_t event, void *data)
440{
Neels Hofmeyr15809592018-04-06 02:57:51 +0200441 enum gsm48_reject_value *cause = data;
Harald Welteb8b85a12016-06-17 00:06:42 +0200442
Neels Hofmeyr923b6642022-09-28 00:15:45 +0200443 switch (event) {
444 case PR_ARQ_E_AUTH_RES:
445 /* Node 2 */
446 _proc_arq_vlr_node2(fi);
447 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200448
Neels Hofmeyr923b6642022-09-28 00:15:45 +0200449 case PR_ARQ_E_AUTH_FAILURE:
Neels Hofmeyr15809592018-04-06 02:57:51 +0200450 proc_arq_fsm_done(fi, cause? *cause : GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200451 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200452
Neels Hofmeyr923b6642022-09-28 00:15:45 +0200453 default:
454 OSMO_ASSERT(false);
455 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200456}
457
458static void proc_arq_vlr_fn_w_ciph(struct osmo_fsm_inst *fi,
459 uint32_t event, void *data)
460{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100461 enum vlr_ciph_result_cause result = VLR_CIPH_REJECT;
Harald Welteb8b85a12016-06-17 00:06:42 +0200462
463 OSMO_ASSERT(event == PR_ARQ_E_CIPH_RES);
464
465 if (!data)
466 LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: NULL\n");
467 else
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100468 result = *(enum vlr_ciph_result_cause*)data;
Harald Welteb8b85a12016-06-17 00:06:42 +0200469
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100470 switch (result) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200471 case VLR_CIPH_COMPL:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100472 _proc_arq_vlr_node2_post_ciph(fi);
473 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200474 case VLR_CIPH_REJECT:
475 LOGPFSM(fi, "ciphering rejected\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200476 proc_arq_fsm_done(fi, GSM48_REJECT_ILLEGAL_MS);
Harald Welteb8b85a12016-06-17 00:06:42 +0200477 return;
478 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100479 LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: %d\n", result);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200480 proc_arq_fsm_done(fi, GSM48_REJECT_ILLEGAL_MS);
Harald Welteb8b85a12016-06-17 00:06:42 +0200481 return;
482 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200483}
484
485/* Update_Location_Child_VLR has completed */
486static void proc_arq_vlr_fn_w_upd_loc(struct osmo_fsm_inst *fi,
487 uint32_t event, void *data)
488{
489 OSMO_ASSERT(event == PR_ARQ_E_UPD_LOC_RES);
490
491 _proc_arq_vlr_node2_post_vlr(fi);
492}
493
494/* Subscriber_Present_VLR has completed */
495static void proc_arq_vlr_fn_w_pres(struct osmo_fsm_inst *fi,
496 uint32_t event, void *data)
497{
498 OSMO_ASSERT(event == PR_ARQ_E_PRES_RES);
499
500 _proc_arq_vlr_post_pres(fi);
501}
502
503static void proc_arq_vlr_fn_w_trace(struct osmo_fsm_inst *fi,
504 uint32_t event, void *data)
505{
506 OSMO_ASSERT(event == PR_ARQ_E_TRACE_RES);
507
508 _proc_arq_vlr_post_trace(fi);
509}
510
511/* we have received the ID RESPONSE (IMEI) */
512static void proc_arq_vlr_fn_w_imei(struct osmo_fsm_inst *fi,
513 uint32_t event, void *data)
514{
515 OSMO_ASSERT(event == PR_ARQ_E_IMEI_RES);
516
517 _proc_arq_vlr_post_imei(fi);
518}
519
520/* MSC tells us that MS has acknowleded TMSI re-allocation */
521static void proc_arq_vlr_fn_w_tmsi(struct osmo_fsm_inst *fi,
522 uint32_t event, void *data)
523{
524 OSMO_ASSERT(event == PR_ARQ_E_TMSI_ACK);
525
526 /* FIXME: check confirmation? unfreeze? */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200527 proc_arq_fsm_done(fi, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200528}
529
530static const struct osmo_fsm_state proc_arq_vlr_states[] = {
531 [PR_ARQ_S_INIT] = {
532 .name = OSMO_STRINGIFY(PR_ARQ_S_INIT),
533 .in_event_mask = S(PR_ARQ_E_START),
534 .out_state_mask = S(PR_ARQ_S_DONE) |
535 S(PR_ARQ_S_WAIT_OBTAIN_IMSI) |
536 S(PR_ARQ_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100537 S(PR_ARQ_S_WAIT_CIPH) |
Harald Welteb8b85a12016-06-17 00:06:42 +0200538 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
539 S(PR_ARQ_S_WAIT_SUB_PRES) |
540 S(PR_ARQ_S_WAIT_TRACE_SUB) |
541 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
542 S(PR_ARQ_S_WAIT_TMSI_ACK),
543 .action = proc_arq_vlr_fn_init,
544 },
545 [PR_ARQ_S_WAIT_OBTAIN_IMSI] = {
546 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_OBTAIN_IMSI),
547 .in_event_mask = S(PR_ARQ_E_ID_IMSI),
548 .out_state_mask = S(PR_ARQ_S_DONE) |
549 S(PR_ARQ_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100550 S(PR_ARQ_S_WAIT_CIPH) |
Harald Welteb8b85a12016-06-17 00:06:42 +0200551 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
552 S(PR_ARQ_S_WAIT_SUB_PRES) |
553 S(PR_ARQ_S_WAIT_TRACE_SUB) |
554 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
555 S(PR_ARQ_S_WAIT_TMSI_ACK),
556 .action = proc_arq_vlr_fn_w_obt_imsi,
557 },
558 [PR_ARQ_S_WAIT_AUTH] = {
559 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_AUTH),
Neels Hofmeyr923b6642022-09-28 00:15:45 +0200560 .in_event_mask = S(PR_ARQ_E_AUTH_RES) |
561 S(PR_ARQ_E_AUTH_FAILURE),
Harald Welteb8b85a12016-06-17 00:06:42 +0200562 .out_state_mask = S(PR_ARQ_S_DONE) |
563 S(PR_ARQ_S_WAIT_CIPH) |
564 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
565 S(PR_ARQ_S_WAIT_SUB_PRES) |
566 S(PR_ARQ_S_WAIT_TRACE_SUB) |
567 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
568 S(PR_ARQ_S_WAIT_TMSI_ACK),
569 .action = proc_arq_vlr_fn_w_auth,
570 },
571 [PR_ARQ_S_WAIT_CIPH] = {
572 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_CIPH),
573 .in_event_mask = S(PR_ARQ_E_CIPH_RES),
574 .out_state_mask = S(PR_ARQ_S_DONE) |
575 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
576 S(PR_ARQ_S_WAIT_SUB_PRES) |
577 S(PR_ARQ_S_WAIT_TRACE_SUB) |
578 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
579 S(PR_ARQ_S_WAIT_TMSI_ACK),
580 .action = proc_arq_vlr_fn_w_ciph,
581 },
582 [PR_ARQ_S_WAIT_UPD_LOC_CHILD] = {
583 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_UPD_LOC_CHILD),
584 .in_event_mask = S(PR_ARQ_E_UPD_LOC_RES),
585 .out_state_mask = S(PR_ARQ_S_DONE) |
586 S(PR_ARQ_S_WAIT_SUB_PRES) |
587 S(PR_ARQ_S_WAIT_TRACE_SUB) |
588 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
589 S(PR_ARQ_S_WAIT_TMSI_ACK),
590 .action = proc_arq_vlr_fn_w_upd_loc,
591 },
592 [PR_ARQ_S_WAIT_SUB_PRES] = {
593 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_SUB_PRES),
594 .in_event_mask = S(PR_ARQ_E_PRES_RES),
595 .out_state_mask = S(PR_ARQ_S_DONE) |
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_pres,
600 },
601 [PR_ARQ_S_WAIT_TRACE_SUB] = {
602 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_TRACE_SUB),
603 .in_event_mask = S(PR_ARQ_E_TRACE_RES),
604 .out_state_mask = S(PR_ARQ_S_DONE) |
605 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
606 S(PR_ARQ_S_WAIT_TMSI_ACK),
607 .action = proc_arq_vlr_fn_w_trace,
608 },
609 [PR_ARQ_S_WAIT_CHECK_IMEI] = {
610 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_CHECK_IMEI),
611 .in_event_mask = S(PR_ARQ_E_IMEI_RES),
612 .out_state_mask = S(PR_ARQ_S_DONE) |
613 S(PR_ARQ_S_WAIT_TMSI_ACK),
614 .action = proc_arq_vlr_fn_w_imei,
615 },
616 [PR_ARQ_S_WAIT_TMSI_ACK] = {
617 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_TMSI_ACK),
618 .in_event_mask = S(PR_ARQ_E_TMSI_ACK),
619 .out_state_mask = S(PR_ARQ_S_DONE),
620 .action = proc_arq_vlr_fn_w_tmsi,
621 },
622 [PR_ARQ_S_DONE] = {
623 .name = OSMO_STRINGIFY(PR_ARQ_S_DONE),
624 .onenter = proc_arq_vlr_dispatch_result,
625 },
626};
627
628static struct osmo_fsm proc_arq_vlr_fsm = {
629 .name = "Process_Access_Request_VLR",
630 .states = proc_arq_vlr_states,
631 .num_states = ARRAY_SIZE(proc_arq_vlr_states),
632 .allstate_event_mask = 0,
633 .allstate_action = NULL,
634 .log_subsys = DVLR,
635 .event_names = proc_arq_vlr_event_names,
636 .cleanup = proc_arq_vlr_cleanup,
637};
638
639void
640vlr_proc_acc_req(struct osmo_fsm_inst *parent,
641 uint32_t parent_event_success,
642 uint32_t parent_event_failure,
643 void *parent_event_data,
644 struct vlr_instance *vlr, void *msc_conn_ref,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100645 enum vlr_parq_type type, enum osmo_cm_service_type cm_service_type,
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200646 const struct osmo_mobile_identity *mi,
Harald Welteb8b85a12016-06-17 00:06:42 +0200647 const struct osmo_location_area_id *lai,
648 bool authentication_required,
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200649 bool is_ciphering_to_be_attempted,
Neels Hofmeyr2ea72642022-10-10 23:35:47 +0200650 bool is_ciphering_required,
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100651 uint8_t key_seq,
Harald Welteb8b85a12016-06-17 00:06:42 +0200652 bool is_r99, bool is_utran)
653{
654 struct osmo_fsm_inst *fi;
655 struct proc_arq_priv *par;
Harald Welteb8b85a12016-06-17 00:06:42 +0200656
Neels Hofmeyr2ea72642022-10-10 23:35:47 +0200657 if (is_ciphering_required)
658 OSMO_ASSERT(is_ciphering_to_be_attempted);
659
Harald Welteb8b85a12016-06-17 00:06:42 +0200660 fi = osmo_fsm_inst_alloc_child(&proc_arq_vlr_fsm, parent,
661 parent_event_failure);
662 if (!fi)
663 return;
664
665 par = talloc_zero(fi, struct proc_arq_priv);
666 fi->priv = par;
667 par->vlr = vlr;
668 par->msc_conn_ref = msc_conn_ref;
669 par->type = type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100670 par->cm_service_type = cm_service_type;
Harald Welteb8b85a12016-06-17 00:06:42 +0200671 par->lai = *lai;
672 par->parent_event_success = parent_event_success;
673 par->parent_event_failure = parent_event_failure;
674 par->parent_event_data = parent_event_data;
675 par->authentication_required = authentication_required;
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200676 par->is_ciphering_to_be_attempted = is_ciphering_to_be_attempted;
Neels Hofmeyr2ea72642022-10-10 23:35:47 +0200677 par->is_ciphering_required = is_ciphering_required;
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100678 par->key_seq = key_seq;
Harald Welteb8b85a12016-06-17 00:06:42 +0200679 par->is_r99 = is_r99;
680 par->is_utran = is_utran;
681
682 LOGPFSM(fi, "rev=%s net=%s%s%s\n",
683 is_r99 ? "R99" : "GSM",
684 is_utran ? "UTRAN" : "GERAN",
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200685 (authentication_required || is_ciphering_to_be_attempted) ?
Harald Welteb8b85a12016-06-17 00:06:42 +0200686 " Auth" : " (no Auth)",
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200687 (authentication_required || is_ciphering_to_be_attempted) ?
688 (is_ciphering_to_be_attempted ? "+Ciph" : " (no Ciph)")
Harald Welteb8b85a12016-06-17 00:06:42 +0200689 : "");
690
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200691 if (is_utran && !authentication_required)
692 LOGPFSML(fi, LOGL_ERROR,
693 "Authentication off on UTRAN network. Good luck.\n");
694
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200695 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200696 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200697 OSMO_STRLCPY_ARRAY(par->imsi, mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200698 par->by_tmsi = false;
699 break;
700 case GSM_MI_TYPE_TMSI:
701 par->by_tmsi = true;
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200702 par->tmsi = mi->tmsi;
Harald Welteb8b85a12016-06-17 00:06:42 +0200703 break;
704 case GSM_MI_TYPE_IMEI:
705 /* TODO: IMEI (emergency call) */
706 default:
Philipp Maier6f4752e2018-05-15 15:23:59 +0200707 proc_arq_fsm_done(fi, GSM48_REJECT_INVALID_MANDANTORY_INF);
Harald Welteb8b85a12016-06-17 00:06:42 +0200708 return;
709 }
710
711 osmo_fsm_inst_dispatch(fi, PR_ARQ_E_START, NULL);
712}
713
714/* Gracefully terminate an FSM created by vlr_proc_acc_req() in case of
715 * external timeout (i.e. from MSC). */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200716void vlr_parq_cancel(struct osmo_fsm_inst *fi,
717 enum osmo_fsm_term_cause fsm_cause,
718 enum gsm48_reject_value gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200719{
720 if (!fi || fi->state == PR_ARQ_S_DONE)
721 return;
Neels Hofmeyr15809592018-04-06 02:57:51 +0200722 LOGPFSM(fi, "Cancel: %s\n", osmo_fsm_term_cause_name(fsm_cause));
723 proc_arq_fsm_done(fi, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200724}
725
726
727#if 0
728/***********************************************************************
729 * Update_Location_Child_VLR, TS 29.002 Chapter 25.4.4
730 ***********************************************************************/
731
732enum upd_loc_child_vlr_state {
733 ULC_S_IDLE,
734 ULC_S_WAIT_HLR_RESP,
735 ULC_S_DONE,
736};
737
738enum upd_loc_child_vlr_event {
739 ULC_E_START,
740};
741
742static const struct value_string upd_loc_child_vlr_event_names[] = {
743 { ULC_E_START, "START" },
744 { 0, NULL }
745};
746
747static void upd_loc_child_f_idle(struct osmo_fsm_inst *fi, uint32_t event,
748 void *data)
749{
750 OSMO_ASSERT(event == ULC_E_START);
751
752 /* send update location */
753}
754
755static void upd_loc_child_f_w_hlr(struct osmo_fsm_inst *fi, uint32_t event,
756 void *data)
757{
758}
759
760static const struct osmo_fsm_state upd_loc_child_vlr_states[] = {
761 [ULC_S_IDLE] = {
762 .in_event_mask = ,
763 .out_state_mask = S(ULC_S_WAIT_HLR_RESP) |
764 S(ULC_S_DONE),
765 .name = "IDLE",
766 .action = upd_loc_child_f_idle,
767 },
768 [ULC_S_WAIT_HLR_RESP] = {
769 .in_event_mask = ,
770 .out_state_mask = S(ULC_S_DONE),
771 .name = "WAIT-HLR-RESP",
772 .action = upd_loc_child_f_w_hlr,
773 },
774 [ULC_S_DONE] = {
775 .name = "DONE",
776 },
777};
778
779static struct osmo_fsm upd_loc_child_vlr_fsm = {
780 .name = "Update_Location_Child_VLR",
781 .states = upd_loc_child_vlr_states,
782 .num_states = ARRAY_SIZE(upd_loc_child_vlr_states),
783 .log_subsys = DVLR,
784 .event_names = upd_loc_child_vlr_event_names,
785};
786#endif
787
788void vlr_parq_fsm_init(void)
789{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100790 //OSMO_ASSERT(osmo_fsm_register(&upd_loc_child_vlr_fsm) == 0);
791 OSMO_ASSERT(osmo_fsm_register(&proc_arq_vlr_fsm) == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200792}