blob: 861489227658d6c5d8e12af6222286ed96adb416 [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/msc/debug.h>
24#include <osmocom/msc/vlr.h>
25#include <osmocom/msc/vlr_sgs.h>
26#include "vlr_sgs_fsm.h"
27
28const struct value_string sgs_state_timer_names[] = {
29 {SGS_STATE_TS5, "Ts5"},
30 {SGS_STATE_TS6_2, "Ts6-2"},
31 {SGS_STATE_TS7, "Ts7"},
32 {SGS_STATE_TS11, "Ts11"},
33 {SGS_STATE_TS14, "Ts14"},
34 {SGS_STATE_TS15, "Ts15"},
35 {0, NULL}
36};
37
38const struct value_string sgs_state_counter_names[] = {
39 {SGS_STATE_NS7, "Ns7"},
40 {SGS_STATE_NS11, "Ns11"},
41 {0, NULL}
42};
43
44/* Reset all SGs-Associations back to zero.
45 * \param[in] vlr VLR instace. */
46void vlr_sgs_reset(struct vlr_instance *vlr)
47{
48 struct vlr_subscr *vsub;
49
50 OSMO_ASSERT(vlr);
51
52 LOGP(DVLR, LOGL_INFO, "dropping all SGs associations.\n");
53
54 llist_for_each_entry(vsub, &vlr->subscribers, list) {
55 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_RESET_FROM_MME, NULL);
56 }
57}
58
59/*! Perform an SGs location update.
60 * \param[in] vlr VLR instace.
61 * \param[in] cfg SGs interface configuration parameters.
62 * \param[in] response_cb calback function that is called when LU is done.
63 * \param[in] paging_cb calback function that is called when LU needs to page.
64 * \param[in] mminfo_cb calback function that is called to provide MM info to the UE.
65 * \param[in] mme_name fqdn of the requesting MME (mme-name).
66 * \param[in] type location update type (normal or IMSI attach).
67 * \param[in] imsi mobile identity (IMSI).
68 * \param[in] new_lai identifier of the new location area.
69 * \returns 0 in case of success, -EINVAL in case of error. */
70int vlr_sgs_loc_update(struct vlr_instance *vlr, struct vlr_sgs_cfg *cfg,
71 vlr_sgs_lu_response_cb_t response_cb, vlr_sgs_lu_paging_cb_t paging_cb,
72 vlr_sgs_lu_mminfo_cb_t mminfo_cb, char *mme_name, enum vlr_lu_type type, const char *imsi,
73 struct osmo_location_area_id *new_lai)
74{
75 struct vlr_subscr *vsub = NULL;
76
77 OSMO_ASSERT(response_cb);
78 OSMO_ASSERT(paging_cb);
79 OSMO_ASSERT(mminfo_cb);
80 OSMO_ASSERT(cfg);
81 OSMO_ASSERT(imsi);
82
83 vsub = vlr_subscr_find_or_create_by_imsi(vlr, imsi, NULL);
84 if (!vsub) {
85 LOGP(DSGS, LOGL_ERROR, "VLR subscriber allocation failed\n");
86 return -EINVAL;
87 }
88
89 vsub->sgs.cfg = *cfg;
90 vsub->sgs.response_cb = response_cb;
91 vsub->sgs.paging_cb = paging_cb;
92 vsub->sgs.mminfo_cb = mminfo_cb;
93 vlr_subscr_set_imsi(vsub, imsi);
94 osmo_strlcpy(vsub->sgs.mme_name, mme_name, sizeof(vsub->sgs.mme_name));
95
96 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_LU_FROM_MME, NULL);
97
98 /* FIXME: Use the "type" type parameter (for what is it useful?) */
99
100 vsub->sgs.lai = *new_lai;
101 vsub->cgi.lai = *new_lai;
102 vsub->cs.lac = vsub->sgs.lai.lac;
103
104 return 0;
105}
106
107/*! Notify that the SGs Location Update accept message has been sent to MME.
108 * \param[in] vsub VLR subscriber. */
109void vlr_sgs_loc_update_acc_sent(struct vlr_subscr *vsub)
110{
111 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_TX_LU_ACCEPT, NULL);
112
113 /* FIXME: At this point we need to check the status of Ts5 and if
114 * it is still running this means the LU has interrupted the paging,
115 * and we need to start paging again. 3GPP TS 29.118,
116 * chapter 5.2.3.2 */
117}
118
119/*! Notify that the SGs Location Update reject message has been sent to MME.
120 * \param[in] vsub VLR subscriber. */
121void vlr_sgs_loc_update_rej_sent(struct vlr_subscr *vsub)
122{
123 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_TX_LU_REJECT, NULL);
124}
125
126/*! Perform an SGs IMSI detach.
127 * \param[in] vsub VLR subscriber.
128 * \param[in] imsi mobile identity (IMSI).
129 * \param[in] type datach type. */
130void vlr_sgs_imsi_detach(struct vlr_instance *vlr, const char *imsi, enum sgsap_imsi_det_noneps_type type)
131{
132 struct vlr_subscr *vsub;
133 enum sgs_ue_fsm_event evt;
134 vsub = vlr_subscr_find_by_imsi(vlr, imsi);
135 if (!vsub)
136 return;
137
138 switch (type) {
139 case SGSAP_ID_NONEPS_T_EXPLICIT_UE_NONEPS:
140 evt = SGS_UE_E_RX_DETACH_IND_FROM_UE;
141 break;
142 case SGSAP_ID_NONEPS_T_COMBINED_UE_EPS_NONEPS:
143 case SGSAP_ID_NONEPS_T_IMPLICIT_UE_EPS_NONEPS:
144 /* FIXME: Is that right? */
145 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
146 break;
147 default:
148 LOGP(DSGS, LOGL_ERROR, "(sub %s) invalid SGS IMSI detach type, detaching anyway...\n",
149 vlr_subscr_msisdn_or_name(vsub));
150 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
151 break;
152 }
153
154 osmo_fsm_inst_dispatch(vsub->sgs_fsm, evt, NULL);
155 vlr_subscr_put(vsub);
156}
157
158/*! Perform an SGs EPS detach.
159 * \param[in] vsub VLR subscriber.
160 * \param[in] imsi mobile identity (IMSI).
161 * \param[in] type datach type. */
162void vlr_sgs_eps_detach(struct vlr_instance *vlr, const char *imsi, enum sgsap_imsi_det_eps_type type)
163{
164 struct vlr_subscr *vsub;
165 enum sgs_ue_fsm_event evt;
166 vsub = vlr_subscr_find_by_imsi(vlr, imsi);
167 if (!vsub)
168 return;
169
170 switch (type) {
171 case SGSAP_ID_EPS_T_NETWORK_INITIATED:
172 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
173 break;
174 case SGSAP_ID_EPS_T_UE_INITIATED:
175 evt = SGS_UE_E_RX_DETACH_IND_FROM_UE;
176 break;
177 case SGSAP_ID_EPS_T_EPS_NOT_ALLOWED:
178 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
179 break;
180 default:
181 LOGP(DSGS, LOGL_ERROR, "(sub %s) invalid SGS IMSI detach type, detaching anyway...\n",
182 vlr_subscr_msisdn_or_name(vsub));
183 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
184 break;
185 }
186
187 osmo_fsm_inst_dispatch(vsub->sgs_fsm, evt, NULL);
188 vlr_subscr_put(vsub);
189}
190
191/*! Perform an SGs TMSI reallocation complete.
192 * \param[in] vsub VLR subscriber.
193 * \param[in] imsi mobile identity (IMSI). */
194void vlr_sgs_tmsi_reall_compl(struct vlr_instance *vlr, const char *imsi)
195{
196 struct vlr_subscr *vsub;
197 vsub = vlr_subscr_find_by_imsi(vlr, imsi);
198 if (!vsub)
199 return;
200
201 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_TMSI_REALLOC, NULL);
202 vlr_subscr_put(vsub);
203}
204
205/*! Notify that an SGs paging has been rejected by the MME.
206 * \param[in] vsub VLR subscriber.
207 * \param[in] imsi mobile identity (IMSI).
208 * \param[in] cause SGs cause code. */
209void vlr_sgs_pag_rej(struct vlr_instance *vlr, const char *imsi, enum sgsap_sgs_cause cause)
210{
211 struct vlr_subscr *vsub;
212 vsub = vlr_subscr_find_by_imsi(vlr, imsi);
213 if (!vsub)
214 return;
215
216 /* On the reception of a paging rej the VLR is supposed to stop Ts5,
217 also 3GPP TS 29.118, chapter 5.1.2.4 */
218 osmo_timer_del(&vsub->sgs.Ts5);
219 LOGP(DSGS, LOGL_DEBUG, "(sub %s) Paging via SGs interface rejected by MME, %s stopped, cause: %s!\n",
220 vlr_subscr_msisdn_or_name(vsub), vlr_sgs_state_timer_name(SGS_STATE_TS5), sgsap_sgs_cause_name(cause));
221
222 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_PAGING_FAILURE, &cause);
223 vlr_subscr_put(vsub);
224
225 /* Balance ref count increment from vlr_sgs_pag() */
226 vlr_subscr_put(vsub);
227}
228
229/*! Notify that an SGs paging has been accepted by the MME.
230 * \param[in] vsub VLR subscriber.
231 * \param[in] imsi mobile identity (IMSI). */
232void vlr_sgs_pag_ack(struct vlr_instance *vlr, const char *imsi)
233{
234 struct vlr_subscr *vsub;
235 vsub = vlr_subscr_find_by_imsi(vlr, imsi);
236 if (!vsub)
237 return;
238
239 /* Stop Ts5 and and consider the paging as successful */
240 osmo_timer_del(&vsub->sgs.Ts5);
241 vlr_subscr_put(vsub);
242
243 /* Balance ref count increment from vlr_sgs_pag() */
244 vlr_subscr_put(vsub);
245}
246
247/*! Notify that the UE has been marked as unreachable by the MME.
248 * \param[in] vsub VLR subscriber.
249 * \param[in] imsi mobile identity (IMSI).
250 * \param[in] cause SGs cause code. */
251void vlr_sgs_ue_unr(struct vlr_instance *vlr, const char *imsi, enum sgsap_sgs_cause cause)
252{
253 struct vlr_subscr *vsub;
254 vsub = vlr_subscr_find_by_imsi(vlr, imsi);
255 if (!vsub)
256 return;
257
258 /* On the reception of an UE unreachable the VLR is supposed to stop
259 * Ts5, also 3GPP TS 29.118, chapter 5.1.2.5 */
260 osmo_timer_del(&vsub->sgs.Ts5);
261 LOGP(DSGS, LOGL_DEBUG,
262 "(sub %s) Paging via SGs interface not possible, UE unreachable, %s stopped, cause: %s\n",
263 vlr_subscr_msisdn_or_name(vsub), vlr_sgs_state_timer_name(SGS_STATE_TS5), sgsap_sgs_cause_name(cause));
264
265 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_SGSAP_UE_UNREACHABLE, &cause);
266 vlr_subscr_put(vsub);
267}
268
269/* Callback function that is called when an SGs paging request times out */
270static void Ts5_timeout_cb(void *arg)
271{
272 struct vlr_subscr *vsub = arg;
273
274 /* 3GPP TS 29.118 does not specify a specif action that has to happen
275 * in case Ts5 times out. The timeout just indicates that the paging
276 * failed. Other actions may check the status of Ts5 to see if a paging
277 * is still ongoing or not. */
278
279 LOGP(DSGS, LOGL_ERROR, "(sub %s) Paging via SGs interface timed out (%s expired)!\n",
280 vlr_subscr_msisdn_or_name(vsub), vlr_sgs_state_timer_name(SGS_STATE_TS5));
281
282 /* Balance ref count increment from vlr_sgs_pag() */
283 vlr_subscr_put(vsub);
284
285 return;
286}
287
288/*! Notify that a paging message has been sent and a paging is now in progress.
289 * \param[in] vsub VLR subscriber. */
290void vlr_sgs_pag(struct vlr_subscr *vsub, enum sgsap_service_ind serv_ind)
291{
292
293 /* In cases where we have to respawn a paging after an intermitted LU,
294 * there may e a Ts5 still running. In those cases we have to remove
295 * the old timer first */
296 if (osmo_timer_pending(&vsub->sgs.Ts5))
297 osmo_timer_del(&vsub->sgs.Ts5);
298
299 /* Note: 3GPP TS 29.118, chapter 4.2.2 mentions paging in the FSM
300 * diagram, but paging never causes a state transition except when
301 * an explicit failure is indicated (MME actively rejects paging).
302 * Apparantly it is also possible that an LU happens while the paging
303 * is still ongoing and Ts5 is running. (chapter 5.1.2.3). This means
304 * that the paging procedure is intended to run in parallel to the
305 * SGs FSM and given that the benaviour around Ts5 must be implemented
306 * also separately, to emphasize this separation Ts5 is implemented
307 * here in and not in vlr_sgs_fsm.c. */
308 osmo_timer_setup(&vsub->sgs.Ts5, Ts5_timeout_cb, vsub);
309 osmo_timer_schedule(&vsub->sgs.Ts5, vsub->sgs.cfg.timer[SGS_STATE_TS5], 0);
310
311 /* Formally 3GPP TS 29.118 defines the sending of a paging request
312 * as an event, but as far as the VLR is concerned only Ts5 is
313 * started. */
314 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_TX_PAGING, NULL);
315
316 /* Memorize service type in for the case that the paging must be
317 * respawned after an LU */
318 vsub->sgs.paging_serv_ind = serv_ind;
319
320 /* Ensure that the reference count is increased by one while the
321 * paging is happening. We will balance this again in vlr_sgs_pag_rej()
322 * and vlr_sgs_pag_ack(); */
323 vlr_subscr_get(vsub);
324}
325
326/*! Check if the SGs interface is currently paging
327 * \param[in] vsub VLR subscriber. */
328bool vlr_sgs_pag_pend(struct vlr_subscr *vsub)
329{
330 return osmo_timer_pending(&vsub->sgs.Ts5);
331}