blob: 7684d02f0a7638378ba39e82284fd98df46be6db [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;
249
250 LOGPFSM(fi, "%s()\n", __func__);
251
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200252 if (par->is_utran) {
253 int rc;
254 rc = par->vlr->ops.tx_common_id(par->msc_conn_ref);
255 if (rc)
256 LOGPFSML(fi, LOGL_ERROR,
257 "Error while sending Common ID (%d)\n", rc);
258 }
259
Harald Welteb8b85a12016-06-17 00:06:42 +0200260 vsub->conf_by_radio_contact_ind = true;
261 if (vsub->loc_conf_in_hlr_ind == false) {
262 /* start Update_Location_Child_VLR. WE use
263 * Update_HLR_VLR instead, the differences appear
264 * insignificant for now. */
265 par->ul_child_fsm = upd_hlr_vlr_proc_start(fi, vsub,
266 PR_ARQ_E_UPD_LOC_RES);
267 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_UPD_LOC_CHILD, 0, 0);
268 return;
269 }
270 _proc_arq_vlr_node2_post_vlr(fi);
271}
272
273static bool is_ciph_required(struct proc_arq_priv *par)
274{
Harald Welte71c51df2017-12-23 18:51:48 +0100275 return 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
286 if (!is_ciph_required(par)) {
287 _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,
305 par->ciphering_required,
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100306 umts_aka,
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200307 vsub->vlr->cfg.retrieve_imeisv_ciphered)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200308 LOGPFSML(fi, LOGL_ERROR,
309 "Failed to send Ciphering Mode Command\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200310 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200311 return;
312 }
313
314 par->implicitly_accepted_parq_by_ciphering_cmd = true;
315 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_CIPH, 0, 0);
316}
317
318static bool is_auth_required(struct proc_arq_priv *par)
319{
320 /* The cases where the authentication procedure should be used
321 * are defined in 3GPP TS 33.102 */
322 /* For now we use a default value passed in to vlr_lu_fsm(). */
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100323 return par->authentication_required ||
324 (par->ciphering_required && !auth_try_reuse_tuple(par->vsub, par->key_seq));
Harald Welteb8b85a12016-06-17 00:06:42 +0200325}
326
327/* after the IMSI is known */
328static void proc_arq_vlr_fn_post_imsi(struct osmo_fsm_inst *fi)
329{
330 struct proc_arq_priv *par = fi->priv;
331 struct vlr_subscr *vsub = par->vsub;
332
333 LOGPFSM(fi, "%s()\n", __func__);
334
335 OSMO_ASSERT(vsub);
336
337 /* TODO: Identity IMEI -> System Failure */
338 if (is_auth_required(par)) {
339 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_AUTH,
340 0, 0);
341 vsub->auth_fsm = auth_fsm_start(vsub, fi->log_level, fi,
342 PR_ARQ_E_AUTH_RES,
343 par->is_r99,
344 par->is_utran);
345 } else {
346 _proc_arq_vlr_node2(fi);
347 }
348}
349
350static void proc_arq_vlr_fn_init(struct osmo_fsm_inst *fi,
351 uint32_t event, void *data)
352{
353 struct proc_arq_priv *par = fi->priv;
354 struct vlr_instance *vlr = par->vlr;
355 struct vlr_subscr *vsub = NULL;
356
357 OSMO_ASSERT(event == PR_ARQ_E_START);
358
359 /* Obtain_Identity_VLR */
360 if (!par->by_tmsi) {
361 /* IMSI was included */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100362 vsub = vlr_subscr_find_by_imsi(par->vlr, par->imsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200363 } else {
364 /* TMSI was included */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100365 vsub = vlr_subscr_find_by_tmsi(par->vlr, par->tmsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200366 }
367 if (vsub) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200368 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200369 if (vsub->proc_arq_fsm && fi != vsub->proc_arq_fsm) {
370 LOGPFSML(fi, LOGL_ERROR,
371 "Another proc_arq_fsm is already"
372 " associated with subscr %s,"
373 " terminating the other FSM.\n",
374 vlr_subscr_name(vsub));
375 proc_arq_fsm_done(vsub->proc_arq_fsm,
Neels Hofmeyr15809592018-04-06 02:57:51 +0200376 GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200377 }
378 vsub->proc_arq_fsm = fi;
Neels Hofmeyr1035d902018-12-28 21:22:32 +0100379 if (assoc_par_with_subscr(fi, vsub) != 0)
380 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
381 else
382 proc_arq_vlr_fn_post_imsi(fi);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100383 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200384 return;
385 }
386 /* No VSUB could be resolved. What now? */
387
388 if (!par->by_tmsi) {
389 /* We couldn't find a subscriber even by IMSI,
390 * Set User Error: Unidentified Subscriber */
Philipp Maier6f4752e2018-05-15 15:23:59 +0200391 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200392 return;
393 } else {
394 /* TMSI was included, are we permitted to use it? */
395 if (vlr->cfg.parq_retrieve_imsi) {
396 /* Obtain_IMSI_VLR */
397 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_OBTAIN_IMSI,
398 vlr_timer(vlr, 3270), 3270);
399 return;
400 } else {
401 /* Set User Error: Unidentified Subscriber */
Philipp Maier6f4752e2018-05-15 15:23:59 +0200402 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200403 return;
404 }
405 }
406}
407
408/* ID REQ(IMSI) has returned */
409static void proc_arq_vlr_fn_w_obt_imsi(struct osmo_fsm_inst *fi,
410 uint32_t event, void *data)
411{
412 struct proc_arq_priv *par = fi->priv;
413 struct vlr_instance *vlr = par->vlr;
414 struct vlr_subscr *vsub;
415
416 OSMO_ASSERT(event == PR_ARQ_E_ID_IMSI);
417
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100418 vsub = vlr_subscr_find_by_imsi(vlr, par->imsi, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200419 if (!vsub) {
420 /* Set User Error: Unidentified Subscriber */
Philipp Maier6f4752e2018-05-15 15:23:59 +0200421 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200422 return;
423 }
Neels Hofmeyr1035d902018-12-28 21:22:32 +0100424 if (assoc_par_with_subscr(fi, vsub))
425 proc_arq_fsm_done(fi, GSM48_REJECT_NETWORK_FAILURE);
426 else
427 proc_arq_vlr_fn_post_imsi(fi);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100428 vlr_subscr_put(vsub, __func__);
Harald Welteb8b85a12016-06-17 00:06:42 +0200429}
430
431/* Authenticate_VLR has completed */
432static void proc_arq_vlr_fn_w_auth(struct osmo_fsm_inst *fi,
433 uint32_t event, void *data)
434{
Neels Hofmeyr15809592018-04-06 02:57:51 +0200435 enum gsm48_reject_value *cause = data;
Harald Welteb8b85a12016-06-17 00:06:42 +0200436
437 OSMO_ASSERT(event == PR_ARQ_E_AUTH_RES);
438
Neels Hofmeyr15809592018-04-06 02:57:51 +0200439 if (!cause || *cause) {
440 proc_arq_fsm_done(fi, cause? *cause : GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200441 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200442 }
443
Neels Hofmeyr15809592018-04-06 02:57:51 +0200444 /* Node 2 */
445 _proc_arq_vlr_node2(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200446}
447
448static void proc_arq_vlr_fn_w_ciph(struct osmo_fsm_inst *fi,
449 uint32_t event, void *data)
450{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100451 enum vlr_ciph_result_cause result = VLR_CIPH_REJECT;
Harald Welteb8b85a12016-06-17 00:06:42 +0200452
453 OSMO_ASSERT(event == PR_ARQ_E_CIPH_RES);
454
455 if (!data)
456 LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: NULL\n");
457 else
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100458 result = *(enum vlr_ciph_result_cause*)data;
Harald Welteb8b85a12016-06-17 00:06:42 +0200459
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100460 switch (result) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200461 case VLR_CIPH_COMPL:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100462 _proc_arq_vlr_node2_post_ciph(fi);
463 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200464 case VLR_CIPH_REJECT:
465 LOGPFSM(fi, "ciphering rejected\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200466 proc_arq_fsm_done(fi, GSM48_REJECT_ILLEGAL_MS);
Harald Welteb8b85a12016-06-17 00:06:42 +0200467 return;
468 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100469 LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: %d\n", result);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200470 proc_arq_fsm_done(fi, GSM48_REJECT_ILLEGAL_MS);
Harald Welteb8b85a12016-06-17 00:06:42 +0200471 return;
472 }
Harald Welteb8b85a12016-06-17 00:06:42 +0200473}
474
475/* Update_Location_Child_VLR has completed */
476static void proc_arq_vlr_fn_w_upd_loc(struct osmo_fsm_inst *fi,
477 uint32_t event, void *data)
478{
479 OSMO_ASSERT(event == PR_ARQ_E_UPD_LOC_RES);
480
481 _proc_arq_vlr_node2_post_vlr(fi);
482}
483
484/* Subscriber_Present_VLR has completed */
485static void proc_arq_vlr_fn_w_pres(struct osmo_fsm_inst *fi,
486 uint32_t event, void *data)
487{
488 OSMO_ASSERT(event == PR_ARQ_E_PRES_RES);
489
490 _proc_arq_vlr_post_pres(fi);
491}
492
493static void proc_arq_vlr_fn_w_trace(struct osmo_fsm_inst *fi,
494 uint32_t event, void *data)
495{
496 OSMO_ASSERT(event == PR_ARQ_E_TRACE_RES);
497
498 _proc_arq_vlr_post_trace(fi);
499}
500
501/* we have received the ID RESPONSE (IMEI) */
502static void proc_arq_vlr_fn_w_imei(struct osmo_fsm_inst *fi,
503 uint32_t event, void *data)
504{
505 OSMO_ASSERT(event == PR_ARQ_E_IMEI_RES);
506
507 _proc_arq_vlr_post_imei(fi);
508}
509
510/* MSC tells us that MS has acknowleded TMSI re-allocation */
511static void proc_arq_vlr_fn_w_tmsi(struct osmo_fsm_inst *fi,
512 uint32_t event, void *data)
513{
514 OSMO_ASSERT(event == PR_ARQ_E_TMSI_ACK);
515
516 /* FIXME: check confirmation? unfreeze? */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200517 proc_arq_fsm_done(fi, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200518}
519
520static const struct osmo_fsm_state proc_arq_vlr_states[] = {
521 [PR_ARQ_S_INIT] = {
522 .name = OSMO_STRINGIFY(PR_ARQ_S_INIT),
523 .in_event_mask = S(PR_ARQ_E_START),
524 .out_state_mask = S(PR_ARQ_S_DONE) |
525 S(PR_ARQ_S_WAIT_OBTAIN_IMSI) |
526 S(PR_ARQ_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100527 S(PR_ARQ_S_WAIT_CIPH) |
Harald Welteb8b85a12016-06-17 00:06:42 +0200528 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
529 S(PR_ARQ_S_WAIT_SUB_PRES) |
530 S(PR_ARQ_S_WAIT_TRACE_SUB) |
531 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
532 S(PR_ARQ_S_WAIT_TMSI_ACK),
533 .action = proc_arq_vlr_fn_init,
534 },
535 [PR_ARQ_S_WAIT_OBTAIN_IMSI] = {
536 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_OBTAIN_IMSI),
537 .in_event_mask = S(PR_ARQ_E_ID_IMSI),
538 .out_state_mask = S(PR_ARQ_S_DONE) |
539 S(PR_ARQ_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100540 S(PR_ARQ_S_WAIT_CIPH) |
Harald Welteb8b85a12016-06-17 00:06:42 +0200541 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
542 S(PR_ARQ_S_WAIT_SUB_PRES) |
543 S(PR_ARQ_S_WAIT_TRACE_SUB) |
544 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
545 S(PR_ARQ_S_WAIT_TMSI_ACK),
546 .action = proc_arq_vlr_fn_w_obt_imsi,
547 },
548 [PR_ARQ_S_WAIT_AUTH] = {
549 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_AUTH),
550 .in_event_mask = S(PR_ARQ_E_AUTH_RES),
551 .out_state_mask = S(PR_ARQ_S_DONE) |
552 S(PR_ARQ_S_WAIT_CIPH) |
553 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
554 S(PR_ARQ_S_WAIT_SUB_PRES) |
555 S(PR_ARQ_S_WAIT_TRACE_SUB) |
556 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
557 S(PR_ARQ_S_WAIT_TMSI_ACK),
558 .action = proc_arq_vlr_fn_w_auth,
559 },
560 [PR_ARQ_S_WAIT_CIPH] = {
561 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_CIPH),
562 .in_event_mask = S(PR_ARQ_E_CIPH_RES),
563 .out_state_mask = S(PR_ARQ_S_DONE) |
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_ciph,
570 },
571 [PR_ARQ_S_WAIT_UPD_LOC_CHILD] = {
572 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_UPD_LOC_CHILD),
573 .in_event_mask = S(PR_ARQ_E_UPD_LOC_RES),
574 .out_state_mask = S(PR_ARQ_S_DONE) |
575 S(PR_ARQ_S_WAIT_SUB_PRES) |
576 S(PR_ARQ_S_WAIT_TRACE_SUB) |
577 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
578 S(PR_ARQ_S_WAIT_TMSI_ACK),
579 .action = proc_arq_vlr_fn_w_upd_loc,
580 },
581 [PR_ARQ_S_WAIT_SUB_PRES] = {
582 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_SUB_PRES),
583 .in_event_mask = S(PR_ARQ_E_PRES_RES),
584 .out_state_mask = S(PR_ARQ_S_DONE) |
585 S(PR_ARQ_S_WAIT_TRACE_SUB) |
586 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
587 S(PR_ARQ_S_WAIT_TMSI_ACK),
588 .action = proc_arq_vlr_fn_w_pres,
589 },
590 [PR_ARQ_S_WAIT_TRACE_SUB] = {
591 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_TRACE_SUB),
592 .in_event_mask = S(PR_ARQ_E_TRACE_RES),
593 .out_state_mask = S(PR_ARQ_S_DONE) |
594 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
595 S(PR_ARQ_S_WAIT_TMSI_ACK),
596 .action = proc_arq_vlr_fn_w_trace,
597 },
598 [PR_ARQ_S_WAIT_CHECK_IMEI] = {
599 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_CHECK_IMEI),
600 .in_event_mask = S(PR_ARQ_E_IMEI_RES),
601 .out_state_mask = S(PR_ARQ_S_DONE) |
602 S(PR_ARQ_S_WAIT_TMSI_ACK),
603 .action = proc_arq_vlr_fn_w_imei,
604 },
605 [PR_ARQ_S_WAIT_TMSI_ACK] = {
606 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_TMSI_ACK),
607 .in_event_mask = S(PR_ARQ_E_TMSI_ACK),
608 .out_state_mask = S(PR_ARQ_S_DONE),
609 .action = proc_arq_vlr_fn_w_tmsi,
610 },
611 [PR_ARQ_S_DONE] = {
612 .name = OSMO_STRINGIFY(PR_ARQ_S_DONE),
613 .onenter = proc_arq_vlr_dispatch_result,
614 },
615};
616
617static struct osmo_fsm proc_arq_vlr_fsm = {
618 .name = "Process_Access_Request_VLR",
619 .states = proc_arq_vlr_states,
620 .num_states = ARRAY_SIZE(proc_arq_vlr_states),
621 .allstate_event_mask = 0,
622 .allstate_action = NULL,
623 .log_subsys = DVLR,
624 .event_names = proc_arq_vlr_event_names,
625 .cleanup = proc_arq_vlr_cleanup,
626};
627
628void
629vlr_proc_acc_req(struct osmo_fsm_inst *parent,
630 uint32_t parent_event_success,
631 uint32_t parent_event_failure,
632 void *parent_event_data,
633 struct vlr_instance *vlr, void *msc_conn_ref,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100634 enum vlr_parq_type type, enum osmo_cm_service_type cm_service_type,
635 const uint8_t *mi_lv,
Harald Welteb8b85a12016-06-17 00:06:42 +0200636 const struct osmo_location_area_id *lai,
637 bool authentication_required,
Harald Welte71c51df2017-12-23 18:51:48 +0100638 bool ciphering_required,
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100639 uint8_t key_seq,
Harald Welteb8b85a12016-06-17 00:06:42 +0200640 bool is_r99, bool is_utran)
641{
642 struct osmo_fsm_inst *fi;
643 struct proc_arq_priv *par;
644 char mi_string[GSM48_MI_SIZE];
645 uint8_t mi_type;
646
647 fi = osmo_fsm_inst_alloc_child(&proc_arq_vlr_fsm, parent,
648 parent_event_failure);
649 if (!fi)
650 return;
651
652 par = talloc_zero(fi, struct proc_arq_priv);
653 fi->priv = par;
654 par->vlr = vlr;
655 par->msc_conn_ref = msc_conn_ref;
656 par->type = type;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100657 par->cm_service_type = cm_service_type;
Harald Welteb8b85a12016-06-17 00:06:42 +0200658 par->lai = *lai;
659 par->parent_event_success = parent_event_success;
660 par->parent_event_failure = parent_event_failure;
661 par->parent_event_data = parent_event_data;
662 par->authentication_required = authentication_required;
663 par->ciphering_required = ciphering_required;
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100664 par->key_seq = key_seq;
Harald Welteb8b85a12016-06-17 00:06:42 +0200665 par->is_r99 = is_r99;
666 par->is_utran = is_utran;
667
668 LOGPFSM(fi, "rev=%s net=%s%s%s\n",
669 is_r99 ? "R99" : "GSM",
670 is_utran ? "UTRAN" : "GERAN",
671 (authentication_required || ciphering_required)?
672 " Auth" : " (no Auth)",
673 (authentication_required || ciphering_required)?
674 (ciphering_required? "+Ciph" : " (no Ciph)")
675 : "");
676
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200677 if (is_utran && !authentication_required)
678 LOGPFSML(fi, LOGL_ERROR,
679 "Authentication off on UTRAN network. Good luck.\n");
680
Harald Welteb8b85a12016-06-17 00:06:42 +0200681 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]);
682 mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
683 switch (mi_type) {
684 case GSM_MI_TYPE_IMSI:
Neels Hofmeyrfa391ee2018-07-26 20:15:51 +0200685 osmo_strlcpy(par->imsi, mi_string, sizeof(par->imsi));
Harald Welteb8b85a12016-06-17 00:06:42 +0200686 par->by_tmsi = false;
687 break;
688 case GSM_MI_TYPE_TMSI:
689 par->by_tmsi = true;
690 par->tmsi = osmo_load32be(mi_lv+2);
691 break;
692 case GSM_MI_TYPE_IMEI:
693 /* TODO: IMEI (emergency call) */
694 default:
Philipp Maier6f4752e2018-05-15 15:23:59 +0200695 proc_arq_fsm_done(fi, GSM48_REJECT_INVALID_MANDANTORY_INF);
Harald Welteb8b85a12016-06-17 00:06:42 +0200696 return;
697 }
698
699 osmo_fsm_inst_dispatch(fi, PR_ARQ_E_START, NULL);
700}
701
702/* Gracefully terminate an FSM created by vlr_proc_acc_req() in case of
703 * external timeout (i.e. from MSC). */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200704void vlr_parq_cancel(struct osmo_fsm_inst *fi,
705 enum osmo_fsm_term_cause fsm_cause,
706 enum gsm48_reject_value gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200707{
708 if (!fi || fi->state == PR_ARQ_S_DONE)
709 return;
Neels Hofmeyr15809592018-04-06 02:57:51 +0200710 LOGPFSM(fi, "Cancel: %s\n", osmo_fsm_term_cause_name(fsm_cause));
711 proc_arq_fsm_done(fi, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200712}
713
714
715#if 0
716/***********************************************************************
717 * Update_Location_Child_VLR, TS 29.002 Chapter 25.4.4
718 ***********************************************************************/
719
720enum upd_loc_child_vlr_state {
721 ULC_S_IDLE,
722 ULC_S_WAIT_HLR_RESP,
723 ULC_S_DONE,
724};
725
726enum upd_loc_child_vlr_event {
727 ULC_E_START,
728};
729
730static const struct value_string upd_loc_child_vlr_event_names[] = {
731 { ULC_E_START, "START" },
732 { 0, NULL }
733};
734
735static void upd_loc_child_f_idle(struct osmo_fsm_inst *fi, uint32_t event,
736 void *data)
737{
738 OSMO_ASSERT(event == ULC_E_START);
739
740 /* send update location */
741}
742
743static void upd_loc_child_f_w_hlr(struct osmo_fsm_inst *fi, uint32_t event,
744 void *data)
745{
746}
747
748static const struct osmo_fsm_state upd_loc_child_vlr_states[] = {
749 [ULC_S_IDLE] = {
750 .in_event_mask = ,
751 .out_state_mask = S(ULC_S_WAIT_HLR_RESP) |
752 S(ULC_S_DONE),
753 .name = "IDLE",
754 .action = upd_loc_child_f_idle,
755 },
756 [ULC_S_WAIT_HLR_RESP] = {
757 .in_event_mask = ,
758 .out_state_mask = S(ULC_S_DONE),
759 .name = "WAIT-HLR-RESP",
760 .action = upd_loc_child_f_w_hlr,
761 },
762 [ULC_S_DONE] = {
763 .name = "DONE",
764 },
765};
766
767static struct osmo_fsm upd_loc_child_vlr_fsm = {
768 .name = "Update_Location_Child_VLR",
769 .states = upd_loc_child_vlr_states,
770 .num_states = ARRAY_SIZE(upd_loc_child_vlr_states),
771 .log_subsys = DVLR,
772 .event_names = upd_loc_child_vlr_event_names,
773};
774#endif
775
776void vlr_parq_fsm_init(void)
777{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100778 //OSMO_ASSERT(osmo_fsm_register(&upd_loc_child_vlr_fsm) == 0);
779 OSMO_ASSERT(osmo_fsm_register(&proc_arq_vlr_fsm) == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200780}