blob: d83a730ef67f798536a7d94197a9b93966e58b32 [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
339 * context-dependent dependand log output */
340static 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) {
355 LOGSGC_VSUB(sgc, subscr_info(imsi), LOGL_ERROR, "Rx %s with errornous conditional %s IEI!\n",
356 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;
376 unsigned int new_id_len = 0;
377 uint8_t resp_msg_type;
378
379 if (response->accepted)
380 resp_msg_type = SGSAP_MSGT_LOC_UPD_ACK;
381 else
382 resp_msg_type = SGSAP_MSGT_LOC_UPD_REJ;
383
384 mme = sgs_mme_ctx_by_vsub(vsub, resp_msg_type);
385 if (!mme)
386 return;
387
388 if (response->accepted) {
389 if (vsub->tmsi_new != GSM_RESERVED_TMSI) {
390 new_id_len = gsm48_generate_mid_from_tmsi(new_id, vsub->tmsi_new);
391 new_id_ptr = new_id + 2;
392 new_id_len -= 2;
393 }
394 resp = gsm29118_create_lu_ack(vsub->imsi, &vsub->sgs.lai, new_id_ptr, new_id_len);
395 sgs_tx(mme->conn, resp);
396 vlr_sgs_loc_update_acc_sent(vsub);
397 } else {
398 resp = gsm29118_create_lu_rej(vsub->imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, &vsub->sgs.lai);
399 sgs_tx(mme->conn, resp);
400 vlr_sgs_loc_update_rej_sent(vsub);
401 }
402}
403
404/* Called by VLR via callback, transmits MM information to the UE */
405static void sgs_tx_mm_info_cb(struct vlr_subscr *vsub)
406{
407 struct msgb *msg;
408 struct msgb *msg_mm_info;
409 struct sgs_mme_ctx *mme;
410
411 /* The sending of MM information requests is an optional feature and
412 * depends on the network configuration (VTY) */
413 if (!gsm_network->send_mm_info)
414 return;
415
416 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_MM_INFO_REQ);
417 if (!mme)
418 return;
419
420 /* Create and send MM information request message, see also:
421 * 3GPP TS 29.118, chapter 8.12 SGsAP-MM-INFORMATION-REQUEST and
422 * 3GPP TS 29.018, chapter 18.4.16 MM information. */
423 msg_mm_info = gsm48_create_mm_info(gsm_network);
424 msg = gsm29118_create_mm_info_req(vsub->imsi, msg_mm_info->data + 2, msg_mm_info->len - 2);
425 sgs_tx(mme->conn, msg);
426 msgb_free(msg_mm_info);
427}
428
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100429enum sgsap_service_ind sgs_serv_ind_from_paging_cause(enum paging_cause cause)
430{
431 switch (cause) {
432 case PAGING_CAUSE_CALL_CONVERSATIONAL:
433 case PAGING_CAUSE_CALL_STREAMING:
434 case PAGING_CAUSE_CALL_INTERACTIVE:
435 case PAGING_CAUSE_CALL_BACKGROUND:
436 return SGSAP_SERV_IND_CS_CALL;
437
438 case PAGING_CAUSE_UNSPECIFIED:
439 case PAGING_CAUSE_SIGNALLING_LOW_PRIO:
440 case PAGING_CAUSE_SIGNALLING_HIGH_PRIO:
441 return SGSAP_SERV_IND_SMS;
442
443 default:
444 OSMO_ASSERT(false);
445 }
446}
447
Harald Welte0df904d2018-12-03 11:00:04 +0100448/*! Page UE through SGs interface
449 * \param[in] vsub subscriber context
450 * \param[in] serv_ind service indicator (sms or voide)
451 * \returns 0 in case of success, -EINVAL in case of error. */
452int sgs_iface_tx_paging(struct vlr_subscr *vsub, enum sgsap_service_ind serv_ind)
453{
454 struct msgb *resp;
455 struct gsm29118_paging_req paging_params;
456 struct sgs_mme_ctx *mme;
457
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100458 LOGP(DMSC, LOGL_NOTICE, "XXXXXXXXXX state == %d conf_by_radio_contact_ind == %d\n",
459 vsub->sgs_fsm->state, vsub->conf_by_radio_contact_ind);
460
Harald Welte0df904d2018-12-03 11:00:04 +0100461 /* See also: 3GPP TS 29.118, chapter 5.1.2.2 Paging Initiation */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100462 if (vsub->sgs_fsm->state == SGS_UE_ST_NULL && vsub->conf_by_radio_contact_ind == true) {
463 LOGPFSMSL(vsub->sgs_fsm, DPAG, LOGL_ERROR, "Will not Page (conf_by_radio_contact_ind == true)\n");
Harald Welte0df904d2018-12-03 11:00:04 +0100464 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100465 }
Harald Welte0df904d2018-12-03 11:00:04 +0100466
467 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_PAGING_REQ);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100468 if (!mme) {
469 LOGPFSMSL(vsub->sgs_fsm, DPAG, LOGL_ERROR, "Will not Page (no MME)\n");
Harald Welte0df904d2018-12-03 11:00:04 +0100470 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100471 }
Harald Welte0df904d2018-12-03 11:00:04 +0100472
473 /* Check if there is still a paging in progress for this subscriber,
474 * if yes, don't initiate another paging request. */
475 if (vlr_sgs_pag_pend(vsub))
476 return 0;
477
478 memset(&paging_params, 0, sizeof(paging_params));
479 osmo_strlcpy(paging_params.imsi, vsub->imsi, sizeof(paging_params.imsi));
480 osmo_strlcpy(paging_params.vlr_name, mme->sgs->cfg.vlr_name, sizeof(paging_params.vlr_name));
481 paging_params.serv_ind = serv_ind;
482 if (vsub->conf_by_radio_contact_ind == true) {
483 memcpy(&paging_params.lai, &vsub->sgs.lai, sizeof(paging_params.lai));
484 paging_params.lai_present = true;
485 }
486 resp = gsm29118_create_paging_req(&paging_params);
487 sgs_tx(mme->conn, resp);
488
489 /* FIXME: If we are in SGS_UE_ST_NULL while sub->conf_by_radio_contact_ind == false,
490 * we are supposed to start a search procedure as defined in 3GPP TS 23.018 */
491
492 /* Inform the VLR that a paging via SGs is in progress */
493 vlr_sgs_pag(vsub, serv_ind);
494
495 /* Return a page count of 1 (success) */
496 return 1;
497}
498
499/***********************************************************************
500 * SGs incoming messages from the MME
501 ***********************************************************************/
502
503/* Safely read out the SGs cause code from a given message/tlv set, send status
504 * message in case the cause code is invalid or missing. */
505static int sgs_cause_from_msg(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp,
506 const char *imsi)
507{
508 enum sgsap_sgs_cause cause;
509 const uint8_t *cause_ptr;
510 cause_ptr = TLVP_VAL_MINLEN(tp, SGSAP_IE_SGS_CAUSE, 1);
511 if (!cause_ptr) {
512 sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_SGS_CAUSE);
513 return -1;
514 } else
515 cause = *cause_ptr;
516 return cause;
517}
518
519/* SGsAP-STATUS 3GPP TS 29.118, chapter 8.18 */
520static int sgs_rx_status(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, const char *imsi)
521{
522 int cause;
523 const uint8_t *err_msg;
524 const char *imsi_ptr;
525 char *err_msg_hex = "(none)";
526
527 cause = sgs_cause_from_msg(sgc, msg, tp, NULL);
528 if (cause < 0)
529 return 0;
530
531 if (imsi[0] != '\0')
532 imsi_ptr = imsi;
533 else
534 imsi_ptr = "<none>";
535
536 if (TLVP_PRESENT(tp, SGSAP_IE_ERR_MSG))
537 err_msg = TLVP_VAL(tp, SGSAP_IE_ERR_MSG);
538 else
539 err_msg = NULL;
540
541 if (err_msg)
542 err_msg_hex = osmo_hexdump(err_msg, TLVP_LEN(tp, SGSAP_IE_ERR_MSG));
543
544 LOGSGC(sgc, LOGL_NOTICE, "Rx STATUS cause=%s, IMSI=%s, err_msg=%s\n",
545 sgsap_sgs_cause_name(cause), imsi_ptr, err_msg_hex);
546
547 return 0;
548}
549
550/* SGsAP-RESET-INDICATION 3GPP TS 29.118, chapter 8.16 */
551static int sgs_rx_reset_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp)
552{
553 struct gsm29118_reset_msg reset_params;
554 struct msgb *resp;
555
556 memset(&reset_params, 0, sizeof(reset_params));
557 osmo_strlcpy(reset_params.vlr_name, sgc->sgs->cfg.vlr_name, sizeof(reset_params.vlr_name));
558 reset_params.vlr_name_present = true;
559
560 resp = gsm29118_create_reset_ack(&reset_params);
561
562 /* Perform a reset of the SGS FSM of all subscribers that are present in the VLR */
563 vlr_sgs_reset(gsm_network->vlr);
564
565 sgs_tx(sgc, resp);
566 return 0;
567}
568
569/* SGsAP-RESET-ACK 3GPP TS 29.118, chapter 8.15 */
570static int sgs_rx_reset_ack(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp)
571{
572 /* dispatch event to VLR reset FSM for this MME */
573 if (sgc->mme && sgc->mme->fi)
574 osmo_fsm_inst_dispatch(sgc->mme->fi, SGS_VLRR_E_RX_RESET_ACK, msg);
575 return 0;
576}
577
578/* SGsAP-LOCATION-UPDATE-REQUEST 3GPP TS 29.118, chapter 8.11 */
579static int sgs_rx_loc_upd_req(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
580{
581 struct msgb *resp;
582 const uint8_t *lu_type_ie;
583 enum vlr_lu_type type;
584 struct osmo_location_area_id new_lai;
585 const struct gsm48_loc_area_id *gsm48_lai;
586 int rc;
587 char *mme_name;
588 struct vlr_sgs_cfg vlr_sgs_cfg;
589 struct vlr_subscr *vsub;
590
591 /* Check for lingering connections */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100592 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100593 if (vsub) {
594 subscr_conn_toss(vsub);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100595 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100596 }
597
598 /* Determine MME-Name */
599 mme_name = sgs_mme_fqdn_get(sgc);
600 if (!mme_name) {
601 resp = gsm29118_create_lu_rej(imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, NULL);
602 sgs_tx(sgc, resp);
603 return 0;
604 }
605
606 /* Parse LU-Type */
607 lu_type_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_EPS_LU_TYPE, 1);
608 if (!lu_type_ie)
609 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_EPS_LU_TYPE);
610 if (lu_type_ie[0] == 0x01)
611 type = VLR_LU_TYPE_IMSI_ATTACH;
612 else
613 type = VLR_LU_TYPE_REGULAR;
614
615 /* Parse LAI of the new location */
616 gsm48_lai = (struct gsm48_loc_area_id *)TLVP_VAL_MINLEN(tp, SGSAP_IE_LAI, 5);
617 if (!gsm48_lai)
618 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_LAI);
619 gsm48_decode_lai2(gsm48_lai, &new_lai);
620
621 /* Perform actual location update */
622 memcpy(vlr_sgs_cfg.timer, sgc->sgs->cfg.timer, sizeof(vlr_sgs_cfg.timer));
623 memcpy(vlr_sgs_cfg.counter, sgc->sgs->cfg.counter, sizeof(vlr_sgs_cfg.counter));
624 rc = vlr_sgs_loc_update(gsm_network->vlr, &vlr_sgs_cfg, sgs_tx_loc_upd_resp_cb, sgs_iface_tx_paging,
625 sgs_tx_mm_info_cb, mme_name, type, imsi, &new_lai);
626 if (rc != 0) {
627 resp = gsm29118_create_lu_rej(imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, NULL);
628 sgs_tx(sgc, resp);
629 }
630
631 return 0;
632}
633
634/* SGsAP-IMSI-DETACH-INDICATION 3GPP TS 29.118, chapter 8.8 */
635static int sgs_rx_imsi_det_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
636{
637 struct msgb *resp;
638 enum sgsap_imsi_det_noneps_type type;
639 const uint8_t *type_ie;
640
641 type_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_IMSI_DET_NONEPS_TYPE, 1);
642 if (!type_ie)
643 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_IMSI_DET_NONEPS_TYPE);
644
645 switch (type_ie[0]) {
646 case SGSAP_ID_NONEPS_T_EXPLICIT_UE_NONEPS:
647 type = SGSAP_ID_NONEPS_T_EXPLICIT_UE_NONEPS;
648 break;
649 case SGSAP_ID_NONEPS_T_COMBINED_UE_EPS_NONEPS:
650 type = SGSAP_ID_NONEPS_T_COMBINED_UE_EPS_NONEPS;
651 break;
652 case SGSAP_ID_NONEPS_T_IMPLICIT_UE_EPS_NONEPS:
653 type = SGSAP_ID_NONEPS_T_IMPLICIT_UE_EPS_NONEPS;
654 break;
655 default:
656 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_INVALID_MAND_IE, msg, SGSAP_IE_IMSI_DET_NONEPS_TYPE);
657 break;
658 }
659
660 vlr_sgs_imsi_detach(gsm_network->vlr, imsi, type);
661 resp = gsm29118_create_imsi_det_ack(imsi);
662 sgs_tx(sgc, resp);
663
664 return 0;
665}
666
667/* SGsAP-EPS-DETACH-INDICATION 3GPP TS 29.118, chapter 8.6 */
668static int sgs_rx_eps_det_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
669{
670 struct msgb *resp;
671 enum sgsap_imsi_det_eps_type type;
672 const uint8_t *type_ie;
673
674 type_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_IMSI_DET_EPS_TYPE, 1);
675 if (!type_ie)
676 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_IMSI_DET_EPS_TYPE);
677
678 switch (type_ie[0]) {
679 case SGSAP_ID_EPS_T_NETWORK_INITIATED:
680 type = SGSAP_ID_EPS_T_NETWORK_INITIATED;
681 break;
682 case SGSAP_ID_EPS_T_UE_INITIATED:
683 type = SGSAP_ID_EPS_T_UE_INITIATED;
684 break;
685 case SGSAP_ID_EPS_T_EPS_NOT_ALLOWED:
686 type = SGSAP_ID_EPS_T_EPS_NOT_ALLOWED;
687 break;
688 default:
689 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_INVALID_MAND_IE, msg, SGSAP_IE_IMSI_DET_EPS_TYPE);
690 break;
691 }
692
693 vlr_sgs_eps_detach(gsm_network->vlr, imsi, type);
694 resp = gsm29118_create_eps_det_ack(imsi);
695 sgs_tx(sgc, resp);
696
697 return 0;
698}
699
700/* SGsAP-PAGING-REJECT 3GPP TS 29.118, chapter 8.13 */
701static int sgs_rx_pag_rej(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
702{
703 int cause;
704 struct vlr_subscr *vsub;
705
706 cause = sgs_cause_from_msg(sgc, msg, tp, NULL);
707 if (cause < 0)
708 return 0;
709
710 /* Subscriber must be known by the VLR */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100711 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100712 if (!vsub)
713 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, msg, SGSAP_IE_IMSI);
714
715 /* Inform the VLR */
716 vlr_sgs_pag_rej(gsm_network->vlr, imsi, cause);
717
718 /* Stop all paging activity */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100719 paging_expired(vsub);
Harald Welte0df904d2018-12-03 11:00:04 +0100720
721 /* Depending on the cause code some action is required */
722 if (cause == SGSAP_SGS_CAUSE_MT_CSFB_REJ_USER) {
723 /* FIXME: We are supposed to trigger a User Determined User Busy (UDUB)
724 * as specified in 3GPP TS 24.082 here, SGs association state shall not
725 * be changed */
726 LOGSGC(sgc, LOGL_ERROR,
727 "Rx %s with SGSAP_SGS_CAUSE_MT_CSFB_REJ_USER, but sending UDUP is not implemented yet!\n",
728 sgsap_msg_type_name(msg->data[0]));
729 } else if (cause == SGSAP_SGS_CAUSE_IMSI_DET_EPS) {
730 /* FIXME: In this case we should send the paging via A/Iu interface */
731 OSMO_ASSERT(false);
732 }
733
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100734 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100735 return 0;
736}
737
738/* SGsAP-UE-UNREACHABLE 3GPP TS 29.118, chapter 8.21 */
739static int sgs_rx_ue_unr(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
740{
741 int cause;
742
743 cause = sgs_cause_from_msg(sgc, msg, tp, NULL);
744 if (cause < 0)
745 return 0;
746
747 vlr_sgs_ue_unr(gsm_network->vlr, imsi, cause);
748
749 return 0;
750}
751
752/* SGsAP-TMSI-REALLOCATION-COMPLETE 3GPP TS 29.118, chapter 8.19 */
753static int sgs_rx_tmsi_reall_cmpl(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
754{
755 vlr_sgs_tmsi_reall_compl(gsm_network->vlr, imsi);
756 return 0;
757}
758
759/* SGsAP-SERVICE-REQUEST 3GPP TS 29.118, chapter 8.17 */
760static int sgs_rx_service_req(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
761{
762 enum sgsap_service_ind serv_ind;
763 const uint8_t *serv_ind_ie;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100764 struct msc_a *msc_a;
Harald Welte0df904d2018-12-03 11:00:04 +0100765 struct vlr_subscr *vsub;
766
767 /* Note: While in other RAN concepts a service request is used to
768 * initiate mobile originated operation, the service request in SGsAP
769 * is comparable to a paging response. The SGsAP SERVICE REQUEST must
770 * not be confused or compared with a CM SERVICE REQUEST! */
771
772 if (!check_sgs_association(sgc, msg, imsi))
773 return 0;
774
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100775 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100776 /* Note: vsub is already sufficiently verified by check_sgs_association(),
777 * we must have a vsub at this point! */
778 OSMO_ASSERT(vsub);
779
780 /* The Service request is intended as a paging response, if one is
781 * received while nothing is paging something is very wrong! */
782 if (!vlr_sgs_pag_pend(vsub)) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100783 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100784 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE, msg, -1);
785 }
786 serv_ind_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_SERVICE_INDICATOR, 1);
787
788 if (!serv_ind_ie) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100789 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100790 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_SERVICE_INDICATOR);
791 }
792 if (serv_ind_ie[0] == SGSAP_SERV_IND_CS_CALL)
793 serv_ind = serv_ind_ie[0];
794 else if (serv_ind_ie[0] == SGSAP_SERV_IND_SMS)
795 serv_ind = serv_ind_ie[0];
796 else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100797 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100798 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_INVALID_MAND_IE, msg, SGSAP_IE_SERVICE_INDICATOR);
799 }
800
801 /* FIXME: The MME shall include an UE EMM Mode IE, but the field is
802 * marked optional. (Why do we need this info at all?) */
803
804 /* Report to the VLR that the paging has successfully completed */
805 vlr_sgs_pag_ack(gsm_network->vlr, imsi);
806
807 /* Exit early when the service indicator indicates that a call is being
808 * established. In those cases we do not allocate a connection, instead
809 * the connection will be allocated when the MS is appearing on the
810 * A-Interface. */
811 if (serv_ind == SGSAP_SERV_IND_CS_CALL) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100812 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100813 return 0;
814 }
815
816 /* Allocate subscriber connection */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100817 msc_a = subscr_conn_allocate_sgs(sgc, vsub, true);
818 if (!msc_a) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100819 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100820 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE, msg, -1);
821 }
822
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100823 /* The msub has added a get() for the vsub, balance above vlr_subscr_find_by_imsi() */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100824 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100825 return 0;
826}
827
828/* SGsAP-UPLINK-UNITDATA 3GPP TS 29.118, chapter 8.22 */
829static int sgs_rx_ul_ud(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
830{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100831 struct msc_a *msc_a;
Harald Welte0df904d2018-12-03 11:00:04 +0100832 const uint8_t *nas_msg_container_ie;
833 struct vlr_subscr *vsub;
834
835 if (!check_sgs_association(sgc, msg, imsi))
836 return 0;
837
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100838 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100839 /* Note: vsub is already sufficiently verified by check_sgs_association(),
840 * we must have a vsub at this point! */
841 OSMO_ASSERT(vsub);
842
843 /* Try to find existing connection (MT) or allocate a new one (MO) */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100844 msc_a = msc_a_for_vsub(vsub, true);
845 if (!msc_a)
846 msc_a = subscr_conn_allocate_sgs(sgc, vsub, false);
Harald Welte0df904d2018-12-03 11:00:04 +0100847
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100848 /* Balance above vlr_subscr_find_by_imsi() */
849 vlr_subscr_put(vsub, __func__);
850
Harald Welte0df904d2018-12-03 11:00:04 +0100851 /* If we do not find an existing connection and allocating a new one
852 * faild, give up and return status. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100853 if (!msc_a)
Harald Welte0df904d2018-12-03 11:00:04 +0100854 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE, msg, 0);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100855
856 if (msc_a->c.ran->type != OSMO_RAT_EUTRAN_SGS) {
857 LOGSGC(sgc, LOGL_ERROR,
858 "Receiving uplink unit-data for non-sgs connection -- discarding message!\n");
859 return -EINVAL;
Harald Welte0df904d2018-12-03 11:00:04 +0100860 }
861
862 nas_msg_container_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_NAS_MSG_CONTAINER, 1);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100863 if (!nas_msg_container_ie)
Harald Welte0df904d2018-12-03 11:00:04 +0100864 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 +0100865
866 /* ran_conn_dtap expects the dtap payload in l3h */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100867 msg->l3h = (uint8_t *)nas_msg_container_ie;
868 msc_a_up_l3(msc_a, msg);
Harald Welte0df904d2018-12-03 11:00:04 +0100869
Harald Welte0df904d2018-12-03 11:00:04 +0100870 return 0;
871}
872
873/* SGsAP-MO-CSFB-INDICATION, chapter 8.25 */
874static int sgs_rx_csfb_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
875{
876 struct vlr_subscr *vsub;
877
878 /* The MME informs us with this message that the UE has returned back
879 * to the 4G network, so we use the SGs interface again for further
880 * communication with the UE. */
881
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100882 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100883 if (!vsub)
884 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, msg, SGSAP_IE_IMSI);
885
886 /* Check for lingering connections */
887 subscr_conn_toss(vsub);
888
889 vsub->cs.attached_via_ran = OSMO_RAT_EUTRAN_SGS;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100890 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100891 return 0;
892}
893
894/* SGsAP-UE-ACTIVITY-INDICATION, chapter 8.20 */
895static int sgs_rx_ue_act_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
896{
897 /* In this MSC/VLR implementation we do not support the alerting
898 * procedure yet and therefore we will never request any alerting
899 * at the MME. Given that it is unlikely that we ever get activity
900 * indications from the MME, but if we do we should not act all too
901 * hostile and ignore the indication silently. */
902
903 LOGSGC(sgc, LOGL_ERROR, "Rx %s unexpected, we do not implement alerting yet, ignoring!\n",
904 sgsap_msg_type_name(msg->data[0]));
905
906 return 0;
907}
908
909#define TX_STATUS_AND_LOG(sgc, msg_type, cause, fmt) \
910 LOGSGC(sgc, LOGL_ERROR, fmt, sgsap_msg_type_name(msg_type)); \
911 resp = gsm29118_create_status(NULL, cause, msg); \
912 sgs_tx(sgc, resp); \
913
914/*! Process incoming SGs message (see sgs_server.c)
915 * \param[in] sgc related sgs connection
916 * \param[in] msg received message
917 * \returns 0 in case of success, -EINVAL in case of error. */
918int sgs_iface_rx(struct sgs_connection *sgc, struct msgb *msg)
919{
920 struct msgb *resp;
921 uint8_t msg_type = msg->l2h[0];
922 struct tlv_parsed tp;
923 int rc;
924 char imsi[GSM48_MI_SIZE];
925 char mme_name[SGS_MME_NAME_LEN + 1];
926
927 memset(imsi, 0, sizeof(imsi));
928 memset(mme_name, 0, sizeof(mme_name));
929
930 /* When the receiving entity receives a message that is too short to contain a complete
931 * message type information element, the receiving entity shall ignore that message. */
932 if (msgb_l2len(msg) < 1)
933 goto error;
934
935 /* Parse TLV elements */
936 rc = tlv_parse(&tp, &sgsap_ie_tlvdef, msgb_l2(msg) + 1, msgb_l2len(msg) - 1, 0, 0);
937 if (rc < 0) {
938 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_SEMANT_INCORR_MSG, "SGsAP Message %s parsing error\n");
939 goto error;
940 }
941
942 /* Most of the messages contain an IMSI as mandatory IE, parse it right here */
943 if (!TLVP_PRESENT(&tp, SGSAP_IE_IMSI) &&
944 msg_type != SGSAP_MSGT_STATUS && msg_type != SGSAP_MSGT_RESET_IND && msg_type != SGSAP_MSGT_RESET_ACK) {
945 /* reject the message; all but the three above have mandatory IMSI */
946 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_MISSING_MAND_IE,
947 "SGsAP Message %s without IMSI, dropping\n");
948 goto error;
949 }
950
951 if (TLVP_PRESENT(&tp, SGSAP_IE_IMSI)) {
952 gsm48_mi_to_string(imsi, sizeof(imsi), TLVP_VAL(&tp, SGSAP_IE_IMSI), TLVP_LEN(&tp, SGSAP_IE_IMSI));
953 if (strlen(imsi) < GSM23003_IMSI_MIN_DIGITS) {
954 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_INVALID_MAND_IE,
955 "SGsAP Message %s with short IMSI, dropping\n");
956 goto error;
957 }
958 }
959
960 /* Some messages contain an MME-NAME as mandatore IE, parse it right here. The
961 * MME-NAME is als immediately registered with the sgc, so it will be implicitly
962 * known to all functions that have access to the sgc context. */
963 if (!TLVP_PRESENT(&tp, SGSAP_IE_MME_NAME)
964 && (msg_type == SGSAP_MSGT_RESET_IND || msg_type == SGSAP_MSGT_RESET_ACK
965 || msg_type == SGSAP_MSGT_LOC_UPD_REQ || msg_type == SGSAP_MSGT_IMSI_DET_IND
966 || msg_type == SGSAP_MSGT_EPS_DET_IND)) {
967 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_MISSING_MAND_IE,
968 "SGsAP Message %s without MME-Name, dropping\n");
969 goto error;
970 }
971
972 if (TLVP_PRESENT(&tp, SGSAP_IE_MME_NAME)) {
973 if (decode_mme_name(mme_name, &tp) != 0) {
974 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_INVALID_MAND_IE,
975 "SGsAP Message %s with invalid MME-Name, dropping\n");
976 goto error;
977 }
978 /* Regsister/check mme_name with sgc */
979 if (sgs_mme_fqdn_received(sgc, mme_name) < 0) {
980 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE,
981 "SGsAP Message %s with invalid MME-Name, dropping\n");
982 goto error;
983 }
984 }
985
986 /* dispatch msg to various handler functions. msgb ownership remains here! */
987 rc = -EINVAL;
988 switch (msg_type) {
989 case SGSAP_MSGT_STATUS:
990 rc = sgs_rx_status(sgc, msg, &tp, imsi);
991 break;
992 case SGSAP_MSGT_RESET_IND:
993 rc = sgs_rx_reset_ind(sgc, msg, &tp);
994 break;
995 case SGSAP_MSGT_RESET_ACK:
996 rc = sgs_rx_reset_ack(sgc, msg, &tp);
997 break;
998 case SGSAP_MSGT_LOC_UPD_REQ:
999 rc = sgs_rx_loc_upd_req(sgc, msg, &tp, imsi);
1000 break;
1001 case SGSAP_MSGT_IMSI_DET_IND:
1002 rc = sgs_rx_imsi_det_ind(sgc, msg, &tp, imsi);
1003 break;
1004 case SGSAP_MSGT_EPS_DET_IND:
1005 rc = sgs_rx_eps_det_ind(sgc, msg, &tp, imsi);
1006 break;
1007 case SGSAP_MSGT_PAGING_REJ:
1008 rc = sgs_rx_pag_rej(sgc, msg, &tp, imsi);
1009 break;
1010 case SGSAP_MSGT_UE_UNREACHABLE:
1011 rc = sgs_rx_ue_unr(sgc, msg, &tp, imsi);
1012 break;
1013 case SGSAP_MSGT_TMSI_REALL_CMPL:
1014 rc = sgs_rx_tmsi_reall_cmpl(sgc, msg, &tp, imsi);
1015 break;
1016 case SGSAP_MSGT_SERVICE_REQ:
1017 rc = sgs_rx_service_req(sgc, msg, &tp, imsi);
1018 break;
1019 case SGSAP_MSGT_UL_UD:
1020 rc = sgs_rx_ul_ud(sgc, msg, &tp, imsi);
1021 break;
1022 case SGSAP_MSGT_MO_CSFB_IND:
1023 rc = sgs_rx_csfb_ind(sgc, msg, &tp, imsi);
1024 break;
1025 case SGSAP_MSGT_UE_ACT_IND:
1026 rc = sgs_rx_ue_act_ind(sgc, msg, &tp, imsi);
1027 break;
1028 case SGSAP_MSGT_ALERT_ACK:
1029 case SGSAP_MSGT_ALERT_REJ:
1030 LOGSGC(sgc, LOGL_ERROR, "Rx unmplemented SGsAP %s: %s\n",
1031 sgsap_msg_type_name(msg_type), msgb_hexdump(msg));
1032 resp = gsm29118_create_status(imsi, SGSAP_SGS_CAUSE_MSG_UNKNOWN, msg);
1033 sgs_tx(sgc, resp);
1034 rc = 0;
1035 break;
1036 default:
1037 LOGSGC(sgc, LOGL_ERROR, "Rx unknown SGsAP message type 0x%02x: %s\n", msg_type, msgb_hexdump(msg));
1038 resp = gsm29118_create_status(imsi, SGSAP_SGS_CAUSE_MSG_UNKNOWN, msg);
1039 sgs_tx(sgc, resp);
1040 rc = 0;
1041 break;
1042 }
1043
1044 /* Catch unhandled errors */
1045 if (rc < 0) {
1046 /* Note: Usually the sgs_rx_ should catch errors locally and
1047 * eimit a status message with proper cause code, including
1048 * a suitable log message. If we end up here, something is
1049 * not right and should be fixed */
1050 LOGSGC(sgc, LOGL_ERROR, "Rx unable to decode SGsAP %s: %s\n",
1051 sgsap_msg_type_name(msg_type), msgb_hexdump(msg));
1052 resp = gsm29118_create_status(imsi, SGSAP_SGS_CAUSE_MSG_UNKNOWN, msg);
1053 sgs_tx(sgc, resp);
1054 }
1055
1056error:
1057 msgb_free(msg);
1058 return 0;
1059}
1060
1061/***********************************************************************
1062 * SGs connection "VLR Reset Procedure" FSM
1063 ***********************************************************************/
1064
1065static const struct value_string sgs_vlr_reset_fsm_event_names[] = {
1066 {SGS_VLRR_E_START_RESET, "START-RESET"},
1067 {SGS_VLRR_E_RX_RESET_ACK, "RX-RESET-ACK"},
1068 {0, NULL}
1069};
1070
1071static void sgs_vlr_reset_fsm_null(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1072{
1073 switch (event) {
1074 case SGS_VLRR_E_RX_RESET_ACK:
1075 break;
1076 default:
1077 OSMO_ASSERT(0);
1078 break;
1079 }
1080}
1081
1082static void sgs_vlr_reset_fsm_wait_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1083{
1084 switch (event) {
1085 case SGS_VLRR_E_RX_RESET_ACK:
1086 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_COMPLETE, 0, 0);
1087 break;
1088 default:
1089 OSMO_ASSERT(0);
1090 break;
1091 }
1092}
1093
1094static void sgs_vlr_reset_fsm_complete(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_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1106{
1107 struct msgb *reset_ind;
1108 struct gsm29118_reset_msg reset_params;
1109 struct sgs_mme_ctx *mme = (struct sgs_mme_ctx *)fi->priv;
1110 struct sgs_connection *sgc = mme->conn;
1111 struct sgs_state *sgs = mme->sgs;
1112
1113 switch (event) {
1114 case SGS_VLRR_E_START_RESET:
1115 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_NULL, 0, 0);
1116 mme->ns11_remaining = sgs->cfg.counter[SGS_STATE_NS11];
1117 /* send a reset message and enter WAIT_ACK state */
1118 memset(&reset_params, 0, sizeof(reset_params));
1119 osmo_strlcpy(reset_params.vlr_name, sgs->cfg.vlr_name, sizeof(reset_params.vlr_name));
1120 reset_params.vlr_name_present = true;
1121 reset_ind = gsm29118_create_reset_ind(&reset_params);
1122 sgs_tx(sgc, reset_ind);
1123 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_WAIT_ACK, sgs->cfg.timer[SGS_STATE_TS11], 11);
1124 break;
1125 default:
1126 OSMO_ASSERT(0);
1127 break;
1128 }
1129}
1130
1131static int sgs_vlr_reset_fsm_timer_cb(struct osmo_fsm_inst *fi)
1132{
1133 struct msgb *reset_ind;
1134 struct gsm29118_reset_msg reset_params;
1135 struct sgs_mme_ctx *mme = (struct sgs_mme_ctx *)fi->priv;
1136 struct sgs_connection *sgc = mme->conn;
1137 struct sgs_state *sgs = mme->sgs;
1138
1139 switch (fi->T) {
1140 case 11:
1141 if (mme->ns11_remaining >= 1) {
1142 memset(&reset_params, 0, sizeof(reset_params));
1143 osmo_strlcpy(reset_params.vlr_name, sgs->cfg.vlr_name, sizeof(reset_params.vlr_name));
1144 reset_params.vlr_name_present = true;
1145 reset_ind = gsm29118_create_reset_ind(&reset_params);
1146 sgs_tx(sgc, reset_ind);
1147 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_WAIT_ACK, sgs->cfg.timer[SGS_STATE_TS11], 11);
1148 mme->ns11_remaining--;
1149 } else {
1150 LOGMME(mme, LOGL_ERROR, "Ts11 expired more than %u (Ns11) times, giving up\n",
Vadim Yanitskiye9ef7c62019-02-23 16:04:17 +07001151 sgs->cfg.counter[SGS_STATE_NS11]);
Harald Welte0df904d2018-12-03 11:00:04 +01001152 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_NULL, 0, 0);
1153 }
1154 break;
1155 default:
1156 OSMO_ASSERT(0);
1157 break;
1158 }
1159 return 0;
1160}
1161
1162static const struct osmo_fsm_state sgs_vlr_reset_fsm_states[] = {
1163 [SGS_VLRR_ST_NULL] = {
1164 /* We haven't even tried yet to send a RESET */
1165 .name = "NULL",
1166 .action = sgs_vlr_reset_fsm_null,
1167 .in_event_mask = S(SGS_VLRR_E_RX_RESET_ACK),
1168 .out_state_mask = S(SGS_VLRR_ST_NULL) | S(SGS_VLRR_ST_WAIT_ACK),
1169 },
1170 [SGS_VLRR_ST_WAIT_ACK] = {
1171 /* We're waiting for a SGsAP_RESET_ACK */
1172 .name = "WAIT-ACK",
1173 .action = sgs_vlr_reset_fsm_wait_ack,
1174 .in_event_mask = S(SGS_VLRR_E_RX_RESET_ACK),
1175 .out_state_mask = S(SGS_VLRR_ST_NULL) |
1176 S(SGS_VLRR_ST_COMPLETE) | S(SGS_VLRR_ST_WAIT_ACK),
1177 },
1178 [SGS_VLRR_ST_COMPLETE] = {
1179 /* Reset procedure to this MME has been completed */
1180 .name = "COMPLETE",
1181 .action = sgs_vlr_reset_fsm_complete,
1182 .in_event_mask = S(SGS_VLRR_E_RX_RESET_ACK),
1183 .out_state_mask = S(SGS_VLRR_ST_NULL) | S(SGS_VLRR_ST_COMPLETE),
1184 },
1185};
1186
1187static struct osmo_fsm sgs_vlr_reset_fsm = {
1188 .name = "SGs-VLR-RESET",
1189 .states = sgs_vlr_reset_fsm_states,
1190 .allstate_event_mask = S(SGS_VLRR_E_START_RESET),
1191 .allstate_action = sgs_vlr_reset_fsm_allstate,
1192 .timer_cb = sgs_vlr_reset_fsm_timer_cb,
1193 .log_subsys = DSGS,
1194 .event_names = sgs_vlr_reset_fsm_event_names,
1195};
1196
1197/*! Send unit-data through SGs interface (see msc_ifaces.c)
1198 * \param[in] msg layer 3 message to send.
1199 * \returns 0 in case of success, -EINVAL in case of error. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001200int sgs_iface_tx_dtap_ud(struct msc_a *msc_a, struct msgb *msg)
Harald Welte0df904d2018-12-03 11:00:04 +01001201{
Harald Welte0df904d2018-12-03 11:00:04 +01001202 struct msgb *msg_sgs;
1203 struct sgs_mme_ctx *mme;
1204 int rc = -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001205 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Harald Welte0df904d2018-12-03 11:00:04 +01001206
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001207 OSMO_ASSERT(vsub);
Harald Welte0df904d2018-12-03 11:00:04 +01001208
1209 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_DL_UD);
1210 if (!mme)
1211 goto error;
1212
1213 /* Make sure the subscriber has a valid SGs association, otherwise
1214 * don't let unit-data through. */
1215 if (vsub->sgs_fsm->state != SGS_UE_ST_ASSOCIATED) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001216 LOG_MSC_A(msc_a, LOGL_NOTICE, "Cannot Tx %s: subscriber not SGs-associated\n",
1217 sgsap_msg_type_name(SGSAP_MSGT_DL_UD));
Harald Welte0df904d2018-12-03 11:00:04 +01001218 goto error;
1219 }
1220
1221 msg_sgs = gsm29118_create_dl_ud(vsub->imsi, msg);
1222 sgs_tx(mme->conn, msg_sgs);
1223 rc = 0;
1224
1225error:
1226 msgb_free(msg);
1227 return rc;
1228}
1229
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001230void sgs_iface_tx_release(struct vlr_subscr *vsub)
Harald Welte0df904d2018-12-03 11:00:04 +01001231{
1232 struct msgb *msg_sgs;
Harald Welte0df904d2018-12-03 11:00:04 +01001233 struct sgs_mme_ctx *mme;
1234
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 return;
1240
1241 msg_sgs = gsm29118_create_release_req(vsub->imsi, 0);
1242 sgs_tx(mme->conn, msg_sgs);
1243}
1244
1245/*! initalize SGs new interface
1246 * \param[in] ctx talloc context
1247 * \param[in] network associated gsm network
1248 * \returns returns allocated sgs_stae, NULL in case of error. */
1249struct sgs_state *sgs_iface_init(void *ctx, struct gsm_network *network)
1250{
1251 struct sgs_state *sgs;
1252
1253 gsm_network = network;
1254
1255 sgs = sgs_server_alloc(ctx);
1256 OSMO_ASSERT(sgs);
1257
1258 /* We currently only support one SGs instance */
1259 if (g_sgs)
1260 return NULL;
1261 g_sgs = sgs;
1262
Harald Welte0df904d2018-12-03 11:00:04 +01001263 return sgs;
1264}
Vadim Yanitskiy4eaefc22019-03-21 20:55:19 +07001265
1266static __attribute__((constructor)) void on_dso_load(void)
1267{
1268 OSMO_ASSERT(osmo_fsm_register(&sgs_vlr_reset_fsm) == 0);
1269}