blob: 908e0e3ca546426d8dd1e3d883ed5dc33496a62a [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"
30
31#define S(x) (1 << (x))
32
33#define LU_TIMEOUT_LONG 30
34
35enum vlr_fsm_result {
36 VLR_FSM_RESULT_NONE,
37 VLR_FSM_RESULT_SUCCESS,
38 VLR_FSM_RESULT_FAILURE,
39};
40
41
42/***********************************************************************
43 * Update_HLR_VLR, TS 23.012 Chapter 4.1.2.4
44 ***********************************************************************/
45
46enum upd_hlr_vlr_state {
47 UPD_HLR_VLR_S_INIT,
48 UPD_HLR_VLR_S_WAIT_FOR_DATA,
49 UPD_HLR_VLR_S_DONE,
50};
51
52enum upd_hlr_vlr_evt {
53 UPD_HLR_VLR_E_START,
54 UPD_HLR_VLR_E_INS_SUB_DATA,
55 UPD_HLR_VLR_E_ACT_TRACE_MODE,
56 UPD_HLR_VLR_E_FW_CHECK_SS_IND,
57 UPD_HLR_VLR_E_UPD_LOC_ACK,
58 UPD_HLR_VLR_E_UPD_LOC_NACK,
59};
60
61static const struct value_string upd_hlr_vlr_event_names[] = {
62 OSMO_VALUE_STRING(UPD_HLR_VLR_E_START),
63 OSMO_VALUE_STRING(UPD_HLR_VLR_E_INS_SUB_DATA),
64 OSMO_VALUE_STRING(UPD_HLR_VLR_E_ACT_TRACE_MODE),
65 OSMO_VALUE_STRING(UPD_HLR_VLR_E_FW_CHECK_SS_IND),
66 OSMO_VALUE_STRING(UPD_HLR_VLR_E_UPD_LOC_ACK),
67 OSMO_VALUE_STRING(UPD_HLR_VLR_E_UPD_LOC_NACK),
68 { 0, NULL }
69};
70
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +010071static inline struct vlr_subscr *upd_hlr_vlr_fi_priv(struct osmo_fsm_inst *fi);
72
Harald Welteb8b85a12016-06-17 00:06:42 +020073static void upd_hlr_vlr_fsm_init(struct osmo_fsm_inst *fi, uint32_t event,
74 void *data)
75{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +010076 struct vlr_subscr *vsub = upd_hlr_vlr_fi_priv(fi);
Max770fbd22018-01-24 12:48:33 +010077 int rc;
Harald Welteb8b85a12016-06-17 00:06:42 +020078
79 OSMO_ASSERT(event == UPD_HLR_VLR_E_START);
80
81 /* Send UpdateLocation to HLR */
Max770fbd22018-01-24 12:48:33 +010082 rc = vlr_subscr_req_lu(vsub, vsub->vlr->cfg.is_ps);
83 if (rc < 0)
84 LOGPFSML(fi, LOGL_ERROR, "Failed to send UpdateLocation to HLR\n");
Harald Welteb8b85a12016-06-17 00:06:42 +020085 osmo_fsm_inst_state_chg(fi, UPD_HLR_VLR_S_WAIT_FOR_DATA,
86 LU_TIMEOUT_LONG, 0);
87}
88
89static void upd_hlr_vlr_fsm_wait_data(struct osmo_fsm_inst *fi, uint32_t event,
90 void *data)
91{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +010092 struct vlr_subscr *vsub = upd_hlr_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +020093
94 switch (event) {
95 case UPD_HLR_VLR_E_INS_SUB_DATA:
96 /* FIXME: Insert_Subscr_Data_VLR */
97 break;
98 case UPD_HLR_VLR_E_ACT_TRACE_MODE:
99 /* TODO: Activate_Tracing_VLR */
100 break;
101 case UPD_HLR_VLR_E_FW_CHECK_SS_IND:
102 /* TODO: Forward Check SS Ind to MSC */
103 break;
104 case UPD_HLR_VLR_E_UPD_LOC_ACK:
105 /* Inside Update_HLR_VLR after UpdateLocationAck */
106 vsub->sub_dataconf_by_hlr_ind = true;
107 vsub->loc_conf_in_hlr_ind = true;
108 osmo_fsm_inst_state_chg(fi, UPD_HLR_VLR_S_DONE, 0, 0);
109 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
110 break;
111 case UPD_HLR_VLR_E_UPD_LOC_NACK:
112 /* Inside Update_HLR_VLR after UpdateLocationNack */
113 /* TODO: Check_User_Error_In_Serving_Network_Entity */
114 vsub->sub_dataconf_by_hlr_ind = false;
115 vsub->loc_conf_in_hlr_ind = false;
116 osmo_fsm_inst_state_chg(fi, UPD_HLR_VLR_S_DONE, 0, 0);
117 /* Data is a pointer to a gsm48_gmm_cause which we
118 * simply pass through */
119 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, data);
120 break;
121 }
122}
123
124static const struct osmo_fsm_state upd_hlr_vlr_states[] = {
125 [UPD_HLR_VLR_S_INIT] = {
126 .in_event_mask = S(UPD_HLR_VLR_E_START),
127 .out_state_mask = S(UPD_HLR_VLR_S_WAIT_FOR_DATA),
128 .name = OSMO_STRINGIFY(UPD_HLR_VLR_S_INIT),
129 .action = upd_hlr_vlr_fsm_init,
130 },
131 [UPD_HLR_VLR_S_WAIT_FOR_DATA] = {
132 .in_event_mask = S(UPD_HLR_VLR_E_INS_SUB_DATA) |
133 S(UPD_HLR_VLR_E_ACT_TRACE_MODE) |
134 S(UPD_HLR_VLR_E_FW_CHECK_SS_IND) |
135 S(UPD_HLR_VLR_E_UPD_LOC_ACK) |
136 S(UPD_HLR_VLR_E_UPD_LOC_NACK),
137 .out_state_mask = S(UPD_HLR_VLR_S_DONE),
138 .name = OSMO_STRINGIFY(UPD_HLR_VLR_S_WAIT_FOR_DATA),
139 .action = upd_hlr_vlr_fsm_wait_data,
140 },
141 [UPD_HLR_VLR_S_DONE] = {
142 .name = OSMO_STRINGIFY(UPD_HLR_VLR_S_DONE),
143 },
144};
145
146static struct osmo_fsm upd_hlr_vlr_fsm = {
147 .name = "upd_hlr_vlr_fsm",
148 .states = upd_hlr_vlr_states,
149 .num_states = ARRAY_SIZE(upd_hlr_vlr_states),
150 .allstate_event_mask = 0,
151 .allstate_action = NULL,
152 .log_subsys = DVLR,
153 .event_names = upd_hlr_vlr_event_names,
154};
155
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100156static inline struct vlr_subscr *upd_hlr_vlr_fi_priv(struct osmo_fsm_inst *fi)
157{
158 OSMO_ASSERT(fi->fsm == &upd_hlr_vlr_fsm);
159 return (struct vlr_subscr*)fi->priv;
160}
161
Harald Welteb8b85a12016-06-17 00:06:42 +0200162struct osmo_fsm_inst *
163upd_hlr_vlr_proc_start(struct osmo_fsm_inst *parent,
164 struct vlr_subscr *vsub,
165 uint32_t parent_event)
166{
167 struct osmo_fsm_inst *fi;
168
169 fi = osmo_fsm_inst_alloc_child(&upd_hlr_vlr_fsm, parent,
170 parent_event);
171 if (!fi)
172 return NULL;
173
174 fi->priv = vsub;
175 osmo_fsm_inst_dispatch(fi, UPD_HLR_VLR_E_START, NULL);
176
177 return fi;
178}
179
180
181/***********************************************************************
182 * Subscriber_Present_VLR, TS 29.002 Chapter 25.10.1
183 ***********************************************************************/
184
185enum sub_pres_vlr_state {
186 SUB_PRES_VLR_S_INIT,
187 SUB_PRES_VLR_S_WAIT_FOR_HLR,
188 SUB_PRES_VLR_S_DONE,
189};
190
191enum sub_pres_vlr_event {
192 SUB_PRES_VLR_E_START,
193 SUB_PRES_VLR_E_READY_SM_CNF,
194 SUB_PRES_VLR_E_READY_SM_ERR,
195};
196
197static const struct value_string sub_pres_vlr_event_names[] = {
198 OSMO_VALUE_STRING(SUB_PRES_VLR_E_START),
199 OSMO_VALUE_STRING(SUB_PRES_VLR_E_READY_SM_CNF),
200 OSMO_VALUE_STRING(SUB_PRES_VLR_E_READY_SM_ERR),
201 { 0, NULL }
202};
203
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100204static inline struct vlr_subscr *sub_pres_vlr_fi_priv(struct osmo_fsm_inst *fi);
205
Harald Welteb8b85a12016-06-17 00:06:42 +0200206static void sub_pres_vlr_fsm_init(struct osmo_fsm_inst *fi, uint32_t event,
207 void *data)
208{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100209 struct vlr_subscr *vsub = sub_pres_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200210 OSMO_ASSERT(event == SUB_PRES_VLR_E_START);
211
212 if (!vsub->ms_not_reachable_flag) {
213 osmo_fsm_inst_state_chg(fi, SUB_PRES_VLR_S_DONE, 0, 0);
214 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
215 return;
216 }
217 /* FIXME: Send READY_FOR_SM via GSUP */
218 osmo_fsm_inst_state_chg(fi, SUB_PRES_VLR_S_WAIT_FOR_HLR,
219 LU_TIMEOUT_LONG, 0);
220}
221
222static void sub_pres_vlr_fsm_wait_hlr(struct osmo_fsm_inst *fi, uint32_t event,
223 void *data)
224{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100225 struct vlr_subscr *vsub = sub_pres_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200226
227 switch (event) {
228 case SUB_PRES_VLR_E_READY_SM_CNF:
229 vsub->ms_not_reachable_flag = false;
230 break;
231 case SUB_PRES_VLR_E_READY_SM_ERR:
232 break;
233 }
234 osmo_fsm_inst_state_chg(fi, SUB_PRES_VLR_S_DONE, 0, 0);
235 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, NULL);
236}
237
238static const struct osmo_fsm_state sub_pres_vlr_states[] = {
239 [SUB_PRES_VLR_S_INIT] = {
240 .in_event_mask = S(SUB_PRES_VLR_E_START),
241 .out_state_mask = S(SUB_PRES_VLR_S_WAIT_FOR_HLR) |
242 S(SUB_PRES_VLR_S_DONE),
243 .name = OSMO_STRINGIFY(SUB_PRES_VLR_S_INIT),
244 .action = sub_pres_vlr_fsm_init,
245 },
246 [SUB_PRES_VLR_S_WAIT_FOR_HLR] = {
247 .in_event_mask = S(SUB_PRES_VLR_E_READY_SM_CNF) |
248 S(SUB_PRES_VLR_E_READY_SM_ERR),
249 .out_state_mask = S(SUB_PRES_VLR_S_DONE),
250 .name = OSMO_STRINGIFY(SUB_PRES_VLR_S_WAIT_FOR_HLR),
251 .action = sub_pres_vlr_fsm_wait_hlr,
252 },
253 [SUB_PRES_VLR_S_DONE] = {
254 .name = OSMO_STRINGIFY(SUB_PRES_VLR_S_DONE),
255 },
256};
257
258static struct osmo_fsm sub_pres_vlr_fsm = {
259 .name = "sub_pres_vlr_fsm",
260 .states = sub_pres_vlr_states,
261 .num_states = ARRAY_SIZE(sub_pres_vlr_states),
262 .allstate_event_mask = 0,
263 .allstate_action = NULL,
264 .log_subsys = DVLR,
265 .event_names = sub_pres_vlr_event_names,
266};
267
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100268static inline struct vlr_subscr *sub_pres_vlr_fi_priv(struct osmo_fsm_inst *fi)
269{
270 OSMO_ASSERT(fi->fsm == &sub_pres_vlr_fsm);
271 return (struct vlr_subscr*)fi->priv;
272}
273
Neels Hofmeyr1a5bcd52017-11-18 22:19:55 +0100274/* Note that the start event is dispatched right away, so in case the FSM immediately concludes from that
275 * event, the created FSM struct may no longer be valid as it already deallocated again, and it may
276 * furthermore already have invoked the parent FSM instance's deallocation as well. Hence, instead of
277 * returning, store the created FSM instance address in *fi_p before dispatching the event. It is thus
278 * possible to store the instance's pointer in a parent FSM instance without running danger of using
279 * already freed memory. */
280void sub_pres_vlr_fsm_start(struct osmo_fsm_inst **fi_p,
281 struct osmo_fsm_inst *parent,
282 struct vlr_subscr *vsub,
283 uint32_t term_event)
Harald Welteb8b85a12016-06-17 00:06:42 +0200284{
285 struct osmo_fsm_inst *fi;
286
Neels Hofmeyr1a5bcd52017-11-18 22:19:55 +0100287 OSMO_ASSERT(fi_p);
288
Harald Welteb8b85a12016-06-17 00:06:42 +0200289 fi = osmo_fsm_inst_alloc_child(&sub_pres_vlr_fsm, parent,
290 term_event);
Neels Hofmeyr1a5bcd52017-11-18 22:19:55 +0100291 *fi_p = fi;
Harald Welteb8b85a12016-06-17 00:06:42 +0200292 if (!fi)
Neels Hofmeyr1a5bcd52017-11-18 22:19:55 +0100293 return;
Harald Welteb8b85a12016-06-17 00:06:42 +0200294
295 fi->priv = vsub;
296 osmo_fsm_inst_dispatch(fi, SUB_PRES_VLR_E_START, NULL);
Harald Welteb8b85a12016-06-17 00:06:42 +0200297}
298
299/***********************************************************************
300 * Location_Update_Completion_VLR, TS 23.012 Chapter 4.1.2.3
301 ***********************************************************************/
302
303enum lu_compl_vlr_state {
304 LU_COMPL_VLR_S_INIT,
305 LU_COMPL_VLR_S_WAIT_SUB_PRES,
306 LU_COMPL_VLR_S_WAIT_IMEI,
307 LU_COMPL_VLR_S_WAIT_IMEI_TMSI,
308 LU_COMPL_VLR_S_WAIT_TMSI_CNF,
309 LU_COMPL_VLR_S_DONE,
310};
311
312enum lu_compl_vlr_event {
313 LU_COMPL_VLR_E_START,
314 LU_COMPL_VLR_E_SUB_PRES_COMPL,
315 LU_COMPL_VLR_E_IMEI_CHECK_ACK,
316 LU_COMPL_VLR_E_IMEI_CHECK_NACK,
317 LU_COMPL_VLR_E_NEW_TMSI_ACK,
318};
319
320static const struct value_string lu_compl_vlr_event_names[] = {
321 OSMO_VALUE_STRING(LU_COMPL_VLR_E_START),
322 OSMO_VALUE_STRING(LU_COMPL_VLR_E_SUB_PRES_COMPL),
323 OSMO_VALUE_STRING(LU_COMPL_VLR_E_IMEI_CHECK_ACK),
324 OSMO_VALUE_STRING(LU_COMPL_VLR_E_IMEI_CHECK_NACK),
325 OSMO_VALUE_STRING(LU_COMPL_VLR_E_NEW_TMSI_ACK),
326 { 0, NULL }
327};
328
329struct lu_compl_vlr_priv {
330 struct vlr_subscr *vsub;
331 void *msc_conn_ref;
332 struct osmo_fsm_inst *sub_pres_vlr_fsm;
333 uint32_t parent_event_success;
334 uint32_t parent_event_failure;
335 void *parent_event_data;
336 enum vlr_fsm_result result;
337 uint8_t cause;
338 bool assign_tmsi;
339};
340
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100341static inline struct lu_compl_vlr_priv *lu_compl_vlr_fi_priv(struct osmo_fsm_inst *fi);
342
Harald Welteb8b85a12016-06-17 00:06:42 +0200343static void _vlr_lu_compl_fsm_done(struct osmo_fsm_inst *fi,
344 enum vlr_fsm_result result,
345 uint8_t cause)
346{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100347 struct lu_compl_vlr_priv *lcvp = lu_compl_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200348 lcvp->result = result;
349 lcvp->cause = cause;
350 osmo_fsm_inst_state_chg(fi, LU_COMPL_VLR_S_DONE, 0, 0);
351}
352
353static void vlr_lu_compl_fsm_success(struct osmo_fsm_inst *fi)
354{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100355 struct lu_compl_vlr_priv *lcvp = lu_compl_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200356 struct vlr_subscr *vsub = lcvp->vsub;
357 if (!vsub->lu_complete) {
358 vsub->lu_complete = true;
359 /* Balanced by vlr_subscr_rx_imsi_detach() */
360 vlr_subscr_get(vsub);
361 }
362 _vlr_lu_compl_fsm_done(fi, VLR_FSM_RESULT_SUCCESS, 0);
363}
364
365static void vlr_lu_compl_fsm_failure(struct osmo_fsm_inst *fi, uint8_t cause)
366{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100367 struct lu_compl_vlr_priv *lcvp = lu_compl_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200368 lcvp->vsub->vlr->ops.tx_lu_rej(lcvp->msc_conn_ref, cause);
369 _vlr_lu_compl_fsm_done(fi, VLR_FSM_RESULT_FAILURE, cause);
370}
371
372static void vlr_lu_compl_fsm_dispatch_result(struct osmo_fsm_inst *fi,
373 uint32_t prev_state)
374{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100375 struct lu_compl_vlr_priv *lcvp = lu_compl_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200376 if (!fi->proc.parent) {
377 LOGPFSML(fi, LOGL_ERROR, "No parent FSM\n");
378 return;
379 }
380 osmo_fsm_inst_dispatch(fi->proc.parent,
381 (lcvp->result == VLR_FSM_RESULT_SUCCESS)
382 ? lcvp->parent_event_success
383 : lcvp->parent_event_failure,
384 &lcvp->cause);
385}
386
387static void lu_compl_vlr_init(struct osmo_fsm_inst *fi, uint32_t event,
388 void *data)
389{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100390 struct lu_compl_vlr_priv *lcvp = lu_compl_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200391 struct vlr_subscr *vsub = lcvp->vsub;
392 struct vlr_instance *vlr;
393 OSMO_ASSERT(vsub);
394 vlr = vsub->vlr;
395 OSMO_ASSERT(vlr);
396
397 OSMO_ASSERT(event == LU_COMPL_VLR_E_START);
398
399 /* TODO: National Roaming restrictions? */
400 /* TODO: Roaming restriction due to unsupported feature in subscriber
401 * data? */
402 /* TODO: Regional subscription restriction? */
403 /* TODO: Administrative restriction of subscribres' access feature? */
404 /* TODO: AccessRestrictuionData parameter available? */
405 /* TODO: AccessRestrictionData permits RAT? */
406 /* Node 1 */
407 /* TODO: Autonomous CSG supported in VPLMN and allowed by HPLMN? */
408 /* TODO: Hybrid Cel / CSG Cell */
409 /* Node 2 */
410 vsub->la_allowed = true;
411 vsub->imsi_detached_flag = false;
412 /* Start Subscriber_Present_VLR Procedure */
413 osmo_fsm_inst_state_chg(fi, LU_COMPL_VLR_S_WAIT_SUB_PRES,
414 LU_TIMEOUT_LONG, 0);
415
Neels Hofmeyr1a5bcd52017-11-18 22:19:55 +0100416 sub_pres_vlr_fsm_start(&lcvp->sub_pres_vlr_fsm, fi, vsub, LU_COMPL_VLR_E_SUB_PRES_COMPL);
Harald Welteb8b85a12016-06-17 00:06:42 +0200417}
418
419static void lu_compl_vlr_new_tmsi(struct osmo_fsm_inst *fi)
420{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100421 struct lu_compl_vlr_priv *lcvp = lu_compl_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200422 struct vlr_subscr *vsub = lcvp->vsub;
423 struct vlr_instance *vlr = vsub->vlr;
424
425 LOGPFSM(fi, "%s()\n", __func__);
426
427 if (vlr_subscr_alloc_tmsi(vsub)) {
428 vlr_lu_compl_fsm_failure(fi,
429 GSM48_REJECT_SRV_OPT_TMP_OUT_OF_ORDER);
430 return;
431 }
432
433 osmo_fsm_inst_state_chg(fi, LU_COMPL_VLR_S_WAIT_TMSI_CNF,
434 vlr_timer(vlr, 3250), 3250);
435
436 vlr->ops.tx_lu_acc(lcvp->msc_conn_ref, vsub->tmsi_new);
437}
438
439/* After completion of Subscriber_Present_VLR */
440static void lu_compl_vlr_wait_subscr_pres(struct osmo_fsm_inst *fi,
441 uint32_t event,
442 void *data)
443{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100444 struct lu_compl_vlr_priv *lcvp = lu_compl_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200445 struct vlr_subscr *vsub = lcvp->vsub;
446 struct vlr_instance *vlr = vsub->vlr;
447
448 OSMO_ASSERT(event == LU_COMPL_VLR_E_SUB_PRES_COMPL);
449
450 lcvp->sub_pres_vlr_fsm = NULL;
451
452 /* TODO: Trace_Subscriber_Activity_VLR */
453
454 if (vlr->cfg.check_imei_rqd) {
455 /* Check IMEI VLR */
456 osmo_fsm_inst_state_chg(fi,
457 lcvp->assign_tmsi ?
458 LU_COMPL_VLR_S_WAIT_IMEI_TMSI
459 : LU_COMPL_VLR_S_WAIT_IMEI,
460 vlr_timer(vlr, 3270), 3270);
461 vlr->ops.tx_id_req(lcvp->msc_conn_ref, GSM_MI_TYPE_IMEI);
462 return;
463 }
464
465 /* Do we need to allocate a TMSI? */
466 if (lcvp->assign_tmsi) {
467 lu_compl_vlr_new_tmsi(fi);
468 return;
469 }
470
471 /* Location Updating Accept */
472 vlr->ops.tx_lu_acc(lcvp->msc_conn_ref, GSM_RESERVED_TMSI);
473 vlr_lu_compl_fsm_success(fi);
474}
475
476/* Waiting for completion of CHECK_IMEI_VLR */
477static void lu_compl_vlr_wait_imei(struct osmo_fsm_inst *fi, uint32_t event,
478 void *data)
479{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100480 struct lu_compl_vlr_priv *lcvp = lu_compl_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200481 struct vlr_subscr *vsub = lcvp->vsub;
482 struct vlr_instance *vlr = vsub->vlr;
483
484 switch (event) {
485 case LU_COMPL_VLR_E_IMEI_CHECK_ACK:
486 if (!vsub->imei[0]) {
487 /* Abort: Do nothing */
488 vlr_lu_compl_fsm_failure(fi,
489 GSM48_REJECT_PROTOCOL_ERROR);
490 return;
491 }
492 /* Pass */
493 break;
494
495 case LU_COMPL_VLR_E_IMEI_CHECK_NACK:
496 vlr_lu_compl_fsm_failure(fi, GSM48_REJECT_ILLEGAL_ME);
497 /* FIXME: IMEI Check Fail to VLR Application (Detach IMSI VLR) */
498 return;
499 }
500
501 /* IMEI is available. Allocate TMSI if needed. */
502 if (lcvp->assign_tmsi) {
503 if (fi->state != LU_COMPL_VLR_S_WAIT_IMEI_TMSI)
504 LOGPFSML(fi, LOGL_ERROR,
505 "TMSI required, expected to be in state"
506 " LU_COMPL_VLR_S_WAIT_IMEI_TMSI,"
507 " am in %s instead\n",
508 osmo_fsm_state_name(fi->fsm, fi->state));
509 /* Logged an error, continue anyway. */
510
511 lu_compl_vlr_new_tmsi(fi);
512
513 /* Wait for TMSI ack */
514 return;
515 }
516
517 /* No TMSI needed, accept now. */
518 vlr->ops.tx_lu_acc(lcvp->msc_conn_ref, GSM_RESERVED_TMSI);
519 vlr_lu_compl_fsm_success(fi);
520}
521
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100522static inline struct lu_compl_vlr_priv *lu_compl_vlr_fi_priv(struct osmo_fsm_inst *fi);
523
Harald Welteb8b85a12016-06-17 00:06:42 +0200524/* Waiting for TMSI confirmation */
525static void lu_compl_vlr_wait_tmsi(struct osmo_fsm_inst *fi, uint32_t event,
526 void *data)
527{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100528 struct lu_compl_vlr_priv *lcvp = lu_compl_vlr_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200529 struct vlr_subscr *vsub = lcvp->vsub;
530
531 OSMO_ASSERT(event == LU_COMPL_VLR_E_NEW_TMSI_ACK);
532
533 if (!vsub || vsub->tmsi_new == GSM_RESERVED_TMSI) {
534 LOGPFSML(fi, LOGL_ERROR, "TMSI Realloc Compl implies that"
535 " the subscriber has a new TMSI allocated, but"
536 " the new TMSI is unset.\n");
537 vlr_lu_compl_fsm_failure(fi, GSM48_REJECT_NETWORK_FAILURE);
538 return;
539 }
540
541 vsub->tmsi = vsub->tmsi_new;
542 vsub->tmsi_new = GSM_RESERVED_TMSI;
543
544 vlr_lu_compl_fsm_success(fi);
545}
546
547static const struct osmo_fsm_state lu_compl_vlr_states[] = {
548 [LU_COMPL_VLR_S_INIT] = {
549 .in_event_mask = S(LU_COMPL_VLR_E_START),
550 .out_state_mask = S(LU_COMPL_VLR_S_DONE) |
551 S(LU_COMPL_VLR_S_WAIT_SUB_PRES) |
552 S(LU_COMPL_VLR_S_WAIT_IMEI),
553 .name = OSMO_STRINGIFY(LU_COMPL_VLR_S_INIT),
554 .action = lu_compl_vlr_init,
555 },
556 [LU_COMPL_VLR_S_WAIT_SUB_PRES] = {
557 .in_event_mask = S(LU_COMPL_VLR_E_SUB_PRES_COMPL),
558 .out_state_mask = S(LU_COMPL_VLR_S_WAIT_IMEI) |
559 S(LU_COMPL_VLR_S_WAIT_IMEI_TMSI) |
560 S(LU_COMPL_VLR_S_WAIT_TMSI_CNF) |
561 S(LU_COMPL_VLR_S_DONE),
562 .name = OSMO_STRINGIFY(LU_COMPL_VLR_S_WAIT_SUB_PRES),
563 .action = lu_compl_vlr_wait_subscr_pres,
564 },
565 [LU_COMPL_VLR_S_WAIT_IMEI] = {
566 .in_event_mask = S(LU_COMPL_VLR_E_IMEI_CHECK_ACK) |
567 S(LU_COMPL_VLR_E_IMEI_CHECK_NACK),
568 .out_state_mask = S(LU_COMPL_VLR_S_DONE),
569 .name = OSMO_STRINGIFY(LU_COMPL_VLR_S_WAIT_IMEI),
570 .action = lu_compl_vlr_wait_imei,
571 },
572 [LU_COMPL_VLR_S_WAIT_IMEI_TMSI] = {
573 .in_event_mask = S(LU_COMPL_VLR_E_IMEI_CHECK_ACK) |
574 S(LU_COMPL_VLR_E_IMEI_CHECK_NACK),
575 .out_state_mask = S(LU_COMPL_VLR_S_DONE) |
576 S(LU_COMPL_VLR_S_WAIT_TMSI_CNF),
577 .name = OSMO_STRINGIFY(LU_COMPL_VLR_S_WAIT_IMEI_TMSI),
578 .action = lu_compl_vlr_wait_imei,
579 },
580 [LU_COMPL_VLR_S_WAIT_TMSI_CNF] = {
581 .in_event_mask = S(LU_COMPL_VLR_E_NEW_TMSI_ACK),
582 .out_state_mask = S(LU_COMPL_VLR_S_DONE),
583 .name = OSMO_STRINGIFY(LU_COMPL_VLR_S_WAIT_TMSI_CNF),
584 .action = lu_compl_vlr_wait_tmsi,
585 },
586 [LU_COMPL_VLR_S_DONE] = {
587 .name = OSMO_STRINGIFY(LU_COMPL_VLR_S_DONE),
588 .onenter = vlr_lu_compl_fsm_dispatch_result,
589 },
590};
591
592static struct osmo_fsm lu_compl_vlr_fsm = {
593 .name = "lu_compl_vlr_fsm",
594 .states = lu_compl_vlr_states,
595 .num_states = ARRAY_SIZE(lu_compl_vlr_states),
596 .allstate_event_mask = 0,
597 .allstate_action = NULL,
598 .log_subsys = DVLR,
599 .event_names = lu_compl_vlr_event_names,
600};
601
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100602static inline struct lu_compl_vlr_priv *lu_compl_vlr_fi_priv(struct osmo_fsm_inst *fi)
603{
604 OSMO_ASSERT(fi->fsm == &lu_compl_vlr_fsm);
605 return (struct lu_compl_vlr_priv*)fi->priv;
606}
607
Harald Welteb8b85a12016-06-17 00:06:42 +0200608struct osmo_fsm_inst *
609lu_compl_vlr_proc_alloc(struct osmo_fsm_inst *parent,
610 struct vlr_subscr *vsub,
611 void *msc_conn_ref,
612 uint32_t parent_event_success,
613 uint32_t parent_event_failure,
614 bool assign_tmsi)
615{
616 struct osmo_fsm_inst *fi;
617 struct lu_compl_vlr_priv *lcvp;
618
619 fi = osmo_fsm_inst_alloc_child(&lu_compl_vlr_fsm, parent,
620 parent_event_failure);
621 if (!fi)
622 return NULL;
623
624 lcvp = talloc_zero(fi, struct lu_compl_vlr_priv);
625 lcvp->vsub = vsub;
626 lcvp->msc_conn_ref = msc_conn_ref;
627 lcvp->parent_event_success = parent_event_success;
628 lcvp->parent_event_failure = parent_event_failure;
629 lcvp->assign_tmsi = assign_tmsi;
630 fi->priv = lcvp;
631
632 return fi;
633}
634
635
636/***********************************************************************
637 * Update_Location_Area_VLR, TS 23.012 Chapter 4.1.2.1
638 ***********************************************************************/
639
640static const struct value_string fsm_lu_event_names[] = {
641 OSMO_VALUE_STRING(VLR_ULA_E_UPDATE_LA),
642 OSMO_VALUE_STRING(VLR_ULA_E_SEND_ID_ACK),
643 OSMO_VALUE_STRING(VLR_ULA_E_SEND_ID_NACK),
644 OSMO_VALUE_STRING(VLR_ULA_E_AUTH_RES),
645 OSMO_VALUE_STRING(VLR_ULA_E_CIPH_RES),
646 OSMO_VALUE_STRING(VLR_ULA_E_ID_IMSI),
647 OSMO_VALUE_STRING(VLR_ULA_E_ID_IMEI),
648 OSMO_VALUE_STRING(VLR_ULA_E_ID_IMEISV),
649 OSMO_VALUE_STRING(VLR_ULA_E_HLR_LU_RES),
650 OSMO_VALUE_STRING(VLR_ULA_E_UPD_HLR_COMPL),
651 OSMO_VALUE_STRING(VLR_ULA_E_LU_COMPL_SUCCESS),
652 OSMO_VALUE_STRING(VLR_ULA_E_LU_COMPL_FAILURE),
653 OSMO_VALUE_STRING(VLR_ULA_E_NEW_TMSI_ACK),
654 { 0, NULL }
655};
656
657struct lu_fsm_priv {
658 struct vlr_instance *vlr;
659 struct vlr_subscr *vsub;
660 void *msc_conn_ref;
661 struct osmo_fsm_inst *upd_hlr_vlr_fsm;
662 struct osmo_fsm_inst *lu_compl_vlr_fsm;
663 uint32_t parent_event_success;
664 uint32_t parent_event_failure;
665 void *parent_event_data;
666 enum vlr_fsm_result result;
667 uint8_t rej_cause;
668
669 enum vlr_lu_type type;
670 bool lu_by_tmsi;
671 char imsi[16];
672 uint32_t tmsi;
673 struct osmo_location_area_id old_lai;
674 struct osmo_location_area_id new_lai;
675 bool authentication_required;
Harald Welte71c51df2017-12-23 18:51:48 +0100676 bool ciphering_required;
Harald Welteb8b85a12016-06-17 00:06:42 +0200677 bool is_r99;
678 bool is_utran;
679 bool assign_tmsi;
680};
681
682
683/* Determine if given location area is served by this VLR */
684static bool lai_in_this_vlr(struct vlr_instance *vlr,
685 const struct osmo_location_area_id *lai)
686{
687 /* TODO: VLR needs to keep a locally configued list of LAIs */
688 return true;
689}
690
691/* Determine if authentication is required */
692static bool is_auth_required(struct lu_fsm_priv *lfp)
693{
694 /* The cases where the authentication procedure should be used
695 * are defined in 3GPP TS 33.102 */
696 /* For now we use a default value passed in to vlr_lu_fsm(). */
Harald Welte71c51df2017-12-23 18:51:48 +0100697 return lfp->authentication_required || lfp->ciphering_required;
Harald Welteb8b85a12016-06-17 00:06:42 +0200698}
699
700/* Determine if ciphering is required */
701static bool is_ciph_required(struct lu_fsm_priv *lfp)
702{
Harald Welte71c51df2017-12-23 18:51:48 +0100703 return lfp->ciphering_required;
Harald Welteb8b85a12016-06-17 00:06:42 +0200704}
705
706/* Determine if a HLR Update is required */
707static bool hlr_update_needed(struct vlr_subscr *vsub)
708{
709 /* TODO: properly decide this, rather than always assuming we
710 * need to update the HLR. */
711 return true;
712}
713
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100714static inline struct lu_fsm_priv *lu_fsm_fi_priv(struct osmo_fsm_inst *fi);
715
Harald Welteb8b85a12016-06-17 00:06:42 +0200716static void lu_fsm_dispatch_result(struct osmo_fsm_inst *fi,
717 uint32_t prev_state)
718{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100719 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200720 if (!fi->proc.parent) {
721 LOGPFSML(fi, LOGL_ERROR, "No parent FSM\n");
722 return;
723 }
724 osmo_fsm_inst_dispatch(fi->proc.parent,
725 (lfp->result == VLR_FSM_RESULT_SUCCESS)
726 ? lfp->parent_event_success
727 : lfp->parent_event_failure,
728 lfp->parent_event_data);
729}
730
731static void _lu_fsm_done(struct osmo_fsm_inst *fi,
732 enum vlr_fsm_result result)
733{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100734 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200735 lfp->result = result;
736 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_DONE, 0, 0);
737}
738
739static void lu_fsm_success(struct osmo_fsm_inst *fi)
740{
741 _lu_fsm_done(fi, VLR_FSM_RESULT_SUCCESS);
742}
743
744static void lu_fsm_failure(struct osmo_fsm_inst *fi, uint8_t rej_cause)
745{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100746 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200747 if (rej_cause)
748 lfp->vlr->ops.tx_lu_rej(lfp->msc_conn_ref, rej_cause);
749 _lu_fsm_done(fi, VLR_FSM_RESULT_FAILURE);
750}
751
752static void vlr_loc_upd_start_lu_compl_fsm(struct osmo_fsm_inst *fi)
753{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100754 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200755 lfp->lu_compl_vlr_fsm =
756 lu_compl_vlr_proc_alloc(fi, lfp->vsub, lfp->msc_conn_ref,
757 VLR_ULA_E_LU_COMPL_SUCCESS,
758 VLR_ULA_E_LU_COMPL_FAILURE,
759 lfp->assign_tmsi);
760
761 osmo_fsm_inst_dispatch(lfp->lu_compl_vlr_fsm, LU_COMPL_VLR_E_START, NULL);
762}
763
764static void lu_fsm_discard_lu_compl_fsm(struct osmo_fsm_inst *fi)
765{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100766 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200767 if (!lfp->lu_compl_vlr_fsm)
768 return;
769 osmo_fsm_inst_term(lfp->lu_compl_vlr_fsm, OSMO_FSM_TERM_PARENT, NULL);
770}
771
772/* 4.1.2.1 Node 4 */
773static void vlr_loc_upd_node_4(struct osmo_fsm_inst *fi)
774{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100775 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200776 struct vlr_subscr *vsub = lfp->vsub;
777 bool hlr_unknown = false;
778
779 LOGPFSM(fi, "%s()\n", __func__);
780
781 if (hlr_unknown) {
782 /* FIXME: Delete subscriber record */
783 /* LU REJ: Roaming not allowed */
784 lu_fsm_failure(fi, GSM48_REJECT_ROAMING_NOT_ALLOWED);
785 } else {
786 /* Update_HLR_VLR */
787 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_HLR_UPD,
788 LU_TIMEOUT_LONG, 0);
789 lfp->upd_hlr_vlr_fsm =
790 upd_hlr_vlr_proc_start(fi, vsub, VLR_ULA_E_UPD_HLR_COMPL);
791 }
792}
793
794/* 4.1.2.1 Node B */
795static void vlr_loc_upd_node_b(struct osmo_fsm_inst *fi)
796{
797 LOGPFSM(fi, "%s()\n", __func__);
798
Neels Hofmeyref9126c2017-07-18 15:38:39 +0200799 /* OsmoHLR does not support PgA, neither stores the IMEISV, so we have no need to update the HLR
800 * with either. TODO: depend on actual HLR configuration. See 3GPP TS 23.012 Release 14, process
801 * Update_Location_Area_VLR (ULA_VLR2). */
Harald Welteb8b85a12016-06-17 00:06:42 +0200802 if (0) { /* IMEISV or PgA to send */
803 vlr_loc_upd_node_4(fi);
804 } else {
805 /* Location_Update_Completion */
806 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_LU_COMPL,
807 LU_TIMEOUT_LONG, 0);
808 vlr_loc_upd_start_lu_compl_fsm(fi);
809 }
810}
811
812/* Non-standard: after Ciphering Mode Complete (or no ciph required) */
813static void vlr_loc_upd_post_ciph(struct osmo_fsm_inst *fi)
814{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100815 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200816 struct vlr_subscr *vsub = lfp->vsub;
817
818 LOGPFSM(fi, "%s()\n", __func__);
819
820 OSMO_ASSERT(vsub);
821
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200822 if (lfp->is_utran) {
823 int rc;
824 rc = lfp->vlr->ops.tx_common_id(lfp->msc_conn_ref);
825 if (rc)
826 LOGPFSML(fi, LOGL_ERROR,
827 "Error while sending Common ID (%d)\n", rc);
828 }
829
Harald Welteb8b85a12016-06-17 00:06:42 +0200830 vsub->conf_by_radio_contact_ind = true;
831 /* Update LAI */
832 vsub->cgi.lai = lfp->new_lai;
833 vsub->dormant_ind = false;
834 vsub->cancel_loc_rx = false;
835 if (hlr_update_needed(vsub)) {
836 vlr_loc_upd_node_4(fi);
837 } else {
838 /* TODO: ADD Support */
839 /* TODO: Node A: PgA Support */
840 vlr_loc_upd_node_b(fi);
841 }
842}
843
844/* 4.1.2.1 after Authentication successful (or no auth rqd) */
845static void vlr_loc_upd_post_auth(struct osmo_fsm_inst *fi)
846{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100847 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200848 struct vlr_subscr *vsub = lfp->vsub;
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100849 bool umts_aka;
Harald Welteb8b85a12016-06-17 00:06:42 +0200850
851 LOGPFSM(fi, "%s()\n", __func__);
852
853 OSMO_ASSERT(vsub);
854
855 if (!is_ciph_required(lfp)) {
856 vlr_loc_upd_post_ciph(fi);
857 return;
858 }
859
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +0100860 if (!vsub->last_tuple) {
861 LOGPFSML(fi, LOGL_ERROR, "No auth tuple available\n");
Neels Hofmeyrd2278ec2018-03-02 02:44:05 +0100862 lu_fsm_failure(fi, GSM48_REJECT_NETWORK_FAILURE);
Neels Hofmeyr2ef2da52017-12-18 01:23:42 +0100863 return;
864 }
865
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100866 switch (vsub->sec_ctx) {
867 case VLR_SEC_CTX_GSM:
868 umts_aka = false;
869 break;
870 case VLR_SEC_CTX_UMTS:
871 umts_aka = true;
872 break;
873 default:
874 LOGPFSML(fi, LOGL_ERROR, "Cannot start ciphering, security context is not established\n");
875 lu_fsm_failure(fi, GSM48_REJECT_NETWORK_FAILURE);
876 return;
877 }
878
Harald Welteb8b85a12016-06-17 00:06:42 +0200879 if (vlr_set_ciph_mode(vsub->vlr, fi, lfp->msc_conn_ref,
880 lfp->ciphering_required,
Neels Hofmeyr7795a192018-03-10 00:26:36 +0100881 umts_aka,
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200882 vsub->vlr->cfg.retrieve_imeisv_ciphered)) {
Harald Welteb8b85a12016-06-17 00:06:42 +0200883 LOGPFSML(fi, LOGL_ERROR,
884 "Failed to send Ciphering Mode Command\n");
Neels Hofmeyrd2278ec2018-03-02 02:44:05 +0100885 lu_fsm_failure(fi, GSM48_REJECT_NETWORK_FAILURE);
Harald Welteb8b85a12016-06-17 00:06:42 +0200886 return;
887 }
888
889 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_CIPH, LU_TIMEOUT_LONG, 0);
890}
891
892static void vlr_loc_upd_node1(struct osmo_fsm_inst *fi)
893{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100894 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200895 struct vlr_subscr *vsub = lfp->vsub;
896
897 LOGPFSM(fi, "%s()\n", __func__);
898
899 OSMO_ASSERT(vsub);
900
901 if (is_auth_required(lfp)) {
902 /* Authenticate_VLR */
903 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_AUTH,
904 LU_TIMEOUT_LONG, 0);
905 vsub->auth_fsm = auth_fsm_start(lfp->vsub, fi->log_level,
906 fi, VLR_ULA_E_AUTH_RES,
907 lfp->is_r99,
908 lfp->is_utran);
909 } else {
910 /* no need for authentication */
911 vlr_loc_upd_post_auth(fi);
912 }
913}
914
915static void vlr_loc_upd_want_imsi(struct osmo_fsm_inst *fi)
916{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100917 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200918 struct vlr_instance *vlr = lfp->vlr;
919
920 LOGPFSM(fi, "%s()\n", __func__);
921
922 OSMO_ASSERT(lfp->vsub);
923
924 /* Obtain_IMSI_VLR */
925 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_IMSI,
926 vlr_timer(vlr, 3270), 3270);
927 vlr->ops.tx_id_req(lfp->msc_conn_ref, GSM_MI_TYPE_IMSI);
928 /* will continue at vlr_loc_upd_node1() once IMSI arrives */
929}
930
931static int assoc_lfp_with_sub(struct osmo_fsm_inst *fi, struct vlr_subscr *vsub)
932{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100933 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +0200934 struct vlr_instance *vlr = lfp->vlr;
935
936 if (vsub->lu_fsm) {
937 LOGPFSML(fi, LOGL_ERROR,
938 "A Location Updating process is already pending for"
939 " this subscriber. Aborting.\n");
940 /* Also get rid of the other pending LU attempt? */
941 /*lu_fsm_failure(vsub->lu_fsm, GSM48_REJECT_CONGESTION);*/
942 lu_fsm_failure(fi, GSM48_REJECT_CONGESTION);
943 return -EINVAL;
944 }
945 vsub->lu_fsm = fi;
946 vsub->msc_conn_ref = lfp->msc_conn_ref;
947 /* FIXME: send new LAC to HLR? */
948 vsub->lac = lfp->new_lai.lac;
949 lfp->vsub = vsub;
950 /* Tell MSC to associate this subscriber with the given
951 * connection */
952 vlr->ops.subscr_assoc(lfp->msc_conn_ref, lfp->vsub);
953 return 0;
954}
955
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200956static int _lu_fsm_associate_vsub(struct osmo_fsm_inst *fi)
957{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +0100958 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Neels Hofmeyr54a706c2017-07-18 15:39:27 +0200959 struct vlr_instance *vlr = lfp->vlr;
960 struct vlr_subscr *vsub = NULL;
961
962 if (!lfp->imsi[0]) {
963 /* TMSI was used */
964 lfp->lu_by_tmsi = true;
965 /* TMSI clash: if a different subscriber already has this TMSI,
966 * we will find that other subscriber in the VLR. So the IMSIs
967 * would mismatch, but we don't know about it. Theoretically,
968 * an authentication process would thwart any attempt to use
969 * someone else's TMSI.
970 * TODO: Otherwise we can ask for the IMSI and verify that it
971 * matches the IMSI on record. */
972 vsub = vlr_subscr_find_or_create_by_tmsi(vlr, lfp->tmsi, NULL);
973
974 if (!vsub) {
975 LOGPFSML(fi, LOGL_ERROR, "VLR subscriber allocation failed\n");
976 lu_fsm_failure(fi, GSM48_REJECT_SRV_OPT_TMP_OUT_OF_ORDER);
977 return -1;
978 }
979
980 vsub->sub_dataconf_by_hlr_ind = false;
981 if (assoc_lfp_with_sub(fi, vsub)) {
982 vlr_subscr_put(vsub);
983 return -1; /* error, fsm failure invoked in assoc_lfp_with_sub() */
984 }
985 vlr_subscr_put(vsub);
986 } else {
987 /* IMSI was used */
988 vsub = vlr_subscr_find_or_create_by_imsi(vlr, lfp->imsi, NULL);
989
990 if (!vsub) {
991 LOGPFSML(fi, LOGL_ERROR, "VLR subscriber allocation failed\n");
992 lu_fsm_failure(fi, GSM48_REJECT_SRV_OPT_TMP_OUT_OF_ORDER);
993 vlr_subscr_put(vsub);
994 return -1;
995 }
996
997 vsub->sub_dataconf_by_hlr_ind = false;
998 if (assoc_lfp_with_sub(fi, vsub)) {
999 vlr_subscr_put(vsub);
1000 return -1; /* error, fsm failure invoked in assoc_lfp_with_sub() */
1001 }
1002 vlr_subscr_put(vsub);
1003 }
1004 return 0;
1005}
1006
Harald Welteb8b85a12016-06-17 00:06:42 +02001007/* 4.1.2.1: Subscriber (via MSC/SGSN) requests location update */
1008static void _start_lu_main(struct osmo_fsm_inst *fi)
1009{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001010 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001011 struct vlr_instance *vlr = lfp->vlr;
Harald Welteb8b85a12016-06-17 00:06:42 +02001012
1013 /* TODO: PUESBINE related handling */
1014
1015 /* Is previous LAI in this VLR? */
1016 if (!lai_in_this_vlr(vlr, &lfp->old_lai)) {
1017#if 0
1018 /* FIXME: check previous VLR, (3) */
1019 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_PVLR,
1020 LU_TIMEOUT_LONG, 0);
1021 return;
1022#endif
1023 LOGPFSML(fi, LOGL_NOTICE, "LAI change from %s,"
1024 " but checking previous VLR not implemented\n",
Neels Hofmeyr379d5792018-02-22 04:04:54 +01001025 osmo_lai_name(&lfp->old_lai));
Harald Welteb8b85a12016-06-17 00:06:42 +02001026 }
1027
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001028 /* If this is a TMSI based LU, we may not have the IMSI. Make sure that
1029 * we know the IMSI, either on record, or request it. */
1030 if (!lfp->vsub->imsi[0])
1031 vlr_loc_upd_want_imsi(fi);
1032 else
Harald Welteb8b85a12016-06-17 00:06:42 +02001033 vlr_loc_upd_node1(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001034}
1035
Harald Welteb8b85a12016-06-17 00:06:42 +02001036static void lu_fsm_idle(struct osmo_fsm_inst *fi, uint32_t event,
1037 void *data)
1038{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001039 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001040 struct vlr_instance *vlr = lfp->vlr;
1041
1042 OSMO_ASSERT(event == VLR_ULA_E_UPDATE_LA);
1043
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001044 if (_lu_fsm_associate_vsub(fi))
1045 return; /* error. FSM already terminated. */
1046
1047 OSMO_ASSERT(lfp->vsub);
1048
1049 /* See 3GPP TS 23.012, procedure Retrieve_IMEISV_If_Required */
1050 if ((!vlr->cfg.retrieve_imeisv_early)
1051 || (lfp->type == VLR_LU_TYPE_PERIODIC && lfp->vsub->imeisv[0])) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001052 /* R_IMEISV_IR1 passed */
1053 _start_lu_main(fi);
1054 } else {
1055 vlr->ops.tx_id_req(lfp->msc_conn_ref, GSM_MI_TYPE_IMEISV);
1056 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_IMEISV,
1057 vlr_timer(vlr, 3270), 3270);
1058 }
1059}
1060
1061static void lu_fsm_wait_imeisv(struct osmo_fsm_inst *fi, uint32_t event,
1062 void *data)
1063{
1064 switch (event) {
1065 case VLR_ULA_E_ID_IMEISV:
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001066 /* IMEISV was copied in vlr_subscr_rx_id_resp(), and that's
1067 * where we received this event from. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001068 _start_lu_main(fi);
1069 break;
1070 default:
1071 LOGPFSML(fi, LOGL_ERROR, "event without effect: %s\n",
1072 osmo_fsm_event_name(fi->fsm, event));
1073 break;
1074 }
1075}
1076
1077/* Wait for response from Send_Identification to PVLR */
1078static void lu_fsm_wait_pvlr(struct osmo_fsm_inst *fi, uint32_t event,
1079 void *data)
1080{
1081 switch (event) {
1082 case VLR_ULA_E_SEND_ID_ACK:
1083 vlr_loc_upd_node1(fi);
1084 break;
1085 case VLR_ULA_E_SEND_ID_NACK:
1086 vlr_loc_upd_want_imsi(fi);
1087 break;
1088 default:
1089 LOGPFSML(fi, LOGL_ERROR, "event without effect: %s\n",
1090 osmo_fsm_event_name(fi->fsm, event));
1091 break;
1092 }
1093}
1094
1095/* Wait for result of Authenticate_VLR procedure */
1096static void lu_fsm_wait_auth(struct osmo_fsm_inst *fi, uint32_t event,
1097 void *data)
1098{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001099 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001100 enum vlr_auth_fsm_result *res = data;
1101 uint8_t rej_cause = 0;
1102
1103 OSMO_ASSERT(event == VLR_ULA_E_AUTH_RES);
1104
1105 lfp->upd_hlr_vlr_fsm = NULL;
1106
1107 if (res) {
1108 switch (*res) {
1109 case VLR_AUTH_RES_PASSED:
1110 /* Result == Pass */
1111 vlr_loc_upd_post_auth(fi);
1112 return;
1113 case VLR_AUTH_RES_ABORTED:
1114 /* go to Idle with no response */
1115 rej_cause = 0;
1116 break;
1117 case VLR_AUTH_RES_UNKNOWN_SUBSCR:
1118 /* FIXME: delete subscribe record */
1119 rej_cause = GSM48_REJECT_IMSI_UNKNOWN_IN_HLR;
1120 break;
1121 case VLR_AUTH_RES_AUTH_FAILED:
1122 /* cause = illegal subscriber */
1123 rej_cause = GSM48_REJECT_ILLEGAL_MS;
1124 break;
1125 case VLR_AUTH_RES_PROC_ERR:
1126 /* cause = system failure */
1127 rej_cause = GSM48_REJECT_NETWORK_FAILURE;
1128 break;
1129 default:
1130 LOGPFSML(fi, LOGL_ERROR, "event without effect: %s\n",
1131 osmo_fsm_event_name(fi->fsm, event));
1132 break;
1133 }
1134 } else
1135 rej_cause = GSM48_REJECT_NETWORK_FAILURE;
1136
1137 lu_fsm_failure(fi, rej_cause);
1138}
1139
1140static void lu_fsm_wait_ciph(struct osmo_fsm_inst *fi, uint32_t event,
1141 void *data)
1142{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001143 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001144 struct vlr_subscr *vsub = lfp->vsub;
1145 struct vlr_ciph_result res = { .cause = VLR_CIPH_REJECT };
1146
1147 OSMO_ASSERT(event == VLR_ULA_E_CIPH_RES);
1148
1149 if (!data)
1150 LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: NULL\n");
1151 else
1152 res = *(struct vlr_ciph_result*)data;
1153
1154 switch (res.cause) {
1155 case VLR_CIPH_COMPL:
1156 break;
1157 case VLR_CIPH_REJECT:
1158 LOGPFSM(fi, "ciphering rejected\n");
1159 lu_fsm_failure(fi, GSM48_REJECT_INVALID_MANDANTORY_INF);
1160 return;
1161 default:
1162 LOGPFSML(fi, LOGL_ERROR, "invalid ciphering result: %d\n",
1163 res.cause);
1164 lu_fsm_failure(fi, GSM48_REJECT_INVALID_MANDANTORY_INF);
1165 return;
1166 }
1167
Neels Hofmeyrfa10eda2018-03-13 01:22:01 +01001168 if (*res.imeisv) {
Harald Welteb8b85a12016-06-17 00:06:42 +02001169 LOGPFSM(fi, "got IMEISV: %s\n", res.imeisv);
1170 vlr_subscr_set_imeisv(vsub, res.imeisv);
1171 }
1172 vlr_loc_upd_post_ciph(fi);
1173}
1174
1175static void lu_fsm_wait_imsi(struct osmo_fsm_inst *fi, uint32_t event,
1176 void *data)
1177{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001178 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001179 struct vlr_subscr *vsub = lfp->vsub;
1180 char *mi_string = data;
1181
1182 switch (event) {
1183 case VLR_ULA_E_ID_IMSI:
1184 vlr_subscr_set_imsi(vsub, mi_string);
1185 vlr_loc_upd_node1(fi);
1186 break;
1187 default:
1188 LOGPFSML(fi, LOGL_ERROR, "event without effect: %s\n",
1189 osmo_fsm_event_name(fi->fsm, event));
1190 break;
1191 }
1192}
1193
1194/* At the end of Update_HLR_VLR */
1195static void lu_fsm_wait_hlr_ul_res(struct osmo_fsm_inst *fi, uint32_t event,
1196 void *data)
1197{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001198 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001199
1200 switch (event) {
1201 case VLR_ULA_E_HLR_LU_RES:
1202 /* pass-through this event to Update_HLR_VLR */
1203 if (data == NULL)
1204 osmo_fsm_inst_dispatch(lfp->upd_hlr_vlr_fsm, UPD_HLR_VLR_E_UPD_LOC_ACK, NULL);
1205 else
1206 osmo_fsm_inst_dispatch(lfp->upd_hlr_vlr_fsm, UPD_HLR_VLR_E_UPD_LOC_NACK, data);
1207 break;
1208 case VLR_ULA_E_UPD_HLR_COMPL:
1209 if (data == NULL) {
1210 /* successful case */
1211 osmo_fsm_inst_state_chg(fi, VLR_ULA_S_WAIT_LU_COMPL,
1212 LU_TIMEOUT_LONG, 0);
1213 vlr_loc_upd_start_lu_compl_fsm(fi);
1214 /* continue in MSC ?!? */
1215 } else {
1216 /* unsuccessful case */
1217 enum gsm48_gmm_cause cause =
1218 *(enum gsm48_gmm_cause *)data;
Neels Hofmeyref9126c2017-07-18 15:38:39 +02001219 /* Ignoring standalone mode for now. */
Harald Welteb8b85a12016-06-17 00:06:42 +02001220 if (0 /* procedure_error && vlr->cfg.standalone_mode */) {
1221 osmo_fsm_inst_state_chg(fi,
1222 VLR_ULA_S_WAIT_LU_COMPL_STANDALONE,
1223 LU_TIMEOUT_LONG, 0);
1224 vlr_loc_upd_start_lu_compl_fsm(fi);
1225 } else {
1226 lu_fsm_failure(fi, cause);
1227 }
1228 }
1229 break;
1230 default:
1231 LOGPFSML(fi, LOGL_ERROR, "event without effect: %s\n",
1232 osmo_fsm_event_name(fi->fsm, event));
1233 break;
1234 }
1235}
1236
1237/* Wait for end of Location_Update_Completion_VLR */
1238static void lu_fsm_wait_lu_compl(struct osmo_fsm_inst *fi, uint32_t event,
1239 void *data)
1240{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001241 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001242 uint8_t cause;
1243
1244 switch (event) {
1245 case VLR_ULA_E_NEW_TMSI_ACK:
1246 osmo_fsm_inst_dispatch(lfp->lu_compl_vlr_fsm,
1247 LU_COMPL_VLR_E_NEW_TMSI_ACK, NULL);
1248 break;
1249 case VLR_ULA_E_ID_IMEI:
1250 osmo_fsm_inst_dispatch(lfp->lu_compl_vlr_fsm,
1251 LU_COMPL_VLR_E_IMEI_CHECK_ACK, NULL);
1252 break;
1253 case VLR_ULA_E_LU_COMPL_SUCCESS:
1254 lu_fsm_discard_lu_compl_fsm(fi);
1255
1256 /* Update Register */
1257 /* TODO: Set_Notification_Type 23.078 */
1258 /* TODO: Notify_gsmSCF 23.078 */
1259 /* TODO: Authenticated Radio Contact Established -> ARC */
Stefan Sperling3a741282018-03-13 21:11:49 +01001260
1261 if (lfp->type == VLR_LU_TYPE_IMSI_ATTACH)
1262 lfp->vlr->ops.tx_mm_info(lfp->msc_conn_ref);
1263
Harald Welteb8b85a12016-06-17 00:06:42 +02001264 lu_fsm_success(fi);
1265 break;
1266 case VLR_ULA_E_LU_COMPL_FAILURE:
1267 cause = GSM48_REJECT_NETWORK_FAILURE;
1268 if (data)
1269 cause = *(uint8_t*)data;
1270 lu_fsm_discard_lu_compl_fsm(fi);
1271 lu_fsm_failure(fi, cause);
1272 break;
1273 default:
1274 LOGPFSML(fi, LOGL_ERROR, "event without effect: %s\n",
1275 osmo_fsm_event_name(fi->fsm, event));
1276 break;
1277 }
1278}
1279
1280/* Wait for end of Location_Update_Completion_VLR (standalone case) */
1281static void lu_fsm_wait_lu_compl_standalone(struct osmo_fsm_inst *fi,
1282 uint32_t event, void *data)
1283{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001284 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001285 struct vlr_subscr *vsub = lfp->vsub;
1286 uint8_t cause;
1287
1288 switch (event) {
1289 case VLR_ULA_E_NEW_TMSI_ACK:
1290 osmo_fsm_inst_dispatch(lfp->lu_compl_vlr_fsm,
1291 LU_COMPL_VLR_E_NEW_TMSI_ACK, NULL);
1292 break;
1293 case VLR_ULA_E_LU_COMPL_SUCCESS:
1294 lu_fsm_discard_lu_compl_fsm(fi);
1295 vsub->sub_dataconf_by_hlr_ind = false;
1296 lu_fsm_success(fi);
1297 break;
1298 case VLR_ULA_E_LU_COMPL_FAILURE:
1299 vsub->sub_dataconf_by_hlr_ind = false;
1300 cause = GSM48_REJECT_NETWORK_FAILURE;
1301 if (data)
1302 cause = *(uint8_t*)data;
1303 lu_fsm_discard_lu_compl_fsm(fi);
1304 lu_fsm_failure(fi, cause);
1305 break;
1306 default:
1307 LOGPFSML(fi, LOGL_ERROR, "event without effect: %s\n",
1308 osmo_fsm_event_name(fi->fsm, event));
1309 break;
1310 }
1311}
1312
1313static const struct osmo_fsm_state vlr_lu_fsm_states[] = {
1314 [VLR_ULA_S_IDLE] = {
1315 .in_event_mask = S(VLR_ULA_E_UPDATE_LA),
1316 .out_state_mask = S(VLR_ULA_S_WAIT_IMEISV) |
1317 S(VLR_ULA_S_WAIT_PVLR) |
1318 S(VLR_ULA_S_WAIT_IMSI) |
1319 S(VLR_ULA_S_WAIT_AUTH) |
1320 S(VLR_ULA_S_WAIT_HLR_UPD) |
1321 S(VLR_ULA_S_DONE),
1322 .name = OSMO_STRINGIFY(VLR_ULA_S_IDLE),
1323 .action = lu_fsm_idle,
1324 },
1325 [VLR_ULA_S_WAIT_IMEISV] = {
1326 .in_event_mask = S(VLR_ULA_E_ID_IMEISV),
1327 .out_state_mask = S(VLR_ULA_S_WAIT_PVLR) |
1328 S(VLR_ULA_S_WAIT_IMSI) |
Neels Hofmeyr54a706c2017-07-18 15:39:27 +02001329 S(VLR_ULA_S_WAIT_AUTH) |
1330 S(VLR_ULA_S_WAIT_HLR_UPD) |
Harald Welteb8b85a12016-06-17 00:06:42 +02001331 S(VLR_ULA_S_DONE),
1332 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_IMEISV),
1333 .action = lu_fsm_wait_imeisv,
1334 },
1335 [VLR_ULA_S_WAIT_PVLR] = {
1336 .in_event_mask = S(VLR_ULA_E_SEND_ID_ACK) |
1337 S(VLR_ULA_E_SEND_ID_NACK),
1338 .out_state_mask = S(VLR_ULA_S_WAIT_IMSI) |
1339 S(VLR_ULA_S_WAIT_AUTH) |
1340 S(VLR_ULA_S_DONE),
1341 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_PVLR),
1342 .action = lu_fsm_wait_pvlr,
1343 },
1344 [VLR_ULA_S_WAIT_AUTH] = {
1345 .in_event_mask = S(VLR_ULA_E_AUTH_RES),
1346 .out_state_mask = S(VLR_ULA_S_WAIT_CIPH) |
1347 S(VLR_ULA_S_WAIT_LU_COMPL) |
1348 S(VLR_ULA_S_WAIT_HLR_UPD) |
1349 S(VLR_ULA_S_DONE),
1350 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_AUTH),
1351 .action = lu_fsm_wait_auth,
1352 },
1353 [VLR_ULA_S_WAIT_CIPH] = {
1354 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_CIPH),
1355 .in_event_mask = S(VLR_ULA_E_CIPH_RES),
1356 .out_state_mask = S(VLR_ULA_S_WAIT_LU_COMPL) |
1357 S(VLR_ULA_S_WAIT_HLR_UPD) |
1358 S(VLR_ULA_S_DONE),
1359 .action = lu_fsm_wait_ciph,
1360 },
1361 [VLR_ULA_S_WAIT_IMSI] = {
1362 .in_event_mask = S(VLR_ULA_E_ID_IMSI),
1363 .out_state_mask = S(VLR_ULA_S_WAIT_AUTH) |
1364 S(VLR_ULA_S_WAIT_HLR_UPD) |
1365 S(VLR_ULA_S_DONE),
1366 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_IMSI),
1367 .action = lu_fsm_wait_imsi,
1368 },
1369 [VLR_ULA_S_WAIT_HLR_UPD] = {
1370 .in_event_mask = S(VLR_ULA_E_HLR_LU_RES) |
1371 S(VLR_ULA_E_UPD_HLR_COMPL),
1372 .out_state_mask = S(VLR_ULA_S_WAIT_LU_COMPL) |
1373 S(VLR_ULA_S_WAIT_LU_COMPL_STANDALONE) |
1374 S(VLR_ULA_S_DONE),
1375 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_HLR_UPD),
1376 .action = lu_fsm_wait_hlr_ul_res,
1377 },
1378 [VLR_ULA_S_WAIT_LU_COMPL] = {
1379 .in_event_mask = S(VLR_ULA_E_LU_COMPL_SUCCESS) |
1380 S(VLR_ULA_E_LU_COMPL_FAILURE) |
1381 S(VLR_ULA_E_NEW_TMSI_ACK) |
1382 S(VLR_ULA_E_ID_IMEI) |
1383 S(VLR_ULA_E_ID_IMEISV),
1384 .out_state_mask = S(VLR_ULA_S_DONE),
1385 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_LU_COMPL),
1386 .action = lu_fsm_wait_lu_compl,
1387 },
1388 [VLR_ULA_S_WAIT_LU_COMPL_STANDALONE] = {
1389 .in_event_mask = S(VLR_ULA_E_LU_COMPL_SUCCESS) |
1390 S(VLR_ULA_E_LU_COMPL_FAILURE) |
1391 S(VLR_ULA_E_NEW_TMSI_ACK),
1392 .out_state_mask = S(VLR_ULA_S_DONE),
1393 .name = OSMO_STRINGIFY(VLR_ULA_S_WAIT_LU_COMPL_STANDALONE),
1394 .action = lu_fsm_wait_lu_compl_standalone,
1395 },
1396 [VLR_ULA_S_DONE] = {
1397 .name = OSMO_STRINGIFY(VLR_ULA_S_DONE),
1398 .onenter = lu_fsm_dispatch_result,
1399 },
1400};
1401
1402static void fsm_lu_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
1403{
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001404 struct lu_fsm_priv *lfp = lu_fsm_fi_priv(fi);
Harald Welteb8b85a12016-06-17 00:06:42 +02001405 struct vlr_subscr *vsub = lfp->vsub;
1406
1407 LOGPFSM(fi, "fsm_lu_cleanup called with cause %s\n",
1408 osmo_fsm_term_cause_name(cause));
1409 if (vsub && vsub->lu_fsm == fi)
1410 vsub->lu_fsm = NULL;
1411}
1412
1413static struct osmo_fsm vlr_lu_fsm = {
1414 .name = "vlr_lu_fsm",
1415 .states = vlr_lu_fsm_states,
1416 .num_states = ARRAY_SIZE(vlr_lu_fsm_states),
1417 .allstate_event_mask = 0,
1418 .allstate_action = NULL,
1419 .log_subsys = DVLR,
1420 .event_names = fsm_lu_event_names,
1421 .cleanup = fsm_lu_cleanup,
1422};
1423
Neels Hofmeyrc5e0ace2018-03-02 03:12:22 +01001424static inline struct lu_fsm_priv *lu_fsm_fi_priv(struct osmo_fsm_inst *fi)
1425{
1426 OSMO_ASSERT(fi->fsm == &vlr_lu_fsm);
1427 return (struct lu_fsm_priv*)fi->priv;
1428}
1429
Harald Welteb8b85a12016-06-17 00:06:42 +02001430struct osmo_fsm_inst *
1431vlr_loc_update(struct osmo_fsm_inst *parent,
1432 uint32_t parent_event_success,
1433 uint32_t parent_event_failure,
1434 void *parent_event_data,
1435 struct vlr_instance *vlr, void *msc_conn_ref,
1436 enum vlr_lu_type type, uint32_t tmsi, const char *imsi,
1437 const struct osmo_location_area_id *old_lai,
1438 const struct osmo_location_area_id *new_lai,
1439 bool authentication_required,
Harald Welte71c51df2017-12-23 18:51:48 +01001440 bool ciphering_required,
Harald Welteb8b85a12016-06-17 00:06:42 +02001441 bool is_r99, bool is_utran,
1442 bool assign_tmsi)
1443{
1444 struct osmo_fsm_inst *fi;
1445 struct lu_fsm_priv *lfp;
1446
1447 fi = osmo_fsm_inst_alloc_child(&vlr_lu_fsm, parent, parent_event_failure);
1448 if (!fi)
1449 return NULL;
1450
1451 lfp = talloc_zero(fi, struct lu_fsm_priv);
1452 lfp->vlr = vlr;
1453 lfp->msc_conn_ref = msc_conn_ref;
1454 lfp->tmsi = tmsi;
1455 lfp->type = type;
1456 lfp->old_lai = *old_lai;
1457 lfp->new_lai = *new_lai;
1458 lfp->lu_by_tmsi = true;
1459 lfp->parent_event_success = parent_event_success;
1460 lfp->parent_event_failure = parent_event_failure;
1461 lfp->parent_event_data = parent_event_data;
1462 lfp->authentication_required = authentication_required;
1463 lfp->ciphering_required = ciphering_required;
1464 lfp->is_r99 = is_r99;
1465 lfp->is_utran = is_utran;
1466 lfp->assign_tmsi = assign_tmsi;
1467 if (imsi) {
1468 strncpy(lfp->imsi, imsi, sizeof(lfp->imsi)-1);
1469 lfp->imsi[sizeof(lfp->imsi)-1] = '\0';
1470 lfp->lu_by_tmsi = false;
1471 }
1472 fi->priv = lfp;
1473
1474 LOGPFSM(fi, "rev=%s net=%s%s%s\n",
1475 is_r99 ? "R99" : "GSM",
1476 is_utran ? "UTRAN" : "GERAN",
1477 (authentication_required || ciphering_required)?
1478 " Auth" : " (no Auth)",
1479 (authentication_required || ciphering_required)?
1480 (ciphering_required? "+Ciph" : " (no Ciph)")
1481 : "");
1482
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001483 if (is_utran && !authentication_required)
1484 LOGPFSML(fi, LOGL_ERROR,
1485 "Authentication off on UTRAN network. Good luck.\n");
1486
Harald Welteb8b85a12016-06-17 00:06:42 +02001487 osmo_fsm_inst_dispatch(fi, VLR_ULA_E_UPDATE_LA, NULL);
1488
1489 return fi;
1490}
1491
1492/* Gracefully terminate an FSM created by vlr_loc_update() in case of external
1493 * timeout (i.e. from MSC). */
1494void vlr_loc_update_conn_timeout(struct osmo_fsm_inst *fi)
1495{
1496 if (!fi || fi->state == VLR_ULA_S_DONE)
1497 return;
1498 LOGPFSM(fi, "Connection timed out\n");
1499 lu_fsm_failure(fi, GSM48_REJECT_CONGESTION);
1500}
1501
1502void vlr_lu_fsm_init(void)
1503{
1504 osmo_fsm_register(&vlr_lu_fsm);
1505 osmo_fsm_register(&upd_hlr_vlr_fsm);
1506 osmo_fsm_register(&sub_pres_vlr_fsm);
1507 osmo_fsm_register(&lu_compl_vlr_fsm);
1508}