blob: ee1d74804f8923d120df83a025204a196d706351 [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;
131 vsub->lu_complete = true;
132 vlr_sgs_fsm_update_id(vsub);
133 vsub->cs.attached_via_ran = OSMO_RAT_EUTRAN_SGS;
134
135 /* Check if we expect a TMSI REALLOCATION COMPLETE message from the MME
136 * by checking the tmsi_new flag. If this flag is not GSM_RESERVED_TMSI
137 * we know that we have a TMSI pending and need to wait for the MME
138 * to acknowlege first */
139 if (vsub->tmsi_new != GSM_RESERVED_TMSI) {
140 osmo_fsm_inst_state_chg(fi, SGS_UE_ST_ASSOCIATED, vsub->sgs.cfg.timer[SGS_STATE_TS6_2],
141 SGS_STATE_TS6_2);
142 } else {
143 /* Trigger sending of an MM information request */
144 vsub->sgs.mminfo_cb(vsub);
145
146 /* In cases where the LU has interrupted the paging, respawn the paging now,
147 * See also: 3GPP TS 29.118, chapter 5.2.3.2 Location update response */
148 if (vlr_sgs_pag_pend(vsub))
149 respawn_paging(vsub);
150
151 osmo_fsm_inst_state_chg(fi, SGS_UE_ST_ASSOCIATED, 0, 0);
152 }
153
154 break;
155 case SGS_UE_E_RX_PAGING_FAILURE:
156 cause = data;
157 if (*cause == SGSAP_SGS_CAUSE_MT_CSFB_REJ_USER)
158 break;
159 to_null(fi);
160 break;
161 case SGS_UE_E_TX_LU_REJECT:
162 case SGS_UE_E_RX_ALERT_FAILURE:
163 to_null(fi);
164 break;
165 case SGS_UE_E_TX_PAGING:
166 /* do nothing */
167 break;
168 default:
169 OSMO_ASSERT(0);
170 break;
171 }
172}
173
174/* Figure 4.2.2.1 SGs-ASSOCIATED */
175static void sgs_ue_fsm_associated(struct osmo_fsm_inst *fi, uint32_t event, void *data)
176{
177 struct vlr_subscr *vsub = fi->priv;
178 enum sgsap_sgs_cause *cause = NULL;
179
180 switch (event) {
181 case SGS_UE_E_TX_PAGING:
182 /* do nothing */
183 break;
184 case SGS_UE_E_RX_TMSI_REALLOC:
185 if (vsub->tmsi_new == GSM_RESERVED_TMSI) {
186 LOGPFSML(fi, LOGL_ERROR,
187 "(sub %s) TMSI reallocation completed at the MME, but no TMSI reallocation ordered.\n",
188 vlr_subscr_msisdn_or_name(vsub));
189 }
190
191 vsub->tmsi = vsub->tmsi_new;
192 vsub->tmsi_new = GSM_RESERVED_TMSI;
193
194 /* Trigger sending of MM information */
195 vsub->sgs.mminfo_cb(vsub);
196
197 /* In cases where the LU has interrupted the paging, respawn the paging now,
198 * See also: 3GPP TS 29.118, chapter 5.2.3.2 Location update response */
199 if (vlr_sgs_pag_pend(vsub))
200 respawn_paging(vsub);
201
202 /* Note: We are already in SGS_UE_ST_ASSOCIATED but the
203 * transition that lead us here had is guarded with Ts6-1,
204 * wo we change the state now once more without timeout
205 * to ensure the timer is stopped */
206 osmo_fsm_inst_state_chg(fi, SGS_UE_ST_ASSOCIATED, 0, 0);
207 break;
208 case SGS_UE_E_RX_SGSAP_UE_UNREACHABLE:
209 /* do nothing */
210 break;
211 case SGS_UE_E_RX_PAGING_FAILURE:
212 cause = data;
213 if (*cause == SGSAP_SGS_CAUSE_MT_CSFB_REJ_USER)
214 break;
215 to_null(fi);
216 case SGS_UE_E_RX_ALERT_FAILURE:
217 to_null(fi);
218 break;
219 case SGS_UE_E_RX_LU_FROM_MME:
220 perform_lu(fi);
221 break;
222 default:
223 OSMO_ASSERT(0);
224 break;
225 }
226}
227
228/* Figure 4.2.2.1 From any of the three states (at the VLR) */
229static void sgs_ue_fsm_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *data)
230{
231 struct vlr_subscr *vsub = fi->priv;
232
233 switch (event) {
234 case SGS_UE_E_RX_DETACH_IND_FROM_MME:
235 case SGS_UE_E_RX_DETACH_IND_FROM_UE:
236 vsub->imsi_detached_flag = true;
237 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
238 /* See 5.4.3 and 5.5.3 */
239 to_null(fi);
240 break;
241 case SGS_UE_E_RX_RESET_FROM_MME:
242 /* See also 3GPP TS 29.118, chapter 5.7.2.1 VLR Reset Initiation */
243 vsub->conf_by_radio_contact_ind = false;
244 to_null(fi);
245 break;
246 case SGS_UE_E_VLR_FAILURE:
247 case SGS_UE_E_RX_LU_FROM_A_IU_GS:
248 to_null(fi);
249 break;
250 default:
251 OSMO_ASSERT(0);
252 break;
253 }
254}
255
256static int sgs_ue_fsm_timer_cb(struct osmo_fsm_inst *fi)
257{
258 struct vlr_subscr *vsub = fi->priv;
259 switch (fi->T) {
260
261 case SGS_STATE_TS6_2:
262 /* Failed TMSI reallocation procedure, deallocate all TMSI
263 * information, but don't change the SGs association state. */
264 vsub->tmsi_new = GSM_RESERVED_TMSI;
265 vsub->tmsi = GSM_RESERVED_TMSI;
266 break;
267 default:
268 /* Unhandled timer */
269 OSMO_ASSERT(false);
270 break;
271 }
272 return 0;
273}
274
275static const struct osmo_fsm_state sgs_ue_fsm_states[] = {
276 [SGS_UE_ST_NULL] = {
277 .name = "SGs-NULL",
278 .action = sgs_ue_fsm_null,
279 .in_event_mask = 0
280 | S(SGS_UE_E_RX_LU_FROM_MME)
281 | S(SGS_UE_E_TX_PAGING)
282 | S(SGS_UE_E_RX_PAGING_FAILURE)
283 ,
284 .out_state_mask = 0
285 | S(SGS_UE_ST_NULL)
286 | S(SGS_UE_ST_LA_UPD_PRES)
287 ,
288 },
289 [SGS_UE_ST_LA_UPD_PRES] = {
290 .name = "SGs-LA-UPDATE-PRESENT",
291 .action = sgs_ue_fsm_lau_present,
292 .in_event_mask = 0
293 | S(SGS_UE_E_TX_LU_ACCEPT)
294 | S(SGS_UE_E_TX_LU_REJECT)
295 | S(SGS_UE_E_TX_PAGING)
296 | S(SGS_UE_E_RX_PAGING_FAILURE)
297 | S(SGS_UE_E_RX_ALERT_FAILURE)
298 ,
299 .out_state_mask = 0
300 | S(SGS_UE_ST_NULL)
301 | S(SGS_UE_ST_ASSOCIATED)
302 | S(SGS_UE_ST_LA_UPD_PRES)
303 ,
304 },
305 [SGS_UE_ST_ASSOCIATED] = {
306 .name = "SGs-ASSOCIATED",
307 .action = sgs_ue_fsm_associated,
308 .in_event_mask = 0
309 | S(SGS_UE_E_TX_PAGING)
310 | S(SGS_UE_E_RX_TMSI_REALLOC)
311 | S(SGS_UE_E_RX_SGSAP_UE_UNREACHABLE)
312 | S(SGS_UE_E_RX_PAGING_FAILURE)
313 | S(SGS_UE_E_RX_ALERT_FAILURE)
314 | S(SGS_UE_E_RX_LU_FROM_MME)
315 ,
316 .out_state_mask = 0
317 | S(SGS_UE_ST_NULL)
318 | S(SGS_UE_ST_ASSOCIATED)
319 | S(SGS_UE_ST_LA_UPD_PRES)
320 ,
321 },
322};
323
324static struct osmo_fsm sgs_ue_fsm = {
325 .name = "SGs-UE",
326 .states = sgs_ue_fsm_states,
327 .num_states = ARRAY_SIZE(sgs_ue_fsm_states),
328 .allstate_event_mask = S(SGS_UE_E_RX_RESET_FROM_MME) |
329 S(SGS_UE_E_VLR_FAILURE) | S(SGS_UE_E_RX_DETACH_IND_FROM_MME) | S(SGS_UE_E_RX_DETACH_IND_FROM_UE) |
330 S(SGS_UE_E_RX_LU_FROM_A_IU_GS),
331 .allstate_action = sgs_ue_fsm_allstate,
332 .timer_cb = sgs_ue_fsm_timer_cb,
333 .log_subsys = DSGS,
334 .event_names = sgs_ue_fsm_event_names,
335};
336
337/*! Initalize/Register SGs FSM in osmo-fsm subsystem */
338void vlr_sgs_fsm_init(void)
339{
340 if (osmo_fsm_find_by_name(sgs_ue_fsm.name) != &sgs_ue_fsm)
341 osmo_fsm_register(&sgs_ue_fsm);
342}
343
344/*! Crate SGs FSM in struct vlr_subscr.
345 * \param[in] vsub VLR subscriber for which the SGs FSM should be created. */
346void vlr_sgs_fsm_create(struct vlr_subscr *vsub)
347{
348 char interim_fsm_id[256];
349 static unsigned int fsm_id_num = 0;
350
351 /* An SGSs FSM must not be created twice! */
352 OSMO_ASSERT(!vsub->sgs_fsm);
353
354 snprintf(interim_fsm_id, sizeof(interim_fsm_id), "num:%u", fsm_id_num);
355
356 vsub->sgs_fsm = osmo_fsm_inst_alloc(&sgs_ue_fsm, vsub, vsub, LOGL_INFO, interim_fsm_id);
357 OSMO_ASSERT(vsub->sgs_fsm);
358
359 osmo_fsm_inst_state_chg(vsub->sgs_fsm, SGS_UE_ST_NULL, 0, 0);
360
361 fsm_id_num++;
362}
363
364/*! Remove SGs FSM from struct vlr_subscr.
365 * \param[in] vsub VLR subscriber from which the SGs FSM should be removed. */
366void vlr_sgs_fsm_remove(struct vlr_subscr *vsub)
367{
368 /* An SGSs FSM must exist! */
369 OSMO_ASSERT(vsub->sgs_fsm);
370
371 osmo_fsm_inst_state_chg(vsub->sgs_fsm, SGS_UE_ST_NULL, 0, 0);
372 osmo_fsm_inst_term(vsub->sgs_fsm, OSMO_FSM_TERM_REGULAR, NULL);
373 vsub->sgs_fsm = NULL;
374}
375
376/*! Update the ID of the SGs FSM with the subscriber IMSI
377 * \param[in] vsub VLR subscriber to update. */
378void vlr_sgs_fsm_update_id(struct vlr_subscr *vsub)
379{
380 char fsm_id[256];
381
382 if (strlen(vsub->imsi) > 0) {
383 snprintf(fsm_id, sizeof(fsm_id), "imsi:%s", vsub->imsi);
384 osmo_fsm_inst_update_id(vsub->sgs_fsm, fsm_id);
385 }
386}