blob: 26db42cb32677bfc991d4ac3882c28097ff3c978 [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 Hofmeyr15809592018-04-06 02:57:51 +020063 enum gsm48_reject_value result; /*< 0 on success */
Harald Welteb8b85a12016-06-17 00:06:42 +020064 bool by_tmsi;
65 char imsi[16];
66 uint32_t tmsi;
67 struct osmo_location_area_id lai;
68 bool authentication_required;
Harald Welte71c51df2017-12-23 18:51:48 +010069 bool ciphering_required;
Sylvain Munautda9f37e2019-03-14 11:02:36 +010070 uint8_t key_seq;
Harald Welteb8b85a12016-06-17 00:06:42 +020071 bool is_r99;
72 bool is_utran;
73 bool implicitly_accepted_parq_by_ciphering_cmd;
74};
75
Neels Hofmeyr1035d902018-12-28 21:22:32 +010076static int assoc_par_with_subscr(struct osmo_fsm_inst *fi, struct vlr_subscr *vsub)
Harald Welteb8b85a12016-06-17 00:06:42 +020077{
78 struct proc_arq_priv *par = fi->priv;
79 struct vlr_instance *vlr = par->vlr;
80
81 vsub->msc_conn_ref = par->msc_conn_ref;
82 par->vsub = vsub;
83 /* Tell MSC to associate this subscriber with the given
84 * connection */
Neels Hofmeyr1035d902018-12-28 21:22:32 +010085 return vlr->ops.subscr_assoc(par->msc_conn_ref, par->vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +020086}
87
Neels Hofmeyr15809592018-04-06 02:57:51 +020088static const char *vlr_proc_arq_result_name(const struct osmo_fsm_inst *fi)
89{
90 struct proc_arq_priv *par = fi->priv;
91 return par->result? gsm48_reject_value_name(par->result) : "PASSED";
92}
93
Harald Welteb8b85a12016-06-17 00:06:42 +020094#define proc_arq_fsm_done(fi, res) _proc_arq_fsm_done(fi, res, __FILE__, __LINE__)
95static void _proc_arq_fsm_done(struct osmo_fsm_inst *fi,
Neels Hofmeyr15809592018-04-06 02:57:51 +020096 enum gsm48_reject_value gsm48_rej,
Harald Welteb8b85a12016-06-17 00:06:42 +020097 const char *file, int line)
98{
99 struct proc_arq_priv *par = fi->priv;
Neels Hofmeyr15809592018-04-06 02:57:51 +0200100 par->result = gsm48_rej;
101 LOGPFSMSRC(fi, file, line, "proc_arq_fsm_done(%s)\n", vlr_proc_arq_result_name(fi));
Harald Welteb8b85a12016-06-17 00:06:42 +0200102 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_DONE, 0, 0);
103}
104
105static void proc_arq_vlr_dispatch_result(struct osmo_fsm_inst *fi,
106 uint32_t prev_state)
107{
108 struct proc_arq_priv *par = fi->priv;
109 bool success;
110 int rc;
Neels Hofmeyr15809592018-04-06 02:57:51 +0200111 LOGPFSM(fi, "Process Access Request result: %s\n", vlr_proc_arq_result_name(fi));
Harald Welteb8b85a12016-06-17 00:06:42 +0200112
Neels Hofmeyr15809592018-04-06 02:57:51 +0200113 success = (par->result == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200114
115 /* It would be logical to first dispatch the success event to the
116 * parent FSM, but that could start actions that send messages to the
117 * MS. Rather send the CM Service Accept message first and then signal
118 * success. Since messages are handled synchronously, the success event
119 * will be processed before we handle new incoming data from the MS. */
120
121 if (par->type == VLR_PR_ARQ_T_CM_SERV_REQ) {
122 if (success
123 && !par->implicitly_accepted_parq_by_ciphering_cmd) {
124 rc = par->vlr->ops.tx_cm_serv_acc(par->msc_conn_ref);
125 if (rc) {
126 LOGPFSML(fi, LOGL_ERROR,
127 "Failed to send CM Service Accept\n");
128 success = false;
129 }
130 }
131 if (!success) {
132 rc = par->vlr->ops.tx_cm_serv_rej(par->msc_conn_ref,
133 par->result);
134 if (rc)
135 LOGPFSML(fi, LOGL_ERROR,
136 "Failed to send CM Service Reject\n");
137 }
138 }
139
140 /* For VLR_PR_ARQ_T_PAGING_RESP, there is nothing to send. The conn_fsm
141 * will start handling pending paging transactions. */
142
143 if (!fi->proc.parent) {
144 LOGPFSML(fi, LOGL_ERROR, "No parent FSM");
145 return;
146 }
147 osmo_fsm_inst_dispatch(fi->proc.parent,
148 success ? par->parent_event_success
149 : par->parent_event_failure,
150 par->parent_event_data);
151}
152
153void proc_arq_vlr_cleanup(struct osmo_fsm_inst *fi,
154 enum osmo_fsm_term_cause cause)
155{
156 struct proc_arq_priv *par = fi->priv;
157 if (par->vsub && par->vsub->proc_arq_fsm == fi)
158 par->vsub->proc_arq_fsm = NULL;
159}
160
161static void _proc_arq_vlr_post_imei(struct osmo_fsm_inst *fi)
162{
163 struct proc_arq_priv *par = fi->priv;
164 struct vlr_subscr *vsub = par->vsub;
165
166 LOGPFSM(fi, "%s()\n", __func__);
167
Neels Hofmeyref9126c2017-07-18 15:38:39 +0200168 /* See 3GPP TS 29.002 Proc_Acc_Req_VLR3. */
Harald Welteb8b85a12016-06-17 00:06:42 +0200169 /* TODO: Identity := IMSI */
170 if (0 /* TODO: TMSI reallocation at access: vlr->cfg.alloc_tmsi_arq */) {
171 vlr_subscr_alloc_tmsi(vsub);
172 /* TODO: forward TMSI to MS, wait for TMSI
173 * REALLOC COMPLETE */
174 /* TODO: Freeze old TMSI */
175 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_TMSI_ACK, 0, 0);
176 return;
177 }
178
Neels Hofmeyr15809592018-04-06 02:57:51 +0200179 proc_arq_fsm_done(fi, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200180}
181
182static void _proc_arq_vlr_post_trace(struct osmo_fsm_inst *fi)
183{
184 struct proc_arq_priv *par = fi->priv;
185 struct vlr_subscr *vsub = par->vsub;
186 struct vlr_instance *vlr = vsub->vlr;
187
188 LOGPFSM(fi, "%s()\n", __func__);
189
190 /* Node 3 */
Neels Hofmeyref9126c2017-07-18 15:38:39 +0200191 /* See 3GPP TS 29.002 Proc_Acc_Req_VLR3. */
Harald Welteb8b85a12016-06-17 00:06:42 +0200192 if (0 /* IMEI check required */) {
193 /* Chck_IMEI_VLR */
194 vlr->ops.tx_id_req(par->msc_conn_ref, GSM_MI_TYPE_IMEI);
195 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_CHECK_IMEI,
196 vlr_timer(vlr, 3270), 3270);
197 } else
198 _proc_arq_vlr_post_imei(fi);
199}
200
201/* After Subscriber_Present_VLR */
202static void _proc_arq_vlr_post_pres(struct osmo_fsm_inst *fi)
203{
204 LOGPFSM(fi, "%s()\n", __func__);
Neels Hofmeyref9126c2017-07-18 15:38:39 +0200205 /* See 3GPP TS 29.002 Proc_Acc_Req_VLR3. */
Harald Welteb8b85a12016-06-17 00:06:42 +0200206 if (0 /* TODO: tracing required */) {
207 /* TODO: Trace_Subscriber_Activity_VLR */
208 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_TRACE_SUB, 0, 0);
209 }
210 _proc_arq_vlr_post_trace(fi);
211}
212
213/* After Update_Location_Child_VLR */
214static void _proc_arq_vlr_node2_post_vlr(struct osmo_fsm_inst *fi)
215{
216 struct proc_arq_priv *par = fi->priv;
217 struct vlr_subscr *vsub = par->vsub;
218
219 LOGPFSM(fi, "%s()\n", __func__);
220
221 if (!vsub->sub_dataconf_by_hlr_ind) {
222 /* Set User Error: Unidentified Subscriber */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200223 proc_arq_fsm_done(fi, GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200224 return;
225 }
Neels Hofmeyref9126c2017-07-18 15:38:39 +0200226 /* We don't feature location area specific blocking (yet). */
Harald Welteb8b85a12016-06-17 00:06:42 +0200227 if (0 /* roaming not allowed in LA */) {
228 /* Set User Error: Roaming not allowed in this LA */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200229 proc_arq_fsm_done(fi, GSM48_REJECT_ROAMING_NOT_ALLOWED);
Harald Welteb8b85a12016-06-17 00:06:42 +0200230 return;
231 }
232 vsub->imsi_detached_flag = false;
233 if (vsub->ms_not_reachable_flag) {
234 /* Start Subscriber_Present_VLR */
235 osmo_fsm_inst_state_chg(fi, PR_ARQ_S_WAIT_SUB_PRES, 0, 0);
Neels Hofmeyr1a5bcd52017-11-18 22:19:55 +0100236 sub_pres_vlr_fsm_start(&par->sub_pres_vlr_fsm, fi, vsub, PR_ARQ_E_PRES_RES);
Harald Welteb8b85a12016-06-17 00:06:42 +0200237 return;
238 }
239 _proc_arq_vlr_post_pres(fi);
240}
241
242static void _proc_arq_vlr_node2_post_ciph(struct osmo_fsm_inst *fi)
243{
244 struct proc_arq_priv *par = fi->priv;
245 struct vlr_subscr *vsub = par->vsub;
246
247 LOGPFSM(fi, "%s()\n", __func__);
248
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200249 if (par->is_utran) {
250 int rc;
251 rc = par->vlr->ops.tx_common_id(par->msc_conn_ref);
252 if (rc)
253 LOGPFSML(fi, LOGL_ERROR,
254 "Error while sending Common ID (%d)\n", rc);
255 }
256
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);
338 vsub->auth_fsm = auth_fsm_start(vsub, fi->log_level, fi,
339 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 */
359 vsub = vlr_subscr_find_by_imsi(par->vlr, par->imsi);
360 } else {
361 /* TMSI was included */
362 vsub = vlr_subscr_find_by_tmsi(par->vlr, par->tmsi);
363 }
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);
Harald Welteb8b85a12016-06-17 00:06:42 +0200380 vlr_subscr_put(vsub);
381 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
415 vsub = vlr_subscr_find_by_imsi(vlr, par->imsi);
416 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);
Harald Welteb8b85a12016-06-17 00:06:42 +0200425 vlr_subscr_put(vsub);
426}
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{
448 struct proc_arq_priv *par = fi->priv;
449 struct vlr_subscr *vsub = par->vsub;
450 struct vlr_ciph_result res = { .cause = VLR_CIPH_REJECT };
451
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
457 res = *(struct vlr_ciph_result*)data;
458
459 switch (res.cause) {
460 case VLR_CIPH_COMPL:
461 break;
462 case VLR_CIPH_REJECT:
463 LOGPFSM(fi, "ciphering rejected\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200464 proc_arq_fsm_done(fi, GSM48_REJECT_ILLEGAL_MS);
Harald Welteb8b85a12016-06-17 00:06:42 +0200465 return;
466 default:
467 LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: %d\n",
468 res.cause);
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 }
472
473
Neels Hofmeyrfa10eda2018-03-13 01:22:01 +0100474 if (*res.imeisv) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200475 LOGPFSM(fi, "got IMEISV: %s\n", res.imeisv);
476 vlr_subscr_set_imeisv(vsub, res.imeisv);
477 }
478 _proc_arq_vlr_node2_post_ciph(fi);
479}
480
481/* Update_Location_Child_VLR has completed */
482static void proc_arq_vlr_fn_w_upd_loc(struct osmo_fsm_inst *fi,
483 uint32_t event, void *data)
484{
485 OSMO_ASSERT(event == PR_ARQ_E_UPD_LOC_RES);
486
487 _proc_arq_vlr_node2_post_vlr(fi);
488}
489
490/* Subscriber_Present_VLR has completed */
491static void proc_arq_vlr_fn_w_pres(struct osmo_fsm_inst *fi,
492 uint32_t event, void *data)
493{
494 OSMO_ASSERT(event == PR_ARQ_E_PRES_RES);
495
496 _proc_arq_vlr_post_pres(fi);
497}
498
499static void proc_arq_vlr_fn_w_trace(struct osmo_fsm_inst *fi,
500 uint32_t event, void *data)
501{
502 OSMO_ASSERT(event == PR_ARQ_E_TRACE_RES);
503
504 _proc_arq_vlr_post_trace(fi);
505}
506
507/* we have received the ID RESPONSE (IMEI) */
508static void proc_arq_vlr_fn_w_imei(struct osmo_fsm_inst *fi,
509 uint32_t event, void *data)
510{
511 OSMO_ASSERT(event == PR_ARQ_E_IMEI_RES);
512
513 _proc_arq_vlr_post_imei(fi);
514}
515
516/* MSC tells us that MS has acknowleded TMSI re-allocation */
517static void proc_arq_vlr_fn_w_tmsi(struct osmo_fsm_inst *fi,
518 uint32_t event, void *data)
519{
520 OSMO_ASSERT(event == PR_ARQ_E_TMSI_ACK);
521
522 /* FIXME: check confirmation? unfreeze? */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200523 proc_arq_fsm_done(fi, 0);
Harald Welteb8b85a12016-06-17 00:06:42 +0200524}
525
526static const struct osmo_fsm_state proc_arq_vlr_states[] = {
527 [PR_ARQ_S_INIT] = {
528 .name = OSMO_STRINGIFY(PR_ARQ_S_INIT),
529 .in_event_mask = S(PR_ARQ_E_START),
530 .out_state_mask = S(PR_ARQ_S_DONE) |
531 S(PR_ARQ_S_WAIT_OBTAIN_IMSI) |
532 S(PR_ARQ_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100533 S(PR_ARQ_S_WAIT_CIPH) |
Harald Welteb8b85a12016-06-17 00:06:42 +0200534 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
535 S(PR_ARQ_S_WAIT_SUB_PRES) |
536 S(PR_ARQ_S_WAIT_TRACE_SUB) |
537 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
538 S(PR_ARQ_S_WAIT_TMSI_ACK),
539 .action = proc_arq_vlr_fn_init,
540 },
541 [PR_ARQ_S_WAIT_OBTAIN_IMSI] = {
542 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_OBTAIN_IMSI),
543 .in_event_mask = S(PR_ARQ_E_ID_IMSI),
544 .out_state_mask = S(PR_ARQ_S_DONE) |
545 S(PR_ARQ_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100546 S(PR_ARQ_S_WAIT_CIPH) |
Harald Welteb8b85a12016-06-17 00:06:42 +0200547 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
548 S(PR_ARQ_S_WAIT_SUB_PRES) |
549 S(PR_ARQ_S_WAIT_TRACE_SUB) |
550 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
551 S(PR_ARQ_S_WAIT_TMSI_ACK),
552 .action = proc_arq_vlr_fn_w_obt_imsi,
553 },
554 [PR_ARQ_S_WAIT_AUTH] = {
555 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_AUTH),
556 .in_event_mask = S(PR_ARQ_E_AUTH_RES),
557 .out_state_mask = S(PR_ARQ_S_DONE) |
558 S(PR_ARQ_S_WAIT_CIPH) |
559 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
560 S(PR_ARQ_S_WAIT_SUB_PRES) |
561 S(PR_ARQ_S_WAIT_TRACE_SUB) |
562 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
563 S(PR_ARQ_S_WAIT_TMSI_ACK),
564 .action = proc_arq_vlr_fn_w_auth,
565 },
566 [PR_ARQ_S_WAIT_CIPH] = {
567 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_CIPH),
568 .in_event_mask = S(PR_ARQ_E_CIPH_RES),
569 .out_state_mask = S(PR_ARQ_S_DONE) |
570 S(PR_ARQ_S_WAIT_UPD_LOC_CHILD) |
571 S(PR_ARQ_S_WAIT_SUB_PRES) |
572 S(PR_ARQ_S_WAIT_TRACE_SUB) |
573 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
574 S(PR_ARQ_S_WAIT_TMSI_ACK),
575 .action = proc_arq_vlr_fn_w_ciph,
576 },
577 [PR_ARQ_S_WAIT_UPD_LOC_CHILD] = {
578 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_UPD_LOC_CHILD),
579 .in_event_mask = S(PR_ARQ_E_UPD_LOC_RES),
580 .out_state_mask = S(PR_ARQ_S_DONE) |
581 S(PR_ARQ_S_WAIT_SUB_PRES) |
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_upd_loc,
586 },
587 [PR_ARQ_S_WAIT_SUB_PRES] = {
588 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_SUB_PRES),
589 .in_event_mask = S(PR_ARQ_E_PRES_RES),
590 .out_state_mask = S(PR_ARQ_S_DONE) |
591 S(PR_ARQ_S_WAIT_TRACE_SUB) |
592 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
593 S(PR_ARQ_S_WAIT_TMSI_ACK),
594 .action = proc_arq_vlr_fn_w_pres,
595 },
596 [PR_ARQ_S_WAIT_TRACE_SUB] = {
597 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_TRACE_SUB),
598 .in_event_mask = S(PR_ARQ_E_TRACE_RES),
599 .out_state_mask = S(PR_ARQ_S_DONE) |
600 S(PR_ARQ_S_WAIT_CHECK_IMEI) |
601 S(PR_ARQ_S_WAIT_TMSI_ACK),
602 .action = proc_arq_vlr_fn_w_trace,
603 },
604 [PR_ARQ_S_WAIT_CHECK_IMEI] = {
605 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_CHECK_IMEI),
606 .in_event_mask = S(PR_ARQ_E_IMEI_RES),
607 .out_state_mask = S(PR_ARQ_S_DONE) |
608 S(PR_ARQ_S_WAIT_TMSI_ACK),
609 .action = proc_arq_vlr_fn_w_imei,
610 },
611 [PR_ARQ_S_WAIT_TMSI_ACK] = {
612 .name = OSMO_STRINGIFY(PR_ARQ_S_WAIT_TMSI_ACK),
613 .in_event_mask = S(PR_ARQ_E_TMSI_ACK),
614 .out_state_mask = S(PR_ARQ_S_DONE),
615 .action = proc_arq_vlr_fn_w_tmsi,
616 },
617 [PR_ARQ_S_DONE] = {
618 .name = OSMO_STRINGIFY(PR_ARQ_S_DONE),
619 .onenter = proc_arq_vlr_dispatch_result,
620 },
621};
622
623static struct osmo_fsm proc_arq_vlr_fsm = {
624 .name = "Process_Access_Request_VLR",
625 .states = proc_arq_vlr_states,
626 .num_states = ARRAY_SIZE(proc_arq_vlr_states),
627 .allstate_event_mask = 0,
628 .allstate_action = NULL,
629 .log_subsys = DVLR,
630 .event_names = proc_arq_vlr_event_names,
631 .cleanup = proc_arq_vlr_cleanup,
632};
633
634void
635vlr_proc_acc_req(struct osmo_fsm_inst *parent,
636 uint32_t parent_event_success,
637 uint32_t parent_event_failure,
638 void *parent_event_data,
639 struct vlr_instance *vlr, void *msc_conn_ref,
640 enum vlr_parq_type type, const uint8_t *mi_lv,
641 const struct osmo_location_area_id *lai,
642 bool authentication_required,
Harald Welte71c51df2017-12-23 18:51:48 +0100643 bool ciphering_required,
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100644 uint8_t key_seq,
Harald Welteb8b85a12016-06-17 00:06:42 +0200645 bool is_r99, bool is_utran)
646{
647 struct osmo_fsm_inst *fi;
648 struct proc_arq_priv *par;
649 char mi_string[GSM48_MI_SIZE];
650 uint8_t mi_type;
651
652 fi = osmo_fsm_inst_alloc_child(&proc_arq_vlr_fsm, parent,
653 parent_event_failure);
654 if (!fi)
655 return;
656
657 par = talloc_zero(fi, struct proc_arq_priv);
658 fi->priv = par;
659 par->vlr = vlr;
660 par->msc_conn_ref = msc_conn_ref;
661 par->type = type;
662 par->lai = *lai;
663 par->parent_event_success = parent_event_success;
664 par->parent_event_failure = parent_event_failure;
665 par->parent_event_data = parent_event_data;
666 par->authentication_required = authentication_required;
667 par->ciphering_required = ciphering_required;
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100668 par->key_seq = key_seq;
Harald Welteb8b85a12016-06-17 00:06:42 +0200669 par->is_r99 = is_r99;
670 par->is_utran = is_utran;
671
672 LOGPFSM(fi, "rev=%s net=%s%s%s\n",
673 is_r99 ? "R99" : "GSM",
674 is_utran ? "UTRAN" : "GERAN",
675 (authentication_required || ciphering_required)?
676 " Auth" : " (no Auth)",
677 (authentication_required || ciphering_required)?
678 (ciphering_required? "+Ciph" : " (no Ciph)")
679 : "");
680
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200681 if (is_utran && !authentication_required)
682 LOGPFSML(fi, LOGL_ERROR,
683 "Authentication off on UTRAN network. Good luck.\n");
684
Harald Welteb8b85a12016-06-17 00:06:42 +0200685 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]);
686 mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
687 switch (mi_type) {
688 case GSM_MI_TYPE_IMSI:
Neels Hofmeyrfa391ee2018-07-26 20:15:51 +0200689 osmo_strlcpy(par->imsi, mi_string, sizeof(par->imsi));
Harald Welteb8b85a12016-06-17 00:06:42 +0200690 par->by_tmsi = false;
691 break;
692 case GSM_MI_TYPE_TMSI:
693 par->by_tmsi = true;
694 par->tmsi = osmo_load32be(mi_lv+2);
695 break;
696 case GSM_MI_TYPE_IMEI:
697 /* TODO: IMEI (emergency call) */
698 default:
Philipp Maier6f4752e2018-05-15 15:23:59 +0200699 proc_arq_fsm_done(fi, GSM48_REJECT_INVALID_MANDANTORY_INF);
Harald Welteb8b85a12016-06-17 00:06:42 +0200700 return;
701 }
702
703 osmo_fsm_inst_dispatch(fi, PR_ARQ_E_START, NULL);
704}
705
706/* Gracefully terminate an FSM created by vlr_proc_acc_req() in case of
707 * external timeout (i.e. from MSC). */
Neels Hofmeyr15809592018-04-06 02:57:51 +0200708void vlr_parq_cancel(struct osmo_fsm_inst *fi,
709 enum osmo_fsm_term_cause fsm_cause,
710 enum gsm48_reject_value gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200711{
712 if (!fi || fi->state == PR_ARQ_S_DONE)
713 return;
Neels Hofmeyr15809592018-04-06 02:57:51 +0200714 LOGPFSM(fi, "Cancel: %s\n", osmo_fsm_term_cause_name(fsm_cause));
715 proc_arq_fsm_done(fi, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +0200716}
717
718
719#if 0
720/***********************************************************************
721 * Update_Location_Child_VLR, TS 29.002 Chapter 25.4.4
722 ***********************************************************************/
723
724enum upd_loc_child_vlr_state {
725 ULC_S_IDLE,
726 ULC_S_WAIT_HLR_RESP,
727 ULC_S_DONE,
728};
729
730enum upd_loc_child_vlr_event {
731 ULC_E_START,
732};
733
734static const struct value_string upd_loc_child_vlr_event_names[] = {
735 { ULC_E_START, "START" },
736 { 0, NULL }
737};
738
739static void upd_loc_child_f_idle(struct osmo_fsm_inst *fi, uint32_t event,
740 void *data)
741{
742 OSMO_ASSERT(event == ULC_E_START);
743
744 /* send update location */
745}
746
747static void upd_loc_child_f_w_hlr(struct osmo_fsm_inst *fi, uint32_t event,
748 void *data)
749{
750}
751
752static const struct osmo_fsm_state upd_loc_child_vlr_states[] = {
753 [ULC_S_IDLE] = {
754 .in_event_mask = ,
755 .out_state_mask = S(ULC_S_WAIT_HLR_RESP) |
756 S(ULC_S_DONE),
757 .name = "IDLE",
758 .action = upd_loc_child_f_idle,
759 },
760 [ULC_S_WAIT_HLR_RESP] = {
761 .in_event_mask = ,
762 .out_state_mask = S(ULC_S_DONE),
763 .name = "WAIT-HLR-RESP",
764 .action = upd_loc_child_f_w_hlr,
765 },
766 [ULC_S_DONE] = {
767 .name = "DONE",
768 },
769};
770
771static struct osmo_fsm upd_loc_child_vlr_fsm = {
772 .name = "Update_Location_Child_VLR",
773 .states = upd_loc_child_vlr_states,
774 .num_states = ARRAY_SIZE(upd_loc_child_vlr_states),
775 .log_subsys = DVLR,
776 .event_names = upd_loc_child_vlr_event_names,
777};
778#endif
779
780void vlr_parq_fsm_init(void)
781{
782 //osmo_fsm_register(&upd_loc_child_vlr_fsm);
783 osmo_fsm_register(&proc_arq_vlr_fsm);
784}