blob: 9aac74bd67a36dca71309e5c4d8ac7e64e5da28a [file] [log] [blame]
Harald Welte0df904d2018-12-03 11:00:04 +01001/* (C) 2018-2019 by sysmocom s.f.m.c. GmbH
2 * All Rights Reserved
3 *
4 * Author: Harald Welte, Philipp Maier
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <osmocom/core/utils.h>
22#include <osmocom/core/fsm.h>
23#include <osmocom/gsm/gsm48.h>
24#include <osmocom/msc/debug.h>
25#include <osmocom/msc/vlr.h>
26#include <osmocom/msc/vlr_sgs.h>
27
28#include "vlr_sgs_fsm.h"
29#include "vlr_core.h"
30
31#define S(x) (1 << (x))
32
33static const struct value_string sgs_ue_fsm_event_names[] = {
34 {SGS_UE_E_VLR_FAILURE, "VLR_FAILURE"},
35 {SGS_UE_E_RX_RESET_FROM_MME, "RX_RESET_FROM_MME"},
36 {SGS_UE_E_RX_DETACH_IND_FROM_MME, "RX_DETACH_IND_FROM_MME"},
37 {SGS_UE_E_RX_DETACH_IND_FROM_UE, "RX_DETACH_IND_FROM_UE"}, /* vlr.c */
38 {SGS_UE_E_RX_LU_FROM_A_IU_GS, "RX_LU_FROM_A_Iu_Gs"}, /* vlr_lu_fsm.c */
39 {SGS_UE_E_RX_PAGING_FAILURE, "RX_PAGING_FAILURE"},
40 {SGS_UE_E_RX_ALERT_FAILURE, "RX_ALERT_FAILURE"},
41 {SGS_UE_E_RX_LU_FROM_MME, "RX_LU_FROM_MME"},
42 {SGS_UE_E_TX_LU_REJECT, "TX_LU_REJECT"},
43 {SGS_UE_E_TX_LU_ACCEPT, "TX_LU_ACCEPT"},
44 {SGS_UE_E_TX_PAGING, "TX_PAGING"},
45 {SGS_UE_E_RX_SGSAP_UE_UNREACHABLE, "RX_SGSAP_UE_UNREACH"},
46 {SGS_UE_E_RX_TMSI_REALLOC, "RX_TMSI_REALLOC"},
47 {0, NULL}
48};
49
Harald Welte0df904d2018-12-03 11:00:04 +010050/* Send the SGs Association to NULL state immediately */
51static void to_null(struct osmo_fsm_inst *fi)
52{
53 struct vlr_subscr *vsub = fi->priv;
54 osmo_fsm_inst_state_chg(fi, SGS_UE_ST_NULL, 0, 0);
55
Martin Hauke3f07dac2019-11-14 17:49:08 +010056 /* Note: This is only relevant for cases where we are in the middle
Harald Welte0df904d2018-12-03 11:00:04 +010057 * of an TMSI reallocation procedure. Should a failure of some sort
58 * put us to NULL state, we have to free the pending TMSI */
59 vsub->tmsi_new = GSM_RESERVED_TMSI;
60
Pau Espin Pedrol1086e202021-07-02 18:36:32 +020061 /* Make sure we remove recorded Last EUTRAN PLMN Id when UE ceases to be
62 * available over SGs */
63 vlr_subscr_set_last_used_eutran_plmn_id(vsub, NULL);
64
Harald Welte0df904d2018-12-03 11:00:04 +010065 /* Make sure any ongoing paging is aborted. */
Alexander Couzens28e43ff2024-09-11 23:49:33 +020066 if (vsub->cs.is_paging && vsub->sgs.paging_cb)
67 vsub->sgs.paging_cb(vsub, SGSAP_SERV_IND_PAGING_TIMEOUT);
Harald Welte0df904d2018-12-03 11:00:04 +010068
69 /* Ensure that Ts5 (pending paging via SGs) is deleted */
70 if (vlr_sgs_pag_pend(vsub))
71 osmo_timer_del(&vsub->sgs.Ts5);
72}
73
Philipp Maier483cea82019-04-03 16:23:29 +020074/* Initiate location update and change to SGS_UE_ST_LA_UPD_PRES state */
75static void perform_lu(struct osmo_fsm_inst *fi)
76{
77 struct vlr_subscr *vsub = fi->priv;
78 struct sgs_lu_response sgs_lu_response = {0};
79 int rc;
80
81 /* Note: At the moment we allocate a new TMSI on each LU. */
82 rc = vlr_subscr_alloc_tmsi(vsub);
83 if (rc != 0) {
84 LOGPFSML(fi, LOGL_ERROR, "(sub %s) VLR LU tmsi allocation failed\n", vlr_subscr_name(vsub));
85 goto error;
86 }
87
88 rc = vlr_subscr_req_lu(vsub);
89 if (rc != 0) {
90 LOGPFSML(fi, LOGL_ERROR, "(sub %s) HLR LU request failed\n", vlr_subscr_name(vsub));
91 goto error;
92 }
93
94 osmo_fsm_inst_state_chg(fi, SGS_UE_ST_LA_UPD_PRES, 0, 0);
95 vsub->ms_not_reachable_flag = false;
96 return;
97
98error:
99 to_null(fi);
100 sgs_lu_response.error = true;
101 sgs_lu_response.vsub = vsub;
102 vsub->sgs.response_cb(&sgs_lu_response);
103}
104
Harald Welte0df904d2018-12-03 11:00:04 +0100105/* Respawn a pending paging (Timer is reset and a new paging request is sent) */
106static void respawn_paging(struct vlr_subscr *vsub)
107{
108 if (vlr_sgs_pag_pend(vsub)) {
109
110 /* Delete the old paging timer first. */
111 osmo_timer_del(&vsub->sgs.Ts5);
112
113 /* Issue a fresh paging request */
114 vsub->sgs.paging_cb(vsub, vsub->sgs.paging_serv_ind);
115 }
116}
117
118/* Figure 4.2.2.1 SGs-NULL */
119static void sgs_ue_fsm_null(struct osmo_fsm_inst *fi, uint32_t event, void *data)
120{
121 switch (event) {
122 case SGS_UE_E_RX_LU_FROM_MME:
123 perform_lu(fi);
124 break;
125 case SGS_UE_E_TX_PAGING:
126 /* do nothing */
127 break;
128 case SGS_UE_E_RX_PAGING_FAILURE:
129 /* do nothing */
130 break;
131 default:
132 OSMO_ASSERT(0);
133 }
134}
135
136/* Figure 4.2.2.1 SGs-LA-UPDATE-PRESENT */
137static void sgs_ue_fsm_lau_present(struct osmo_fsm_inst *fi, uint32_t event, void *data)
138{
139 struct vlr_subscr *vsub = fi->priv;
140 enum sgsap_sgs_cause *cause = NULL;
141
142 switch (event) {
143 case SGS_UE_E_TX_LU_ACCEPT:
144 vsub->conf_by_radio_contact_ind = true;
145 vsub->sub_dataconf_by_hlr_ind = true;
146 vsub->loc_conf_in_hlr_ind = true;
147 vsub->la_allowed = true;
148 vsub->imsi_detached_flag = false;
Philipp Maierbb5ba8b2019-04-17 10:06:39 +0200149
150 if (!vsub->lu_complete) {
151 vsub->lu_complete = true;
152 /* Balanced by vlr_subscr_expire() */
153 vlr_subscr_get(vsub, VSUB_USE_ATTACHED);
154 }
155
Harald Welte0df904d2018-12-03 11:00:04 +0100156 vlr_sgs_fsm_update_id(vsub);
157 vsub->cs.attached_via_ran = OSMO_RAT_EUTRAN_SGS;
158
159 /* Check if we expect a TMSI REALLOCATION COMPLETE message from the MME
160 * by checking the tmsi_new flag. If this flag is not GSM_RESERVED_TMSI
161 * we know that we have a TMSI pending and need to wait for the MME
Martin Hauke3f07dac2019-11-14 17:49:08 +0100162 * to acknowledge first */
Harald Welte0df904d2018-12-03 11:00:04 +0100163 if (vsub->tmsi_new != GSM_RESERVED_TMSI) {
164 osmo_fsm_inst_state_chg(fi, SGS_UE_ST_ASSOCIATED, vsub->sgs.cfg.timer[SGS_STATE_TS6_2],
165 SGS_STATE_TS6_2);
166 } else {
167 /* Trigger sending of an MM information request */
168 vsub->sgs.mminfo_cb(vsub);
169
170 /* In cases where the LU has interrupted the paging, respawn the paging now,
171 * See also: 3GPP TS 29.118, chapter 5.2.3.2 Location update response */
172 if (vlr_sgs_pag_pend(vsub))
173 respawn_paging(vsub);
174
175 osmo_fsm_inst_state_chg(fi, SGS_UE_ST_ASSOCIATED, 0, 0);
176 }
177
178 break;
179 case SGS_UE_E_RX_PAGING_FAILURE:
180 cause = data;
181 if (*cause == SGSAP_SGS_CAUSE_MT_CSFB_REJ_USER)
182 break;
183 to_null(fi);
184 break;
185 case SGS_UE_E_TX_LU_REJECT:
186 case SGS_UE_E_RX_ALERT_FAILURE:
187 to_null(fi);
188 break;
189 case SGS_UE_E_TX_PAGING:
190 /* do nothing */
191 break;
192 default:
193 OSMO_ASSERT(0);
194 break;
195 }
196}
197
198/* Figure 4.2.2.1 SGs-ASSOCIATED */
199static void sgs_ue_fsm_associated(struct osmo_fsm_inst *fi, uint32_t event, void *data)
200{
201 struct vlr_subscr *vsub = fi->priv;
202 enum sgsap_sgs_cause *cause = NULL;
203
204 switch (event) {
205 case SGS_UE_E_TX_PAGING:
206 /* do nothing */
207 break;
208 case SGS_UE_E_RX_TMSI_REALLOC:
209 if (vsub->tmsi_new == GSM_RESERVED_TMSI) {
210 LOGPFSML(fi, LOGL_ERROR,
211 "(sub %s) TMSI reallocation completed at the MME, but no TMSI reallocation ordered.\n",
212 vlr_subscr_msisdn_or_name(vsub));
213 }
214
215 vsub->tmsi = vsub->tmsi_new;
216 vsub->tmsi_new = GSM_RESERVED_TMSI;
217
218 /* Trigger sending of MM information */
219 vsub->sgs.mminfo_cb(vsub);
220
221 /* In cases where the LU has interrupted the paging, respawn the paging now,
222 * See also: 3GPP TS 29.118, chapter 5.2.3.2 Location update response */
223 if (vlr_sgs_pag_pend(vsub))
224 respawn_paging(vsub);
225
226 /* Note: We are already in SGS_UE_ST_ASSOCIATED but the
227 * transition that lead us here had is guarded with Ts6-1,
Pau Espin Pedrol35ff8522021-08-25 12:40:36 +0200228 * so we change the state now once more without timeout
Harald Welte0df904d2018-12-03 11:00:04 +0100229 * to ensure the timer is stopped */
230 osmo_fsm_inst_state_chg(fi, SGS_UE_ST_ASSOCIATED, 0, 0);
231 break;
232 case SGS_UE_E_RX_SGSAP_UE_UNREACHABLE:
233 /* do nothing */
234 break;
235 case SGS_UE_E_RX_PAGING_FAILURE:
236 cause = data;
237 if (*cause == SGSAP_SGS_CAUSE_MT_CSFB_REJ_USER)
238 break;
239 to_null(fi);
Vadim Yanitskiy601af0c2020-01-25 06:18:09 +0700240 break;
Harald Welte0df904d2018-12-03 11:00:04 +0100241 case SGS_UE_E_RX_ALERT_FAILURE:
242 to_null(fi);
243 break;
244 case SGS_UE_E_RX_LU_FROM_MME:
245 perform_lu(fi);
246 break;
247 default:
248 OSMO_ASSERT(0);
249 break;
250 }
251}
252
253/* Figure 4.2.2.1 From any of the three states (at the VLR) */
254static void sgs_ue_fsm_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *data)
255{
256 struct vlr_subscr *vsub = fi->priv;
257
258 switch (event) {
259 case SGS_UE_E_RX_DETACH_IND_FROM_MME:
260 case SGS_UE_E_RX_DETACH_IND_FROM_UE:
261 vsub->imsi_detached_flag = true;
262 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
263 /* See 5.4.3 and 5.5.3 */
264 to_null(fi);
265 break;
266 case SGS_UE_E_RX_RESET_FROM_MME:
267 /* See also 3GPP TS 29.118, chapter 5.7.2.1 VLR Reset Initiation */
268 vsub->conf_by_radio_contact_ind = false;
269 to_null(fi);
270 break;
271 case SGS_UE_E_VLR_FAILURE:
272 case SGS_UE_E_RX_LU_FROM_A_IU_GS:
273 to_null(fi);
274 break;
275 default:
276 OSMO_ASSERT(0);
277 break;
278 }
279}
280
281static int sgs_ue_fsm_timer_cb(struct osmo_fsm_inst *fi)
282{
283 struct vlr_subscr *vsub = fi->priv;
284 switch (fi->T) {
285
286 case SGS_STATE_TS6_2:
287 /* Failed TMSI reallocation procedure, deallocate all TMSI
288 * information, but don't change the SGs association state. */
289 vsub->tmsi_new = GSM_RESERVED_TMSI;
290 vsub->tmsi = GSM_RESERVED_TMSI;
291 break;
292 default:
293 /* Unhandled timer */
294 OSMO_ASSERT(false);
295 break;
296 }
297 return 0;
298}
299
300static const struct osmo_fsm_state sgs_ue_fsm_states[] = {
301 [SGS_UE_ST_NULL] = {
302 .name = "SGs-NULL",
303 .action = sgs_ue_fsm_null,
304 .in_event_mask = 0
305 | S(SGS_UE_E_RX_LU_FROM_MME)
306 | S(SGS_UE_E_TX_PAGING)
307 | S(SGS_UE_E_RX_PAGING_FAILURE)
308 ,
309 .out_state_mask = 0
310 | S(SGS_UE_ST_NULL)
311 | S(SGS_UE_ST_LA_UPD_PRES)
312 ,
313 },
314 [SGS_UE_ST_LA_UPD_PRES] = {
315 .name = "SGs-LA-UPDATE-PRESENT",
316 .action = sgs_ue_fsm_lau_present,
317 .in_event_mask = 0
318 | S(SGS_UE_E_TX_LU_ACCEPT)
319 | S(SGS_UE_E_TX_LU_REJECT)
320 | S(SGS_UE_E_TX_PAGING)
321 | S(SGS_UE_E_RX_PAGING_FAILURE)
322 | S(SGS_UE_E_RX_ALERT_FAILURE)
323 ,
324 .out_state_mask = 0
325 | S(SGS_UE_ST_NULL)
326 | S(SGS_UE_ST_ASSOCIATED)
327 | S(SGS_UE_ST_LA_UPD_PRES)
328 ,
329 },
330 [SGS_UE_ST_ASSOCIATED] = {
331 .name = "SGs-ASSOCIATED",
332 .action = sgs_ue_fsm_associated,
333 .in_event_mask = 0
334 | S(SGS_UE_E_TX_PAGING)
335 | S(SGS_UE_E_RX_TMSI_REALLOC)
336 | S(SGS_UE_E_RX_SGSAP_UE_UNREACHABLE)
337 | S(SGS_UE_E_RX_PAGING_FAILURE)
338 | S(SGS_UE_E_RX_ALERT_FAILURE)
339 | S(SGS_UE_E_RX_LU_FROM_MME)
340 ,
341 .out_state_mask = 0
342 | S(SGS_UE_ST_NULL)
343 | S(SGS_UE_ST_ASSOCIATED)
344 | S(SGS_UE_ST_LA_UPD_PRES)
345 ,
346 },
347};
348
349static struct osmo_fsm sgs_ue_fsm = {
350 .name = "SGs-UE",
351 .states = sgs_ue_fsm_states,
352 .num_states = ARRAY_SIZE(sgs_ue_fsm_states),
353 .allstate_event_mask = S(SGS_UE_E_RX_RESET_FROM_MME) |
354 S(SGS_UE_E_VLR_FAILURE) | S(SGS_UE_E_RX_DETACH_IND_FROM_MME) | S(SGS_UE_E_RX_DETACH_IND_FROM_UE) |
355 S(SGS_UE_E_RX_LU_FROM_A_IU_GS),
356 .allstate_action = sgs_ue_fsm_allstate,
357 .timer_cb = sgs_ue_fsm_timer_cb,
358 .log_subsys = DSGS,
359 .event_names = sgs_ue_fsm_event_names,
360};
361
Martin Hauke3f07dac2019-11-14 17:49:08 +0100362/*! Initialize/Register SGs FSM in osmo-fsm subsystem */
Harald Welte0df904d2018-12-03 11:00:04 +0100363void vlr_sgs_fsm_init(void)
364{
365 if (osmo_fsm_find_by_name(sgs_ue_fsm.name) != &sgs_ue_fsm)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100366 OSMO_ASSERT(osmo_fsm_register(&sgs_ue_fsm) == 0);
Harald Welte0df904d2018-12-03 11:00:04 +0100367}
368
369/*! Crate SGs FSM in struct vlr_subscr.
370 * \param[in] vsub VLR subscriber for which the SGs FSM should be created. */
371void vlr_sgs_fsm_create(struct vlr_subscr *vsub)
372{
373 char interim_fsm_id[256];
374 static unsigned int fsm_id_num = 0;
375
376 /* An SGSs FSM must not be created twice! */
377 OSMO_ASSERT(!vsub->sgs_fsm);
378
379 snprintf(interim_fsm_id, sizeof(interim_fsm_id), "num:%u", fsm_id_num);
380
381 vsub->sgs_fsm = osmo_fsm_inst_alloc(&sgs_ue_fsm, vsub, vsub, LOGL_INFO, interim_fsm_id);
382 OSMO_ASSERT(vsub->sgs_fsm);
383
384 osmo_fsm_inst_state_chg(vsub->sgs_fsm, SGS_UE_ST_NULL, 0, 0);
385
386 fsm_id_num++;
387}
388
389/*! Remove SGs FSM from struct vlr_subscr.
390 * \param[in] vsub VLR subscriber from which the SGs FSM should be removed. */
391void vlr_sgs_fsm_remove(struct vlr_subscr *vsub)
392{
393 /* An SGSs FSM must exist! */
394 OSMO_ASSERT(vsub->sgs_fsm);
395
396 osmo_fsm_inst_state_chg(vsub->sgs_fsm, SGS_UE_ST_NULL, 0, 0);
397 osmo_fsm_inst_term(vsub->sgs_fsm, OSMO_FSM_TERM_REGULAR, NULL);
398 vsub->sgs_fsm = NULL;
399}
400
401/*! Update the ID of the SGs FSM with the subscriber IMSI
402 * \param[in] vsub VLR subscriber to update. */
403void vlr_sgs_fsm_update_id(struct vlr_subscr *vsub)
404{
405 char fsm_id[256];
406
407 if (strlen(vsub->imsi) > 0) {
408 snprintf(fsm_id, sizeof(fsm_id), "imsi:%s", vsub->imsi);
409 osmo_fsm_inst_update_id(vsub->sgs_fsm, fsm_id);
410 }
411}