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