blob: e4d40f0f01720f06f89a89679f0f72acb678b665 [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;
821
822 LOGPFSM(fi, "%s()\n", __func__);
823
824 OSMO_ASSERT(vsub);
825
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200826 if (lfp->is_utran) {
827 int rc;
828 rc = lfp->vlr->ops.tx_common_id(lfp->msc_conn_ref);
829 if (rc)
830 LOGPFSML(fi, LOGL_ERROR,
831 "Error while sending Common ID (%d)\n", rc);
832 }
833
Harald Welteb8b85a12016-06-17 00:06:42 +0200834 vsub->conf_by_radio_contact_ind = true;
835 /* Update LAI */
836 vsub->cgi.lai = lfp->new_lai;
837 vsub->dormant_ind = false;
838 vsub->cancel_loc_rx = false;
839 if (hlr_update_needed(vsub)) {
840 vlr_loc_upd_node_4(fi);
841 } else {
842 /* TODO: ADD Support */
843 /* TODO: Node A: PgA Support */
844 vlr_loc_upd_node_b(fi);
845 }
846}
847
848/* 4.1.2.1 after Authentication successful (or no auth rqd) */
849static void vlr_loc_upd_post_auth(struct osmo_fsm_inst *fi)
850{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100851 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200852 struct vlr_subscr *vsub = lfp->vsub;
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100853 bool umts_aka;
Harald Welteb8b85a12016-06-17 00:06:42 +0200854
855 LOGPFSM(fi, "%s()\n", __func__);
856
857 OSMO_ASSERT(vsub);
858
859 if (!is_ciph_required(lfp)) {
860 vlr_loc_upd_post_ciph(fi);
861 return;
862 }
863
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +0100864 if (!vsub->last_tuple) {
865 LOGPFSML(fi, LOGL_ERROR, "No auth tuple available\n");
Neels Hofmeyrd2278ec2018-03-02 02:44:05 +0100866 lu_fsm_failure(fi, GSM48_REJECT_NETWORK_FAILURE);
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +0100867 return;
868 }
869
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100870 switch (vsub->sec_ctx) {
871 case VLR_SEC_CTX_GSM:
872 umts_aka = false;
873 break;
874 case VLR_SEC_CTX_UMTS:
875 umts_aka = true;
876 break;
877 default:
878 LOGPFSML(fi, LOGL_ERROR, "Cannot start ciphering, security context is not established\n");
879 lu_fsm_failure(fi, GSM48_REJECT_NETWORK_FAILURE);
880 return;
881 }
882
Harald Welteb8b85a12016-06-17 00:06:42 +0200883 if (vlr_set_ciph_mode(vsub->vlr, fi, lfp->msc_conn_ref,
884 lfp->ciphering_required,
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100885 umts_aka,
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200886 vsub->vlr->cfg.retrieve_imeisv_ciphered)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200887 LOGPFSML(fi, LOGL_ERROR,
888 "Failed to send Ciphering Mode Command\n");
Neels Hofmeyrd2278ec2018-03-02 02:44:05 +0100889 lu_fsm_failure(fi, GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200890 return;
891 }
892
893 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_CIPH, LU_TIMEOUT_LONG, 0);
894}
895
896static void vlr_loc_upd_node1(struct osmo_fsm_inst *fi)
897{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100898 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200899 struct vlr_subscr *vsub = lfp->vsub;
900
901 LOGPFSM(fi, "%s()\n", __func__);
902
903 OSMO_ASSERT(vsub);
904
905 if (is_auth_required(lfp)) {
906 /* Authenticate_VLR */
907 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_AUTH,
908 LU_TIMEOUT_LONG, 0);
909 vsub->auth_fsm = auth_fsm_start(lfp->vsub, fi->log_level,
910 fi, VLR_ULA_E_AUTH_RES,
911 lfp->is_r99,
912 lfp->is_utran);
913 } else {
914 /* no need for authentication */
915 vlr_loc_upd_post_auth(fi);
916 }
917}
918
Oliver Smithcbf2c932019-05-06 13:09:55 +0200919static void vlr_loc_upd_node1_pre(struct osmo_fsm_inst *fi)
920{
921 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
922 struct vlr_instance *vlr = lfp->vlr;
923
924 LOGPFSM(fi, "%s()\n", __func__);
925
926 if (vlr->cfg.check_imei_rqd && vlr->cfg.retrieve_imeisv_early) {
927 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_HLR_CHECK_IMEI_EARLY, vlr_timer(lfp->vlr, 3270), 3270);
928 vlr_subscr_tx_req_check_imei(lfp->vsub);
929 } else {
930 vlr_loc_upd_node1(fi);
931 }
932}
933
934/* End of Check_IMEI Procedure. Executed early (before the location update), so we can send the IMEI to the HLR even if
935 * the MS would be rejected in LU. See the "Configuring the Subscribers Create on Demand Feature" section of the OsmoHLR
936 * user manual for a detailed explanation of the use case. */
937static void lu_fsm_wait_hlr_check_imei_early(struct osmo_fsm_inst *fi, uint32_t event, void *data)
938{
939 switch (event) {
940 case VLR_ULA_E_HLR_IMEI_ACK:
941 vlr_loc_upd_node1(fi);
942 break;
943 case VLR_ULA_E_HLR_IMEI_NACK:
944 lu_fsm_failure(fi, GSM48_REJECT_ILLEGAL_ME);
945 break;
946 default:
947 OSMO_ASSERT(0);
948 break;
949 }
950}
951
Harald Welteb8b85a12016-06-17 00:06:42 +0200952static void vlr_loc_upd_want_imsi(struct osmo_fsm_inst *fi)
953{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100954 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200955 struct vlr_instance *vlr = lfp->vlr;
956
957 LOGPFSM(fi, "%s()\n", __func__);
958
959 OSMO_ASSERT(lfp->vsub);
960
961 /* Obtain_IMSI_VLR */
962 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_IMSI,
963 vlr_timer(vlr, 3270), 3270);
964 vlr->ops.tx_id_req(lfp->msc_conn_ref, GSM_MI_TYPE_IMSI);
Oliver Smithcbf2c932019-05-06 13:09:55 +0200965 /* will continue at vlr_loc_upd_node1_pre() once IMSI arrives */
Harald Welteb8b85a12016-06-17 00:06:42 +0200966}
967
968static int assoc_lfp_with_sub(struct osmo_fsm_inst *fi, struct vlr_subscr *vsub)
969{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100970 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200971 struct vlr_instance *vlr = lfp->vlr;
972
973 if (vsub->lu_fsm) {
974 LOGPFSML(fi, LOGL_ERROR,
975 "A Location Updating process is already pending for"
976 " this subscriber. Aborting.\n");
977 /* Also get rid of the other pending LU attempt? */
978 /*lu_fsm_failure(vsub->lu_fsm, GSM48_REJECT_CONGESTION);*/
979 lu_fsm_failure(fi, GSM48_REJECT_CONGESTION);
980 return -EINVAL;
981 }
982 vsub->lu_fsm = fi;
983 vsub->msc_conn_ref = lfp->msc_conn_ref;
984 /* FIXME: send new LAC to HLR? */
Max7d41d872018-12-19 11:48:33 +0100985 vsub->cgi.lai.lac = lfp->new_lai.lac;
Harald Welteb8b85a12016-06-17 00:06:42 +0200986 lfp->vsub = vsub;
987 /* Tell MSC to associate this subscriber with the given
988 * connection */
Neels Hofmeyr1035d902018-12-28 21:22:32 +0100989 if (vlr->ops.subscr_assoc(lfp->msc_conn_ref, lfp->vsub))
990 lu_fsm_failure(fi, GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200991 return 0;
992}
993
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200994static int _lu_fsm_associate_vsub(struct osmo_fsm_inst *fi)
995{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100996 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200997 struct vlr_instance *vlr = lfp->vlr;
998 struct vlr_subscr *vsub = NULL;
999
1000 if (!lfp->imsi[0]) {
1001 /* TMSI was used */
1002 lfp->lu_by_tmsi = true;
1003 /* TMSI clash: if a different subscriber already has this TMSI,
1004 * we will find that other subscriber in the VLR. So the IMSIs
1005 * would mismatch, but we don't know about it. Theoretically,
1006 * an authentication process would thwart any attempt to use
1007 * someone else's TMSI.
1008 * TODO: Otherwise we can ask for the IMSI and verify that it
1009 * matches the IMSI on record. */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001010 vsub = vlr_subscr_find_or_create_by_tmsi(vlr, lfp->tmsi, __func__, NULL);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001011
1012 if (!vsub) {
1013 LOGPFSML(fi, LOGL_ERROR, "VLR subscriber allocation failed\n");
1014 lu_fsm_failure(fi, GSM48_REJECT_SRV_OPT_TMP_OUT_OF_ORDER);
1015 return -1;
1016 }
1017
1018 vsub->sub_dataconf_by_hlr_ind = false;
1019 if (assoc_lfp_with_sub(fi, vsub)) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001020 vlr_subscr_put(vsub, __func__);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001021 return -1; /* error, fsm failure invoked in assoc_lfp_with_sub() */
1022 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001023 vlr_subscr_put(vsub, __func__);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001024 } else {
1025 /* IMSI was used */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001026 vsub = vlr_subscr_find_or_create_by_imsi(vlr, lfp->imsi, __func__, NULL);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001027
1028 if (!vsub) {
1029 LOGPFSML(fi, LOGL_ERROR, "VLR subscriber allocation failed\n");
1030 lu_fsm_failure(fi, GSM48_REJECT_SRV_OPT_TMP_OUT_OF_ORDER);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001031 vlr_subscr_put(vsub, __func__);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001032 return -1;
1033 }
1034
1035 vsub->sub_dataconf_by_hlr_ind = false;
1036 if (assoc_lfp_with_sub(fi, vsub)) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001037 vlr_subscr_put(vsub, __func__);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001038 return -1; /* error, fsm failure invoked in assoc_lfp_with_sub() */
1039 }
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +01001040 vlr_subscr_put(vsub, __func__);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001041 }
1042 return 0;
1043}
1044
Harald Welteb8b85a12016-06-17 00:06:42 +02001045/* 4.1.2.1: Subscriber (via MSC/SGSN) requests location update */
1046static void _start_lu_main(struct osmo_fsm_inst *fi)
1047{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001048 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001049 struct vlr_instance *vlr = lfp->vlr;
Harald Welteb8b85a12016-06-17 00:06:42 +02001050
1051 /* TODO: PUESBINE related handling */
1052
1053 /* Is previous LAI in this VLR? */
1054 if (!lai_in_this_vlr(vlr, &lfp->old_lai)) {
1055#if 0
1056 /* FIXME: check previous VLR, (3) */
1057 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_PVLR,
1058 LU_TIMEOUT_LONG, 0);
1059 return;
1060#endif
1061 LOGPFSML(fi, LOGL_NOTICE, "LAI change from %s,"
1062 " but checking previous VLR not implemented\n",
Neels Hofmeyr379d5792018-02-22 04:04:54 +01001063 osmo_lai_name(&lfp->old_lai));
Harald Welteb8b85a12016-06-17 00:06:42 +02001064 }
1065
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001066 /* If this is a TMSI based LU, we may not have the IMSI. Make sure that
1067 * we know the IMSI, either on record, or request it. */
1068 if (!lfp->vsub->imsi[0])
1069 vlr_loc_upd_want_imsi(fi);
1070 else
Oliver Smithcbf2c932019-05-06 13:09:55 +02001071 vlr_loc_upd_node1_pre(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001072}
1073
Harald Welteb8b85a12016-06-17 00:06:42 +02001074static void lu_fsm_idle(struct osmo_fsm_inst *fi, uint32_t event,
1075 void *data)
1076{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001077 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001078 struct vlr_instance *vlr = lfp->vlr;
1079
1080 OSMO_ASSERT(event == VLR_ULA_E_UPDATE_LA);
1081
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001082 if (_lu_fsm_associate_vsub(fi))
1083 return; /* error. FSM already terminated. */
1084
1085 OSMO_ASSERT(lfp->vsub);
1086
Harald Welte0df904d2018-12-03 11:00:04 +01001087 /* At this point we know for which subscriber the location update is,
1088 * we now must inform SGs-UE FSM that we received a location update
1089 * via A, IU or Gs interface. */
1090 osmo_fsm_inst_dispatch(lfp->vsub->sgs_fsm, SGS_UE_E_RX_LU_FROM_A_IU_GS, NULL);
1091
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001092 /* See 3GPP TS 23.012, procedure Retrieve_IMEISV_If_Required */
1093 if ((!vlr->cfg.retrieve_imeisv_early)
1094 || (lfp->type == VLR_LU_TYPE_PERIODIC && lfp->vsub->imeisv[0])) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001095 /* R_IMEISV_IR1 passed */
1096 _start_lu_main(fi);
1097 } else {
1098 vlr->ops.tx_id_req(lfp->msc_conn_ref, GSM_MI_TYPE_IMEISV);
1099 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_IMEISV,
1100 vlr_timer(vlr, 3270), 3270);
1101 }
1102}
1103
1104static void lu_fsm_wait_imeisv(struct osmo_fsm_inst *fi, uint32_t event,
1105 void *data)
1106{
1107 switch (event) {
1108 case VLR_ULA_E_ID_IMEISV:
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001109 /* IMEISV was copied in vlr_subscr_rx_id_resp(), and that's
1110 * where we received this event from. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001111 _start_lu_main(fi);
1112 break;
1113 default:
Oliver Smithffd522e2019-05-10 14:39:37 +02001114 OSMO_ASSERT(0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001115 break;
1116 }
1117}
1118
1119/* Wait for response from Send_Identification to PVLR */
1120static void lu_fsm_wait_pvlr(struct osmo_fsm_inst *fi, uint32_t event,
1121 void *data)
1122{
1123 switch (event) {
1124 case VLR_ULA_E_SEND_ID_ACK:
Oliver Smithcbf2c932019-05-06 13:09:55 +02001125 vlr_loc_upd_node1_pre(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001126 break;
1127 case VLR_ULA_E_SEND_ID_NACK:
1128 vlr_loc_upd_want_imsi(fi);
1129 break;
1130 default:
Oliver Smithffd522e2019-05-10 14:39:37 +02001131 OSMO_ASSERT(0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001132 break;
1133 }
1134}
1135
1136/* Wait for result of Authenticate_VLR procedure */
1137static void lu_fsm_wait_auth(struct osmo_fsm_inst *fi, uint32_t event,
1138 void *data)
1139{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001140 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Neels Hofmeyr15809592018-04-06 02:57:51 +02001141 enum gsm48_reject_value *res = data;
Harald Welteb8b85a12016-06-17 00:06:42 +02001142
1143 OSMO_ASSERT(event == VLR_ULA_E_AUTH_RES);
1144
1145 lfp->upd_hlr_vlr_fsm = NULL;
1146
Neels Hofmeyr15809592018-04-06 02:57:51 +02001147 if (!res || *res) {
1148 lu_fsm_failure(fi, res? *res : GSM48_REJECT_NETWORK_FAILURE);
1149 return;
1150 }
Harald Welteb8b85a12016-06-17 00:06:42 +02001151
Neels Hofmeyr15809592018-04-06 02:57:51 +02001152 /* Result == Pass */
1153 vlr_loc_upd_post_auth(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001154}
1155
1156static void lu_fsm_wait_ciph(struct osmo_fsm_inst *fi, uint32_t event,
1157 void *data)
1158{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001159 enum vlr_ciph_result_cause result = VLR_CIPH_REJECT;
Harald Welteb8b85a12016-06-17 00:06:42 +02001160
1161 OSMO_ASSERT(event == VLR_ULA_E_CIPH_RES);
1162
1163 if (!data)
1164 LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: NULL\n");
1165 else
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001166 result = *(enum vlr_ciph_result_cause*)data;
Harald Welteb8b85a12016-06-17 00:06:42 +02001167
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001168 switch (result) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001169 case VLR_CIPH_COMPL:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001170 vlr_loc_upd_post_ciph(fi);
1171 return;
Harald Welteb8b85a12016-06-17 00:06:42 +02001172 case VLR_CIPH_REJECT:
1173 LOGPFSM(fi, "ciphering rejected\n");
1174 lu_fsm_failure(fi, GSM48_REJECT_INVALID_MANDANTORY_INF);
1175 return;
1176 default:
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001177 LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: %d\n", result);
Harald Welteb8b85a12016-06-17 00:06:42 +02001178 lu_fsm_failure(fi, GSM48_REJECT_INVALID_MANDANTORY_INF);
1179 return;
1180 }
Harald Welteb8b85a12016-06-17 00:06:42 +02001181}
1182
1183static void lu_fsm_wait_imsi(struct osmo_fsm_inst *fi, uint32_t event,
1184 void *data)
1185{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001186 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001187 struct vlr_subscr *vsub = lfp->vsub;
1188 char *mi_string = data;
1189
1190 switch (event) {
1191 case VLR_ULA_E_ID_IMSI:
1192 vlr_subscr_set_imsi(vsub, mi_string);
Oliver Smithcbf2c932019-05-06 13:09:55 +02001193 vlr_loc_upd_node1_pre(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001194 break;
1195 default:
Oliver Smithffd522e2019-05-10 14:39:37 +02001196 OSMO_ASSERT(0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001197 break;
1198 }
1199}
1200
1201/* At the end of Update_HLR_VLR */
1202static void lu_fsm_wait_hlr_ul_res(struct osmo_fsm_inst *fi, uint32_t event,
1203 void *data)
1204{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001205 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001206
1207 switch (event) {
1208 case VLR_ULA_E_HLR_LU_RES:
1209 /* pass-through this event to Update_HLR_VLR */
1210 if (data == NULL)
1211 osmo_fsm_inst_dispatch(lfp->upd_hlr_vlr_fsm, UPD_HLR_VLR_E_UPD_LOC_ACK, NULL);
1212 else
1213 osmo_fsm_inst_dispatch(lfp->upd_hlr_vlr_fsm, UPD_HLR_VLR_E_UPD_LOC_NACK, data);
1214 break;
1215 case VLR_ULA_E_UPD_HLR_COMPL:
1216 if (data == NULL) {
1217 /* successful case */
1218 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_LU_COMPL,
1219 LU_TIMEOUT_LONG, 0);
1220 vlr_loc_upd_start_lu_compl_fsm(fi);
1221 /* continue in MSC ?!? */
1222 } else {
1223 /* unsuccessful case */
Neels Hofmeyrdd2aeba2018-10-30 19:47:01 +01001224 enum gsm48_reject_value cause =
1225 *(enum gsm48_reject_value *)data;
Neels Hofmeyref9126c2017-07-18 15:38:39 +02001226 /* Ignoring standalone mode for now. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001227 if (0 /* procedure_error && vlr->cfg.standalone_mode */) {
1228 osmo_fsm_inst_state_chg(fi,
1229 VLR_ULA_S_WAIT_LU_COMPL_STANDALONE,
1230 LU_TIMEOUT_LONG, 0);
1231 vlr_loc_upd_start_lu_compl_fsm(fi);
1232 } else {
1233 lu_fsm_failure(fi, cause);
1234 }
1235 }
1236 break;
Neels Hofmeyr364f9272019-08-22 17:20:14 +02001237 case VLR_ULA_E_ID_IMEI:
1238 case VLR_ULA_E_ID_IMEISV:
1239 /* Got the IMEI from ME, nothing to do right now though. */
1240 break;
Harald Welteb8b85a12016-06-17 00:06:42 +02001241 default:
Oliver Smithffd522e2019-05-10 14:39:37 +02001242 OSMO_ASSERT(0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001243 break;
1244 }
1245}
1246
1247/* Wait for end of Location_Update_Completion_VLR */
1248static void lu_fsm_wait_lu_compl(struct osmo_fsm_inst *fi, uint32_t event,
1249 void *data)
1250{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001251 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001252 uint8_t cause;
1253
1254 switch (event) {
1255 case VLR_ULA_E_NEW_TMSI_ACK:
1256 osmo_fsm_inst_dispatch(lfp->lu_compl_vlr_fsm,
1257 LU_COMPL_VLR_E_NEW_TMSI_ACK, NULL);
1258 break;
1259 case VLR_ULA_E_ID_IMEI:
Neels Hofmeyr106ba522019-08-08 02:00:54 +02001260 case VLR_ULA_E_ID_IMEISV:
Oliver Smith7d053092018-12-14 17:37:38 +01001261 /* Got the IMEI from ME, now send it to HLR */
1262 vlr_subscr_tx_req_check_imei(lfp->vsub);
1263 break;
1264 case VLR_ULA_E_HLR_IMEI_ACK:
Harald Welteb8b85a12016-06-17 00:06:42 +02001265 osmo_fsm_inst_dispatch(lfp->lu_compl_vlr_fsm,
1266 LU_COMPL_VLR_E_IMEI_CHECK_ACK, NULL);
1267 break;
Oliver Smith7d053092018-12-14 17:37:38 +01001268 case VLR_ULA_E_HLR_IMEI_NACK:
1269 osmo_fsm_inst_dispatch(lfp->lu_compl_vlr_fsm,
1270 LU_COMPL_VLR_E_IMEI_CHECK_NACK, NULL);
1271 break;
Harald Welteb8b85a12016-06-17 00:06:42 +02001272 case VLR_ULA_E_LU_COMPL_SUCCESS:
1273 lu_fsm_discard_lu_compl_fsm(fi);
1274
1275 /* Update Register */
1276 /* TODO: Set_Notification_Type 23.078 */
1277 /* TODO: Notify_gsmSCF 23.078 */
1278 /* TODO: Authenticated Radio Contact Established -> ARC */
Stefan Sperling3a741282018-03-13 21:11:49 +01001279
1280 if (lfp->type == VLR_LU_TYPE_IMSI_ATTACH)
1281 lfp->vlr->ops.tx_mm_info(lfp->msc_conn_ref);
1282
Harald Welteb8b85a12016-06-17 00:06:42 +02001283 lu_fsm_success(fi);
1284 break;
1285 case VLR_ULA_E_LU_COMPL_FAILURE:
1286 cause = GSM48_REJECT_NETWORK_FAILURE;
1287 if (data)
1288 cause = *(uint8_t*)data;
1289 lu_fsm_discard_lu_compl_fsm(fi);
1290 lu_fsm_failure(fi, cause);
1291 break;
1292 default:
Oliver Smithffd522e2019-05-10 14:39:37 +02001293 OSMO_ASSERT(0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001294 break;
1295 }
1296}
1297
1298/* Wait for end of Location_Update_Completion_VLR (standalone case) */
1299static void lu_fsm_wait_lu_compl_standalone(struct osmo_fsm_inst *fi,
1300 uint32_t event, void *data)
1301{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001302 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001303 struct vlr_subscr *vsub = lfp->vsub;
1304 uint8_t cause;
1305
1306 switch (event) {
1307 case VLR_ULA_E_NEW_TMSI_ACK:
1308 osmo_fsm_inst_dispatch(lfp->lu_compl_vlr_fsm,
1309 LU_COMPL_VLR_E_NEW_TMSI_ACK, NULL);
1310 break;
1311 case VLR_ULA_E_LU_COMPL_SUCCESS:
1312 lu_fsm_discard_lu_compl_fsm(fi);
1313 vsub->sub_dataconf_by_hlr_ind = false;
1314 lu_fsm_success(fi);
1315 break;
1316 case VLR_ULA_E_LU_COMPL_FAILURE:
1317 vsub->sub_dataconf_by_hlr_ind = false;
1318 cause = GSM48_REJECT_NETWORK_FAILURE;
1319 if (data)
1320 cause = *(uint8_t*)data;
1321 lu_fsm_discard_lu_compl_fsm(fi);
1322 lu_fsm_failure(fi, cause);
1323 break;
1324 default:
Oliver Smithffd522e2019-05-10 14:39:37 +02001325 OSMO_ASSERT(0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001326 break;
1327 }
1328}
1329
1330static const struct osmo_fsm_state vlr_lu_fsm_states[] = {
1331 [VLR_ULA_S_IDLE] = {
1332 .in_event_mask = S(VLR_ULA_E_UPDATE_LA),
1333 .out_state_mask = S(VLR_ULA_S_WAIT_IMEISV) |
1334 S(VLR_ULA_S_WAIT_PVLR) |
1335 S(VLR_ULA_S_WAIT_IMSI) |
1336 S(VLR_ULA_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +01001337 S(VLR_ULA_S_WAIT_CIPH) |
Oliver Smithcbf2c932019-05-06 13:09:55 +02001338 S(VLR_ULA_S_WAIT_HLR_CHECK_IMEI_EARLY) |
Harald Welteb8b85a12016-06-17 00:06:42 +02001339 S(VLR_ULA_S_WAIT_HLR_UPD) |
1340 S(VLR_ULA_S_DONE),
1341 .name = OSMO_STRINGIFY(VLR_ULA_S_IDLE),
1342 .action = lu_fsm_idle,
1343 },
1344 [VLR_ULA_S_WAIT_IMEISV] = {
1345 .in_event_mask = S(VLR_ULA_E_ID_IMEISV),
1346 .out_state_mask = S(VLR_ULA_S_WAIT_PVLR) |
1347 S(VLR_ULA_S_WAIT_IMSI) |
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001348 S(VLR_ULA_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +01001349 S(VLR_ULA_S_WAIT_CIPH) |
Oliver Smithcbf2c932019-05-06 13:09:55 +02001350 S(VLR_ULA_S_WAIT_HLR_CHECK_IMEI_EARLY) |
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001351 S(VLR_ULA_S_WAIT_HLR_UPD) |
Harald Welteb8b85a12016-06-17 00:06:42 +02001352 S(VLR_ULA_S_DONE),
1353 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_IMEISV),
1354 .action = lu_fsm_wait_imeisv,
1355 },
1356 [VLR_ULA_S_WAIT_PVLR] = {
1357 .in_event_mask = S(VLR_ULA_E_SEND_ID_ACK) |
1358 S(VLR_ULA_E_SEND_ID_NACK),
1359 .out_state_mask = S(VLR_ULA_S_WAIT_IMSI) |
1360 S(VLR_ULA_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +01001361 S(VLR_ULA_S_WAIT_CIPH) |
Oliver Smithcbf2c932019-05-06 13:09:55 +02001362 S(VLR_ULA_S_WAIT_HLR_CHECK_IMEI_EARLY) |
Harald Welteb8b85a12016-06-17 00:06:42 +02001363 S(VLR_ULA_S_DONE),
1364 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_PVLR),
1365 .action = lu_fsm_wait_pvlr,
1366 },
1367 [VLR_ULA_S_WAIT_AUTH] = {
1368 .in_event_mask = S(VLR_ULA_E_AUTH_RES),
1369 .out_state_mask = S(VLR_ULA_S_WAIT_CIPH) |
1370 S(VLR_ULA_S_WAIT_LU_COMPL) |
1371 S(VLR_ULA_S_WAIT_HLR_UPD) |
1372 S(VLR_ULA_S_DONE),
1373 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_AUTH),
1374 .action = lu_fsm_wait_auth,
1375 },
1376 [VLR_ULA_S_WAIT_CIPH] = {
1377 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_CIPH),
1378 .in_event_mask = S(VLR_ULA_E_CIPH_RES),
1379 .out_state_mask = S(VLR_ULA_S_WAIT_LU_COMPL) |
1380 S(VLR_ULA_S_WAIT_HLR_UPD) |
1381 S(VLR_ULA_S_DONE),
1382 .action = lu_fsm_wait_ciph,
1383 },
1384 [VLR_ULA_S_WAIT_IMSI] = {
1385 .in_event_mask = S(VLR_ULA_E_ID_IMSI),
1386 .out_state_mask = S(VLR_ULA_S_WAIT_AUTH) |
Sylvain Munautda9f37e2019-03-14 11:02:36 +01001387 S(VLR_ULA_S_WAIT_CIPH) |
Oliver Smithcbf2c932019-05-06 13:09:55 +02001388 S(VLR_ULA_S_WAIT_HLR_CHECK_IMEI_EARLY) |
Harald Welteb8b85a12016-06-17 00:06:42 +02001389 S(VLR_ULA_S_WAIT_HLR_UPD) |
1390 S(VLR_ULA_S_DONE),
1391 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_IMSI),
1392 .action = lu_fsm_wait_imsi,
1393 },
Oliver Smithcbf2c932019-05-06 13:09:55 +02001394 [VLR_ULA_S_WAIT_HLR_CHECK_IMEI_EARLY] = {
1395 .in_event_mask = S(VLR_ULA_E_HLR_IMEI_ACK) |
1396 S(VLR_ULA_E_HLR_IMEI_NACK),
1397 .out_state_mask = S(VLR_ULA_S_WAIT_AUTH) |
1398 S(VLR_ULA_S_WAIT_CIPH) |
1399 S(VLR_ULA_S_WAIT_HLR_UPD) |
1400 S(VLR_ULA_S_WAIT_LU_COMPL) |
1401 S(VLR_ULA_S_DONE),
1402 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_HLR_CHECK_IMEI_EARLY),
1403 .action = lu_fsm_wait_hlr_check_imei_early,
1404 },
Harald Welteb8b85a12016-06-17 00:06:42 +02001405 [VLR_ULA_S_WAIT_HLR_UPD] = {
1406 .in_event_mask = S(VLR_ULA_E_HLR_LU_RES) |
Neels Hofmeyr364f9272019-08-22 17:20:14 +02001407 S(VLR_ULA_E_UPD_HLR_COMPL) |
1408 S(VLR_ULA_E_ID_IMEI) |
1409 S(VLR_ULA_E_ID_IMEISV),
Harald Welteb8b85a12016-06-17 00:06:42 +02001410 .out_state_mask = S(VLR_ULA_S_WAIT_LU_COMPL) |
1411 S(VLR_ULA_S_WAIT_LU_COMPL_STANDALONE) |
1412 S(VLR_ULA_S_DONE),
1413 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_HLR_UPD),
1414 .action = lu_fsm_wait_hlr_ul_res,
1415 },
1416 [VLR_ULA_S_WAIT_LU_COMPL] = {
1417 .in_event_mask = S(VLR_ULA_E_LU_COMPL_SUCCESS) |
1418 S(VLR_ULA_E_LU_COMPL_FAILURE) |
1419 S(VLR_ULA_E_NEW_TMSI_ACK) |
1420 S(VLR_ULA_E_ID_IMEI) |
Oliver Smith7d053092018-12-14 17:37:38 +01001421 S(VLR_ULA_E_ID_IMEISV) |
1422 S(VLR_ULA_E_HLR_IMEI_ACK) |
1423 S(VLR_ULA_E_HLR_IMEI_NACK),
Harald Welteb8b85a12016-06-17 00:06:42 +02001424 .out_state_mask = S(VLR_ULA_S_DONE),
1425 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_LU_COMPL),
1426 .action = lu_fsm_wait_lu_compl,
1427 },
1428 [VLR_ULA_S_WAIT_LU_COMPL_STANDALONE] = {
1429 .in_event_mask = S(VLR_ULA_E_LU_COMPL_SUCCESS) |
1430 S(VLR_ULA_E_LU_COMPL_FAILURE) |
1431 S(VLR_ULA_E_NEW_TMSI_ACK),
1432 .out_state_mask = S(VLR_ULA_S_DONE),
1433 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_LU_COMPL_STANDALONE),
1434 .action = lu_fsm_wait_lu_compl_standalone,
1435 },
1436 [VLR_ULA_S_DONE] = {
1437 .name = OSMO_STRINGIFY(VLR_ULA_S_DONE),
1438 .onenter = lu_fsm_dispatch_result,
1439 },
1440};
1441
1442static void fsm_lu_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
1443{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001444 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001445 struct vlr_subscr *vsub = lfp->vsub;
1446
1447 LOGPFSM(fi, "fsm_lu_cleanup called with cause %s\n",
1448 osmo_fsm_term_cause_name(cause));
1449 if (vsub && vsub->lu_fsm == fi)
1450 vsub->lu_fsm = NULL;
1451}
1452
1453static struct osmo_fsm vlr_lu_fsm = {
1454 .name = "vlr_lu_fsm",
1455 .states = vlr_lu_fsm_states,
1456 .num_states = ARRAY_SIZE(vlr_lu_fsm_states),
1457 .allstate_event_mask = 0,
1458 .allstate_action = NULL,
1459 .log_subsys = DVLR,
1460 .event_names = fsm_lu_event_names,
1461 .cleanup = fsm_lu_cleanup,
1462};
1463
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001464static inline struct lu_fsm_priv *lu_fsm_fi_priv(struct osmo_fsm_inst *fi)
1465{
1466 OSMO_ASSERT(fi->fsm == &vlr_lu_fsm);
1467 return (struct lu_fsm_priv*)fi->priv;
1468}
1469
Harald Welteb8b85a12016-06-17 00:06:42 +02001470struct osmo_fsm_inst *
1471vlr_loc_update(struct osmo_fsm_inst *parent,
1472 uint32_t parent_event_success,
1473 uint32_t parent_event_failure,
1474 void *parent_event_data,
1475 struct vlr_instance *vlr, void *msc_conn_ref,
1476 enum vlr_lu_type type, uint32_t tmsi, const char *imsi,
1477 const struct osmo_location_area_id *old_lai,
1478 const struct osmo_location_area_id *new_lai,
1479 bool authentication_required,
Harald Welte71c51df2017-12-23 18:51:48 +01001480 bool ciphering_required,
Sylvain Munautda9f37e2019-03-14 11:02:36 +01001481 uint8_t key_seq,
Harald Welteb8b85a12016-06-17 00:06:42 +02001482 bool is_r99, bool is_utran,
1483 bool assign_tmsi)
1484{
1485 struct osmo_fsm_inst *fi;
1486 struct lu_fsm_priv *lfp;
1487
1488 fi = osmo_fsm_inst_alloc_child(&vlr_lu_fsm, parent, parent_event_failure);
1489 if (!fi)
1490 return NULL;
1491
1492 lfp = talloc_zero(fi, struct lu_fsm_priv);
1493 lfp->vlr = vlr;
1494 lfp->msc_conn_ref = msc_conn_ref;
1495 lfp->tmsi = tmsi;
1496 lfp->type = type;
1497 lfp->old_lai = *old_lai;
1498 lfp->new_lai = *new_lai;
1499 lfp->lu_by_tmsi = true;
1500 lfp->parent_event_success = parent_event_success;
1501 lfp->parent_event_failure = parent_event_failure;
1502 lfp->parent_event_data = parent_event_data;
1503 lfp->authentication_required = authentication_required;
1504 lfp->ciphering_required = ciphering_required;
Sylvain Munautda9f37e2019-03-14 11:02:36 +01001505 lfp->key_seq = key_seq;
Harald Welteb8b85a12016-06-17 00:06:42 +02001506 lfp->is_r99 = is_r99;
1507 lfp->is_utran = is_utran;
1508 lfp->assign_tmsi = assign_tmsi;
1509 if (imsi) {
1510 strncpy(lfp->imsi, imsi, sizeof(lfp->imsi)-1);
1511 lfp->imsi[sizeof(lfp->imsi)-1] = '\0';
1512 lfp->lu_by_tmsi = false;
1513 }
1514 fi->priv = lfp;
1515
1516 LOGPFSM(fi, "rev=%s net=%s%s%s\n",
1517 is_r99 ? "R99" : "GSM",
1518 is_utran ? "UTRAN" : "GERAN",
1519 (authentication_required || ciphering_required)?
1520 " Auth" : " (no Auth)",
1521 (authentication_required || ciphering_required)?
1522 (ciphering_required? "+Ciph" : " (no Ciph)")
1523 : "");
1524
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001525 if (is_utran && !authentication_required)
1526 LOGPFSML(fi, LOGL_ERROR,
1527 "Authentication off on UTRAN network. Good luck.\n");
1528
Harald Welteb8b85a12016-06-17 00:06:42 +02001529 osmo_fsm_inst_dispatch(fi, VLR_ULA_E_UPDATE_LA, NULL);
1530
1531 return fi;
1532}
1533
Neels Hofmeyr15809592018-04-06 02:57:51 +02001534void vlr_loc_update_cancel(struct osmo_fsm_inst *fi,
1535 enum osmo_fsm_term_cause fsm_cause,
1536 uint8_t gsm48_cause)
Harald Welteb8b85a12016-06-17 00:06:42 +02001537{
Neels Hofmeyr15809592018-04-06 02:57:51 +02001538 struct lu_fsm_priv *lfp;
1539
1540 OSMO_ASSERT(fi);
1541 OSMO_ASSERT(fi->fsm == &vlr_lu_fsm);
1542
1543 lfp = fi->priv;
1544 lfp->rej_cause = gsm48_cause;
1545
1546 if (fi->state != VLR_ULA_S_DONE)
1547 lu_fsm_failure(fi, gsm48_cause);
Harald Welteb8b85a12016-06-17 00:06:42 +02001548}
1549
1550void vlr_lu_fsm_init(void)
1551{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001552 OSMO_ASSERT(osmo_fsm_register(&vlr_lu_fsm) == 0);
1553 OSMO_ASSERT(osmo_fsm_register(&upd_hlr_vlr_fsm) == 0);
1554 OSMO_ASSERT(osmo_fsm_register(&sub_pres_vlr_fsm) == 0);
1555 OSMO_ASSERT(osmo_fsm_register(&lu_compl_vlr_fsm) == 0);
Harald Welteb8b85a12016-06-17 00:06:42 +02001556}