blob: b3660ff8b7124915497ccd55f714ca3a56d02f0d [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
270static bool is_ciph_required(struct proc_arq_priv *par)
271{
Harald Welte71c51df2017-12-23 18:51:48 +0100272 return par->ciphering_required;
Harald Welteb8b85a12016-06-17 00:06:42 +0200273}
274
275static void _proc_arq_vlr_node2(struct osmo_fsm_inst *fi)
276{
277 struct proc_arq_priv *par = fi->priv;
278 struct vlr_subscr *vsub = par->vsub;
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100279 bool umts_aka;
Harald Welteb8b85a12016-06-17 00:06:42 +0200280
281 LOGPFSM(fi, "%s()\n", __func__);
282
283 if (!is_ciph_required(par)) {
284 _proc_arq_vlr_node2_post_ciph(fi);
285 return;
286 }
287
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100288 switch (vsub->sec_ctx) {
289 case VLR_SEC_CTX_GSM:
290 umts_aka = false;
291 break;
292 case VLR_SEC_CTX_UMTS:
293 umts_aka = true;
294 break;
295 default:
296 LOGPFSML(fi, LOGL_ERROR, "Cannot start ciphering, security context is not established\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200297 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100298 return;
299 }
300
Harald Welteb8b85a12016-06-17 00:06:42 +0200301 if (vlr_set_ciph_mode(vsub->vlr, fi, par->msc_conn_ref,
302 par->ciphering_required,
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100303 umts_aka,
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200304 vsub->vlr->cfg.retrieve_imeisv_ciphered)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200305 LOGPFSML(fi, LOGL_ERROR,
306 "Failed to send Ciphering Mode Command\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200307 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200308 return;
309 }
310
311 par->implicitly_accepted_parq_by_ciphering_cmd = true;
312 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_CIPH, 0, 0);
313}
314
315static bool is_auth_required(struct proc_arq_priv *par)
316{
317 /* The cases where the authentication procedure should be used
318 * are defined in 3GPP TS 33.102 */
319 /* For now we use a default value passed in to vlr_lu_fsm(). */
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100320 return par->authentication_required ||
321 (par->ciphering_required && !auth_try_reuse_tuple(par->vsub, par->key_seq));
Harald Welteb8b85a12016-06-17 00:06:42 +0200322}
323
324/* after the IMSI is known */
325static void proc_arq_vlr_fn_post_imsi(struct osmo_fsm_inst *fi)
326{
327 struct proc_arq_priv *par = fi->priv;
328 struct vlr_subscr *vsub = par->vsub;
329
330 LOGPFSM(fi, "%s()\n", __func__);
331
332 OSMO_ASSERT(vsub);
333
334 /* TODO: Identity IMEI -> System Failure */
335 if (is_auth_required(par)) {
336 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_AUTH,
337 0, 0);
Vadim Yanitskiy3daf0c22020-01-25 06:02:48 +0700338 vsub->auth_fsm = auth_fsm_start(vsub, fi,
Harald Welteb8b85a12016-06-17 00:06:42 +0200339 PR_ARQ_E_AUTH_RES,
340 par->is_r99,
341 par->is_utran);
342 } else {
343 _proc_arq_vlr_node2(fi);
344 }
345}
346
347static void proc_arq_vlr_fn_init(struct osmo_fsm_inst *fi,
348 uint32_t event, void *data)
349{
350 struct proc_arq_priv *par = fi->priv;
351 struct vlr_instance *vlr = par->vlr;
352 struct vlr_subscr *vsub = NULL;
353
354 OSMO_ASSERT(event == PR_ARQ_E_START);
355
356 /* Obtain_Identity_VLR */
357 if (!par->by_tmsi) {
358 /* IMSI was included */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100359 vsub = vlr_subscr_find_by_imsi(par->vlr, par->imsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200360 } else {
361 /* TMSI was included */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100362 vsub = vlr_subscr_find_by_tmsi(par->vlr, par->tmsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200363 }
364 if (vsub) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200365 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200366 if (vsub->proc_arq_fsm && fi != vsub->proc_arq_fsm) {
367 LOGPFSML(fi, LOGL_ERROR,
368 "Another proc_arq_fsm is already"
369 " associated with subscr %s,"
370 " terminating the other FSM.\n",
371 vlr_subscr_name(vsub));
372 proc_arq_fsm_done(vsub->proc_arq_fsm,
Neels Hofmeyr15809592018-04-06 02:57:51 +0200373 GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200374 }
375 vsub->proc_arq_fsm = fi;
Neels Hofmeyr1035d902018-12-28 21:22:32 +0100376 if (assoc_par_with_subscr(fi, vsub) != 0)
377 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
378 else
379 proc_arq_vlr_fn_post_imsi(fi);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100380 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200381 return;
382 }
383 /* No VSUB could be resolved. What now? */
384
385 if (!par->by_tmsi) {
386 /* We couldn't find a subscriber even by IMSI,
387 * Set User Error: Unidentified Subscriber */
Philipp Maier6f4752e2018-05-15 15:23:59 +0200388 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200389 return;
390 } else {
391 /* TMSI was included, are we permitted to use it? */
392 if (vlr->cfg.parq_retrieve_imsi) {
393 /* Obtain_IMSI_VLR */
394 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_OBTAIN_IMSI,
395 vlr_timer(vlr, 3270), 3270);
396 return;
397 } else {
398 /* Set User Error: Unidentified Subscriber */
Philipp Maier6f4752e2018-05-15 15:23:59 +0200399 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200400 return;
401 }
402 }
403}
404
405/* ID REQ(IMSI) has returned */
406static void proc_arq_vlr_fn_w_obt_imsi(struct osmo_fsm_inst *fi,
407 uint32_t event, void *data)
408{
409 struct proc_arq_priv *par = fi->priv;
410 struct vlr_instance *vlr = par->vlr;
411 struct vlr_subscr *vsub;
412
413 OSMO_ASSERT(event == PR_ARQ_E_ID_IMSI);
414
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100415 vsub = vlr_subscr_find_by_imsi(vlr, par->imsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200416 if (!vsub) {
417 /* Set User Error: Unidentified Subscriber */
Philipp Maier6f4752e2018-05-15 15:23:59 +0200418 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200419 return;
420 }
Neels Hofmeyr1035d902018-12-28 21:22:32 +0100421 if (assoc_par_with_subscr(fi, vsub))
422 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
423 else
424 proc_arq_vlr_fn_post_imsi(fi);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100425 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200426}
427
428/* Authenticate_VLR has completed */
429static void proc_arq_vlr_fn_w_auth(struct osmo_fsm_inst *fi,
430 uint32_t event, void *data)
431{
Neels Hofmeyr15809592018-04-06 02:57:51 +0200432 enum gsm48_reject_value *cause = data;
Harald Welteb8b85a12016-06-17 00:06:42 +0200433
434 OSMO_ASSERT(event == PR_ARQ_E_AUTH_RES);
435
Neels Hofmeyr15809592018-04-06 02:57:51 +0200436 if (!cause || *cause) {
437 proc_arq_fsm_done(fi, cause? *cause : GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200438 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200439 }
440
Neels Hofmeyr15809592018-04-06 02:57:51 +0200441 /* Node 2 */
442 _proc_arq_vlr_node2(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200443}
444
445static void proc_arq_vlr_fn_w_ciph(struct osmo_fsm_inst *fi,
446 uint32_t event, void *data)
447{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100448 enum vlr_ciph_result_cause result = VLR_CIPH_REJECT;
Harald Welteb8b85a12016-06-17 00:06:42 +0200449
450 OSMO_ASSERT(event == PR_ARQ_E_CIPH_RES);
451
452 if (!data)
453 LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: NULL\n");
454 else
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100455 result = *(enum vlr_ciph_result_cause*)data;
Harald Welteb8b85a12016-06-17 00:06:42 +0200456
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100457 switch (result) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200458 case VLR_CIPH_COMPL:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100459 _proc_arq_vlr_node2_post_ciph(fi);
460 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200461 case VLR_CIPH_REJECT:
462 LOGPFSM(fi, "ciphering rejected\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200463 proc_arq_fsm_done(fi, GSM48_REJECT_ILLEGAL_MS);
Harald Welteb8b85a12016-06-17 00:06:42 +0200464 return;
465 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100466 LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: %d\n", result);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200467 proc_arq_fsm_done(fi, GSM48_REJECT_ILLEGAL_MS);
Harald Welteb8b85a12016-06-17 00:06:42 +0200468 return;
469 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200470}
471
472/* Update_Location_Child_VLR has completed */
473static void proc_arq_vlr_fn_w_upd_loc(struct osmo_fsm_inst *fi,
474 uint32_t event, void *data)
475{
476 OSMO_ASSERT(event == PR_ARQ_E_UPD_LOC_RES);
477
478 _proc_arq_vlr_node2_post_vlr(fi);
479}
480
481/* Subscriber_Present_VLR has completed */
482static void proc_arq_vlr_fn_w_pres(struct osmo_fsm_inst *fi,
483 uint32_t event, void *data)
484{
485 OSMO_ASSERT(event == PR_ARQ_E_PRES_RES);
486
487 _proc_arq_vlr_post_pres(fi);
488}
489
490static void proc_arq_vlr_fn_w_trace(struct osmo_fsm_inst *fi,
491 uint32_t event, void *data)
492{
493 OSMO_ASSERT(event == PR_ARQ_E_TRACE_RES);
494
495 _proc_arq_vlr_post_trace(fi);
496}
497
498/* we have received the ID RESPONSE (IMEI) */
499static void proc_arq_vlr_fn_w_imei(struct osmo_fsm_inst *fi,
500 uint32_t event, void *data)
501{
502 OSMO_ASSERT(event == PR_ARQ_E_IMEI_RES);
503
504 _proc_arq_vlr_post_imei(fi);
505}
506
507/* MSC tells us that MS has acknowleded TMSI re-allocation */
508static void proc_arq_vlr_fn_w_tmsi(struct osmo_fsm_inst *fi,
509 uint32_t event, void *data)
510{
511 OSMO_ASSERT(event == PR_ARQ_E_TMSI_ACK);
512
513 /* FIXME: check confirmation? unfreeze? */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200514 proc_arq_fsm_done(fi, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200515}
516
517static const struct osmo_fsm_state proc_arq_vlr_states[] = {
518 [PR_ARQ_S_INIT] = {
519 .name = OSMO_STRINGIFY(PR_ARQ_S_INIT),
520 .in_event_mask = S(PR_ARQ_E_START),
521 .out_state_mask = S(PR_ARQ_S_DONE) |
522 S(PR_ARQ_S_WAIT_OBTAIN_IMSI) |
523 S(PR_ARQ_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100524 S(PR_ARQ_S_WAIT_CIPH) |
Harald Welteb8b85a12016-06-17 00:06:42 +0200525 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
526 S(PR_ARQ_S_WAIT_SUB_PRES) |
527 S(PR_ARQ_S_WAIT_TRACE_SUB) |
528 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
529 S(PR_ARQ_S_WAIT_TMSI_ACK),
530 .action = proc_arq_vlr_fn_init,
531 },
532 [PR_ARQ_S_WAIT_OBTAIN_IMSI] = {
533 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_OBTAIN_IMSI),
534 .in_event_mask = S(PR_ARQ_E_ID_IMSI),
535 .out_state_mask = S(PR_ARQ_S_DONE) |
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_w_obt_imsi,
544 },
545 [PR_ARQ_S_WAIT_AUTH] = {
546 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_AUTH),
547 .in_event_mask = S(PR_ARQ_E_AUTH_RES),
548 .out_state_mask = S(PR_ARQ_S_DONE) |
549 S(PR_ARQ_S_WAIT_CIPH) |
550 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
551 S(PR_ARQ_S_WAIT_SUB_PRES) |
552 S(PR_ARQ_S_WAIT_TRACE_SUB) |
553 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
554 S(PR_ARQ_S_WAIT_TMSI_ACK),
555 .action = proc_arq_vlr_fn_w_auth,
556 },
557 [PR_ARQ_S_WAIT_CIPH] = {
558 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_CIPH),
559 .in_event_mask = S(PR_ARQ_E_CIPH_RES),
560 .out_state_mask = S(PR_ARQ_S_DONE) |
561 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
562 S(PR_ARQ_S_WAIT_SUB_PRES) |
563 S(PR_ARQ_S_WAIT_TRACE_SUB) |
564 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
565 S(PR_ARQ_S_WAIT_TMSI_ACK),
566 .action = proc_arq_vlr_fn_w_ciph,
567 },
568 [PR_ARQ_S_WAIT_UPD_LOC_CHILD] = {
569 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_UPD_LOC_CHILD),
570 .in_event_mask = S(PR_ARQ_E_UPD_LOC_RES),
571 .out_state_mask = S(PR_ARQ_S_DONE) |
572 S(PR_ARQ_S_WAIT_SUB_PRES) |
573 S(PR_ARQ_S_WAIT_TRACE_SUB) |
574 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
575 S(PR_ARQ_S_WAIT_TMSI_ACK),
576 .action = proc_arq_vlr_fn_w_upd_loc,
577 },
578 [PR_ARQ_S_WAIT_SUB_PRES] = {
579 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_SUB_PRES),
580 .in_event_mask = S(PR_ARQ_E_PRES_RES),
581 .out_state_mask = S(PR_ARQ_S_DONE) |
582 S(PR_ARQ_S_WAIT_TRACE_SUB) |
583 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
584 S(PR_ARQ_S_WAIT_TMSI_ACK),
585 .action = proc_arq_vlr_fn_w_pres,
586 },
587 [PR_ARQ_S_WAIT_TRACE_SUB] = {
588 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_TRACE_SUB),
589 .in_event_mask = S(PR_ARQ_E_TRACE_RES),
590 .out_state_mask = S(PR_ARQ_S_DONE) |
591 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
592 S(PR_ARQ_S_WAIT_TMSI_ACK),
593 .action = proc_arq_vlr_fn_w_trace,
594 },
595 [PR_ARQ_S_WAIT_CHECK_IMEI] = {
596 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_CHECK_IMEI),
597 .in_event_mask = S(PR_ARQ_E_IMEI_RES),
598 .out_state_mask = S(PR_ARQ_S_DONE) |
599 S(PR_ARQ_S_WAIT_TMSI_ACK),
600 .action = proc_arq_vlr_fn_w_imei,
601 },
602 [PR_ARQ_S_WAIT_TMSI_ACK] = {
603 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_TMSI_ACK),
604 .in_event_mask = S(PR_ARQ_E_TMSI_ACK),
605 .out_state_mask = S(PR_ARQ_S_DONE),
606 .action = proc_arq_vlr_fn_w_tmsi,
607 },
608 [PR_ARQ_S_DONE] = {
609 .name = OSMO_STRINGIFY(PR_ARQ_S_DONE),
610 .onenter = proc_arq_vlr_dispatch_result,
611 },
612};
613
614static struct osmo_fsm proc_arq_vlr_fsm = {
615 .name = "Process_Access_Request_VLR",
616 .states = proc_arq_vlr_states,
617 .num_states = ARRAY_SIZE(proc_arq_vlr_states),
618 .allstate_event_mask = 0,
619 .allstate_action = NULL,
620 .log_subsys = DVLR,
621 .event_names = proc_arq_vlr_event_names,
622 .cleanup = proc_arq_vlr_cleanup,
623};
624
625void
626vlr_proc_acc_req(struct osmo_fsm_inst *parent,
627 uint32_t parent_event_success,
628 uint32_t parent_event_failure,
629 void *parent_event_data,
630 struct vlr_instance *vlr, void *msc_conn_ref,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100631 enum vlr_parq_type type, enum osmo_cm_service_type cm_service_type,
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200632 const struct osmo_mobile_identity *mi,
Harald Welteb8b85a12016-06-17 00:06:42 +0200633 const struct osmo_location_area_id *lai,
634 bool authentication_required,
Harald Welte71c51df2017-12-23 18:51:48 +0100635 bool ciphering_required,
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100636 uint8_t key_seq,
Harald Welteb8b85a12016-06-17 00:06:42 +0200637 bool is_r99, bool is_utran)
638{
639 struct osmo_fsm_inst *fi;
640 struct proc_arq_priv *par;
Harald Welteb8b85a12016-06-17 00:06:42 +0200641
642 fi = osmo_fsm_inst_alloc_child(&proc_arq_vlr_fsm, parent,
643 parent_event_failure);
644 if (!fi)
645 return;
646
647 par = talloc_zero(fi, struct proc_arq_priv);
648 fi->priv = par;
649 par->vlr = vlr;
650 par->msc_conn_ref = msc_conn_ref;
651 par->type = type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100652 par->cm_service_type = cm_service_type;
Harald Welteb8b85a12016-06-17 00:06:42 +0200653 par->lai = *lai;
654 par->parent_event_success = parent_event_success;
655 par->parent_event_failure = parent_event_failure;
656 par->parent_event_data = parent_event_data;
657 par->authentication_required = authentication_required;
658 par->ciphering_required = ciphering_required;
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100659 par->key_seq = key_seq;
Harald Welteb8b85a12016-06-17 00:06:42 +0200660 par->is_r99 = is_r99;
661 par->is_utran = is_utran;
662
663 LOGPFSM(fi, "rev=%s net=%s%s%s\n",
664 is_r99 ? "R99" : "GSM",
665 is_utran ? "UTRAN" : "GERAN",
666 (authentication_required || ciphering_required)?
667 " Auth" : " (no Auth)",
668 (authentication_required || ciphering_required)?
669 (ciphering_required? "+Ciph" : " (no Ciph)")
670 : "");
671
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200672 if (is_utran && !authentication_required)
673 LOGPFSML(fi, LOGL_ERROR,
674 "Authentication off on UTRAN network. Good luck.\n");
675
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200676 switch (mi->type) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200677 case GSM_MI_TYPE_IMSI:
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200678 OSMO_STRLCPY_ARRAY(par->imsi, mi->imsi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200679 par->by_tmsi = false;
680 break;
681 case GSM_MI_TYPE_TMSI:
682 par->by_tmsi = true;
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200683 par->tmsi = mi->tmsi;
Harald Welteb8b85a12016-06-17 00:06:42 +0200684 break;
685 case GSM_MI_TYPE_IMEI:
686 /* TODO: IMEI (emergency call) */
687 default:
Philipp Maier6f4752e2018-05-15 15:23:59 +0200688 proc_arq_fsm_done(fi, GSM48_REJECT_INVALID_MANDANTORY_INF);
Harald Welteb8b85a12016-06-17 00:06:42 +0200689 return;
690 }
691
692 osmo_fsm_inst_dispatch(fi, PR_ARQ_E_START, NULL);
693}
694
695/* Gracefully terminate an FSM created by vlr_proc_acc_req() in case of
696 * external timeout (i.e. from MSC). */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200697void vlr_parq_cancel(struct osmo_fsm_inst *fi,
698 enum osmo_fsm_term_cause fsm_cause,
699 enum gsm48_reject_value gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200700{
701 if (!fi || fi->state == PR_ARQ_S_DONE)
702 return;
Neels Hofmeyr15809592018-04-06 02:57:51 +0200703 LOGPFSM(fi, "Cancel: %s\n", osmo_fsm_term_cause_name(fsm_cause));
704 proc_arq_fsm_done(fi, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200705}
706
707
708#if 0
709/***********************************************************************
710 * Update_Location_Child_VLR, TS 29.002 Chapter 25.4.4
711 ***********************************************************************/
712
713enum upd_loc_child_vlr_state {
714 ULC_S_IDLE,
715 ULC_S_WAIT_HLR_RESP,
716 ULC_S_DONE,
717};
718
719enum upd_loc_child_vlr_event {
720 ULC_E_START,
721};
722
723static const struct value_string upd_loc_child_vlr_event_names[] = {
724 { ULC_E_START, "START" },
725 { 0, NULL }
726};
727
728static void upd_loc_child_f_idle(struct osmo_fsm_inst *fi, uint32_t event,
729 void *data)
730{
731 OSMO_ASSERT(event == ULC_E_START);
732
733 /* send update location */
734}
735
736static void upd_loc_child_f_w_hlr(struct osmo_fsm_inst *fi, uint32_t event,
737 void *data)
738{
739}
740
741static const struct osmo_fsm_state upd_loc_child_vlr_states[] = {
742 [ULC_S_IDLE] = {
743 .in_event_mask = ,
744 .out_state_mask = S(ULC_S_WAIT_HLR_RESP) |
745 S(ULC_S_DONE),
746 .name = "IDLE",
747 .action = upd_loc_child_f_idle,
748 },
749 [ULC_S_WAIT_HLR_RESP] = {
750 .in_event_mask = ,
751 .out_state_mask = S(ULC_S_DONE),
752 .name = "WAIT-HLR-RESP",
753 .action = upd_loc_child_f_w_hlr,
754 },
755 [ULC_S_DONE] = {
756 .name = "DONE",
757 },
758};
759
760static struct osmo_fsm upd_loc_child_vlr_fsm = {
761 .name = "Update_Location_Child_VLR",
762 .states = upd_loc_child_vlr_states,
763 .num_states = ARRAY_SIZE(upd_loc_child_vlr_states),
764 .log_subsys = DVLR,
765 .event_names = upd_loc_child_vlr_event_names,
766};
767#endif
768
769void vlr_parq_fsm_init(void)
770{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100771 //OSMO_ASSERT(osmo_fsm_register(&upd_loc_child_vlr_fsm) == 0);
772 OSMO_ASSERT(osmo_fsm_register(&proc_arq_vlr_fsm) == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200773}