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