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