blob: 4f603b3ca4a840d29966cf23e3c6172957ed2258 [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
Vadim Yanitskiy565ea2b2021-11-28 16:42:58 +0300705/* Determine if sending of CMC/SMC is required */
706static bool is_cmc_smc_required(struct lu_fsm_priv *lfp)
Harald Welteb8b85a12016-06-17 00:06:42 +0200707{
Vadim Yanitskiy565ea2b2021-11-28 16:42:58 +0300708 /* UTRAN: always send SecModeCmd, even if ciphering is not required.
709 * GERAN: avoid sending CiphModeCmd if ciphering is not required. */
710 return lfp->is_utran || lfp->ciphering_required;
Harald Welteb8b85a12016-06-17 00:06:42 +0200711}
712
713/* Determine if a HLR Update is required */
714static bool hlr_update_needed(struct vlr_subscr *vsub)
715{
716 /* TODO: properly decide this, rather than always assuming we
717 * need to update the HLR. */
718 return true;
719}
720
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100721static inline struct lu_fsm_priv *lu_fsm_fi_priv(struct osmo_fsm_inst *fi);
722
Harald Welteb8b85a12016-06-17 00:06:42 +0200723static void lu_fsm_dispatch_result(struct osmo_fsm_inst *fi,
724 uint32_t prev_state)
725{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100726 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200727 if (!fi->proc.parent) {
728 LOGPFSML(fi, LOGL_ERROR, "No parent FSM\n");
729 return;
730 }
731 osmo_fsm_inst_dispatch(fi->proc.parent,
732 (lfp->result == VLR_FSM_RESULT_SUCCESS)
733 ? lfp->parent_event_success
734 : lfp->parent_event_failure,
735 lfp->parent_event_data);
736}
737
738static void _lu_fsm_done(struct osmo_fsm_inst *fi,
739 enum vlr_fsm_result result)
740{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100741 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200742 lfp->result = result;
743 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_DONE, 0, 0);
744}
745
746static void lu_fsm_success(struct osmo_fsm_inst *fi)
747{
748 _lu_fsm_done(fi, VLR_FSM_RESULT_SUCCESS);
749}
750
Neels Hofmeyr15809592018-04-06 02:57:51 +0200751static void lu_fsm_failure(struct osmo_fsm_inst *fi, enum gsm48_reject_value rej_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +0200752{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100753 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200754 lfp->vlr->ops.tx_lu_rej(lfp->msc_conn_ref, rej_cause ? : GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200755 _lu_fsm_done(fi, VLR_FSM_RESULT_FAILURE);
756}
757
758static void vlr_loc_upd_start_lu_compl_fsm(struct osmo_fsm_inst *fi)
759{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100760 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200761 lfp->lu_compl_vlr_fsm =
762 lu_compl_vlr_proc_alloc(fi, lfp->vsub, lfp->msc_conn_ref,
763 VLR_ULA_E_LU_COMPL_SUCCESS,
764 VLR_ULA_E_LU_COMPL_FAILURE,
765 lfp->assign_tmsi);
766
767 osmo_fsm_inst_dispatch(lfp->lu_compl_vlr_fsm, LU_COMPL_VLR_E_START, NULL);
768}
769
770static void lu_fsm_discard_lu_compl_fsm(struct osmo_fsm_inst *fi)
771{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100772 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200773 if (!lfp->lu_compl_vlr_fsm)
774 return;
775 osmo_fsm_inst_term(lfp->lu_compl_vlr_fsm, OSMO_FSM_TERM_PARENT, NULL);
776}
777
778/* 4.1.2.1 Node 4 */
779static void vlr_loc_upd_node_4(struct osmo_fsm_inst *fi)
780{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100781 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200782 struct vlr_subscr *vsub = lfp->vsub;
783 bool hlr_unknown = false;
784
785 LOGPFSM(fi, "%s()\n", __func__);
786
787 if (hlr_unknown) {
788 /* FIXME: Delete subscriber record */
789 /* LU REJ: Roaming not allowed */
790 lu_fsm_failure(fi, GSM48_REJECT_ROAMING_NOT_ALLOWED);
791 } else {
792 /* Update_HLR_VLR */
793 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_HLR_UPD,
794 LU_TIMEOUT_LONG, 0);
795 lfp->upd_hlr_vlr_fsm =
796 upd_hlr_vlr_proc_start(fi, vsub, VLR_ULA_E_UPD_HLR_COMPL);
797 }
798}
799
800/* 4.1.2.1 Node B */
801static void vlr_loc_upd_node_b(struct osmo_fsm_inst *fi)
802{
803 LOGPFSM(fi, "%s()\n", __func__);
804
Neels Hofmeyref9126c2017-07-18 15:38:39 +0200805 /* OsmoHLR does not support PgA, neither stores the IMEISV, so we have no need to update the HLR
806 * with either. TODO: depend on actual HLR configuration. See 3GPP TS 23.012 Release 14, process
807 * Update_Location_Area_VLR (ULA_VLR2). */
Harald Welteb8b85a12016-06-17 00:06:42 +0200808 if (0) { /* IMEISV or PgA to send */
809 vlr_loc_upd_node_4(fi);
810 } else {
811 /* Location_Update_Completion */
812 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_LU_COMPL,
813 LU_TIMEOUT_LONG, 0);
814 vlr_loc_upd_start_lu_compl_fsm(fi);
815 }
816}
817
818/* Non-standard: after Ciphering Mode Complete (or no ciph required) */
819static void vlr_loc_upd_post_ciph(struct osmo_fsm_inst *fi)
820{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100821 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200822 struct vlr_subscr *vsub = lfp->vsub;
Harald Welte544a32f2020-06-21 22:15:53 +0200823 int rc;
Harald Welteb8b85a12016-06-17 00:06:42 +0200824
825 LOGPFSM(fi, "%s()\n", __func__);
826
827 OSMO_ASSERT(vsub);
828
Harald Welte544a32f2020-06-21 22:15:53 +0200829 rc = lfp->vlr->ops.tx_common_id(lfp->msc_conn_ref);
830 if (rc)
831 LOGPFSML(fi, LOGL_ERROR, "Error while sending Common ID (%d)\n", rc);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200832
Harald Welteb8b85a12016-06-17 00:06:42 +0200833 vsub->conf_by_radio_contact_ind = true;
834 /* Update LAI */
835 vsub->cgi.lai = lfp->new_lai;
836 vsub->dormant_ind = false;
837 vsub->cancel_loc_rx = false;
838 if (hlr_update_needed(vsub)) {
839 vlr_loc_upd_node_4(fi);
840 } else {
841 /* TODO: ADD Support */
842 /* TODO: Node A: PgA Support */
843 vlr_loc_upd_node_b(fi);
844 }
845}
846
847/* 4.1.2.1 after Authentication successful (or no auth rqd) */
848static void vlr_loc_upd_post_auth(struct osmo_fsm_inst *fi)
849{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100850 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200851 struct vlr_subscr *vsub = lfp->vsub;
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100852 bool umts_aka;
Harald Welteb8b85a12016-06-17 00:06:42 +0200853
854 LOGPFSM(fi, "%s()\n", __func__);
855
856 OSMO_ASSERT(vsub);
857
Vadim Yanitskiy565ea2b2021-11-28 16:42:58 +0300858 if (!is_cmc_smc_required(lfp)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200859 vlr_loc_upd_post_ciph(fi);
860 return;
861 }
862
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +0100863 if (!vsub->last_tuple) {
864 LOGPFSML(fi, LOGL_ERROR, "No auth tuple available\n");
Neels Hofmeyrd2278ec2018-03-02 02:44:05 +0100865 lu_fsm_failure(fi, GSM48_REJECT_NETWORK_FAILURE);
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +0100866 return;
867 }
868
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100869 switch (vsub->sec_ctx) {
870 case VLR_SEC_CTX_GSM:
871 umts_aka = false;
872 break;
873 case VLR_SEC_CTX_UMTS:
874 umts_aka = true;
875 break;
876 default:
877 LOGPFSML(fi, LOGL_ERROR, "Cannot start ciphering, security context is not established\n");
878 lu_fsm_failure(fi, GSM48_REJECT_NETWORK_FAILURE);
879 return;
880 }
881
Harald Welteb8b85a12016-06-17 00:06:42 +0200882 if (vlr_set_ciph_mode(vsub->vlr, fi, lfp->msc_conn_ref,
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100883 umts_aka,
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200884 vsub->vlr->cfg.retrieve_imeisv_ciphered)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200885 LOGPFSML(fi, LOGL_ERROR,
886 "Failed to send Ciphering Mode Command\n");
Neels Hofmeyrd2278ec2018-03-02 02:44:05 +0100887 lu_fsm_failure(fi, GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200888 return;
889 }
890
891 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_CIPH, LU_TIMEOUT_LONG, 0);
892}
893
894static void vlr_loc_upd_node1(struct osmo_fsm_inst *fi)
895{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100896 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200897 struct vlr_subscr *vsub = lfp->vsub;
898
899 LOGPFSM(fi, "%s()\n", __func__);
900
901 OSMO_ASSERT(vsub);
902
903 if (is_auth_required(lfp)) {
904 /* Authenticate_VLR */
905 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_AUTH,
906 LU_TIMEOUT_LONG, 0);
Vadim Yanitskiy3daf0c22020-01-25 06:02:48 +0700907 vsub->auth_fsm = auth_fsm_start(lfp->vsub,
Harald Welteb8b85a12016-06-17 00:06:42 +0200908 fi, VLR_ULA_E_AUTH_RES,
909 lfp->is_r99,
910 lfp->is_utran);
911 } else {
912 /* no need for authentication */
913 vlr_loc_upd_post_auth(fi);
914 }
915}
916
Oliver Smithcbf2c932019-05-06 13:09:55 +0200917static void vlr_loc_upd_node1_pre(struct osmo_fsm_inst *fi)
918{
919 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
920 struct vlr_instance *vlr = lfp->vlr;
921
922 LOGPFSM(fi, "%s()\n", __func__);
923
924 if (vlr->cfg.check_imei_rqd && vlr->cfg.retrieve_imeisv_early) {
925 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_HLR_CHECK_IMEI_EARLY, vlr_timer(lfp->vlr, 3270), 3270);
926 vlr_subscr_tx_req_check_imei(lfp->vsub);
927 } else {
928 vlr_loc_upd_node1(fi);
929 }
930}
931
932/* End of Check_IMEI Procedure. Executed early (before the location update), so we can send the IMEI to the HLR even if
933 * the MS would be rejected in LU. See the "Configuring the Subscribers Create on Demand Feature" section of the OsmoHLR
934 * user manual for a detailed explanation of the use case. */
935static void lu_fsm_wait_hlr_check_imei_early(struct osmo_fsm_inst *fi, uint32_t event, void *data)
936{
937 switch (event) {
938 case VLR_ULA_E_HLR_IMEI_ACK:
939 vlr_loc_upd_node1(fi);
940 break;
941 case VLR_ULA_E_HLR_IMEI_NACK:
942 lu_fsm_failure(fi, GSM48_REJECT_ILLEGAL_ME);
943 break;
944 default:
945 OSMO_ASSERT(0);
946 break;
947 }
948}
949
Harald Welteb8b85a12016-06-17 00:06:42 +0200950static void vlr_loc_upd_want_imsi(struct osmo_fsm_inst *fi)
951{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100952 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200953 struct vlr_instance *vlr = lfp->vlr;
954
955 LOGPFSM(fi, "%s()\n", __func__);
956
957 OSMO_ASSERT(lfp->vsub);
958
959 /* Obtain_IMSI_VLR */
960 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_IMSI,
961 vlr_timer(vlr, 3270), 3270);
962 vlr->ops.tx_id_req(lfp->msc_conn_ref, GSM_MI_TYPE_IMSI);
Oliver Smithcbf2c932019-05-06 13:09:55 +0200963 /* will continue at vlr_loc_upd_node1_pre() once IMSI arrives */
Harald Welteb8b85a12016-06-17 00:06:42 +0200964}
965
966static int assoc_lfp_with_sub(struct osmo_fsm_inst *fi, struct vlr_subscr *vsub)
967{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100968 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200969 struct vlr_instance *vlr = lfp->vlr;
970
971 if (vsub->lu_fsm) {
972 LOGPFSML(fi, LOGL_ERROR,
973 "A Location Updating process is already pending for"
974 " this subscriber. Aborting.\n");
975 /* Also get rid of the other pending LU attempt? */
976 /*lu_fsm_failure(vsub->lu_fsm, GSM48_REJECT_CONGESTION);*/
977 lu_fsm_failure(fi, GSM48_REJECT_CONGESTION);
978 return -EINVAL;
979 }
980 vsub->lu_fsm = fi;
981 vsub->msc_conn_ref = lfp->msc_conn_ref;
982 /* FIXME: send new LAC to HLR? */
Max7d41d872018-12-19 11:48:33 +0100983 vsub->cgi.lai.lac = lfp->new_lai.lac;
Harald Welteb8b85a12016-06-17 00:06:42 +0200984 lfp->vsub = vsub;
985 /* Tell MSC to associate this subscriber with the given
986 * connection */
Neels Hofmeyr1035d902018-12-28 21:22:32 +0100987 if (vlr->ops.subscr_assoc(lfp->msc_conn_ref, lfp->vsub))
988 lu_fsm_failure(fi, GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200989 return 0;
990}
991
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200992static int _lu_fsm_associate_vsub(struct osmo_fsm_inst *fi)
993{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100994 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200995 struct vlr_instance *vlr = lfp->vlr;
996 struct vlr_subscr *vsub = NULL;
997
998 if (!lfp->imsi[0]) {
999 /* TMSI was used */
1000 lfp->lu_by_tmsi = true;
1001 /* TMSI clash: if a different subscriber already has this TMSI,
1002 * we will find that other subscriber in the VLR. So the IMSIs
1003 * would mismatch, but we don't know about it. Theoretically,
1004 * an authentication process would thwart any attempt to use
1005 * someone else's TMSI.
1006 * TODO: Otherwise we can ask for the IMSI and verify that it
1007 * matches the IMSI on record. */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001008 vsub = vlr_subscr_find_or_create_by_tmsi(vlr, lfp->tmsi, __func__, NULL);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001009
1010 if (!vsub) {
1011 LOGPFSML(fi, LOGL_ERROR, "VLR subscriber allocation failed\n");
1012 lu_fsm_failure(fi, GSM48_REJECT_SRV_OPT_TMP_OUT_OF_ORDER);
1013 return -1;
1014 }
1015
1016 vsub->sub_dataconf_by_hlr_ind = false;
1017 if (assoc_lfp_with_sub(fi, vsub)) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001018 vlr_subscr_put(vsub, __func__);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001019 return -1; /* error, fsm failure invoked in assoc_lfp_with_sub() */
1020 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001021 vlr_subscr_put(vsub, __func__);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001022 } else {
1023 /* IMSI was used */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001024 vsub = vlr_subscr_find_or_create_by_imsi(vlr, lfp->imsi, __func__, NULL);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001025
1026 if (!vsub) {
1027 LOGPFSML(fi, LOGL_ERROR, "VLR subscriber allocation failed\n");
1028 lu_fsm_failure(fi, GSM48_REJECT_SRV_OPT_TMP_OUT_OF_ORDER);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001029 vlr_subscr_put(vsub, __func__);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001030 return -1;
1031 }
1032
1033 vsub->sub_dataconf_by_hlr_ind = false;
1034 if (assoc_lfp_with_sub(fi, vsub)) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001035 vlr_subscr_put(vsub, __func__);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001036 return -1; /* error, fsm failure invoked in assoc_lfp_with_sub() */
1037 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001038 vlr_subscr_put(vsub, __func__);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001039 }
1040 return 0;
1041}
1042
Harald Welteb8b85a12016-06-17 00:06:42 +02001043/* 4.1.2.1: Subscriber (via MSC/SGSN) requests location update */
1044static void _start_lu_main(struct osmo_fsm_inst *fi)
1045{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001046 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001047 struct vlr_instance *vlr = lfp->vlr;
Harald Welteb8b85a12016-06-17 00:06:42 +02001048
1049 /* TODO: PUESBINE related handling */
1050
1051 /* Is previous LAI in this VLR? */
1052 if (!lai_in_this_vlr(vlr, &lfp->old_lai)) {
1053#if 0
1054 /* FIXME: check previous VLR, (3) */
1055 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_PVLR,
1056 LU_TIMEOUT_LONG, 0);
1057 return;
1058#endif
1059 LOGPFSML(fi, LOGL_NOTICE, "LAI change from %s,"
1060 " but checking previous VLR not implemented\n",
Neels Hofmeyr379d5792018-02-22 04:04:54 +01001061 osmo_lai_name(&lfp->old_lai));
Harald Welteb8b85a12016-06-17 00:06:42 +02001062 }
1063
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001064 /* If this is a TMSI based LU, we may not have the IMSI. Make sure that
1065 * we know the IMSI, either on record, or request it. */
1066 if (!lfp->vsub->imsi[0])
1067 vlr_loc_upd_want_imsi(fi);
1068 else
Oliver Smithcbf2c932019-05-06 13:09:55 +02001069 vlr_loc_upd_node1_pre(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001070}
1071
Harald Welteb8b85a12016-06-17 00:06:42 +02001072static void lu_fsm_idle(struct osmo_fsm_inst *fi, uint32_t event,
1073 void *data)
1074{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001075 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001076 struct vlr_instance *vlr = lfp->vlr;
1077
1078 OSMO_ASSERT(event == VLR_ULA_E_UPDATE_LA);
1079
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001080 if (_lu_fsm_associate_vsub(fi))
1081 return; /* error. FSM already terminated. */
1082
1083 OSMO_ASSERT(lfp->vsub);
1084
Harald Welte0df904d2018-12-03 11:00:04 +01001085 /* At this point we know for which subscriber the location update is,
1086 * we now must inform SGs-UE FSM that we received a location update
1087 * via A, IU or Gs interface. */
1088 osmo_fsm_inst_dispatch(lfp->vsub->sgs_fsm, SGS_UE_E_RX_LU_FROM_A_IU_GS, NULL);
1089
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001090 /* See 3GPP TS 23.012, procedure Retrieve_IMEISV_If_Required */
1091 if ((!vlr->cfg.retrieve_imeisv_early)
1092 || (lfp->type == VLR_LU_TYPE_PERIODIC && lfp->vsub->imeisv[0])) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001093 /* R_IMEISV_IR1 passed */
1094 _start_lu_main(fi);
1095 } else {
1096 vlr->ops.tx_id_req(lfp->msc_conn_ref, GSM_MI_TYPE_IMEISV);
1097 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_IMEISV,
1098 vlr_timer(vlr, 3270), 3270);
1099 }
1100}
1101
1102static void lu_fsm_wait_imeisv(struct osmo_fsm_inst *fi, uint32_t event,
1103 void *data)
1104{
1105 switch (event) {
1106 case VLR_ULA_E_ID_IMEISV:
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001107 /* IMEISV was copied in vlr_subscr_rx_id_resp(), and that's
1108 * where we received this event from. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001109 _start_lu_main(fi);
1110 break;
1111 default:
Oliver Smithffd522e2019-05-10 14:39:37 +02001112 OSMO_ASSERT(0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001113 break;
1114 }
1115}
1116
1117/* Wait for response from Send_Identification to PVLR */
1118static void lu_fsm_wait_pvlr(struct osmo_fsm_inst *fi, uint32_t event,
1119 void *data)
1120{
1121 switch (event) {
1122 case VLR_ULA_E_SEND_ID_ACK:
Oliver Smithcbf2c932019-05-06 13:09:55 +02001123 vlr_loc_upd_node1_pre(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001124 break;
1125 case VLR_ULA_E_SEND_ID_NACK:
1126 vlr_loc_upd_want_imsi(fi);
1127 break;
1128 default:
Oliver Smithffd522e2019-05-10 14:39:37 +02001129 OSMO_ASSERT(0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001130 break;
1131 }
1132}
1133
1134/* Wait for result of Authenticate_VLR procedure */
1135static void lu_fsm_wait_auth(struct osmo_fsm_inst *fi, uint32_t event,
1136 void *data)
1137{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001138 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Neels Hofmeyr15809592018-04-06 02:57:51 +02001139 enum gsm48_reject_value *res = data;
Harald Welteb8b85a12016-06-17 00:06:42 +02001140
1141 OSMO_ASSERT(event == VLR_ULA_E_AUTH_RES);
1142
1143 lfp->upd_hlr_vlr_fsm = NULL;
1144
Neels Hofmeyr15809592018-04-06 02:57:51 +02001145 if (!res || *res) {
1146 lu_fsm_failure(fi, res? *res : GSM48_REJECT_NETWORK_FAILURE);
1147 return;
1148 }
Harald Welteb8b85a12016-06-17 00:06:42 +02001149
Neels Hofmeyr15809592018-04-06 02:57:51 +02001150 /* Result == Pass */
1151 vlr_loc_upd_post_auth(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001152}
1153
1154static void lu_fsm_wait_ciph(struct osmo_fsm_inst *fi, uint32_t event,
1155 void *data)
1156{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001157 enum vlr_ciph_result_cause result = VLR_CIPH_REJECT;
Harald Welteb8b85a12016-06-17 00:06:42 +02001158
1159 OSMO_ASSERT(event == VLR_ULA_E_CIPH_RES);
1160
1161 if (!data)
1162 LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: NULL\n");
1163 else
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001164 result = *(enum vlr_ciph_result_cause*)data;
Harald Welteb8b85a12016-06-17 00:06:42 +02001165
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001166 switch (result) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001167 case VLR_CIPH_COMPL:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001168 vlr_loc_upd_post_ciph(fi);
1169 return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001170 case VLR_CIPH_REJECT:
1171 LOGPFSM(fi, "ciphering rejected\n");
1172 lu_fsm_failure(fi, GSM48_REJECT_INVALID_MANDANTORY_INF);
1173 return;
1174 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001175 LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: %d\n", result);
Harald Welteb8b85a12016-06-17 00:06:42 +02001176 lu_fsm_failure(fi, GSM48_REJECT_INVALID_MANDANTORY_INF);
1177 return;
1178 }
Harald Welteb8b85a12016-06-17 00:06:42 +02001179}
1180
1181static void lu_fsm_wait_imsi(struct osmo_fsm_inst *fi, uint32_t event,
1182 void *data)
1183{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001184 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001185 struct vlr_subscr *vsub = lfp->vsub;
1186 char *mi_string = data;
1187
1188 switch (event) {
1189 case VLR_ULA_E_ID_IMSI:
1190 vlr_subscr_set_imsi(vsub, mi_string);
Oliver Smithcbf2c932019-05-06 13:09:55 +02001191 vlr_loc_upd_node1_pre(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001192 break;
1193 default:
Oliver Smithffd522e2019-05-10 14:39:37 +02001194 OSMO_ASSERT(0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001195 break;
1196 }
1197}
1198
1199/* At the end of Update_HLR_VLR */
1200static void lu_fsm_wait_hlr_ul_res(struct osmo_fsm_inst *fi, uint32_t event,
1201 void *data)
1202{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001203 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001204
1205 switch (event) {
1206 case VLR_ULA_E_HLR_LU_RES:
1207 /* pass-through this event to Update_HLR_VLR */
1208 if (data == NULL)
1209 osmo_fsm_inst_dispatch(lfp->upd_hlr_vlr_fsm, UPD_HLR_VLR_E_UPD_LOC_ACK, NULL);
1210 else
1211 osmo_fsm_inst_dispatch(lfp->upd_hlr_vlr_fsm, UPD_HLR_VLR_E_UPD_LOC_NACK, data);
1212 break;
1213 case VLR_ULA_E_UPD_HLR_COMPL:
1214 if (data == NULL) {
1215 /* successful case */
1216 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_LU_COMPL,
1217 LU_TIMEOUT_LONG, 0);
1218 vlr_loc_upd_start_lu_compl_fsm(fi);
1219 /* continue in MSC ?!? */
1220 } else {
1221 /* unsuccessful case */
Neels Hofmeyrdd2aeba2018-10-30 19:47:01 +01001222 enum gsm48_reject_value cause =
1223 *(enum gsm48_reject_value *)data;
Neels Hofmeyref9126c2017-07-18 15:38:39 +02001224 /* Ignoring standalone mode for now. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001225 if (0 /* procedure_error && vlr->cfg.standalone_mode */) {
1226 osmo_fsm_inst_state_chg(fi,
1227 VLR_ULA_S_WAIT_LU_COMPL_STANDALONE,
1228 LU_TIMEOUT_LONG, 0);
1229 vlr_loc_upd_start_lu_compl_fsm(fi);
1230 } else {
1231 lu_fsm_failure(fi, cause);
1232 }
1233 }
1234 break;
Neels Hofmeyr364f9272019-08-22 17:20:14 +02001235 case VLR_ULA_E_ID_IMEI:
1236 case VLR_ULA_E_ID_IMEISV:
1237 /* Got the IMEI from ME, nothing to do right now though. */
1238 break;
Harald Welteb8b85a12016-06-17 00:06:42 +02001239 default:
Oliver Smithffd522e2019-05-10 14:39:37 +02001240 OSMO_ASSERT(0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001241 break;
1242 }
1243}
1244
1245/* Wait for end of Location_Update_Completion_VLR */
1246static void lu_fsm_wait_lu_compl(struct osmo_fsm_inst *fi, uint32_t event,
1247 void *data)
1248{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001249 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001250 uint8_t cause;
1251
1252 switch (event) {
1253 case VLR_ULA_E_NEW_TMSI_ACK:
1254 osmo_fsm_inst_dispatch(lfp->lu_compl_vlr_fsm,
1255 LU_COMPL_VLR_E_NEW_TMSI_ACK, NULL);
1256 break;
1257 case VLR_ULA_E_ID_IMEI:
Neels Hofmeyr106ba522019-08-08 02:00:54 +02001258 case VLR_ULA_E_ID_IMEISV:
Oliver Smith7d053092018-12-14 17:37:38 +01001259 /* Got the IMEI from ME, now send it to HLR */
1260 vlr_subscr_tx_req_check_imei(lfp->vsub);
1261 break;
1262 case VLR_ULA_E_HLR_IMEI_ACK:
Harald Welteb8b85a12016-06-17 00:06:42 +02001263 osmo_fsm_inst_dispatch(lfp->lu_compl_vlr_fsm,
1264 LU_COMPL_VLR_E_IMEI_CHECK_ACK, NULL);
1265 break;
Oliver Smith7d053092018-12-14 17:37:38 +01001266 case VLR_ULA_E_HLR_IMEI_NACK:
1267 osmo_fsm_inst_dispatch(lfp->lu_compl_vlr_fsm,
1268 LU_COMPL_VLR_E_IMEI_CHECK_NACK, NULL);
1269 break;
Harald Welteb8b85a12016-06-17 00:06:42 +02001270 case VLR_ULA_E_LU_COMPL_SUCCESS:
1271 lu_fsm_discard_lu_compl_fsm(fi);
1272
1273 /* Update Register */
1274 /* TODO: Set_Notification_Type 23.078 */
1275 /* TODO: Notify_gsmSCF 23.078 */
1276 /* TODO: Authenticated Radio Contact Established -> ARC */
Stefan Sperling3a741282018-03-13 21:11:49 +01001277
1278 if (lfp->type == VLR_LU_TYPE_IMSI_ATTACH)
1279 lfp->vlr->ops.tx_mm_info(lfp->msc_conn_ref);
1280
Harald Welteb8b85a12016-06-17 00:06:42 +02001281 lu_fsm_success(fi);
1282 break;
1283 case VLR_ULA_E_LU_COMPL_FAILURE:
1284 cause = GSM48_REJECT_NETWORK_FAILURE;
1285 if (data)
1286 cause = *(uint8_t*)data;
1287 lu_fsm_discard_lu_compl_fsm(fi);
1288 lu_fsm_failure(fi, cause);
1289 break;
1290 default:
Oliver Smithffd522e2019-05-10 14:39:37 +02001291 OSMO_ASSERT(0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001292 break;
1293 }
1294}
1295
1296/* Wait for end of Location_Update_Completion_VLR (standalone case) */
1297static void lu_fsm_wait_lu_compl_standalone(struct osmo_fsm_inst *fi,
1298 uint32_t event, void *data)
1299{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001300 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001301 struct vlr_subscr *vsub = lfp->vsub;
1302 uint8_t cause;
1303
1304 switch (event) {
1305 case VLR_ULA_E_NEW_TMSI_ACK:
1306 osmo_fsm_inst_dispatch(lfp->lu_compl_vlr_fsm,
1307 LU_COMPL_VLR_E_NEW_TMSI_ACK, NULL);
1308 break;
1309 case VLR_ULA_E_LU_COMPL_SUCCESS:
1310 lu_fsm_discard_lu_compl_fsm(fi);
1311 vsub->sub_dataconf_by_hlr_ind = false;
1312 lu_fsm_success(fi);
1313 break;
1314 case VLR_ULA_E_LU_COMPL_FAILURE:
1315 vsub->sub_dataconf_by_hlr_ind = false;
1316 cause = GSM48_REJECT_NETWORK_FAILURE;
1317 if (data)
1318 cause = *(uint8_t*)data;
1319 lu_fsm_discard_lu_compl_fsm(fi);
1320 lu_fsm_failure(fi, cause);
1321 break;
1322 default:
Oliver Smithffd522e2019-05-10 14:39:37 +02001323 OSMO_ASSERT(0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001324 break;
1325 }
1326}
1327
1328static const struct osmo_fsm_state vlr_lu_fsm_states[] = {
1329 [VLR_ULA_S_IDLE] = {
1330 .in_event_mask = S(VLR_ULA_E_UPDATE_LA),
1331 .out_state_mask = S(VLR_ULA_S_WAIT_IMEISV) |
1332 S(VLR_ULA_S_WAIT_PVLR) |
1333 S(VLR_ULA_S_WAIT_IMSI) |
1334 S(VLR_ULA_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +01001335 S(VLR_ULA_S_WAIT_CIPH) |
Oliver Smithcbf2c932019-05-06 13:09:55 +02001336 S(VLR_ULA_S_WAIT_HLR_CHECK_IMEI_EARLY) |
Harald Welteb8b85a12016-06-17 00:06:42 +02001337 S(VLR_ULA_S_WAIT_HLR_UPD) |
1338 S(VLR_ULA_S_DONE),
1339 .name = OSMO_STRINGIFY(VLR_ULA_S_IDLE),
1340 .action = lu_fsm_idle,
1341 },
1342 [VLR_ULA_S_WAIT_IMEISV] = {
1343 .in_event_mask = S(VLR_ULA_E_ID_IMEISV),
1344 .out_state_mask = S(VLR_ULA_S_WAIT_PVLR) |
1345 S(VLR_ULA_S_WAIT_IMSI) |
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001346 S(VLR_ULA_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +01001347 S(VLR_ULA_S_WAIT_CIPH) |
Oliver Smithcbf2c932019-05-06 13:09:55 +02001348 S(VLR_ULA_S_WAIT_HLR_CHECK_IMEI_EARLY) |
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001349 S(VLR_ULA_S_WAIT_HLR_UPD) |
Harald Welteb8b85a12016-06-17 00:06:42 +02001350 S(VLR_ULA_S_DONE),
1351 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_IMEISV),
1352 .action = lu_fsm_wait_imeisv,
1353 },
1354 [VLR_ULA_S_WAIT_PVLR] = {
1355 .in_event_mask = S(VLR_ULA_E_SEND_ID_ACK) |
1356 S(VLR_ULA_E_SEND_ID_NACK),
1357 .out_state_mask = S(VLR_ULA_S_WAIT_IMSI) |
1358 S(VLR_ULA_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +01001359 S(VLR_ULA_S_WAIT_CIPH) |
Oliver Smithcbf2c932019-05-06 13:09:55 +02001360 S(VLR_ULA_S_WAIT_HLR_CHECK_IMEI_EARLY) |
Harald Welteb8b85a12016-06-17 00:06:42 +02001361 S(VLR_ULA_S_DONE),
1362 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_PVLR),
1363 .action = lu_fsm_wait_pvlr,
1364 },
1365 [VLR_ULA_S_WAIT_AUTH] = {
1366 .in_event_mask = S(VLR_ULA_E_AUTH_RES),
1367 .out_state_mask = S(VLR_ULA_S_WAIT_CIPH) |
1368 S(VLR_ULA_S_WAIT_LU_COMPL) |
1369 S(VLR_ULA_S_WAIT_HLR_UPD) |
1370 S(VLR_ULA_S_DONE),
1371 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_AUTH),
1372 .action = lu_fsm_wait_auth,
1373 },
1374 [VLR_ULA_S_WAIT_CIPH] = {
1375 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_CIPH),
1376 .in_event_mask = S(VLR_ULA_E_CIPH_RES),
1377 .out_state_mask = S(VLR_ULA_S_WAIT_LU_COMPL) |
1378 S(VLR_ULA_S_WAIT_HLR_UPD) |
1379 S(VLR_ULA_S_DONE),
1380 .action = lu_fsm_wait_ciph,
1381 },
1382 [VLR_ULA_S_WAIT_IMSI] = {
1383 .in_event_mask = S(VLR_ULA_E_ID_IMSI),
1384 .out_state_mask = S(VLR_ULA_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +01001385 S(VLR_ULA_S_WAIT_CIPH) |
Oliver Smithcbf2c932019-05-06 13:09:55 +02001386 S(VLR_ULA_S_WAIT_HLR_CHECK_IMEI_EARLY) |
Harald Welteb8b85a12016-06-17 00:06:42 +02001387 S(VLR_ULA_S_WAIT_HLR_UPD) |
1388 S(VLR_ULA_S_DONE),
1389 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_IMSI),
1390 .action = lu_fsm_wait_imsi,
1391 },
Oliver Smithcbf2c932019-05-06 13:09:55 +02001392 [VLR_ULA_S_WAIT_HLR_CHECK_IMEI_EARLY] = {
1393 .in_event_mask = S(VLR_ULA_E_HLR_IMEI_ACK) |
1394 S(VLR_ULA_E_HLR_IMEI_NACK),
1395 .out_state_mask = S(VLR_ULA_S_WAIT_AUTH) |
1396 S(VLR_ULA_S_WAIT_CIPH) |
1397 S(VLR_ULA_S_WAIT_HLR_UPD) |
1398 S(VLR_ULA_S_WAIT_LU_COMPL) |
1399 S(VLR_ULA_S_DONE),
1400 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_HLR_CHECK_IMEI_EARLY),
1401 .action = lu_fsm_wait_hlr_check_imei_early,
1402 },
Harald Welteb8b85a12016-06-17 00:06:42 +02001403 [VLR_ULA_S_WAIT_HLR_UPD] = {
1404 .in_event_mask = S(VLR_ULA_E_HLR_LU_RES) |
Neels Hofmeyr364f9272019-08-22 17:20:14 +02001405 S(VLR_ULA_E_UPD_HLR_COMPL) |
1406 S(VLR_ULA_E_ID_IMEI) |
1407 S(VLR_ULA_E_ID_IMEISV),
Harald Welteb8b85a12016-06-17 00:06:42 +02001408 .out_state_mask = S(VLR_ULA_S_WAIT_LU_COMPL) |
1409 S(VLR_ULA_S_WAIT_LU_COMPL_STANDALONE) |
1410 S(VLR_ULA_S_DONE),
1411 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_HLR_UPD),
1412 .action = lu_fsm_wait_hlr_ul_res,
1413 },
1414 [VLR_ULA_S_WAIT_LU_COMPL] = {
1415 .in_event_mask = S(VLR_ULA_E_LU_COMPL_SUCCESS) |
1416 S(VLR_ULA_E_LU_COMPL_FAILURE) |
1417 S(VLR_ULA_E_NEW_TMSI_ACK) |
1418 S(VLR_ULA_E_ID_IMEI) |
Oliver Smith7d053092018-12-14 17:37:38 +01001419 S(VLR_ULA_E_ID_IMEISV) |
1420 S(VLR_ULA_E_HLR_IMEI_ACK) |
1421 S(VLR_ULA_E_HLR_IMEI_NACK),
Harald Welteb8b85a12016-06-17 00:06:42 +02001422 .out_state_mask = S(VLR_ULA_S_DONE),
1423 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_LU_COMPL),
1424 .action = lu_fsm_wait_lu_compl,
1425 },
1426 [VLR_ULA_S_WAIT_LU_COMPL_STANDALONE] = {
1427 .in_event_mask = S(VLR_ULA_E_LU_COMPL_SUCCESS) |
1428 S(VLR_ULA_E_LU_COMPL_FAILURE) |
1429 S(VLR_ULA_E_NEW_TMSI_ACK),
1430 .out_state_mask = S(VLR_ULA_S_DONE),
1431 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_LU_COMPL_STANDALONE),
1432 .action = lu_fsm_wait_lu_compl_standalone,
1433 },
1434 [VLR_ULA_S_DONE] = {
1435 .name = OSMO_STRINGIFY(VLR_ULA_S_DONE),
1436 .onenter = lu_fsm_dispatch_result,
1437 },
1438};
1439
1440static void fsm_lu_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
1441{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001442 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001443 struct vlr_subscr *vsub = lfp->vsub;
1444
1445 LOGPFSM(fi, "fsm_lu_cleanup called with cause %s\n",
1446 osmo_fsm_term_cause_name(cause));
1447 if (vsub && vsub->lu_fsm == fi)
1448 vsub->lu_fsm = NULL;
1449}
1450
1451static struct osmo_fsm vlr_lu_fsm = {
1452 .name = "vlr_lu_fsm",
1453 .states = vlr_lu_fsm_states,
1454 .num_states = ARRAY_SIZE(vlr_lu_fsm_states),
1455 .allstate_event_mask = 0,
1456 .allstate_action = NULL,
1457 .log_subsys = DVLR,
1458 .event_names = fsm_lu_event_names,
1459 .cleanup = fsm_lu_cleanup,
1460};
1461
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001462static inline struct lu_fsm_priv *lu_fsm_fi_priv(struct osmo_fsm_inst *fi)
1463{
1464 OSMO_ASSERT(fi->fsm == &vlr_lu_fsm);
1465 return (struct lu_fsm_priv*)fi->priv;
1466}
1467
Harald Welteb8b85a12016-06-17 00:06:42 +02001468struct osmo_fsm_inst *
1469vlr_loc_update(struct osmo_fsm_inst *parent,
1470 uint32_t parent_event_success,
1471 uint32_t parent_event_failure,
1472 void *parent_event_data,
1473 struct vlr_instance *vlr, void *msc_conn_ref,
1474 enum vlr_lu_type type, uint32_t tmsi, const char *imsi,
1475 const struct osmo_location_area_id *old_lai,
1476 const struct osmo_location_area_id *new_lai,
1477 bool authentication_required,
Harald Welte71c51df2017-12-23 18:51:48 +01001478 bool ciphering_required,
Sylvain Munautda9f37e2019-03-14 11:02:36 +01001479 uint8_t key_seq,
Harald Welteb8b85a12016-06-17 00:06:42 +02001480 bool is_r99, bool is_utran,
1481 bool assign_tmsi)
1482{
1483 struct osmo_fsm_inst *fi;
1484 struct lu_fsm_priv *lfp;
1485
1486 fi = osmo_fsm_inst_alloc_child(&vlr_lu_fsm, parent, parent_event_failure);
1487 if (!fi)
1488 return NULL;
1489
1490 lfp = talloc_zero(fi, struct lu_fsm_priv);
1491 lfp->vlr = vlr;
1492 lfp->msc_conn_ref = msc_conn_ref;
1493 lfp->tmsi = tmsi;
1494 lfp->type = type;
1495 lfp->old_lai = *old_lai;
1496 lfp->new_lai = *new_lai;
1497 lfp->lu_by_tmsi = true;
1498 lfp->parent_event_success = parent_event_success;
1499 lfp->parent_event_failure = parent_event_failure;
1500 lfp->parent_event_data = parent_event_data;
1501 lfp->authentication_required = authentication_required;
1502 lfp->ciphering_required = ciphering_required;
Sylvain Munautda9f37e2019-03-14 11:02:36 +01001503 lfp->key_seq = key_seq;
Harald Welteb8b85a12016-06-17 00:06:42 +02001504 lfp->is_r99 = is_r99;
1505 lfp->is_utran = is_utran;
1506 lfp->assign_tmsi = assign_tmsi;
1507 if (imsi) {
1508 strncpy(lfp->imsi, imsi, sizeof(lfp->imsi)-1);
1509 lfp->imsi[sizeof(lfp->imsi)-1] = '\0';
1510 lfp->lu_by_tmsi = false;
1511 }
1512 fi->priv = lfp;
1513
1514 LOGPFSM(fi, "rev=%s net=%s%s%s\n",
1515 is_r99 ? "R99" : "GSM",
1516 is_utran ? "UTRAN" : "GERAN",
1517 (authentication_required || ciphering_required)?
1518 " Auth" : " (no Auth)",
1519 (authentication_required || ciphering_required)?
1520 (ciphering_required? "+Ciph" : " (no Ciph)")
1521 : "");
1522
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001523 if (is_utran && !authentication_required)
1524 LOGPFSML(fi, LOGL_ERROR,
1525 "Authentication off on UTRAN network. Good luck.\n");
1526
Harald Welteb8b85a12016-06-17 00:06:42 +02001527 osmo_fsm_inst_dispatch(fi, VLR_ULA_E_UPDATE_LA, NULL);
1528
1529 return fi;
1530}
1531
Neels Hofmeyr15809592018-04-06 02:57:51 +02001532void vlr_loc_update_cancel(struct osmo_fsm_inst *fi,
1533 enum osmo_fsm_term_cause fsm_cause,
1534 uint8_t gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +02001535{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001536 struct lu_fsm_priv *lfp;
1537
1538 OSMO_ASSERT(fi);
1539 OSMO_ASSERT(fi->fsm == &vlr_lu_fsm);
1540
1541 lfp = fi->priv;
1542 lfp->rej_cause = gsm48_cause;
1543
1544 if (fi->state != VLR_ULA_S_DONE)
1545 lu_fsm_failure(fi, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +02001546}
1547
1548void vlr_lu_fsm_init(void)
1549{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001550 OSMO_ASSERT(osmo_fsm_register(&vlr_lu_fsm) == 0);
1551 OSMO_ASSERT(osmo_fsm_register(&upd_hlr_vlr_fsm) == 0);
1552 OSMO_ASSERT(osmo_fsm_register(&sub_pres_vlr_fsm) == 0);
1553 OSMO_ASSERT(osmo_fsm_register(&lu_compl_vlr_fsm) == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001554}