blob: 8daaaad680f95756b6fd3dda4f283f41c21f80d9 [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;
Harald Welte71c51df2017-12-23 18:51:48 +010070 bool ciphering_required;
Sylvain Munautda9f37e2019-03-14 11:02:36 +010071 uint8_t key_seq;
Harald Welteb8b85a12016-06-17 00:06:42 +020072 bool is_r99;
73 bool is_utran;
74 bool implicitly_accepted_parq_by_ciphering_cmd;
75};
76
Neels Hofmeyr1035d902018-12-28 21:22:32 +010077static int assoc_par_with_subscr(struct osmo_fsm_inst *fi, struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +020078{
79 struct proc_arq_priv *par = fi->priv;
80 struct vlr_instance *vlr = par->vlr;
81
82 vsub->msc_conn_ref = par->msc_conn_ref;
83 par->vsub = vsub;
84 /* Tell MSC to associate this subscriber with the given
85 * connection */
Neels Hofmeyr1035d902018-12-28 21:22:32 +010086 return vlr->ops.subscr_assoc(par->msc_conn_ref, par->vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +020087}
88
Neels Hofmeyr15809592018-04-06 02:57:51 +020089static const char *vlr_proc_arq_result_name(const struct osmo_fsm_inst *fi)
90{
91 struct proc_arq_priv *par = fi->priv;
92 return par->result? gsm48_reject_value_name(par->result) : "PASSED";
93}
94
Harald Welteb8b85a12016-06-17 00:06:42 +020095#define proc_arq_fsm_done(fi, res) _proc_arq_fsm_done(fi, res, __FILE__, __LINE__)
96static void _proc_arq_fsm_done(struct osmo_fsm_inst *fi,
Neels Hofmeyr15809592018-04-06 02:57:51 +020097 enum gsm48_reject_value gsm48_rej,
Harald Welteb8b85a12016-06-17 00:06:42 +020098 const char *file, int line)
99{
100 struct proc_arq_priv *par = fi->priv;
Neels Hofmeyr15809592018-04-06 02:57:51 +0200101 par->result = gsm48_rej;
102 LOGPFSMSRC(fi, file, line, "proc_arq_fsm_done(%s)\n", vlr_proc_arq_result_name(fi));
Harald Welteb8b85a12016-06-17 00:06:42 +0200103 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_DONE, 0, 0);
104}
105
106static void proc_arq_vlr_dispatch_result(struct osmo_fsm_inst *fi,
107 uint32_t prev_state)
108{
109 struct proc_arq_priv *par = fi->priv;
110 bool success;
111 int rc;
Neels Hofmeyr15809592018-04-06 02:57:51 +0200112 LOGPFSM(fi, "Process Access Request result: %s\n", vlr_proc_arq_result_name(fi));
Harald Welteb8b85a12016-06-17 00:06:42 +0200113
Neels Hofmeyr15809592018-04-06 02:57:51 +0200114 success = (par->result == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200115
116 /* It would be logical to first dispatch the success event to the
117 * parent FSM, but that could start actions that send messages to the
118 * MS. Rather send the CM Service Accept message first and then signal
119 * success. Since messages are handled synchronously, the success event
120 * will be processed before we handle new incoming data from the MS. */
121
122 if (par->type == VLR_PR_ARQ_T_CM_SERV_REQ) {
123 if (success
124 && !par->implicitly_accepted_parq_by_ciphering_cmd) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100125 rc = par->vlr->ops.tx_cm_serv_acc(par->msc_conn_ref,
126 par->cm_service_type);
Harald Welteb8b85a12016-06-17 00:06:42 +0200127 if (rc) {
128 LOGPFSML(fi, LOGL_ERROR,
129 "Failed to send CM Service Accept\n");
130 success = false;
131 }
132 }
133 if (!success) {
134 rc = par->vlr->ops.tx_cm_serv_rej(par->msc_conn_ref,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100135 par->cm_service_type,
Harald Welteb8b85a12016-06-17 00:06:42 +0200136 par->result);
137 if (rc)
138 LOGPFSML(fi, LOGL_ERROR,
139 "Failed to send CM Service Reject\n");
140 }
141 }
142
143 /* For VLR_PR_ARQ_T_PAGING_RESP, there is nothing to send. The conn_fsm
144 * will start handling pending paging transactions. */
145
146 if (!fi->proc.parent) {
Neels Hofmeyr5b1e0302019-05-06 23:45:09 +0200147 LOGPFSML(fi, LOGL_ERROR, "No parent FSM\n");
Harald Welteb8b85a12016-06-17 00:06:42 +0200148 return;
149 }
150 osmo_fsm_inst_dispatch(fi->proc.parent,
151 success ? par->parent_event_success
152 : par->parent_event_failure,
153 par->parent_event_data);
154}
155
156void proc_arq_vlr_cleanup(struct osmo_fsm_inst *fi,
157 enum osmo_fsm_term_cause cause)
158{
159 struct proc_arq_priv *par = fi->priv;
160 if (par->vsub && par->vsub->proc_arq_fsm == fi)
161 par->vsub->proc_arq_fsm = NULL;
162}
163
164static void _proc_arq_vlr_post_imei(struct osmo_fsm_inst *fi)
165{
166 struct proc_arq_priv *par = fi->priv;
167 struct vlr_subscr *vsub = par->vsub;
168
169 LOGPFSM(fi, "%s()\n", __func__);
170
Neels Hofmeyref9126c2017-07-18 15:38:39 +0200171 /* See 3GPP TS 29.002 Proc_Acc_Req_VLR3. */
Harald Welteb8b85a12016-06-17 00:06:42 +0200172 /* TODO: Identity := IMSI */
173 if (0 /* TODO: TMSI reallocation at access: vlr->cfg.alloc_tmsi_arq */) {
174 vlr_subscr_alloc_tmsi(vsub);
175 /* TODO: forward TMSI to MS, wait for TMSI
176 * REALLOC COMPLETE */
177 /* TODO: Freeze old TMSI */
178 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_TMSI_ACK, 0, 0);
179 return;
180 }
181
Neels Hofmeyr15809592018-04-06 02:57:51 +0200182 proc_arq_fsm_done(fi, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200183}
184
185static void _proc_arq_vlr_post_trace(struct osmo_fsm_inst *fi)
186{
187 struct proc_arq_priv *par = fi->priv;
188 struct vlr_subscr *vsub = par->vsub;
189 struct vlr_instance *vlr = vsub->vlr;
190
191 LOGPFSM(fi, "%s()\n", __func__);
192
193 /* Node 3 */
Neels Hofmeyref9126c2017-07-18 15:38:39 +0200194 /* See 3GPP TS 29.002 Proc_Acc_Req_VLR3. */
Harald Welteb8b85a12016-06-17 00:06:42 +0200195 if (0 /* IMEI check required */) {
196 /* Chck_IMEI_VLR */
197 vlr->ops.tx_id_req(par->msc_conn_ref, GSM_MI_TYPE_IMEI);
198 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_CHECK_IMEI,
199 vlr_timer(vlr, 3270), 3270);
200 } else
201 _proc_arq_vlr_post_imei(fi);
202}
203
204/* After Subscriber_Present_VLR */
205static void _proc_arq_vlr_post_pres(struct osmo_fsm_inst *fi)
206{
207 LOGPFSM(fi, "%s()\n", __func__);
Neels Hofmeyref9126c2017-07-18 15:38:39 +0200208 /* See 3GPP TS 29.002 Proc_Acc_Req_VLR3. */
Harald Welteb8b85a12016-06-17 00:06:42 +0200209 if (0 /* TODO: tracing required */) {
210 /* TODO: Trace_Subscriber_Activity_VLR */
211 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_TRACE_SUB, 0, 0);
212 }
213 _proc_arq_vlr_post_trace(fi);
214}
215
216/* After Update_Location_Child_VLR */
217static void _proc_arq_vlr_node2_post_vlr(struct osmo_fsm_inst *fi)
218{
219 struct proc_arq_priv *par = fi->priv;
220 struct vlr_subscr *vsub = par->vsub;
221
222 LOGPFSM(fi, "%s()\n", __func__);
223
224 if (!vsub->sub_dataconf_by_hlr_ind) {
225 /* Set User Error: Unidentified Subscriber */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200226 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200227 return;
228 }
Neels Hofmeyref9126c2017-07-18 15:38:39 +0200229 /* We don't feature location area specific blocking (yet). */
Harald Welteb8b85a12016-06-17 00:06:42 +0200230 if (0 /* roaming not allowed in LA */) {
231 /* Set User Error: Roaming not allowed in this LA */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200232 proc_arq_fsm_done(fi, GSM48_REJECT_ROAMING_NOT_ALLOWED);
Harald Welteb8b85a12016-06-17 00:06:42 +0200233 return;
234 }
235 vsub->imsi_detached_flag = false;
236 if (vsub->ms_not_reachable_flag) {
237 /* Start Subscriber_Present_VLR */
238 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_SUB_PRES, 0, 0);
Neels Hofmeyr1a5bcd52017-11-18 22:19:55 +0100239 sub_pres_vlr_fsm_start(&par->sub_pres_vlr_fsm, fi, vsub, PR_ARQ_E_PRES_RES);
Harald Welteb8b85a12016-06-17 00:06:42 +0200240 return;
241 }
242 _proc_arq_vlr_post_pres(fi);
243}
244
245static void _proc_arq_vlr_node2_post_ciph(struct osmo_fsm_inst *fi)
246{
247 struct proc_arq_priv *par = fi->priv;
248 struct vlr_subscr *vsub = par->vsub;
Harald Welte544a32f2020-06-21 22:15:53 +0200249 int rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200250
251 LOGPFSM(fi, "%s()\n", __func__);
252
Harald Welte544a32f2020-06-21 22:15:53 +0200253 rc = par->vlr->ops.tx_common_id(par->msc_conn_ref);
254 if (rc)
255 LOGPFSML(fi, LOGL_ERROR, "Error while sending Common ID (%d)\n", rc);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200256
Harald Welteb8b85a12016-06-17 00:06:42 +0200257 vsub->conf_by_radio_contact_ind = true;
258 if (vsub->loc_conf_in_hlr_ind == false) {
259 /* start Update_Location_Child_VLR. WE use
260 * Update_HLR_VLR instead, the differences appear
261 * insignificant for now. */
262 par->ul_child_fsm = upd_hlr_vlr_proc_start(fi, vsub,
263 PR_ARQ_E_UPD_LOC_RES);
264 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_UPD_LOC_CHILD, 0, 0);
265 return;
266 }
267 _proc_arq_vlr_node2_post_vlr(fi);
268}
269
Vadim Yanitskiy565ea2b2021-11-28 16:42:58 +0300270/* Determine if sending of CMC/SMC is required */
271static bool is_cmc_smc_required(struct proc_arq_priv *par)
Harald Welteb8b85a12016-06-17 00:06:42 +0200272{
Vadim Yanitskiy565ea2b2021-11-28 16:42:58 +0300273 /* UTRAN: always send SecModeCmd, even if ciphering is not required.
274 * GERAN: avoid sending CiphModeCmd if ciphering is not required. */
275 return par->is_utran || par->ciphering_required;
Harald Welteb8b85a12016-06-17 00:06:42 +0200276}
277
278static void _proc_arq_vlr_node2(struct osmo_fsm_inst *fi)
279{
280 struct proc_arq_priv *par = fi->priv;
281 struct vlr_subscr *vsub = par->vsub;
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100282 bool umts_aka;
Harald Welteb8b85a12016-06-17 00:06:42 +0200283
284 LOGPFSM(fi, "%s()\n", __func__);
285
Vadim Yanitskiy565ea2b2021-11-28 16:42:58 +0300286 if (!is_cmc_smc_required(par)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200287 _proc_arq_vlr_node2_post_ciph(fi);
288 return;
289 }
290
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100291 switch (vsub->sec_ctx) {
292 case VLR_SEC_CTX_GSM:
293 umts_aka = false;
294 break;
295 case VLR_SEC_CTX_UMTS:
296 umts_aka = true;
297 break;
298 default:
299 LOGPFSML(fi, LOGL_ERROR, "Cannot start ciphering, security context is not established\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200300 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100301 return;
302 }
303
Harald Welteb8b85a12016-06-17 00:06:42 +0200304 if (vlr_set_ciph_mode(vsub->vlr, fi, par->msc_conn_ref,
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100305 umts_aka,
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200306 vsub->vlr->cfg.retrieve_imeisv_ciphered)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200307 LOGPFSML(fi, LOGL_ERROR,
308 "Failed to send Ciphering Mode Command\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200309 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200310 return;
311 }
312
313 par->implicitly_accepted_parq_by_ciphering_cmd = true;
314 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_CIPH, 0, 0);
315}
316
317static bool is_auth_required(struct proc_arq_priv *par)
318{
319 /* The cases where the authentication procedure should be used
320 * are defined in 3GPP TS 33.102 */
321 /* For now we use a default value passed in to vlr_lu_fsm(). */
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100322 return par->authentication_required ||
323 (par->ciphering_required && !auth_try_reuse_tuple(par->vsub, par->key_seq));
Harald Welteb8b85a12016-06-17 00:06:42 +0200324}
325
326/* after the IMSI is known */
327static void proc_arq_vlr_fn_post_imsi(struct osmo_fsm_inst *fi)
328{
329 struct proc_arq_priv *par = fi->priv;
330 struct vlr_subscr *vsub = par->vsub;
331
332 LOGPFSM(fi, "%s()\n", __func__);
333
334 OSMO_ASSERT(vsub);
335
336 /* TODO: Identity IMEI -> System Failure */
337 if (is_auth_required(par)) {
338 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_AUTH,
339 0, 0);
Vadim Yanitskiy3daf0c22020-01-25 06:02:48 +0700340 vsub->auth_fsm = auth_fsm_start(vsub, fi,
Harald Welteb8b85a12016-06-17 00:06:42 +0200341 PR_ARQ_E_AUTH_RES,
342 par->is_r99,
343 par->is_utran);
344 } else {
345 _proc_arq_vlr_node2(fi);
346 }
347}
348
349static void proc_arq_vlr_fn_init(struct osmo_fsm_inst *fi,
350 uint32_t event, void *data)
351{
352 struct proc_arq_priv *par = fi->priv;
353 struct vlr_instance *vlr = par->vlr;
354 struct vlr_subscr *vsub = NULL;
355
356 OSMO_ASSERT(event == PR_ARQ_E_START);
357
358 /* Obtain_Identity_VLR */
359 if (!par->by_tmsi) {
360 /* IMSI was included */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100361 vsub = vlr_subscr_find_by_imsi(par->vlr, par->imsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200362 } else {
363 /* TMSI was included */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100364 vsub = vlr_subscr_find_by_tmsi(par->vlr, par->tmsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200365 }
366 if (vsub) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200367 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200368 if (vsub->proc_arq_fsm && fi != vsub->proc_arq_fsm) {
369 LOGPFSML(fi, LOGL_ERROR,
370 "Another proc_arq_fsm is already"
371 " associated with subscr %s,"
372 " terminating the other FSM.\n",
373 vlr_subscr_name(vsub));
374 proc_arq_fsm_done(vsub->proc_arq_fsm,
Neels Hofmeyr15809592018-04-06 02:57:51 +0200375 GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200376 }
377 vsub->proc_arq_fsm = fi;
Neels Hofmeyr1035d902018-12-28 21:22:32 +0100378 if (assoc_par_with_subscr(fi, vsub) != 0)
379 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
380 else
381 proc_arq_vlr_fn_post_imsi(fi);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100382 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200383 return;
384 }
385 /* No VSUB could be resolved. What now? */
386
387 if (!par->by_tmsi) {
388 /* We couldn't find a subscriber even by IMSI,
389 * Set User Error: Unidentified Subscriber */
Philipp Maier6f4752e2018-05-15 15:23:59 +0200390 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200391 return;
392 } else {
393 /* TMSI was included, are we permitted to use it? */
394 if (vlr->cfg.parq_retrieve_imsi) {
395 /* Obtain_IMSI_VLR */
396 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_OBTAIN_IMSI,
397 vlr_timer(vlr, 3270), 3270);
398 return;
399 } else {
400 /* Set User Error: Unidentified Subscriber */
Philipp Maier6f4752e2018-05-15 15:23:59 +0200401 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200402 return;
403 }
404 }
405}
406
407/* ID REQ(IMSI) has returned */
408static void proc_arq_vlr_fn_w_obt_imsi(struct osmo_fsm_inst *fi,
409 uint32_t event, void *data)
410{
411 struct proc_arq_priv *par = fi->priv;
412 struct vlr_instance *vlr = par->vlr;
413 struct vlr_subscr *vsub;
414
415 OSMO_ASSERT(event == PR_ARQ_E_ID_IMSI);
416
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100417 vsub = vlr_subscr_find_by_imsi(vlr, par->imsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200418 if (!vsub) {
419 /* Set User Error: Unidentified Subscriber */
Philipp Maier6f4752e2018-05-15 15:23:59 +0200420 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200421 return;
422 }
Neels Hofmeyr1035d902018-12-28 21:22:32 +0100423 if (assoc_par_with_subscr(fi, vsub))
424 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
425 else
426 proc_arq_vlr_fn_post_imsi(fi);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100427 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200428}
429
430/* Authenticate_VLR has completed */
431static void proc_arq_vlr_fn_w_auth(struct osmo_fsm_inst *fi,
432 uint32_t event, void *data)
433{
Neels Hofmeyr15809592018-04-06 02:57:51 +0200434 enum gsm48_reject_value *cause = data;
Harald Welteb8b85a12016-06-17 00:06:42 +0200435
436 OSMO_ASSERT(event == PR_ARQ_E_AUTH_RES);
437
Neels Hofmeyr15809592018-04-06 02:57:51 +0200438 if (!cause || *cause) {
439 proc_arq_fsm_done(fi, cause? *cause : GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200440 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200441 }
442
Neels Hofmeyr15809592018-04-06 02:57:51 +0200443 /* Node 2 */
444 _proc_arq_vlr_node2(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200445}
446
447static void proc_arq_vlr_fn_w_ciph(struct osmo_fsm_inst *fi,
448 uint32_t event, void *data)
449{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100450 enum vlr_ciph_result_cause result = VLR_CIPH_REJECT;
Harald Welteb8b85a12016-06-17 00:06:42 +0200451
452 OSMO_ASSERT(event == PR_ARQ_E_CIPH_RES);
453
454 if (!data)
455 LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: NULL\n");
456 else
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100457 result = *(enum vlr_ciph_result_cause*)data;
Harald Welteb8b85a12016-06-17 00:06:42 +0200458
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100459 switch (result) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200460 case VLR_CIPH_COMPL:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100461 _proc_arq_vlr_node2_post_ciph(fi);
462 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200463 case VLR_CIPH_REJECT:
464 LOGPFSM(fi, "ciphering rejected\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200465 proc_arq_fsm_done(fi, GSM48_REJECT_ILLEGAL_MS);
Harald Welteb8b85a12016-06-17 00:06:42 +0200466 return;
467 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100468 LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: %d\n", result);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200469 proc_arq_fsm_done(fi, GSM48_REJECT_ILLEGAL_MS);
Harald Welteb8b85a12016-06-17 00:06:42 +0200470 return;
471 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200472}
473
474/* Update_Location_Child_VLR has completed */
475static void proc_arq_vlr_fn_w_upd_loc(struct osmo_fsm_inst *fi,
476 uint32_t event, void *data)
477{
478 OSMO_ASSERT(event == PR_ARQ_E_UPD_LOC_RES);
479
480 _proc_arq_vlr_node2_post_vlr(fi);
481}
482
483/* Subscriber_Present_VLR has completed */
484static void proc_arq_vlr_fn_w_pres(struct osmo_fsm_inst *fi,
485 uint32_t event, void *data)
486{
487 OSMO_ASSERT(event == PR_ARQ_E_PRES_RES);
488
489 _proc_arq_vlr_post_pres(fi);
490}
491
492static void proc_arq_vlr_fn_w_trace(struct osmo_fsm_inst *fi,
493 uint32_t event, void *data)
494{
495 OSMO_ASSERT(event == PR_ARQ_E_TRACE_RES);
496
497 _proc_arq_vlr_post_trace(fi);
498}
499
500/* we have received the ID RESPONSE (IMEI) */
501static void proc_arq_vlr_fn_w_imei(struct osmo_fsm_inst *fi,
502 uint32_t event, void *data)
503{
504 OSMO_ASSERT(event == PR_ARQ_E_IMEI_RES);
505
506 _proc_arq_vlr_post_imei(fi);
507}
508
509/* MSC tells us that MS has acknowleded TMSI re-allocation */
510static void proc_arq_vlr_fn_w_tmsi(struct osmo_fsm_inst *fi,
511 uint32_t event, void *data)
512{
513 OSMO_ASSERT(event == PR_ARQ_E_TMSI_ACK);
514
515 /* FIXME: check confirmation? unfreeze? */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200516 proc_arq_fsm_done(fi, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200517}
518
519static const struct osmo_fsm_state proc_arq_vlr_states[] = {
520 [PR_ARQ_S_INIT] = {
521 .name = OSMO_STRINGIFY(PR_ARQ_S_INIT),
522 .in_event_mask = S(PR_ARQ_E_START),
523 .out_state_mask = S(PR_ARQ_S_DONE) |
524 S(PR_ARQ_S_WAIT_OBTAIN_IMSI) |
525 S(PR_ARQ_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100526 S(PR_ARQ_S_WAIT_CIPH) |
Harald Welteb8b85a12016-06-17 00:06:42 +0200527 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
528 S(PR_ARQ_S_WAIT_SUB_PRES) |
529 S(PR_ARQ_S_WAIT_TRACE_SUB) |
530 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
531 S(PR_ARQ_S_WAIT_TMSI_ACK),
532 .action = proc_arq_vlr_fn_init,
533 },
534 [PR_ARQ_S_WAIT_OBTAIN_IMSI] = {
535 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_OBTAIN_IMSI),
536 .in_event_mask = S(PR_ARQ_E_ID_IMSI),
537 .out_state_mask = S(PR_ARQ_S_DONE) |
538 S(PR_ARQ_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100539 S(PR_ARQ_S_WAIT_CIPH) |
Harald Welteb8b85a12016-06-17 00:06:42 +0200540 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
541 S(PR_ARQ_S_WAIT_SUB_PRES) |
542 S(PR_ARQ_S_WAIT_TRACE_SUB) |
543 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
544 S(PR_ARQ_S_WAIT_TMSI_ACK),
545 .action = proc_arq_vlr_fn_w_obt_imsi,
546 },
547 [PR_ARQ_S_WAIT_AUTH] = {
548 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_AUTH),
549 .in_event_mask = S(PR_ARQ_E_AUTH_RES),
550 .out_state_mask = S(PR_ARQ_S_DONE) |
551 S(PR_ARQ_S_WAIT_CIPH) |
552 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
553 S(PR_ARQ_S_WAIT_SUB_PRES) |
554 S(PR_ARQ_S_WAIT_TRACE_SUB) |
555 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
556 S(PR_ARQ_S_WAIT_TMSI_ACK),
557 .action = proc_arq_vlr_fn_w_auth,
558 },
559 [PR_ARQ_S_WAIT_CIPH] = {
560 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_CIPH),
561 .in_event_mask = S(PR_ARQ_E_CIPH_RES),
562 .out_state_mask = S(PR_ARQ_S_DONE) |
563 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
564 S(PR_ARQ_S_WAIT_SUB_PRES) |
565 S(PR_ARQ_S_WAIT_TRACE_SUB) |
566 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
567 S(PR_ARQ_S_WAIT_TMSI_ACK),
568 .action = proc_arq_vlr_fn_w_ciph,
569 },
570 [PR_ARQ_S_WAIT_UPD_LOC_CHILD] = {
571 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_UPD_LOC_CHILD),
572 .in_event_mask = S(PR_ARQ_E_UPD_LOC_RES),
573 .out_state_mask = S(PR_ARQ_S_DONE) |
574 S(PR_ARQ_S_WAIT_SUB_PRES) |
575 S(PR_ARQ_S_WAIT_TRACE_SUB) |
576 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
577 S(PR_ARQ_S_WAIT_TMSI_ACK),
578 .action = proc_arq_vlr_fn_w_upd_loc,
579 },
580 [PR_ARQ_S_WAIT_SUB_PRES] = {
581 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_SUB_PRES),
582 .in_event_mask = S(PR_ARQ_E_PRES_RES),
583 .out_state_mask = S(PR_ARQ_S_DONE) |
584 S(PR_ARQ_S_WAIT_TRACE_SUB) |
585 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
586 S(PR_ARQ_S_WAIT_TMSI_ACK),
587 .action = proc_arq_vlr_fn_w_pres,
588 },
589 [PR_ARQ_S_WAIT_TRACE_SUB] = {
590 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_TRACE_SUB),
591 .in_event_mask = S(PR_ARQ_E_TRACE_RES),
592 .out_state_mask = S(PR_ARQ_S_DONE) |
593 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
594 S(PR_ARQ_S_WAIT_TMSI_ACK),
595 .action = proc_arq_vlr_fn_w_trace,
596 },
597 [PR_ARQ_S_WAIT_CHECK_IMEI] = {
598 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_CHECK_IMEI),
599 .in_event_mask = S(PR_ARQ_E_IMEI_RES),
600 .out_state_mask = S(PR_ARQ_S_DONE) |
601 S(PR_ARQ_S_WAIT_TMSI_ACK),
602 .action = proc_arq_vlr_fn_w_imei,
603 },
604 [PR_ARQ_S_WAIT_TMSI_ACK] = {
605 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_TMSI_ACK),
606 .in_event_mask = S(PR_ARQ_E_TMSI_ACK),
607 .out_state_mask = S(PR_ARQ_S_DONE),
608 .action = proc_arq_vlr_fn_w_tmsi,
609 },
610 [PR_ARQ_S_DONE] = {
611 .name = OSMO_STRINGIFY(PR_ARQ_S_DONE),
612 .onenter = proc_arq_vlr_dispatch_result,
613 },
614};
615
616static struct osmo_fsm proc_arq_vlr_fsm = {
617 .name = "Process_Access_Request_VLR",
618 .states = proc_arq_vlr_states,
619 .num_states = ARRAY_SIZE(proc_arq_vlr_states),
620 .allstate_event_mask = 0,
621 .allstate_action = NULL,
622 .log_subsys = DVLR,
623 .event_names = proc_arq_vlr_event_names,
624 .cleanup = proc_arq_vlr_cleanup,
625};
626
627void
628vlr_proc_acc_req(struct osmo_fsm_inst *parent,
629 uint32_t parent_event_success,
630 uint32_t parent_event_failure,
631 void *parent_event_data,
632 struct vlr_instance *vlr, void *msc_conn_ref,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100633 enum vlr_parq_type type, enum osmo_cm_service_type cm_service_type,
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200634 const struct osmo_mobile_identity *mi,
Harald Welteb8b85a12016-06-17 00:06:42 +0200635 const struct osmo_location_area_id *lai,
636 bool authentication_required,
Harald Welte71c51df2017-12-23 18:51:48 +0100637 bool ciphering_required,
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100638 uint8_t key_seq,
Harald Welteb8b85a12016-06-17 00:06:42 +0200639 bool is_r99, bool is_utran)
640{
641 struct osmo_fsm_inst *fi;
642 struct proc_arq_priv *par;
Harald Welteb8b85a12016-06-17 00:06:42 +0200643
644 fi = osmo_fsm_inst_alloc_child(&proc_arq_vlr_fsm, parent,
645 parent_event_failure);
646 if (!fi)
647 return;
648
649 par = talloc_zero(fi, struct proc_arq_priv);
650 fi->priv = par;
651 par->vlr = vlr;
652 par->msc_conn_ref = msc_conn_ref;
653 par->type = type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100654 par->cm_service_type = cm_service_type;
Harald Welteb8b85a12016-06-17 00:06:42 +0200655 par->lai = *lai;
656 par->parent_event_success = parent_event_success;
657 par->parent_event_failure = parent_event_failure;
658 par->parent_event_data = parent_event_data;
659 par->authentication_required = authentication_required;
660 par->ciphering_required = ciphering_required;
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100661 par->key_seq = key_seq;
Harald Welteb8b85a12016-06-17 00:06:42 +0200662 par->is_r99 = is_r99;
663 par->is_utran = is_utran;
664
665 LOGPFSM(fi, "rev=%s net=%s%s%s\n",
666 is_r99 ? "R99" : "GSM",
667 is_utran ? "UTRAN" : "GERAN",
668 (authentication_required || ciphering_required)?
669 " Auth" : " (no Auth)",
670 (authentication_required || ciphering_required)?
671 (ciphering_required? "+Ciph" : " (no Ciph)")
672 : "");
673
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200674 if (is_utran && !authentication_required)
675 LOGPFSML(fi, LOGL_ERROR,
676 "Authentication off on UTRAN network. Good luck.\n");
677
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200678 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200679 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200680 OSMO_STRLCPY_ARRAY(par->imsi, mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200681 par->by_tmsi = false;
682 break;
683 case GSM_MI_TYPE_TMSI:
684 par->by_tmsi = true;
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200685 par->tmsi = mi->tmsi;
Harald Welteb8b85a12016-06-17 00:06:42 +0200686 break;
687 case GSM_MI_TYPE_IMEI:
688 /* TODO: IMEI (emergency call) */
689 default:
Philipp Maier6f4752e2018-05-15 15:23:59 +0200690 proc_arq_fsm_done(fi, GSM48_REJECT_INVALID_MANDANTORY_INF);
Harald Welteb8b85a12016-06-17 00:06:42 +0200691 return;
692 }
693
694 osmo_fsm_inst_dispatch(fi, PR_ARQ_E_START, NULL);
695}
696
697/* Gracefully terminate an FSM created by vlr_proc_acc_req() in case of
698 * external timeout (i.e. from MSC). */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200699void vlr_parq_cancel(struct osmo_fsm_inst *fi,
700 enum osmo_fsm_term_cause fsm_cause,
701 enum gsm48_reject_value gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200702{
703 if (!fi || fi->state == PR_ARQ_S_DONE)
704 return;
Neels Hofmeyr15809592018-04-06 02:57:51 +0200705 LOGPFSM(fi, "Cancel: %s\n", osmo_fsm_term_cause_name(fsm_cause));
706 proc_arq_fsm_done(fi, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200707}
708
709
710#if 0
711/***********************************************************************
712 * Update_Location_Child_VLR, TS 29.002 Chapter 25.4.4
713 ***********************************************************************/
714
715enum upd_loc_child_vlr_state {
716 ULC_S_IDLE,
717 ULC_S_WAIT_HLR_RESP,
718 ULC_S_DONE,
719};
720
721enum upd_loc_child_vlr_event {
722 ULC_E_START,
723};
724
725static const struct value_string upd_loc_child_vlr_event_names[] = {
726 { ULC_E_START, "START" },
727 { 0, NULL }
728};
729
730static void upd_loc_child_f_idle(struct osmo_fsm_inst *fi, uint32_t event,
731 void *data)
732{
733 OSMO_ASSERT(event == ULC_E_START);
734
735 /* send update location */
736}
737
738static void upd_loc_child_f_w_hlr(struct osmo_fsm_inst *fi, uint32_t event,
739 void *data)
740{
741}
742
743static const struct osmo_fsm_state upd_loc_child_vlr_states[] = {
744 [ULC_S_IDLE] = {
745 .in_event_mask = ,
746 .out_state_mask = S(ULC_S_WAIT_HLR_RESP) |
747 S(ULC_S_DONE),
748 .name = "IDLE",
749 .action = upd_loc_child_f_idle,
750 },
751 [ULC_S_WAIT_HLR_RESP] = {
752 .in_event_mask = ,
753 .out_state_mask = S(ULC_S_DONE),
754 .name = "WAIT-HLR-RESP",
755 .action = upd_loc_child_f_w_hlr,
756 },
757 [ULC_S_DONE] = {
758 .name = "DONE",
759 },
760};
761
762static struct osmo_fsm upd_loc_child_vlr_fsm = {
763 .name = "Update_Location_Child_VLR",
764 .states = upd_loc_child_vlr_states,
765 .num_states = ARRAY_SIZE(upd_loc_child_vlr_states),
766 .log_subsys = DVLR,
767 .event_names = upd_loc_child_vlr_event_names,
768};
769#endif
770
771void vlr_parq_fsm_init(void)
772{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100773 //OSMO_ASSERT(osmo_fsm_register(&upd_loc_child_vlr_fsm) == 0);
774 OSMO_ASSERT(osmo_fsm_register(&proc_arq_vlr_fsm) == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200775}