blob: a845ab84b6c32fd066bfd973e5abf9b36bcc5392 [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
Harald Welte0df904d2018-12-03 11:00:04 +0100469/*! Page UE through SGs interface
470 * \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. */
473int sgs_iface_tx_paging(struct vlr_subscr *vsub, enum sgsap_service_ind serv_ind)
474{
475 struct msgb *resp;
476 struct gsm29118_paging_req paging_params;
477 struct sgs_mme_ctx *mme;
478
479 /* See also: 3GPP TS 29.118, chapter 5.1.2.2 Paging Initiation */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100480 if (vsub->sgs_fsm->state == SGS_UE_ST_NULL && vsub->conf_by_radio_contact_ind == true) {
481 LOGPFSMSL(vsub->sgs_fsm, DPAG, LOGL_ERROR, "Will not Page (conf_by_radio_contact_ind == true)\n");
Harald Welte0df904d2018-12-03 11:00:04 +0100482 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100483 }
Harald Welte0df904d2018-12-03 11:00:04 +0100484
485 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_PAGING_REQ);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100486 if (!mme) {
487 LOGPFSMSL(vsub->sgs_fsm, DPAG, LOGL_ERROR, "Will not Page (no MME)\n");
Harald Welte0df904d2018-12-03 11:00:04 +0100488 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100489 }
Harald Welte0df904d2018-12-03 11:00:04 +0100490
491 /* Check if there is still a paging in progress for this subscriber,
492 * if yes, don't initiate another paging request. */
493 if (vlr_sgs_pag_pend(vsub))
494 return 0;
495
Pau Espin Pedrol42ac3662021-07-12 13:32:07 +0200496 LOGMME(mme, LOGL_INFO, "Paging on SGs: %s for %s (conf_by_radio_contact_ind=%d)\n",
497 vlr_subscr_name(vsub), sgsap_service_ind_name(serv_ind), vsub->conf_by_radio_contact_ind);
498
Harald Welte0df904d2018-12-03 11:00:04 +0100499 memset(&paging_params, 0, sizeof(paging_params));
500 osmo_strlcpy(paging_params.imsi, vsub->imsi, sizeof(paging_params.imsi));
501 osmo_strlcpy(paging_params.vlr_name, mme->sgs->cfg.vlr_name, sizeof(paging_params.vlr_name));
502 paging_params.serv_ind = serv_ind;
503 if (vsub->conf_by_radio_contact_ind == true) {
504 memcpy(&paging_params.lai, &vsub->sgs.lai, sizeof(paging_params.lai));
505 paging_params.lai_present = true;
506 }
507 resp = gsm29118_create_paging_req(&paging_params);
508 sgs_tx(mme->conn, resp);
509
510 /* FIXME: If we are in SGS_UE_ST_NULL while sub->conf_by_radio_contact_ind == false,
511 * we are supposed to start a search procedure as defined in 3GPP TS 23.018 */
512
513 /* Inform the VLR that a paging via SGs is in progress */
514 vlr_sgs_pag(vsub, serv_ind);
515
516 /* Return a page count of 1 (success) */
517 return 1;
518}
519
520/***********************************************************************
521 * SGs incoming messages from the MME
522 ***********************************************************************/
523
524/* Safely read out the SGs cause code from a given message/tlv set, send status
525 * message in case the cause code is invalid or missing. */
526static int sgs_cause_from_msg(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp,
527 const char *imsi)
528{
529 enum sgsap_sgs_cause cause;
530 const uint8_t *cause_ptr;
531 cause_ptr = TLVP_VAL_MINLEN(tp, SGSAP_IE_SGS_CAUSE, 1);
532 if (!cause_ptr) {
533 sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_SGS_CAUSE);
534 return -1;
535 } else
536 cause = *cause_ptr;
537 return cause;
538}
539
540/* SGsAP-STATUS 3GPP TS 29.118, chapter 8.18 */
541static int sgs_rx_status(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, const char *imsi)
542{
543 int cause;
544 const uint8_t *err_msg;
545 const char *imsi_ptr;
546 char *err_msg_hex = "(none)";
547
548 cause = sgs_cause_from_msg(sgc, msg, tp, NULL);
549 if (cause < 0)
550 return 0;
551
552 if (imsi[0] != '\0')
553 imsi_ptr = imsi;
554 else
555 imsi_ptr = "<none>";
556
557 if (TLVP_PRESENT(tp, SGSAP_IE_ERR_MSG))
558 err_msg = TLVP_VAL(tp, SGSAP_IE_ERR_MSG);
559 else
560 err_msg = NULL;
561
562 if (err_msg)
563 err_msg_hex = osmo_hexdump(err_msg, TLVP_LEN(tp, SGSAP_IE_ERR_MSG));
564
565 LOGSGC(sgc, LOGL_NOTICE, "Rx STATUS cause=%s, IMSI=%s, err_msg=%s\n",
566 sgsap_sgs_cause_name(cause), imsi_ptr, err_msg_hex);
567
568 return 0;
569}
570
571/* SGsAP-RESET-INDICATION 3GPP TS 29.118, chapter 8.16 */
572static int sgs_rx_reset_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp)
573{
574 struct gsm29118_reset_msg reset_params;
575 struct msgb *resp;
576
577 memset(&reset_params, 0, sizeof(reset_params));
578 osmo_strlcpy(reset_params.vlr_name, sgc->sgs->cfg.vlr_name, sizeof(reset_params.vlr_name));
579 reset_params.vlr_name_present = true;
580
581 resp = gsm29118_create_reset_ack(&reset_params);
582
583 /* Perform a reset of the SGS FSM of all subscribers that are present in the VLR */
584 vlr_sgs_reset(gsm_network->vlr);
585
586 sgs_tx(sgc, resp);
587 return 0;
588}
589
590/* SGsAP-RESET-ACK 3GPP TS 29.118, chapter 8.15 */
591static int sgs_rx_reset_ack(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp)
592{
593 /* dispatch event to VLR reset FSM for this MME */
594 if (sgc->mme && sgc->mme->fi)
595 osmo_fsm_inst_dispatch(sgc->mme->fi, SGS_VLRR_E_RX_RESET_ACK, msg);
596 return 0;
597}
598
599/* SGsAP-LOCATION-UPDATE-REQUEST 3GPP TS 29.118, chapter 8.11 */
600static int sgs_rx_loc_upd_req(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
601{
602 struct msgb *resp;
603 const uint8_t *lu_type_ie;
604 enum vlr_lu_type type;
605 struct osmo_location_area_id new_lai;
606 const struct gsm48_loc_area_id *gsm48_lai;
607 int rc;
608 char *mme_name;
609 struct vlr_sgs_cfg vlr_sgs_cfg;
610 struct vlr_subscr *vsub;
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200611 struct osmo_plmn_id last_eutran_plmn_buf, *last_eutran_plmn = NULL;
Harald Welte0df904d2018-12-03 11:00:04 +0100612
613 /* Check for lingering connections */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100614 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100615 if (vsub) {
616 subscr_conn_toss(vsub);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100617 vlr_subscr_put(vsub, __func__);
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200618 vsub = NULL;
Harald Welte0df904d2018-12-03 11:00:04 +0100619 }
620
621 /* Determine MME-Name */
622 mme_name = sgs_mme_fqdn_get(sgc);
623 if (!mme_name) {
624 resp = gsm29118_create_lu_rej(imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, NULL);
625 sgs_tx(sgc, resp);
626 return 0;
627 }
628
629 /* Parse LU-Type */
630 lu_type_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_EPS_LU_TYPE, 1);
631 if (!lu_type_ie)
632 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_EPS_LU_TYPE);
633 if (lu_type_ie[0] == 0x01)
634 type = VLR_LU_TYPE_IMSI_ATTACH;
635 else
636 type = VLR_LU_TYPE_REGULAR;
637
638 /* Parse LAI of the new location */
639 gsm48_lai = (struct gsm48_loc_area_id *)TLVP_VAL_MINLEN(tp, SGSAP_IE_LAI, 5);
640 if (!gsm48_lai)
641 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_LAI);
642 gsm48_decode_lai2(gsm48_lai, &new_lai);
643
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200644 /* 3GPP TS 23.272 sec 4.3.3 (CSFB):
645 * "During the SGs location update procedure, obtaining the last used LTE PLMN ID via TAI"
646 */
647 if (TLVP_PRES_LEN(tp, SGSAP_IE_TAI, 3)) {
648 last_eutran_plmn = &last_eutran_plmn_buf;
649 osmo_plmn_from_bcd(TLVP_VAL(tp, SGSAP_IE_TAI), last_eutran_plmn);
650 /* TODO: we could also gather the TAC from here, but we don't need it yet */
651 } else if (TLVP_PRES_LEN(tp, SGSAP_IE_EUTRAN_CGI, 3)) {
652 /* Since TAI is optional, let's try harder getting Last Used
653 * E-UTRAN PLMN ID by fetching it from E-UTRAN CGI */
654 last_eutran_plmn = &last_eutran_plmn_buf;
655 osmo_plmn_from_bcd(TLVP_VAL(tp, SGSAP_IE_EUTRAN_CGI), last_eutran_plmn);
656 /* TODO: we could also gather the ECI from here, but we don't need it yet */
657 } else {
658 LOGSGC(sgc, LOGL_INFO, "Receiving SGsAP-LOCATION-UPDATE-REQUEST without TAI nor "
659 "E-CGI IEs, fast fallback GERAN->EUTRAN won't be possible!\n");
660 }
661
Harald Welte0df904d2018-12-03 11:00:04 +0100662 /* Perform actual location update */
663 memcpy(vlr_sgs_cfg.timer, sgc->sgs->cfg.timer, sizeof(vlr_sgs_cfg.timer));
664 memcpy(vlr_sgs_cfg.counter, sgc->sgs->cfg.counter, sizeof(vlr_sgs_cfg.counter));
665 rc = vlr_sgs_loc_update(gsm_network->vlr, &vlr_sgs_cfg, sgs_tx_loc_upd_resp_cb, sgs_iface_tx_paging,
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200666 sgs_tx_mm_info_cb, mme_name, type, imsi, &new_lai, last_eutran_plmn);
Harald Welte0df904d2018-12-03 11:00:04 +0100667 if (rc != 0) {
668 resp = gsm29118_create_lu_rej(imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, NULL);
669 sgs_tx(sgc, resp);
670 }
671
672 return 0;
673}
674
675/* SGsAP-IMSI-DETACH-INDICATION 3GPP TS 29.118, chapter 8.8 */
676static int sgs_rx_imsi_det_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
677{
678 struct msgb *resp;
679 enum sgsap_imsi_det_noneps_type type;
680 const uint8_t *type_ie;
681
682 type_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_IMSI_DET_NONEPS_TYPE, 1);
683 if (!type_ie)
684 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_IMSI_DET_NONEPS_TYPE);
685
686 switch (type_ie[0]) {
687 case SGSAP_ID_NONEPS_T_EXPLICIT_UE_NONEPS:
688 type = SGSAP_ID_NONEPS_T_EXPLICIT_UE_NONEPS;
689 break;
690 case SGSAP_ID_NONEPS_T_COMBINED_UE_EPS_NONEPS:
691 type = SGSAP_ID_NONEPS_T_COMBINED_UE_EPS_NONEPS;
692 break;
693 case SGSAP_ID_NONEPS_T_IMPLICIT_UE_EPS_NONEPS:
694 type = SGSAP_ID_NONEPS_T_IMPLICIT_UE_EPS_NONEPS;
695 break;
696 default:
697 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_INVALID_MAND_IE, msg, SGSAP_IE_IMSI_DET_NONEPS_TYPE);
698 break;
699 }
700
701 vlr_sgs_imsi_detach(gsm_network->vlr, imsi, type);
702 resp = gsm29118_create_imsi_det_ack(imsi);
703 sgs_tx(sgc, resp);
704
705 return 0;
706}
707
708/* SGsAP-EPS-DETACH-INDICATION 3GPP TS 29.118, chapter 8.6 */
709static int sgs_rx_eps_det_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
710{
711 struct msgb *resp;
712 enum sgsap_imsi_det_eps_type type;
713 const uint8_t *type_ie;
714
715 type_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_IMSI_DET_EPS_TYPE, 1);
716 if (!type_ie)
717 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_IMSI_DET_EPS_TYPE);
718
719 switch (type_ie[0]) {
720 case SGSAP_ID_EPS_T_NETWORK_INITIATED:
721 type = SGSAP_ID_EPS_T_NETWORK_INITIATED;
722 break;
723 case SGSAP_ID_EPS_T_UE_INITIATED:
724 type = SGSAP_ID_EPS_T_UE_INITIATED;
725 break;
726 case SGSAP_ID_EPS_T_EPS_NOT_ALLOWED:
727 type = SGSAP_ID_EPS_T_EPS_NOT_ALLOWED;
728 break;
729 default:
730 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_INVALID_MAND_IE, msg, SGSAP_IE_IMSI_DET_EPS_TYPE);
731 break;
732 }
733
734 vlr_sgs_eps_detach(gsm_network->vlr, imsi, type);
735 resp = gsm29118_create_eps_det_ack(imsi);
736 sgs_tx(sgc, resp);
737
738 return 0;
739}
740
741/* SGsAP-PAGING-REJECT 3GPP TS 29.118, chapter 8.13 */
742static int sgs_rx_pag_rej(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
743{
744 int cause;
745 struct vlr_subscr *vsub;
746
747 cause = sgs_cause_from_msg(sgc, msg, tp, NULL);
748 if (cause < 0)
749 return 0;
750
751 /* Subscriber must be known by the VLR */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100752 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100753 if (!vsub)
754 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, msg, SGSAP_IE_IMSI);
755
756 /* Inform the VLR */
757 vlr_sgs_pag_rej(gsm_network->vlr, imsi, cause);
758
759 /* Stop all paging activity */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100760 paging_expired(vsub);
Harald Welte0df904d2018-12-03 11:00:04 +0100761
762 /* Depending on the cause code some action is required */
763 if (cause == SGSAP_SGS_CAUSE_MT_CSFB_REJ_USER) {
764 /* FIXME: We are supposed to trigger a User Determined User Busy (UDUB)
765 * as specified in 3GPP TS 24.082 here, SGs association state shall not
766 * be changed */
767 LOGSGC(sgc, LOGL_ERROR,
768 "Rx %s with SGSAP_SGS_CAUSE_MT_CSFB_REJ_USER, but sending UDUP is not implemented yet!\n",
769 sgsap_msg_type_name(msg->data[0]));
770 } else if (cause == SGSAP_SGS_CAUSE_IMSI_DET_EPS) {
771 /* FIXME: In this case we should send the paging via A/Iu interface */
772 OSMO_ASSERT(false);
773 }
774
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100775 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100776 return 0;
777}
778
779/* SGsAP-UE-UNREACHABLE 3GPP TS 29.118, chapter 8.21 */
780static int sgs_rx_ue_unr(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
781{
782 int cause;
783
784 cause = sgs_cause_from_msg(sgc, msg, tp, NULL);
785 if (cause < 0)
786 return 0;
787
788 vlr_sgs_ue_unr(gsm_network->vlr, imsi, cause);
789
790 return 0;
791}
792
793/* SGsAP-TMSI-REALLOCATION-COMPLETE 3GPP TS 29.118, chapter 8.19 */
794static int sgs_rx_tmsi_reall_cmpl(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
795{
796 vlr_sgs_tmsi_reall_compl(gsm_network->vlr, imsi);
797 return 0;
798}
799
800/* SGsAP-SERVICE-REQUEST 3GPP TS 29.118, chapter 8.17 */
801static int sgs_rx_service_req(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
802{
803 enum sgsap_service_ind serv_ind;
804 const uint8_t *serv_ind_ie;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100805 struct msc_a *msc_a;
Harald Welte0df904d2018-12-03 11:00:04 +0100806 struct vlr_subscr *vsub;
807
808 /* Note: While in other RAN concepts a service request is used to
809 * initiate mobile originated operation, the service request in SGsAP
810 * is comparable to a paging response. The SGsAP SERVICE REQUEST must
811 * not be confused or compared with a CM SERVICE REQUEST! */
812
813 if (!check_sgs_association(sgc, msg, imsi))
814 return 0;
815
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100816 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100817 /* Note: vsub is already sufficiently verified by check_sgs_association(),
818 * we must have a vsub at this point! */
819 OSMO_ASSERT(vsub);
820
821 /* The Service request is intended as a paging response, if one is
822 * received while nothing is paging something is very wrong! */
823 if (!vlr_sgs_pag_pend(vsub)) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100824 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100825 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE, msg, -1);
826 }
827 serv_ind_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_SERVICE_INDICATOR, 1);
828
829 if (!serv_ind_ie) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100830 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100831 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_SERVICE_INDICATOR);
832 }
833 if (serv_ind_ie[0] == SGSAP_SERV_IND_CS_CALL)
834 serv_ind = serv_ind_ie[0];
835 else if (serv_ind_ie[0] == SGSAP_SERV_IND_SMS)
836 serv_ind = serv_ind_ie[0];
837 else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100838 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100839 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_INVALID_MAND_IE, msg, SGSAP_IE_SERVICE_INDICATOR);
840 }
841
842 /* FIXME: The MME shall include an UE EMM Mode IE, but the field is
843 * marked optional. (Why do we need this info at all?) */
844
845 /* Report to the VLR that the paging has successfully completed */
846 vlr_sgs_pag_ack(gsm_network->vlr, imsi);
847
848 /* Exit early when the service indicator indicates that a call is being
849 * established. In those cases we do not allocate a connection, instead
850 * the connection will be allocated when the MS is appearing on the
851 * A-Interface. */
852 if (serv_ind == SGSAP_SERV_IND_CS_CALL) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100853 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100854 return 0;
855 }
856
857 /* Allocate subscriber connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100858 msc_a = subscr_conn_allocate_sgs(sgc, vsub, true);
859 if (!msc_a) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100860 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100861 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE, msg, -1);
862 }
863
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100864 /* The msub has added a get() for the vsub, balance above vlr_subscr_find_by_imsi() */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100865 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100866 return 0;
867}
868
869/* SGsAP-UPLINK-UNITDATA 3GPP TS 29.118, chapter 8.22 */
870static int sgs_rx_ul_ud(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
871{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100872 struct msc_a *msc_a;
Harald Welte0df904d2018-12-03 11:00:04 +0100873 const uint8_t *nas_msg_container_ie;
874 struct vlr_subscr *vsub;
875
876 if (!check_sgs_association(sgc, msg, imsi))
877 return 0;
878
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100879 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100880 /* Note: vsub is already sufficiently verified by check_sgs_association(),
881 * we must have a vsub at this point! */
882 OSMO_ASSERT(vsub);
883
884 /* Try to find existing connection (MT) or allocate a new one (MO) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100885 msc_a = msc_a_for_vsub(vsub, true);
886 if (!msc_a)
887 msc_a = subscr_conn_allocate_sgs(sgc, vsub, false);
Harald Welte0df904d2018-12-03 11:00:04 +0100888
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100889 /* Balance above vlr_subscr_find_by_imsi() */
890 vlr_subscr_put(vsub, __func__);
891
Harald Welte0df904d2018-12-03 11:00:04 +0100892 /* If we do not find an existing connection and allocating a new one
Martin Hauke3f07dac2019-11-14 17:49:08 +0100893 * failed, give up and return status. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100894 if (!msc_a)
Harald Welte0df904d2018-12-03 11:00:04 +0100895 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE, msg, 0);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100896
897 if (msc_a->c.ran->type != OSMO_RAT_EUTRAN_SGS) {
898 LOGSGC(sgc, LOGL_ERROR,
899 "Receiving uplink unit-data for non-sgs connection -- discarding message!\n");
900 return -EINVAL;
Harald Welte0df904d2018-12-03 11:00:04 +0100901 }
902
903 nas_msg_container_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_NAS_MSG_CONTAINER, 1);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100904 if (!nas_msg_container_ie)
Harald Welte0df904d2018-12-03 11:00:04 +0100905 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 +0100906
907 /* ran_conn_dtap expects the dtap payload in l3h */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100908 msg->l3h = (uint8_t *)nas_msg_container_ie;
909 msc_a_up_l3(msc_a, msg);
Harald Welte0df904d2018-12-03 11:00:04 +0100910
Harald Welte0df904d2018-12-03 11:00:04 +0100911 return 0;
912}
913
914/* SGsAP-MO-CSFB-INDICATION, chapter 8.25 */
915static int sgs_rx_csfb_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
916{
917 struct vlr_subscr *vsub;
Pau Espin Pedrol82529592021-07-01 17:59:49 +0200918 struct osmo_plmn_id last_eutran_plmn_buf;
919 const struct osmo_plmn_id *last_eutran_plmn = &last_eutran_plmn_buf;
Harald Welte0df904d2018-12-03 11:00:04 +0100920
Philipp Maier25e7ba52019-08-15 14:51:49 +0200921 /* The MME informs us with this message that the UE has initiated a
922 * service request for MO CS fallback. There is not much we can do with
923 * this information, however, we can check if the subscriber actually
924 * exists in the VLR and if there are any lingering connections open.*/
Harald Welte0df904d2018-12-03 11:00:04 +0100925
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100926 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100927 if (!vsub)
928 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, msg, SGSAP_IE_IMSI);
929
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200930 /* 3GPP TS 23.272 sec 4.3.3 (CSFB):
931 * "During the SGs location update procedure, obtaining the last used LTE PLMN ID via TAI"
932 */
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200933 if (TLVP_PRES_LEN(tp, SGSAP_IE_TAI, 3)) {
Pau Espin Pedrol82529592021-07-01 17:59:49 +0200934 osmo_plmn_from_bcd(TLVP_VAL(tp, SGSAP_IE_TAI), &last_eutran_plmn_buf);
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200935 /* TODO: we could also gather the TAC from here, but we don't need it yet */
936 } else if (TLVP_PRES_LEN(tp, SGSAP_IE_EUTRAN_CGI, 3)) {
937 /* Since TAI is optional, let's try harder getting Last Used
938 * E-UTRAN PLMN ID by fetching it from E-UTRAN CGI */
Pau Espin Pedrol82529592021-07-01 17:59:49 +0200939 osmo_plmn_from_bcd(TLVP_VAL(tp, SGSAP_IE_EUTRAN_CGI), &last_eutran_plmn_buf);
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200940 /* TODO: we could also gather the ECI from here, but we don't need it yet */
Pau Espin Pedrol82529592021-07-01 17:59:49 +0200941 } else {
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200942 LOGSGC(sgc, LOGL_INFO, "Receiving SGsAP-MO-CSFB-INDICATION without TAI nor "
943 "E-CGI IEs, and they are not known from previous SGsAP-LOCATION-UPDATE-REQUEST. "
944 "Fast fallback GERAN->EUTRAN won't be possible!\n");
Pau Espin Pedrol82529592021-07-01 17:59:49 +0200945 last_eutran_plmn = NULL;
Pau Espin Pedrol67106702021-04-27 18:20:15 +0200946 }
947
Pau Espin Pedrol82529592021-07-01 17:59:49 +0200948 vlr_subscr_set_last_used_eutran_plmn_id(vsub, last_eutran_plmn);
949
Harald Welte0df904d2018-12-03 11:00:04 +0100950 /* Check for lingering connections */
951 subscr_conn_toss(vsub);
952
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100953 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100954 return 0;
955}
956
957/* SGsAP-UE-ACTIVITY-INDICATION, chapter 8.20 */
958static int sgs_rx_ue_act_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
959{
960 /* In this MSC/VLR implementation we do not support the alerting
961 * procedure yet and therefore we will never request any alerting
962 * at the MME. Given that it is unlikely that we ever get activity
963 * indications from the MME, but if we do we should not act all too
964 * hostile and ignore the indication silently. */
965
966 LOGSGC(sgc, LOGL_ERROR, "Rx %s unexpected, we do not implement alerting yet, ignoring!\n",
967 sgsap_msg_type_name(msg->data[0]));
968
969 return 0;
970}
971
972#define TX_STATUS_AND_LOG(sgc, msg_type, cause, fmt) \
973 LOGSGC(sgc, LOGL_ERROR, fmt, sgsap_msg_type_name(msg_type)); \
974 resp = gsm29118_create_status(NULL, cause, msg); \
975 sgs_tx(sgc, resp); \
976
977/*! Process incoming SGs message (see sgs_server.c)
978 * \param[in] sgc related sgs connection
979 * \param[in] msg received message
980 * \returns 0 in case of success, -EINVAL in case of error. */
981int sgs_iface_rx(struct sgs_connection *sgc, struct msgb *msg)
982{
983 struct msgb *resp;
984 uint8_t msg_type = msg->l2h[0];
985 struct tlv_parsed tp;
986 int rc;
987 char imsi[GSM48_MI_SIZE];
988 char mme_name[SGS_MME_NAME_LEN + 1];
989
990 memset(imsi, 0, sizeof(imsi));
991 memset(mme_name, 0, sizeof(mme_name));
992
993 /* When the receiving entity receives a message that is too short to contain a complete
994 * message type information element, the receiving entity shall ignore that message. */
995 if (msgb_l2len(msg) < 1)
996 goto error;
997
998 /* Parse TLV elements */
999 rc = tlv_parse(&tp, &sgsap_ie_tlvdef, msgb_l2(msg) + 1, msgb_l2len(msg) - 1, 0, 0);
Philipp Maier6dcdfb02019-09-24 11:44:28 +02001000 if (rc < 0)
1001 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 +01001002
1003 /* Most of the messages contain an IMSI as mandatory IE, parse it right here */
1004 if (!TLVP_PRESENT(&tp, SGSAP_IE_IMSI) &&
1005 msg_type != SGSAP_MSGT_STATUS && msg_type != SGSAP_MSGT_RESET_IND && msg_type != SGSAP_MSGT_RESET_ACK) {
1006 /* reject the message; all but the three above have mandatory IMSI */
1007 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_MISSING_MAND_IE,
1008 "SGsAP Message %s without IMSI, dropping\n");
1009 goto error;
1010 }
1011
1012 if (TLVP_PRESENT(&tp, SGSAP_IE_IMSI)) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001013 struct osmo_mobile_identity mi;
1014 if (osmo_mobile_identity_decode(&mi,
1015 TLVP_VAL(&tp, SGSAP_IE_IMSI),
1016 TLVP_LEN(&tp, SGSAP_IE_IMSI), false)
1017 || mi.type != GSM_MI_TYPE_IMSI) {
Harald Welte0df904d2018-12-03 11:00:04 +01001018 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_INVALID_MAND_IE,
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001019 "SGsAP Message %s with invalid IMSI, dropping\n");
Harald Welte0df904d2018-12-03 11:00:04 +01001020 goto error;
1021 }
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001022 OSMO_STRLCPY_ARRAY(imsi, mi.imsi);
Harald Welte0df904d2018-12-03 11:00:04 +01001023 }
1024
Pau Espin Pedrol25b68472021-08-24 11:34:05 +02001025 /* Some messages contain an MME-NAME as mandatory IE, parse it right here. The
1026 * MME-NAME is also immediately registered with the sgc, so it will be implicitly
Harald Welte0df904d2018-12-03 11:00:04 +01001027 * known to all functions that have access to the sgc context. */
1028 if (!TLVP_PRESENT(&tp, SGSAP_IE_MME_NAME)
1029 && (msg_type == SGSAP_MSGT_RESET_IND || msg_type == SGSAP_MSGT_RESET_ACK
1030 || msg_type == SGSAP_MSGT_LOC_UPD_REQ || msg_type == SGSAP_MSGT_IMSI_DET_IND
1031 || msg_type == SGSAP_MSGT_EPS_DET_IND)) {
1032 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_MISSING_MAND_IE,
1033 "SGsAP Message %s without MME-Name, dropping\n");
1034 goto error;
1035 }
1036
1037 if (TLVP_PRESENT(&tp, SGSAP_IE_MME_NAME)) {
1038 if (decode_mme_name(mme_name, &tp) != 0) {
1039 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_INVALID_MAND_IE,
1040 "SGsAP Message %s with invalid MME-Name, dropping\n");
1041 goto error;
1042 }
1043 /* Regsister/check mme_name with sgc */
1044 if (sgs_mme_fqdn_received(sgc, mme_name) < 0) {
1045 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE,
1046 "SGsAP Message %s with invalid MME-Name, dropping\n");
1047 goto error;
1048 }
1049 }
1050
1051 /* dispatch msg to various handler functions. msgb ownership remains here! */
1052 rc = -EINVAL;
1053 switch (msg_type) {
1054 case SGSAP_MSGT_STATUS:
1055 rc = sgs_rx_status(sgc, msg, &tp, imsi);
1056 break;
1057 case SGSAP_MSGT_RESET_IND:
1058 rc = sgs_rx_reset_ind(sgc, msg, &tp);
1059 break;
1060 case SGSAP_MSGT_RESET_ACK:
1061 rc = sgs_rx_reset_ack(sgc, msg, &tp);
1062 break;
1063 case SGSAP_MSGT_LOC_UPD_REQ:
1064 rc = sgs_rx_loc_upd_req(sgc, msg, &tp, imsi);
1065 break;
1066 case SGSAP_MSGT_IMSI_DET_IND:
1067 rc = sgs_rx_imsi_det_ind(sgc, msg, &tp, imsi);
1068 break;
1069 case SGSAP_MSGT_EPS_DET_IND:
1070 rc = sgs_rx_eps_det_ind(sgc, msg, &tp, imsi);
1071 break;
1072 case SGSAP_MSGT_PAGING_REJ:
1073 rc = sgs_rx_pag_rej(sgc, msg, &tp, imsi);
1074 break;
1075 case SGSAP_MSGT_UE_UNREACHABLE:
1076 rc = sgs_rx_ue_unr(sgc, msg, &tp, imsi);
1077 break;
1078 case SGSAP_MSGT_TMSI_REALL_CMPL:
1079 rc = sgs_rx_tmsi_reall_cmpl(sgc, msg, &tp, imsi);
1080 break;
1081 case SGSAP_MSGT_SERVICE_REQ:
1082 rc = sgs_rx_service_req(sgc, msg, &tp, imsi);
1083 break;
1084 case SGSAP_MSGT_UL_UD:
1085 rc = sgs_rx_ul_ud(sgc, msg, &tp, imsi);
1086 break;
1087 case SGSAP_MSGT_MO_CSFB_IND:
1088 rc = sgs_rx_csfb_ind(sgc, msg, &tp, imsi);
1089 break;
1090 case SGSAP_MSGT_UE_ACT_IND:
1091 rc = sgs_rx_ue_act_ind(sgc, msg, &tp, imsi);
1092 break;
1093 case SGSAP_MSGT_ALERT_ACK:
1094 case SGSAP_MSGT_ALERT_REJ:
1095 LOGSGC(sgc, LOGL_ERROR, "Rx unmplemented SGsAP %s: %s\n",
1096 sgsap_msg_type_name(msg_type), msgb_hexdump(msg));
1097 resp = gsm29118_create_status(imsi, SGSAP_SGS_CAUSE_MSG_UNKNOWN, msg);
1098 sgs_tx(sgc, resp);
1099 rc = 0;
1100 break;
1101 default:
1102 LOGSGC(sgc, LOGL_ERROR, "Rx unknown SGsAP message type 0x%02x: %s\n", msg_type, msgb_hexdump(msg));
1103 resp = gsm29118_create_status(imsi, SGSAP_SGS_CAUSE_MSG_UNKNOWN, msg);
1104 sgs_tx(sgc, resp);
1105 rc = 0;
1106 break;
1107 }
1108
1109 /* Catch unhandled errors */
1110 if (rc < 0) {
1111 /* Note: Usually the sgs_rx_ should catch errors locally and
1112 * eimit a status message with proper cause code, including
1113 * a suitable log message. If we end up here, something is
1114 * not right and should be fixed */
1115 LOGSGC(sgc, LOGL_ERROR, "Rx unable to decode SGsAP %s: %s\n",
1116 sgsap_msg_type_name(msg_type), msgb_hexdump(msg));
1117 resp = gsm29118_create_status(imsi, SGSAP_SGS_CAUSE_MSG_UNKNOWN, msg);
1118 sgs_tx(sgc, resp);
1119 }
1120
1121error:
1122 msgb_free(msg);
1123 return 0;
1124}
1125
1126/***********************************************************************
1127 * SGs connection "VLR Reset Procedure" FSM
1128 ***********************************************************************/
1129
1130static const struct value_string sgs_vlr_reset_fsm_event_names[] = {
1131 {SGS_VLRR_E_START_RESET, "START-RESET"},
1132 {SGS_VLRR_E_RX_RESET_ACK, "RX-RESET-ACK"},
1133 {0, NULL}
1134};
1135
1136static void sgs_vlr_reset_fsm_null(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1137{
1138 switch (event) {
1139 case SGS_VLRR_E_RX_RESET_ACK:
1140 break;
1141 default:
1142 OSMO_ASSERT(0);
1143 break;
1144 }
1145}
1146
1147static void sgs_vlr_reset_fsm_wait_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1148{
1149 switch (event) {
1150 case SGS_VLRR_E_RX_RESET_ACK:
1151 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_COMPLETE, 0, 0);
1152 break;
1153 default:
1154 OSMO_ASSERT(0);
1155 break;
1156 }
1157}
1158
1159static void sgs_vlr_reset_fsm_complete(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1160{
1161 switch (event) {
1162 case SGS_VLRR_E_RX_RESET_ACK:
1163 break;
1164 default:
1165 OSMO_ASSERT(0);
1166 break;
1167 }
1168}
1169
1170static void sgs_vlr_reset_fsm_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1171{
1172 struct msgb *reset_ind;
1173 struct gsm29118_reset_msg reset_params;
1174 struct sgs_mme_ctx *mme = (struct sgs_mme_ctx *)fi->priv;
1175 struct sgs_connection *sgc = mme->conn;
1176 struct sgs_state *sgs = mme->sgs;
1177
1178 switch (event) {
1179 case SGS_VLRR_E_START_RESET:
1180 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_NULL, 0, 0);
1181 mme->ns11_remaining = sgs->cfg.counter[SGS_STATE_NS11];
1182 /* send a reset message and enter WAIT_ACK state */
1183 memset(&reset_params, 0, sizeof(reset_params));
1184 osmo_strlcpy(reset_params.vlr_name, sgs->cfg.vlr_name, sizeof(reset_params.vlr_name));
1185 reset_params.vlr_name_present = true;
1186 reset_ind = gsm29118_create_reset_ind(&reset_params);
1187 sgs_tx(sgc, reset_ind);
Philipp Maier483cea82019-04-03 16:23:29 +02001188
1189 /* Perform a reset of the SGS FSM of all subscribers that are present in the VLR */
1190 vlr_sgs_reset(gsm_network->vlr);
1191
Harald Welte0df904d2018-12-03 11:00:04 +01001192 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_WAIT_ACK, sgs->cfg.timer[SGS_STATE_TS11], 11);
1193 break;
1194 default:
1195 OSMO_ASSERT(0);
1196 break;
1197 }
1198}
1199
1200static int sgs_vlr_reset_fsm_timer_cb(struct osmo_fsm_inst *fi)
1201{
1202 struct msgb *reset_ind;
1203 struct gsm29118_reset_msg reset_params;
1204 struct sgs_mme_ctx *mme = (struct sgs_mme_ctx *)fi->priv;
1205 struct sgs_connection *sgc = mme->conn;
1206 struct sgs_state *sgs = mme->sgs;
1207
1208 switch (fi->T) {
1209 case 11:
1210 if (mme->ns11_remaining >= 1) {
1211 memset(&reset_params, 0, sizeof(reset_params));
1212 osmo_strlcpy(reset_params.vlr_name, sgs->cfg.vlr_name, sizeof(reset_params.vlr_name));
1213 reset_params.vlr_name_present = true;
1214 reset_ind = gsm29118_create_reset_ind(&reset_params);
1215 sgs_tx(sgc, reset_ind);
1216 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_WAIT_ACK, sgs->cfg.timer[SGS_STATE_TS11], 11);
1217 mme->ns11_remaining--;
1218 } else {
1219 LOGMME(mme, LOGL_ERROR, "Ts11 expired more than %u (Ns11) times, giving up\n",
Vadim Yanitskiye9ef7c62019-02-23 16:04:17 +07001220 sgs->cfg.counter[SGS_STATE_NS11]);
Harald Welte0df904d2018-12-03 11:00:04 +01001221 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_NULL, 0, 0);
1222 }
1223 break;
1224 default:
1225 OSMO_ASSERT(0);
1226 break;
1227 }
1228 return 0;
1229}
1230
1231static const struct osmo_fsm_state sgs_vlr_reset_fsm_states[] = {
1232 [SGS_VLRR_ST_NULL] = {
1233 /* We haven't even tried yet to send a RESET */
1234 .name = "NULL",
1235 .action = sgs_vlr_reset_fsm_null,
1236 .in_event_mask = S(SGS_VLRR_E_RX_RESET_ACK),
1237 .out_state_mask = S(SGS_VLRR_ST_NULL) | S(SGS_VLRR_ST_WAIT_ACK),
1238 },
1239 [SGS_VLRR_ST_WAIT_ACK] = {
1240 /* We're waiting for a SGsAP_RESET_ACK */
1241 .name = "WAIT-ACK",
1242 .action = sgs_vlr_reset_fsm_wait_ack,
1243 .in_event_mask = S(SGS_VLRR_E_RX_RESET_ACK),
1244 .out_state_mask = S(SGS_VLRR_ST_NULL) |
1245 S(SGS_VLRR_ST_COMPLETE) | S(SGS_VLRR_ST_WAIT_ACK),
1246 },
1247 [SGS_VLRR_ST_COMPLETE] = {
1248 /* Reset procedure to this MME has been completed */
1249 .name = "COMPLETE",
1250 .action = sgs_vlr_reset_fsm_complete,
1251 .in_event_mask = S(SGS_VLRR_E_RX_RESET_ACK),
1252 .out_state_mask = S(SGS_VLRR_ST_NULL) | S(SGS_VLRR_ST_COMPLETE),
1253 },
1254};
1255
1256static struct osmo_fsm sgs_vlr_reset_fsm = {
1257 .name = "SGs-VLR-RESET",
1258 .states = sgs_vlr_reset_fsm_states,
Philipp Maier483cea82019-04-03 16:23:29 +02001259 .num_states = ARRAY_SIZE(sgs_vlr_reset_fsm_states),
Harald Welte0df904d2018-12-03 11:00:04 +01001260 .allstate_event_mask = S(SGS_VLRR_E_START_RESET),
1261 .allstate_action = sgs_vlr_reset_fsm_allstate,
1262 .timer_cb = sgs_vlr_reset_fsm_timer_cb,
1263 .log_subsys = DSGS,
1264 .event_names = sgs_vlr_reset_fsm_event_names,
1265};
1266
1267/*! Send unit-data through SGs interface (see msc_ifaces.c)
1268 * \param[in] msg layer 3 message to send.
1269 * \returns 0 in case of success, -EINVAL in case of error. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001270int sgs_iface_tx_dtap_ud(struct msc_a *msc_a, struct msgb *msg)
Harald Welte0df904d2018-12-03 11:00:04 +01001271{
Harald Welte0df904d2018-12-03 11:00:04 +01001272 struct msgb *msg_sgs;
1273 struct sgs_mme_ctx *mme;
1274 int rc = -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001275 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Harald Welte0df904d2018-12-03 11:00:04 +01001276
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001277 OSMO_ASSERT(vsub);
Harald Welte0df904d2018-12-03 11:00:04 +01001278
1279 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_DL_UD);
1280 if (!mme)
1281 goto error;
1282
1283 /* Make sure the subscriber has a valid SGs association, otherwise
1284 * don't let unit-data through. */
1285 if (vsub->sgs_fsm->state != SGS_UE_ST_ASSOCIATED) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001286 LOG_MSC_A(msc_a, LOGL_NOTICE, "Cannot Tx %s: subscriber not SGs-associated\n",
1287 sgsap_msg_type_name(SGSAP_MSGT_DL_UD));
Harald Welte0df904d2018-12-03 11:00:04 +01001288 goto error;
1289 }
1290
1291 msg_sgs = gsm29118_create_dl_ud(vsub->imsi, msg);
1292 sgs_tx(mme->conn, msg_sgs);
1293 rc = 0;
1294
1295error:
1296 msgb_free(msg);
1297 return rc;
1298}
1299
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001300void sgs_iface_tx_release(struct vlr_subscr *vsub)
Harald Welte0df904d2018-12-03 11:00:04 +01001301{
1302 struct msgb *msg_sgs;
Harald Welte0df904d2018-12-03 11:00:04 +01001303 struct sgs_mme_ctx *mme;
1304
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001305 OSMO_ASSERT(vsub);
Harald Welte0df904d2018-12-03 11:00:04 +01001306
1307 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_DL_UD);
1308 if (!mme)
1309 return;
1310
1311 msg_sgs = gsm29118_create_release_req(vsub->imsi, 0);
1312 sgs_tx(mme->conn, msg_sgs);
1313}
1314
Philipp Maier002fb012019-09-24 09:26:47 +02001315/*! Send SGsAP-SERVICE-ABORT-REQUEST message to MME
1316 * \param[in] vsub subscriber context */
1317void sgs_iface_tx_serv_abrt(struct vlr_subscr *vsub)
1318{
1319 struct msgb *msg_sgs;
1320 struct sgs_mme_ctx *mme;
1321
1322 OSMO_ASSERT(vsub);
1323
1324 /* The service abort procedure is only defined for MT calls,
1325 * see also 3GPP TS 29.118, chapter 5.13.2 */
1326 if (vsub->sgs.paging_serv_ind != SGSAP_SERV_IND_CS_CALL)
1327 return;
1328
1329 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_DL_UD);
1330 if (!mme)
1331 return;
1332
1333 msg_sgs = gsm29118_create_service_abort_req(vsub->imsi);
1334 sgs_tx(mme->conn, msg_sgs);
1335}
1336
Martin Hauke3f07dac2019-11-14 17:49:08 +01001337/*! initialize SGs new interface
Harald Welte0df904d2018-12-03 11:00:04 +01001338 * \param[in] ctx talloc context
1339 * \param[in] network associated gsm network
1340 * \returns returns allocated sgs_stae, NULL in case of error. */
1341struct sgs_state *sgs_iface_init(void *ctx, struct gsm_network *network)
1342{
1343 struct sgs_state *sgs;
1344
1345 gsm_network = network;
1346
1347 sgs = sgs_server_alloc(ctx);
1348 OSMO_ASSERT(sgs);
1349
1350 /* We currently only support one SGs instance */
1351 if (g_sgs)
1352 return NULL;
1353 g_sgs = sgs;
1354
Harald Welte0df904d2018-12-03 11:00:04 +01001355 return sgs;
1356}
Vadim Yanitskiy4eaefc22019-03-21 20:55:19 +07001357
1358static __attribute__((constructor)) void on_dso_load(void)
1359{
1360 OSMO_ASSERT(osmo_fsm_register(&sgs_vlr_reset_fsm) == 0);
1361}