blob: 53e614d8ed1d931d1a7fdd1ba5df5035fded1882 [file] [log] [blame]
Harald Welteb8b85a12016-06-17 00:06:42 +02001/* Osmocom Visitor Location Register (VLR): Location Update 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>
Max43b01b02017-09-15 11:22:30 +020023#include <osmocom/gsm/gsm48.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020024#include <osmocom/msc/vlr.h>
25#include <osmocom/msc/debug.h>
Harald Welteb8b85a12016-06-17 00:06:42 +020026
27#include "vlr_core.h"
28#include "vlr_auth_fsm.h"
29#include "vlr_lu_fsm.h"
Harald Welte0df904d2018-12-03 11:00:04 +010030#include "vlr_sgs_fsm.h"
Harald Welteb8b85a12016-06-17 00:06:42 +020031
32#define S(x) (1 << (x))
33
34#define LU_TIMEOUT_LONG 30
35
36enum vlr_fsm_result {
37 VLR_FSM_RESULT_NONE,
38 VLR_FSM_RESULT_SUCCESS,
39 VLR_FSM_RESULT_FAILURE,
40};
41
42
43/***********************************************************************
44 * Update_HLR_VLR, TS 23.012 Chapter 4.1.2.4
45 ***********************************************************************/
46
47enum upd_hlr_vlr_state {
48 UPD_HLR_VLR_S_INIT,
49 UPD_HLR_VLR_S_WAIT_FOR_DATA,
50 UPD_HLR_VLR_S_DONE,
51};
52
53enum upd_hlr_vlr_evt {
54 UPD_HLR_VLR_E_START,
55 UPD_HLR_VLR_E_INS_SUB_DATA,
56 UPD_HLR_VLR_E_ACT_TRACE_MODE,
57 UPD_HLR_VLR_E_FW_CHECK_SS_IND,
58 UPD_HLR_VLR_E_UPD_LOC_ACK,
59 UPD_HLR_VLR_E_UPD_LOC_NACK,
60};
61
62static const struct value_string upd_hlr_vlr_event_names[] = {
63 OSMO_VALUE_STRING(UPD_HLR_VLR_E_START),
64 OSMO_VALUE_STRING(UPD_HLR_VLR_E_INS_SUB_DATA),
65 OSMO_VALUE_STRING(UPD_HLR_VLR_E_ACT_TRACE_MODE),
66 OSMO_VALUE_STRING(UPD_HLR_VLR_E_FW_CHECK_SS_IND),
67 OSMO_VALUE_STRING(UPD_HLR_VLR_E_UPD_LOC_ACK),
68 OSMO_VALUE_STRING(UPD_HLR_VLR_E_UPD_LOC_NACK),
69 { 0, NULL }
70};
71
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +010072static inline struct vlr_subscr *upd_hlr_vlr_fi_priv(struct osmo_fsm_inst *fi);
73
Harald Welteb8b85a12016-06-17 00:06:42 +020074static void upd_hlr_vlr_fsm_init(struct osmo_fsm_inst *fi, uint32_t event,
75 void *data)
76{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +010077 struct vlr_subscr *vsub = upd_hlr_vlr_fi_priv(fi);
Max770fbd22018-01-24 12:48:33 +010078 int rc;
Harald Welteb8b85a12016-06-17 00:06:42 +020079
80 OSMO_ASSERT(event == UPD_HLR_VLR_E_START);
81
82 /* Send UpdateLocation to HLR */
Philipp Maier6038ad42018-11-13 13:55:09 +010083 rc = vlr_subscr_req_lu(vsub);
Max770fbd22018-01-24 12:48:33 +010084 if (rc < 0)
85 LOGPFSML(fi, LOGL_ERROR, "Failed to send UpdateLocation to HLR\n");
Harald Welteb8b85a12016-06-17 00:06:42 +020086 osmo_fsm_inst_state_chg(fi, UPD_HLR_VLR_S_WAIT_FOR_DATA,
87 LU_TIMEOUT_LONG, 0);
88}
89
90static void upd_hlr_vlr_fsm_wait_data(struct osmo_fsm_inst *fi, uint32_t event,
91 void *data)
92{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +010093 struct vlr_subscr *vsub = upd_hlr_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +020094
95 switch (event) {
96 case UPD_HLR_VLR_E_INS_SUB_DATA:
97 /* FIXME: Insert_Subscr_Data_VLR */
98 break;
99 case UPD_HLR_VLR_E_ACT_TRACE_MODE:
100 /* TODO: Activate_Tracing_VLR */
101 break;
102 case UPD_HLR_VLR_E_FW_CHECK_SS_IND:
103 /* TODO: Forward Check SS Ind to MSC */
104 break;
105 case UPD_HLR_VLR_E_UPD_LOC_ACK:
106 /* Inside Update_HLR_VLR after UpdateLocationAck */
107 vsub->sub_dataconf_by_hlr_ind = true;
108 vsub->loc_conf_in_hlr_ind = true;
109 osmo_fsm_inst_state_chg(fi, UPD_HLR_VLR_S_DONE, 0, 0);
110 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
111 break;
112 case UPD_HLR_VLR_E_UPD_LOC_NACK:
113 /* Inside Update_HLR_VLR after UpdateLocationNack */
114 /* TODO: Check_User_Error_In_Serving_Network_Entity */
115 vsub->sub_dataconf_by_hlr_ind = false;
116 vsub->loc_conf_in_hlr_ind = false;
117 osmo_fsm_inst_state_chg(fi, UPD_HLR_VLR_S_DONE, 0, 0);
118 /* Data is a pointer to a gsm48_gmm_cause which we
119 * simply pass through */
120 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, data);
121 break;
122 }
123}
124
125static const struct osmo_fsm_state upd_hlr_vlr_states[] = {
126 [UPD_HLR_VLR_S_INIT] = {
127 .in_event_mask = S(UPD_HLR_VLR_E_START),
128 .out_state_mask = S(UPD_HLR_VLR_S_WAIT_FOR_DATA),
129 .name = OSMO_STRINGIFY(UPD_HLR_VLR_S_INIT),
130 .action = upd_hlr_vlr_fsm_init,
131 },
132 [UPD_HLR_VLR_S_WAIT_FOR_DATA] = {
133 .in_event_mask = S(UPD_HLR_VLR_E_INS_SUB_DATA) |
134 S(UPD_HLR_VLR_E_ACT_TRACE_MODE) |
135 S(UPD_HLR_VLR_E_FW_CHECK_SS_IND) |
136 S(UPD_HLR_VLR_E_UPD_LOC_ACK) |
137 S(UPD_HLR_VLR_E_UPD_LOC_NACK),
138 .out_state_mask = S(UPD_HLR_VLR_S_DONE),
139 .name = OSMO_STRINGIFY(UPD_HLR_VLR_S_WAIT_FOR_DATA),
140 .action = upd_hlr_vlr_fsm_wait_data,
141 },
142 [UPD_HLR_VLR_S_DONE] = {
143 .name = OSMO_STRINGIFY(UPD_HLR_VLR_S_DONE),
144 },
145};
146
147static struct osmo_fsm upd_hlr_vlr_fsm = {
148 .name = "upd_hlr_vlr_fsm",
149 .states = upd_hlr_vlr_states,
150 .num_states = ARRAY_SIZE(upd_hlr_vlr_states),
151 .allstate_event_mask = 0,
152 .allstate_action = NULL,
153 .log_subsys = DVLR,
154 .event_names = upd_hlr_vlr_event_names,
155};
156
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100157static inline struct vlr_subscr *upd_hlr_vlr_fi_priv(struct osmo_fsm_inst *fi)
158{
159 OSMO_ASSERT(fi->fsm == &upd_hlr_vlr_fsm);
160 return (struct vlr_subscr*)fi->priv;
161}
162
Harald Welteb8b85a12016-06-17 00:06:42 +0200163struct osmo_fsm_inst *
164upd_hlr_vlr_proc_start(struct osmo_fsm_inst *parent,
165 struct vlr_subscr *vsub,
166 uint32_t parent_event)
167{
168 struct osmo_fsm_inst *fi;
169
170 fi = osmo_fsm_inst_alloc_child(&upd_hlr_vlr_fsm, parent,
171 parent_event);
172 if (!fi)
173 return NULL;
174
175 fi->priv = vsub;
176 osmo_fsm_inst_dispatch(fi, UPD_HLR_VLR_E_START, NULL);
177
178 return fi;
179}
180
181
182/***********************************************************************
183 * Subscriber_Present_VLR, TS 29.002 Chapter 25.10.1
184 ***********************************************************************/
185
186enum sub_pres_vlr_state {
187 SUB_PRES_VLR_S_INIT,
188 SUB_PRES_VLR_S_WAIT_FOR_HLR,
189 SUB_PRES_VLR_S_DONE,
190};
191
192enum sub_pres_vlr_event {
193 SUB_PRES_VLR_E_START,
194 SUB_PRES_VLR_E_READY_SM_CNF,
195 SUB_PRES_VLR_E_READY_SM_ERR,
196};
197
198static const struct value_string sub_pres_vlr_event_names[] = {
199 OSMO_VALUE_STRING(SUB_PRES_VLR_E_START),
200 OSMO_VALUE_STRING(SUB_PRES_VLR_E_READY_SM_CNF),
201 OSMO_VALUE_STRING(SUB_PRES_VLR_E_READY_SM_ERR),
202 { 0, NULL }
203};
204
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100205static inline struct vlr_subscr *sub_pres_vlr_fi_priv(struct osmo_fsm_inst *fi);
206
Harald Welteb8b85a12016-06-17 00:06:42 +0200207static void sub_pres_vlr_fsm_init(struct osmo_fsm_inst *fi, uint32_t event,
208 void *data)
209{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100210 struct vlr_subscr *vsub = sub_pres_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200211 OSMO_ASSERT(event == SUB_PRES_VLR_E_START);
212
213 if (!vsub->ms_not_reachable_flag) {
214 osmo_fsm_inst_state_chg(fi, SUB_PRES_VLR_S_DONE, 0, 0);
215 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
216 return;
217 }
218 /* FIXME: Send READY_FOR_SM via GSUP */
219 osmo_fsm_inst_state_chg(fi, SUB_PRES_VLR_S_WAIT_FOR_HLR,
220 LU_TIMEOUT_LONG, 0);
221}
222
223static void sub_pres_vlr_fsm_wait_hlr(struct osmo_fsm_inst *fi, uint32_t event,
224 void *data)
225{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100226 struct vlr_subscr *vsub = sub_pres_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200227
228 switch (event) {
229 case SUB_PRES_VLR_E_READY_SM_CNF:
230 vsub->ms_not_reachable_flag = false;
231 break;
232 case SUB_PRES_VLR_E_READY_SM_ERR:
233 break;
234 }
235 osmo_fsm_inst_state_chg(fi, SUB_PRES_VLR_S_DONE, 0, 0);
236 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
237}
238
239static const struct osmo_fsm_state sub_pres_vlr_states[] = {
240 [SUB_PRES_VLR_S_INIT] = {
241 .in_event_mask = S(SUB_PRES_VLR_E_START),
242 .out_state_mask = S(SUB_PRES_VLR_S_WAIT_FOR_HLR) |
243 S(SUB_PRES_VLR_S_DONE),
244 .name = OSMO_STRINGIFY(SUB_PRES_VLR_S_INIT),
245 .action = sub_pres_vlr_fsm_init,
246 },
247 [SUB_PRES_VLR_S_WAIT_FOR_HLR] = {
248 .in_event_mask = S(SUB_PRES_VLR_E_READY_SM_CNF) |
249 S(SUB_PRES_VLR_E_READY_SM_ERR),
250 .out_state_mask = S(SUB_PRES_VLR_S_DONE),
251 .name = OSMO_STRINGIFY(SUB_PRES_VLR_S_WAIT_FOR_HLR),
252 .action = sub_pres_vlr_fsm_wait_hlr,
253 },
254 [SUB_PRES_VLR_S_DONE] = {
255 .name = OSMO_STRINGIFY(SUB_PRES_VLR_S_DONE),
256 },
257};
258
259static struct osmo_fsm sub_pres_vlr_fsm = {
260 .name = "sub_pres_vlr_fsm",
261 .states = sub_pres_vlr_states,
262 .num_states = ARRAY_SIZE(sub_pres_vlr_states),
263 .allstate_event_mask = 0,
264 .allstate_action = NULL,
265 .log_subsys = DVLR,
266 .event_names = sub_pres_vlr_event_names,
267};
268
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100269static inline struct vlr_subscr *sub_pres_vlr_fi_priv(struct osmo_fsm_inst *fi)
270{
271 OSMO_ASSERT(fi->fsm == &sub_pres_vlr_fsm);
272 return (struct vlr_subscr*)fi->priv;
273}
274
Neels Hofmeyr5f1ac6d2018-12-11 01:59:25 +0100275/* THIS IS CURRENTLY DEAD CODE, SINCE WE NEVER SET vsub->ms_not_reachable_flag = true.
276 *
277 * Note that the start event is dispatched right away, so in case the FSM immediately concludes from that
Neels Hofmeyr1a5bcd52017-11-18 22:19:55 +0100278 * event, the created FSM struct may no longer be valid as it already deallocated again, and it may
279 * furthermore already have invoked the parent FSM instance's deallocation as well. Hence, instead of
280 * returning, store the created FSM instance address in *fi_p before dispatching the event. It is thus
281 * possible to store the instance's pointer in a parent FSM instance without running danger of using
282 * already freed memory. */
283void sub_pres_vlr_fsm_start(struct osmo_fsm_inst **fi_p,
284 struct osmo_fsm_inst *parent,
285 struct vlr_subscr *vsub,
286 uint32_t term_event)
Harald Welteb8b85a12016-06-17 00:06:42 +0200287{
288 struct osmo_fsm_inst *fi;
289
Neels Hofmeyr1a5bcd52017-11-18 22:19:55 +0100290 OSMO_ASSERT(fi_p);
291
Harald Welteb8b85a12016-06-17 00:06:42 +0200292 fi = osmo_fsm_inst_alloc_child(&sub_pres_vlr_fsm, parent,
293 term_event);
Neels Hofmeyr1a5bcd52017-11-18 22:19:55 +0100294 *fi_p = fi;
Harald Welteb8b85a12016-06-17 00:06:42 +0200295 if (!fi)
Neels Hofmeyr1a5bcd52017-11-18 22:19:55 +0100296 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200297
298 fi->priv = vsub;
299 osmo_fsm_inst_dispatch(fi, SUB_PRES_VLR_E_START, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +0200300}
301
302/***********************************************************************
303 * Location_Update_Completion_VLR, TS 23.012 Chapter 4.1.2.3
304 ***********************************************************************/
305
306enum lu_compl_vlr_state {
307 LU_COMPL_VLR_S_INIT,
308 LU_COMPL_VLR_S_WAIT_SUB_PRES,
309 LU_COMPL_VLR_S_WAIT_IMEI,
310 LU_COMPL_VLR_S_WAIT_IMEI_TMSI,
311 LU_COMPL_VLR_S_WAIT_TMSI_CNF,
312 LU_COMPL_VLR_S_DONE,
313};
314
315enum lu_compl_vlr_event {
316 LU_COMPL_VLR_E_START,
317 LU_COMPL_VLR_E_SUB_PRES_COMPL,
318 LU_COMPL_VLR_E_IMEI_CHECK_ACK,
319 LU_COMPL_VLR_E_IMEI_CHECK_NACK,
320 LU_COMPL_VLR_E_NEW_TMSI_ACK,
321};
322
323static const struct value_string lu_compl_vlr_event_names[] = {
324 OSMO_VALUE_STRING(LU_COMPL_VLR_E_START),
325 OSMO_VALUE_STRING(LU_COMPL_VLR_E_SUB_PRES_COMPL),
326 OSMO_VALUE_STRING(LU_COMPL_VLR_E_IMEI_CHECK_ACK),
327 OSMO_VALUE_STRING(LU_COMPL_VLR_E_IMEI_CHECK_NACK),
328 OSMO_VALUE_STRING(LU_COMPL_VLR_E_NEW_TMSI_ACK),
329 { 0, NULL }
330};
331
332struct lu_compl_vlr_priv {
333 struct vlr_subscr *vsub;
334 void *msc_conn_ref;
335 struct osmo_fsm_inst *sub_pres_vlr_fsm;
336 uint32_t parent_event_success;
337 uint32_t parent_event_failure;
338 void *parent_event_data;
339 enum vlr_fsm_result result;
340 uint8_t cause;
341 bool assign_tmsi;
342};
343
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100344static inline struct lu_compl_vlr_priv *lu_compl_vlr_fi_priv(struct osmo_fsm_inst *fi);
345
Harald Welteb8b85a12016-06-17 00:06:42 +0200346static void _vlr_lu_compl_fsm_done(struct osmo_fsm_inst *fi,
347 enum vlr_fsm_result result,
348 uint8_t cause)
349{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100350 struct lu_compl_vlr_priv *lcvp = lu_compl_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200351 lcvp->result = result;
352 lcvp->cause = cause;
353 osmo_fsm_inst_state_chg(fi, LU_COMPL_VLR_S_DONE, 0, 0);
354}
355
356static void vlr_lu_compl_fsm_success(struct osmo_fsm_inst *fi)
357{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100358 struct lu_compl_vlr_priv *lcvp = lu_compl_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200359 struct vlr_subscr *vsub = lcvp->vsub;
360 if (!vsub->lu_complete) {
361 vsub->lu_complete = true;
Stefan Sperlingdefc3c82018-05-15 14:48:04 +0200362 /* Balanced by vlr_subscr_expire() */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100363 vlr_subscr_get(vsub, VSUB_USE_ATTACHED);
Harald Welteb8b85a12016-06-17 00:06:42 +0200364 }
365 _vlr_lu_compl_fsm_done(fi, VLR_FSM_RESULT_SUCCESS, 0);
Harald Welte0df904d2018-12-03 11:00:04 +0100366 vlr_sgs_fsm_update_id(vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200367}
368
Harald Welteb8b85a12016-06-17 00:06:42 +0200369static void vlr_lu_compl_fsm_dispatch_result(struct osmo_fsm_inst *fi,
370 uint32_t prev_state)
371{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100372 struct lu_compl_vlr_priv *lcvp = lu_compl_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200373 if (!fi->proc.parent) {
374 LOGPFSML(fi, LOGL_ERROR, "No parent FSM\n");
375 return;
376 }
377 osmo_fsm_inst_dispatch(fi->proc.parent,
378 (lcvp->result == VLR_FSM_RESULT_SUCCESS)
379 ? lcvp->parent_event_success
380 : lcvp->parent_event_failure,
381 &lcvp->cause);
382}
383
384static void lu_compl_vlr_init(struct osmo_fsm_inst *fi, uint32_t event,
385 void *data)
386{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100387 struct lu_compl_vlr_priv *lcvp = lu_compl_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200388 struct vlr_subscr *vsub = lcvp->vsub;
389 struct vlr_instance *vlr;
390 OSMO_ASSERT(vsub);
391 vlr = vsub->vlr;
392 OSMO_ASSERT(vlr);
393
394 OSMO_ASSERT(event == LU_COMPL_VLR_E_START);
395
396 /* TODO: National Roaming restrictions? */
397 /* TODO: Roaming restriction due to unsupported feature in subscriber
398 * data? */
399 /* TODO: Regional subscription restriction? */
400 /* TODO: Administrative restriction of subscribres' access feature? */
401 /* TODO: AccessRestrictuionData parameter available? */
402 /* TODO: AccessRestrictionData permits RAT? */
403 /* Node 1 */
404 /* TODO: Autonomous CSG supported in VPLMN and allowed by HPLMN? */
405 /* TODO: Hybrid Cel / CSG Cell */
406 /* Node 2 */
407 vsub->la_allowed = true;
408 vsub->imsi_detached_flag = false;
409 /* Start Subscriber_Present_VLR Procedure */
410 osmo_fsm_inst_state_chg(fi, LU_COMPL_VLR_S_WAIT_SUB_PRES,
411 LU_TIMEOUT_LONG, 0);
412
Neels Hofmeyraa14bac2018-12-11 01:56:11 +0100413 /* If ms_not_reachable_flag == false, the sub_pres_vlr_fsm will anyway terminate straight away and dispatch
414 * LU_COMPL_VLR_E_SUB_PRES_COMPL to this fi, so we might as well skip that dance here and save some logging. */
415 if (vsub->ms_not_reachable_flag)
416 sub_pres_vlr_fsm_start(&lcvp->sub_pres_vlr_fsm, fi, vsub, LU_COMPL_VLR_E_SUB_PRES_COMPL);
417 else
418 osmo_fsm_inst_dispatch(fi, LU_COMPL_VLR_E_SUB_PRES_COMPL, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +0200419}
420
421static void lu_compl_vlr_new_tmsi(struct osmo_fsm_inst *fi)
422{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100423 struct lu_compl_vlr_priv *lcvp = lu_compl_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200424 struct vlr_subscr *vsub = lcvp->vsub;
425 struct vlr_instance *vlr = vsub->vlr;
426
427 LOGPFSM(fi, "%s()\n", __func__);
428
429 if (vlr_subscr_alloc_tmsi(vsub)) {
Oliver Smithc0a5e712019-07-23 10:43:36 +0200430 _vlr_lu_compl_fsm_done(fi, VLR_FSM_RESULT_FAILURE, GSM48_REJECT_SRV_OPT_TMP_OUT_OF_ORDER);
Harald Welteb8b85a12016-06-17 00:06:42 +0200431 return;
432 }
433
434 osmo_fsm_inst_state_chg(fi, LU_COMPL_VLR_S_WAIT_TMSI_CNF,
435 vlr_timer(vlr, 3250), 3250);
436
437 vlr->ops.tx_lu_acc(lcvp->msc_conn_ref, vsub->tmsi_new);
438}
439
440/* After completion of Subscriber_Present_VLR */
441static void lu_compl_vlr_wait_subscr_pres(struct osmo_fsm_inst *fi,
442 uint32_t event,
443 void *data)
444{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100445 struct lu_compl_vlr_priv *lcvp = lu_compl_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200446 struct vlr_subscr *vsub = lcvp->vsub;
447 struct vlr_instance *vlr = vsub->vlr;
448
449 OSMO_ASSERT(event == LU_COMPL_VLR_E_SUB_PRES_COMPL);
450
451 lcvp->sub_pres_vlr_fsm = NULL;
452
453 /* TODO: Trace_Subscriber_Activity_VLR */
454
Oliver Smithcbf2c932019-05-06 13:09:55 +0200455 /* If imeisv_early is enabled: IMEI already retrieved and checked (vlr_loc_upd_node1_pre), don't do it again. */
456 if (vlr->cfg.check_imei_rqd && !vlr->cfg.retrieve_imeisv_early) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200457 /* Check IMEI VLR */
458 osmo_fsm_inst_state_chg(fi,
459 lcvp->assign_tmsi ?
460 LU_COMPL_VLR_S_WAIT_IMEI_TMSI
461 : LU_COMPL_VLR_S_WAIT_IMEI,
462 vlr_timer(vlr, 3270), 3270);
463 vlr->ops.tx_id_req(lcvp->msc_conn_ref, GSM_MI_TYPE_IMEI);
464 return;
465 }
466
467 /* Do we need to allocate a TMSI? */
468 if (lcvp->assign_tmsi) {
469 lu_compl_vlr_new_tmsi(fi);
470 return;
471 }
472
473 /* Location Updating Accept */
474 vlr->ops.tx_lu_acc(lcvp->msc_conn_ref, GSM_RESERVED_TMSI);
475 vlr_lu_compl_fsm_success(fi);
476}
477
478/* Waiting for completion of CHECK_IMEI_VLR */
479static void lu_compl_vlr_wait_imei(struct osmo_fsm_inst *fi, uint32_t event,
480 void *data)
481{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100482 struct lu_compl_vlr_priv *lcvp = lu_compl_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200483 struct vlr_subscr *vsub = lcvp->vsub;
484 struct vlr_instance *vlr = vsub->vlr;
485
486 switch (event) {
487 case LU_COMPL_VLR_E_IMEI_CHECK_ACK:
488 if (!vsub->imei[0]) {
489 /* Abort: Do nothing */
Oliver Smithc0a5e712019-07-23 10:43:36 +0200490 _vlr_lu_compl_fsm_done(fi, VLR_FSM_RESULT_FAILURE, GSM48_REJECT_PROTOCOL_ERROR);
Harald Welteb8b85a12016-06-17 00:06:42 +0200491 return;
492 }
493 /* Pass */
494 break;
495
496 case LU_COMPL_VLR_E_IMEI_CHECK_NACK:
Oliver Smithc0a5e712019-07-23 10:43:36 +0200497 _vlr_lu_compl_fsm_done(fi, VLR_FSM_RESULT_FAILURE, GSM48_REJECT_ILLEGAL_ME);
Harald Welteb8b85a12016-06-17 00:06:42 +0200498 /* FIXME: IMEI Check Fail to VLR Application (Detach IMSI VLR) */
499 return;
500 }
501
502 /* IMEI is available. Allocate TMSI if needed. */
503 if (lcvp->assign_tmsi) {
504 if (fi->state != LU_COMPL_VLR_S_WAIT_IMEI_TMSI)
505 LOGPFSML(fi, LOGL_ERROR,
506 "TMSI required, expected to be in state"
507 " LU_COMPL_VLR_S_WAIT_IMEI_TMSI,"
508 " am in %s instead\n",
509 osmo_fsm_state_name(fi->fsm, fi->state));
510 /* Logged an error, continue anyway. */
511
512 lu_compl_vlr_new_tmsi(fi);
513
514 /* Wait for TMSI ack */
515 return;
516 }
517
518 /* No TMSI needed, accept now. */
519 vlr->ops.tx_lu_acc(lcvp->msc_conn_ref, GSM_RESERVED_TMSI);
520 vlr_lu_compl_fsm_success(fi);
521}
522
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100523static inline struct lu_compl_vlr_priv *lu_compl_vlr_fi_priv(struct osmo_fsm_inst *fi);
524
Harald Welteb8b85a12016-06-17 00:06:42 +0200525/* Waiting for TMSI confirmation */
526static void lu_compl_vlr_wait_tmsi(struct osmo_fsm_inst *fi, uint32_t event,
527 void *data)
528{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100529 struct lu_compl_vlr_priv *lcvp = lu_compl_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200530 struct vlr_subscr *vsub = lcvp->vsub;
531
532 OSMO_ASSERT(event == LU_COMPL_VLR_E_NEW_TMSI_ACK);
533
534 if (!vsub || vsub->tmsi_new == GSM_RESERVED_TMSI) {
535 LOGPFSML(fi, LOGL_ERROR, "TMSI Realloc Compl implies that"
536 " the subscriber has a new TMSI allocated, but"
537 " the new TMSI is unset.\n");
Oliver Smithc0a5e712019-07-23 10:43:36 +0200538 _vlr_lu_compl_fsm_done(fi, VLR_FSM_RESULT_FAILURE, GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200539 return;
540 }
541
542 vsub->tmsi = vsub->tmsi_new;
543 vsub->tmsi_new = GSM_RESERVED_TMSI;
Neels Hofmeyr361e5712019-01-03 02:32:14 +0100544 vsub->vlr->ops.subscr_update(vsub);
Harald Welteb8b85a12016-06-17 00:06:42 +0200545
546 vlr_lu_compl_fsm_success(fi);
547}
548
549static const struct osmo_fsm_state lu_compl_vlr_states[] = {
550 [LU_COMPL_VLR_S_INIT] = {
551 .in_event_mask = S(LU_COMPL_VLR_E_START),
552 .out_state_mask = S(LU_COMPL_VLR_S_DONE) |
Neels Hofmeyrd50f8982018-12-11 02:24:57 +0100553 S(LU_COMPL_VLR_S_WAIT_SUB_PRES),
Harald Welteb8b85a12016-06-17 00:06:42 +0200554 .name = OSMO_STRINGIFY(LU_COMPL_VLR_S_INIT),
555 .action = lu_compl_vlr_init,
556 },
557 [LU_COMPL_VLR_S_WAIT_SUB_PRES] = {
558 .in_event_mask = S(LU_COMPL_VLR_E_SUB_PRES_COMPL),
559 .out_state_mask = S(LU_COMPL_VLR_S_WAIT_IMEI) |
560 S(LU_COMPL_VLR_S_WAIT_IMEI_TMSI) |
561 S(LU_COMPL_VLR_S_WAIT_TMSI_CNF) |
562 S(LU_COMPL_VLR_S_DONE),
563 .name = OSMO_STRINGIFY(LU_COMPL_VLR_S_WAIT_SUB_PRES),
564 .action = lu_compl_vlr_wait_subscr_pres,
565 },
566 [LU_COMPL_VLR_S_WAIT_IMEI] = {
567 .in_event_mask = S(LU_COMPL_VLR_E_IMEI_CHECK_ACK) |
568 S(LU_COMPL_VLR_E_IMEI_CHECK_NACK),
569 .out_state_mask = S(LU_COMPL_VLR_S_DONE),
570 .name = OSMO_STRINGIFY(LU_COMPL_VLR_S_WAIT_IMEI),
571 .action = lu_compl_vlr_wait_imei,
572 },
573 [LU_COMPL_VLR_S_WAIT_IMEI_TMSI] = {
574 .in_event_mask = S(LU_COMPL_VLR_E_IMEI_CHECK_ACK) |
575 S(LU_COMPL_VLR_E_IMEI_CHECK_NACK),
576 .out_state_mask = S(LU_COMPL_VLR_S_DONE) |
577 S(LU_COMPL_VLR_S_WAIT_TMSI_CNF),
578 .name = OSMO_STRINGIFY(LU_COMPL_VLR_S_WAIT_IMEI_TMSI),
579 .action = lu_compl_vlr_wait_imei,
580 },
581 [LU_COMPL_VLR_S_WAIT_TMSI_CNF] = {
582 .in_event_mask = S(LU_COMPL_VLR_E_NEW_TMSI_ACK),
583 .out_state_mask = S(LU_COMPL_VLR_S_DONE),
584 .name = OSMO_STRINGIFY(LU_COMPL_VLR_S_WAIT_TMSI_CNF),
585 .action = lu_compl_vlr_wait_tmsi,
586 },
587 [LU_COMPL_VLR_S_DONE] = {
588 .name = OSMO_STRINGIFY(LU_COMPL_VLR_S_DONE),
589 .onenter = vlr_lu_compl_fsm_dispatch_result,
590 },
591};
592
593static struct osmo_fsm lu_compl_vlr_fsm = {
594 .name = "lu_compl_vlr_fsm",
595 .states = lu_compl_vlr_states,
596 .num_states = ARRAY_SIZE(lu_compl_vlr_states),
597 .allstate_event_mask = 0,
598 .allstate_action = NULL,
599 .log_subsys = DVLR,
600 .event_names = lu_compl_vlr_event_names,
601};
602
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100603static inline struct lu_compl_vlr_priv *lu_compl_vlr_fi_priv(struct osmo_fsm_inst *fi)
604{
605 OSMO_ASSERT(fi->fsm == &lu_compl_vlr_fsm);
606 return (struct lu_compl_vlr_priv*)fi->priv;
607}
608
Harald Welteb8b85a12016-06-17 00:06:42 +0200609struct osmo_fsm_inst *
610lu_compl_vlr_proc_alloc(struct osmo_fsm_inst *parent,
611 struct vlr_subscr *vsub,
612 void *msc_conn_ref,
613 uint32_t parent_event_success,
614 uint32_t parent_event_failure,
615 bool assign_tmsi)
616{
617 struct osmo_fsm_inst *fi;
618 struct lu_compl_vlr_priv *lcvp;
619
620 fi = osmo_fsm_inst_alloc_child(&lu_compl_vlr_fsm, parent,
621 parent_event_failure);
622 if (!fi)
623 return NULL;
624
625 lcvp = talloc_zero(fi, struct lu_compl_vlr_priv);
626 lcvp->vsub = vsub;
627 lcvp->msc_conn_ref = msc_conn_ref;
628 lcvp->parent_event_success = parent_event_success;
629 lcvp->parent_event_failure = parent_event_failure;
630 lcvp->assign_tmsi = assign_tmsi;
631 fi->priv = lcvp;
632
633 return fi;
634}
635
636
637/***********************************************************************
638 * Update_Location_Area_VLR, TS 23.012 Chapter 4.1.2.1
639 ***********************************************************************/
640
641static const struct value_string fsm_lu_event_names[] = {
642 OSMO_VALUE_STRING(VLR_ULA_E_UPDATE_LA),
643 OSMO_VALUE_STRING(VLR_ULA_E_SEND_ID_ACK),
644 OSMO_VALUE_STRING(VLR_ULA_E_SEND_ID_NACK),
645 OSMO_VALUE_STRING(VLR_ULA_E_AUTH_RES),
646 OSMO_VALUE_STRING(VLR_ULA_E_CIPH_RES),
647 OSMO_VALUE_STRING(VLR_ULA_E_ID_IMSI),
648 OSMO_VALUE_STRING(VLR_ULA_E_ID_IMEI),
649 OSMO_VALUE_STRING(VLR_ULA_E_ID_IMEISV),
Oliver Smith7d053092018-12-14 17:37:38 +0100650 OSMO_VALUE_STRING(VLR_ULA_E_HLR_IMEI_ACK),
651 OSMO_VALUE_STRING(VLR_ULA_E_HLR_IMEI_NACK),
Harald Welteb8b85a12016-06-17 00:06:42 +0200652 OSMO_VALUE_STRING(VLR_ULA_E_HLR_LU_RES),
653 OSMO_VALUE_STRING(VLR_ULA_E_UPD_HLR_COMPL),
654 OSMO_VALUE_STRING(VLR_ULA_E_LU_COMPL_SUCCESS),
655 OSMO_VALUE_STRING(VLR_ULA_E_LU_COMPL_FAILURE),
656 OSMO_VALUE_STRING(VLR_ULA_E_NEW_TMSI_ACK),
657 { 0, NULL }
658};
659
660struct lu_fsm_priv {
661 struct vlr_instance *vlr;
662 struct vlr_subscr *vsub;
663 void *msc_conn_ref;
664 struct osmo_fsm_inst *upd_hlr_vlr_fsm;
665 struct osmo_fsm_inst *lu_compl_vlr_fsm;
666 uint32_t parent_event_success;
667 uint32_t parent_event_failure;
668 void *parent_event_data;
669 enum vlr_fsm_result result;
670 uint8_t rej_cause;
671
672 enum vlr_lu_type type;
673 bool lu_by_tmsi;
674 char imsi[16];
675 uint32_t tmsi;
676 struct osmo_location_area_id old_lai;
677 struct osmo_location_area_id new_lai;
678 bool authentication_required;
Harald Welte71c51df2017-12-23 18:51:48 +0100679 bool ciphering_required;
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100680 uint8_t key_seq;
Harald Welteb8b85a12016-06-17 00:06:42 +0200681 bool is_r99;
682 bool is_utran;
683 bool assign_tmsi;
684};
685
686
687/* Determine if given location area is served by this VLR */
688static bool lai_in_this_vlr(struct vlr_instance *vlr,
689 const struct osmo_location_area_id *lai)
690{
Martin Hauke3f07dac2019-11-14 17:49:08 +0100691 /* TODO: VLR needs to keep a locally configured list of LAIs */
Harald Welteb8b85a12016-06-17 00:06:42 +0200692 return true;
693}
694
695/* Determine if authentication is required */
696static bool is_auth_required(struct lu_fsm_priv *lfp)
697{
698 /* The cases where the authentication procedure should be used
699 * are defined in 3GPP TS 33.102 */
700 /* For now we use a default value passed in to vlr_lu_fsm(). */
Sylvain Munautda9f37e2019-03-14 11:02:36 +0100701 return lfp->authentication_required ||
702 (lfp->ciphering_required && !auth_try_reuse_tuple(lfp->vsub, lfp->key_seq));
Harald Welteb8b85a12016-06-17 00:06:42 +0200703}
704
705/* Determine if ciphering is required */
706static bool is_ciph_required(struct lu_fsm_priv *lfp)
707{
Harald Welte71c51df2017-12-23 18:51:48 +0100708 return lfp->ciphering_required;
Harald Welteb8b85a12016-06-17 00:06:42 +0200709}
710
711/* Determine if a HLR Update is required */
712static bool hlr_update_needed(struct vlr_subscr *vsub)
713{
714 /* TODO: properly decide this, rather than always assuming we
715 * need to update the HLR. */
716 return true;
717}
718
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100719static inline struct lu_fsm_priv *lu_fsm_fi_priv(struct osmo_fsm_inst *fi);
720
Harald Welteb8b85a12016-06-17 00:06:42 +0200721static void lu_fsm_dispatch_result(struct osmo_fsm_inst *fi,
722 uint32_t prev_state)
723{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100724 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200725 if (!fi->proc.parent) {
726 LOGPFSML(fi, LOGL_ERROR, "No parent FSM\n");
727 return;
728 }
729 osmo_fsm_inst_dispatch(fi->proc.parent,
730 (lfp->result == VLR_FSM_RESULT_SUCCESS)
731 ? lfp->parent_event_success
732 : lfp->parent_event_failure,
733 lfp->parent_event_data);
734}
735
736static void _lu_fsm_done(struct osmo_fsm_inst *fi,
737 enum vlr_fsm_result result)
738{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100739 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200740 lfp->result = result;
741 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_DONE, 0, 0);
742}
743
744static void lu_fsm_success(struct osmo_fsm_inst *fi)
745{
746 _lu_fsm_done(fi, VLR_FSM_RESULT_SUCCESS);
747}
748
Neels Hofmeyr15809592018-04-06 02:57:51 +0200749static void lu_fsm_failure(struct osmo_fsm_inst *fi, enum gsm48_reject_value rej_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200750{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100751 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200752 lfp->vlr->ops.tx_lu_rej(lfp->msc_conn_ref, rej_cause ? : GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200753 _lu_fsm_done(fi, VLR_FSM_RESULT_FAILURE);
754}
755
756static void vlr_loc_upd_start_lu_compl_fsm(struct osmo_fsm_inst *fi)
757{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100758 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200759 lfp->lu_compl_vlr_fsm =
760 lu_compl_vlr_proc_alloc(fi, lfp->vsub, lfp->msc_conn_ref,
761 VLR_ULA_E_LU_COMPL_SUCCESS,
762 VLR_ULA_E_LU_COMPL_FAILURE,
763 lfp->assign_tmsi);
764
765 osmo_fsm_inst_dispatch(lfp->lu_compl_vlr_fsm, LU_COMPL_VLR_E_START, NULL);
766}
767
768static void lu_fsm_discard_lu_compl_fsm(struct osmo_fsm_inst *fi)
769{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100770 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200771 if (!lfp->lu_compl_vlr_fsm)
772 return;
773 osmo_fsm_inst_term(lfp->lu_compl_vlr_fsm, OSMO_FSM_TERM_PARENT, NULL);
774}
775
776/* 4.1.2.1 Node 4 */
777static void vlr_loc_upd_node_4(struct osmo_fsm_inst *fi)
778{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100779 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200780 struct vlr_subscr *vsub = lfp->vsub;
781 bool hlr_unknown = false;
782
783 LOGPFSM(fi, "%s()\n", __func__);
784
785 if (hlr_unknown) {
786 /* FIXME: Delete subscriber record */
787 /* LU REJ: Roaming not allowed */
788 lu_fsm_failure(fi, GSM48_REJECT_ROAMING_NOT_ALLOWED);
789 } else {
790 /* Update_HLR_VLR */
791 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_HLR_UPD,
792 LU_TIMEOUT_LONG, 0);
793 lfp->upd_hlr_vlr_fsm =
794 upd_hlr_vlr_proc_start(fi, vsub, VLR_ULA_E_UPD_HLR_COMPL);
795 }
796}
797
798/* 4.1.2.1 Node B */
799static void vlr_loc_upd_node_b(struct osmo_fsm_inst *fi)
800{
801 LOGPFSM(fi, "%s()\n", __func__);
802
Neels Hofmeyref9126c2017-07-18 15:38:39 +0200803 /* OsmoHLR does not support PgA, neither stores the IMEISV, so we have no need to update the HLR
804 * with either. TODO: depend on actual HLR configuration. See 3GPP TS 23.012 Release 14, process
805 * Update_Location_Area_VLR (ULA_VLR2). */
Harald Welteb8b85a12016-06-17 00:06:42 +0200806 if (0) { /* IMEISV or PgA to send */
807 vlr_loc_upd_node_4(fi);
808 } else {
809 /* Location_Update_Completion */
810 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_LU_COMPL,
811 LU_TIMEOUT_LONG, 0);
812 vlr_loc_upd_start_lu_compl_fsm(fi);
813 }
814}
815
816/* Non-standard: after Ciphering Mode Complete (or no ciph required) */
817static void vlr_loc_upd_post_ciph(struct osmo_fsm_inst *fi)
818{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100819 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200820 struct vlr_subscr *vsub = lfp->vsub;
Harald Welte544a32f2020-06-21 22:15:53 +0200821 int rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200822
823 LOGPFSM(fi, "%s()\n", __func__);
824
825 OSMO_ASSERT(vsub);
826
Harald Welte544a32f2020-06-21 22:15:53 +0200827 rc = lfp->vlr->ops.tx_common_id(lfp->msc_conn_ref);
828 if (rc)
829 LOGPFSML(fi, LOGL_ERROR, "Error while sending Common ID (%d)\n", rc);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200830
Harald Welteb8b85a12016-06-17 00:06:42 +0200831 vsub->conf_by_radio_contact_ind = true;
832 /* Update LAI */
833 vsub->cgi.lai = lfp->new_lai;
834 vsub->dormant_ind = false;
835 vsub->cancel_loc_rx = false;
836 if (hlr_update_needed(vsub)) {
837 vlr_loc_upd_node_4(fi);
838 } else {
839 /* TODO: ADD Support */
840 /* TODO: Node A: PgA Support */
841 vlr_loc_upd_node_b(fi);
842 }
843}
844
845/* 4.1.2.1 after Authentication successful (or no auth rqd) */
846static void vlr_loc_upd_post_auth(struct osmo_fsm_inst *fi)
847{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100848 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200849 struct vlr_subscr *vsub = lfp->vsub;
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100850 bool umts_aka;
Harald Welteb8b85a12016-06-17 00:06:42 +0200851
852 LOGPFSM(fi, "%s()\n", __func__);
853
854 OSMO_ASSERT(vsub);
855
856 if (!is_ciph_required(lfp)) {
857 vlr_loc_upd_post_ciph(fi);
858 return;
859 }
860
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +0100861 if (!vsub->last_tuple) {
862 LOGPFSML(fi, LOGL_ERROR, "No auth tuple available\n");
Neels Hofmeyrd2278ec2018-03-02 02:44:05 +0100863 lu_fsm_failure(fi, GSM48_REJECT_NETWORK_FAILURE);
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +0100864 return;
865 }
866
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100867 switch (vsub->sec_ctx) {
868 case VLR_SEC_CTX_GSM:
869 umts_aka = false;
870 break;
871 case VLR_SEC_CTX_UMTS:
872 umts_aka = true;
873 break;
874 default:
875 LOGPFSML(fi, LOGL_ERROR, "Cannot start ciphering, security context is not established\n");
876 lu_fsm_failure(fi, GSM48_REJECT_NETWORK_FAILURE);
877 return;
878 }
879
Harald Welteb8b85a12016-06-17 00:06:42 +0200880 if (vlr_set_ciph_mode(vsub->vlr, fi, lfp->msc_conn_ref,
881 lfp->ciphering_required,
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100882 umts_aka,
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200883 vsub->vlr->cfg.retrieve_imeisv_ciphered)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200884 LOGPFSML(fi, LOGL_ERROR,
885 "Failed to send Ciphering Mode Command\n");
Neels Hofmeyrd2278ec2018-03-02 02:44:05 +0100886 lu_fsm_failure(fi, GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200887 return;
888 }
889
890 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_CIPH, LU_TIMEOUT_LONG, 0);
891}
892
893static void vlr_loc_upd_node1(struct osmo_fsm_inst *fi)
894{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100895 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200896 struct vlr_subscr *vsub = lfp->vsub;
897
898 LOGPFSM(fi, "%s()\n", __func__);
899
900 OSMO_ASSERT(vsub);
901
902 if (is_auth_required(lfp)) {
903 /* Authenticate_VLR */
904 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_AUTH,
905 LU_TIMEOUT_LONG, 0);
Vadim Yanitskiy3daf0c22020-01-25 06:02:48 +0700906 vsub->auth_fsm = auth_fsm_start(lfp->vsub,
Harald Welteb8b85a12016-06-17 00:06:42 +0200907 fi, VLR_ULA_E_AUTH_RES,
908 lfp->is_r99,
909 lfp->is_utran);
910 } else {
911 /* no need for authentication */
912 vlr_loc_upd_post_auth(fi);
913 }
914}
915
Oliver Smithcbf2c932019-05-06 13:09:55 +0200916static void vlr_loc_upd_node1_pre(struct osmo_fsm_inst *fi)
917{
918 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
919 struct vlr_instance *vlr = lfp->vlr;
920
921 LOGPFSM(fi, "%s()\n", __func__);
922
923 if (vlr->cfg.check_imei_rqd && vlr->cfg.retrieve_imeisv_early) {
924 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_HLR_CHECK_IMEI_EARLY, vlr_timer(lfp->vlr, 3270), 3270);
925 vlr_subscr_tx_req_check_imei(lfp->vsub);
926 } else {
927 vlr_loc_upd_node1(fi);
928 }
929}
930
931/* End of Check_IMEI Procedure. Executed early (before the location update), so we can send the IMEI to the HLR even if
932 * the MS would be rejected in LU. See the "Configuring the Subscribers Create on Demand Feature" section of the OsmoHLR
933 * user manual for a detailed explanation of the use case. */
934static void lu_fsm_wait_hlr_check_imei_early(struct osmo_fsm_inst *fi, uint32_t event, void *data)
935{
936 switch (event) {
937 case VLR_ULA_E_HLR_IMEI_ACK:
938 vlr_loc_upd_node1(fi);
939 break;
940 case VLR_ULA_E_HLR_IMEI_NACK:
941 lu_fsm_failure(fi, GSM48_REJECT_ILLEGAL_ME);
942 break;
943 default:
944 OSMO_ASSERT(0);
945 break;
946 }
947}
948
Harald Welteb8b85a12016-06-17 00:06:42 +0200949static void vlr_loc_upd_want_imsi(struct osmo_fsm_inst *fi)
950{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100951 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200952 struct vlr_instance *vlr = lfp->vlr;
953
954 LOGPFSM(fi, "%s()\n", __func__);
955
956 OSMO_ASSERT(lfp->vsub);
957
958 /* Obtain_IMSI_VLR */
959 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_IMSI,
960 vlr_timer(vlr, 3270), 3270);
961 vlr->ops.tx_id_req(lfp->msc_conn_ref, GSM_MI_TYPE_IMSI);
Oliver Smithcbf2c932019-05-06 13:09:55 +0200962 /* will continue at vlr_loc_upd_node1_pre() once IMSI arrives */
Harald Welteb8b85a12016-06-17 00:06:42 +0200963}
964
965static int assoc_lfp_with_sub(struct osmo_fsm_inst *fi, struct vlr_subscr *vsub)
966{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100967 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200968 struct vlr_instance *vlr = lfp->vlr;
969
970 if (vsub->lu_fsm) {
971 LOGPFSML(fi, LOGL_ERROR,
972 "A Location Updating process is already pending for"
973 " this subscriber. Aborting.\n");
974 /* Also get rid of the other pending LU attempt? */
975 /*lu_fsm_failure(vsub->lu_fsm, GSM48_REJECT_CONGESTION);*/
976 lu_fsm_failure(fi, GSM48_REJECT_CONGESTION);
977 return -EINVAL;
978 }
979 vsub->lu_fsm = fi;
980 vsub->msc_conn_ref = lfp->msc_conn_ref;
981 /* FIXME: send new LAC to HLR? */
Max7d41d872018-12-19 11:48:33 +0100982 vsub->cgi.lai.lac = lfp->new_lai.lac;
Harald Welteb8b85a12016-06-17 00:06:42 +0200983 lfp->vsub = vsub;
984 /* Tell MSC to associate this subscriber with the given
985 * connection */
Neels Hofmeyr1035d902018-12-28 21:22:32 +0100986 if (vlr->ops.subscr_assoc(lfp->msc_conn_ref, lfp->vsub))
987 lu_fsm_failure(fi, GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200988 return 0;
989}
990
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200991static int _lu_fsm_associate_vsub(struct osmo_fsm_inst *fi)
992{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100993 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200994 struct vlr_instance *vlr = lfp->vlr;
995 struct vlr_subscr *vsub = NULL;
996
997 if (!lfp->imsi[0]) {
998 /* TMSI was used */
999 lfp->lu_by_tmsi = true;
1000 /* TMSI clash: if a different subscriber already has this TMSI,
1001 * we will find that other subscriber in the VLR. So the IMSIs
1002 * would mismatch, but we don't know about it. Theoretically,
1003 * an authentication process would thwart any attempt to use
1004 * someone else's TMSI.
1005 * TODO: Otherwise we can ask for the IMSI and verify that it
1006 * matches the IMSI on record. */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001007 vsub = vlr_subscr_find_or_create_by_tmsi(vlr, lfp->tmsi, __func__, NULL);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001008
1009 if (!vsub) {
1010 LOGPFSML(fi, LOGL_ERROR, "VLR subscriber allocation failed\n");
1011 lu_fsm_failure(fi, GSM48_REJECT_SRV_OPT_TMP_OUT_OF_ORDER);
1012 return -1;
1013 }
1014
1015 vsub->sub_dataconf_by_hlr_ind = false;
1016 if (assoc_lfp_with_sub(fi, vsub)) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001017 vlr_subscr_put(vsub, __func__);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001018 return -1; /* error, fsm failure invoked in assoc_lfp_with_sub() */
1019 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001020 vlr_subscr_put(vsub, __func__);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001021 } else {
1022 /* IMSI was used */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001023 vsub = vlr_subscr_find_or_create_by_imsi(vlr, lfp->imsi, __func__, NULL);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001024
1025 if (!vsub) {
1026 LOGPFSML(fi, LOGL_ERROR, "VLR subscriber allocation failed\n");
1027 lu_fsm_failure(fi, GSM48_REJECT_SRV_OPT_TMP_OUT_OF_ORDER);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001028 vlr_subscr_put(vsub, __func__);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001029 return -1;
1030 }
1031
1032 vsub->sub_dataconf_by_hlr_ind = false;
1033 if (assoc_lfp_with_sub(fi, vsub)) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001034 vlr_subscr_put(vsub, __func__);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001035 return -1; /* error, fsm failure invoked in assoc_lfp_with_sub() */
1036 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001037 vlr_subscr_put(vsub, __func__);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001038 }
1039 return 0;
1040}
1041
Harald Welteb8b85a12016-06-17 00:06:42 +02001042/* 4.1.2.1: Subscriber (via MSC/SGSN) requests location update */
1043static void _start_lu_main(struct osmo_fsm_inst *fi)
1044{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001045 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001046 struct vlr_instance *vlr = lfp->vlr;
Harald Welteb8b85a12016-06-17 00:06:42 +02001047
1048 /* TODO: PUESBINE related handling */
1049
1050 /* Is previous LAI in this VLR? */
1051 if (!lai_in_this_vlr(vlr, &lfp->old_lai)) {
1052#if 0
1053 /* FIXME: check previous VLR, (3) */
1054 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_PVLR,
1055 LU_TIMEOUT_LONG, 0);
1056 return;
1057#endif
1058 LOGPFSML(fi, LOGL_NOTICE, "LAI change from %s,"
1059 " but checking previous VLR not implemented\n",
Neels Hofmeyr379d5792018-02-22 04:04:54 +01001060 osmo_lai_name(&lfp->old_lai));
Harald Welteb8b85a12016-06-17 00:06:42 +02001061 }
1062
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001063 /* If this is a TMSI based LU, we may not have the IMSI. Make sure that
1064 * we know the IMSI, either on record, or request it. */
1065 if (!lfp->vsub->imsi[0])
1066 vlr_loc_upd_want_imsi(fi);
1067 else
Oliver Smithcbf2c932019-05-06 13:09:55 +02001068 vlr_loc_upd_node1_pre(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001069}
1070
Harald Welteb8b85a12016-06-17 00:06:42 +02001071static void lu_fsm_idle(struct osmo_fsm_inst *fi, uint32_t event,
1072 void *data)
1073{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001074 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001075 struct vlr_instance *vlr = lfp->vlr;
1076
1077 OSMO_ASSERT(event == VLR_ULA_E_UPDATE_LA);
1078
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001079 if (_lu_fsm_associate_vsub(fi))
1080 return; /* error. FSM already terminated. */
1081
1082 OSMO_ASSERT(lfp->vsub);
1083
Harald Welte0df904d2018-12-03 11:00:04 +01001084 /* At this point we know for which subscriber the location update is,
1085 * we now must inform SGs-UE FSM that we received a location update
1086 * via A, IU or Gs interface. */
1087 osmo_fsm_inst_dispatch(lfp->vsub->sgs_fsm, SGS_UE_E_RX_LU_FROM_A_IU_GS, NULL);
1088
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001089 /* See 3GPP TS 23.012, procedure Retrieve_IMEISV_If_Required */
1090 if ((!vlr->cfg.retrieve_imeisv_early)
1091 || (lfp->type == VLR_LU_TYPE_PERIODIC && lfp->vsub->imeisv[0])) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001092 /* R_IMEISV_IR1 passed */
1093 _start_lu_main(fi);
1094 } else {
1095 vlr->ops.tx_id_req(lfp->msc_conn_ref, GSM_MI_TYPE_IMEISV);
1096 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_IMEISV,
1097 vlr_timer(vlr, 3270), 3270);
1098 }
1099}
1100
1101static void lu_fsm_wait_imeisv(struct osmo_fsm_inst *fi, uint32_t event,
1102 void *data)
1103{
1104 switch (event) {
1105 case VLR_ULA_E_ID_IMEISV:
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001106 /* IMEISV was copied in vlr_subscr_rx_id_resp(), and that's
1107 * where we received this event from. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001108 _start_lu_main(fi);
1109 break;
1110 default:
Oliver Smithffd522e2019-05-10 14:39:37 +02001111 OSMO_ASSERT(0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001112 break;
1113 }
1114}
1115
1116/* Wait for response from Send_Identification to PVLR */
1117static void lu_fsm_wait_pvlr(struct osmo_fsm_inst *fi, uint32_t event,
1118 void *data)
1119{
1120 switch (event) {
1121 case VLR_ULA_E_SEND_ID_ACK:
Oliver Smithcbf2c932019-05-06 13:09:55 +02001122 vlr_loc_upd_node1_pre(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001123 break;
1124 case VLR_ULA_E_SEND_ID_NACK:
1125 vlr_loc_upd_want_imsi(fi);
1126 break;
1127 default:
Oliver Smithffd522e2019-05-10 14:39:37 +02001128 OSMO_ASSERT(0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001129 break;
1130 }
1131}
1132
1133/* Wait for result of Authenticate_VLR procedure */
1134static void lu_fsm_wait_auth(struct osmo_fsm_inst *fi, uint32_t event,
1135 void *data)
1136{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001137 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Neels Hofmeyr15809592018-04-06 02:57:51 +02001138 enum gsm48_reject_value *res = data;
Harald Welteb8b85a12016-06-17 00:06:42 +02001139
1140 OSMO_ASSERT(event == VLR_ULA_E_AUTH_RES);
1141
1142 lfp->upd_hlr_vlr_fsm = NULL;
1143
Neels Hofmeyr15809592018-04-06 02:57:51 +02001144 if (!res || *res) {
1145 lu_fsm_failure(fi, res? *res : GSM48_REJECT_NETWORK_FAILURE);
1146 return;
1147 }
Harald Welteb8b85a12016-06-17 00:06:42 +02001148
Neels Hofmeyr15809592018-04-06 02:57:51 +02001149 /* Result == Pass */
1150 vlr_loc_upd_post_auth(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001151}
1152
1153static void lu_fsm_wait_ciph(struct osmo_fsm_inst *fi, uint32_t event,
1154 void *data)
1155{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001156 enum vlr_ciph_result_cause result = VLR_CIPH_REJECT;
Harald Welteb8b85a12016-06-17 00:06:42 +02001157
1158 OSMO_ASSERT(event == VLR_ULA_E_CIPH_RES);
1159
1160 if (!data)
1161 LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: NULL\n");
1162 else
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001163 result = *(enum vlr_ciph_result_cause*)data;
Harald Welteb8b85a12016-06-17 00:06:42 +02001164
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001165 switch (result) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001166 case VLR_CIPH_COMPL:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001167 vlr_loc_upd_post_ciph(fi);
1168 return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001169 case VLR_CIPH_REJECT:
1170 LOGPFSM(fi, "ciphering rejected\n");
1171 lu_fsm_failure(fi, GSM48_REJECT_INVALID_MANDANTORY_INF);
1172 return;
1173 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001174 LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: %d\n", result);
Harald Welteb8b85a12016-06-17 00:06:42 +02001175 lu_fsm_failure(fi, GSM48_REJECT_INVALID_MANDANTORY_INF);
1176 return;
1177 }
Harald Welteb8b85a12016-06-17 00:06:42 +02001178}
1179
1180static void lu_fsm_wait_imsi(struct osmo_fsm_inst *fi, uint32_t event,
1181 void *data)
1182{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001183 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001184 struct vlr_subscr *vsub = lfp->vsub;
1185 char *mi_string = data;
1186
1187 switch (event) {
1188 case VLR_ULA_E_ID_IMSI:
1189 vlr_subscr_set_imsi(vsub, mi_string);
Oliver Smithcbf2c932019-05-06 13:09:55 +02001190 vlr_loc_upd_node1_pre(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001191 break;
1192 default:
Oliver Smithffd522e2019-05-10 14:39:37 +02001193 OSMO_ASSERT(0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001194 break;
1195 }
1196}
1197
1198/* At the end of Update_HLR_VLR */
1199static void lu_fsm_wait_hlr_ul_res(struct osmo_fsm_inst *fi, uint32_t event,
1200 void *data)
1201{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001202 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001203
1204 switch (event) {
1205 case VLR_ULA_E_HLR_LU_RES:
1206 /* pass-through this event to Update_HLR_VLR */
1207 if (data == NULL)
1208 osmo_fsm_inst_dispatch(lfp->upd_hlr_vlr_fsm, UPD_HLR_VLR_E_UPD_LOC_ACK, NULL);
1209 else
1210 osmo_fsm_inst_dispatch(lfp->upd_hlr_vlr_fsm, UPD_HLR_VLR_E_UPD_LOC_NACK, data);
1211 break;
1212 case VLR_ULA_E_UPD_HLR_COMPL:
1213 if (data == NULL) {
1214 /* successful case */
1215 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_LU_COMPL,
1216 LU_TIMEOUT_LONG, 0);
1217 vlr_loc_upd_start_lu_compl_fsm(fi);
1218 /* continue in MSC ?!? */
1219 } else {
1220 /* unsuccessful case */
Neels Hofmeyrdd2aeba2018-10-30 19:47:01 +01001221 enum gsm48_reject_value cause =
1222 *(enum gsm48_reject_value *)data;
Neels Hofmeyref9126c2017-07-18 15:38:39 +02001223 /* Ignoring standalone mode for now. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001224 if (0 /* procedure_error && vlr->cfg.standalone_mode */) {
1225 osmo_fsm_inst_state_chg(fi,
1226 VLR_ULA_S_WAIT_LU_COMPL_STANDALONE,
1227 LU_TIMEOUT_LONG, 0);
1228 vlr_loc_upd_start_lu_compl_fsm(fi);
1229 } else {
1230 lu_fsm_failure(fi, cause);
1231 }
1232 }
1233 break;
Neels Hofmeyr364f9272019-08-22 17:20:14 +02001234 case VLR_ULA_E_ID_IMEI:
1235 case VLR_ULA_E_ID_IMEISV:
1236 /* Got the IMEI from ME, nothing to do right now though. */
1237 break;
Harald Welteb8b85a12016-06-17 00:06:42 +02001238 default:
Oliver Smithffd522e2019-05-10 14:39:37 +02001239 OSMO_ASSERT(0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001240 break;
1241 }
1242}
1243
1244/* Wait for end of Location_Update_Completion_VLR */
1245static void lu_fsm_wait_lu_compl(struct osmo_fsm_inst *fi, uint32_t event,
1246 void *data)
1247{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001248 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001249 uint8_t cause;
1250
1251 switch (event) {
1252 case VLR_ULA_E_NEW_TMSI_ACK:
1253 osmo_fsm_inst_dispatch(lfp->lu_compl_vlr_fsm,
1254 LU_COMPL_VLR_E_NEW_TMSI_ACK, NULL);
1255 break;
1256 case VLR_ULA_E_ID_IMEI:
Neels Hofmeyr106ba522019-08-08 02:00:54 +02001257 case VLR_ULA_E_ID_IMEISV:
Oliver Smith7d053092018-12-14 17:37:38 +01001258 /* Got the IMEI from ME, now send it to HLR */
1259 vlr_subscr_tx_req_check_imei(lfp->vsub);
1260 break;
1261 case VLR_ULA_E_HLR_IMEI_ACK:
Harald Welteb8b85a12016-06-17 00:06:42 +02001262 osmo_fsm_inst_dispatch(lfp->lu_compl_vlr_fsm,
1263 LU_COMPL_VLR_E_IMEI_CHECK_ACK, NULL);
1264 break;
Oliver Smith7d053092018-12-14 17:37:38 +01001265 case VLR_ULA_E_HLR_IMEI_NACK:
1266 osmo_fsm_inst_dispatch(lfp->lu_compl_vlr_fsm,
1267 LU_COMPL_VLR_E_IMEI_CHECK_NACK, NULL);
1268 break;
Harald Welteb8b85a12016-06-17 00:06:42 +02001269 case VLR_ULA_E_LU_COMPL_SUCCESS:
1270 lu_fsm_discard_lu_compl_fsm(fi);
1271
1272 /* Update Register */
1273 /* TODO: Set_Notification_Type 23.078 */
1274 /* TODO: Notify_gsmSCF 23.078 */
1275 /* TODO: Authenticated Radio Contact Established -> ARC */
Stefan Sperling3a741282018-03-13 21:11:49 +01001276
1277 if (lfp->type == VLR_LU_TYPE_IMSI_ATTACH)
1278 lfp->vlr->ops.tx_mm_info(lfp->msc_conn_ref);
1279
Harald Welteb8b85a12016-06-17 00:06:42 +02001280 lu_fsm_success(fi);
1281 break;
1282 case VLR_ULA_E_LU_COMPL_FAILURE:
1283 cause = GSM48_REJECT_NETWORK_FAILURE;
1284 if (data)
1285 cause = *(uint8_t*)data;
1286 lu_fsm_discard_lu_compl_fsm(fi);
1287 lu_fsm_failure(fi, cause);
1288 break;
1289 default:
Oliver Smithffd522e2019-05-10 14:39:37 +02001290 OSMO_ASSERT(0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001291 break;
1292 }
1293}
1294
1295/* Wait for end of Location_Update_Completion_VLR (standalone case) */
1296static void lu_fsm_wait_lu_compl_standalone(struct osmo_fsm_inst *fi,
1297 uint32_t event, void *data)
1298{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001299 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001300 struct vlr_subscr *vsub = lfp->vsub;
1301 uint8_t cause;
1302
1303 switch (event) {
1304 case VLR_ULA_E_NEW_TMSI_ACK:
1305 osmo_fsm_inst_dispatch(lfp->lu_compl_vlr_fsm,
1306 LU_COMPL_VLR_E_NEW_TMSI_ACK, NULL);
1307 break;
1308 case VLR_ULA_E_LU_COMPL_SUCCESS:
1309 lu_fsm_discard_lu_compl_fsm(fi);
1310 vsub->sub_dataconf_by_hlr_ind = false;
1311 lu_fsm_success(fi);
1312 break;
1313 case VLR_ULA_E_LU_COMPL_FAILURE:
1314 vsub->sub_dataconf_by_hlr_ind = false;
1315 cause = GSM48_REJECT_NETWORK_FAILURE;
1316 if (data)
1317 cause = *(uint8_t*)data;
1318 lu_fsm_discard_lu_compl_fsm(fi);
1319 lu_fsm_failure(fi, cause);
1320 break;
1321 default:
Oliver Smithffd522e2019-05-10 14:39:37 +02001322 OSMO_ASSERT(0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001323 break;
1324 }
1325}
1326
1327static const struct osmo_fsm_state vlr_lu_fsm_states[] = {
1328 [VLR_ULA_S_IDLE] = {
1329 .in_event_mask = S(VLR_ULA_E_UPDATE_LA),
1330 .out_state_mask = S(VLR_ULA_S_WAIT_IMEISV) |
1331 S(VLR_ULA_S_WAIT_PVLR) |
1332 S(VLR_ULA_S_WAIT_IMSI) |
1333 S(VLR_ULA_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +01001334 S(VLR_ULA_S_WAIT_CIPH) |
Oliver Smithcbf2c932019-05-06 13:09:55 +02001335 S(VLR_ULA_S_WAIT_HLR_CHECK_IMEI_EARLY) |
Harald Welteb8b85a12016-06-17 00:06:42 +02001336 S(VLR_ULA_S_WAIT_HLR_UPD) |
1337 S(VLR_ULA_S_DONE),
1338 .name = OSMO_STRINGIFY(VLR_ULA_S_IDLE),
1339 .action = lu_fsm_idle,
1340 },
1341 [VLR_ULA_S_WAIT_IMEISV] = {
1342 .in_event_mask = S(VLR_ULA_E_ID_IMEISV),
1343 .out_state_mask = S(VLR_ULA_S_WAIT_PVLR) |
1344 S(VLR_ULA_S_WAIT_IMSI) |
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001345 S(VLR_ULA_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +01001346 S(VLR_ULA_S_WAIT_CIPH) |
Oliver Smithcbf2c932019-05-06 13:09:55 +02001347 S(VLR_ULA_S_WAIT_HLR_CHECK_IMEI_EARLY) |
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001348 S(VLR_ULA_S_WAIT_HLR_UPD) |
Harald Welteb8b85a12016-06-17 00:06:42 +02001349 S(VLR_ULA_S_DONE),
1350 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_IMEISV),
1351 .action = lu_fsm_wait_imeisv,
1352 },
1353 [VLR_ULA_S_WAIT_PVLR] = {
1354 .in_event_mask = S(VLR_ULA_E_SEND_ID_ACK) |
1355 S(VLR_ULA_E_SEND_ID_NACK),
1356 .out_state_mask = S(VLR_ULA_S_WAIT_IMSI) |
1357 S(VLR_ULA_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +01001358 S(VLR_ULA_S_WAIT_CIPH) |
Oliver Smithcbf2c932019-05-06 13:09:55 +02001359 S(VLR_ULA_S_WAIT_HLR_CHECK_IMEI_EARLY) |
Harald Welteb8b85a12016-06-17 00:06:42 +02001360 S(VLR_ULA_S_DONE),
1361 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_PVLR),
1362 .action = lu_fsm_wait_pvlr,
1363 },
1364 [VLR_ULA_S_WAIT_AUTH] = {
1365 .in_event_mask = S(VLR_ULA_E_AUTH_RES),
1366 .out_state_mask = S(VLR_ULA_S_WAIT_CIPH) |
1367 S(VLR_ULA_S_WAIT_LU_COMPL) |
1368 S(VLR_ULA_S_WAIT_HLR_UPD) |
1369 S(VLR_ULA_S_DONE),
1370 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_AUTH),
1371 .action = lu_fsm_wait_auth,
1372 },
1373 [VLR_ULA_S_WAIT_CIPH] = {
1374 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_CIPH),
1375 .in_event_mask = S(VLR_ULA_E_CIPH_RES),
1376 .out_state_mask = S(VLR_ULA_S_WAIT_LU_COMPL) |
1377 S(VLR_ULA_S_WAIT_HLR_UPD) |
1378 S(VLR_ULA_S_DONE),
1379 .action = lu_fsm_wait_ciph,
1380 },
1381 [VLR_ULA_S_WAIT_IMSI] = {
1382 .in_event_mask = S(VLR_ULA_E_ID_IMSI),
1383 .out_state_mask = S(VLR_ULA_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +01001384 S(VLR_ULA_S_WAIT_CIPH) |
Oliver Smithcbf2c932019-05-06 13:09:55 +02001385 S(VLR_ULA_S_WAIT_HLR_CHECK_IMEI_EARLY) |
Harald Welteb8b85a12016-06-17 00:06:42 +02001386 S(VLR_ULA_S_WAIT_HLR_UPD) |
1387 S(VLR_ULA_S_DONE),
1388 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_IMSI),
1389 .action = lu_fsm_wait_imsi,
1390 },
Oliver Smithcbf2c932019-05-06 13:09:55 +02001391 [VLR_ULA_S_WAIT_HLR_CHECK_IMEI_EARLY] = {
1392 .in_event_mask = S(VLR_ULA_E_HLR_IMEI_ACK) |
1393 S(VLR_ULA_E_HLR_IMEI_NACK),
1394 .out_state_mask = S(VLR_ULA_S_WAIT_AUTH) |
1395 S(VLR_ULA_S_WAIT_CIPH) |
1396 S(VLR_ULA_S_WAIT_HLR_UPD) |
1397 S(VLR_ULA_S_WAIT_LU_COMPL) |
1398 S(VLR_ULA_S_DONE),
1399 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_HLR_CHECK_IMEI_EARLY),
1400 .action = lu_fsm_wait_hlr_check_imei_early,
1401 },
Harald Welteb8b85a12016-06-17 00:06:42 +02001402 [VLR_ULA_S_WAIT_HLR_UPD] = {
1403 .in_event_mask = S(VLR_ULA_E_HLR_LU_RES) |
Neels Hofmeyr364f9272019-08-22 17:20:14 +02001404 S(VLR_ULA_E_UPD_HLR_COMPL) |
1405 S(VLR_ULA_E_ID_IMEI) |
1406 S(VLR_ULA_E_ID_IMEISV),
Harald Welteb8b85a12016-06-17 00:06:42 +02001407 .out_state_mask = S(VLR_ULA_S_WAIT_LU_COMPL) |
1408 S(VLR_ULA_S_WAIT_LU_COMPL_STANDALONE) |
1409 S(VLR_ULA_S_DONE),
1410 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_HLR_UPD),
1411 .action = lu_fsm_wait_hlr_ul_res,
1412 },
1413 [VLR_ULA_S_WAIT_LU_COMPL] = {
1414 .in_event_mask = S(VLR_ULA_E_LU_COMPL_SUCCESS) |
1415 S(VLR_ULA_E_LU_COMPL_FAILURE) |
1416 S(VLR_ULA_E_NEW_TMSI_ACK) |
1417 S(VLR_ULA_E_ID_IMEI) |
Oliver Smith7d053092018-12-14 17:37:38 +01001418 S(VLR_ULA_E_ID_IMEISV) |
1419 S(VLR_ULA_E_HLR_IMEI_ACK) |
1420 S(VLR_ULA_E_HLR_IMEI_NACK),
Harald Welteb8b85a12016-06-17 00:06:42 +02001421 .out_state_mask = S(VLR_ULA_S_DONE),
1422 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_LU_COMPL),
1423 .action = lu_fsm_wait_lu_compl,
1424 },
1425 [VLR_ULA_S_WAIT_LU_COMPL_STANDALONE] = {
1426 .in_event_mask = S(VLR_ULA_E_LU_COMPL_SUCCESS) |
1427 S(VLR_ULA_E_LU_COMPL_FAILURE) |
1428 S(VLR_ULA_E_NEW_TMSI_ACK),
1429 .out_state_mask = S(VLR_ULA_S_DONE),
1430 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_LU_COMPL_STANDALONE),
1431 .action = lu_fsm_wait_lu_compl_standalone,
1432 },
1433 [VLR_ULA_S_DONE] = {
1434 .name = OSMO_STRINGIFY(VLR_ULA_S_DONE),
1435 .onenter = lu_fsm_dispatch_result,
1436 },
1437};
1438
1439static void fsm_lu_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
1440{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001441 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001442 struct vlr_subscr *vsub = lfp->vsub;
1443
1444 LOGPFSM(fi, "fsm_lu_cleanup called with cause %s\n",
1445 osmo_fsm_term_cause_name(cause));
1446 if (vsub && vsub->lu_fsm == fi)
1447 vsub->lu_fsm = NULL;
1448}
1449
1450static struct osmo_fsm vlr_lu_fsm = {
1451 .name = "vlr_lu_fsm",
1452 .states = vlr_lu_fsm_states,
1453 .num_states = ARRAY_SIZE(vlr_lu_fsm_states),
1454 .allstate_event_mask = 0,
1455 .allstate_action = NULL,
1456 .log_subsys = DVLR,
1457 .event_names = fsm_lu_event_names,
1458 .cleanup = fsm_lu_cleanup,
1459};
1460
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001461static inline struct lu_fsm_priv *lu_fsm_fi_priv(struct osmo_fsm_inst *fi)
1462{
1463 OSMO_ASSERT(fi->fsm == &vlr_lu_fsm);
1464 return (struct lu_fsm_priv*)fi->priv;
1465}
1466
Harald Welteb8b85a12016-06-17 00:06:42 +02001467struct osmo_fsm_inst *
1468vlr_loc_update(struct osmo_fsm_inst *parent,
1469 uint32_t parent_event_success,
1470 uint32_t parent_event_failure,
1471 void *parent_event_data,
1472 struct vlr_instance *vlr, void *msc_conn_ref,
1473 enum vlr_lu_type type, uint32_t tmsi, const char *imsi,
1474 const struct osmo_location_area_id *old_lai,
1475 const struct osmo_location_area_id *new_lai,
1476 bool authentication_required,
Harald Welte71c51df2017-12-23 18:51:48 +01001477 bool ciphering_required,
Sylvain Munautda9f37e2019-03-14 11:02:36 +01001478 uint8_t key_seq,
Harald Welteb8b85a12016-06-17 00:06:42 +02001479 bool is_r99, bool is_utran,
1480 bool assign_tmsi)
1481{
1482 struct osmo_fsm_inst *fi;
1483 struct lu_fsm_priv *lfp;
1484
1485 fi = osmo_fsm_inst_alloc_child(&vlr_lu_fsm, parent, parent_event_failure);
1486 if (!fi)
1487 return NULL;
1488
1489 lfp = talloc_zero(fi, struct lu_fsm_priv);
1490 lfp->vlr = vlr;
1491 lfp->msc_conn_ref = msc_conn_ref;
1492 lfp->tmsi = tmsi;
1493 lfp->type = type;
1494 lfp->old_lai = *old_lai;
1495 lfp->new_lai = *new_lai;
1496 lfp->lu_by_tmsi = true;
1497 lfp->parent_event_success = parent_event_success;
1498 lfp->parent_event_failure = parent_event_failure;
1499 lfp->parent_event_data = parent_event_data;
1500 lfp->authentication_required = authentication_required;
1501 lfp->ciphering_required = ciphering_required;
Sylvain Munautda9f37e2019-03-14 11:02:36 +01001502 lfp->key_seq = key_seq;
Harald Welteb8b85a12016-06-17 00:06:42 +02001503 lfp->is_r99 = is_r99;
1504 lfp->is_utran = is_utran;
1505 lfp->assign_tmsi = assign_tmsi;
1506 if (imsi) {
1507 strncpy(lfp->imsi, imsi, sizeof(lfp->imsi)-1);
1508 lfp->imsi[sizeof(lfp->imsi)-1] = '\0';
1509 lfp->lu_by_tmsi = false;
1510 }
1511 fi->priv = lfp;
1512
1513 LOGPFSM(fi, "rev=%s net=%s%s%s\n",
1514 is_r99 ? "R99" : "GSM",
1515 is_utran ? "UTRAN" : "GERAN",
1516 (authentication_required || ciphering_required)?
1517 " Auth" : " (no Auth)",
1518 (authentication_required || ciphering_required)?
1519 (ciphering_required? "+Ciph" : " (no Ciph)")
1520 : "");
1521
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001522 if (is_utran && !authentication_required)
1523 LOGPFSML(fi, LOGL_ERROR,
1524 "Authentication off on UTRAN network. Good luck.\n");
1525
Harald Welteb8b85a12016-06-17 00:06:42 +02001526 osmo_fsm_inst_dispatch(fi, VLR_ULA_E_UPDATE_LA, NULL);
1527
1528 return fi;
1529}
1530
Neels Hofmeyr15809592018-04-06 02:57:51 +02001531void vlr_loc_update_cancel(struct osmo_fsm_inst *fi,
1532 enum osmo_fsm_term_cause fsm_cause,
1533 uint8_t gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +02001534{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001535 struct lu_fsm_priv *lfp;
1536
1537 OSMO_ASSERT(fi);
1538 OSMO_ASSERT(fi->fsm == &vlr_lu_fsm);
1539
1540 lfp = fi->priv;
1541 lfp->rej_cause = gsm48_cause;
1542
1543 if (fi->state != VLR_ULA_S_DONE)
1544 lu_fsm_failure(fi, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +02001545}
1546
1547void vlr_lu_fsm_init(void)
1548{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001549 OSMO_ASSERT(osmo_fsm_register(&vlr_lu_fsm) == 0);
1550 OSMO_ASSERT(osmo_fsm_register(&upd_hlr_vlr_fsm) == 0);
1551 OSMO_ASSERT(osmo_fsm_register(&sub_pres_vlr_fsm) == 0);
1552 OSMO_ASSERT(osmo_fsm_register(&lu_compl_vlr_fsm) == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001553}