blob: 0147d3f4f33837b6f02a666670ab76aad841e1c0 [file] [log] [blame]
Harald Welte0df904d2018-12-03 11:00:04 +01001/* SGs Interface according to 3GPP TS 23.272 + TS 29.118 */
2
3/* (C) 2018-2019 by sysmocom s.f.m.c. GmbH
4 * All Rights Reserved
5 *
6 * Author: Harald Welte, Philipp Maier
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23#include <osmocom/core/utils.h>
24#include <osmocom/core/msgb.h>
25#include <osmocom/core/fsm.h>
26#include <osmocom/core/socket.h>
27#include <osmocom/core/select.h>
28
29#include <osmocom/gsm/tlv.h>
30#include <osmocom/gsm/gsm48.h>
31#include <osmocom/gsm/gsm23003.h>
32#include <osmocom/gsm/gsm29118.h>
33
34#include <osmocom/netif/stream.h>
35
36#include <osmocom/msc/vlr.h>
37#include <osmocom/msc/vlr_sgs.h>
38#include <osmocom/msc/gsm_data.h>
Harald Welte0df904d2018-12-03 11:00:04 +010039#include <osmocom/msc/gsm_04_08.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010040#include <osmocom/msc/msub.h>
41#include <osmocom/msc/msc_a.h>
42#include <osmocom/msc/msc_i.h>
Harald Welte0df904d2018-12-03 11:00:04 +010043
44#include <osmocom/msc/debug.h>
45#include <osmocom/msc/sgs_iface.h>
46#include <osmocom/msc/sgs_server.h>
Harald Welte0df904d2018-12-03 11:00:04 +010047#include <osmocom/gsm/protocol/gsm_29_118.h>
48
49#include <osmocom/gsm/apn.h>
50
51#define S(x) (1 << (x))
52
53/* A pointer to the GSM network we work with. By the current paradigm,
54 * there can only be one gsm_network per MSC. The pointer is set once
Vadim Yanitskiy118a0b82019-03-21 20:51:55 +070055 * when calling sgs_iface_init() */
Harald Welte0df904d2018-12-03 11:00:04 +010056static struct gsm_network *gsm_network = NULL;
57
58static struct osmo_fsm sgs_vlr_reset_fsm;
59static void sgs_tx(struct sgs_connection *sgc, struct msgb *msg);
60
61struct sgs_state *g_sgs;
62
63/***********************************************************************
64 * SGs state per MME connection
65 ***********************************************************************/
66
67#define LOGSGC(sgc, lvl, fmt, args...) \
68 LOGP(DSGS, lvl, "%s: " fmt, sgc->sockname, ## args)
69
70#define LOGSGC_VSUB(sgc, sub_info, lvl, fmt, args...) \
71 LOGP(DSGS, lvl, "(sub %s) %s: " fmt, sub_info, sgc->sockname, ## args)
72
73#define LOGMME(mme, lvl, fmt, args...) \
74 LOGP(DSGS, lvl, "%s: " fmt, mme->fqdn ? mme->fqdn : mme->conn->sockname, ## args)
75
76enum sgs_vlr_reset_fsm_state {
77 SGS_VLRR_ST_NULL,
78 SGS_VLRR_ST_WAIT_ACK,
79 SGS_VLRR_ST_COMPLETE,
80};
81
82enum sgs_vlr_reset_fsm_event {
83 SGS_VLRR_E_START_RESET,
84 SGS_VLRR_E_RX_RESET_ACK,
85};
86
87/***********************************************************************
88 * SGs utility functions
89 ***********************************************************************/
90
91/* Allocate a new subscriber connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010092static struct msc_a *subscr_conn_allocate_sgs(struct sgs_connection *sgc, struct vlr_subscr *vsub, bool mt)
Harald Welte0df904d2018-12-03 11:00:04 +010093{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010094 struct msub *msub;
95 struct msc_a *msc_a;
Harald Welte0df904d2018-12-03 11:00:04 +010096
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010097 msub = msub_alloc(gsm_network);
98 msc_a = msc_a_alloc(msub,
99 &msc_ran_infra[OSMO_RAT_EUTRAN_SGS]);
100 msc_a->complete_layer3_type = mt ? COMPLETE_LAYER3_PAGING_RESP : COMPLETE_LAYER3_CM_SERVICE_REQ;
101 msub_set_vsub(msub, vsub);
Harald Welte0df904d2018-12-03 11:00:04 +0100102
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100103 if (mt)
104 msc_a_get(msc_a, MSC_A_USE_PAGING_RESPONSE);
Harald Welte0df904d2018-12-03 11:00:04 +0100105
106 /* Accept the connection immediately, since the UE is already
107 * authenticated by the MME no authentication is required. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100108 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_COMPLETE_LAYER_3_OK, NULL);
109 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_AUTHENTICATED, NULL);
Harald Welte0df904d2018-12-03 11:00:04 +0100110
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100111 return msc_a;
Harald Welte0df904d2018-12-03 11:00:04 +0100112}
113
114/* Check if there are connections associated with a given subscriber. If yes,
115 * make sure that those connections are tossed. */
116static void subscr_conn_toss(struct vlr_subscr *vsub)
117{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100118 struct msub *msub;
Harald Welte0df904d2018-12-03 11:00:04 +0100119
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100120 msub = msub_for_vsub(vsub);
121 if (!msub)
Harald Welte0df904d2018-12-03 11:00:04 +0100122 return;
123
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100124 LOG_MSUB(msub, LOGL_ERROR, "Force releasing previous subscriber connection: an SGs connection for this"
125 " subscriber is being initiated\n");
Harald Welte0df904d2018-12-03 11:00:04 +0100126
Vadim Yanitskiyd851b2c2024-02-14 18:04:51 +0700127 msc_a_release_mo(msub_msc_a(msub), GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100128 /* TODO: is this strong enough? After this, it should be completely disassociated with this subscriber. */
Harald Welte0df904d2018-12-03 11:00:04 +0100129}
130
131struct sgs_mme_ctx *sgs_mme_by_fqdn(struct sgs_state *sgs, const char *mme_fqdn)
132{
133 struct sgs_mme_ctx *mme;
134
135 llist_for_each_entry(mme, &sgs->mme_list, entry) {
136 if (!strcasecmp(mme_fqdn, mme->fqdn))
137 return mme;
138 }
139 return NULL;
140}
141
142static struct sgs_mme_ctx *sgs_mme_alloc(struct sgs_state *sgs, const char *mme_fqdn, const struct osmo_gummei *gummei)
143{
144 struct sgs_mme_ctx *mme;
145
146 OSMO_ASSERT(sgs_mme_by_fqdn(sgs, mme_fqdn) == NULL);
147
148 mme = talloc_zero(sgs, struct sgs_mme_ctx);
149 if (!mme)
150 return NULL;
151 mme->sgs = sgs;
152 OSMO_STRLCPY_ARRAY(mme->fqdn, mme_fqdn);
153 mme->fi = osmo_fsm_inst_alloc(&sgs_vlr_reset_fsm, mme, mme, LOGL_INFO, osmo_gummei_name(gummei));
154 if (!mme->fi) {
155 talloc_free(mme);
156 return NULL;
157 }
158 llist_add_tail(&mme->entry, &sgs->mme_list);
159 return mme;
160}
161
162/* Decode and verify MME name */
163static int decode_mme_name(char *mme_name, const struct tlv_parsed *tp)
164{
165 const uint8_t *mme_name_enc = TLVP_VAL_MINLEN(tp, SGSAP_IE_MME_NAME, SGS_MME_NAME_LEN);
166 struct osmo_gummei gummei;
167
168 if (!mme_name_enc)
169 return -EINVAL;
170
Omar Ramadane2bd9eb2019-04-12 09:03:39 -0700171 /* some implementations use FDQN format violating TS 29.118 9.3.14 */
172 if (!osmo_parse_mme_domain(&gummei, (const char *) mme_name_enc)) {
173 memcpy(mme_name, mme_name_enc, TLVP_LEN(tp, SGSAP_IE_MME_NAME));
174 return 0;
175 }
176
Harald Welte0df904d2018-12-03 11:00:04 +0100177 /* decode the MME name from DNS labels to string */
178 osmo_apn_to_str(mme_name, TLVP_VAL(tp, SGSAP_IE_MME_NAME), TLVP_LEN(tp, SGSAP_IE_MME_NAME));
179
180 /* try to parse the MME name into a GUMMEI as a test for the format */
181 if (osmo_parse_mme_domain(&gummei, mme_name) < 0)
182 return -EINVAL;
183
184 return 0;
185}
186
187/* A MME FQDN was received (e.g. RESET-IND/RESET-ACK/LU-REQ) */
188static int sgs_mme_fqdn_received(struct sgs_connection *sgc, const char *mme_fqdn)
189{
190 struct sgs_mme_ctx *mme;
191 struct osmo_gummei gummei;
192
193 /* caller must pass in a valid FQDN string syntax */
194 OSMO_ASSERT(osmo_parse_mme_domain(&gummei, mme_fqdn) == 0);
195
196 if (!sgc->mme) {
197 /* attempt to find MME with given name */
198 mme = sgs_mme_by_fqdn(sgc->sgs, mme_fqdn);
199 if (!mme)
200 mme = sgs_mme_alloc(sgc->sgs, mme_fqdn, &gummei);
201 OSMO_ASSERT(mme);
202
203 if (mme->conn) {
204 /* The MME context has another connection !?! */
205 LOGSGC(sgc, LOGL_ERROR, "Rx MME name %s, but that MME already has other "
206 "SCTP connection?!?\n", mme_fqdn);
207 return -1;
208 } else {
209 /* associate the two */
210 mme->conn = sgc;
211 sgc->mme = mme;
212 }
213 } else {
214 mme = sgc->mme;
215 if (strcasecmp(mme->fqdn, mme_fqdn) != 0) {
216 LOGMME(mme, LOGL_ERROR, "Rx MME name \"%s\" in packet from MME \"%s\" ?!?\n", mme_fqdn,
217 mme->fqdn);
218 return -2;
219 }
220 }
221 return 0;
222}
223
224/* Safely get the mme-name for an sgs-connection */
225static char *sgs_mme_fqdn_get(struct sgs_connection *sgc)
226{
227 if (!sgc)
228 return NULL;
229 if (!sgc->mme)
230 return NULL;
231 if (sgc->mme->fqdn[0] == '\0')
232 return NULL;
233 return sgc->mme->fqdn;
234}
235
236/* Find an sgs_mme_ctx for a given vlr subscriber, also check result */
237struct sgs_mme_ctx *sgs_mme_ctx_by_vsub(struct vlr_subscr *vsub, uint8_t msg_type)
238{
239 struct sgs_mme_ctx *mme;
240
241 /* Find SGS connection by MME name */
242 mme = sgs_mme_by_fqdn(g_sgs, vsub->sgs.mme_name);
243 if (!mme) {
244 LOGP(DSGS, LOGL_ERROR, "(sub %s) Tx %s cannot find suitable MME!\n",
245 vlr_subscr_name(vsub), sgsap_msg_type_name(msg_type));
246 return NULL;
247 }
248 if (!mme->conn) {
249 LOGP(DSGS, LOGL_ERROR,
250 "(sub %s) Tx %s suitable MME found, but no SGS connection present!\n",
251 vlr_subscr_name(vsub), sgsap_msg_type_name(msg_type));
252 return NULL;
253 }
254 if (!mme->sgs) {
255 LOGP(DSGS, LOGL_ERROR,
256 "(sub %s) Tx %s suitable MME found, but no SGS state present!\n",
257 vlr_subscr_name(vsub), sgsap_msg_type_name(msg_type));
258 return NULL;
259 }
260
261 return mme;
262}
263
264/* Make sure that the subscriber is known and that the subscriber is in the
265 * SGs associated state. In case of failure the function returns false and
266 * automatically sends a release message to the MME */
267static bool check_sgs_association(struct sgs_connection *sgc, struct msgb *msg, char *imsi)
268{
269 struct vlr_subscr *vsub;
270 struct msgb *resp;
271 uint8_t msg_type = msg->data[0];
272
273 /* Subscriber must be known by the VLR */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100274 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100275 if (!vsub) {
276 LOGSGC(sgc, LOGL_NOTICE, "SGsAP Message %s with unknown IMSI (%s), releasing\n",
277 sgsap_msg_type_name(msg_type), imsi);
278 resp = gsm29118_create_release_req(imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN);
279 sgs_tx(sgc, resp);
280 return false;
281 }
282
283 /* The SGs FSM must also be in SGs associated state */
284 if (vsub->sgs_fsm->state != SGS_UE_ST_ASSOCIATED) {
285 LOGSGC(sgc, LOGL_NOTICE, "(sub %s) SGsAP Message %s subscriber not SGs-associated, releasing\n",
286 vlr_subscr_name(vsub), sgsap_msg_type_name(msg_type));
287 resp = gsm29118_create_release_req(vsub->imsi, SGSAP_SGS_CAUSE_IMSI_DET_EPS_NONEPS);
288 sgs_tx(sgc, resp);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100289 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100290 return false;
291 }
292
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100293 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100294 return true;
295}
296
297/***********************************************************************
298 * SGsAP transmit functions
299 ***********************************************************************/
300
301/* Send message out to remote end (final step) */
302static void sgs_tx(struct sgs_connection *sgc, struct msgb *msg)
303{
304 if (!msg) {
305 LOGSGC(sgc, LOGL_NOTICE, "Null message, cannot transmit!\n");
306 return;
307 }
308
309 msgb_sctp_ppid(msg) = 0;
310 if (!sgc) {
Philipp Maier7231edb2019-04-04 10:30:28 +0200311 LOGP(LOGL_NOTICE, DSGS, "Cannot transmit %s: connection dead. Discarding\n",
312 sgsap_msg_type_name(msg->data[0]));
Harald Welte0df904d2018-12-03 11:00:04 +0100313 msgb_free(msg);
314 return;
315 }
316 osmo_stream_srv_send(sgc->srv, msg);
317}
318
319/* Get some subscriber info from ISMI (for the log text) */
320const char *subscr_info(const char *imsi)
321{
322 const char *subscr_string = "<unknown>";
323 struct vlr_subscr *vsub;
324
325 if (imsi) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100326 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100327 if (!vsub)
328 subscr_string = imsi;
329 else {
330 subscr_string = vlr_subscr_name(vsub);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100331 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100332 }
333 }
334
335 return subscr_string;
336}
337
338/* Comfortable status message generator that also generates some basic
Martin Hauke3f07dac2019-11-14 17:49:08 +0100339 * context-dependent log output */
Harald Welte0df904d2018-12-03 11:00:04 +0100340static int sgs_tx_status(struct sgs_connection *sgc, const char *imsi, enum sgsap_sgs_cause cause, struct msgb *msg,
341 int sgsap_iei)
342{
343 struct msgb *resp;
344
345 if (sgsap_iei < 0) {
346 LOGSGC_VSUB(sgc, subscr_info(imsi), LOGL_ERROR, "Rx %s failed with cause %s!\n",
347 sgsap_msg_type_name(msg->data[0]), sgsap_sgs_cause_name(cause));
348 } else if (cause == SGSAP_SGS_CAUSE_MISSING_MAND_IE) {
349 LOGSGC_VSUB(sgc, subscr_info(imsi), LOGL_ERROR, "Rx %s with missing mandatory %s IEI!\n",
350 sgsap_msg_type_name(msg->data[0]), sgsap_iei_name(sgsap_iei));
351 } else if (cause == SGSAP_SGS_CAUSE_INVALID_MAND_IE) {
352 LOGSGC_VSUB(sgc, subscr_info(imsi), LOGL_ERROR, "Rx %s with invalid mandatory %s IEI!\n",
353 sgsap_msg_type_name(msg->data[0]), sgsap_iei_name(sgsap_iei));
354 } else if (cause == SGSAP_SGS_CAUSE_COND_IE_ERROR) {
Martin Hauke3f07dac2019-11-14 17:49:08 +0100355 LOGSGC_VSUB(sgc, subscr_info(imsi), LOGL_ERROR, "Rx %s with erroneous conditional %s IEI!\n",
Harald Welte0df904d2018-12-03 11:00:04 +0100356 sgsap_msg_type_name(msg->data[0]), sgsap_iei_name(sgsap_iei));
357 } else {
358 LOGSGC_VSUB(sgc, subscr_info(imsi), LOGL_ERROR, "Rx %s failed with cause %s at %s IEI!\n",
359 sgsap_msg_type_name(msg->data[0]), sgsap_sgs_cause_name(cause), sgsap_iei_name(sgsap_iei));
360 }
361
362 resp = gsm29118_create_status(imsi, cause, msg);
363 sgs_tx(sgc, resp);
364 return 0;
365}
366
Pau Espin Pedrol25b68472021-08-24 11:34:05 +0200367/* Called by VLR via callback, transmits the location update response or
Harald Welte0df904d2018-12-03 11:00:04 +0100368 * reject, depending on the outcome of the location update. */
369static void sgs_tx_loc_upd_resp_cb(struct sgs_lu_response *response)
370{
371 struct msgb *resp;
372 struct vlr_subscr *vsub = response->vsub;
373 struct sgs_mme_ctx *mme;
374 uint8_t new_id[2 + GSM48_TMSI_LEN];
Neels Hofmeyr5b36d132020-07-01 16:11:09 +0200375 uint8_t *new_id_ptr = NULL;
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200376 int new_id_len = 0;
Harald Welte0df904d2018-12-03 11:00:04 +0100377 uint8_t resp_msg_type;
378
Philipp Maier483cea82019-04-03 16:23:29 +0200379 /* Determine message type that is sent next (needed for logging) */
Harald Welte0df904d2018-12-03 11:00:04 +0100380 if (response->accepted)
381 resp_msg_type = SGSAP_MSGT_LOC_UPD_ACK;
Philipp Maier483cea82019-04-03 16:23:29 +0200382 else if (response->error)
383 resp_msg_type = SGSAP_MSGT_RESET_IND;
Harald Welte0df904d2018-12-03 11:00:04 +0100384 else
385 resp_msg_type = SGSAP_MSGT_LOC_UPD_REJ;
386
Philipp Maier483cea82019-04-03 16:23:29 +0200387 /* Determine MME */
Harald Welte0df904d2018-12-03 11:00:04 +0100388 mme = sgs_mme_ctx_by_vsub(vsub, resp_msg_type);
389 if (!mme)
390 return;
391
Philipp Maier483cea82019-04-03 16:23:29 +0200392 /* Handle error (HLR failure) */
393 if (response->error) {
394 osmo_fsm_inst_dispatch(mme->fi, SGS_VLRR_E_START_RESET, NULL);
395 return;
396 }
397
398 /* Handle LU accept/reject */
Harald Welte0df904d2018-12-03 11:00:04 +0100399 if (response->accepted) {
400 if (vsub->tmsi_new != GSM_RESERVED_TMSI) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200401 struct osmo_mobile_identity tmsi_mi = {
402 .type = GSM_MI_TYPE_TMSI,
403 .tmsi = vsub->tmsi_new,
404 };
405 new_id_len = osmo_mobile_identity_encode_buf(new_id, sizeof(new_id), &tmsi_mi, false);
Neels Hofmeyr5b36d132020-07-01 16:11:09 +0200406 if (new_id_len > 0) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200407 new_id_ptr = new_id;
Neels Hofmeyr5b36d132020-07-01 16:11:09 +0200408 } else {
409 /* Failure to encode the TMSI is not actually possible here, this is just for paranoia
410 * and coverity scan. */
411 new_id_len = 0;
412 LOGPFSMSL(vsub->sgs_fsm, DMM, LOGL_ERROR, "Cannot encode TMSI Mobile Identity\n");
413 }
Harald Welte0df904d2018-12-03 11:00:04 +0100414 }
415 resp = gsm29118_create_lu_ack(vsub->imsi, &vsub->sgs.lai, new_id_ptr, new_id_len);
416 sgs_tx(mme->conn, resp);
417 vlr_sgs_loc_update_acc_sent(vsub);
418 } else {
419 resp = gsm29118_create_lu_rej(vsub->imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, &vsub->sgs.lai);
420 sgs_tx(mme->conn, resp);
421 vlr_sgs_loc_update_rej_sent(vsub);
422 }
423}
424
425/* Called by VLR via callback, transmits MM information to the UE */
426static void sgs_tx_mm_info_cb(struct vlr_subscr *vsub)
427{
428 struct msgb *msg;
429 struct msgb *msg_mm_info;
430 struct sgs_mme_ctx *mme;
431
432 /* The sending of MM information requests is an optional feature and
433 * depends on the network configuration (VTY) */
434 if (!gsm_network->send_mm_info)
435 return;
436
437 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_MM_INFO_REQ);
438 if (!mme)
439 return;
440
441 /* Create and send MM information request message, see also:
442 * 3GPP TS 29.118, chapter 8.12 SGsAP-MM-INFORMATION-REQUEST and
443 * 3GPP TS 29.018, chapter 18.4.16 MM information. */
444 msg_mm_info = gsm48_create_mm_info(gsm_network);
445 msg = gsm29118_create_mm_info_req(vsub->imsi, msg_mm_info->data + 2, msg_mm_info->len - 2);
446 sgs_tx(mme->conn, msg);
447 msgb_free(msg_mm_info);
448}
449
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100450enum sgsap_service_ind sgs_serv_ind_from_paging_cause(enum paging_cause cause)
451{
452 switch (cause) {
453 case PAGING_CAUSE_CALL_CONVERSATIONAL:
454 case PAGING_CAUSE_CALL_STREAMING:
455 case PAGING_CAUSE_CALL_INTERACTIVE:
456 case PAGING_CAUSE_CALL_BACKGROUND:
457 return SGSAP_SERV_IND_CS_CALL;
458
459 case PAGING_CAUSE_UNSPECIFIED:
460 case PAGING_CAUSE_SIGNALLING_LOW_PRIO:
461 case PAGING_CAUSE_SIGNALLING_HIGH_PRIO:
462 return SGSAP_SERV_IND_SMS;
463
464 default:
465 OSMO_ASSERT(false);
466 }
467}
468
Alexander Couzens28e43ff2024-09-11 23:49:33 +0200469/*! Page UE through SGs interface or inform about an expired paging
Harald Welte0df904d2018-12-03 11:00:04 +0100470 * \param[in] vsub subscriber context
471 * \param[in] serv_ind service indicator (sms or voide)
472 * \returns 0 in case of success, -EINVAL in case of error. */
Alexander Couzens28e43ff2024-09-11 23:49:33 +0200473int sgs_iface_paging_cb(struct vlr_subscr *vsub, enum sgsap_service_ind serv_ind)
Harald Welte0df904d2018-12-03 11:00:04 +0100474{
475 struct msgb *resp;
476 struct gsm29118_paging_req paging_params;
477 struct sgs_mme_ctx *mme;
478
Alexander Couzens28e43ff2024-09-11 23:49:33 +0200479 if (serv_ind == SGSAP_SERV_IND_PAGING_TIMEOUT) {
480 paging_expired(vsub);
481 return 0;
482 }
483
Harald Welte0df904d2018-12-03 11:00:04 +0100484 /* See also: 3GPP TS 29.118, chapter 5.1.2.2 Paging Initiation */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100485 if (vsub->sgs_fsm->state == SGS_UE_ST_NULL && vsub->conf_by_radio_contact_ind == true) {
486 LOGPFSMSL(vsub->sgs_fsm, DPAG, LOGL_ERROR, "Will not Page (conf_by_radio_contact_ind == true)\n");
Harald Welte0df904d2018-12-03 11:00:04 +0100487 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100488 }
Harald Welte0df904d2018-12-03 11:00:04 +0100489
490 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_PAGING_REQ);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100491 if (!mme) {
492 LOGPFSMSL(vsub->sgs_fsm, DPAG, LOGL_ERROR, "Will not Page (no MME)\n");
Harald Welte0df904d2018-12-03 11:00:04 +0100493 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100494 }
Harald Welte0df904d2018-12-03 11:00:04 +0100495
496 /* Check if there is still a paging in progress for this subscriber,
497 * if yes, don't initiate another paging request. */
498 if (vlr_sgs_pag_pend(vsub))
499 return 0;
500
Pau Espin Pedrol42ac3662021-07-12 13:32:07 +0200501 LOGMME(mme, LOGL_INFO, "Paging on SGs: %s for %s (conf_by_radio_contact_ind=%d)\n",
502 vlr_subscr_name(vsub), sgsap_service_ind_name(serv_ind), vsub->conf_by_radio_contact_ind);
503
Harald Welte0df904d2018-12-03 11:00:04 +0100504 memset(&paging_params, 0, sizeof(paging_params));
505 osmo_strlcpy(paging_params.imsi, vsub->imsi, sizeof(paging_params.imsi));
506 osmo_strlcpy(paging_params.vlr_name, mme->sgs->cfg.vlr_name, sizeof(paging_params.vlr_name));
507 paging_params.serv_ind = serv_ind;
508 if (vsub->conf_by_radio_contact_ind == true) {
509 memcpy(&paging_params.lai, &vsub->sgs.lai, sizeof(paging_params.lai));
510 paging_params.lai_present = true;
511 }
512 resp = gsm29118_create_paging_req(&paging_params);
513 sgs_tx(mme->conn, resp);
514
515 /* FIXME: If we are in SGS_UE_ST_NULL while sub->conf_by_radio_contact_ind == false,
516 * we are supposed to start a search procedure as defined in 3GPP TS 23.018 */
517
518 /* Inform the VLR that a paging via SGs is in progress */
519 vlr_sgs_pag(vsub, serv_ind);
520
521 /* Return a page count of 1 (success) */
522 return 1;
523}
524
525/***********************************************************************
526 * SGs incoming messages from the MME
527 ***********************************************************************/
528
529/* Safely read out the SGs cause code from a given message/tlv set, send status
530 * message in case the cause code is invalid or missing. */
531static int sgs_cause_from_msg(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp,
532 const char *imsi)
533{
534 enum sgsap_sgs_cause cause;
535 const uint8_t *cause_ptr;
536 cause_ptr = TLVP_VAL_MINLEN(tp, SGSAP_IE_SGS_CAUSE, 1);
537 if (!cause_ptr) {
538 sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_SGS_CAUSE);
539 return -1;
540 } else
541 cause = *cause_ptr;
542 return cause;
543}
544
545/* SGsAP-STATUS 3GPP TS 29.118, chapter 8.18 */
546static int sgs_rx_status(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, const char *imsi)
547{
548 int cause;
549 const uint8_t *err_msg;
550 const char *imsi_ptr;
551 char *err_msg_hex = "(none)";
552
553 cause = sgs_cause_from_msg(sgc, msg, tp, NULL);
554 if (cause < 0)
555 return 0;
556
557 if (imsi[0] != '\0')
558 imsi_ptr = imsi;
559 else
560 imsi_ptr = "<none>";
561
562 if (TLVP_PRESENT(tp, SGSAP_IE_ERR_MSG))
563 err_msg = TLVP_VAL(tp, SGSAP_IE_ERR_MSG);
564 else
565 err_msg = NULL;
566
567 if (err_msg)
568 err_msg_hex = osmo_hexdump(err_msg, TLVP_LEN(tp, SGSAP_IE_ERR_MSG));
569
570 LOGSGC(sgc, LOGL_NOTICE, "Rx STATUS cause=%s, IMSI=%s, err_msg=%s\n",
571 sgsap_sgs_cause_name(cause), imsi_ptr, err_msg_hex);
572
573 return 0;
574}
575
576/* SGsAP-RESET-INDICATION 3GPP TS 29.118, chapter 8.16 */
577static int sgs_rx_reset_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp)
578{
579 struct gsm29118_reset_msg reset_params;
580 struct msgb *resp;
581
582 memset(&reset_params, 0, sizeof(reset_params));
583 osmo_strlcpy(reset_params.vlr_name, sgc->sgs->cfg.vlr_name, sizeof(reset_params.vlr_name));
584 reset_params.vlr_name_present = true;
585
586 resp = gsm29118_create_reset_ack(&reset_params);
587
588 /* Perform a reset of the SGS FSM of all subscribers that are present in the VLR */
589 vlr_sgs_reset(gsm_network->vlr);
590
591 sgs_tx(sgc, resp);
592 return 0;
593}
594
595/* SGsAP-RESET-ACK 3GPP TS 29.118, chapter 8.15 */
596static int sgs_rx_reset_ack(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp)
597{
598 /* dispatch event to VLR reset FSM for this MME */
599 if (sgc->mme && sgc->mme->fi)
600 osmo_fsm_inst_dispatch(sgc->mme->fi, SGS_VLRR_E_RX_RESET_ACK, msg);
601 return 0;
602}
603
604/* SGsAP-LOCATION-UPDATE-REQUEST 3GPP TS 29.118, chapter 8.11 */
605static int sgs_rx_loc_upd_req(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
606{
607 struct msgb *resp;
608 const uint8_t *lu_type_ie;
609 enum vlr_lu_type type;
610 struct osmo_location_area_id new_lai;
611 const struct gsm48_loc_area_id *gsm48_lai;
612 int rc;
613 char *mme_name;
614 struct vlr_sgs_cfg vlr_sgs_cfg;
615 struct vlr_subscr *vsub;
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200616 struct osmo_plmn_id last_eutran_plmn_buf, *last_eutran_plmn = NULL;
Harald Welte0df904d2018-12-03 11:00:04 +0100617
618 /* Check for lingering connections */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100619 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100620 if (vsub) {
621 subscr_conn_toss(vsub);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100622 vlr_subscr_put(vsub, __func__);
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200623 vsub = NULL;
Harald Welte0df904d2018-12-03 11:00:04 +0100624 }
625
626 /* Determine MME-Name */
627 mme_name = sgs_mme_fqdn_get(sgc);
628 if (!mme_name) {
629 resp = gsm29118_create_lu_rej(imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, NULL);
630 sgs_tx(sgc, resp);
631 return 0;
632 }
633
634 /* Parse LU-Type */
635 lu_type_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_EPS_LU_TYPE, 1);
636 if (!lu_type_ie)
637 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_EPS_LU_TYPE);
638 if (lu_type_ie[0] == 0x01)
639 type = VLR_LU_TYPE_IMSI_ATTACH;
640 else
641 type = VLR_LU_TYPE_REGULAR;
642
643 /* Parse LAI of the new location */
644 gsm48_lai = (struct gsm48_loc_area_id *)TLVP_VAL_MINLEN(tp, SGSAP_IE_LAI, 5);
645 if (!gsm48_lai)
646 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_LAI);
647 gsm48_decode_lai2(gsm48_lai, &new_lai);
648
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200649 /* 3GPP TS 23.272 sec 4.3.3 (CSFB):
650 * "During the SGs location update procedure, obtaining the last used LTE PLMN ID via TAI"
651 */
652 if (TLVP_PRES_LEN(tp, SGSAP_IE_TAI, 3)) {
653 last_eutran_plmn = &last_eutran_plmn_buf;
654 osmo_plmn_from_bcd(TLVP_VAL(tp, SGSAP_IE_TAI), last_eutran_plmn);
655 /* TODO: we could also gather the TAC from here, but we don't need it yet */
656 } else if (TLVP_PRES_LEN(tp, SGSAP_IE_EUTRAN_CGI, 3)) {
657 /* Since TAI is optional, let's try harder getting Last Used
658 * E-UTRAN PLMN ID by fetching it from E-UTRAN CGI */
659 last_eutran_plmn = &last_eutran_plmn_buf;
660 osmo_plmn_from_bcd(TLVP_VAL(tp, SGSAP_IE_EUTRAN_CGI), last_eutran_plmn);
661 /* TODO: we could also gather the ECI from here, but we don't need it yet */
662 } else {
663 LOGSGC(sgc, LOGL_INFO, "Receiving SGsAP-LOCATION-UPDATE-REQUEST without TAI nor "
664 "E-CGI IEs, fast fallback GERAN->EUTRAN won't be possible!\n");
665 }
666
Harald Welte0df904d2018-12-03 11:00:04 +0100667 /* Perform actual location update */
668 memcpy(vlr_sgs_cfg.timer, sgc->sgs->cfg.timer, sizeof(vlr_sgs_cfg.timer));
669 memcpy(vlr_sgs_cfg.counter, sgc->sgs->cfg.counter, sizeof(vlr_sgs_cfg.counter));
Alexander Couzens28e43ff2024-09-11 23:49:33 +0200670 rc = vlr_sgs_loc_update(gsm_network->vlr, &vlr_sgs_cfg, sgs_tx_loc_upd_resp_cb, sgs_iface_paging_cb,
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200671 sgs_tx_mm_info_cb, mme_name, type, imsi, &new_lai, last_eutran_plmn);
Harald Welte0df904d2018-12-03 11:00:04 +0100672 if (rc != 0) {
673 resp = gsm29118_create_lu_rej(imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, NULL);
674 sgs_tx(sgc, resp);
675 }
676
677 return 0;
678}
679
680/* SGsAP-IMSI-DETACH-INDICATION 3GPP TS 29.118, chapter 8.8 */
681static int sgs_rx_imsi_det_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
682{
683 struct msgb *resp;
684 enum sgsap_imsi_det_noneps_type type;
685 const uint8_t *type_ie;
686
687 type_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_IMSI_DET_NONEPS_TYPE, 1);
688 if (!type_ie)
689 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_IMSI_DET_NONEPS_TYPE);
690
691 switch (type_ie[0]) {
692 case SGSAP_ID_NONEPS_T_EXPLICIT_UE_NONEPS:
693 type = SGSAP_ID_NONEPS_T_EXPLICIT_UE_NONEPS;
694 break;
695 case SGSAP_ID_NONEPS_T_COMBINED_UE_EPS_NONEPS:
696 type = SGSAP_ID_NONEPS_T_COMBINED_UE_EPS_NONEPS;
697 break;
698 case SGSAP_ID_NONEPS_T_IMPLICIT_UE_EPS_NONEPS:
699 type = SGSAP_ID_NONEPS_T_IMPLICIT_UE_EPS_NONEPS;
700 break;
701 default:
702 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_INVALID_MAND_IE, msg, SGSAP_IE_IMSI_DET_NONEPS_TYPE);
703 break;
704 }
705
706 vlr_sgs_imsi_detach(gsm_network->vlr, imsi, type);
707 resp = gsm29118_create_imsi_det_ack(imsi);
708 sgs_tx(sgc, resp);
709
710 return 0;
711}
712
713/* SGsAP-EPS-DETACH-INDICATION 3GPP TS 29.118, chapter 8.6 */
714static int sgs_rx_eps_det_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
715{
716 struct msgb *resp;
717 enum sgsap_imsi_det_eps_type type;
718 const uint8_t *type_ie;
719
720 type_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_IMSI_DET_EPS_TYPE, 1);
721 if (!type_ie)
722 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_IMSI_DET_EPS_TYPE);
723
724 switch (type_ie[0]) {
725 case SGSAP_ID_EPS_T_NETWORK_INITIATED:
726 type = SGSAP_ID_EPS_T_NETWORK_INITIATED;
727 break;
728 case SGSAP_ID_EPS_T_UE_INITIATED:
729 type = SGSAP_ID_EPS_T_UE_INITIATED;
730 break;
731 case SGSAP_ID_EPS_T_EPS_NOT_ALLOWED:
732 type = SGSAP_ID_EPS_T_EPS_NOT_ALLOWED;
733 break;
734 default:
735 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_INVALID_MAND_IE, msg, SGSAP_IE_IMSI_DET_EPS_TYPE);
736 break;
737 }
738
739 vlr_sgs_eps_detach(gsm_network->vlr, imsi, type);
740 resp = gsm29118_create_eps_det_ack(imsi);
741 sgs_tx(sgc, resp);
742
743 return 0;
744}
745
746/* SGsAP-PAGING-REJECT 3GPP TS 29.118, chapter 8.13 */
747static int sgs_rx_pag_rej(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
748{
749 int cause;
750 struct vlr_subscr *vsub;
751
752 cause = sgs_cause_from_msg(sgc, msg, tp, NULL);
753 if (cause < 0)
754 return 0;
755
756 /* Subscriber must be known by the VLR */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100757 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100758 if (!vsub)
759 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, msg, SGSAP_IE_IMSI);
760
761 /* Inform the VLR */
762 vlr_sgs_pag_rej(gsm_network->vlr, imsi, cause);
763
764 /* Stop all paging activity */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100765 paging_expired(vsub);
Harald Welte0df904d2018-12-03 11:00:04 +0100766
767 /* Depending on the cause code some action is required */
768 if (cause == SGSAP_SGS_CAUSE_MT_CSFB_REJ_USER) {
769 /* FIXME: We are supposed to trigger a User Determined User Busy (UDUB)
770 * as specified in 3GPP TS 24.082 here, SGs association state shall not
771 * be changed */
772 LOGSGC(sgc, LOGL_ERROR,
773 "Rx %s with SGSAP_SGS_CAUSE_MT_CSFB_REJ_USER, but sending UDUP is not implemented yet!\n",
774 sgsap_msg_type_name(msg->data[0]));
775 } else if (cause == SGSAP_SGS_CAUSE_IMSI_DET_EPS) {
776 /* FIXME: In this case we should send the paging via A/Iu interface */
777 OSMO_ASSERT(false);
778 }
779
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100780 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100781 return 0;
782}
783
784/* SGsAP-UE-UNREACHABLE 3GPP TS 29.118, chapter 8.21 */
785static int sgs_rx_ue_unr(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
786{
787 int cause;
788
789 cause = sgs_cause_from_msg(sgc, msg, tp, NULL);
790 if (cause < 0)
791 return 0;
792
793 vlr_sgs_ue_unr(gsm_network->vlr, imsi, cause);
794
795 return 0;
796}
797
798/* SGsAP-TMSI-REALLOCATION-COMPLETE 3GPP TS 29.118, chapter 8.19 */
799static int sgs_rx_tmsi_reall_cmpl(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
800{
801 vlr_sgs_tmsi_reall_compl(gsm_network->vlr, imsi);
802 return 0;
803}
804
805/* SGsAP-SERVICE-REQUEST 3GPP TS 29.118, chapter 8.17 */
806static int sgs_rx_service_req(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
807{
808 enum sgsap_service_ind serv_ind;
809 const uint8_t *serv_ind_ie;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100810 struct msc_a *msc_a;
Harald Welte0df904d2018-12-03 11:00:04 +0100811 struct vlr_subscr *vsub;
812
813 /* Note: While in other RAN concepts a service request is used to
814 * initiate mobile originated operation, the service request in SGsAP
815 * is comparable to a paging response. The SGsAP SERVICE REQUEST must
816 * not be confused or compared with a CM SERVICE REQUEST! */
817
818 if (!check_sgs_association(sgc, msg, imsi))
819 return 0;
820
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100821 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100822 /* Note: vsub is already sufficiently verified by check_sgs_association(),
823 * we must have a vsub at this point! */
824 OSMO_ASSERT(vsub);
825
826 /* The Service request is intended as a paging response, if one is
827 * received while nothing is paging something is very wrong! */
828 if (!vlr_sgs_pag_pend(vsub)) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100829 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100830 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE, msg, -1);
831 }
832 serv_ind_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_SERVICE_INDICATOR, 1);
833
834 if (!serv_ind_ie) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100835 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100836 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_SERVICE_INDICATOR);
837 }
838 if (serv_ind_ie[0] == SGSAP_SERV_IND_CS_CALL)
839 serv_ind = serv_ind_ie[0];
840 else if (serv_ind_ie[0] == SGSAP_SERV_IND_SMS)
841 serv_ind = serv_ind_ie[0];
842 else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100843 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100844 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_INVALID_MAND_IE, msg, SGSAP_IE_SERVICE_INDICATOR);
845 }
846
847 /* FIXME: The MME shall include an UE EMM Mode IE, but the field is
848 * marked optional. (Why do we need this info at all?) */
849
850 /* Report to the VLR that the paging has successfully completed */
851 vlr_sgs_pag_ack(gsm_network->vlr, imsi);
852
853 /* Exit early when the service indicator indicates that a call is being
854 * established. In those cases we do not allocate a connection, instead
855 * the connection will be allocated when the MS is appearing on the
856 * A-Interface. */
857 if (serv_ind == SGSAP_SERV_IND_CS_CALL) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100858 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100859 return 0;
860 }
861
862 /* Allocate subscriber connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100863 msc_a = subscr_conn_allocate_sgs(sgc, vsub, true);
864 if (!msc_a) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100865 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100866 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE, msg, -1);
867 }
868
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100869 /* The msub has added a get() for the vsub, balance above vlr_subscr_find_by_imsi() */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100870 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100871 return 0;
872}
873
874/* SGsAP-UPLINK-UNITDATA 3GPP TS 29.118, chapter 8.22 */
875static int sgs_rx_ul_ud(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
876{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100877 struct msc_a *msc_a;
Harald Welte0df904d2018-12-03 11:00:04 +0100878 const uint8_t *nas_msg_container_ie;
879 struct vlr_subscr *vsub;
880
881 if (!check_sgs_association(sgc, msg, imsi))
882 return 0;
883
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100884 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100885 /* Note: vsub is already sufficiently verified by check_sgs_association(),
886 * we must have a vsub at this point! */
887 OSMO_ASSERT(vsub);
888
889 /* Try to find existing connection (MT) or allocate a new one (MO) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100890 msc_a = msc_a_for_vsub(vsub, true);
891 if (!msc_a)
892 msc_a = subscr_conn_allocate_sgs(sgc, vsub, false);
Harald Welte0df904d2018-12-03 11:00:04 +0100893
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100894 /* Balance above vlr_subscr_find_by_imsi() */
895 vlr_subscr_put(vsub, __func__);
896
Harald Welte0df904d2018-12-03 11:00:04 +0100897 /* If we do not find an existing connection and allocating a new one
Martin Hauke3f07dac2019-11-14 17:49:08 +0100898 * failed, give up and return status. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100899 if (!msc_a)
Harald Welte0df904d2018-12-03 11:00:04 +0100900 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE, msg, 0);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100901
902 if (msc_a->c.ran->type != OSMO_RAT_EUTRAN_SGS) {
903 LOGSGC(sgc, LOGL_ERROR,
904 "Receiving uplink unit-data for non-sgs connection -- discarding message!\n");
905 return -EINVAL;
Harald Welte0df904d2018-12-03 11:00:04 +0100906 }
907
908 nas_msg_container_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_NAS_MSG_CONTAINER, 1);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100909 if (!nas_msg_container_ie)
Harald Welte0df904d2018-12-03 11:00:04 +0100910 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_NAS_MSG_CONTAINER);
Harald Welte0df904d2018-12-03 11:00:04 +0100911
912 /* ran_conn_dtap expects the dtap payload in l3h */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100913 msg->l3h = (uint8_t *)nas_msg_container_ie;
914 msc_a_up_l3(msc_a, msg);
Harald Welte0df904d2018-12-03 11:00:04 +0100915
Harald Welte0df904d2018-12-03 11:00:04 +0100916 return 0;
917}
918
919/* SGsAP-MO-CSFB-INDICATION, chapter 8.25 */
920static int sgs_rx_csfb_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
921{
922 struct vlr_subscr *vsub;
Pau Espin Pedrol82529592021-07-01 17:59:49 +0200923 struct osmo_plmn_id last_eutran_plmn_buf;
924 const struct osmo_plmn_id *last_eutran_plmn = &last_eutran_plmn_buf;
Harald Welte0df904d2018-12-03 11:00:04 +0100925
Philipp Maier25e7ba52019-08-15 14:51:49 +0200926 /* The MME informs us with this message that the UE has initiated a
927 * service request for MO CS fallback. There is not much we can do with
928 * this information, however, we can check if the subscriber actually
929 * exists in the VLR and if there are any lingering connections open.*/
Harald Welte0df904d2018-12-03 11:00:04 +0100930
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100931 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100932 if (!vsub)
933 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, msg, SGSAP_IE_IMSI);
934
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200935 /* 3GPP TS 23.272 sec 4.3.3 (CSFB):
936 * "During the SGs location update procedure, obtaining the last used LTE PLMN ID via TAI"
937 */
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200938 if (TLVP_PRES_LEN(tp, SGSAP_IE_TAI, 3)) {
Pau Espin Pedrol82529592021-07-01 17:59:49 +0200939 osmo_plmn_from_bcd(TLVP_VAL(tp, SGSAP_IE_TAI), &last_eutran_plmn_buf);
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200940 /* TODO: we could also gather the TAC from here, but we don't need it yet */
941 } else if (TLVP_PRES_LEN(tp, SGSAP_IE_EUTRAN_CGI, 3)) {
942 /* Since TAI is optional, let's try harder getting Last Used
943 * E-UTRAN PLMN ID by fetching it from E-UTRAN CGI */
Pau Espin Pedrol82529592021-07-01 17:59:49 +0200944 osmo_plmn_from_bcd(TLVP_VAL(tp, SGSAP_IE_EUTRAN_CGI), &last_eutran_plmn_buf);
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200945 /* TODO: we could also gather the ECI from here, but we don't need it yet */
Pau Espin Pedrol82529592021-07-01 17:59:49 +0200946 } else {
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200947 LOGSGC(sgc, LOGL_INFO, "Receiving SGsAP-MO-CSFB-INDICATION without TAI nor "
948 "E-CGI IEs, and they are not known from previous SGsAP-LOCATION-UPDATE-REQUEST. "
949 "Fast fallback GERAN->EUTRAN won't be possible!\n");
Pau Espin Pedrol82529592021-07-01 17:59:49 +0200950 last_eutran_plmn = NULL;
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200951 }
952
Pau Espin Pedrol82529592021-07-01 17:59:49 +0200953 vlr_subscr_set_last_used_eutran_plmn_id(vsub, last_eutran_plmn);
954
Harald Welte0df904d2018-12-03 11:00:04 +0100955 /* Check for lingering connections */
956 subscr_conn_toss(vsub);
957
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100958 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100959 return 0;
960}
961
962/* SGsAP-UE-ACTIVITY-INDICATION, chapter 8.20 */
963static int sgs_rx_ue_act_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
964{
965 /* In this MSC/VLR implementation we do not support the alerting
966 * procedure yet and therefore we will never request any alerting
967 * at the MME. Given that it is unlikely that we ever get activity
968 * indications from the MME, but if we do we should not act all too
969 * hostile and ignore the indication silently. */
970
971 LOGSGC(sgc, LOGL_ERROR, "Rx %s unexpected, we do not implement alerting yet, ignoring!\n",
972 sgsap_msg_type_name(msg->data[0]));
973
974 return 0;
975}
976
977#define TX_STATUS_AND_LOG(sgc, msg_type, cause, fmt) \
978 LOGSGC(sgc, LOGL_ERROR, fmt, sgsap_msg_type_name(msg_type)); \
979 resp = gsm29118_create_status(NULL, cause, msg); \
980 sgs_tx(sgc, resp); \
981
982/*! Process incoming SGs message (see sgs_server.c)
983 * \param[in] sgc related sgs connection
984 * \param[in] msg received message
985 * \returns 0 in case of success, -EINVAL in case of error. */
986int sgs_iface_rx(struct sgs_connection *sgc, struct msgb *msg)
987{
988 struct msgb *resp;
989 uint8_t msg_type = msg->l2h[0];
990 struct tlv_parsed tp;
991 int rc;
992 char imsi[GSM48_MI_SIZE];
993 char mme_name[SGS_MME_NAME_LEN + 1];
994
995 memset(imsi, 0, sizeof(imsi));
996 memset(mme_name, 0, sizeof(mme_name));
997
998 /* When the receiving entity receives a message that is too short to contain a complete
999 * message type information element, the receiving entity shall ignore that message. */
1000 if (msgb_l2len(msg) < 1)
1001 goto error;
1002
1003 /* Parse TLV elements */
1004 rc = tlv_parse(&tp, &sgsap_ie_tlvdef, msgb_l2(msg) + 1, msgb_l2len(msg) - 1, 0, 0);
Philipp Maier6dcdfb02019-09-24 11:44:28 +02001005 if (rc < 0)
1006 LOGSGC(sgc, LOGL_NOTICE, "SGsAP Message %s contains unknown TLV IEs\n", sgsap_msg_type_name(msg_type));
Harald Welte0df904d2018-12-03 11:00:04 +01001007
1008 /* Most of the messages contain an IMSI as mandatory IE, parse it right here */
1009 if (!TLVP_PRESENT(&tp, SGSAP_IE_IMSI) &&
1010 msg_type != SGSAP_MSGT_STATUS && msg_type != SGSAP_MSGT_RESET_IND && msg_type != SGSAP_MSGT_RESET_ACK) {
1011 /* reject the message; all but the three above have mandatory IMSI */
1012 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_MISSING_MAND_IE,
1013 "SGsAP Message %s without IMSI, dropping\n");
1014 goto error;
1015 }
1016
1017 if (TLVP_PRESENT(&tp, SGSAP_IE_IMSI)) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001018 struct osmo_mobile_identity mi;
1019 if (osmo_mobile_identity_decode(&mi,
1020 TLVP_VAL(&tp, SGSAP_IE_IMSI),
1021 TLVP_LEN(&tp, SGSAP_IE_IMSI), false)
1022 || mi.type != GSM_MI_TYPE_IMSI) {
Harald Welte0df904d2018-12-03 11:00:04 +01001023 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_INVALID_MAND_IE,
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001024 "SGsAP Message %s with invalid IMSI, dropping\n");
Harald Welte0df904d2018-12-03 11:00:04 +01001025 goto error;
1026 }
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001027 OSMO_STRLCPY_ARRAY(imsi, mi.imsi);
Harald Welte0df904d2018-12-03 11:00:04 +01001028 }
1029
Pau Espin Pedrol25b68472021-08-24 11:34:05 +02001030 /* Some messages contain an MME-NAME as mandatory IE, parse it right here. The
1031 * MME-NAME is also immediately registered with the sgc, so it will be implicitly
Harald Welte0df904d2018-12-03 11:00:04 +01001032 * known to all functions that have access to the sgc context. */
1033 if (!TLVP_PRESENT(&tp, SGSAP_IE_MME_NAME)
1034 && (msg_type == SGSAP_MSGT_RESET_IND || msg_type == SGSAP_MSGT_RESET_ACK
1035 || msg_type == SGSAP_MSGT_LOC_UPD_REQ || msg_type == SGSAP_MSGT_IMSI_DET_IND
1036 || msg_type == SGSAP_MSGT_EPS_DET_IND)) {
1037 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_MISSING_MAND_IE,
1038 "SGsAP Message %s without MME-Name, dropping\n");
1039 goto error;
1040 }
1041
1042 if (TLVP_PRESENT(&tp, SGSAP_IE_MME_NAME)) {
1043 if (decode_mme_name(mme_name, &tp) != 0) {
1044 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_INVALID_MAND_IE,
1045 "SGsAP Message %s with invalid MME-Name, dropping\n");
1046 goto error;
1047 }
1048 /* Regsister/check mme_name with sgc */
1049 if (sgs_mme_fqdn_received(sgc, mme_name) < 0) {
1050 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE,
1051 "SGsAP Message %s with invalid MME-Name, dropping\n");
1052 goto error;
1053 }
1054 }
1055
1056 /* dispatch msg to various handler functions. msgb ownership remains here! */
1057 rc = -EINVAL;
1058 switch (msg_type) {
1059 case SGSAP_MSGT_STATUS:
1060 rc = sgs_rx_status(sgc, msg, &tp, imsi);
1061 break;
1062 case SGSAP_MSGT_RESET_IND:
1063 rc = sgs_rx_reset_ind(sgc, msg, &tp);
1064 break;
1065 case SGSAP_MSGT_RESET_ACK:
1066 rc = sgs_rx_reset_ack(sgc, msg, &tp);
1067 break;
1068 case SGSAP_MSGT_LOC_UPD_REQ:
1069 rc = sgs_rx_loc_upd_req(sgc, msg, &tp, imsi);
1070 break;
1071 case SGSAP_MSGT_IMSI_DET_IND:
1072 rc = sgs_rx_imsi_det_ind(sgc, msg, &tp, imsi);
1073 break;
1074 case SGSAP_MSGT_EPS_DET_IND:
1075 rc = sgs_rx_eps_det_ind(sgc, msg, &tp, imsi);
1076 break;
1077 case SGSAP_MSGT_PAGING_REJ:
1078 rc = sgs_rx_pag_rej(sgc, msg, &tp, imsi);
1079 break;
1080 case SGSAP_MSGT_UE_UNREACHABLE:
1081 rc = sgs_rx_ue_unr(sgc, msg, &tp, imsi);
1082 break;
1083 case SGSAP_MSGT_TMSI_REALL_CMPL:
1084 rc = sgs_rx_tmsi_reall_cmpl(sgc, msg, &tp, imsi);
1085 break;
1086 case SGSAP_MSGT_SERVICE_REQ:
1087 rc = sgs_rx_service_req(sgc, msg, &tp, imsi);
1088 break;
1089 case SGSAP_MSGT_UL_UD:
1090 rc = sgs_rx_ul_ud(sgc, msg, &tp, imsi);
1091 break;
1092 case SGSAP_MSGT_MO_CSFB_IND:
1093 rc = sgs_rx_csfb_ind(sgc, msg, &tp, imsi);
1094 break;
1095 case SGSAP_MSGT_UE_ACT_IND:
1096 rc = sgs_rx_ue_act_ind(sgc, msg, &tp, imsi);
1097 break;
1098 case SGSAP_MSGT_ALERT_ACK:
1099 case SGSAP_MSGT_ALERT_REJ:
1100 LOGSGC(sgc, LOGL_ERROR, "Rx unmplemented SGsAP %s: %s\n",
1101 sgsap_msg_type_name(msg_type), msgb_hexdump(msg));
1102 resp = gsm29118_create_status(imsi, SGSAP_SGS_CAUSE_MSG_UNKNOWN, msg);
1103 sgs_tx(sgc, resp);
1104 rc = 0;
1105 break;
1106 default:
1107 LOGSGC(sgc, LOGL_ERROR, "Rx unknown SGsAP message type 0x%02x: %s\n", msg_type, msgb_hexdump(msg));
1108 resp = gsm29118_create_status(imsi, SGSAP_SGS_CAUSE_MSG_UNKNOWN, msg);
1109 sgs_tx(sgc, resp);
1110 rc = 0;
1111 break;
1112 }
1113
1114 /* Catch unhandled errors */
1115 if (rc < 0) {
1116 /* Note: Usually the sgs_rx_ should catch errors locally and
1117 * eimit a status message with proper cause code, including
1118 * a suitable log message. If we end up here, something is
1119 * not right and should be fixed */
1120 LOGSGC(sgc, LOGL_ERROR, "Rx unable to decode SGsAP %s: %s\n",
1121 sgsap_msg_type_name(msg_type), msgb_hexdump(msg));
1122 resp = gsm29118_create_status(imsi, SGSAP_SGS_CAUSE_MSG_UNKNOWN, msg);
1123 sgs_tx(sgc, resp);
1124 }
1125
1126error:
1127 msgb_free(msg);
1128 return 0;
1129}
1130
1131/***********************************************************************
1132 * SGs connection "VLR Reset Procedure" FSM
1133 ***********************************************************************/
1134
1135static const struct value_string sgs_vlr_reset_fsm_event_names[] = {
1136 {SGS_VLRR_E_START_RESET, "START-RESET"},
1137 {SGS_VLRR_E_RX_RESET_ACK, "RX-RESET-ACK"},
1138 {0, NULL}
1139};
1140
1141static void sgs_vlr_reset_fsm_null(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1142{
1143 switch (event) {
1144 case SGS_VLRR_E_RX_RESET_ACK:
1145 break;
1146 default:
1147 OSMO_ASSERT(0);
1148 break;
1149 }
1150}
1151
1152static void sgs_vlr_reset_fsm_wait_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1153{
1154 switch (event) {
1155 case SGS_VLRR_E_RX_RESET_ACK:
1156 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_COMPLETE, 0, 0);
1157 break;
1158 default:
1159 OSMO_ASSERT(0);
1160 break;
1161 }
1162}
1163
1164static void sgs_vlr_reset_fsm_complete(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1165{
1166 switch (event) {
1167 case SGS_VLRR_E_RX_RESET_ACK:
1168 break;
1169 default:
1170 OSMO_ASSERT(0);
1171 break;
1172 }
1173}
1174
1175static void sgs_vlr_reset_fsm_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1176{
1177 struct msgb *reset_ind;
1178 struct gsm29118_reset_msg reset_params;
1179 struct sgs_mme_ctx *mme = (struct sgs_mme_ctx *)fi->priv;
1180 struct sgs_connection *sgc = mme->conn;
1181 struct sgs_state *sgs = mme->sgs;
1182
1183 switch (event) {
1184 case SGS_VLRR_E_START_RESET:
1185 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_NULL, 0, 0);
1186 mme->ns11_remaining = sgs->cfg.counter[SGS_STATE_NS11];
1187 /* send a reset message and enter WAIT_ACK state */
1188 memset(&reset_params, 0, sizeof(reset_params));
1189 osmo_strlcpy(reset_params.vlr_name, sgs->cfg.vlr_name, sizeof(reset_params.vlr_name));
1190 reset_params.vlr_name_present = true;
1191 reset_ind = gsm29118_create_reset_ind(&reset_params);
1192 sgs_tx(sgc, reset_ind);
Philipp Maier483cea82019-04-03 16:23:29 +02001193
1194 /* Perform a reset of the SGS FSM of all subscribers that are present in the VLR */
1195 vlr_sgs_reset(gsm_network->vlr);
1196
Harald Welte0df904d2018-12-03 11:00:04 +01001197 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_WAIT_ACK, sgs->cfg.timer[SGS_STATE_TS11], 11);
1198 break;
1199 default:
1200 OSMO_ASSERT(0);
1201 break;
1202 }
1203}
1204
1205static int sgs_vlr_reset_fsm_timer_cb(struct osmo_fsm_inst *fi)
1206{
1207 struct msgb *reset_ind;
1208 struct gsm29118_reset_msg reset_params;
1209 struct sgs_mme_ctx *mme = (struct sgs_mme_ctx *)fi->priv;
1210 struct sgs_connection *sgc = mme->conn;
1211 struct sgs_state *sgs = mme->sgs;
1212
1213 switch (fi->T) {
1214 case 11:
1215 if (mme->ns11_remaining >= 1) {
1216 memset(&reset_params, 0, sizeof(reset_params));
1217 osmo_strlcpy(reset_params.vlr_name, sgs->cfg.vlr_name, sizeof(reset_params.vlr_name));
1218 reset_params.vlr_name_present = true;
1219 reset_ind = gsm29118_create_reset_ind(&reset_params);
1220 sgs_tx(sgc, reset_ind);
1221 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_WAIT_ACK, sgs->cfg.timer[SGS_STATE_TS11], 11);
1222 mme->ns11_remaining--;
1223 } else {
1224 LOGMME(mme, LOGL_ERROR, "Ts11 expired more than %u (Ns11) times, giving up\n",
Vadim Yanitskiye9ef7c62019-02-23 16:04:17 +07001225 sgs->cfg.counter[SGS_STATE_NS11]);
Harald Welte0df904d2018-12-03 11:00:04 +01001226 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_NULL, 0, 0);
1227 }
1228 break;
1229 default:
1230 OSMO_ASSERT(0);
1231 break;
1232 }
1233 return 0;
1234}
1235
1236static const struct osmo_fsm_state sgs_vlr_reset_fsm_states[] = {
1237 [SGS_VLRR_ST_NULL] = {
1238 /* We haven't even tried yet to send a RESET */
1239 .name = "NULL",
1240 .action = sgs_vlr_reset_fsm_null,
1241 .in_event_mask = S(SGS_VLRR_E_RX_RESET_ACK),
1242 .out_state_mask = S(SGS_VLRR_ST_NULL) | S(SGS_VLRR_ST_WAIT_ACK),
1243 },
1244 [SGS_VLRR_ST_WAIT_ACK] = {
1245 /* We're waiting for a SGsAP_RESET_ACK */
1246 .name = "WAIT-ACK",
1247 .action = sgs_vlr_reset_fsm_wait_ack,
1248 .in_event_mask = S(SGS_VLRR_E_RX_RESET_ACK),
1249 .out_state_mask = S(SGS_VLRR_ST_NULL) |
1250 S(SGS_VLRR_ST_COMPLETE) | S(SGS_VLRR_ST_WAIT_ACK),
1251 },
1252 [SGS_VLRR_ST_COMPLETE] = {
1253 /* Reset procedure to this MME has been completed */
1254 .name = "COMPLETE",
1255 .action = sgs_vlr_reset_fsm_complete,
1256 .in_event_mask = S(SGS_VLRR_E_RX_RESET_ACK),
1257 .out_state_mask = S(SGS_VLRR_ST_NULL) | S(SGS_VLRR_ST_COMPLETE),
1258 },
1259};
1260
1261static struct osmo_fsm sgs_vlr_reset_fsm = {
1262 .name = "SGs-VLR-RESET",
1263 .states = sgs_vlr_reset_fsm_states,
Philipp Maier483cea82019-04-03 16:23:29 +02001264 .num_states = ARRAY_SIZE(sgs_vlr_reset_fsm_states),
Harald Welte0df904d2018-12-03 11:00:04 +01001265 .allstate_event_mask = S(SGS_VLRR_E_START_RESET),
1266 .allstate_action = sgs_vlr_reset_fsm_allstate,
1267 .timer_cb = sgs_vlr_reset_fsm_timer_cb,
1268 .log_subsys = DSGS,
1269 .event_names = sgs_vlr_reset_fsm_event_names,
1270};
1271
1272/*! Send unit-data through SGs interface (see msc_ifaces.c)
1273 * \param[in] msg layer 3 message to send.
1274 * \returns 0 in case of success, -EINVAL in case of error. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001275int sgs_iface_tx_dtap_ud(struct msc_a *msc_a, struct msgb *msg)
Harald Welte0df904d2018-12-03 11:00:04 +01001276{
Harald Welte0df904d2018-12-03 11:00:04 +01001277 struct msgb *msg_sgs;
1278 struct sgs_mme_ctx *mme;
1279 int rc = -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001280 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Harald Welte0df904d2018-12-03 11:00:04 +01001281
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001282 OSMO_ASSERT(vsub);
Harald Welte0df904d2018-12-03 11:00:04 +01001283
1284 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_DL_UD);
1285 if (!mme)
1286 goto error;
1287
1288 /* Make sure the subscriber has a valid SGs association, otherwise
1289 * don't let unit-data through. */
1290 if (vsub->sgs_fsm->state != SGS_UE_ST_ASSOCIATED) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001291 LOG_MSC_A(msc_a, LOGL_NOTICE, "Cannot Tx %s: subscriber not SGs-associated\n",
1292 sgsap_msg_type_name(SGSAP_MSGT_DL_UD));
Harald Welte0df904d2018-12-03 11:00:04 +01001293 goto error;
1294 }
1295
1296 msg_sgs = gsm29118_create_dl_ud(vsub->imsi, msg);
1297 sgs_tx(mme->conn, msg_sgs);
1298 rc = 0;
1299
1300error:
1301 msgb_free(msg);
1302 return rc;
1303}
1304
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001305void sgs_iface_tx_release(struct vlr_subscr *vsub)
Harald Welte0df904d2018-12-03 11:00:04 +01001306{
1307 struct msgb *msg_sgs;
Harald Welte0df904d2018-12-03 11:00:04 +01001308 struct sgs_mme_ctx *mme;
1309
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001310 OSMO_ASSERT(vsub);
Harald Welte0df904d2018-12-03 11:00:04 +01001311
1312 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_DL_UD);
1313 if (!mme)
1314 return;
1315
1316 msg_sgs = gsm29118_create_release_req(vsub->imsi, 0);
1317 sgs_tx(mme->conn, msg_sgs);
1318}
1319
Philipp Maier002fb012019-09-24 09:26:47 +02001320/*! Send SGsAP-SERVICE-ABORT-REQUEST message to MME
1321 * \param[in] vsub subscriber context */
1322void sgs_iface_tx_serv_abrt(struct vlr_subscr *vsub)
1323{
1324 struct msgb *msg_sgs;
1325 struct sgs_mme_ctx *mme;
1326
1327 OSMO_ASSERT(vsub);
1328
1329 /* The service abort procedure is only defined for MT calls,
1330 * see also 3GPP TS 29.118, chapter 5.13.2 */
1331 if (vsub->sgs.paging_serv_ind != SGSAP_SERV_IND_CS_CALL)
1332 return;
1333
1334 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_DL_UD);
1335 if (!mme)
1336 return;
1337
1338 msg_sgs = gsm29118_create_service_abort_req(vsub->imsi);
1339 sgs_tx(mme->conn, msg_sgs);
1340}
1341
Martin Hauke3f07dac2019-11-14 17:49:08 +01001342/*! initialize SGs new interface
Harald Welte0df904d2018-12-03 11:00:04 +01001343 * \param[in] ctx talloc context
1344 * \param[in] network associated gsm network
1345 * \returns returns allocated sgs_stae, NULL in case of error. */
1346struct sgs_state *sgs_iface_init(void *ctx, struct gsm_network *network)
1347{
1348 struct sgs_state *sgs;
1349
1350 gsm_network = network;
1351
1352 sgs = sgs_server_alloc(ctx);
1353 OSMO_ASSERT(sgs);
1354
1355 /* We currently only support one SGs instance */
1356 if (g_sgs)
1357 return NULL;
1358 g_sgs = sgs;
1359
Harald Welte0df904d2018-12-03 11:00:04 +01001360 return sgs;
1361}
Vadim Yanitskiy4eaefc22019-03-21 20:55:19 +07001362
1363static __attribute__((constructor)) void on_dso_load(void)
1364{
1365 OSMO_ASSERT(osmo_fsm_register(&sgs_vlr_reset_fsm) == 0);
1366}