blob: 565988621e96aff72c35605eb913e452c97de9fc [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
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +010086 vsub = vlr_subscr_find_or_create_by_imsi(vlr, imsi, VSUB_USE_SGS, 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
122 /* FIXME: At this point we need to check the status of Ts5 and if
123 * it is still running this means the LU has interrupted the paging,
124 * and we need to start paging again. 3GPP TS 29.118,
125 * chapter 5.2.3.2 */
126}
127
128/*! Notify that the SGs Location Update reject message has been sent to MME.
129 * \param[in] vsub VLR subscriber. */
130void vlr_sgs_loc_update_rej_sent(struct vlr_subscr *vsub)
131{
132 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_TX_LU_REJECT, NULL);
133}
134
135/*! Perform an SGs IMSI detach.
136 * \param[in] vsub VLR subscriber.
137 * \param[in] imsi mobile identity (IMSI).
138 * \param[in] type datach type. */
139void vlr_sgs_imsi_detach(struct vlr_instance *vlr, const char *imsi, enum sgsap_imsi_det_noneps_type type)
140{
141 struct vlr_subscr *vsub;
142 enum sgs_ue_fsm_event evt;
Philipp Maier0803d882019-04-02 16:07:13 +0200143
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100144 vsub = vlr_subscr_find_by_imsi(vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100145 if (!vsub)
146 return;
147
Philipp Maier0803d882019-04-02 16:07:13 +0200148 /* See also: 3GPP TS 29.118, 5.6.3 Procedures in the VLR: In case of
149 * an implicit detach, we are supposed to check if the state of the
150 * SGs-association, and only when it is not SGs-NULL, we may proceed. */
151 if (vsub->sgs_fsm->state == SGS_UE_ST_NULL && type == SGSAP_ID_NONEPS_T_IMPLICIT_UE_EPS_NONEPS)
152 return;
153
Harald Welte0df904d2018-12-03 11:00:04 +0100154 switch (type) {
155 case SGSAP_ID_NONEPS_T_EXPLICIT_UE_NONEPS:
156 evt = SGS_UE_E_RX_DETACH_IND_FROM_UE;
157 break;
158 case SGSAP_ID_NONEPS_T_COMBINED_UE_EPS_NONEPS:
159 case SGSAP_ID_NONEPS_T_IMPLICIT_UE_EPS_NONEPS:
160 /* FIXME: Is that right? */
161 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
162 break;
163 default:
164 LOGP(DSGS, LOGL_ERROR, "(sub %s) invalid SGS IMSI detach type, detaching anyway...\n",
165 vlr_subscr_msisdn_or_name(vsub));
166 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
167 break;
168 }
169
170 osmo_fsm_inst_dispatch(vsub->sgs_fsm, evt, NULL);
Philipp Maier0803d882019-04-02 16:07:13 +0200171
172 /* Detaching from non EPS services essentially means that the
173 * subscriber is detached from 2G. In any case the VLR will
174 * get rid of the subscriber. */
175 vlr_subscr_expire(vsub);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100176 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100177}
178
179/*! Perform an SGs EPS detach.
180 * \param[in] vsub VLR subscriber.
181 * \param[in] imsi mobile identity (IMSI).
182 * \param[in] type datach type. */
183void vlr_sgs_eps_detach(struct vlr_instance *vlr, const char *imsi, enum sgsap_imsi_det_eps_type type)
184{
185 struct vlr_subscr *vsub;
186 enum sgs_ue_fsm_event evt;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100187 vsub = vlr_subscr_find_by_imsi(vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100188 if (!vsub)
189 return;
190
191 switch (type) {
192 case SGSAP_ID_EPS_T_NETWORK_INITIATED:
193 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
194 break;
195 case SGSAP_ID_EPS_T_UE_INITIATED:
196 evt = SGS_UE_E_RX_DETACH_IND_FROM_UE;
197 break;
198 case SGSAP_ID_EPS_T_EPS_NOT_ALLOWED:
199 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
200 break;
201 default:
202 LOGP(DSGS, LOGL_ERROR, "(sub %s) invalid SGS IMSI detach type, detaching anyway...\n",
203 vlr_subscr_msisdn_or_name(vsub));
204 evt = SGS_UE_E_RX_DETACH_IND_FROM_MME;
205 break;
206 }
207
208 osmo_fsm_inst_dispatch(vsub->sgs_fsm, evt, NULL);
Philipp Maier48264652019-04-02 16:58:59 +0200209
210 /* See also 3GPP TS 29.118, 5.4.3 Procedures in the VLR. Detaching from
211 * EPS services essentially means that the subscriber leaves the 4G RAN
212 * but continues to live on 2G, this basically turns the subscriber into
213 * a normal 2G subscriber and we need to make sure that the lu-
214 * expiration timer is running. */
215 if (vsub->expire_lu == VLR_SUBSCRIBER_NO_EXPIRATION)
216 vlr_subscr_enable_expire_lu(vsub);
217
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100218 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100219}
220
221/*! Perform an SGs TMSI reallocation complete.
222 * \param[in] vsub VLR subscriber.
223 * \param[in] imsi mobile identity (IMSI). */
224void vlr_sgs_tmsi_reall_compl(struct vlr_instance *vlr, const char *imsi)
225{
226 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100227 vsub = vlr_subscr_find_by_imsi(vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100228 if (!vsub)
229 return;
230
231 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_TMSI_REALLOC, NULL);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100232 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100233}
234
235/*! Notify that an SGs paging has been rejected by the MME.
236 * \param[in] vsub VLR subscriber.
237 * \param[in] imsi mobile identity (IMSI).
238 * \param[in] cause SGs cause code. */
239void vlr_sgs_pag_rej(struct vlr_instance *vlr, const char *imsi, enum sgsap_sgs_cause cause)
240{
241 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100242 vsub = vlr_subscr_find_by_imsi(vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100243 if (!vsub)
244 return;
245
246 /* On the reception of a paging rej the VLR is supposed to stop Ts5,
247 also 3GPP TS 29.118, chapter 5.1.2.4 */
248 osmo_timer_del(&vsub->sgs.Ts5);
249 LOGP(DSGS, LOGL_DEBUG, "(sub %s) Paging via SGs interface rejected by MME, %s stopped, cause: %s!\n",
250 vlr_subscr_msisdn_or_name(vsub), vlr_sgs_state_timer_name(SGS_STATE_TS5), sgsap_sgs_cause_name(cause));
251
252 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_PAGING_FAILURE, &cause);
Harald Welte0df904d2018-12-03 11:00:04 +0100253 /* Balance ref count increment from vlr_sgs_pag() */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100254 vlr_subscr_put(vsub, VSUB_USE_SGS_PAGING_REQ);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100255
256 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100257}
258
259/*! Notify that an SGs paging has been accepted by the MME.
260 * \param[in] vsub VLR subscriber.
261 * \param[in] imsi mobile identity (IMSI). */
262void vlr_sgs_pag_ack(struct vlr_instance *vlr, const char *imsi)
263{
264 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100265 vsub = vlr_subscr_find_by_imsi(vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100266 if (!vsub)
267 return;
268
269 /* Stop Ts5 and and consider the paging as successful */
270 osmo_timer_del(&vsub->sgs.Ts5);
Harald Welte0df904d2018-12-03 11:00:04 +0100271 /* Balance ref count increment from vlr_sgs_pag() */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100272 vlr_subscr_put(vsub, VSUB_USE_SGS_PAGING_REQ);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100273
274 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100275}
276
277/*! Notify that the UE has been marked as unreachable by the MME.
278 * \param[in] vsub VLR subscriber.
279 * \param[in] imsi mobile identity (IMSI).
280 * \param[in] cause SGs cause code. */
281void vlr_sgs_ue_unr(struct vlr_instance *vlr, const char *imsi, enum sgsap_sgs_cause cause)
282{
283 struct vlr_subscr *vsub;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100284 vsub = vlr_subscr_find_by_imsi(vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100285 if (!vsub)
286 return;
287
288 /* On the reception of an UE unreachable the VLR is supposed to stop
289 * Ts5, also 3GPP TS 29.118, chapter 5.1.2.5 */
290 osmo_timer_del(&vsub->sgs.Ts5);
291 LOGP(DSGS, LOGL_DEBUG,
292 "(sub %s) Paging via SGs interface not possible, UE unreachable, %s stopped, cause: %s\n",
293 vlr_subscr_msisdn_or_name(vsub), vlr_sgs_state_timer_name(SGS_STATE_TS5), sgsap_sgs_cause_name(cause));
294
295 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_RX_SGSAP_UE_UNREACHABLE, &cause);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100296 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100297}
298
299/* Callback function that is called when an SGs paging request times out */
300static void Ts5_timeout_cb(void *arg)
301{
302 struct vlr_subscr *vsub = arg;
303
Martin Hauke3f07dac2019-11-14 17:49:08 +0100304 /* 3GPP TS 29.118 does not specify a specific action that has to happen
Harald Welte0df904d2018-12-03 11:00:04 +0100305 * in case Ts5 times out. The timeout just indicates that the paging
306 * failed. Other actions may check the status of Ts5 to see if a paging
307 * is still ongoing or not. */
308
309 LOGP(DSGS, LOGL_ERROR, "(sub %s) Paging via SGs interface timed out (%s expired)!\n",
310 vlr_subscr_msisdn_or_name(vsub), vlr_sgs_state_timer_name(SGS_STATE_TS5));
311
312 /* Balance ref count increment from vlr_sgs_pag() */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100313 vlr_subscr_put(vsub, VSUB_USE_SGS_PAGING_REQ);
Harald Welte0df904d2018-12-03 11:00:04 +0100314
315 return;
316}
317
318/*! Notify that a paging message has been sent and a paging is now in progress.
319 * \param[in] vsub VLR subscriber. */
320void vlr_sgs_pag(struct vlr_subscr *vsub, enum sgsap_service_ind serv_ind)
321{
322
323 /* In cases where we have to respawn a paging after an intermitted LU,
324 * there may e a Ts5 still running. In those cases we have to remove
325 * the old timer first */
326 if (osmo_timer_pending(&vsub->sgs.Ts5))
327 osmo_timer_del(&vsub->sgs.Ts5);
328
329 /* Note: 3GPP TS 29.118, chapter 4.2.2 mentions paging in the FSM
330 * diagram, but paging never causes a state transition except when
331 * an explicit failure is indicated (MME actively rejects paging).
Martin Hauke3f07dac2019-11-14 17:49:08 +0100332 * Apparently it is also possible that an LU happens while the paging
Harald Welte0df904d2018-12-03 11:00:04 +0100333 * is still ongoing and Ts5 is running. (chapter 5.1.2.3). This means
334 * that the paging procedure is intended to run in parallel to the
335 * SGs FSM and given that the benaviour around Ts5 must be implemented
336 * also separately, to emphasize this separation Ts5 is implemented
337 * here in and not in vlr_sgs_fsm.c. */
338 osmo_timer_setup(&vsub->sgs.Ts5, Ts5_timeout_cb, vsub);
339 osmo_timer_schedule(&vsub->sgs.Ts5, vsub->sgs.cfg.timer[SGS_STATE_TS5], 0);
340
341 /* Formally 3GPP TS 29.118 defines the sending of a paging request
342 * as an event, but as far as the VLR is concerned only Ts5 is
343 * started. */
344 osmo_fsm_inst_dispatch(vsub->sgs_fsm, SGS_UE_E_TX_PAGING, NULL);
345
346 /* Memorize service type in for the case that the paging must be
347 * respawned after an LU */
348 vsub->sgs.paging_serv_ind = serv_ind;
349
350 /* Ensure that the reference count is increased by one while the
351 * paging is happening. We will balance this again in vlr_sgs_pag_rej()
352 * and vlr_sgs_pag_ack(); */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100353 vlr_subscr_get(vsub, VSUB_USE_SGS_PAGING_REQ);
Harald Welte0df904d2018-12-03 11:00:04 +0100354}
355
356/*! Check if the SGs interface is currently paging
357 * \param[in] vsub VLR subscriber. */
358bool vlr_sgs_pag_pend(struct vlr_subscr *vsub)
359{
360 return osmo_timer_pending(&vsub->sgs.Ts5);
361}