blob: 06737db46e3ee85f23f3c14ed0019f1ce3503b08 [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
Philipp Maier48264652019-04-02 16:58:59 +0200104 /* Subscribers that are created by the SGs location update will not
105 * expire automatically, however a 2G LU or an implicit IMSI detach
106 * from EPS services may change this. */
107 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
108
Harald Welte0df904d2018-12-03 11:00:04 +0100109 return 0;
110}
111
112/*! Notify that the SGs Location Update accept message has been sent to MME.
113 * \param[in] vsub VLR subscriber. */
114void vlr_sgs_loc_update_acc_sent(struct vlr_subscr *vsub)
115{
116 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_TX_LU_ACCEPT, NULL);
117
118 /* FIXME: At this point we need to check the status of Ts5 and if
119 * it is still running this means the LU has interrupted the paging,
120 * and we need to start paging again. 3GPP TS 29.118,
121 * chapter 5.2.3.2 */
122}
123
124/*! Notify that the SGs Location Update reject message has been sent to MME.
125 * \param[in] vsub VLR subscriber. */
126void vlr_sgs_loc_update_rej_sent(struct vlr_subscr *vsub)
127{
128 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_TX_LU_REJECT, NULL);
129}
130
131/*! Perform an SGs IMSI detach.
132 * \param[in] vsub VLR subscriber.
133 * \param[in] imsi mobile identity (IMSI).
134 * \param[in] type datach type. */
135void vlr_sgs_imsi_detach(struct vlr_instance *vlr, const char *imsi, enum sgsap_imsi_det_noneps_type type)
136{
137 struct vlr_subscr *vsub;
138 enum sgs_ue_fsm_event evt;
Philipp Maier0803d882019-04-02 16:07:13 +0200139
Harald Welte0df904d2018-12-03 11:00:04 +0100140 vsub = vlr_subscr_find_by_imsi(vlr, imsi);
141 if (!vsub)
142 return;
143
Philipp Maier0803d882019-04-02 16:07:13 +0200144 /* See also: 3GPP TS 29.118, 5.6.3 Procedures in the VLR: In case of
145 * an implicit detach, we are supposed to check if the state of the
146 * SGs-association, and only when it is not SGs-NULL, we may proceed. */
147 if (vsub->sgs_fsm->state == SGS_UE_ST_NULL && type == SGSAP_ID_NONEPS_T_IMPLICIT_UE_EPS_NONEPS)
148 return;
149
Harald Welte0df904d2018-12-03 11:00:04 +0100150 switch (type) {
151 case SGSAP_ID_NONEPS_T_EXPLICIT_UE_NONEPS:
152 evt = SGS_UE_E_RX_DETACH_IND_FROM_UE;
153 break;
154 case SGSAP_ID_NONEPS_T_COMBINED_UE_EPS_NONEPS:
155 case SGSAP_ID_NONEPS_T_IMPLICIT_UE_EPS_NONEPS:
156 /* FIXME: Is that right? */
157 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
158 break;
159 default:
160 LOGP(DSGS, LOGL_ERROR, "(sub %s) invalid SGS IMSI detach type, detaching anyway...\n",
161 vlr_subscr_msisdn_or_name(vsub));
162 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
163 break;
164 }
165
166 osmo_fsm_inst_dispatch(vsub->sgs_fsm, evt, NULL);
167 vlr_subscr_put(vsub);
Philipp Maier0803d882019-04-02 16:07:13 +0200168
169 /* Detaching from non EPS services essentially means that the
170 * subscriber is detached from 2G. In any case the VLR will
171 * get rid of the subscriber. */
172 vlr_subscr_expire(vsub);
Harald Welte0df904d2018-12-03 11:00:04 +0100173}
174
175/*! Perform an SGs EPS detach.
176 * \param[in] vsub VLR subscriber.
177 * \param[in] imsi mobile identity (IMSI).
178 * \param[in] type datach type. */
179void vlr_sgs_eps_detach(struct vlr_instance *vlr, const char *imsi, enum sgsap_imsi_det_eps_type type)
180{
181 struct vlr_subscr *vsub;
182 enum sgs_ue_fsm_event evt;
183 vsub = vlr_subscr_find_by_imsi(vlr, imsi);
184 if (!vsub)
185 return;
186
187 switch (type) {
188 case SGSAP_ID_EPS_T_NETWORK_INITIATED:
189 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
190 break;
191 case SGSAP_ID_EPS_T_UE_INITIATED:
192 evt = SGS_UE_E_RX_DETACH_IND_FROM_UE;
193 break;
194 case SGSAP_ID_EPS_T_EPS_NOT_ALLOWED:
195 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
196 break;
197 default:
198 LOGP(DSGS, LOGL_ERROR, "(sub %s) invalid SGS IMSI detach type, detaching anyway...\n",
199 vlr_subscr_msisdn_or_name(vsub));
200 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
201 break;
202 }
203
204 osmo_fsm_inst_dispatch(vsub->sgs_fsm, evt, NULL);
Philipp Maier48264652019-04-02 16:58:59 +0200205
206 /* See also 3GPP TS 29.118, 5.4.3 Procedures in the VLR. Detaching from
207 * EPS services essentially means that the subscriber leaves the 4G RAN
208 * but continues to live on 2G, this basically turns the subscriber into
209 * a normal 2G subscriber and we need to make sure that the lu-
210 * expiration timer is running. */
211 if (vsub->expire_lu == VLR_SUBSCRIBER_NO_EXPIRATION)
212 vlr_subscr_enable_expire_lu(vsub);
213
Harald Welte0df904d2018-12-03 11:00:04 +0100214 vlr_subscr_put(vsub);
215}
216
217/*! Perform an SGs TMSI reallocation complete.
218 * \param[in] vsub VLR subscriber.
219 * \param[in] imsi mobile identity (IMSI). */
220void vlr_sgs_tmsi_reall_compl(struct vlr_instance *vlr, const char *imsi)
221{
222 struct vlr_subscr *vsub;
223 vsub = vlr_subscr_find_by_imsi(vlr, imsi);
224 if (!vsub)
225 return;
226
227 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_TMSI_REALLOC, NULL);
228 vlr_subscr_put(vsub);
229}
230
231/*! Notify that an SGs paging has been rejected by the MME.
232 * \param[in] vsub VLR subscriber.
233 * \param[in] imsi mobile identity (IMSI).
234 * \param[in] cause SGs cause code. */
235void vlr_sgs_pag_rej(struct vlr_instance *vlr, const char *imsi, enum sgsap_sgs_cause cause)
236{
237 struct vlr_subscr *vsub;
238 vsub = vlr_subscr_find_by_imsi(vlr, imsi);
239 if (!vsub)
240 return;
241
242 /* On the reception of a paging rej the VLR is supposed to stop Ts5,
243 also 3GPP TS 29.118, chapter 5.1.2.4 */
244 osmo_timer_del(&vsub->sgs.Ts5);
245 LOGP(DSGS, LOGL_DEBUG, "(sub %s) Paging via SGs interface rejected by MME, %s stopped, cause: %s!\n",
246 vlr_subscr_msisdn_or_name(vsub), vlr_sgs_state_timer_name(SGS_STATE_TS5), sgsap_sgs_cause_name(cause));
247
248 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_PAGING_FAILURE, &cause);
249 vlr_subscr_put(vsub);
250
251 /* Balance ref count increment from vlr_sgs_pag() */
252 vlr_subscr_put(vsub);
253}
254
255/*! Notify that an SGs paging has been accepted by the MME.
256 * \param[in] vsub VLR subscriber.
257 * \param[in] imsi mobile identity (IMSI). */
258void vlr_sgs_pag_ack(struct vlr_instance *vlr, const char *imsi)
259{
260 struct vlr_subscr *vsub;
261 vsub = vlr_subscr_find_by_imsi(vlr, imsi);
262 if (!vsub)
263 return;
264
265 /* Stop Ts5 and and consider the paging as successful */
266 osmo_timer_del(&vsub->sgs.Ts5);
267 vlr_subscr_put(vsub);
268
269 /* Balance ref count increment from vlr_sgs_pag() */
270 vlr_subscr_put(vsub);
271}
272
273/*! Notify that the UE has been marked as unreachable by the MME.
274 * \param[in] vsub VLR subscriber.
275 * \param[in] imsi mobile identity (IMSI).
276 * \param[in] cause SGs cause code. */
277void vlr_sgs_ue_unr(struct vlr_instance *vlr, const char *imsi, enum sgsap_sgs_cause cause)
278{
279 struct vlr_subscr *vsub;
280 vsub = vlr_subscr_find_by_imsi(vlr, imsi);
281 if (!vsub)
282 return;
283
284 /* On the reception of an UE unreachable the VLR is supposed to stop
285 * Ts5, also 3GPP TS 29.118, chapter 5.1.2.5 */
286 osmo_timer_del(&vsub->sgs.Ts5);
287 LOGP(DSGS, LOGL_DEBUG,
288 "(sub %s) Paging via SGs interface not possible, UE unreachable, %s stopped, cause: %s\n",
289 vlr_subscr_msisdn_or_name(vsub), vlr_sgs_state_timer_name(SGS_STATE_TS5), sgsap_sgs_cause_name(cause));
290
291 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_SGSAP_UE_UNREACHABLE, &cause);
292 vlr_subscr_put(vsub);
293}
294
295/* Callback function that is called when an SGs paging request times out */
296static void Ts5_timeout_cb(void *arg)
297{
298 struct vlr_subscr *vsub = arg;
299
300 /* 3GPP TS 29.118 does not specify a specif action that has to happen
301 * in case Ts5 times out. The timeout just indicates that the paging
302 * failed. Other actions may check the status of Ts5 to see if a paging
303 * is still ongoing or not. */
304
305 LOGP(DSGS, LOGL_ERROR, "(sub %s) Paging via SGs interface timed out (%s expired)!\n",
306 vlr_subscr_msisdn_or_name(vsub), vlr_sgs_state_timer_name(SGS_STATE_TS5));
307
308 /* Balance ref count increment from vlr_sgs_pag() */
309 vlr_subscr_put(vsub);
310
311 return;
312}
313
314/*! Notify that a paging message has been sent and a paging is now in progress.
315 * \param[in] vsub VLR subscriber. */
316void vlr_sgs_pag(struct vlr_subscr *vsub, enum sgsap_service_ind serv_ind)
317{
318
319 /* In cases where we have to respawn a paging after an intermitted LU,
320 * there may e a Ts5 still running. In those cases we have to remove
321 * the old timer first */
322 if (osmo_timer_pending(&vsub->sgs.Ts5))
323 osmo_timer_del(&vsub->sgs.Ts5);
324
325 /* Note: 3GPP TS 29.118, chapter 4.2.2 mentions paging in the FSM
326 * diagram, but paging never causes a state transition except when
327 * an explicit failure is indicated (MME actively rejects paging).
328 * Apparantly it is also possible that an LU happens while the paging
329 * is still ongoing and Ts5 is running. (chapter 5.1.2.3). This means
330 * that the paging procedure is intended to run in parallel to the
331 * SGs FSM and given that the benaviour around Ts5 must be implemented
332 * also separately, to emphasize this separation Ts5 is implemented
333 * here in and not in vlr_sgs_fsm.c. */
334 osmo_timer_setup(&vsub->sgs.Ts5, Ts5_timeout_cb, vsub);
335 osmo_timer_schedule(&vsub->sgs.Ts5, vsub->sgs.cfg.timer[SGS_STATE_TS5], 0);
336
337 /* Formally 3GPP TS 29.118 defines the sending of a paging request
338 * as an event, but as far as the VLR is concerned only Ts5 is
339 * started. */
340 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_TX_PAGING, NULL);
341
342 /* Memorize service type in for the case that the paging must be
343 * respawned after an LU */
344 vsub->sgs.paging_serv_ind = serv_ind;
345
346 /* Ensure that the reference count is increased by one while the
347 * paging is happening. We will balance this again in vlr_sgs_pag_rej()
348 * and vlr_sgs_pag_ack(); */
349 vlr_subscr_get(vsub);
350}
351
352/*! Check if the SGs interface is currently paging
353 * \param[in] vsub VLR subscriber. */
354bool vlr_sgs_pag_pend(struct vlr_subscr *vsub)
355{
356 return osmo_timer_pending(&vsub->sgs.Ts5);
357}