blob: 49ad09a591ff1bb73f00da6300c55db0972bdba0 [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
57 /* Note: This is only relevent for cases where we are in the middle
58 * 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
159 * to acknowlege first */
160 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);
237 case SGS_UE_E_RX_ALERT_FAILURE:
238 to_null(fi);
239 break;
240 case SGS_UE_E_RX_LU_FROM_MME:
241 perform_lu(fi);
242 break;
243 default:
244 OSMO_ASSERT(0);
245 break;
246 }
247}
248
249/* Figure 4.2.2.1 From any of the three states (at the VLR) */
250static void sgs_ue_fsm_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *data)
251{
252 struct vlr_subscr *vsub = fi->priv;
253
254 switch (event) {
255 case SGS_UE_E_RX_DETACH_IND_FROM_MME:
256 case SGS_UE_E_RX_DETACH_IND_FROM_UE:
257 vsub->imsi_detached_flag = true;
258 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
259 /* See 5.4.3 and 5.5.3 */
260 to_null(fi);
261 break;
262 case SGS_UE_E_RX_RESET_FROM_MME:
263 /* See also 3GPP TS 29.118, chapter 5.7.2.1 VLR Reset Initiation */
264 vsub->conf_by_radio_contact_ind = false;
265 to_null(fi);
266 break;
267 case SGS_UE_E_VLR_FAILURE:
268 case SGS_UE_E_RX_LU_FROM_A_IU_GS:
269 to_null(fi);
270 break;
271 default:
272 OSMO_ASSERT(0);
273 break;
274 }
275}
276
277static int sgs_ue_fsm_timer_cb(struct osmo_fsm_inst *fi)
278{
279 struct vlr_subscr *vsub = fi->priv;
280 switch (fi->T) {
281
282 case SGS_STATE_TS6_2:
283 /* Failed TMSI reallocation procedure, deallocate all TMSI
284 * information, but don't change the SGs association state. */
285 vsub->tmsi_new = GSM_RESERVED_TMSI;
286 vsub->tmsi = GSM_RESERVED_TMSI;
287 break;
288 default:
289 /* Unhandled timer */
290 OSMO_ASSERT(false);
291 break;
292 }
293 return 0;
294}
295
296static const struct osmo_fsm_state sgs_ue_fsm_states[] = {
297 [SGS_UE_ST_NULL] = {
298 .name = "SGs-NULL",
299 .action = sgs_ue_fsm_null,
300 .in_event_mask = 0
301 | S(SGS_UE_E_RX_LU_FROM_MME)
302 | S(SGS_UE_E_TX_PAGING)
303 | S(SGS_UE_E_RX_PAGING_FAILURE)
304 ,
305 .out_state_mask = 0
306 | S(SGS_UE_ST_NULL)
307 | S(SGS_UE_ST_LA_UPD_PRES)
308 ,
309 },
310 [SGS_UE_ST_LA_UPD_PRES] = {
311 .name = "SGs-LA-UPDATE-PRESENT",
312 .action = sgs_ue_fsm_lau_present,
313 .in_event_mask = 0
314 | S(SGS_UE_E_TX_LU_ACCEPT)
315 | S(SGS_UE_E_TX_LU_REJECT)
316 | S(SGS_UE_E_TX_PAGING)
317 | S(SGS_UE_E_RX_PAGING_FAILURE)
318 | S(SGS_UE_E_RX_ALERT_FAILURE)
319 ,
320 .out_state_mask = 0
321 | S(SGS_UE_ST_NULL)
322 | S(SGS_UE_ST_ASSOCIATED)
323 | S(SGS_UE_ST_LA_UPD_PRES)
324 ,
325 },
326 [SGS_UE_ST_ASSOCIATED] = {
327 .name = "SGs-ASSOCIATED",
328 .action = sgs_ue_fsm_associated,
329 .in_event_mask = 0
330 | S(SGS_UE_E_TX_PAGING)
331 | S(SGS_UE_E_RX_TMSI_REALLOC)
332 | S(SGS_UE_E_RX_SGSAP_UE_UNREACHABLE)
333 | S(SGS_UE_E_RX_PAGING_FAILURE)
334 | S(SGS_UE_E_RX_ALERT_FAILURE)
335 | S(SGS_UE_E_RX_LU_FROM_MME)
336 ,
337 .out_state_mask = 0
338 | S(SGS_UE_ST_NULL)
339 | S(SGS_UE_ST_ASSOCIATED)
340 | S(SGS_UE_ST_LA_UPD_PRES)
341 ,
342 },
343};
344
345static struct osmo_fsm sgs_ue_fsm = {
346 .name = "SGs-UE",
347 .states = sgs_ue_fsm_states,
348 .num_states = ARRAY_SIZE(sgs_ue_fsm_states),
349 .allstate_event_mask = S(SGS_UE_E_RX_RESET_FROM_MME) |
350 S(SGS_UE_E_VLR_FAILURE) | S(SGS_UE_E_RX_DETACH_IND_FROM_MME) | S(SGS_UE_E_RX_DETACH_IND_FROM_UE) |
351 S(SGS_UE_E_RX_LU_FROM_A_IU_GS),
352 .allstate_action = sgs_ue_fsm_allstate,
353 .timer_cb = sgs_ue_fsm_timer_cb,
354 .log_subsys = DSGS,
355 .event_names = sgs_ue_fsm_event_names,
356};
357
358/*! Initalize/Register SGs FSM in osmo-fsm subsystem */
359void vlr_sgs_fsm_init(void)
360{
361 if (osmo_fsm_find_by_name(sgs_ue_fsm.name) != &sgs_ue_fsm)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100362 OSMO_ASSERT(osmo_fsm_register(&sgs_ue_fsm) == 0);
Harald Welte0df904d2018-12-03 11:00:04 +0100363}
364
365/*! Crate SGs FSM in struct vlr_subscr.
366 * \param[in] vsub VLR subscriber for which the SGs FSM should be created. */
367void vlr_sgs_fsm_create(struct vlr_subscr *vsub)
368{
369 char interim_fsm_id[256];
370 static unsigned int fsm_id_num = 0;
371
372 /* An SGSs FSM must not be created twice! */
373 OSMO_ASSERT(!vsub->sgs_fsm);
374
375 snprintf(interim_fsm_id, sizeof(interim_fsm_id), "num:%u", fsm_id_num);
376
377 vsub->sgs_fsm = osmo_fsm_inst_alloc(&sgs_ue_fsm, vsub, vsub, LOGL_INFO, interim_fsm_id);
378 OSMO_ASSERT(vsub->sgs_fsm);
379
380 osmo_fsm_inst_state_chg(vsub->sgs_fsm, SGS_UE_ST_NULL, 0, 0);
381
382 fsm_id_num++;
383}
384
385/*! Remove SGs FSM from struct vlr_subscr.
386 * \param[in] vsub VLR subscriber from which the SGs FSM should be removed. */
387void vlr_sgs_fsm_remove(struct vlr_subscr *vsub)
388{
389 /* An SGSs FSM must exist! */
390 OSMO_ASSERT(vsub->sgs_fsm);
391
392 osmo_fsm_inst_state_chg(vsub->sgs_fsm, SGS_UE_ST_NULL, 0, 0);
393 osmo_fsm_inst_term(vsub->sgs_fsm, OSMO_FSM_TERM_REGULAR, NULL);
394 vsub->sgs_fsm = NULL;
395}
396
397/*! Update the ID of the SGs FSM with the subscriber IMSI
398 * \param[in] vsub VLR subscriber to update. */
399void vlr_sgs_fsm_update_id(struct vlr_subscr *vsub)
400{
401 char fsm_id[256];
402
403 if (strlen(vsub->imsi) > 0) {
404 snprintf(fsm_id, sizeof(fsm_id), "imsi:%s", vsub->imsi);
405 osmo_fsm_inst_update_id(vsub->sgs_fsm, fsm_id);
406 }
407}