blob: 269dda64b139814cbb7ed94b5113e647ca8e4427 [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
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010021#include <errno.h>
22
Harald Welte0df904d2018-12-03 11:00:04 +010023#include <osmocom/core/utils.h>
24#include <osmocom/core/fsm.h>
25#include <osmocom/msc/debug.h>
26#include <osmocom/msc/vlr.h>
27#include <osmocom/msc/vlr_sgs.h>
28#include "vlr_sgs_fsm.h"
29
30const struct value_string sgs_state_timer_names[] = {
31 {SGS_STATE_TS5, "Ts5"},
32 {SGS_STATE_TS6_2, "Ts6-2"},
33 {SGS_STATE_TS7, "Ts7"},
34 {SGS_STATE_TS11, "Ts11"},
35 {SGS_STATE_TS14, "Ts14"},
36 {SGS_STATE_TS15, "Ts15"},
37 {0, NULL}
38};
39
40const struct value_string sgs_state_counter_names[] = {
41 {SGS_STATE_NS7, "Ns7"},
42 {SGS_STATE_NS11, "Ns11"},
43 {0, NULL}
44};
45
46/* Reset all SGs-Associations back to zero.
Martin Hauke3f07dac2019-11-14 17:49:08 +010047 * \param[in] vlr VLR instance. */
Harald Welte0df904d2018-12-03 11:00:04 +010048void vlr_sgs_reset(struct vlr_instance *vlr)
49{
50 struct vlr_subscr *vsub;
51
52 OSMO_ASSERT(vlr);
53
54 LOGP(DVLR, LOGL_INFO, "dropping all SGs associations.\n");
55
56 llist_for_each_entry(vsub, &vlr->subscribers, list) {
57 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_RESET_FROM_MME, NULL);
58 }
59}
60
61/*! Perform an SGs location update.
Martin Hauke3f07dac2019-11-14 17:49:08 +010062 * \param[in] vlr VLR instance.
Harald Welte0df904d2018-12-03 11:00:04 +010063 * \param[in] cfg SGs interface configuration parameters.
Martin Hauke3f07dac2019-11-14 17:49:08 +010064 * \param[in] response_cb callback function that is called when LU is done.
65 * \param[in] paging_cb callback function that is called when LU needs to page.
66 * \param[in] mminfo_cb callback function that is called to provide MM info to the UE.
Harald Welte0df904d2018-12-03 11:00:04 +010067 * \param[in] mme_name fqdn of the requesting MME (mme-name).
68 * \param[in] type location update type (normal or IMSI attach).
69 * \param[in] imsi mobile identity (IMSI).
70 * \param[in] new_lai identifier of the new location area.
71 * \returns 0 in case of success, -EINVAL in case of error. */
72int vlr_sgs_loc_update(struct vlr_instance *vlr, struct vlr_sgs_cfg *cfg,
73 vlr_sgs_lu_response_cb_t response_cb, vlr_sgs_lu_paging_cb_t paging_cb,
74 vlr_sgs_lu_mminfo_cb_t mminfo_cb, char *mme_name, enum vlr_lu_type type, const char *imsi,
75 struct osmo_location_area_id *new_lai)
76{
77 struct vlr_subscr *vsub = NULL;
78
79 OSMO_ASSERT(response_cb);
80 OSMO_ASSERT(paging_cb);
81 OSMO_ASSERT(mminfo_cb);
82 OSMO_ASSERT(cfg);
83 OSMO_ASSERT(imsi);
84
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +010085 vsub = vlr_subscr_find_or_create_by_imsi(vlr, imsi, VSUB_USE_SGS, NULL);
Harald Welte0df904d2018-12-03 11:00:04 +010086 if (!vsub) {
87 LOGP(DSGS, LOGL_ERROR, "VLR subscriber allocation failed\n");
88 return -EINVAL;
89 }
90
91 vsub->sgs.cfg = *cfg;
92 vsub->sgs.response_cb = response_cb;
93 vsub->sgs.paging_cb = paging_cb;
94 vsub->sgs.mminfo_cb = mminfo_cb;
95 vlr_subscr_set_imsi(vsub, imsi);
96 osmo_strlcpy(vsub->sgs.mme_name, mme_name, sizeof(vsub->sgs.mme_name));
97
98 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_LU_FROM_MME, NULL);
99
100 /* FIXME: Use the "type" type parameter (for what is it useful?) */
101
102 vsub->sgs.lai = *new_lai;
103 vsub->cgi.lai = *new_lai;
104 vsub->cs.lac = vsub->sgs.lai.lac;
105
Philipp Maier48264652019-04-02 16:58:59 +0200106 /* Subscribers that are created by the SGs location update will not
107 * expire automatically, however a 2G LU or an implicit IMSI detach
108 * from EPS services may change this. */
109 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
110
Harald Welte0df904d2018-12-03 11:00:04 +0100111 return 0;
112}
113
114/*! Notify that the SGs Location Update accept message has been sent to MME.
115 * \param[in] vsub VLR subscriber. */
116void vlr_sgs_loc_update_acc_sent(struct vlr_subscr *vsub)
117{
118 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_TX_LU_ACCEPT, NULL);
119
120 /* FIXME: At this point we need to check the status of Ts5 and if
121 * it is still running this means the LU has interrupted the paging,
122 * and we need to start paging again. 3GPP TS 29.118,
123 * chapter 5.2.3.2 */
124}
125
126/*! Notify that the SGs Location Update reject message has been sent to MME.
127 * \param[in] vsub VLR subscriber. */
128void vlr_sgs_loc_update_rej_sent(struct vlr_subscr *vsub)
129{
130 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_TX_LU_REJECT, NULL);
131}
132
133/*! Perform an SGs IMSI detach.
134 * \param[in] vsub VLR subscriber.
135 * \param[in] imsi mobile identity (IMSI).
136 * \param[in] type datach type. */
137void vlr_sgs_imsi_detach(struct vlr_instance *vlr, const char *imsi, enum sgsap_imsi_det_noneps_type type)
138{
139 struct vlr_subscr *vsub;
140 enum sgs_ue_fsm_event evt;
Philipp Maier0803d882019-04-02 16:07:13 +0200141
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100142 vsub = vlr_subscr_find_by_imsi(vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100143 if (!vsub)
144 return;
145
Philipp Maier0803d882019-04-02 16:07:13 +0200146 /* See also: 3GPP TS 29.118, 5.6.3 Procedures in the VLR: In case of
147 * an implicit detach, we are supposed to check if the state of the
148 * SGs-association, and only when it is not SGs-NULL, we may proceed. */
149 if (vsub->sgs_fsm->state == SGS_UE_ST_NULL && type == SGSAP_ID_NONEPS_T_IMPLICIT_UE_EPS_NONEPS)
150 return;
151
Harald Welte0df904d2018-12-03 11:00:04 +0100152 switch (type) {
153 case SGSAP_ID_NONEPS_T_EXPLICIT_UE_NONEPS:
154 evt = SGS_UE_E_RX_DETACH_IND_FROM_UE;
155 break;
156 case SGSAP_ID_NONEPS_T_COMBINED_UE_EPS_NONEPS:
157 case SGSAP_ID_NONEPS_T_IMPLICIT_UE_EPS_NONEPS:
158 /* FIXME: Is that right? */
159 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
160 break;
161 default:
162 LOGP(DSGS, LOGL_ERROR, "(sub %s) invalid SGS IMSI detach type, detaching anyway...\n",
163 vlr_subscr_msisdn_or_name(vsub));
164 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
165 break;
166 }
167
168 osmo_fsm_inst_dispatch(vsub->sgs_fsm, evt, NULL);
Philipp Maier0803d882019-04-02 16:07:13 +0200169
170 /* Detaching from non EPS services essentially means that the
171 * subscriber is detached from 2G. In any case the VLR will
172 * get rid of the subscriber. */
173 vlr_subscr_expire(vsub);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100174 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100175}
176
177/*! Perform an SGs EPS detach.
178 * \param[in] vsub VLR subscriber.
179 * \param[in] imsi mobile identity (IMSI).
180 * \param[in] type datach type. */
181void vlr_sgs_eps_detach(struct vlr_instance *vlr, const char *imsi, enum sgsap_imsi_det_eps_type type)
182{
183 struct vlr_subscr *vsub;
184 enum sgs_ue_fsm_event evt;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100185 vsub = vlr_subscr_find_by_imsi(vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100186 if (!vsub)
187 return;
188
189 switch (type) {
190 case SGSAP_ID_EPS_T_NETWORK_INITIATED:
191 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
192 break;
193 case SGSAP_ID_EPS_T_UE_INITIATED:
194 evt = SGS_UE_E_RX_DETACH_IND_FROM_UE;
195 break;
196 case SGSAP_ID_EPS_T_EPS_NOT_ALLOWED:
197 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
198 break;
199 default:
200 LOGP(DSGS, LOGL_ERROR, "(sub %s) invalid SGS IMSI detach type, detaching anyway...\n",
201 vlr_subscr_msisdn_or_name(vsub));
202 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
203 break;
204 }
205
206 osmo_fsm_inst_dispatch(vsub->sgs_fsm, evt, NULL);
Philipp Maier48264652019-04-02 16:58:59 +0200207
208 /* See also 3GPP TS 29.118, 5.4.3 Procedures in the VLR. Detaching from
209 * EPS services essentially means that the subscriber leaves the 4G RAN
210 * but continues to live on 2G, this basically turns the subscriber into
211 * a normal 2G subscriber and we need to make sure that the lu-
212 * expiration timer is running. */
213 if (vsub->expire_lu == VLR_SUBSCRIBER_NO_EXPIRATION)
214 vlr_subscr_enable_expire_lu(vsub);
215
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100216 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100217}
218
219/*! Perform an SGs TMSI reallocation complete.
220 * \param[in] vsub VLR subscriber.
221 * \param[in] imsi mobile identity (IMSI). */
222void vlr_sgs_tmsi_reall_compl(struct vlr_instance *vlr, const char *imsi)
223{
224 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100225 vsub = vlr_subscr_find_by_imsi(vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100226 if (!vsub)
227 return;
228
229 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_TMSI_REALLOC, NULL);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100230 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100231}
232
233/*! Notify that an SGs paging has been rejected by the MME.
234 * \param[in] vsub VLR subscriber.
235 * \param[in] imsi mobile identity (IMSI).
236 * \param[in] cause SGs cause code. */
237void vlr_sgs_pag_rej(struct vlr_instance *vlr, const char *imsi, enum sgsap_sgs_cause cause)
238{
239 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100240 vsub = vlr_subscr_find_by_imsi(vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100241 if (!vsub)
242 return;
243
244 /* On the reception of a paging rej the VLR is supposed to stop Ts5,
245 also 3GPP TS 29.118, chapter 5.1.2.4 */
246 osmo_timer_del(&vsub->sgs.Ts5);
247 LOGP(DSGS, LOGL_DEBUG, "(sub %s) Paging via SGs interface rejected by MME, %s stopped, cause: %s!\n",
248 vlr_subscr_msisdn_or_name(vsub), vlr_sgs_state_timer_name(SGS_STATE_TS5), sgsap_sgs_cause_name(cause));
249
250 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_PAGING_FAILURE, &cause);
Harald Welte0df904d2018-12-03 11:00:04 +0100251 /* Balance ref count increment from vlr_sgs_pag() */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100252 vlr_subscr_put(vsub, VSUB_USE_SGS_PAGING_REQ);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100253
254 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100255}
256
257/*! Notify that an SGs paging has been accepted by the MME.
258 * \param[in] vsub VLR subscriber.
259 * \param[in] imsi mobile identity (IMSI). */
260void vlr_sgs_pag_ack(struct vlr_instance *vlr, const char *imsi)
261{
262 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100263 vsub = vlr_subscr_find_by_imsi(vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100264 if (!vsub)
265 return;
266
267 /* Stop Ts5 and and consider the paging as successful */
268 osmo_timer_del(&vsub->sgs.Ts5);
Harald Welte0df904d2018-12-03 11:00:04 +0100269 /* Balance ref count increment from vlr_sgs_pag() */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100270 vlr_subscr_put(vsub, VSUB_USE_SGS_PAGING_REQ);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100271
272 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100273}
274
275/*! Notify that the UE has been marked as unreachable by the MME.
276 * \param[in] vsub VLR subscriber.
277 * \param[in] imsi mobile identity (IMSI).
278 * \param[in] cause SGs cause code. */
279void vlr_sgs_ue_unr(struct vlr_instance *vlr, const char *imsi, enum sgsap_sgs_cause cause)
280{
281 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100282 vsub = vlr_subscr_find_by_imsi(vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100283 if (!vsub)
284 return;
285
286 /* On the reception of an UE unreachable the VLR is supposed to stop
287 * Ts5, also 3GPP TS 29.118, chapter 5.1.2.5 */
288 osmo_timer_del(&vsub->sgs.Ts5);
289 LOGP(DSGS, LOGL_DEBUG,
290 "(sub %s) Paging via SGs interface not possible, UE unreachable, %s stopped, cause: %s\n",
291 vlr_subscr_msisdn_or_name(vsub), vlr_sgs_state_timer_name(SGS_STATE_TS5), sgsap_sgs_cause_name(cause));
292
293 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_SGSAP_UE_UNREACHABLE, &cause);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100294 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100295}
296
297/* Callback function that is called when an SGs paging request times out */
298static void Ts5_timeout_cb(void *arg)
299{
300 struct vlr_subscr *vsub = arg;
301
Martin Hauke3f07dac2019-11-14 17:49:08 +0100302 /* 3GPP TS 29.118 does not specify a specific action that has to happen
Harald Welte0df904d2018-12-03 11:00:04 +0100303 * in case Ts5 times out. The timeout just indicates that the paging
304 * failed. Other actions may check the status of Ts5 to see if a paging
305 * is still ongoing or not. */
306
307 LOGP(DSGS, LOGL_ERROR, "(sub %s) Paging via SGs interface timed out (%s expired)!\n",
308 vlr_subscr_msisdn_or_name(vsub), vlr_sgs_state_timer_name(SGS_STATE_TS5));
309
310 /* Balance ref count increment from vlr_sgs_pag() */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100311 vlr_subscr_put(vsub, VSUB_USE_SGS_PAGING_REQ);
Harald Welte0df904d2018-12-03 11:00:04 +0100312
313 return;
314}
315
316/*! Notify that a paging message has been sent and a paging is now in progress.
317 * \param[in] vsub VLR subscriber. */
318void vlr_sgs_pag(struct vlr_subscr *vsub, enum sgsap_service_ind serv_ind)
319{
320
321 /* In cases where we have to respawn a paging after an intermitted LU,
322 * there may e a Ts5 still running. In those cases we have to remove
323 * the old timer first */
324 if (osmo_timer_pending(&vsub->sgs.Ts5))
325 osmo_timer_del(&vsub->sgs.Ts5);
326
327 /* Note: 3GPP TS 29.118, chapter 4.2.2 mentions paging in the FSM
328 * diagram, but paging never causes a state transition except when
329 * an explicit failure is indicated (MME actively rejects paging).
Martin Hauke3f07dac2019-11-14 17:49:08 +0100330 * Apparently it is also possible that an LU happens while the paging
Harald Welte0df904d2018-12-03 11:00:04 +0100331 * is still ongoing and Ts5 is running. (chapter 5.1.2.3). This means
332 * that the paging procedure is intended to run in parallel to the
333 * SGs FSM and given that the benaviour around Ts5 must be implemented
334 * also separately, to emphasize this separation Ts5 is implemented
335 * here in and not in vlr_sgs_fsm.c. */
336 osmo_timer_setup(&vsub->sgs.Ts5, Ts5_timeout_cb, vsub);
337 osmo_timer_schedule(&vsub->sgs.Ts5, vsub->sgs.cfg.timer[SGS_STATE_TS5], 0);
338
339 /* Formally 3GPP TS 29.118 defines the sending of a paging request
340 * as an event, but as far as the VLR is concerned only Ts5 is
341 * started. */
342 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_TX_PAGING, NULL);
343
344 /* Memorize service type in for the case that the paging must be
345 * respawned after an LU */
346 vsub->sgs.paging_serv_ind = serv_ind;
347
348 /* Ensure that the reference count is increased by one while the
349 * paging is happening. We will balance this again in vlr_sgs_pag_rej()
350 * and vlr_sgs_pag_ack(); */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100351 vlr_subscr_get(vsub, VSUB_USE_SGS_PAGING_REQ);
Harald Welte0df904d2018-12-03 11:00:04 +0100352}
353
354/*! Check if the SGs interface is currently paging
355 * \param[in] vsub VLR subscriber. */
356bool vlr_sgs_pag_pend(struct vlr_subscr *vsub)
357{
358 return osmo_timer_pending(&vsub->sgs.Ts5);
359}