blob: b792e466f098ae4ff918039f210c94408b6f0767 [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];
375 uint8_t *new_id_ptr = new_id;
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);
406 if (new_id_len > 0)
407 new_id_ptr = new_id;
Harald Welte0df904d2018-12-03 11:00:04 +0100408 }
409 resp = gsm29118_create_lu_ack(vsub->imsi, &vsub->sgs.lai, new_id_ptr, new_id_len);
410 sgs_tx(mme->conn, resp);
411 vlr_sgs_loc_update_acc_sent(vsub);
412 } else {
413 resp = gsm29118_create_lu_rej(vsub->imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, &vsub->sgs.lai);
414 sgs_tx(mme->conn, resp);
415 vlr_sgs_loc_update_rej_sent(vsub);
416 }
417}
418
419/* Called by VLR via callback, transmits MM information to the UE */
420static void sgs_tx_mm_info_cb(struct vlr_subscr *vsub)
421{
422 struct msgb *msg;
423 struct msgb *msg_mm_info;
424 struct sgs_mme_ctx *mme;
425
426 /* The sending of MM information requests is an optional feature and
427 * depends on the network configuration (VTY) */
428 if (!gsm_network->send_mm_info)
429 return;
430
431 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_MM_INFO_REQ);
432 if (!mme)
433 return;
434
435 /* Create and send MM information request message, see also:
436 * 3GPP TS 29.118, chapter 8.12 SGsAP-MM-INFORMATION-REQUEST and
437 * 3GPP TS 29.018, chapter 18.4.16 MM information. */
438 msg_mm_info = gsm48_create_mm_info(gsm_network);
439 msg = gsm29118_create_mm_info_req(vsub->imsi, msg_mm_info->data + 2, msg_mm_info->len - 2);
440 sgs_tx(mme->conn, msg);
441 msgb_free(msg_mm_info);
442}
443
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100444enum sgsap_service_ind sgs_serv_ind_from_paging_cause(enum paging_cause cause)
445{
446 switch (cause) {
447 case PAGING_CAUSE_CALL_CONVERSATIONAL:
448 case PAGING_CAUSE_CALL_STREAMING:
449 case PAGING_CAUSE_CALL_INTERACTIVE:
450 case PAGING_CAUSE_CALL_BACKGROUND:
451 return SGSAP_SERV_IND_CS_CALL;
452
453 case PAGING_CAUSE_UNSPECIFIED:
454 case PAGING_CAUSE_SIGNALLING_LOW_PRIO:
455 case PAGING_CAUSE_SIGNALLING_HIGH_PRIO:
456 return SGSAP_SERV_IND_SMS;
457
458 default:
459 OSMO_ASSERT(false);
460 }
461}
462
Harald Welte0df904d2018-12-03 11:00:04 +0100463/*! Page UE through SGs interface
464 * \param[in] vsub subscriber context
465 * \param[in] serv_ind service indicator (sms or voide)
466 * \returns 0 in case of success, -EINVAL in case of error. */
467int sgs_iface_tx_paging(struct vlr_subscr *vsub, enum sgsap_service_ind serv_ind)
468{
469 struct msgb *resp;
470 struct gsm29118_paging_req paging_params;
471 struct sgs_mme_ctx *mme;
472
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100473 LOGP(DMSC, LOGL_NOTICE, "XXXXXXXXXX state == %d conf_by_radio_contact_ind == %d\n",
474 vsub->sgs_fsm->state, vsub->conf_by_radio_contact_ind);
475
Harald Welte0df904d2018-12-03 11:00:04 +0100476 /* See also: 3GPP TS 29.118, chapter 5.1.2.2 Paging Initiation */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100477 if (vsub->sgs_fsm->state == SGS_UE_ST_NULL && vsub->conf_by_radio_contact_ind == true) {
478 LOGPFSMSL(vsub->sgs_fsm, DPAG, LOGL_ERROR, "Will not Page (conf_by_radio_contact_ind == true)\n");
Harald Welte0df904d2018-12-03 11:00:04 +0100479 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100480 }
Harald Welte0df904d2018-12-03 11:00:04 +0100481
482 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_PAGING_REQ);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100483 if (!mme) {
484 LOGPFSMSL(vsub->sgs_fsm, DPAG, LOGL_ERROR, "Will not Page (no MME)\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 /* Check if there is still a paging in progress for this subscriber,
489 * if yes, don't initiate another paging request. */
490 if (vlr_sgs_pag_pend(vsub))
491 return 0;
492
493 memset(&paging_params, 0, sizeof(paging_params));
494 osmo_strlcpy(paging_params.imsi, vsub->imsi, sizeof(paging_params.imsi));
495 osmo_strlcpy(paging_params.vlr_name, mme->sgs->cfg.vlr_name, sizeof(paging_params.vlr_name));
496 paging_params.serv_ind = serv_ind;
497 if (vsub->conf_by_radio_contact_ind == true) {
498 memcpy(&paging_params.lai, &vsub->sgs.lai, sizeof(paging_params.lai));
499 paging_params.lai_present = true;
500 }
501 resp = gsm29118_create_paging_req(&paging_params);
502 sgs_tx(mme->conn, resp);
503
504 /* FIXME: If we are in SGS_UE_ST_NULL while sub->conf_by_radio_contact_ind == false,
505 * we are supposed to start a search procedure as defined in 3GPP TS 23.018 */
506
507 /* Inform the VLR that a paging via SGs is in progress */
508 vlr_sgs_pag(vsub, serv_ind);
509
510 /* Return a page count of 1 (success) */
511 return 1;
512}
513
514/***********************************************************************
515 * SGs incoming messages from the MME
516 ***********************************************************************/
517
518/* Safely read out the SGs cause code from a given message/tlv set, send status
519 * message in case the cause code is invalid or missing. */
520static int sgs_cause_from_msg(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp,
521 const char *imsi)
522{
523 enum sgsap_sgs_cause cause;
524 const uint8_t *cause_ptr;
525 cause_ptr = TLVP_VAL_MINLEN(tp, SGSAP_IE_SGS_CAUSE, 1);
526 if (!cause_ptr) {
527 sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_SGS_CAUSE);
528 return -1;
529 } else
530 cause = *cause_ptr;
531 return cause;
532}
533
534/* SGsAP-STATUS 3GPP TS 29.118, chapter 8.18 */
535static int sgs_rx_status(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, const char *imsi)
536{
537 int cause;
538 const uint8_t *err_msg;
539 const char *imsi_ptr;
540 char *err_msg_hex = "(none)";
541
542 cause = sgs_cause_from_msg(sgc, msg, tp, NULL);
543 if (cause < 0)
544 return 0;
545
546 if (imsi[0] != '\0')
547 imsi_ptr = imsi;
548 else
549 imsi_ptr = "<none>";
550
551 if (TLVP_PRESENT(tp, SGSAP_IE_ERR_MSG))
552 err_msg = TLVP_VAL(tp, SGSAP_IE_ERR_MSG);
553 else
554 err_msg = NULL;
555
556 if (err_msg)
557 err_msg_hex = osmo_hexdump(err_msg, TLVP_LEN(tp, SGSAP_IE_ERR_MSG));
558
559 LOGSGC(sgc, LOGL_NOTICE, "Rx STATUS cause=%s, IMSI=%s, err_msg=%s\n",
560 sgsap_sgs_cause_name(cause), imsi_ptr, err_msg_hex);
561
562 return 0;
563}
564
565/* SGsAP-RESET-INDICATION 3GPP TS 29.118, chapter 8.16 */
566static int sgs_rx_reset_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp)
567{
568 struct gsm29118_reset_msg reset_params;
569 struct msgb *resp;
570
571 memset(&reset_params, 0, sizeof(reset_params));
572 osmo_strlcpy(reset_params.vlr_name, sgc->sgs->cfg.vlr_name, sizeof(reset_params.vlr_name));
573 reset_params.vlr_name_present = true;
574
575 resp = gsm29118_create_reset_ack(&reset_params);
576
577 /* Perform a reset of the SGS FSM of all subscribers that are present in the VLR */
578 vlr_sgs_reset(gsm_network->vlr);
579
580 sgs_tx(sgc, resp);
581 return 0;
582}
583
584/* SGsAP-RESET-ACK 3GPP TS 29.118, chapter 8.15 */
585static int sgs_rx_reset_ack(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp)
586{
587 /* dispatch event to VLR reset FSM for this MME */
588 if (sgc->mme && sgc->mme->fi)
589 osmo_fsm_inst_dispatch(sgc->mme->fi, SGS_VLRR_E_RX_RESET_ACK, msg);
590 return 0;
591}
592
593/* SGsAP-LOCATION-UPDATE-REQUEST 3GPP TS 29.118, chapter 8.11 */
594static int sgs_rx_loc_upd_req(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
595{
596 struct msgb *resp;
597 const uint8_t *lu_type_ie;
598 enum vlr_lu_type type;
599 struct osmo_location_area_id new_lai;
600 const struct gsm48_loc_area_id *gsm48_lai;
601 int rc;
602 char *mme_name;
603 struct vlr_sgs_cfg vlr_sgs_cfg;
604 struct vlr_subscr *vsub;
605
606 /* Check for lingering connections */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100607 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100608 if (vsub) {
609 subscr_conn_toss(vsub);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100610 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100611 }
612
613 /* Determine MME-Name */
614 mme_name = sgs_mme_fqdn_get(sgc);
615 if (!mme_name) {
616 resp = gsm29118_create_lu_rej(imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, NULL);
617 sgs_tx(sgc, resp);
618 return 0;
619 }
620
621 /* Parse LU-Type */
622 lu_type_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_EPS_LU_TYPE, 1);
623 if (!lu_type_ie)
624 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_EPS_LU_TYPE);
625 if (lu_type_ie[0] == 0x01)
626 type = VLR_LU_TYPE_IMSI_ATTACH;
627 else
628 type = VLR_LU_TYPE_REGULAR;
629
630 /* Parse LAI of the new location */
631 gsm48_lai = (struct gsm48_loc_area_id *)TLVP_VAL_MINLEN(tp, SGSAP_IE_LAI, 5);
632 if (!gsm48_lai)
633 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_LAI);
634 gsm48_decode_lai2(gsm48_lai, &new_lai);
635
636 /* Perform actual location update */
637 memcpy(vlr_sgs_cfg.timer, sgc->sgs->cfg.timer, sizeof(vlr_sgs_cfg.timer));
638 memcpy(vlr_sgs_cfg.counter, sgc->sgs->cfg.counter, sizeof(vlr_sgs_cfg.counter));
639 rc = vlr_sgs_loc_update(gsm_network->vlr, &vlr_sgs_cfg, sgs_tx_loc_upd_resp_cb, sgs_iface_tx_paging,
640 sgs_tx_mm_info_cb, mme_name, type, imsi, &new_lai);
641 if (rc != 0) {
642 resp = gsm29118_create_lu_rej(imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, NULL);
643 sgs_tx(sgc, resp);
644 }
645
646 return 0;
647}
648
649/* SGsAP-IMSI-DETACH-INDICATION 3GPP TS 29.118, chapter 8.8 */
650static int sgs_rx_imsi_det_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
651{
652 struct msgb *resp;
653 enum sgsap_imsi_det_noneps_type type;
654 const uint8_t *type_ie;
655
656 type_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_IMSI_DET_NONEPS_TYPE, 1);
657 if (!type_ie)
658 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_IMSI_DET_NONEPS_TYPE);
659
660 switch (type_ie[0]) {
661 case SGSAP_ID_NONEPS_T_EXPLICIT_UE_NONEPS:
662 type = SGSAP_ID_NONEPS_T_EXPLICIT_UE_NONEPS;
663 break;
664 case SGSAP_ID_NONEPS_T_COMBINED_UE_EPS_NONEPS:
665 type = SGSAP_ID_NONEPS_T_COMBINED_UE_EPS_NONEPS;
666 break;
667 case SGSAP_ID_NONEPS_T_IMPLICIT_UE_EPS_NONEPS:
668 type = SGSAP_ID_NONEPS_T_IMPLICIT_UE_EPS_NONEPS;
669 break;
670 default:
671 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_INVALID_MAND_IE, msg, SGSAP_IE_IMSI_DET_NONEPS_TYPE);
672 break;
673 }
674
675 vlr_sgs_imsi_detach(gsm_network->vlr, imsi, type);
676 resp = gsm29118_create_imsi_det_ack(imsi);
677 sgs_tx(sgc, resp);
678
679 return 0;
680}
681
682/* SGsAP-EPS-DETACH-INDICATION 3GPP TS 29.118, chapter 8.6 */
683static int sgs_rx_eps_det_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
684{
685 struct msgb *resp;
686 enum sgsap_imsi_det_eps_type type;
687 const uint8_t *type_ie;
688
689 type_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_IMSI_DET_EPS_TYPE, 1);
690 if (!type_ie)
691 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_IMSI_DET_EPS_TYPE);
692
693 switch (type_ie[0]) {
694 case SGSAP_ID_EPS_T_NETWORK_INITIATED:
695 type = SGSAP_ID_EPS_T_NETWORK_INITIATED;
696 break;
697 case SGSAP_ID_EPS_T_UE_INITIATED:
698 type = SGSAP_ID_EPS_T_UE_INITIATED;
699 break;
700 case SGSAP_ID_EPS_T_EPS_NOT_ALLOWED:
701 type = SGSAP_ID_EPS_T_EPS_NOT_ALLOWED;
702 break;
703 default:
704 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_INVALID_MAND_IE, msg, SGSAP_IE_IMSI_DET_EPS_TYPE);
705 break;
706 }
707
708 vlr_sgs_eps_detach(gsm_network->vlr, imsi, type);
709 resp = gsm29118_create_eps_det_ack(imsi);
710 sgs_tx(sgc, resp);
711
712 return 0;
713}
714
715/* SGsAP-PAGING-REJECT 3GPP TS 29.118, chapter 8.13 */
716static int sgs_rx_pag_rej(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
717{
718 int cause;
719 struct vlr_subscr *vsub;
720
721 cause = sgs_cause_from_msg(sgc, msg, tp, NULL);
722 if (cause < 0)
723 return 0;
724
725 /* Subscriber must be known by the VLR */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100726 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100727 if (!vsub)
728 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, msg, SGSAP_IE_IMSI);
729
730 /* Inform the VLR */
731 vlr_sgs_pag_rej(gsm_network->vlr, imsi, cause);
732
733 /* Stop all paging activity */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100734 paging_expired(vsub);
Harald Welte0df904d2018-12-03 11:00:04 +0100735
736 /* Depending on the cause code some action is required */
737 if (cause == SGSAP_SGS_CAUSE_MT_CSFB_REJ_USER) {
738 /* FIXME: We are supposed to trigger a User Determined User Busy (UDUB)
739 * as specified in 3GPP TS 24.082 here, SGs association state shall not
740 * be changed */
741 LOGSGC(sgc, LOGL_ERROR,
742 "Rx %s with SGSAP_SGS_CAUSE_MT_CSFB_REJ_USER, but sending UDUP is not implemented yet!\n",
743 sgsap_msg_type_name(msg->data[0]));
744 } else if (cause == SGSAP_SGS_CAUSE_IMSI_DET_EPS) {
745 /* FIXME: In this case we should send the paging via A/Iu interface */
746 OSMO_ASSERT(false);
747 }
748
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100749 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100750 return 0;
751}
752
753/* SGsAP-UE-UNREACHABLE 3GPP TS 29.118, chapter 8.21 */
754static int sgs_rx_ue_unr(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
755{
756 int cause;
757
758 cause = sgs_cause_from_msg(sgc, msg, tp, NULL);
759 if (cause < 0)
760 return 0;
761
762 vlr_sgs_ue_unr(gsm_network->vlr, imsi, cause);
763
764 return 0;
765}
766
767/* SGsAP-TMSI-REALLOCATION-COMPLETE 3GPP TS 29.118, chapter 8.19 */
768static int sgs_rx_tmsi_reall_cmpl(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
769{
770 vlr_sgs_tmsi_reall_compl(gsm_network->vlr, imsi);
771 return 0;
772}
773
774/* SGsAP-SERVICE-REQUEST 3GPP TS 29.118, chapter 8.17 */
775static int sgs_rx_service_req(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
776{
777 enum sgsap_service_ind serv_ind;
778 const uint8_t *serv_ind_ie;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100779 struct msc_a *msc_a;
Harald Welte0df904d2018-12-03 11:00:04 +0100780 struct vlr_subscr *vsub;
781
782 /* Note: While in other RAN concepts a service request is used to
783 * initiate mobile originated operation, the service request in SGsAP
784 * is comparable to a paging response. The SGsAP SERVICE REQUEST must
785 * not be confused or compared with a CM SERVICE REQUEST! */
786
787 if (!check_sgs_association(sgc, msg, imsi))
788 return 0;
789
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100790 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100791 /* Note: vsub is already sufficiently verified by check_sgs_association(),
792 * we must have a vsub at this point! */
793 OSMO_ASSERT(vsub);
794
795 /* The Service request is intended as a paging response, if one is
796 * received while nothing is paging something is very wrong! */
797 if (!vlr_sgs_pag_pend(vsub)) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100798 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100799 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE, msg, -1);
800 }
801 serv_ind_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_SERVICE_INDICATOR, 1);
802
803 if (!serv_ind_ie) {
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_MISSING_MAND_IE, msg, SGSAP_IE_SERVICE_INDICATOR);
806 }
807 if (serv_ind_ie[0] == SGSAP_SERV_IND_CS_CALL)
808 serv_ind = serv_ind_ie[0];
809 else if (serv_ind_ie[0] == SGSAP_SERV_IND_SMS)
810 serv_ind = serv_ind_ie[0];
811 else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100812 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100813 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_INVALID_MAND_IE, msg, SGSAP_IE_SERVICE_INDICATOR);
814 }
815
816 /* FIXME: The MME shall include an UE EMM Mode IE, but the field is
817 * marked optional. (Why do we need this info at all?) */
818
819 /* Report to the VLR that the paging has successfully completed */
820 vlr_sgs_pag_ack(gsm_network->vlr, imsi);
821
822 /* Exit early when the service indicator indicates that a call is being
823 * established. In those cases we do not allocate a connection, instead
824 * the connection will be allocated when the MS is appearing on the
825 * A-Interface. */
826 if (serv_ind == SGSAP_SERV_IND_CS_CALL) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100827 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100828 return 0;
829 }
830
831 /* Allocate subscriber connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100832 msc_a = subscr_conn_allocate_sgs(sgc, vsub, true);
833 if (!msc_a) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100834 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100835 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE, msg, -1);
836 }
837
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100838 /* The msub has added a get() for the vsub, balance above vlr_subscr_find_by_imsi() */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100839 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100840 return 0;
841}
842
843/* SGsAP-UPLINK-UNITDATA 3GPP TS 29.118, chapter 8.22 */
844static int sgs_rx_ul_ud(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
845{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100846 struct msc_a *msc_a;
Harald Welte0df904d2018-12-03 11:00:04 +0100847 const uint8_t *nas_msg_container_ie;
848 struct vlr_subscr *vsub;
849
850 if (!check_sgs_association(sgc, msg, imsi))
851 return 0;
852
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100853 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100854 /* Note: vsub is already sufficiently verified by check_sgs_association(),
855 * we must have a vsub at this point! */
856 OSMO_ASSERT(vsub);
857
858 /* Try to find existing connection (MT) or allocate a new one (MO) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100859 msc_a = msc_a_for_vsub(vsub, true);
860 if (!msc_a)
861 msc_a = subscr_conn_allocate_sgs(sgc, vsub, false);
Harald Welte0df904d2018-12-03 11:00:04 +0100862
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100863 /* Balance above vlr_subscr_find_by_imsi() */
864 vlr_subscr_put(vsub, __func__);
865
Harald Welte0df904d2018-12-03 11:00:04 +0100866 /* If we do not find an existing connection and allocating a new one
Martin Hauke3f07dac2019-11-14 17:49:08 +0100867 * failed, give up and return status. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100868 if (!msc_a)
Harald Welte0df904d2018-12-03 11:00:04 +0100869 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE, msg, 0);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100870
871 if (msc_a->c.ran->type != OSMO_RAT_EUTRAN_SGS) {
872 LOGSGC(sgc, LOGL_ERROR,
873 "Receiving uplink unit-data for non-sgs connection -- discarding message!\n");
874 return -EINVAL;
Harald Welte0df904d2018-12-03 11:00:04 +0100875 }
876
877 nas_msg_container_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_NAS_MSG_CONTAINER, 1);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100878 if (!nas_msg_container_ie)
Harald Welte0df904d2018-12-03 11:00:04 +0100879 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 +0100880
881 /* ran_conn_dtap expects the dtap payload in l3h */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100882 msg->l3h = (uint8_t *)nas_msg_container_ie;
883 msc_a_up_l3(msc_a, msg);
Harald Welte0df904d2018-12-03 11:00:04 +0100884
Harald Welte0df904d2018-12-03 11:00:04 +0100885 return 0;
886}
887
888/* SGsAP-MO-CSFB-INDICATION, chapter 8.25 */
889static int sgs_rx_csfb_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
890{
891 struct vlr_subscr *vsub;
892
Philipp Maier25e7ba52019-08-15 14:51:49 +0200893 /* The MME informs us with this message that the UE has initiated a
894 * service request for MO CS fallback. There is not much we can do with
895 * this information, however, we can check if the subscriber actually
896 * exists in the VLR and if there are any lingering connections open.*/
Harald Welte0df904d2018-12-03 11:00:04 +0100897
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100898 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100899 if (!vsub)
900 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, msg, SGSAP_IE_IMSI);
901
902 /* Check for lingering connections */
903 subscr_conn_toss(vsub);
904
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100905 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100906 return 0;
907}
908
909/* SGsAP-UE-ACTIVITY-INDICATION, chapter 8.20 */
910static int sgs_rx_ue_act_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
911{
912 /* In this MSC/VLR implementation we do not support the alerting
913 * procedure yet and therefore we will never request any alerting
914 * at the MME. Given that it is unlikely that we ever get activity
915 * indications from the MME, but if we do we should not act all too
916 * hostile and ignore the indication silently. */
917
918 LOGSGC(sgc, LOGL_ERROR, "Rx %s unexpected, we do not implement alerting yet, ignoring!\n",
919 sgsap_msg_type_name(msg->data[0]));
920
921 return 0;
922}
923
924#define TX_STATUS_AND_LOG(sgc, msg_type, cause, fmt) \
925 LOGSGC(sgc, LOGL_ERROR, fmt, sgsap_msg_type_name(msg_type)); \
926 resp = gsm29118_create_status(NULL, cause, msg); \
927 sgs_tx(sgc, resp); \
928
929/*! Process incoming SGs message (see sgs_server.c)
930 * \param[in] sgc related sgs connection
931 * \param[in] msg received message
932 * \returns 0 in case of success, -EINVAL in case of error. */
933int sgs_iface_rx(struct sgs_connection *sgc, struct msgb *msg)
934{
935 struct msgb *resp;
936 uint8_t msg_type = msg->l2h[0];
937 struct tlv_parsed tp;
938 int rc;
939 char imsi[GSM48_MI_SIZE];
940 char mme_name[SGS_MME_NAME_LEN + 1];
941
942 memset(imsi, 0, sizeof(imsi));
943 memset(mme_name, 0, sizeof(mme_name));
944
945 /* When the receiving entity receives a message that is too short to contain a complete
946 * message type information element, the receiving entity shall ignore that message. */
947 if (msgb_l2len(msg) < 1)
948 goto error;
949
950 /* Parse TLV elements */
951 rc = tlv_parse(&tp, &sgsap_ie_tlvdef, msgb_l2(msg) + 1, msgb_l2len(msg) - 1, 0, 0);
Philipp Maier6dcdfb02019-09-24 11:44:28 +0200952 if (rc < 0)
953 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 +0100954
955 /* Most of the messages contain an IMSI as mandatory IE, parse it right here */
956 if (!TLVP_PRESENT(&tp, SGSAP_IE_IMSI) &&
957 msg_type != SGSAP_MSGT_STATUS && msg_type != SGSAP_MSGT_RESET_IND && msg_type != SGSAP_MSGT_RESET_ACK) {
958 /* reject the message; all but the three above have mandatory IMSI */
959 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_MISSING_MAND_IE,
960 "SGsAP Message %s without IMSI, dropping\n");
961 goto error;
962 }
963
964 if (TLVP_PRESENT(&tp, SGSAP_IE_IMSI)) {
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200965 struct osmo_mobile_identity mi;
966 if (osmo_mobile_identity_decode(&mi,
967 TLVP_VAL(&tp, SGSAP_IE_IMSI),
968 TLVP_LEN(&tp, SGSAP_IE_IMSI), false)
969 || mi.type != GSM_MI_TYPE_IMSI) {
Harald Welte0df904d2018-12-03 11:00:04 +0100970 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_INVALID_MAND_IE,
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200971 "SGsAP Message %s with invalid IMSI, dropping\n");
Harald Welte0df904d2018-12-03 11:00:04 +0100972 goto error;
973 }
Neels Hofmeyr46d526a2020-05-29 03:27:50 +0200974 OSMO_STRLCPY_ARRAY(imsi, mi.imsi);
Harald Welte0df904d2018-12-03 11:00:04 +0100975 }
976
977 /* Some messages contain an MME-NAME as mandatore IE, parse it right here. The
978 * MME-NAME is als immediately registered with the sgc, so it will be implicitly
979 * known to all functions that have access to the sgc context. */
980 if (!TLVP_PRESENT(&tp, SGSAP_IE_MME_NAME)
981 && (msg_type == SGSAP_MSGT_RESET_IND || msg_type == SGSAP_MSGT_RESET_ACK
982 || msg_type == SGSAP_MSGT_LOC_UPD_REQ || msg_type == SGSAP_MSGT_IMSI_DET_IND
983 || msg_type == SGSAP_MSGT_EPS_DET_IND)) {
984 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_MISSING_MAND_IE,
985 "SGsAP Message %s without MME-Name, dropping\n");
986 goto error;
987 }
988
989 if (TLVP_PRESENT(&tp, SGSAP_IE_MME_NAME)) {
990 if (decode_mme_name(mme_name, &tp) != 0) {
991 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_INVALID_MAND_IE,
992 "SGsAP Message %s with invalid MME-Name, dropping\n");
993 goto error;
994 }
995 /* Regsister/check mme_name with sgc */
996 if (sgs_mme_fqdn_received(sgc, mme_name) < 0) {
997 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE,
998 "SGsAP Message %s with invalid MME-Name, dropping\n");
999 goto error;
1000 }
1001 }
1002
1003 /* dispatch msg to various handler functions. msgb ownership remains here! */
1004 rc = -EINVAL;
1005 switch (msg_type) {
1006 case SGSAP_MSGT_STATUS:
1007 rc = sgs_rx_status(sgc, msg, &tp, imsi);
1008 break;
1009 case SGSAP_MSGT_RESET_IND:
1010 rc = sgs_rx_reset_ind(sgc, msg, &tp);
1011 break;
1012 case SGSAP_MSGT_RESET_ACK:
1013 rc = sgs_rx_reset_ack(sgc, msg, &tp);
1014 break;
1015 case SGSAP_MSGT_LOC_UPD_REQ:
1016 rc = sgs_rx_loc_upd_req(sgc, msg, &tp, imsi);
1017 break;
1018 case SGSAP_MSGT_IMSI_DET_IND:
1019 rc = sgs_rx_imsi_det_ind(sgc, msg, &tp, imsi);
1020 break;
1021 case SGSAP_MSGT_EPS_DET_IND:
1022 rc = sgs_rx_eps_det_ind(sgc, msg, &tp, imsi);
1023 break;
1024 case SGSAP_MSGT_PAGING_REJ:
1025 rc = sgs_rx_pag_rej(sgc, msg, &tp, imsi);
1026 break;
1027 case SGSAP_MSGT_UE_UNREACHABLE:
1028 rc = sgs_rx_ue_unr(sgc, msg, &tp, imsi);
1029 break;
1030 case SGSAP_MSGT_TMSI_REALL_CMPL:
1031 rc = sgs_rx_tmsi_reall_cmpl(sgc, msg, &tp, imsi);
1032 break;
1033 case SGSAP_MSGT_SERVICE_REQ:
1034 rc = sgs_rx_service_req(sgc, msg, &tp, imsi);
1035 break;
1036 case SGSAP_MSGT_UL_UD:
1037 rc = sgs_rx_ul_ud(sgc, msg, &tp, imsi);
1038 break;
1039 case SGSAP_MSGT_MO_CSFB_IND:
1040 rc = sgs_rx_csfb_ind(sgc, msg, &tp, imsi);
1041 break;
1042 case SGSAP_MSGT_UE_ACT_IND:
1043 rc = sgs_rx_ue_act_ind(sgc, msg, &tp, imsi);
1044 break;
1045 case SGSAP_MSGT_ALERT_ACK:
1046 case SGSAP_MSGT_ALERT_REJ:
1047 LOGSGC(sgc, LOGL_ERROR, "Rx unmplemented SGsAP %s: %s\n",
1048 sgsap_msg_type_name(msg_type), msgb_hexdump(msg));
1049 resp = gsm29118_create_status(imsi, SGSAP_SGS_CAUSE_MSG_UNKNOWN, msg);
1050 sgs_tx(sgc, resp);
1051 rc = 0;
1052 break;
1053 default:
1054 LOGSGC(sgc, LOGL_ERROR, "Rx unknown SGsAP message type 0x%02x: %s\n", 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 }
1060
1061 /* Catch unhandled errors */
1062 if (rc < 0) {
1063 /* Note: Usually the sgs_rx_ should catch errors locally and
1064 * eimit a status message with proper cause code, including
1065 * a suitable log message. If we end up here, something is
1066 * not right and should be fixed */
1067 LOGSGC(sgc, LOGL_ERROR, "Rx unable to decode SGsAP %s: %s\n",
1068 sgsap_msg_type_name(msg_type), msgb_hexdump(msg));
1069 resp = gsm29118_create_status(imsi, SGSAP_SGS_CAUSE_MSG_UNKNOWN, msg);
1070 sgs_tx(sgc, resp);
1071 }
1072
1073error:
1074 msgb_free(msg);
1075 return 0;
1076}
1077
1078/***********************************************************************
1079 * SGs connection "VLR Reset Procedure" FSM
1080 ***********************************************************************/
1081
1082static const struct value_string sgs_vlr_reset_fsm_event_names[] = {
1083 {SGS_VLRR_E_START_RESET, "START-RESET"},
1084 {SGS_VLRR_E_RX_RESET_ACK, "RX-RESET-ACK"},
1085 {0, NULL}
1086};
1087
1088static void sgs_vlr_reset_fsm_null(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1089{
1090 switch (event) {
1091 case SGS_VLRR_E_RX_RESET_ACK:
1092 break;
1093 default:
1094 OSMO_ASSERT(0);
1095 break;
1096 }
1097}
1098
1099static void sgs_vlr_reset_fsm_wait_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1100{
1101 switch (event) {
1102 case SGS_VLRR_E_RX_RESET_ACK:
1103 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_COMPLETE, 0, 0);
1104 break;
1105 default:
1106 OSMO_ASSERT(0);
1107 break;
1108 }
1109}
1110
1111static void sgs_vlr_reset_fsm_complete(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1112{
1113 switch (event) {
1114 case SGS_VLRR_E_RX_RESET_ACK:
1115 break;
1116 default:
1117 OSMO_ASSERT(0);
1118 break;
1119 }
1120}
1121
1122static void sgs_vlr_reset_fsm_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1123{
1124 struct msgb *reset_ind;
1125 struct gsm29118_reset_msg reset_params;
1126 struct sgs_mme_ctx *mme = (struct sgs_mme_ctx *)fi->priv;
1127 struct sgs_connection *sgc = mme->conn;
1128 struct sgs_state *sgs = mme->sgs;
1129
1130 switch (event) {
1131 case SGS_VLRR_E_START_RESET:
1132 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_NULL, 0, 0);
1133 mme->ns11_remaining = sgs->cfg.counter[SGS_STATE_NS11];
1134 /* send a reset message and enter WAIT_ACK state */
1135 memset(&reset_params, 0, sizeof(reset_params));
1136 osmo_strlcpy(reset_params.vlr_name, sgs->cfg.vlr_name, sizeof(reset_params.vlr_name));
1137 reset_params.vlr_name_present = true;
1138 reset_ind = gsm29118_create_reset_ind(&reset_params);
1139 sgs_tx(sgc, reset_ind);
Philipp Maier483cea82019-04-03 16:23:29 +02001140
1141 /* Perform a reset of the SGS FSM of all subscribers that are present in the VLR */
1142 vlr_sgs_reset(gsm_network->vlr);
1143
Harald Welte0df904d2018-12-03 11:00:04 +01001144 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_WAIT_ACK, sgs->cfg.timer[SGS_STATE_TS11], 11);
1145 break;
1146 default:
1147 OSMO_ASSERT(0);
1148 break;
1149 }
1150}
1151
1152static int sgs_vlr_reset_fsm_timer_cb(struct osmo_fsm_inst *fi)
1153{
1154 struct msgb *reset_ind;
1155 struct gsm29118_reset_msg reset_params;
1156 struct sgs_mme_ctx *mme = (struct sgs_mme_ctx *)fi->priv;
1157 struct sgs_connection *sgc = mme->conn;
1158 struct sgs_state *sgs = mme->sgs;
1159
1160 switch (fi->T) {
1161 case 11:
1162 if (mme->ns11_remaining >= 1) {
1163 memset(&reset_params, 0, sizeof(reset_params));
1164 osmo_strlcpy(reset_params.vlr_name, sgs->cfg.vlr_name, sizeof(reset_params.vlr_name));
1165 reset_params.vlr_name_present = true;
1166 reset_ind = gsm29118_create_reset_ind(&reset_params);
1167 sgs_tx(sgc, reset_ind);
1168 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_WAIT_ACK, sgs->cfg.timer[SGS_STATE_TS11], 11);
1169 mme->ns11_remaining--;
1170 } else {
1171 LOGMME(mme, LOGL_ERROR, "Ts11 expired more than %u (Ns11) times, giving up\n",
Vadim Yanitskiye9ef7c62019-02-23 16:04:17 +07001172 sgs->cfg.counter[SGS_STATE_NS11]);
Harald Welte0df904d2018-12-03 11:00:04 +01001173 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_NULL, 0, 0);
1174 }
1175 break;
1176 default:
1177 OSMO_ASSERT(0);
1178 break;
1179 }
1180 return 0;
1181}
1182
1183static const struct osmo_fsm_state sgs_vlr_reset_fsm_states[] = {
1184 [SGS_VLRR_ST_NULL] = {
1185 /* We haven't even tried yet to send a RESET */
1186 .name = "NULL",
1187 .action = sgs_vlr_reset_fsm_null,
1188 .in_event_mask = S(SGS_VLRR_E_RX_RESET_ACK),
1189 .out_state_mask = S(SGS_VLRR_ST_NULL) | S(SGS_VLRR_ST_WAIT_ACK),
1190 },
1191 [SGS_VLRR_ST_WAIT_ACK] = {
1192 /* We're waiting for a SGsAP_RESET_ACK */
1193 .name = "WAIT-ACK",
1194 .action = sgs_vlr_reset_fsm_wait_ack,
1195 .in_event_mask = S(SGS_VLRR_E_RX_RESET_ACK),
1196 .out_state_mask = S(SGS_VLRR_ST_NULL) |
1197 S(SGS_VLRR_ST_COMPLETE) | S(SGS_VLRR_ST_WAIT_ACK),
1198 },
1199 [SGS_VLRR_ST_COMPLETE] = {
1200 /* Reset procedure to this MME has been completed */
1201 .name = "COMPLETE",
1202 .action = sgs_vlr_reset_fsm_complete,
1203 .in_event_mask = S(SGS_VLRR_E_RX_RESET_ACK),
1204 .out_state_mask = S(SGS_VLRR_ST_NULL) | S(SGS_VLRR_ST_COMPLETE),
1205 },
1206};
1207
1208static struct osmo_fsm sgs_vlr_reset_fsm = {
1209 .name = "SGs-VLR-RESET",
1210 .states = sgs_vlr_reset_fsm_states,
Philipp Maier483cea82019-04-03 16:23:29 +02001211 .num_states = ARRAY_SIZE(sgs_vlr_reset_fsm_states),
Harald Welte0df904d2018-12-03 11:00:04 +01001212 .allstate_event_mask = S(SGS_VLRR_E_START_RESET),
1213 .allstate_action = sgs_vlr_reset_fsm_allstate,
1214 .timer_cb = sgs_vlr_reset_fsm_timer_cb,
1215 .log_subsys = DSGS,
1216 .event_names = sgs_vlr_reset_fsm_event_names,
1217};
1218
1219/*! Send unit-data through SGs interface (see msc_ifaces.c)
1220 * \param[in] msg layer 3 message to send.
1221 * \returns 0 in case of success, -EINVAL in case of error. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001222int sgs_iface_tx_dtap_ud(struct msc_a *msc_a, struct msgb *msg)
Harald Welte0df904d2018-12-03 11:00:04 +01001223{
Harald Welte0df904d2018-12-03 11:00:04 +01001224 struct msgb *msg_sgs;
1225 struct sgs_mme_ctx *mme;
1226 int rc = -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001227 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Harald Welte0df904d2018-12-03 11:00:04 +01001228
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001229 OSMO_ASSERT(vsub);
Harald Welte0df904d2018-12-03 11:00:04 +01001230
1231 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_DL_UD);
1232 if (!mme)
1233 goto error;
1234
1235 /* Make sure the subscriber has a valid SGs association, otherwise
1236 * don't let unit-data through. */
1237 if (vsub->sgs_fsm->state != SGS_UE_ST_ASSOCIATED) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001238 LOG_MSC_A(msc_a, LOGL_NOTICE, "Cannot Tx %s: subscriber not SGs-associated\n",
1239 sgsap_msg_type_name(SGSAP_MSGT_DL_UD));
Harald Welte0df904d2018-12-03 11:00:04 +01001240 goto error;
1241 }
1242
1243 msg_sgs = gsm29118_create_dl_ud(vsub->imsi, msg);
1244 sgs_tx(mme->conn, msg_sgs);
1245 rc = 0;
1246
1247error:
1248 msgb_free(msg);
1249 return rc;
1250}
1251
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001252void sgs_iface_tx_release(struct vlr_subscr *vsub)
Harald Welte0df904d2018-12-03 11:00:04 +01001253{
1254 struct msgb *msg_sgs;
Harald Welte0df904d2018-12-03 11:00:04 +01001255 struct sgs_mme_ctx *mme;
1256
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001257 OSMO_ASSERT(vsub);
Harald Welte0df904d2018-12-03 11:00:04 +01001258
1259 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_DL_UD);
1260 if (!mme)
1261 return;
1262
1263 msg_sgs = gsm29118_create_release_req(vsub->imsi, 0);
1264 sgs_tx(mme->conn, msg_sgs);
1265}
1266
Philipp Maier002fb012019-09-24 09:26:47 +02001267/*! Send SGsAP-SERVICE-ABORT-REQUEST message to MME
1268 * \param[in] vsub subscriber context */
1269void sgs_iface_tx_serv_abrt(struct vlr_subscr *vsub)
1270{
1271 struct msgb *msg_sgs;
1272 struct sgs_mme_ctx *mme;
1273
1274 OSMO_ASSERT(vsub);
1275
1276 /* The service abort procedure is only defined for MT calls,
1277 * see also 3GPP TS 29.118, chapter 5.13.2 */
1278 if (vsub->sgs.paging_serv_ind != SGSAP_SERV_IND_CS_CALL)
1279 return;
1280
1281 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_DL_UD);
1282 if (!mme)
1283 return;
1284
1285 msg_sgs = gsm29118_create_service_abort_req(vsub->imsi);
1286 sgs_tx(mme->conn, msg_sgs);
1287}
1288
Martin Hauke3f07dac2019-11-14 17:49:08 +01001289/*! initialize SGs new interface
Harald Welte0df904d2018-12-03 11:00:04 +01001290 * \param[in] ctx talloc context
1291 * \param[in] network associated gsm network
1292 * \returns returns allocated sgs_stae, NULL in case of error. */
1293struct sgs_state *sgs_iface_init(void *ctx, struct gsm_network *network)
1294{
1295 struct sgs_state *sgs;
1296
1297 gsm_network = network;
1298
1299 sgs = sgs_server_alloc(ctx);
1300 OSMO_ASSERT(sgs);
1301
1302 /* We currently only support one SGs instance */
1303 if (g_sgs)
1304 return NULL;
1305 g_sgs = sgs;
1306
Harald Welte0df904d2018-12-03 11:00:04 +01001307 return sgs;
1308}
Vadim Yanitskiy4eaefc22019-03-21 20:55:19 +07001309
1310static __attribute__((constructor)) void on_dso_load(void)
1311{
1312 OSMO_ASSERT(osmo_fsm_register(&sgs_vlr_reset_fsm) == 0);
1313}