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