blob: d13449d296a919582484219f6fc150d3c9350a5b [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
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100127 msc_a_release_mo(msub_msc_a(msub), GSM48_REJECT_CONGESTION);
128 /* 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
367/* Called by VLR via callback, transmits the the location update response or
368 * 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
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100479 LOGP(DMSC, LOGL_NOTICE, "XXXXXXXXXX state == %d conf_by_radio_contact_ind == %d\n",
480 vsub->sgs_fsm->state, vsub->conf_by_radio_contact_ind);
481
Harald Welte0df904d2018-12-03 11:00:04 +0100482 /* See also: 3GPP TS 29.118, chapter 5.1.2.2 Paging Initiation */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100483 if (vsub->sgs_fsm->state == SGS_UE_ST_NULL && vsub->conf_by_radio_contact_ind == true) {
484 LOGPFSMSL(vsub->sgs_fsm, DPAG, LOGL_ERROR, "Will not Page (conf_by_radio_contact_ind == true)\n");
Harald Welte0df904d2018-12-03 11:00:04 +0100485 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100486 }
Harald Welte0df904d2018-12-03 11:00:04 +0100487
488 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_PAGING_REQ);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100489 if (!mme) {
490 LOGPFSMSL(vsub->sgs_fsm, DPAG, LOGL_ERROR, "Will not Page (no MME)\n");
Harald Welte0df904d2018-12-03 11:00:04 +0100491 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100492 }
Harald Welte0df904d2018-12-03 11:00:04 +0100493
494 /* Check if there is still a paging in progress for this subscriber,
495 * if yes, don't initiate another paging request. */
496 if (vlr_sgs_pag_pend(vsub))
497 return 0;
498
499 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;
611
612 /* Check for lingering connections */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100613 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100614 if (vsub) {
615 subscr_conn_toss(vsub);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100616 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100617 }
618
619 /* Determine MME-Name */
620 mme_name = sgs_mme_fqdn_get(sgc);
621 if (!mme_name) {
622 resp = gsm29118_create_lu_rej(imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, NULL);
623 sgs_tx(sgc, resp);
624 return 0;
625 }
626
627 /* Parse LU-Type */
628 lu_type_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_EPS_LU_TYPE, 1);
629 if (!lu_type_ie)
630 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_EPS_LU_TYPE);
631 if (lu_type_ie[0] == 0x01)
632 type = VLR_LU_TYPE_IMSI_ATTACH;
633 else
634 type = VLR_LU_TYPE_REGULAR;
635
636 /* Parse LAI of the new location */
637 gsm48_lai = (struct gsm48_loc_area_id *)TLVP_VAL_MINLEN(tp, SGSAP_IE_LAI, 5);
638 if (!gsm48_lai)
639 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_LAI);
640 gsm48_decode_lai2(gsm48_lai, &new_lai);
641
642 /* Perform actual location update */
643 memcpy(vlr_sgs_cfg.timer, sgc->sgs->cfg.timer, sizeof(vlr_sgs_cfg.timer));
644 memcpy(vlr_sgs_cfg.counter, sgc->sgs->cfg.counter, sizeof(vlr_sgs_cfg.counter));
645 rc = vlr_sgs_loc_update(gsm_network->vlr, &vlr_sgs_cfg, sgs_tx_loc_upd_resp_cb, sgs_iface_tx_paging,
646 sgs_tx_mm_info_cb, mme_name, type, imsi, &new_lai);
647 if (rc != 0) {
648 resp = gsm29118_create_lu_rej(imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, NULL);
649 sgs_tx(sgc, resp);
650 }
651
652 return 0;
653}
654
655/* SGsAP-IMSI-DETACH-INDICATION 3GPP TS 29.118, chapter 8.8 */
656static int sgs_rx_imsi_det_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
657{
658 struct msgb *resp;
659 enum sgsap_imsi_det_noneps_type type;
660 const uint8_t *type_ie;
661
662 type_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_IMSI_DET_NONEPS_TYPE, 1);
663 if (!type_ie)
664 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_IMSI_DET_NONEPS_TYPE);
665
666 switch (type_ie[0]) {
667 case SGSAP_ID_NONEPS_T_EXPLICIT_UE_NONEPS:
668 type = SGSAP_ID_NONEPS_T_EXPLICIT_UE_NONEPS;
669 break;
670 case SGSAP_ID_NONEPS_T_COMBINED_UE_EPS_NONEPS:
671 type = SGSAP_ID_NONEPS_T_COMBINED_UE_EPS_NONEPS;
672 break;
673 case SGSAP_ID_NONEPS_T_IMPLICIT_UE_EPS_NONEPS:
674 type = SGSAP_ID_NONEPS_T_IMPLICIT_UE_EPS_NONEPS;
675 break;
676 default:
677 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_INVALID_MAND_IE, msg, SGSAP_IE_IMSI_DET_NONEPS_TYPE);
678 break;
679 }
680
681 vlr_sgs_imsi_detach(gsm_network->vlr, imsi, type);
682 resp = gsm29118_create_imsi_det_ack(imsi);
683 sgs_tx(sgc, resp);
684
685 return 0;
686}
687
688/* SGsAP-EPS-DETACH-INDICATION 3GPP TS 29.118, chapter 8.6 */
689static int sgs_rx_eps_det_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
690{
691 struct msgb *resp;
692 enum sgsap_imsi_det_eps_type type;
693 const uint8_t *type_ie;
694
695 type_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_IMSI_DET_EPS_TYPE, 1);
696 if (!type_ie)
697 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_IMSI_DET_EPS_TYPE);
698
699 switch (type_ie[0]) {
700 case SGSAP_ID_EPS_T_NETWORK_INITIATED:
701 type = SGSAP_ID_EPS_T_NETWORK_INITIATED;
702 break;
703 case SGSAP_ID_EPS_T_UE_INITIATED:
704 type = SGSAP_ID_EPS_T_UE_INITIATED;
705 break;
706 case SGSAP_ID_EPS_T_EPS_NOT_ALLOWED:
707 type = SGSAP_ID_EPS_T_EPS_NOT_ALLOWED;
708 break;
709 default:
710 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_INVALID_MAND_IE, msg, SGSAP_IE_IMSI_DET_EPS_TYPE);
711 break;
712 }
713
714 vlr_sgs_eps_detach(gsm_network->vlr, imsi, type);
715 resp = gsm29118_create_eps_det_ack(imsi);
716 sgs_tx(sgc, resp);
717
718 return 0;
719}
720
721/* SGsAP-PAGING-REJECT 3GPP TS 29.118, chapter 8.13 */
722static int sgs_rx_pag_rej(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
723{
724 int cause;
725 struct vlr_subscr *vsub;
726
727 cause = sgs_cause_from_msg(sgc, msg, tp, NULL);
728 if (cause < 0)
729 return 0;
730
731 /* Subscriber must be known by the VLR */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100732 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100733 if (!vsub)
734 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, msg, SGSAP_IE_IMSI);
735
736 /* Inform the VLR */
737 vlr_sgs_pag_rej(gsm_network->vlr, imsi, cause);
738
739 /* Stop all paging activity */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100740 paging_expired(vsub);
Harald Welte0df904d2018-12-03 11:00:04 +0100741
742 /* Depending on the cause code some action is required */
743 if (cause == SGSAP_SGS_CAUSE_MT_CSFB_REJ_USER) {
744 /* FIXME: We are supposed to trigger a User Determined User Busy (UDUB)
745 * as specified in 3GPP TS 24.082 here, SGs association state shall not
746 * be changed */
747 LOGSGC(sgc, LOGL_ERROR,
748 "Rx %s with SGSAP_SGS_CAUSE_MT_CSFB_REJ_USER, but sending UDUP is not implemented yet!\n",
749 sgsap_msg_type_name(msg->data[0]));
750 } else if (cause == SGSAP_SGS_CAUSE_IMSI_DET_EPS) {
751 /* FIXME: In this case we should send the paging via A/Iu interface */
752 OSMO_ASSERT(false);
753 }
754
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100755 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100756 return 0;
757}
758
759/* SGsAP-UE-UNREACHABLE 3GPP TS 29.118, chapter 8.21 */
760static int sgs_rx_ue_unr(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
761{
762 int cause;
763
764 cause = sgs_cause_from_msg(sgc, msg, tp, NULL);
765 if (cause < 0)
766 return 0;
767
768 vlr_sgs_ue_unr(gsm_network->vlr, imsi, cause);
769
770 return 0;
771}
772
773/* SGsAP-TMSI-REALLOCATION-COMPLETE 3GPP TS 29.118, chapter 8.19 */
774static int sgs_rx_tmsi_reall_cmpl(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
775{
776 vlr_sgs_tmsi_reall_compl(gsm_network->vlr, imsi);
777 return 0;
778}
779
780/* SGsAP-SERVICE-REQUEST 3GPP TS 29.118, chapter 8.17 */
781static int sgs_rx_service_req(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
782{
783 enum sgsap_service_ind serv_ind;
784 const uint8_t *serv_ind_ie;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100785 struct msc_a *msc_a;
Harald Welte0df904d2018-12-03 11:00:04 +0100786 struct vlr_subscr *vsub;
787
788 /* Note: While in other RAN concepts a service request is used to
789 * initiate mobile originated operation, the service request in SGsAP
790 * is comparable to a paging response. The SGsAP SERVICE REQUEST must
791 * not be confused or compared with a CM SERVICE REQUEST! */
792
793 if (!check_sgs_association(sgc, msg, imsi))
794 return 0;
795
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100796 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100797 /* Note: vsub is already sufficiently verified by check_sgs_association(),
798 * we must have a vsub at this point! */
799 OSMO_ASSERT(vsub);
800
801 /* The Service request is intended as a paging response, if one is
802 * received while nothing is paging something is very wrong! */
803 if (!vlr_sgs_pag_pend(vsub)) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100804 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100805 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE, msg, -1);
806 }
807 serv_ind_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_SERVICE_INDICATOR, 1);
808
809 if (!serv_ind_ie) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100810 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100811 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_SERVICE_INDICATOR);
812 }
813 if (serv_ind_ie[0] == SGSAP_SERV_IND_CS_CALL)
814 serv_ind = serv_ind_ie[0];
815 else if (serv_ind_ie[0] == SGSAP_SERV_IND_SMS)
816 serv_ind = serv_ind_ie[0];
817 else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100818 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100819 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_INVALID_MAND_IE, msg, SGSAP_IE_SERVICE_INDICATOR);
820 }
821
822 /* FIXME: The MME shall include an UE EMM Mode IE, but the field is
823 * marked optional. (Why do we need this info at all?) */
824
825 /* Report to the VLR that the paging has successfully completed */
826 vlr_sgs_pag_ack(gsm_network->vlr, imsi);
827
828 /* Exit early when the service indicator indicates that a call is being
829 * established. In those cases we do not allocate a connection, instead
830 * the connection will be allocated when the MS is appearing on the
831 * A-Interface. */
832 if (serv_ind == SGSAP_SERV_IND_CS_CALL) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100833 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100834 return 0;
835 }
836
837 /* Allocate subscriber connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100838 msc_a = subscr_conn_allocate_sgs(sgc, vsub, true);
839 if (!msc_a) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100840 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100841 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE, msg, -1);
842 }
843
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100844 /* The msub has added a get() for the vsub, balance above vlr_subscr_find_by_imsi() */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100845 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100846 return 0;
847}
848
849/* SGsAP-UPLINK-UNITDATA 3GPP TS 29.118, chapter 8.22 */
850static int sgs_rx_ul_ud(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
851{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100852 struct msc_a *msc_a;
Harald Welte0df904d2018-12-03 11:00:04 +0100853 const uint8_t *nas_msg_container_ie;
854 struct vlr_subscr *vsub;
855
856 if (!check_sgs_association(sgc, msg, imsi))
857 return 0;
858
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100859 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100860 /* Note: vsub is already sufficiently verified by check_sgs_association(),
861 * we must have a vsub at this point! */
862 OSMO_ASSERT(vsub);
863
864 /* Try to find existing connection (MT) or allocate a new one (MO) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100865 msc_a = msc_a_for_vsub(vsub, true);
866 if (!msc_a)
867 msc_a = subscr_conn_allocate_sgs(sgc, vsub, false);
Harald Welte0df904d2018-12-03 11:00:04 +0100868
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100869 /* Balance above vlr_subscr_find_by_imsi() */
870 vlr_subscr_put(vsub, __func__);
871
Harald Welte0df904d2018-12-03 11:00:04 +0100872 /* If we do not find an existing connection and allocating a new one
Martin Hauke3f07dac2019-11-14 17:49:08 +0100873 * failed, give up and return status. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100874 if (!msc_a)
Harald Welte0df904d2018-12-03 11:00:04 +0100875 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE, msg, 0);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100876
877 if (msc_a->c.ran->type != OSMO_RAT_EUTRAN_SGS) {
878 LOGSGC(sgc, LOGL_ERROR,
879 "Receiving uplink unit-data for non-sgs connection -- discarding message!\n");
880 return -EINVAL;
Harald Welte0df904d2018-12-03 11:00:04 +0100881 }
882
883 nas_msg_container_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_NAS_MSG_CONTAINER, 1);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100884 if (!nas_msg_container_ie)
Harald Welte0df904d2018-12-03 11:00:04 +0100885 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 +0100886
887 /* ran_conn_dtap expects the dtap payload in l3h */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100888 msg->l3h = (uint8_t *)nas_msg_container_ie;
889 msc_a_up_l3(msc_a, msg);
Harald Welte0df904d2018-12-03 11:00:04 +0100890
Harald Welte0df904d2018-12-03 11:00:04 +0100891 return 0;
892}
893
894/* SGsAP-MO-CSFB-INDICATION, chapter 8.25 */
895static int sgs_rx_csfb_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
896{
897 struct vlr_subscr *vsub;
898
Philipp Maier25e7ba52019-08-15 14:51:49 +0200899 /* The MME informs us with this message that the UE has initiated a
900 * service request for MO CS fallback. There is not much we can do with
901 * this information, however, we can check if the subscriber actually
902 * exists in the VLR and if there are any lingering connections open.*/
Harald Welte0df904d2018-12-03 11:00:04 +0100903
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100904 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100905 if (!vsub)
906 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, msg, SGSAP_IE_IMSI);
907
908 /* Check for lingering connections */
909 subscr_conn_toss(vsub);
910
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100911 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100912 return 0;
913}
914
915/* SGsAP-UE-ACTIVITY-INDICATION, chapter 8.20 */
916static int sgs_rx_ue_act_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
917{
918 /* In this MSC/VLR implementation we do not support the alerting
919 * procedure yet and therefore we will never request any alerting
920 * at the MME. Given that it is unlikely that we ever get activity
921 * indications from the MME, but if we do we should not act all too
922 * hostile and ignore the indication silently. */
923
924 LOGSGC(sgc, LOGL_ERROR, "Rx %s unexpected, we do not implement alerting yet, ignoring!\n",
925 sgsap_msg_type_name(msg->data[0]));
926
927 return 0;
928}
929
930#define TX_STATUS_AND_LOG(sgc, msg_type, cause, fmt) \
931 LOGSGC(sgc, LOGL_ERROR, fmt, sgsap_msg_type_name(msg_type)); \
932 resp = gsm29118_create_status(NULL, cause, msg); \
933 sgs_tx(sgc, resp); \
934
935/*! Process incoming SGs message (see sgs_server.c)
936 * \param[in] sgc related sgs connection
937 * \param[in] msg received message
938 * \returns 0 in case of success, -EINVAL in case of error. */
939int sgs_iface_rx(struct sgs_connection *sgc, struct msgb *msg)
940{
941 struct msgb *resp;
942 uint8_t msg_type = msg->l2h[0];
943 struct tlv_parsed tp;
944 int rc;
945 char imsi[GSM48_MI_SIZE];
946 char mme_name[SGS_MME_NAME_LEN + 1];
947
948 memset(imsi, 0, sizeof(imsi));
949 memset(mme_name, 0, sizeof(mme_name));
950
951 /* When the receiving entity receives a message that is too short to contain a complete
952 * message type information element, the receiving entity shall ignore that message. */
953 if (msgb_l2len(msg) < 1)
954 goto error;
955
956 /* Parse TLV elements */
957 rc = tlv_parse(&tp, &sgsap_ie_tlvdef, msgb_l2(msg) + 1, msgb_l2len(msg) - 1, 0, 0);
Philipp Maier6dcdfb02019-09-24 11:44:28 +0200958 if (rc < 0)
959 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 +0100960
961 /* Most of the messages contain an IMSI as mandatory IE, parse it right here */
962 if (!TLVP_PRESENT(&tp, SGSAP_IE_IMSI) &&
963 msg_type != SGSAP_MSGT_STATUS && msg_type != SGSAP_MSGT_RESET_IND && msg_type != SGSAP_MSGT_RESET_ACK) {
964 /* reject the message; all but the three above have mandatory IMSI */
965 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_MISSING_MAND_IE,
966 "SGsAP Message %s without IMSI, dropping\n");
967 goto error;
968 }
969
970 if (TLVP_PRESENT(&tp, SGSAP_IE_IMSI)) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200971 struct osmo_mobile_identity mi;
972 if (osmo_mobile_identity_decode(&mi,
973 TLVP_VAL(&tp, SGSAP_IE_IMSI),
974 TLVP_LEN(&tp, SGSAP_IE_IMSI), false)
975 || mi.type != GSM_MI_TYPE_IMSI) {
Harald Welte0df904d2018-12-03 11:00:04 +0100976 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_INVALID_MAND_IE,
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200977 "SGsAP Message %s with invalid IMSI, dropping\n");
Harald Welte0df904d2018-12-03 11:00:04 +0100978 goto error;
979 }
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200980 OSMO_STRLCPY_ARRAY(imsi, mi.imsi);
Harald Welte0df904d2018-12-03 11:00:04 +0100981 }
982
983 /* Some messages contain an MME-NAME as mandatore IE, parse it right here. The
984 * MME-NAME is als immediately registered with the sgc, so it will be implicitly
985 * known to all functions that have access to the sgc context. */
986 if (!TLVP_PRESENT(&tp, SGSAP_IE_MME_NAME)
987 && (msg_type == SGSAP_MSGT_RESET_IND || msg_type == SGSAP_MSGT_RESET_ACK
988 || msg_type == SGSAP_MSGT_LOC_UPD_REQ || msg_type == SGSAP_MSGT_IMSI_DET_IND
989 || msg_type == SGSAP_MSGT_EPS_DET_IND)) {
990 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_MISSING_MAND_IE,
991 "SGsAP Message %s without MME-Name, dropping\n");
992 goto error;
993 }
994
995 if (TLVP_PRESENT(&tp, SGSAP_IE_MME_NAME)) {
996 if (decode_mme_name(mme_name, &tp) != 0) {
997 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_INVALID_MAND_IE,
998 "SGsAP Message %s with invalid MME-Name, dropping\n");
999 goto error;
1000 }
1001 /* Regsister/check mme_name with sgc */
1002 if (sgs_mme_fqdn_received(sgc, mme_name) < 0) {
1003 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE,
1004 "SGsAP Message %s with invalid MME-Name, dropping\n");
1005 goto error;
1006 }
1007 }
1008
1009 /* dispatch msg to various handler functions. msgb ownership remains here! */
1010 rc = -EINVAL;
1011 switch (msg_type) {
1012 case SGSAP_MSGT_STATUS:
1013 rc = sgs_rx_status(sgc, msg, &tp, imsi);
1014 break;
1015 case SGSAP_MSGT_RESET_IND:
1016 rc = sgs_rx_reset_ind(sgc, msg, &tp);
1017 break;
1018 case SGSAP_MSGT_RESET_ACK:
1019 rc = sgs_rx_reset_ack(sgc, msg, &tp);
1020 break;
1021 case SGSAP_MSGT_LOC_UPD_REQ:
1022 rc = sgs_rx_loc_upd_req(sgc, msg, &tp, imsi);
1023 break;
1024 case SGSAP_MSGT_IMSI_DET_IND:
1025 rc = sgs_rx_imsi_det_ind(sgc, msg, &tp, imsi);
1026 break;
1027 case SGSAP_MSGT_EPS_DET_IND:
1028 rc = sgs_rx_eps_det_ind(sgc, msg, &tp, imsi);
1029 break;
1030 case SGSAP_MSGT_PAGING_REJ:
1031 rc = sgs_rx_pag_rej(sgc, msg, &tp, imsi);
1032 break;
1033 case SGSAP_MSGT_UE_UNREACHABLE:
1034 rc = sgs_rx_ue_unr(sgc, msg, &tp, imsi);
1035 break;
1036 case SGSAP_MSGT_TMSI_REALL_CMPL:
1037 rc = sgs_rx_tmsi_reall_cmpl(sgc, msg, &tp, imsi);
1038 break;
1039 case SGSAP_MSGT_SERVICE_REQ:
1040 rc = sgs_rx_service_req(sgc, msg, &tp, imsi);
1041 break;
1042 case SGSAP_MSGT_UL_UD:
1043 rc = sgs_rx_ul_ud(sgc, msg, &tp, imsi);
1044 break;
1045 case SGSAP_MSGT_MO_CSFB_IND:
1046 rc = sgs_rx_csfb_ind(sgc, msg, &tp, imsi);
1047 break;
1048 case SGSAP_MSGT_UE_ACT_IND:
1049 rc = sgs_rx_ue_act_ind(sgc, msg, &tp, imsi);
1050 break;
1051 case SGSAP_MSGT_ALERT_ACK:
1052 case SGSAP_MSGT_ALERT_REJ:
1053 LOGSGC(sgc, LOGL_ERROR, "Rx unmplemented SGsAP %s: %s\n",
1054 sgsap_msg_type_name(msg_type), msgb_hexdump(msg));
1055 resp = gsm29118_create_status(imsi, SGSAP_SGS_CAUSE_MSG_UNKNOWN, msg);
1056 sgs_tx(sgc, resp);
1057 rc = 0;
1058 break;
1059 default:
1060 LOGSGC(sgc, LOGL_ERROR, "Rx unknown SGsAP message type 0x%02x: %s\n", msg_type, msgb_hexdump(msg));
1061 resp = gsm29118_create_status(imsi, SGSAP_SGS_CAUSE_MSG_UNKNOWN, msg);
1062 sgs_tx(sgc, resp);
1063 rc = 0;
1064 break;
1065 }
1066
1067 /* Catch unhandled errors */
1068 if (rc < 0) {
1069 /* Note: Usually the sgs_rx_ should catch errors locally and
1070 * eimit a status message with proper cause code, including
1071 * a suitable log message. If we end up here, something is
1072 * not right and should be fixed */
1073 LOGSGC(sgc, LOGL_ERROR, "Rx unable to decode SGsAP %s: %s\n",
1074 sgsap_msg_type_name(msg_type), msgb_hexdump(msg));
1075 resp = gsm29118_create_status(imsi, SGSAP_SGS_CAUSE_MSG_UNKNOWN, msg);
1076 sgs_tx(sgc, resp);
1077 }
1078
1079error:
1080 msgb_free(msg);
1081 return 0;
1082}
1083
1084/***********************************************************************
1085 * SGs connection "VLR Reset Procedure" FSM
1086 ***********************************************************************/
1087
1088static const struct value_string sgs_vlr_reset_fsm_event_names[] = {
1089 {SGS_VLRR_E_START_RESET, "START-RESET"},
1090 {SGS_VLRR_E_RX_RESET_ACK, "RX-RESET-ACK"},
1091 {0, NULL}
1092};
1093
1094static void sgs_vlr_reset_fsm_null(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1095{
1096 switch (event) {
1097 case SGS_VLRR_E_RX_RESET_ACK:
1098 break;
1099 default:
1100 OSMO_ASSERT(0);
1101 break;
1102 }
1103}
1104
1105static void sgs_vlr_reset_fsm_wait_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1106{
1107 switch (event) {
1108 case SGS_VLRR_E_RX_RESET_ACK:
1109 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_COMPLETE, 0, 0);
1110 break;
1111 default:
1112 OSMO_ASSERT(0);
1113 break;
1114 }
1115}
1116
1117static void sgs_vlr_reset_fsm_complete(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1118{
1119 switch (event) {
1120 case SGS_VLRR_E_RX_RESET_ACK:
1121 break;
1122 default:
1123 OSMO_ASSERT(0);
1124 break;
1125 }
1126}
1127
1128static void sgs_vlr_reset_fsm_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1129{
1130 struct msgb *reset_ind;
1131 struct gsm29118_reset_msg reset_params;
1132 struct sgs_mme_ctx *mme = (struct sgs_mme_ctx *)fi->priv;
1133 struct sgs_connection *sgc = mme->conn;
1134 struct sgs_state *sgs = mme->sgs;
1135
1136 switch (event) {
1137 case SGS_VLRR_E_START_RESET:
1138 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_NULL, 0, 0);
1139 mme->ns11_remaining = sgs->cfg.counter[SGS_STATE_NS11];
1140 /* send a reset message and enter WAIT_ACK state */
1141 memset(&reset_params, 0, sizeof(reset_params));
1142 osmo_strlcpy(reset_params.vlr_name, sgs->cfg.vlr_name, sizeof(reset_params.vlr_name));
1143 reset_params.vlr_name_present = true;
1144 reset_ind = gsm29118_create_reset_ind(&reset_params);
1145 sgs_tx(sgc, reset_ind);
Philipp Maier483cea82019-04-03 16:23:29 +02001146
1147 /* Perform a reset of the SGS FSM of all subscribers that are present in the VLR */
1148 vlr_sgs_reset(gsm_network->vlr);
1149
Harald Welte0df904d2018-12-03 11:00:04 +01001150 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_WAIT_ACK, sgs->cfg.timer[SGS_STATE_TS11], 11);
1151 break;
1152 default:
1153 OSMO_ASSERT(0);
1154 break;
1155 }
1156}
1157
1158static int sgs_vlr_reset_fsm_timer_cb(struct osmo_fsm_inst *fi)
1159{
1160 struct msgb *reset_ind;
1161 struct gsm29118_reset_msg reset_params;
1162 struct sgs_mme_ctx *mme = (struct sgs_mme_ctx *)fi->priv;
1163 struct sgs_connection *sgc = mme->conn;
1164 struct sgs_state *sgs = mme->sgs;
1165
1166 switch (fi->T) {
1167 case 11:
1168 if (mme->ns11_remaining >= 1) {
1169 memset(&reset_params, 0, sizeof(reset_params));
1170 osmo_strlcpy(reset_params.vlr_name, sgs->cfg.vlr_name, sizeof(reset_params.vlr_name));
1171 reset_params.vlr_name_present = true;
1172 reset_ind = gsm29118_create_reset_ind(&reset_params);
1173 sgs_tx(sgc, reset_ind);
1174 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_WAIT_ACK, sgs->cfg.timer[SGS_STATE_TS11], 11);
1175 mme->ns11_remaining--;
1176 } else {
1177 LOGMME(mme, LOGL_ERROR, "Ts11 expired more than %u (Ns11) times, giving up\n",
Vadim Yanitskiye9ef7c62019-02-23 16:04:17 +07001178 sgs->cfg.counter[SGS_STATE_NS11]);
Harald Welte0df904d2018-12-03 11:00:04 +01001179 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_NULL, 0, 0);
1180 }
1181 break;
1182 default:
1183 OSMO_ASSERT(0);
1184 break;
1185 }
1186 return 0;
1187}
1188
1189static const struct osmo_fsm_state sgs_vlr_reset_fsm_states[] = {
1190 [SGS_VLRR_ST_NULL] = {
1191 /* We haven't even tried yet to send a RESET */
1192 .name = "NULL",
1193 .action = sgs_vlr_reset_fsm_null,
1194 .in_event_mask = S(SGS_VLRR_E_RX_RESET_ACK),
1195 .out_state_mask = S(SGS_VLRR_ST_NULL) | S(SGS_VLRR_ST_WAIT_ACK),
1196 },
1197 [SGS_VLRR_ST_WAIT_ACK] = {
1198 /* We're waiting for a SGsAP_RESET_ACK */
1199 .name = "WAIT-ACK",
1200 .action = sgs_vlr_reset_fsm_wait_ack,
1201 .in_event_mask = S(SGS_VLRR_E_RX_RESET_ACK),
1202 .out_state_mask = S(SGS_VLRR_ST_NULL) |
1203 S(SGS_VLRR_ST_COMPLETE) | S(SGS_VLRR_ST_WAIT_ACK),
1204 },
1205 [SGS_VLRR_ST_COMPLETE] = {
1206 /* Reset procedure to this MME has been completed */
1207 .name = "COMPLETE",
1208 .action = sgs_vlr_reset_fsm_complete,
1209 .in_event_mask = S(SGS_VLRR_E_RX_RESET_ACK),
1210 .out_state_mask = S(SGS_VLRR_ST_NULL) | S(SGS_VLRR_ST_COMPLETE),
1211 },
1212};
1213
1214static struct osmo_fsm sgs_vlr_reset_fsm = {
1215 .name = "SGs-VLR-RESET",
1216 .states = sgs_vlr_reset_fsm_states,
Philipp Maier483cea82019-04-03 16:23:29 +02001217 .num_states = ARRAY_SIZE(sgs_vlr_reset_fsm_states),
Harald Welte0df904d2018-12-03 11:00:04 +01001218 .allstate_event_mask = S(SGS_VLRR_E_START_RESET),
1219 .allstate_action = sgs_vlr_reset_fsm_allstate,
1220 .timer_cb = sgs_vlr_reset_fsm_timer_cb,
1221 .log_subsys = DSGS,
1222 .event_names = sgs_vlr_reset_fsm_event_names,
1223};
1224
1225/*! Send unit-data through SGs interface (see msc_ifaces.c)
1226 * \param[in] msg layer 3 message to send.
1227 * \returns 0 in case of success, -EINVAL in case of error. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001228int sgs_iface_tx_dtap_ud(struct msc_a *msc_a, struct msgb *msg)
Harald Welte0df904d2018-12-03 11:00:04 +01001229{
Harald Welte0df904d2018-12-03 11:00:04 +01001230 struct msgb *msg_sgs;
1231 struct sgs_mme_ctx *mme;
1232 int rc = -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001233 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Harald Welte0df904d2018-12-03 11:00:04 +01001234
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001235 OSMO_ASSERT(vsub);
Harald Welte0df904d2018-12-03 11:00:04 +01001236
1237 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_DL_UD);
1238 if (!mme)
1239 goto error;
1240
1241 /* Make sure the subscriber has a valid SGs association, otherwise
1242 * don't let unit-data through. */
1243 if (vsub->sgs_fsm->state != SGS_UE_ST_ASSOCIATED) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001244 LOG_MSC_A(msc_a, LOGL_NOTICE, "Cannot Tx %s: subscriber not SGs-associated\n",
1245 sgsap_msg_type_name(SGSAP_MSGT_DL_UD));
Harald Welte0df904d2018-12-03 11:00:04 +01001246 goto error;
1247 }
1248
1249 msg_sgs = gsm29118_create_dl_ud(vsub->imsi, msg);
1250 sgs_tx(mme->conn, msg_sgs);
1251 rc = 0;
1252
1253error:
1254 msgb_free(msg);
1255 return rc;
1256}
1257
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001258void sgs_iface_tx_release(struct vlr_subscr *vsub)
Harald Welte0df904d2018-12-03 11:00:04 +01001259{
1260 struct msgb *msg_sgs;
Harald Welte0df904d2018-12-03 11:00:04 +01001261 struct sgs_mme_ctx *mme;
1262
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001263 OSMO_ASSERT(vsub);
Harald Welte0df904d2018-12-03 11:00:04 +01001264
1265 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_DL_UD);
1266 if (!mme)
1267 return;
1268
1269 msg_sgs = gsm29118_create_release_req(vsub->imsi, 0);
1270 sgs_tx(mme->conn, msg_sgs);
1271}
1272
Philipp Maier002fb012019-09-24 09:26:47 +02001273/*! Send SGsAP-SERVICE-ABORT-REQUEST message to MME
1274 * \param[in] vsub subscriber context */
1275void sgs_iface_tx_serv_abrt(struct vlr_subscr *vsub)
1276{
1277 struct msgb *msg_sgs;
1278 struct sgs_mme_ctx *mme;
1279
1280 OSMO_ASSERT(vsub);
1281
1282 /* The service abort procedure is only defined for MT calls,
1283 * see also 3GPP TS 29.118, chapter 5.13.2 */
1284 if (vsub->sgs.paging_serv_ind != SGSAP_SERV_IND_CS_CALL)
1285 return;
1286
1287 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_DL_UD);
1288 if (!mme)
1289 return;
1290
1291 msg_sgs = gsm29118_create_service_abort_req(vsub->imsi);
1292 sgs_tx(mme->conn, msg_sgs);
1293}
1294
Martin Hauke3f07dac2019-11-14 17:49:08 +01001295/*! initialize SGs new interface
Harald Welte0df904d2018-12-03 11:00:04 +01001296 * \param[in] ctx talloc context
1297 * \param[in] network associated gsm network
1298 * \returns returns allocated sgs_stae, NULL in case of error. */
1299struct sgs_state *sgs_iface_init(void *ctx, struct gsm_network *network)
1300{
1301 struct sgs_state *sgs;
1302
1303 gsm_network = network;
1304
1305 sgs = sgs_server_alloc(ctx);
1306 OSMO_ASSERT(sgs);
1307
1308 /* We currently only support one SGs instance */
1309 if (g_sgs)
1310 return NULL;
1311 g_sgs = sgs;
1312
Harald Welte0df904d2018-12-03 11:00:04 +01001313 return sgs;
1314}
Vadim Yanitskiy4eaefc22019-03-21 20:55:19 +07001315
1316static __attribute__((constructor)) void on_dso_load(void)
1317{
1318 OSMO_ASSERT(osmo_fsm_register(&sgs_vlr_reset_fsm) == 0);
1319}