blob: 8b4b61d30b6fed22e702eced05d2d3ff9e27d0fe [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>
39#include <osmocom/msc/a_iface.h>
40#include <osmocom/msc/gsm_04_08.h>
41
42#include <osmocom/msc/debug.h>
43#include <osmocom/msc/sgs_iface.h>
44#include <osmocom/msc/sgs_server.h>
45#include <osmocom/msc/msc_ifaces.h>
46#include <osmocom/gsm/protocol/gsm_29_118.h>
47
48#include <osmocom/gsm/apn.h>
49
50#define S(x) (1 << (x))
51
52/* A pointer to the GSM network we work with. By the current paradigm,
53 * there can only be one gsm_network per MSC. The pointer is set once
Vadim Yanitskiy118a0b82019-03-21 20:51:55 +070054 * when calling sgs_iface_init() */
Harald Welte0df904d2018-12-03 11:00:04 +010055static struct gsm_network *gsm_network = NULL;
56
57static struct osmo_fsm sgs_vlr_reset_fsm;
58static void sgs_tx(struct sgs_connection *sgc, struct msgb *msg);
59
60struct sgs_state *g_sgs;
61
62/***********************************************************************
63 * SGs state per MME connection
64 ***********************************************************************/
65
66#define LOGSGC(sgc, lvl, fmt, args...) \
67 LOGP(DSGS, lvl, "%s: " fmt, sgc->sockname, ## args)
68
69#define LOGSGC_VSUB(sgc, sub_info, lvl, fmt, args...) \
70 LOGP(DSGS, lvl, "(sub %s) %s: " fmt, sub_info, sgc->sockname, ## args)
71
72#define LOGMME(mme, lvl, fmt, args...) \
73 LOGP(DSGS, lvl, "%s: " fmt, mme->fqdn ? mme->fqdn : mme->conn->sockname, ## args)
74
75enum sgs_vlr_reset_fsm_state {
76 SGS_VLRR_ST_NULL,
77 SGS_VLRR_ST_WAIT_ACK,
78 SGS_VLRR_ST_COMPLETE,
79};
80
81enum sgs_vlr_reset_fsm_event {
82 SGS_VLRR_E_START_RESET,
83 SGS_VLRR_E_RX_RESET_ACK,
84};
85
86/***********************************************************************
87 * SGs utility functions
88 ***********************************************************************/
89
90/* Allocate a new subscriber connection */
91static struct ran_conn *subscr_conn_allocate_sgs(struct sgs_connection *sgc, struct vlr_subscr *vsub, bool mt)
92{
93 struct ran_conn *conn;
94
95 conn = ran_conn_alloc(gsm_network, OSMO_RAT_EUTRAN_SGS, vsub->sgs.lai.lac);
96 if (!conn) {
97 LOGSGC_VSUB(sgc, vlr_subscr_name(vsub), LOGL_ERROR, "Connection allocation failed\n");
98 return NULL;
99 }
100
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100101 vlr_subscr_get(vsub, VSUB_USE_CONN);
Harald Welte0df904d2018-12-03 11:00:04 +0100102 conn->vsub = vsub;
103 conn->vsub->cs.attached_via_ran = conn->via_ran;
104
105 /* Accept the connection immediately, since the UE is already
106 * authenticated by the MME no authentication is required. */
107 conn->complete_layer3_type = mt ? COMPLETE_LAYER3_PAGING_RESP : COMPLETE_LAYER3_CM_SERVICE_REQ;
108 ran_conn_update_id(conn);
109 osmo_fsm_inst_dispatch(conn->fi, RAN_CONN_E_COMPLETE_LAYER_3, NULL);
110 osmo_fsm_inst_dispatch(conn->fi, RAN_CONN_E_ACCEPTED, NULL);
111
112 LOG_RAN_CONN(conn, LOGL_DEBUG, "RAN connection successfully allocated!\n");
113 return conn;
114}
115
116/* Check if there are connections associated with a given subscriber. If yes,
117 * make sure that those connections are tossed. */
118static void subscr_conn_toss(struct vlr_subscr *vsub)
119{
120 struct ran_conn *conn;
121
122 conn = connection_for_subscr(vsub);
123 if (!conn)
124 return;
125
126 LOG_RAN_CONN(conn, LOGL_DEBUG, "RAN connection tossed because of unexpected RAN change!\n");
127
128 ran_conn_mo_close(conn, GSM48_REJECT_CONGESTION);
129}
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
429/*! Page UE through SGs interface
430 * \param[in] vsub subscriber context
431 * \param[in] serv_ind service indicator (sms or voide)
432 * \returns 0 in case of success, -EINVAL in case of error. */
433int sgs_iface_tx_paging(struct vlr_subscr *vsub, enum sgsap_service_ind serv_ind)
434{
435 struct msgb *resp;
436 struct gsm29118_paging_req paging_params;
437 struct sgs_mme_ctx *mme;
438
439 /* See also: 3GPP TS 29.118, chapter 5.1.2.2 Paging Initiation */
440 if (vsub->sgs_fsm->state == SGS_UE_ST_NULL && vsub->conf_by_radio_contact_ind == true)
441 return -EINVAL;
442
443 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_PAGING_REQ);
444 if (!mme)
445 return -EINVAL;
446
447 /* Check if there is still a paging in progress for this subscriber,
448 * if yes, don't initiate another paging request. */
449 if (vlr_sgs_pag_pend(vsub))
450 return 0;
451
452 memset(&paging_params, 0, sizeof(paging_params));
453 osmo_strlcpy(paging_params.imsi, vsub->imsi, sizeof(paging_params.imsi));
454 osmo_strlcpy(paging_params.vlr_name, mme->sgs->cfg.vlr_name, sizeof(paging_params.vlr_name));
455 paging_params.serv_ind = serv_ind;
456 if (vsub->conf_by_radio_contact_ind == true) {
457 memcpy(&paging_params.lai, &vsub->sgs.lai, sizeof(paging_params.lai));
458 paging_params.lai_present = true;
459 }
460 resp = gsm29118_create_paging_req(&paging_params);
461 sgs_tx(mme->conn, resp);
462
463 /* FIXME: If we are in SGS_UE_ST_NULL while sub->conf_by_radio_contact_ind == false,
464 * we are supposed to start a search procedure as defined in 3GPP TS 23.018 */
465
466 /* Inform the VLR that a paging via SGs is in progress */
467 vlr_sgs_pag(vsub, serv_ind);
468
469 /* Return a page count of 1 (success) */
470 return 1;
471}
472
473/***********************************************************************
474 * SGs incoming messages from the MME
475 ***********************************************************************/
476
477/* Safely read out the SGs cause code from a given message/tlv set, send status
478 * message in case the cause code is invalid or missing. */
479static int sgs_cause_from_msg(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp,
480 const char *imsi)
481{
482 enum sgsap_sgs_cause cause;
483 const uint8_t *cause_ptr;
484 cause_ptr = TLVP_VAL_MINLEN(tp, SGSAP_IE_SGS_CAUSE, 1);
485 if (!cause_ptr) {
486 sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_SGS_CAUSE);
487 return -1;
488 } else
489 cause = *cause_ptr;
490 return cause;
491}
492
493/* SGsAP-STATUS 3GPP TS 29.118, chapter 8.18 */
494static int sgs_rx_status(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, const char *imsi)
495{
496 int cause;
497 const uint8_t *err_msg;
498 const char *imsi_ptr;
499 char *err_msg_hex = "(none)";
500
501 cause = sgs_cause_from_msg(sgc, msg, tp, NULL);
502 if (cause < 0)
503 return 0;
504
505 if (imsi[0] != '\0')
506 imsi_ptr = imsi;
507 else
508 imsi_ptr = "<none>";
509
510 if (TLVP_PRESENT(tp, SGSAP_IE_ERR_MSG))
511 err_msg = TLVP_VAL(tp, SGSAP_IE_ERR_MSG);
512 else
513 err_msg = NULL;
514
515 if (err_msg)
516 err_msg_hex = osmo_hexdump(err_msg, TLVP_LEN(tp, SGSAP_IE_ERR_MSG));
517
518 LOGSGC(sgc, LOGL_NOTICE, "Rx STATUS cause=%s, IMSI=%s, err_msg=%s\n",
519 sgsap_sgs_cause_name(cause), imsi_ptr, err_msg_hex);
520
521 return 0;
522}
523
524/* SGsAP-RESET-INDICATION 3GPP TS 29.118, chapter 8.16 */
525static int sgs_rx_reset_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp)
526{
527 struct gsm29118_reset_msg reset_params;
528 struct msgb *resp;
529
530 memset(&reset_params, 0, sizeof(reset_params));
531 osmo_strlcpy(reset_params.vlr_name, sgc->sgs->cfg.vlr_name, sizeof(reset_params.vlr_name));
532 reset_params.vlr_name_present = true;
533
534 resp = gsm29118_create_reset_ack(&reset_params);
535
536 /* Perform a reset of the SGS FSM of all subscribers that are present in the VLR */
537 vlr_sgs_reset(gsm_network->vlr);
538
539 sgs_tx(sgc, resp);
540 return 0;
541}
542
543/* SGsAP-RESET-ACK 3GPP TS 29.118, chapter 8.15 */
544static int sgs_rx_reset_ack(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp)
545{
546 /* dispatch event to VLR reset FSM for this MME */
547 if (sgc->mme && sgc->mme->fi)
548 osmo_fsm_inst_dispatch(sgc->mme->fi, SGS_VLRR_E_RX_RESET_ACK, msg);
549 return 0;
550}
551
552/* SGsAP-LOCATION-UPDATE-REQUEST 3GPP TS 29.118, chapter 8.11 */
553static int sgs_rx_loc_upd_req(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
554{
555 struct msgb *resp;
556 const uint8_t *lu_type_ie;
557 enum vlr_lu_type type;
558 struct osmo_location_area_id new_lai;
559 const struct gsm48_loc_area_id *gsm48_lai;
560 int rc;
561 char *mme_name;
562 struct vlr_sgs_cfg vlr_sgs_cfg;
563 struct vlr_subscr *vsub;
564
565 /* Check for lingering connections */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100566 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100567 if (vsub) {
568 subscr_conn_toss(vsub);
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100569 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100570 }
571
572 /* Determine MME-Name */
573 mme_name = sgs_mme_fqdn_get(sgc);
574 if (!mme_name) {
575 resp = gsm29118_create_lu_rej(imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, NULL);
576 sgs_tx(sgc, resp);
577 return 0;
578 }
579
580 /* Parse LU-Type */
581 lu_type_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_EPS_LU_TYPE, 1);
582 if (!lu_type_ie)
583 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_EPS_LU_TYPE);
584 if (lu_type_ie[0] == 0x01)
585 type = VLR_LU_TYPE_IMSI_ATTACH;
586 else
587 type = VLR_LU_TYPE_REGULAR;
588
589 /* Parse LAI of the new location */
590 gsm48_lai = (struct gsm48_loc_area_id *)TLVP_VAL_MINLEN(tp, SGSAP_IE_LAI, 5);
591 if (!gsm48_lai)
592 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_LAI);
593 gsm48_decode_lai2(gsm48_lai, &new_lai);
594
595 /* Perform actual location update */
596 memcpy(vlr_sgs_cfg.timer, sgc->sgs->cfg.timer, sizeof(vlr_sgs_cfg.timer));
597 memcpy(vlr_sgs_cfg.counter, sgc->sgs->cfg.counter, sizeof(vlr_sgs_cfg.counter));
598 rc = vlr_sgs_loc_update(gsm_network->vlr, &vlr_sgs_cfg, sgs_tx_loc_upd_resp_cb, sgs_iface_tx_paging,
599 sgs_tx_mm_info_cb, mme_name, type, imsi, &new_lai);
600 if (rc != 0) {
601 resp = gsm29118_create_lu_rej(imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, NULL);
602 sgs_tx(sgc, resp);
603 }
604
605 return 0;
606}
607
608/* SGsAP-IMSI-DETACH-INDICATION 3GPP TS 29.118, chapter 8.8 */
609static int sgs_rx_imsi_det_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
610{
611 struct msgb *resp;
612 enum sgsap_imsi_det_noneps_type type;
613 const uint8_t *type_ie;
614
615 type_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_IMSI_DET_NONEPS_TYPE, 1);
616 if (!type_ie)
617 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_IMSI_DET_NONEPS_TYPE);
618
619 switch (type_ie[0]) {
620 case SGSAP_ID_NONEPS_T_EXPLICIT_UE_NONEPS:
621 type = SGSAP_ID_NONEPS_T_EXPLICIT_UE_NONEPS;
622 break;
623 case SGSAP_ID_NONEPS_T_COMBINED_UE_EPS_NONEPS:
624 type = SGSAP_ID_NONEPS_T_COMBINED_UE_EPS_NONEPS;
625 break;
626 case SGSAP_ID_NONEPS_T_IMPLICIT_UE_EPS_NONEPS:
627 type = SGSAP_ID_NONEPS_T_IMPLICIT_UE_EPS_NONEPS;
628 break;
629 default:
630 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_INVALID_MAND_IE, msg, SGSAP_IE_IMSI_DET_NONEPS_TYPE);
631 break;
632 }
633
634 vlr_sgs_imsi_detach(gsm_network->vlr, imsi, type);
635 resp = gsm29118_create_imsi_det_ack(imsi);
636 sgs_tx(sgc, resp);
637
638 return 0;
639}
640
641/* SGsAP-EPS-DETACH-INDICATION 3GPP TS 29.118, chapter 8.6 */
642static int sgs_rx_eps_det_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
643{
644 struct msgb *resp;
645 enum sgsap_imsi_det_eps_type type;
646 const uint8_t *type_ie;
647
648 type_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_IMSI_DET_EPS_TYPE, 1);
649 if (!type_ie)
650 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_IMSI_DET_EPS_TYPE);
651
652 switch (type_ie[0]) {
653 case SGSAP_ID_EPS_T_NETWORK_INITIATED:
654 type = SGSAP_ID_EPS_T_NETWORK_INITIATED;
655 break;
656 case SGSAP_ID_EPS_T_UE_INITIATED:
657 type = SGSAP_ID_EPS_T_UE_INITIATED;
658 break;
659 case SGSAP_ID_EPS_T_EPS_NOT_ALLOWED:
660 type = SGSAP_ID_EPS_T_EPS_NOT_ALLOWED;
661 break;
662 default:
663 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_INVALID_MAND_IE, msg, SGSAP_IE_IMSI_DET_EPS_TYPE);
664 break;
665 }
666
667 vlr_sgs_eps_detach(gsm_network->vlr, imsi, type);
668 resp = gsm29118_create_eps_det_ack(imsi);
669 sgs_tx(sgc, resp);
670
671 return 0;
672}
673
674/* SGsAP-PAGING-REJECT 3GPP TS 29.118, chapter 8.13 */
675static int sgs_rx_pag_rej(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
676{
677 int cause;
678 struct vlr_subscr *vsub;
679
680 cause = sgs_cause_from_msg(sgc, msg, tp, NULL);
681 if (cause < 0)
682 return 0;
683
684 /* Subscriber must be known by the VLR */
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100685 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100686 if (!vsub)
687 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, msg, SGSAP_IE_IMSI);
688
689 /* Inform the VLR */
690 vlr_sgs_pag_rej(gsm_network->vlr, imsi, cause);
691
692 /* Stop all paging activity */
693 subscr_paging_cancel(vsub, GSM_PAGING_EXPIRED);
694
695 /* Depending on the cause code some action is required */
696 if (cause == SGSAP_SGS_CAUSE_MT_CSFB_REJ_USER) {
697 /* FIXME: We are supposed to trigger a User Determined User Busy (UDUB)
698 * as specified in 3GPP TS 24.082 here, SGs association state shall not
699 * be changed */
700 LOGSGC(sgc, LOGL_ERROR,
701 "Rx %s with SGSAP_SGS_CAUSE_MT_CSFB_REJ_USER, but sending UDUP is not implemented yet!\n",
702 sgsap_msg_type_name(msg->data[0]));
703 } else if (cause == SGSAP_SGS_CAUSE_IMSI_DET_EPS) {
704 /* FIXME: In this case we should send the paging via A/Iu interface */
705 OSMO_ASSERT(false);
706 }
707
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100708 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100709 return 0;
710}
711
712/* SGsAP-UE-UNREACHABLE 3GPP TS 29.118, chapter 8.21 */
713static int sgs_rx_ue_unr(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
714{
715 int cause;
716
717 cause = sgs_cause_from_msg(sgc, msg, tp, NULL);
718 if (cause < 0)
719 return 0;
720
721 vlr_sgs_ue_unr(gsm_network->vlr, imsi, cause);
722
723 return 0;
724}
725
726/* SGsAP-TMSI-REALLOCATION-COMPLETE 3GPP TS 29.118, chapter 8.19 */
727static int sgs_rx_tmsi_reall_cmpl(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
728{
729 vlr_sgs_tmsi_reall_compl(gsm_network->vlr, imsi);
730 return 0;
731}
732
733/* SGsAP-SERVICE-REQUEST 3GPP TS 29.118, chapter 8.17 */
734static int sgs_rx_service_req(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
735{
736 enum sgsap_service_ind serv_ind;
737 const uint8_t *serv_ind_ie;
738 struct ran_conn *conn;
739 struct vlr_subscr *vsub;
740
741 /* Note: While in other RAN concepts a service request is used to
742 * initiate mobile originated operation, the service request in SGsAP
743 * is comparable to a paging response. The SGsAP SERVICE REQUEST must
744 * not be confused or compared with a CM SERVICE REQUEST! */
745
746 if (!check_sgs_association(sgc, msg, imsi))
747 return 0;
748
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100749 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100750 /* Note: vsub is already sufficiently verified by check_sgs_association(),
751 * we must have a vsub at this point! */
752 OSMO_ASSERT(vsub);
753
754 /* The Service request is intended as a paging response, if one is
755 * received while nothing is paging something is very wrong! */
756 if (!vlr_sgs_pag_pend(vsub)) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100757 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100758 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE, msg, -1);
759 }
760 serv_ind_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_SERVICE_INDICATOR, 1);
761
762 if (!serv_ind_ie) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100763 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100764 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_SERVICE_INDICATOR);
765 }
766 if (serv_ind_ie[0] == SGSAP_SERV_IND_CS_CALL)
767 serv_ind = serv_ind_ie[0];
768 else if (serv_ind_ie[0] == SGSAP_SERV_IND_SMS)
769 serv_ind = serv_ind_ie[0];
770 else {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100771 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100772 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_INVALID_MAND_IE, msg, SGSAP_IE_SERVICE_INDICATOR);
773 }
774
775 /* FIXME: The MME shall include an UE EMM Mode IE, but the field is
776 * marked optional. (Why do we need this info at all?) */
777
778 /* Report to the VLR that the paging has successfully completed */
779 vlr_sgs_pag_ack(gsm_network->vlr, imsi);
780
781 /* Exit early when the service indicator indicates that a call is being
782 * established. In those cases we do not allocate a connection, instead
783 * the connection will be allocated when the MS is appearing on the
784 * A-Interface. */
785 if (serv_ind == SGSAP_SERV_IND_CS_CALL) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100786 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100787 return 0;
788 }
789
790 /* Allocate subscriber connection */
791 conn = subscr_conn_allocate_sgs(sgc, vsub, true);
792 if (!conn) {
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100793 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100794 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE, msg, -1);
795 }
796
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100797 /* The conn has added a get() for the vsub, balance above vlr_subscr_find_by_imsi() */
798 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100799 return 0;
800}
801
802/* SGsAP-UPLINK-UNITDATA 3GPP TS 29.118, chapter 8.22 */
803static int sgs_rx_ul_ud(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
804{
805 struct dtap_header *dtap;
806 struct ran_conn *conn;
Harald Welte0df904d2018-12-03 11:00:04 +0100807 const uint8_t *nas_msg_container_ie;
808 struct vlr_subscr *vsub;
809
810 if (!check_sgs_association(sgc, msg, imsi))
811 return 0;
812
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100813 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100814 /* Note: vsub is already sufficiently verified by check_sgs_association(),
815 * we must have a vsub at this point! */
816 OSMO_ASSERT(vsub);
817
818 /* Try to find existing connection (MT) or allocate a new one (MO) */
819 conn = connection_for_subscr(vsub);
820 if (!conn) {
821 conn = subscr_conn_allocate_sgs(sgc, vsub, false);
Harald Welte0df904d2018-12-03 11:00:04 +0100822 } else {
823 if (conn->via_ran != OSMO_RAT_EUTRAN_SGS) {
824 LOGSGC(sgc, LOGL_ERROR,
825 "Receiving uplink unit-data for non-sgs connection -- discarding message!\n");
826 msgb_free(msg);
827 return 0;
828 }
829 }
830
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100831 /* Balance above vlr_subscr_find_by_imsi() */
832 vlr_subscr_put(vsub, __func__);
833
Harald Welte0df904d2018-12-03 11:00:04 +0100834 /* If we do not find an existing connection and allocating a new one
835 * faild, give up and return status. */
836 if (!conn) {
Harald Welte0df904d2018-12-03 11:00:04 +0100837 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE, msg, 0);
838 }
839
840 nas_msg_container_ie = TLVP_VAL_MINLEN(tp, SGSAP_IE_NAS_MSG_CONTAINER, 1);
841 if (!nas_msg_container_ie) {
Harald Welte0df904d2018-12-03 11:00:04 +0100842 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_MISSING_MAND_IE, msg, SGSAP_IE_NAS_MSG_CONTAINER);
843 }
844
845 /* ran_conn_dtap expects the dtap payload in l3h */
846 dtap = (struct dtap_header *)nas_msg_container_ie;
847 msg->l3h = (uint8_t *) nas_msg_container_ie;
848 OMSC_LINKID_CB(msg) = dtap->link_id;
849
850 /* Forward dtap payload into the msc */
851 ran_conn_dtap(conn, msg);
852
Harald Welte0df904d2018-12-03 11:00:04 +0100853 return 0;
854}
855
856/* SGsAP-MO-CSFB-INDICATION, chapter 8.25 */
857static int sgs_rx_csfb_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
858{
859 struct vlr_subscr *vsub;
860
861 /* The MME informs us with this message that the UE has returned back
862 * to the 4G network, so we use the SGs interface again for further
863 * communication with the UE. */
864
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100865 vsub = vlr_subscr_find_by_imsi(gsm_network->vlr, imsi, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100866 if (!vsub)
867 return sgs_tx_status(sgc, imsi, SGSAP_SGS_CAUSE_IMSI_UNKNOWN, msg, SGSAP_IE_IMSI);
868
869 /* Check for lingering connections */
870 subscr_conn_toss(vsub);
871
872 vsub->cs.attached_via_ran = OSMO_RAT_EUTRAN_SGS;
Neels Hofmeyr7c5346c2019-02-19 02:36:35 +0100873 vlr_subscr_put(vsub, __func__);
Harald Welte0df904d2018-12-03 11:00:04 +0100874 return 0;
875}
876
877/* SGsAP-UE-ACTIVITY-INDICATION, chapter 8.20 */
878static int sgs_rx_ue_act_ind(struct sgs_connection *sgc, struct msgb *msg, const struct tlv_parsed *tp, char *imsi)
879{
880 /* In this MSC/VLR implementation we do not support the alerting
881 * procedure yet and therefore we will never request any alerting
882 * at the MME. Given that it is unlikely that we ever get activity
883 * indications from the MME, but if we do we should not act all too
884 * hostile and ignore the indication silently. */
885
886 LOGSGC(sgc, LOGL_ERROR, "Rx %s unexpected, we do not implement alerting yet, ignoring!\n",
887 sgsap_msg_type_name(msg->data[0]));
888
889 return 0;
890}
891
892#define TX_STATUS_AND_LOG(sgc, msg_type, cause, fmt) \
893 LOGSGC(sgc, LOGL_ERROR, fmt, sgsap_msg_type_name(msg_type)); \
894 resp = gsm29118_create_status(NULL, cause, msg); \
895 sgs_tx(sgc, resp); \
896
897/*! Process incoming SGs message (see sgs_server.c)
898 * \param[in] sgc related sgs connection
899 * \param[in] msg received message
900 * \returns 0 in case of success, -EINVAL in case of error. */
901int sgs_iface_rx(struct sgs_connection *sgc, struct msgb *msg)
902{
903 struct msgb *resp;
904 uint8_t msg_type = msg->l2h[0];
905 struct tlv_parsed tp;
906 int rc;
907 char imsi[GSM48_MI_SIZE];
908 char mme_name[SGS_MME_NAME_LEN + 1];
909
910 memset(imsi, 0, sizeof(imsi));
911 memset(mme_name, 0, sizeof(mme_name));
912
913 /* When the receiving entity receives a message that is too short to contain a complete
914 * message type information element, the receiving entity shall ignore that message. */
915 if (msgb_l2len(msg) < 1)
916 goto error;
917
918 /* Parse TLV elements */
919 rc = tlv_parse(&tp, &sgsap_ie_tlvdef, msgb_l2(msg) + 1, msgb_l2len(msg) - 1, 0, 0);
920 if (rc < 0) {
921 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_SEMANT_INCORR_MSG, "SGsAP Message %s parsing error\n");
922 goto error;
923 }
924
925 /* Most of the messages contain an IMSI as mandatory IE, parse it right here */
926 if (!TLVP_PRESENT(&tp, SGSAP_IE_IMSI) &&
927 msg_type != SGSAP_MSGT_STATUS && msg_type != SGSAP_MSGT_RESET_IND && msg_type != SGSAP_MSGT_RESET_ACK) {
928 /* reject the message; all but the three above have mandatory IMSI */
929 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_MISSING_MAND_IE,
930 "SGsAP Message %s without IMSI, dropping\n");
931 goto error;
932 }
933
934 if (TLVP_PRESENT(&tp, SGSAP_IE_IMSI)) {
935 gsm48_mi_to_string(imsi, sizeof(imsi), TLVP_VAL(&tp, SGSAP_IE_IMSI), TLVP_LEN(&tp, SGSAP_IE_IMSI));
936 if (strlen(imsi) < GSM23003_IMSI_MIN_DIGITS) {
937 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_INVALID_MAND_IE,
938 "SGsAP Message %s with short IMSI, dropping\n");
939 goto error;
940 }
941 }
942
943 /* Some messages contain an MME-NAME as mandatore IE, parse it right here. The
944 * MME-NAME is als immediately registered with the sgc, so it will be implicitly
945 * known to all functions that have access to the sgc context. */
946 if (!TLVP_PRESENT(&tp, SGSAP_IE_MME_NAME)
947 && (msg_type == SGSAP_MSGT_RESET_IND || msg_type == SGSAP_MSGT_RESET_ACK
948 || msg_type == SGSAP_MSGT_LOC_UPD_REQ || msg_type == SGSAP_MSGT_IMSI_DET_IND
949 || msg_type == SGSAP_MSGT_EPS_DET_IND)) {
950 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_MISSING_MAND_IE,
951 "SGsAP Message %s without MME-Name, dropping\n");
952 goto error;
953 }
954
955 if (TLVP_PRESENT(&tp, SGSAP_IE_MME_NAME)) {
956 if (decode_mme_name(mme_name, &tp) != 0) {
957 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_INVALID_MAND_IE,
958 "SGsAP Message %s with invalid MME-Name, dropping\n");
959 goto error;
960 }
961 /* Regsister/check mme_name with sgc */
962 if (sgs_mme_fqdn_received(sgc, mme_name) < 0) {
963 TX_STATUS_AND_LOG(sgc, msg_type, SGSAP_SGS_CAUSE_MSG_INCOMP_STATE,
964 "SGsAP Message %s with invalid MME-Name, dropping\n");
965 goto error;
966 }
967 }
968
969 /* dispatch msg to various handler functions. msgb ownership remains here! */
970 rc = -EINVAL;
971 switch (msg_type) {
972 case SGSAP_MSGT_STATUS:
973 rc = sgs_rx_status(sgc, msg, &tp, imsi);
974 break;
975 case SGSAP_MSGT_RESET_IND:
976 rc = sgs_rx_reset_ind(sgc, msg, &tp);
977 break;
978 case SGSAP_MSGT_RESET_ACK:
979 rc = sgs_rx_reset_ack(sgc, msg, &tp);
980 break;
981 case SGSAP_MSGT_LOC_UPD_REQ:
982 rc = sgs_rx_loc_upd_req(sgc, msg, &tp, imsi);
983 break;
984 case SGSAP_MSGT_IMSI_DET_IND:
985 rc = sgs_rx_imsi_det_ind(sgc, msg, &tp, imsi);
986 break;
987 case SGSAP_MSGT_EPS_DET_IND:
988 rc = sgs_rx_eps_det_ind(sgc, msg, &tp, imsi);
989 break;
990 case SGSAP_MSGT_PAGING_REJ:
991 rc = sgs_rx_pag_rej(sgc, msg, &tp, imsi);
992 break;
993 case SGSAP_MSGT_UE_UNREACHABLE:
994 rc = sgs_rx_ue_unr(sgc, msg, &tp, imsi);
995 break;
996 case SGSAP_MSGT_TMSI_REALL_CMPL:
997 rc = sgs_rx_tmsi_reall_cmpl(sgc, msg, &tp, imsi);
998 break;
999 case SGSAP_MSGT_SERVICE_REQ:
1000 rc = sgs_rx_service_req(sgc, msg, &tp, imsi);
1001 break;
1002 case SGSAP_MSGT_UL_UD:
1003 rc = sgs_rx_ul_ud(sgc, msg, &tp, imsi);
1004 break;
1005 case SGSAP_MSGT_MO_CSFB_IND:
1006 rc = sgs_rx_csfb_ind(sgc, msg, &tp, imsi);
1007 break;
1008 case SGSAP_MSGT_UE_ACT_IND:
1009 rc = sgs_rx_ue_act_ind(sgc, msg, &tp, imsi);
1010 break;
1011 case SGSAP_MSGT_ALERT_ACK:
1012 case SGSAP_MSGT_ALERT_REJ:
1013 LOGSGC(sgc, LOGL_ERROR, "Rx unmplemented SGsAP %s: %s\n",
1014 sgsap_msg_type_name(msg_type), msgb_hexdump(msg));
1015 resp = gsm29118_create_status(imsi, SGSAP_SGS_CAUSE_MSG_UNKNOWN, msg);
1016 sgs_tx(sgc, resp);
1017 rc = 0;
1018 break;
1019 default:
1020 LOGSGC(sgc, LOGL_ERROR, "Rx unknown SGsAP message type 0x%02x: %s\n", msg_type, msgb_hexdump(msg));
1021 resp = gsm29118_create_status(imsi, SGSAP_SGS_CAUSE_MSG_UNKNOWN, msg);
1022 sgs_tx(sgc, resp);
1023 rc = 0;
1024 break;
1025 }
1026
1027 /* Catch unhandled errors */
1028 if (rc < 0) {
1029 /* Note: Usually the sgs_rx_ should catch errors locally and
1030 * eimit a status message with proper cause code, including
1031 * a suitable log message. If we end up here, something is
1032 * not right and should be fixed */
1033 LOGSGC(sgc, LOGL_ERROR, "Rx unable to decode SGsAP %s: %s\n",
1034 sgsap_msg_type_name(msg_type), msgb_hexdump(msg));
1035 resp = gsm29118_create_status(imsi, SGSAP_SGS_CAUSE_MSG_UNKNOWN, msg);
1036 sgs_tx(sgc, resp);
1037 }
1038
1039error:
1040 msgb_free(msg);
1041 return 0;
1042}
1043
1044/***********************************************************************
1045 * SGs connection "VLR Reset Procedure" FSM
1046 ***********************************************************************/
1047
1048static const struct value_string sgs_vlr_reset_fsm_event_names[] = {
1049 {SGS_VLRR_E_START_RESET, "START-RESET"},
1050 {SGS_VLRR_E_RX_RESET_ACK, "RX-RESET-ACK"},
1051 {0, NULL}
1052};
1053
1054static void sgs_vlr_reset_fsm_null(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1055{
1056 switch (event) {
1057 case SGS_VLRR_E_RX_RESET_ACK:
1058 break;
1059 default:
1060 OSMO_ASSERT(0);
1061 break;
1062 }
1063}
1064
1065static void sgs_vlr_reset_fsm_wait_ack(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1066{
1067 switch (event) {
1068 case SGS_VLRR_E_RX_RESET_ACK:
1069 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_COMPLETE, 0, 0);
1070 break;
1071 default:
1072 OSMO_ASSERT(0);
1073 break;
1074 }
1075}
1076
1077static void sgs_vlr_reset_fsm_complete(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1078{
1079 switch (event) {
1080 case SGS_VLRR_E_RX_RESET_ACK:
1081 break;
1082 default:
1083 OSMO_ASSERT(0);
1084 break;
1085 }
1086}
1087
1088static void sgs_vlr_reset_fsm_allstate(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1089{
1090 struct msgb *reset_ind;
1091 struct gsm29118_reset_msg reset_params;
1092 struct sgs_mme_ctx *mme = (struct sgs_mme_ctx *)fi->priv;
1093 struct sgs_connection *sgc = mme->conn;
1094 struct sgs_state *sgs = mme->sgs;
1095
1096 switch (event) {
1097 case SGS_VLRR_E_START_RESET:
1098 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_NULL, 0, 0);
1099 mme->ns11_remaining = sgs->cfg.counter[SGS_STATE_NS11];
1100 /* send a reset message and enter WAIT_ACK state */
1101 memset(&reset_params, 0, sizeof(reset_params));
1102 osmo_strlcpy(reset_params.vlr_name, sgs->cfg.vlr_name, sizeof(reset_params.vlr_name));
1103 reset_params.vlr_name_present = true;
1104 reset_ind = gsm29118_create_reset_ind(&reset_params);
1105 sgs_tx(sgc, reset_ind);
1106 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_WAIT_ACK, sgs->cfg.timer[SGS_STATE_TS11], 11);
1107 break;
1108 default:
1109 OSMO_ASSERT(0);
1110 break;
1111 }
1112}
1113
1114static int sgs_vlr_reset_fsm_timer_cb(struct osmo_fsm_inst *fi)
1115{
1116 struct msgb *reset_ind;
1117 struct gsm29118_reset_msg reset_params;
1118 struct sgs_mme_ctx *mme = (struct sgs_mme_ctx *)fi->priv;
1119 struct sgs_connection *sgc = mme->conn;
1120 struct sgs_state *sgs = mme->sgs;
1121
1122 switch (fi->T) {
1123 case 11:
1124 if (mme->ns11_remaining >= 1) {
1125 memset(&reset_params, 0, sizeof(reset_params));
1126 osmo_strlcpy(reset_params.vlr_name, sgs->cfg.vlr_name, sizeof(reset_params.vlr_name));
1127 reset_params.vlr_name_present = true;
1128 reset_ind = gsm29118_create_reset_ind(&reset_params);
1129 sgs_tx(sgc, reset_ind);
1130 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_WAIT_ACK, sgs->cfg.timer[SGS_STATE_TS11], 11);
1131 mme->ns11_remaining--;
1132 } else {
1133 LOGMME(mme, LOGL_ERROR, "Ts11 expired more than %u (Ns11) times, giving up\n",
Vadim Yanitskiye9ef7c62019-02-23 16:04:17 +07001134 sgs->cfg.counter[SGS_STATE_NS11]);
Harald Welte0df904d2018-12-03 11:00:04 +01001135 osmo_fsm_inst_state_chg(fi, SGS_VLRR_ST_NULL, 0, 0);
1136 }
1137 break;
1138 default:
1139 OSMO_ASSERT(0);
1140 break;
1141 }
1142 return 0;
1143}
1144
1145static const struct osmo_fsm_state sgs_vlr_reset_fsm_states[] = {
1146 [SGS_VLRR_ST_NULL] = {
1147 /* We haven't even tried yet to send a RESET */
1148 .name = "NULL",
1149 .action = sgs_vlr_reset_fsm_null,
1150 .in_event_mask = S(SGS_VLRR_E_RX_RESET_ACK),
1151 .out_state_mask = S(SGS_VLRR_ST_NULL) | S(SGS_VLRR_ST_WAIT_ACK),
1152 },
1153 [SGS_VLRR_ST_WAIT_ACK] = {
1154 /* We're waiting for a SGsAP_RESET_ACK */
1155 .name = "WAIT-ACK",
1156 .action = sgs_vlr_reset_fsm_wait_ack,
1157 .in_event_mask = S(SGS_VLRR_E_RX_RESET_ACK),
1158 .out_state_mask = S(SGS_VLRR_ST_NULL) |
1159 S(SGS_VLRR_ST_COMPLETE) | S(SGS_VLRR_ST_WAIT_ACK),
1160 },
1161 [SGS_VLRR_ST_COMPLETE] = {
1162 /* Reset procedure to this MME has been completed */
1163 .name = "COMPLETE",
1164 .action = sgs_vlr_reset_fsm_complete,
1165 .in_event_mask = S(SGS_VLRR_E_RX_RESET_ACK),
1166 .out_state_mask = S(SGS_VLRR_ST_NULL) | S(SGS_VLRR_ST_COMPLETE),
1167 },
1168};
1169
1170static struct osmo_fsm sgs_vlr_reset_fsm = {
1171 .name = "SGs-VLR-RESET",
1172 .states = sgs_vlr_reset_fsm_states,
1173 .allstate_event_mask = S(SGS_VLRR_E_START_RESET),
1174 .allstate_action = sgs_vlr_reset_fsm_allstate,
1175 .timer_cb = sgs_vlr_reset_fsm_timer_cb,
1176 .log_subsys = DSGS,
1177 .event_names = sgs_vlr_reset_fsm_event_names,
1178};
1179
1180/*! Send unit-data through SGs interface (see msc_ifaces.c)
1181 * \param[in] msg layer 3 message to send.
1182 * \returns 0 in case of success, -EINVAL in case of error. */
1183int sgs_iface_tx_dtap_ud(struct msgb *msg)
1184{
1185 struct ran_conn *conn;
1186 struct vlr_subscr *vsub;
1187 struct msgb *msg_sgs;
1188 struct sgs_mme_ctx *mme;
1189 int rc = -EINVAL;
1190
1191 /* This function expects a pointer to the related gsm subscriber
1192 * connection (conn) in msg->dst. Also conn->vsub must point to
1193 * the related subscriber */
1194
1195 OSMO_ASSERT(msg->dst);
1196 conn = msg->dst;
1197 OSMO_ASSERT(conn->vsub);
1198 vsub = conn->vsub;
1199
1200 mme = sgs_mme_ctx_by_vsub(vsub, SGSAP_MSGT_DL_UD);
1201 if (!mme)
1202 goto error;
1203
1204 /* Make sure the subscriber has a valid SGs association, otherwise
1205 * don't let unit-data through. */
1206 if (vsub->sgs_fsm->state != SGS_UE_ST_ASSOCIATED) {
1207 LOG_RAN_CONN(conn, LOGL_NOTICE, "Tx %s subscriber not SGs-associated, dropping\n",
1208 sgsap_msg_type_name(SGSAP_MSGT_DL_UD));
1209 goto error;
1210 }
1211
1212 msg_sgs = gsm29118_create_dl_ud(vsub->imsi, msg);
1213 sgs_tx(mme->conn, msg_sgs);
1214 rc = 0;
1215
1216error:
1217 msgb_free(msg);
1218 return rc;
1219}
1220
1221/*! Send a relase message through SGs interface (see msc_ifaces.c)
1222 * \param[in] msg layer 3 message to send.
1223 * \returns 0 in case of success, -EINVAL in case of error. */
1224void sgs_iface_tx_release(struct ran_conn *conn)
1225{
1226 struct msgb *msg_sgs;
1227 struct vlr_subscr *vsub;
1228 struct sgs_mme_ctx *mme;
1229
1230 /*! Use this function to release an SGs connection normally
1231 * (cause code is 0). This function also automatically causes
1232 * the VLR subscriber usage to be balanced. */
1233
1234 OSMO_ASSERT(conn->vsub);
1235 vsub = conn->vsub;
1236
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}