blob: 61db585b636e324802c9d0a1a5ea23ca07718e7b [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.
Pau Espin Pedrol67106702021-04-27 18:20:15 +020071 * \param[in] last_eutran_plnm_id Last E-UTRAN PLMN ID (can be NULL).
Harald Welte0df904d2018-12-03 11:00:04 +010072 * \returns 0 in case of success, -EINVAL in case of error. */
73int vlr_sgs_loc_update(struct vlr_instance *vlr, struct vlr_sgs_cfg *cfg,
74 vlr_sgs_lu_response_cb_t response_cb, vlr_sgs_lu_paging_cb_t paging_cb,
75 vlr_sgs_lu_mminfo_cb_t mminfo_cb, char *mme_name, enum vlr_lu_type type, const char *imsi,
Pau Espin Pedrol67106702021-04-27 18:20:15 +020076 struct osmo_location_area_id *new_lai, struct osmo_plmn_id *last_eutran_plmn)
Harald Welte0df904d2018-12-03 11:00:04 +010077{
78 struct vlr_subscr *vsub = NULL;
79
80 OSMO_ASSERT(response_cb);
81 OSMO_ASSERT(paging_cb);
82 OSMO_ASSERT(mminfo_cb);
83 OSMO_ASSERT(cfg);
84 OSMO_ASSERT(imsi);
85
Pau Espin Pedrol017ba1f2021-08-24 14:56:03 +020086 vsub = vlr_subscr_find_or_create_by_imsi(vlr, imsi, VSUB_USE_SGS_LU, NULL);
Harald Welte0df904d2018-12-03 11:00:04 +010087 if (!vsub) {
88 LOGP(DSGS, LOGL_ERROR, "VLR subscriber allocation failed\n");
89 return -EINVAL;
90 }
91
92 vsub->sgs.cfg = *cfg;
93 vsub->sgs.response_cb = response_cb;
94 vsub->sgs.paging_cb = paging_cb;
95 vsub->sgs.mminfo_cb = mminfo_cb;
96 vlr_subscr_set_imsi(vsub, imsi);
Pau Espin Pedrol67106702021-04-27 18:20:15 +020097 vlr_subscr_set_last_used_eutran_plmn_id(vsub, last_eutran_plmn);
Harald Welte0df904d2018-12-03 11:00:04 +010098 osmo_strlcpy(vsub->sgs.mme_name, mme_name, sizeof(vsub->sgs.mme_name));
99
100 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_LU_FROM_MME, NULL);
101
102 /* FIXME: Use the "type" type parameter (for what is it useful?) */
103
104 vsub->sgs.lai = *new_lai;
105 vsub->cgi.lai = *new_lai;
106 vsub->cs.lac = vsub->sgs.lai.lac;
107
Philipp Maier48264652019-04-02 16:58:59 +0200108 /* Subscribers that are created by the SGs location update will not
109 * expire automatically, however a 2G LU or an implicit IMSI detach
110 * from EPS services may change this. */
111 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
112
Harald Welte0df904d2018-12-03 11:00:04 +0100113 return 0;
114}
115
116/*! Notify that the SGs Location Update accept message has been sent to MME.
117 * \param[in] vsub VLR subscriber. */
118void vlr_sgs_loc_update_acc_sent(struct vlr_subscr *vsub)
119{
120 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_TX_LU_ACCEPT, NULL);
121
Pau Espin Pedrol017ba1f2021-08-24 14:56:03 +0200122 /* Balance vlr_subscr_find_or_create_by_imsi() in vlr_sgs_loc_update() */
123 vlr_subscr_put(vsub, VSUB_USE_SGS_LU);
124
Harald Welte0df904d2018-12-03 11:00:04 +0100125 /* FIXME: At this point we need to check the status of Ts5 and if
126 * it is still running this means the LU has interrupted the paging,
127 * and we need to start paging again. 3GPP TS 29.118,
128 * chapter 5.2.3.2 */
129}
130
131/*! Notify that the SGs Location Update reject message has been sent to MME.
132 * \param[in] vsub VLR subscriber. */
133void vlr_sgs_loc_update_rej_sent(struct vlr_subscr *vsub)
134{
135 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_TX_LU_REJECT, NULL);
Pau Espin Pedrol017ba1f2021-08-24 14:56:03 +0200136 /* Balance vlr_subscr_find_or_create_by_imsi() in vlr_sgs_loc_update() */
137 vlr_subscr_put(vsub, VSUB_USE_SGS_LU);
Harald Welte0df904d2018-12-03 11:00:04 +0100138}
139
140/*! Perform an SGs IMSI detach.
141 * \param[in] vsub VLR subscriber.
142 * \param[in] imsi mobile identity (IMSI).
143 * \param[in] type datach type. */
144void vlr_sgs_imsi_detach(struct vlr_instance *vlr, const char *imsi, enum sgsap_imsi_det_noneps_type type)
145{
146 struct vlr_subscr *vsub;
147 enum sgs_ue_fsm_event evt;
Philipp Maier0803d882019-04-02 16:07:13 +0200148
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100149 vsub = vlr_subscr_find_by_imsi(vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100150 if (!vsub)
151 return;
152
Philipp Maier0803d882019-04-02 16:07:13 +0200153 /* See also: 3GPP TS 29.118, 5.6.3 Procedures in the VLR: In case of
154 * an implicit detach, we are supposed to check if the state of the
155 * SGs-association, and only when it is not SGs-NULL, we may proceed. */
Pau Espin Pedrol25b3ffb2021-08-24 14:14:57 +0200156 if (vsub->sgs_fsm->state == SGS_UE_ST_NULL && type == SGSAP_ID_NONEPS_T_IMPLICIT_UE_EPS_NONEPS) {
157 vlr_subscr_put(vsub, __func__);
Philipp Maier0803d882019-04-02 16:07:13 +0200158 return;
Pau Espin Pedrol25b3ffb2021-08-24 14:14:57 +0200159 }
Philipp Maier0803d882019-04-02 16:07:13 +0200160
Harald Welte0df904d2018-12-03 11:00:04 +0100161 switch (type) {
162 case SGSAP_ID_NONEPS_T_EXPLICIT_UE_NONEPS:
163 evt = SGS_UE_E_RX_DETACH_IND_FROM_UE;
164 break;
165 case SGSAP_ID_NONEPS_T_COMBINED_UE_EPS_NONEPS:
166 case SGSAP_ID_NONEPS_T_IMPLICIT_UE_EPS_NONEPS:
167 /* FIXME: Is that right? */
168 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
169 break;
170 default:
171 LOGP(DSGS, LOGL_ERROR, "(sub %s) invalid SGS IMSI detach type, detaching anyway...\n",
172 vlr_subscr_msisdn_or_name(vsub));
173 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
174 break;
175 }
176
177 osmo_fsm_inst_dispatch(vsub->sgs_fsm, evt, NULL);
Philipp Maier0803d882019-04-02 16:07:13 +0200178
179 /* Detaching from non EPS services essentially means that the
180 * subscriber is detached from 2G. In any case the VLR will
181 * get rid of the subscriber. */
182 vlr_subscr_expire(vsub);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100183 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100184}
185
186/*! Perform an SGs EPS detach.
187 * \param[in] vsub VLR subscriber.
188 * \param[in] imsi mobile identity (IMSI).
189 * \param[in] type datach type. */
190void vlr_sgs_eps_detach(struct vlr_instance *vlr, const char *imsi, enum sgsap_imsi_det_eps_type type)
191{
192 struct vlr_subscr *vsub;
193 enum sgs_ue_fsm_event evt;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100194 vsub = vlr_subscr_find_by_imsi(vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100195 if (!vsub)
196 return;
197
198 switch (type) {
199 case SGSAP_ID_EPS_T_NETWORK_INITIATED:
200 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
201 break;
202 case SGSAP_ID_EPS_T_UE_INITIATED:
203 evt = SGS_UE_E_RX_DETACH_IND_FROM_UE;
204 break;
205 case SGSAP_ID_EPS_T_EPS_NOT_ALLOWED:
206 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
207 break;
208 default:
209 LOGP(DSGS, LOGL_ERROR, "(sub %s) invalid SGS IMSI detach type, detaching anyway...\n",
210 vlr_subscr_msisdn_or_name(vsub));
211 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
212 break;
213 }
214
215 osmo_fsm_inst_dispatch(vsub->sgs_fsm, evt, NULL);
Philipp Maier48264652019-04-02 16:58:59 +0200216
217 /* See also 3GPP TS 29.118, 5.4.3 Procedures in the VLR. Detaching from
218 * EPS services essentially means that the subscriber leaves the 4G RAN
219 * but continues to live on 2G, this basically turns the subscriber into
220 * a normal 2G subscriber and we need to make sure that the lu-
221 * expiration timer is running. */
222 if (vsub->expire_lu == VLR_SUBSCRIBER_NO_EXPIRATION)
223 vlr_subscr_enable_expire_lu(vsub);
224
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100225 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100226}
227
228/*! Perform an SGs TMSI reallocation complete.
229 * \param[in] vsub VLR subscriber.
230 * \param[in] imsi mobile identity (IMSI). */
231void vlr_sgs_tmsi_reall_compl(struct vlr_instance *vlr, const char *imsi)
232{
233 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100234 vsub = vlr_subscr_find_by_imsi(vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100235 if (!vsub)
236 return;
237
238 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_TMSI_REALLOC, NULL);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100239 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100240}
241
242/*! Notify that an SGs paging has been rejected by the MME.
243 * \param[in] vsub VLR subscriber.
244 * \param[in] imsi mobile identity (IMSI).
245 * \param[in] cause SGs cause code. */
246void vlr_sgs_pag_rej(struct vlr_instance *vlr, const char *imsi, enum sgsap_sgs_cause cause)
247{
248 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100249 vsub = vlr_subscr_find_by_imsi(vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100250 if (!vsub)
251 return;
252
253 /* On the reception of a paging rej the VLR is supposed to stop Ts5,
254 also 3GPP TS 29.118, chapter 5.1.2.4 */
255 osmo_timer_del(&vsub->sgs.Ts5);
256 LOGP(DSGS, LOGL_DEBUG, "(sub %s) Paging via SGs interface rejected by MME, %s stopped, cause: %s!\n",
257 vlr_subscr_msisdn_or_name(vsub), vlr_sgs_state_timer_name(SGS_STATE_TS5), sgsap_sgs_cause_name(cause));
258
259 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_PAGING_FAILURE, &cause);
Harald Welte0df904d2018-12-03 11:00:04 +0100260 /* Balance ref count increment from vlr_sgs_pag() */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100261 vlr_subscr_put(vsub, VSUB_USE_SGS_PAGING_REQ);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100262
263 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100264}
265
266/*! Notify that an SGs paging has been accepted by the MME.
267 * \param[in] vsub VLR subscriber.
268 * \param[in] imsi mobile identity (IMSI). */
269void vlr_sgs_pag_ack(struct vlr_instance *vlr, const char *imsi)
270{
271 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100272 vsub = vlr_subscr_find_by_imsi(vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100273 if (!vsub)
274 return;
275
276 /* Stop Ts5 and and consider the paging as successful */
277 osmo_timer_del(&vsub->sgs.Ts5);
Harald Welte0df904d2018-12-03 11:00:04 +0100278 /* Balance ref count increment from vlr_sgs_pag() */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100279 vlr_subscr_put(vsub, VSUB_USE_SGS_PAGING_REQ);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100280
281 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100282}
283
284/*! Notify that the UE has been marked as unreachable by the MME.
285 * \param[in] vsub VLR subscriber.
286 * \param[in] imsi mobile identity (IMSI).
287 * \param[in] cause SGs cause code. */
288void vlr_sgs_ue_unr(struct vlr_instance *vlr, const char *imsi, enum sgsap_sgs_cause cause)
289{
290 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100291 vsub = vlr_subscr_find_by_imsi(vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100292 if (!vsub)
293 return;
294
295 /* On the reception of an UE unreachable the VLR is supposed to stop
296 * Ts5, also 3GPP TS 29.118, chapter 5.1.2.5 */
297 osmo_timer_del(&vsub->sgs.Ts5);
298 LOGP(DSGS, LOGL_DEBUG,
299 "(sub %s) Paging via SGs interface not possible, UE unreachable, %s stopped, cause: %s\n",
300 vlr_subscr_msisdn_or_name(vsub), vlr_sgs_state_timer_name(SGS_STATE_TS5), sgsap_sgs_cause_name(cause));
301
302 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_SGSAP_UE_UNREACHABLE, &cause);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100303 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100304}
305
306/* Callback function that is called when an SGs paging request times out */
307static void Ts5_timeout_cb(void *arg)
308{
309 struct vlr_subscr *vsub = arg;
310
Martin Hauke3f07dac2019-11-14 17:49:08 +0100311 /* 3GPP TS 29.118 does not specify a specific action that has to happen
Harald Welte0df904d2018-12-03 11:00:04 +0100312 * in case Ts5 times out. The timeout just indicates that the paging
313 * failed. Other actions may check the status of Ts5 to see if a paging
314 * is still ongoing or not. */
315
316 LOGP(DSGS, LOGL_ERROR, "(sub %s) Paging via SGs interface timed out (%s expired)!\n",
317 vlr_subscr_msisdn_or_name(vsub), vlr_sgs_state_timer_name(SGS_STATE_TS5));
318
319 /* Balance ref count increment from vlr_sgs_pag() */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100320 vlr_subscr_put(vsub, VSUB_USE_SGS_PAGING_REQ);
Harald Welte0df904d2018-12-03 11:00:04 +0100321
322 return;
323}
324
325/*! Notify that a paging message has been sent and a paging is now in progress.
326 * \param[in] vsub VLR subscriber. */
327void vlr_sgs_pag(struct vlr_subscr *vsub, enum sgsap_service_ind serv_ind)
328{
329
330 /* In cases where we have to respawn a paging after an intermitted LU,
331 * there may e a Ts5 still running. In those cases we have to remove
332 * the old timer first */
333 if (osmo_timer_pending(&vsub->sgs.Ts5))
334 osmo_timer_del(&vsub->sgs.Ts5);
335
336 /* Note: 3GPP TS 29.118, chapter 4.2.2 mentions paging in the FSM
337 * diagram, but paging never causes a state transition except when
338 * an explicit failure is indicated (MME actively rejects paging).
Martin Hauke3f07dac2019-11-14 17:49:08 +0100339 * Apparently it is also possible that an LU happens while the paging
Harald Welte0df904d2018-12-03 11:00:04 +0100340 * is still ongoing and Ts5 is running. (chapter 5.1.2.3). This means
341 * that the paging procedure is intended to run in parallel to the
342 * SGs FSM and given that the benaviour around Ts5 must be implemented
343 * also separately, to emphasize this separation Ts5 is implemented
344 * here in and not in vlr_sgs_fsm.c. */
345 osmo_timer_setup(&vsub->sgs.Ts5, Ts5_timeout_cb, vsub);
346 osmo_timer_schedule(&vsub->sgs.Ts5, vsub->sgs.cfg.timer[SGS_STATE_TS5], 0);
347
348 /* Formally 3GPP TS 29.118 defines the sending of a paging request
349 * as an event, but as far as the VLR is concerned only Ts5 is
350 * started. */
351 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_TX_PAGING, NULL);
352
353 /* Memorize service type in for the case that the paging must be
354 * respawned after an LU */
355 vsub->sgs.paging_serv_ind = serv_ind;
356
357 /* Ensure that the reference count is increased by one while the
358 * paging is happening. We will balance this again in vlr_sgs_pag_rej()
359 * and vlr_sgs_pag_ack(); */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100360 vlr_subscr_get(vsub, VSUB_USE_SGS_PAGING_REQ);
Harald Welte0df904d2018-12-03 11:00:04 +0100361}
362
363/*! Check if the SGs interface is currently paging
364 * \param[in] vsub VLR subscriber. */
365bool vlr_sgs_pag_pend(struct vlr_subscr *vsub)
366{
367 return osmo_timer_pending(&vsub->sgs.Ts5);
368}