blob: 2e88a327b3bae0ecad34988beb680264b1bf079b [file] [log] [blame]
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001/* Code to manage a subscriber's MSC-A role */
2/*
Vadim Yanitskiy999a5932023-05-18 17:22:26 +07003 * (C) 2019 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01004 * All Rights Reserved
5 *
6 * SPDX-License-Identifier: AGPL-3.0+
7 *
8 * Author: Neels Hofmeyr
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
19 *
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#include <osmocom/core/utils.h>
25#include <osmocom/core/tdef.h>
26#include <osmocom/core/rate_ctr.h>
27#include <osmocom/core/signal.h>
28
29#include <osmocom/msc/gsm_data.h>
30#include <osmocom/msc/msc_roles.h>
31#include <osmocom/msc/msub.h>
32#include <osmocom/msc/msc_a.h>
33#include <osmocom/msc/msc_t.h>
34#include <osmocom/msc/msc_i.h>
35#include <osmocom/msc/paging.h>
36#include <osmocom/msc/signal.h>
37#include <osmocom/msc/vlr.h>
38#include <osmocom/msc/transaction.h>
Oliver Smithceca8e62023-05-24 11:15:52 +020039#include <osmocom/msc/transaction_cc.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010040#include <osmocom/msc/ran_peer.h>
41#include <osmocom/msc/ran_msg_a.h>
42#include <osmocom/msc/ran_msg_iu.h>
43#include <osmocom/msc/sgs_iface.h>
44#include <osmocom/msc/gsm_04_08.h>
45#include <osmocom/msc/gsm_09_11.h>
46#include <osmocom/msc/gsm_04_14.h>
47#include <osmocom/msc/call_leg.h>
48#include <osmocom/msc/rtp_stream.h>
49#include <osmocom/msc/msc_ho.h>
Neels Hofmeyre276ae92022-01-13 21:38:35 +010050#include <osmocom/msc/codec_mapping.h>
Andreas Eversbergf7c6f142023-04-23 12:10:42 +020051#include <osmocom/msc/msc_vgcs.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010052
53#define MSC_A_USE_WAIT_CLEAR_COMPLETE "wait-Clear-Complete"
54
55static struct osmo_fsm msc_a_fsm;
56
57static const struct osmo_tdef_state_timeout msc_a_fsm_timeouts[32] = {
58 [MSC_A_ST_VALIDATE_L3] = { .T = -1 },
59 [MSC_A_ST_AUTH_CIPH] = { .keep_timer = true },
60 [MSC_A_ST_WAIT_CLASSMARK_UPDATE] = { .keep_timer = true },
61 [MSC_A_ST_AUTHENTICATED] = { .keep_timer = true },
62 [MSC_A_ST_RELEASING] = { .T = -2 },
63 [MSC_A_ST_RELEASED] = { .T = -2 },
64};
65
66/* Transition to a state, using the T timer defined in msc_a_fsm_timeouts.
67 * The actual timeout value is in turn obtained from network->T_defs.
68 * Assumes local variable fi exists. */
Neels Hofmeyr01653252019-09-03 02:06:22 +020069#define msc_a_state_chg_always(msc_a, state) \
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010070 osmo_tdef_fsm_inst_state_chg((msc_a)->c.fi, state, msc_a_fsm_timeouts, (msc_a)->c.ran->tdefs, 5)
71
Neels Hofmeyr01653252019-09-03 02:06:22 +020072/* Same as msc_a_state_chg_always() but ignore if the msc_a already is in the target state. */
73#define msc_a_state_chg(msc_a, STATE) do { \
74 if ((msc_a)->c.fi->state != STATE) \
75 msc_a_state_chg_always(msc_a, STATE); \
76 } while(0)
77
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010078struct gsm_network *msc_a_net(const struct msc_a *msc_a)
79{
80 return msub_net(msc_a->c.msub);
81}
82
83struct vlr_subscr *msc_a_vsub(const struct msc_a *msc_a)
84{
Neels Hofmeyr911e5972019-05-09 13:28:26 +020085 if (!msc_a)
86 return NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010087 return msub_vsub(msc_a->c.msub);
88}
89
90struct msc_i *msc_a_msc_i(const struct msc_a *msc_a)
91{
Neels Hofmeyr911e5972019-05-09 13:28:26 +020092 if (!msc_a)
93 return NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010094 return msub_msc_i(msc_a->c.msub);
95}
96
97struct msc_t *msc_a_msc_t(const struct msc_a *msc_a)
98{
Neels Hofmeyr911e5972019-05-09 13:28:26 +020099 if (!msc_a)
100 return NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100101 return msub_msc_t(msc_a->c.msub);
102}
103
104struct msc_a *msc_a_fi_priv(struct osmo_fsm_inst *fi)
105{
106 OSMO_ASSERT(fi);
107 OSMO_ASSERT(fi->fsm == &msc_a_fsm);
108 OSMO_ASSERT(fi->priv);
109 return fi->priv;
110}
111
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200112bool msc_a_is_ciphering_to_be_attempted(const struct msc_a *msc_a)
Alexander Couzens2c5e4612021-11-05 02:00:17 +0100113{
114 struct gsm_network *net = msc_a_net(msc_a);
115 bool is_utran = (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU);
116 if (is_utran)
117 return net->uea_encryption_mask > (1 << OSMO_UTRAN_UEA0);
118 else
119 return net->a5_encryption_mask > 0x1;
120}
121
Neels Hofmeyr2ea72642022-10-10 23:35:47 +0200122bool msc_a_is_ciphering_required(const struct msc_a *msc_a)
123{
124 struct gsm_network *net = msc_a_net(msc_a);
125 bool is_utran = (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU);
126 if (is_utran)
127 return net->uea_encryption_mask
128 && ((net->uea_encryption_mask & (1 << OSMO_UTRAN_UEA0)) == 0);
129 else
130 return net->a5_encryption_mask
131 && ((net->a5_encryption_mask & 0x1) == 0);
132}
133
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100134static void update_counters(struct osmo_fsm_inst *fi, bool conn_accepted)
135{
136 struct msc_a *msc_a = fi->priv;
137 struct gsm_network *net = msc_a_net(msc_a);
138 switch (msc_a->complete_layer3_type) {
139 case COMPLETE_LAYER3_LU:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200140 rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs, conn_accepted ? MSC_CTR_LOC_UPDATE_COMPLETED : MSC_CTR_LOC_UPDATE_FAILED));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100141 break;
142 case COMPLETE_LAYER3_CM_SERVICE_REQ:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200143 rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs, conn_accepted ? MSC_CTR_CM_SERVICE_REQUEST_ACCEPTED : MSC_CTR_CM_SERVICE_REQUEST_REJECTED));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100144 break;
145 case COMPLETE_LAYER3_PAGING_RESP:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200146 rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs, conn_accepted ? MSC_CTR_PAGING_RESP_ACCEPTED : MSC_CTR_PAGING_RESP_REJECTED));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100147 break;
Neels Hofmeyrae98b972021-07-27 03:46:49 +0200148 case COMPLETE_LAYER3_CM_RE_ESTABLISH_REQ:
149 rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs,
150 conn_accepted ? MSC_CTR_CM_RE_ESTABLISH_REQ_ACCEPTED
151 : MSC_CTR_CM_RE_ESTABLISH_REQ_REJECTED));
152 break;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100153 default:
154 break;
155 }
156}
157
Vadim Yanitskiy3e66ec52024-05-08 00:17:43 +0200158static void lu_delay_timer_cb(void *data)
159{
160 struct msc_a *msc_a = (struct msc_a *)data;
161 msc_a_put(msc_a, MSC_A_USE_LOCATION_UPDATING);
162}
163
164static void msc_a_put_lu_deferred(struct msc_a *msc_a)
165{
166 unsigned long Tval;
167
168 /* The idea behind timer X36 is to allow re-using the radio channel that was used for
169 * Location Updating to deliver MT SMS over GSUP. This is achieved by delaying
170 * release of a BSSAP/RANAP connection and thus delaying the release of the radio
171 * channel. The delay can be configured separately for GERAN and UTRAN. */
172 switch (msc_a->c.ran->type) {
173 case OSMO_RAT_GERAN_A:
174 Tval = osmo_tdef_get(msc_tdefs_geran, -36, OSMO_TDEF_MS, 0);
175 break;
176 case OSMO_RAT_UTRAN_IU:
177 Tval = osmo_tdef_get(msc_tdefs_utran, -36, OSMO_TDEF_MS, 0);
178 break;
179 default:
180 Tval = 0;
181 break;
182 }
183
184 if (Tval == 0) {
185 /* no delay, put LU token immediately */
186 msc_a_put(msc_a, MSC_A_USE_LOCATION_UPDATING);
187 return;
188 }
189
190 LOG_MSC_A(msc_a, LOGL_INFO, "Keeping LU token for +%lu ms\n", Tval);
191 osmo_timer_schedule(&msc_a->lu_delay_timer,
192 Tval / 1000, /* seconds */
193 Tval % 1000 * 1000); /* microseconds */
194}
195
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100196static void evaluate_acceptance_outcome(struct osmo_fsm_inst *fi, bool conn_accepted)
197{
198 struct msc_a *msc_a = fi->priv;
199 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
200
201 update_counters(fi, conn_accepted);
202
Neels Hofmeyr83cf10c2020-06-24 14:23:26 +0200203 if (conn_accepted) {
204 /* Record the Cell ID seen in Complete Layer 3 Information in the VLR, so that it also shows in vty
205 * 'show' output. */
206 vsub->cgi = msc_a->via_cell;
207 }
208
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100209 /* Trigger transactions that we paged for */
210 if (msc_a->complete_layer3_type == COMPLETE_LAYER3_PAGING_RESP) {
211 if (conn_accepted)
212 paging_response(msc_a);
213 else
214 paging_expired(vsub);
215 }
216
217 if (conn_accepted)
218 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_ATTACHED, msc_a_vsub(msc_a));
219
220 if (msc_a->complete_layer3_type == COMPLETE_LAYER3_LU)
Vadim Yanitskiy3e66ec52024-05-08 00:17:43 +0200221 msc_a_put_lu_deferred(msc_a);
Neels Hofmeyrae98b972021-07-27 03:46:49 +0200222
Neels Hofmeyrc19f8fe2024-04-03 20:50:36 +0200223 if (conn_accepted && msc_a->complete_layer3_type == COMPLETE_LAYER3_CM_RE_ESTABLISH_REQ) {
Neels Hofmeyrae98b972021-07-27 03:46:49 +0200224 /* Trigger new Assignment to recommence the voice call. A little dance here because normally we verify
225 * that no CC trans is already active. */
226 struct gsm_trans *cc_trans = msc_a->cc.active_trans;
227 msc_a->cc.active_trans = NULL;
228 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, cc_trans);
229 msc_a_try_call_assignment(cc_trans);
230 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100231}
232
233bool msc_a_is_accepted(const struct msc_a *msc_a)
234{
235 if (!msc_a || !msc_a->c.fi)
236 return false;
237 return msc_a->c.fi->state == MSC_A_ST_AUTHENTICATED
238 || msc_a->c.fi->state == MSC_A_ST_COMMUNICATING;
239}
240
241bool msc_a_in_release(struct msc_a *msc_a)
242{
243 if (!msc_a)
244 return true;
245 if (msc_a->c.fi->state == MSC_A_ST_RELEASING)
246 return true;
247 if (msc_a->c.fi->state == MSC_A_ST_RELEASED)
248 return true;
249 return false;
250}
251
252static int msc_a_ran_dec(struct msc_a *msc_a, const struct an_apdu *an_apdu, enum msc_role from_role)
253{
254 int rc;
255 struct msc_a_ran_dec_data d = {
256 .from_role = from_role,
257 .an_apdu = an_apdu,
258 };
259 msc_a_get(msc_a, __func__);
260 rc = msc_role_ran_decode(msc_a->c.fi, an_apdu, msc_a_ran_decode_cb, &d);
261 msc_a_put(msc_a, __func__);
262 return rc;
263};
264
265static void msc_a_fsm_validate_l3(struct osmo_fsm_inst *fi, uint32_t event, void *data)
266{
267 struct msc_a *msc_a = fi->priv;
268 const struct an_apdu *an_apdu;
269
270 switch (event) {
271 case MSC_A_EV_FROM_I_COMPLETE_LAYER_3:
272 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
273 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
274 an_apdu = data;
275 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_I);
276 return;
277
278 case MSC_A_EV_COMPLETE_LAYER_3_OK:
279 msc_a_state_chg(msc_a, MSC_A_ST_AUTH_CIPH);
280 return;
281
282 case MSC_A_EV_MO_CLOSE:
283 case MSC_A_EV_CN_CLOSE:
284 evaluate_acceptance_outcome(fi, false);
285 /* fall through */
286 case MSC_A_EV_UNUSED:
287 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
288 return;
289
290 default:
291 OSMO_ASSERT(false);
292 }
293}
294
295/* Figure out whether to first send a Classmark Request to the MS to figure out algorithm support. */
296static bool msc_a_need_classmark_for_ciphering(struct msc_a *msc_a)
297{
298 struct gsm_network *net = msc_a_net(msc_a);
299 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
300 int i = 0;
301 bool request_classmark = false;
302
303 /* Only on GERAN-A do we ever need Classmark Information for Ciphering. */
304 if (msc_a->c.ran->type != OSMO_RAT_GERAN_A)
305 return false;
306
307 for (i = 0; i < 8; i++) {
308 int supported;
309
310 /* A5/n permitted by osmo-msc.cfg? */
311 if (!(net->a5_encryption_mask & (1 << i)))
312 continue;
313
314 /* A5/n supported by MS? */
315 supported = osmo_gsm48_classmark_supports_a5(&vsub->classmark, i);
316 if (supported < 0) {
317 LOG_MSC_A(msc_a, LOGL_DEBUG, "For A5/%d, we still need Classmark %d\n", i, -supported);
318 request_classmark = true;
319 }
320 }
321
322 return request_classmark;
323}
324
325static int msc_a_ran_enc_ciphering(struct msc_a *msc_a, bool umts_aka, bool retrieve_imeisv);
326
327/* VLR callback for ops.set_ciph_mode() */
328int msc_a_vlr_set_cipher_mode(void *_msc_a, bool umts_aka, bool retrieve_imeisv)
329{
330 struct msc_a *msc_a = _msc_a;
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700331 struct vlr_subscr *vsub;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100332
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700333 if (!msc_a) {
334 LOGP(DMSC, LOGL_ERROR, "Insufficient info to start ciphering: "
335 "MSC-A role is NULL?!?\n");
336 return -EINVAL;
337 }
338
339 vsub = msc_a_vsub(msc_a);
340 if (!vsub || !vsub->last_tuple) {
341 LOG_MSC_A(msc_a, LOGL_ERROR, "Insufficient info to start ciphering: "
342 "vlr_subscr is NULL?!?\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100343 return -EINVAL;
344 }
345
346 if (msc_a_need_classmark_for_ciphering(msc_a)) {
347 int rc;
348 struct ran_msg msg = {
349 .msg_type = RAN_MSG_CLASSMARK_REQUEST,
350 };
351 rc = msc_a_ran_down(msc_a, MSC_ROLE_I, &msg);
352 if (rc) {
353 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot send Classmark Request\n");
354 return -EIO;
355 }
356
357 msc_a->state_before_classmark_update = msc_a->c.fi->state;
358 msc_a->action_on_classmark_update = (struct msc_a_action_on_classmark_update){
359 .type = MSC_A_CLASSMARK_UPDATE_THEN_CIPHERING,
360 .ciphering = {
361 .umts_aka = umts_aka,
362 .retrieve_imeisv = retrieve_imeisv,
363 },
364 };
365 msc_a_state_chg(msc_a, MSC_A_ST_WAIT_CLASSMARK_UPDATE);
366 return 0;
367 }
368
369 return msc_a_ran_enc_ciphering(msc_a, umts_aka, retrieve_imeisv);
370}
371
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +0200372static uint8_t filter_a5(uint8_t a5_mask, bool umts_aka)
373{
374 /* With GSM AKA: allow A5/0, 1, 3 = 0b00001011 = 0xb.
375 * UMTS aka: allow A5/0, 1, 3, 4 = 0b00011011 = 0x1b.
376 */
377 return a5_mask & (umts_aka ? 0x1b : 0x0b);
378}
379
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100380static int msc_a_ran_enc_ciphering(struct msc_a *msc_a, bool umts_aka, bool retrieve_imeisv)
381{
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700382 struct gsm_network *net;
383 struct vlr_subscr *vsub;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100384 struct ran_msg msg;
385
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700386 if (!msc_a) {
387 LOGP(DMSC, LOGL_ERROR, "Insufficient info to start ciphering: "
388 "MSC-A role is NULL?!?\n");
389 return -EINVAL;
390 }
391
392 net = msc_a_net(msc_a);
393 vsub = msc_a_vsub(msc_a);
394
395 if (!net || !vsub || !vsub->last_tuple) {
396 LOG_MSC_A(msc_a, LOGL_ERROR, "Insufficient info to start ciphering: "
397 "gsm_network and/or vlr_subscr is NULL?!?\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100398 return -EINVAL;
399 }
400
401 msg = (struct ran_msg){
402 .msg_type = RAN_MSG_CIPHER_MODE_COMMAND,
403 .cipher_mode_command = {
404 .vec = vsub->last_tuple ? &vsub->last_tuple->vec : NULL,
405 .classmark = &vsub->classmark,
406 .geran = {
407 .umts_aka = umts_aka,
408 .retrieve_imeisv = retrieve_imeisv,
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +0200409 .a5_encryption_mask = filter_a5(net->a5_encryption_mask, umts_aka),
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100410
411 /* for ran_a.c to store the GERAN key that is actually used */
412 .chosen_key = &msc_a->geran_encr,
413 },
Harald Welte274b70f2021-02-06 16:47:39 +0100414 .utran = {
Harald Welte505a94a2021-02-06 17:12:20 +0100415 .uea_encryption_mask = net->uea_encryption_mask,
416 },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100417 },
418 };
419
420 if (msc_a_ran_down(msc_a, MSC_ROLE_I, &msg)) {
421 LOG_MSC_A(msc_a, LOGL_ERROR, "Sending Cipher Mode Command failed\n");
422 /* Returning error to the VLR ops.set_ciph_mode() will cancel the attach. Other callers need to take
423 * care of the return value. */
424 return -EINVAL;
425 }
426
427 if (msc_a->geran_encr.key_len)
Neels Hofmeyr73d093a2021-06-23 23:54:43 +0200428 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN encoding chose ciphering: A5/%d kc %s kc128 %s\n",
429 msc_a->geran_encr.alg_id - 1,
430 osmo_hexdump_nospc_c(OTC_SELECT, msc_a->geran_encr.key, msc_a->geran_encr.key_len),
431 msc_a->geran_encr.kc128_present ?
432 osmo_hexdump_nospc_c(OTC_SELECT, msc_a->geran_encr.kc128, sizeof(msc_a->geran_encr.kc128))
433 : "-");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100434 return 0;
435}
436
437static void msc_a_fsm_auth_ciph(struct osmo_fsm_inst *fi, uint32_t event, void *data)
438{
439 struct msc_a *msc_a = fi->priv;
440
441 /* If accepted, transition the state, all other cases mean failure. */
442 switch (event) {
443 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
444 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
445 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
446 return;
447
448 case MSC_A_EV_AUTHENTICATED:
449 msc_a_state_chg(msc_a, MSC_A_ST_AUTHENTICATED);
450 return;
451
452 case MSC_A_EV_UNUSED:
453 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
454 return;
455
456 case MSC_A_EV_MO_CLOSE:
457 case MSC_A_EV_CN_CLOSE:
458 evaluate_acceptance_outcome(fi, false);
459 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
460 return;
461
462
463 default:
464 OSMO_ASSERT(false);
465 }
466}
467
468static void msc_a_fsm_wait_classmark_update(struct osmo_fsm_inst *fi, uint32_t event, void *data)
469{
470 struct msc_a *msc_a = fi->priv;
471
472 switch (event) {
473 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
474 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
475 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
476 return;
477
478 case MSC_A_EV_CLASSMARK_UPDATE:
479 switch (msc_a->action_on_classmark_update.type) {
480 case MSC_A_CLASSMARK_UPDATE_THEN_CIPHERING:
481 msc_a_state_chg(msc_a, MSC_A_ST_AUTH_CIPH);
482 if (msc_a_ran_enc_ciphering(msc_a,
483 msc_a->action_on_classmark_update.ciphering.umts_aka,
484 msc_a->action_on_classmark_update.ciphering.retrieve_imeisv)) {
485 LOG_MSC_A(msc_a, LOGL_ERROR,
486 "After Classmark Update, still failed to send Cipher Mode Command\n");
487 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
488 }
489 return;
490
491 default:
492 LOG_MSC_A(msc_a, LOGL_ERROR, "Internal error: After Classmark Update, don't know what to do\n");
493 msc_a_state_chg(msc_a, msc_a->state_before_classmark_update);
494 return;
495 }
496
497 case MSC_A_EV_UNUSED:
498 /* Seems something detached / aborted in the middle of auth+ciph. */
499 evaluate_acceptance_outcome(fi, false);
500 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
501 return;
502
503 case MSC_A_EV_MO_CLOSE:
504 case MSC_A_EV_CN_CLOSE:
505 evaluate_acceptance_outcome(fi, false);
506 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
507 return;
508
509 default:
510 OSMO_ASSERT(false);
511 }
512}
513
514static bool msc_a_fsm_has_active_transactions(struct osmo_fsm_inst *fi)
515{
516 struct msc_a *msc_a = fi->priv;
517 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
518 struct gsm_trans *trans;
519
520 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_SILENT_CALL)) {
521 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: silent call still active\n", __func__);
522 return true;
523 }
524
525 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC)) {
526 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO CC request after a CM Service Request\n",
527 __func__);
528 return true;
529 }
Andreas Eversberg456c6f72023-04-23 11:54:16 +0200530 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_GCC)) {
531 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO GCC request after a CM Service Request\n",
532 __func__);
533 return true;
534 }
535 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_BCC)) {
536 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO BCC request after a CM Service Request\n",
537 __func__);
538 return true;
539 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100540 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_SMS)) {
541 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO SMS after a CM Service Request\n",
542 __func__);
543 return true;
544 }
545 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_SS)) {
546 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO SS after a CM Service Request\n",
547 __func__);
548 return true;
549 }
550
551 if (vsub && !llist_empty(&vsub->cs.requests)) {
552 struct paging_request *pr;
553 llist_for_each_entry(pr, &vsub->cs.requests, entry) {
554 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still active: %s\n", __func__, pr->label);
555 }
556 return true;
557 }
558
559 if ((trans = trans_has_conn(msc_a))) {
560 LOG_MSC_A(msc_a, LOGL_DEBUG, "connection still has active transaction: %s\n",
561 trans_type_name(trans->type));
562 return true;
563 }
564
565 return false;
566}
567
568static void msc_a_fsm_authenticated_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
569{
570 struct msc_a *msc_a = fi->priv;
571 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
572
573 /* Stop Location Update expiry for this subscriber. While the subscriber
574 * has an open connection the LU expiry timer must remain disabled.
575 * Otherwise we would kick the subscriber off the network when the timer
576 * expires e.g. during a long phone call.
577 * The LU expiry timer will restart once the connection is closed. */
578 if (vsub)
579 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
580
581 evaluate_acceptance_outcome(fi, true);
582}
583
584static void msc_a_fsm_authenticated(struct osmo_fsm_inst *fi, uint32_t event, void *data)
585{
586 struct msc_a *msc_a = fi->priv;
587
588 switch (event) {
589 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
590 case MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST:
591 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
592 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
593 return;
594
595 case MSC_A_EV_COMPLETE_LAYER_3_OK:
596 /* When Authentication is off, we may already be in the Accepted state when the code
597 * evaluates the Compl L3. Simply ignore. This just cosmetically mutes the error log
598 * about the useless event. */
599 return;
600
601 case MSC_A_EV_TRANSACTION_ACCEPTED:
602 msc_a_state_chg(msc_a, MSC_A_ST_COMMUNICATING);
603 return;
604
605 case MSC_A_EV_MO_CLOSE:
606 case MSC_A_EV_CN_CLOSE:
607 case MSC_A_EV_UNUSED:
608 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
609 return;
610
611 default:
612 OSMO_ASSERT(false);
613 }
614}
615
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100616static struct call_leg *msc_a_ensure_call_leg(struct msc_a *msc_a, struct gsm_trans *for_cc_trans)
617{
618 struct call_leg *cl = msc_a->cc.call_leg;
619 struct gsm_network *net = msc_a_net(msc_a);
620
621 /* Ensure that events about RTP endpoints coming from the msc_a->cc.call_leg know which gsm_trans to abort on
622 * error */
623 if (!msc_a->cc.active_trans)
624 msc_a->cc.active_trans = for_cc_trans;
625 if (msc_a->cc.active_trans != for_cc_trans) {
626 LOG_TRANS(for_cc_trans, LOGL_ERROR,
627 "Cannot create call leg, another trans is already active for this conn\n");
628 return NULL;
629 }
630
631 if (!cl) {
632 cl = msc_a->cc.call_leg = call_leg_alloc(msc_a->c.fi,
633 MSC_EV_CALL_LEG_TERM,
634 MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE,
635 MSC_EV_CALL_LEG_RTP_COMPLETE);
636 OSMO_ASSERT(cl);
637
638 if (net->use_osmux != OSMUX_USAGE_OFF) {
639 struct msc_i *msc_i = msc_a_msc_i(msc_a);
640 if (msc_i->c.remote_to) {
641 /* TODO: investigate what to do in this case */
642 LOG_MSC_A(msc_a, LOGL_ERROR, "Osmux not yet supported for inter-MSC");
643 } else {
644 cl->ran_peer_supports_osmux = msc_i->ran_conn->ran_peer->remote_supports_osmux;
645 }
646 }
647
648 }
649 return cl;
650}
651
652int msc_a_ensure_cn_local_rtp(struct msc_a *msc_a, struct gsm_trans *cc_trans)
653{
654 struct call_leg *cl;
655 struct rtp_stream *rtp_to_ran;
656
657 cl = msc_a_ensure_call_leg(msc_a, cc_trans);
658 if (!cl)
659 return -EINVAL;
660 rtp_to_ran = cl->rtp[RTP_TO_RAN];
661
662 if (call_leg_local_ip(cl, RTP_TO_CN)) {
663 /* Already has an RTP address and port towards the CN, continue right away. */
664 return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_CN]);
665 }
666
667 /* No CN RTP address available yet, ask the MGW to create one.
668 * Set a codec to be used: if Assignment on the RAN side is already done, take the same codec as the RTP_TO_RAN.
669 * If no RAN side RTP is established, try to guess a preliminary codec from SDP -- before Assignment, picking a
670 * codec from the SDP is more politeness/avoiding confusion than necessity. The actual codec to be used would be
671 * determined later. If no codec could be determined, pass none for the time being. */
Andreas Eversberg712b28e2023-06-21 11:17:26 +0200672 return call_leg_ensure_ci(cl, RTP_TO_CN, cc_trans->call_id, cc_trans,
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100673 rtp_to_ran->codecs_known ? &rtp_to_ran->codecs : NULL, NULL);
674}
675
Neels Hofmeyr22d4f352024-06-21 19:08:34 +0200676static void assignment_request_timeout_cb(void *data);
677
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100678/* The MGW has given us a local IP address for the RAN side. Ready to start the Assignment of a voice channel. */
Neels Hofmeyrd767c732023-11-17 04:12:29 +0100679void msc_a_tx_assignment_cmd(struct msc_a *msc_a)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100680{
681 struct ran_msg msg;
682 struct gsm_trans *cc_trans = msc_a->cc.active_trans;
683 struct gsm0808_channel_type channel_type;
684
Neels Hofmeyr22d4f352024-06-21 19:08:34 +0200685 /* Do not dispatch another Assignment Command before an earlier assignment is completed. This is a sanity
686 * safeguard, ideally callers should not even invoke this function when an Assignment is already ongoing.
687 * (There is no osmo_fsm for Assignment / the CC trans code; when we refactor that one day, this timer should be
688 * an FSM state.) */
689 if (osmo_timer_pending(&msc_a->cc.assignment_request_pending)) {
690 LOG_MSC_A(msc_a, LOGL_ERROR,
691 "Not transmitting Assignment, still waiting for the response to an earlier Assignment\n");
692 return;
693 }
694 osmo_timer_setup(&msc_a->cc.assignment_request_pending, assignment_request_timeout_cb, msc_a);
695 osmo_timer_schedule(&msc_a->cc.assignment_request_pending,
696 osmo_tdef_get(msc_a->c.ran->tdefs, -37, OSMO_TDEF_S, 10), 0);
697
Neels Hofmeyr00a476b2019-11-28 02:46:05 +0100698 if (!cc_trans) {
699 LOG_MSC_A(msc_a, LOGL_ERROR, "No CC transaction active\n");
700 call_leg_release(msc_a->cc.call_leg);
701 return;
702 }
703
Oliver Smithceca8e62023-05-24 11:15:52 +0200704 trans_cc_filter_run(cc_trans);
705 LOG_TRANS(cc_trans, LOGL_DEBUG, "Sending Assignment Command\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100706
Oliver Smith10632132023-05-12 12:14:22 +0200707 switch (cc_trans->bearer_cap.transfer) {
708 case GSM48_BCAP_ITCAP_SPEECH:
709 if (!cc_trans->cc.local.audio_codecs.count) {
710 LOG_TRANS(cc_trans, LOGL_ERROR, "Assignment not possible, no matching codec: %s\n",
711 codec_filter_to_str(&cc_trans->cc.codecs, &cc_trans->cc.local, &cc_trans->cc.remote));
712 call_leg_release(msc_a->cc.call_leg);
713 return;
714 }
715
716 /* Compose 48.008 Channel Type from the current set of codecs
717 * determined from both local and remote codec capabilities. */
718 if (sdp_audio_codecs_to_gsm0808_channel_type(&channel_type, &cc_trans->cc.local.audio_codecs)) {
719 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot compose Channel Type (Permitted Speech) from codecs: %s\n",
720 codec_filter_to_str(&cc_trans->cc.codecs, &cc_trans->cc.local, &cc_trans->cc.remote));
721 trans_free(cc_trans);
722 return;
723 }
724 break;
Manawyrm1ed12ea2023-10-14 17:23:04 +0200725 case GSM48_BCAP_ITCAP_3k1_AUDIO:
726 case GSM48_BCAP_ITCAP_FAX_G3:
Oliver Smith10632132023-05-12 12:14:22 +0200727 case GSM48_BCAP_ITCAP_UNR_DIG_INF:
728 if (!cc_trans->cc.local.bearer_services.count) {
729 LOG_TRANS(cc_trans, LOGL_ERROR, "Assignment not possible, no matching bearer service: %s\n",
730 csd_filter_to_str(&cc_trans->cc.csd, &cc_trans->cc.local, &cc_trans->cc.remote));
731 call_leg_release(msc_a->cc.call_leg);
732 return;
733 }
734
735 /* Compose 48.008 Channel Type from the current set of bearer
736 * services determined from local and remote capabilities. */
737 if (csd_bs_list_to_gsm0808_channel_type(&channel_type, &cc_trans->cc.local.bearer_services)) {
738 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot compose channel type from: %s\n",
739 csd_filter_to_str(&cc_trans->cc.csd, &cc_trans->cc.local, &cc_trans->cc.remote));
740 return;
741 }
742 break;
743 default:
744 LOG_TRANS(cc_trans, LOGL_ERROR, "Assignment not possible for information transfer capability %d\n",
745 cc_trans->bearer_cap.transfer);
Neels Hofmeyr11a746a2023-01-26 15:00:06 +0100746 call_leg_release(msc_a->cc.call_leg);
747 return;
748 }
749
Oliver Smith10632132023-05-12 12:14:22 +0200750 /* The RAN side RTP address is known, so the voice/CSD Assignment can commence. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100751 msg = (struct ran_msg){
752 .msg_type = RAN_MSG_ASSIGNMENT_COMMAND,
753 .assignment_command = {
754 .cn_rtp = &msc_a->cc.call_leg->rtp[RTP_TO_RAN]->local,
755 .channel_type = &channel_type,
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200756 .osmux_present = msc_a->cc.call_leg->rtp[RTP_TO_RAN]->use_osmux,
757 .osmux_cid = msc_a->cc.call_leg->rtp[RTP_TO_RAN]->local_osmux_cid,
Philipp Maierf34d9452020-06-05 15:49:35 +0200758 .call_id_present = true,
Andreas Eversberg712b28e2023-06-21 11:17:26 +0200759 .call_id = cc_trans->call_id,
Keith Whytea1a70be2021-05-16 02:59:52 +0200760 .lcls = cc_trans->cc.lcls,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100761 },
762 };
763 if (msc_a_ran_down(msc_a, MSC_ROLE_I, &msg)) {
764 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot send Assignment\n");
Neels Hofmeyrf439ff12019-10-05 04:19:36 +0200765 trans_free(cc_trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100766 return;
767 }
768}
769
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100770static struct gsm_trans *find_waiting_call(struct msc_a *msc_a)
771{
772 struct gsm_trans *trans;
773 struct gsm_network *net = msc_a_net(msc_a);
774
775 llist_for_each_entry(trans, &net->trans_list, entry) {
776 if (trans->msc_a != msc_a)
777 continue;
778 if (trans->type != TRANS_CC)
779 continue;
780 if (trans->msc_a->cc.active_trans == trans)
781 continue;
782 return trans;
783 }
784 return NULL;
785}
786
787static void msc_a_cleanup_rtp_streams(struct msc_a *msc_a, uint32_t event, void *data)
788{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100789 switch (event) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100790
791 case MSC_EV_CALL_LEG_TERM:
792 msc_a->cc.call_leg = NULL;
793 if (msc_a->cc.mncc_forwarding_to_remote_ran)
794 msc_a->cc.mncc_forwarding_to_remote_ran->rtps = NULL;
795
Neels Hofmeyr265a4c72019-05-09 16:20:51 +0200796 if (msc_a->ho.new_cell.mncc_forwarding_to_remote_ran)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100797 msc_a->ho.new_cell.mncc_forwarding_to_remote_ran->rtps = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100798 return;
799
800 case MSC_MNCC_EV_CALL_ENDED:
801 msc_a->cc.mncc_forwarding_to_remote_ran = NULL;
802 return;
803
804 default:
805 return;
806 }
807}
808
809static void msc_a_fsm_communicating(struct osmo_fsm_inst *fi, uint32_t event, void *data)
810{
811 struct msc_a *msc_a = fi->priv;
812 struct rtp_stream *rtps;
813 struct gsm_trans *waiting_trans;
814 struct an_apdu *an_apdu;
815
816 msc_a_cleanup_rtp_streams(msc_a, event, data);
817
818 switch (event) {
819 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
820 case MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST:
821 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
822 an_apdu = data;
823 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_I);
824 return;
825
826 case MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE:
827 case MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE:
828 case MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST:
829 case MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST:
830 an_apdu = data;
831 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_T);
832 return;
833
834 case MSC_A_EV_TRANSACTION_ACCEPTED:
835 /* no-op */
836 return;
837
838 case MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE:
839 rtps = data;
840 if (!rtps) {
841 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid data for %s\n", osmo_fsm_event_name(fi->fsm, event));
842 return;
843 }
Neels Hofmeyr00a476b2019-11-28 02:46:05 +0100844 if (!msc_a->cc.call_leg) {
845 LOG_MSC_A(msc_a, LOGL_ERROR, "No call leg active\n");
846 return;
847 }
Neels Hofmeyrcc918cb2019-11-28 02:16:34 +0100848 if (!osmo_sockaddr_str_is_nonzero(&rtps->local)) {
849 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid RTP address received from MGW: " OSMO_SOCKADDR_STR_FMT "\n",
850 OSMO_SOCKADDR_STR_FMT_ARGS(&rtps->local));
851 call_leg_release(msc_a->cc.call_leg);
852 return;
853 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100854 LOG_MSC_A(msc_a, LOGL_DEBUG,
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200855 "MGW endpoint's RTP address available for the CI %s: " OSMO_SOCKADDR_STR_FMT " (osmux=%s:%d)\n",
856 rtp_direction_name(rtps->dir), OSMO_SOCKADDR_STR_FMT_ARGS(&rtps->local),
857 rtps->use_osmux ? "yes" : "no", rtps->local_osmux_cid);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100858 switch (rtps->dir) {
859 case RTP_TO_RAN:
Neels Hofmeyrd767c732023-11-17 04:12:29 +0100860 msc_a_tx_assignment_cmd(msc_a);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100861 return;
862 case RTP_TO_CN:
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100863 cc_on_cn_local_rtp_port_known(rtps->for_trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100864 return;
865 default:
866 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid data for %s\n", osmo_fsm_event_name(fi->fsm, event));
867 return;
868 }
869
870 case MSC_EV_CALL_LEG_RTP_COMPLETE:
871 /* Nothing to do. */
872 return;
873
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100874 case MSC_MNCC_EV_CALL_ENDED:
875 /* Cleaned up above */
876 return;
877
878 case MSC_EV_CALL_LEG_TERM:
879 /* RTP streams cleaned up above */
880
881 msc_a_get(msc_a, __func__);
882 if (msc_a->cc.active_trans)
883 trans_free(msc_a->cc.active_trans);
884
885 /* If there is another call still waiting to be activated, this is the time when the mgcp_ctx is
886 * available again and the other call can start assigning. */
887 waiting_trans = find_waiting_call(msc_a);
888 if (waiting_trans) {
889 LOG_MSC_A(msc_a, LOGL_DEBUG, "(ti %02x) Call waiting: starting Assignment\n",
890 waiting_trans->transaction_id);
891 msc_a_try_call_assignment(waiting_trans);
892 }
893 msc_a_put(msc_a, __func__);
894 return;
895
896 case MSC_A_EV_HANDOVER_REQUIRED:
897 msc_ho_start(msc_a, (struct ran_handover_required*)data);
898 return;
899
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200900 case MSC_A_EV_HANDOVER_END:
901 /* Termination event of the msc_ho_fsm. No action needed, it's all done in the msc_ho_fsm cleanup. This
902 * event only exists because osmo_fsm_inst_alloc_child() requires a parent term event; and maybe
903 * interesting for logging. */
904 return;
905
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100906 case MSC_A_EV_MO_CLOSE:
907 case MSC_A_EV_CN_CLOSE:
908 case MSC_A_EV_UNUSED:
909 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
910 return;
911
912 default:
913 OSMO_ASSERT(false);
914 }
915}
916
917static int msc_a_fsm_timer_cb(struct osmo_fsm_inst *fi)
918{
919 struct msc_a *msc_a = fi->priv;
920 if (msc_a_in_release(msc_a)) {
921 LOG_MSC_A(msc_a, LOGL_ERROR, "Timeout while releasing, discarding right now\n");
922 msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
923 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
924 } else {
925 enum gsm48_reject_value cause = GSM48_REJECT_CONGESTION;
926 osmo_fsm_inst_dispatch(fi, MSC_A_EV_CN_CLOSE, &cause);
927 }
928 return 0;
929}
930
931static void msc_a_fsm_releasing_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
932{
933 struct msc_a *msc_a = fi->priv;
934 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
935 int i;
936 char buf[128];
937 const char * const use_counts_to_cancel[] = {
938 MSC_A_USE_LOCATION_UPDATING,
939 MSC_A_USE_CM_SERVICE_CC,
940 MSC_A_USE_CM_SERVICE_SMS,
941 MSC_A_USE_CM_SERVICE_SS,
Andreas Eversberg456c6f72023-04-23 11:54:16 +0200942 MSC_A_USE_CM_SERVICE_GCC,
943 MSC_A_USE_CM_SERVICE_BCC,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100944 MSC_A_USE_PAGING_RESPONSE,
945 };
946
947 LOG_MSC_A(msc_a, LOGL_DEBUG, "Releasing: msc_a use is %s\n",
948 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
949
950 if (vsub) {
951 vlr_subscr_get(vsub, __func__);
952
953 /* Cancel all VLR FSMs, if any */
954 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
955
956 /* The subscriber has no active connection anymore.
957 * Restart the periodic Location Update expiry timer for this subscriber. */
958 vlr_subscr_enable_expire_lu(vsub);
959 }
960
Neels Hofmeyr22d4f352024-06-21 19:08:34 +0200961 /* We no longer care about assignment responses. */
962 osmo_timer_del(&msc_a->cc.assignment_request_pending);
963
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100964 /* If we're closing in a middle of a trans, we need to clean up */
965 trans_conn_closed(msc_a);
966
967 call_leg_release(msc_a->cc.call_leg);
968
969 /* Cancel use counts for pending CM Service / Paging */
970 for (i = 0; i < ARRAY_SIZE(use_counts_to_cancel); i++) {
971 const char *use = use_counts_to_cancel[i];
972 int32_t count = osmo_use_count_by(&msc_a->use_count, use);
973 if (!count)
974 continue;
975 LOG_MSC_A(msc_a, LOGL_DEBUG, "Releasing: canceling still pending use: %s (%d)\n", use, count);
976 osmo_use_count_get_put(&msc_a->use_count, use, -count);
977 }
978
979 if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) {
980 sgs_iface_tx_release(vsub);
981 /* In SGsAP there is no confirmation of a release. */
982 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
983 } else {
984 struct ran_msg msg = {
985 .msg_type = RAN_MSG_CLEAR_COMMAND,
986 .clear_command = {
Neels Hofmeyrd9fe7112020-07-11 00:20:20 +0200987 /* "Call Control" is the only cause code listed in 3GPP TS 48.008 3.2.1.21 CLEAR COMMAND
988 * that qualifies for a normal release situation. (OS#4664) */
989 .gsm0808_cause = GSM0808_CAUSE_CALL_CONTROL,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100990 .csfb_ind = (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED),
991 },
992 };
993 msc_a_get(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
994 msc_a_ran_down(msc_a, MSC_ROLE_I, &msg);
Philipp Maier47cf84d2019-08-15 14:56:54 +0200995
996 /* The connection is cleared. The MS will now go back to 4G,
997 Switch the RAN type back to SGS. */
998 if (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED)
999 vsub->cs.attached_via_ran = OSMO_RAT_EUTRAN_SGS;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001000 }
1001
1002 if (vsub)
1003 vlr_subscr_put(vsub, __func__);
1004}
1005
1006static void msc_a_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1007{
1008 struct msc_a *msc_a = fi->priv;
1009
1010 msc_a_cleanup_rtp_streams(msc_a, event, data);
1011
1012 switch (event) {
1013 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
1014 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
1015 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
1016 return;
1017
1018 case MSC_A_EV_MO_CLOSE:
1019 case MSC_A_EV_CN_CLOSE:
1020 case MSC_A_EV_UNUSED:
1021 /* Already releasing */
1022 return;
1023
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001024 case MSC_EV_CALL_LEG_TERM:
1025 case MSC_MNCC_EV_CALL_ENDED:
1026 /* RTP streams cleaned up above */
1027 return;
1028
Neels Hofmeyr0a437be2019-05-10 15:55:52 +02001029 case MSC_A_EV_HANDOVER_END:
1030 /* msc_ho_fsm does cleanup. */
1031 return;
1032
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001033 default:
1034 OSMO_ASSERT(false);
1035 }
1036}
1037
1038
1039static void msc_a_fsm_released_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
1040{
1041 struct msc_a *msc_a = msc_a_fi_priv(fi);
1042 char buf[128];
1043 LOG_MSC_A(msc_a, LOGL_DEBUG, "Released: msc_a use is %s\n",
1044 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
1045 if (osmo_use_count_total(&msc_a->use_count) == 0)
1046 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi);
1047}
1048
1049static void msc_a_fsm_released(struct osmo_fsm_inst *fi, uint32_t event, void *data)
1050{
1051 if (event == MSC_A_EV_UNUSED)
1052 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi);
1053}
1054
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001055void msc_a_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
1056{
1057 struct msc_a *msc_a = msc_a_fi_priv(fi);
Neels Hofmeyr72ef7d82024-03-26 01:43:53 +01001058 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001059
1060 trans_conn_closed(msc_a);
1061
1062 if (msc_a_fsm_has_active_transactions(fi))
1063 LOG_MSC_A(msc_a, LOGL_ERROR, "Deallocating active transactions failed\n");
1064
1065 LOG_MSC_A_CAT(msc_a, DREF, LOGL_DEBUG, "max total use count was %d\n", msc_a->max_total_use_count);
Neels Hofmeyr72ef7d82024-03-26 01:43:53 +01001066
1067 /* Invalidate the active conn in VLR subscriber state, if any. */
1068 if (vsub && vsub->msc_conn_ref == msc_a)
1069 vsub->msc_conn_ref = NULL;
Vadim Yanitskiy3e66ec52024-05-08 00:17:43 +02001070
1071 osmo_timer_del(&msc_a->lu_delay_timer);
Neels Hofmeyr22d4f352024-06-21 19:08:34 +02001072 osmo_timer_del(&msc_a->cc.assignment_request_pending);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001073}
1074
1075const struct value_string msc_a_fsm_event_names[] = {
1076 OSMO_VALUE_STRING(MSC_REMOTE_EV_RX_GSUP),
1077 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE),
1078 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_COMPLETE),
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001079 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_TERM),
1080 OSMO_VALUE_STRING(MSC_MNCC_EV_NEED_LOCAL_RTP),
1081 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_PROCEEDING),
1082 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_COMPLETE),
1083 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_ENDED),
1084 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_COMPLETE_LAYER_3),
1085 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST),
1086 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST),
1087 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST),
1088 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST),
1089 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE),
1090 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE),
1091 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST),
1092 OSMO_VALUE_STRING(MSC_A_EV_COMPLETE_LAYER_3_OK),
1093 OSMO_VALUE_STRING(MSC_A_EV_CLASSMARK_UPDATE),
1094 OSMO_VALUE_STRING(MSC_A_EV_AUTHENTICATED),
1095 OSMO_VALUE_STRING(MSC_A_EV_TRANSACTION_ACCEPTED),
1096 OSMO_VALUE_STRING(MSC_A_EV_CN_CLOSE),
1097 OSMO_VALUE_STRING(MSC_A_EV_MO_CLOSE),
1098 OSMO_VALUE_STRING(MSC_A_EV_UNUSED),
1099 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_REQUIRED),
1100 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_END),
1101 {}
1102};
1103
1104#define S(x) (1 << (x))
1105
1106static const struct osmo_fsm_state msc_a_fsm_states[] = {
1107 [MSC_A_ST_VALIDATE_L3] = {
1108 .name = OSMO_STRINGIFY(MSC_A_ST_VALIDATE_L3),
1109 .in_event_mask = 0
1110 | S(MSC_A_EV_FROM_I_COMPLETE_LAYER_3)
1111 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1112 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1113 | S(MSC_A_EV_COMPLETE_LAYER_3_OK)
1114 | S(MSC_A_EV_MO_CLOSE)
1115 | S(MSC_A_EV_CN_CLOSE)
1116 | S(MSC_A_EV_UNUSED)
1117 ,
1118 .out_state_mask = 0
1119 | S(MSC_A_ST_VALIDATE_L3)
1120 | S(MSC_A_ST_AUTH_CIPH)
1121 | S(MSC_A_ST_RELEASING)
1122 ,
1123 .action = msc_a_fsm_validate_l3,
1124 },
1125 [MSC_A_ST_AUTH_CIPH] = {
1126 .name = OSMO_STRINGIFY(MSC_A_ST_AUTH_CIPH),
1127 .in_event_mask = 0
1128 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1129 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1130 | S(MSC_A_EV_AUTHENTICATED)
1131 | S(MSC_A_EV_MO_CLOSE)
1132 | S(MSC_A_EV_CN_CLOSE)
1133 | S(MSC_A_EV_UNUSED)
1134 ,
1135 .out_state_mask = 0
1136 | S(MSC_A_ST_WAIT_CLASSMARK_UPDATE)
1137 | S(MSC_A_ST_AUTHENTICATED)
1138 | S(MSC_A_ST_RELEASING)
1139 ,
1140 .action = msc_a_fsm_auth_ciph,
1141 },
1142 [MSC_A_ST_WAIT_CLASSMARK_UPDATE] = {
1143 .name = OSMO_STRINGIFY(MSC_A_ST_WAIT_CLASSMARK_UPDATE),
1144 .in_event_mask = 0
1145 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1146 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1147 | S(MSC_A_EV_CLASSMARK_UPDATE)
1148 | S(MSC_A_EV_MO_CLOSE)
1149 | S(MSC_A_EV_CN_CLOSE)
1150 ,
1151 .out_state_mask = 0
1152 | S(MSC_A_ST_AUTH_CIPH)
1153 | S(MSC_A_ST_RELEASING)
1154 ,
1155 .action = msc_a_fsm_wait_classmark_update,
1156 },
1157 [MSC_A_ST_AUTHENTICATED] = {
1158 .name = OSMO_STRINGIFY(MSC_A_ST_AUTHENTICATED),
1159 /* allow everything to release for any odd behavior */
1160 .in_event_mask = 0
1161 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1162 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
1163 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1164 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
1165 | S(MSC_A_EV_MO_CLOSE)
1166 | S(MSC_A_EV_CN_CLOSE)
1167 | S(MSC_A_EV_UNUSED)
1168 ,
1169 .out_state_mask = 0
1170 | S(MSC_A_ST_RELEASING)
1171 | S(MSC_A_ST_COMMUNICATING)
1172 ,
1173 .onenter = msc_a_fsm_authenticated_enter,
1174 .action = msc_a_fsm_authenticated,
1175 },
1176 [MSC_A_ST_COMMUNICATING] = {
1177 .name = OSMO_STRINGIFY(MSC_A_ST_COMMUNICATING),
1178 /* allow everything to release for any odd behavior */
1179 .in_event_mask = 0
1180 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1181 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
1182 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1183 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE)
1184 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE)
1185 | S(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST)
1186 | S(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST)
1187 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
1188 | S(MSC_A_EV_MO_CLOSE)
1189 | S(MSC_A_EV_CN_CLOSE)
1190 | S(MSC_A_EV_UNUSED)
1191 | S(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE)
1192 | S(MSC_EV_CALL_LEG_RTP_COMPLETE)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001193 | S(MSC_EV_CALL_LEG_TERM)
1194 | S(MSC_MNCC_EV_CALL_ENDED)
1195 | S(MSC_A_EV_HANDOVER_REQUIRED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +02001196 | S(MSC_A_EV_HANDOVER_END)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001197 ,
1198 .out_state_mask = 0
1199 | S(MSC_A_ST_RELEASING)
1200 ,
1201 .action = msc_a_fsm_communicating,
1202 },
1203 [MSC_A_ST_RELEASING] = {
1204 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASING),
1205 .in_event_mask = 0
1206 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1207 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1208 | S(MSC_A_EV_UNUSED)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001209 | S(MSC_EV_CALL_LEG_TERM)
1210 | S(MSC_MNCC_EV_CALL_ENDED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +02001211 | S(MSC_A_EV_HANDOVER_END)
Pau Espin Pedrole53ecde2021-07-12 13:37:24 +02001212 | S(MSC_A_EV_CN_CLOSE)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001213 ,
1214 .out_state_mask = 0
1215 | S(MSC_A_ST_RELEASED)
1216 ,
1217 .onenter = msc_a_fsm_releasing_onenter,
1218 .action = msc_a_fsm_releasing,
1219 },
1220 [MSC_A_ST_RELEASED] = {
1221 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASED),
1222 .in_event_mask = 0
1223 | S(MSC_A_EV_UNUSED)
1224 ,
1225 .onenter = msc_a_fsm_released_onenter,
1226 .action = msc_a_fsm_released,
1227 },
1228};
1229
1230static struct osmo_fsm msc_a_fsm = {
1231 .name = "msc_a",
1232 .states = msc_a_fsm_states,
1233 .num_states = ARRAY_SIZE(msc_a_fsm_states),
1234 .log_subsys = DMSC,
1235 .event_names = msc_a_fsm_event_names,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001236 .timer_cb = msc_a_fsm_timer_cb,
1237 .cleanup = msc_a_fsm_cleanup,
1238};
1239
1240static __attribute__((constructor)) void msc_a_fsm_init()
1241{
1242 OSMO_ASSERT(osmo_fsm_register(&msc_a_fsm) == 0);
1243}
1244
1245static int msc_a_use_cb(struct osmo_use_count_entry *e, int32_t old_use_count, const char *file, int line)
1246{
1247 struct msc_a *msc_a = e->use_count->talloc_object;
1248 char buf[128];
1249 int32_t total;
1250 int level;
1251
1252 if (!e->use)
1253 return -EINVAL;
1254
1255 total = osmo_use_count_total(&msc_a->use_count);
1256
1257 if (total == 0
1258 || (total == 1 && old_use_count == 0 && e->count == 1))
1259 level = LOGL_INFO;
1260 else
1261 level = LOGL_DEBUG;
1262
1263 LOG_MSC_A_CAT_SRC(msc_a, DREF, level, file, line, "%s %s: now used by %s\n",
1264 (e->count - old_use_count) > 0? "+" : "-", e->use,
1265 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
1266
1267 if (e->count < 0)
1268 return -ERANGE;
1269
1270 msc_a->max_total_use_count = OSMO_MAX(msc_a->max_total_use_count, total);
1271
1272 if (total == 0)
1273 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_UNUSED, NULL);
1274 return 0;
1275}
1276
1277struct msc_a *msc_a_alloc(struct msub *msub, struct ran_infra *ran)
1278{
1279 struct msc_a *msc_a = msub_role_alloc(msub, MSC_ROLE_A, &msc_a_fsm, struct msc_a, ran);
1280 msc_a->use_count = (struct osmo_use_count){
1281 .talloc_object = msc_a,
1282 .use_cb = msc_a_use_cb,
1283 };
1284 osmo_use_count_make_static_entries(&msc_a->use_count, msc_a->use_count_buf, ARRAY_SIZE(msc_a->use_count_buf));
1285 /* Start timeout for first state */
Neels Hofmeyr01653252019-09-03 02:06:22 +02001286 msc_a_state_chg_always(msc_a, MSC_A_ST_VALIDATE_L3);
Vadim Yanitskiy3e66ec52024-05-08 00:17:43 +02001287 osmo_timer_setup(&msc_a->lu_delay_timer, &lu_delay_timer_cb, msc_a);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001288 return msc_a;
1289}
1290
1291bool msc_a_is_establishing_auth_ciph(const struct msc_a *msc_a)
1292{
1293 if (!msc_a || !msc_a->c.fi)
1294 return false;
1295 return msc_a->c.fi->state == MSC_A_ST_AUTH_CIPH;
1296}
1297
1298const struct value_string complete_layer3_type_names[] = {
1299 { COMPLETE_LAYER3_NONE, "NONE" },
1300 { COMPLETE_LAYER3_LU, "LU" },
1301 { COMPLETE_LAYER3_CM_SERVICE_REQ, "CM_SERVICE_REQ" },
1302 { COMPLETE_LAYER3_PAGING_RESP, "PAGING_RESP" },
Neels Hofmeyrae98b972021-07-27 03:46:49 +02001303 { COMPLETE_LAYER3_CM_RE_ESTABLISH_REQ, "CM_RE_ESTABLISH_REQ" },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001304 { 0, NULL }
1305};
1306
1307#define _msc_a_update_id(MSC_A, FMT, ARGS ...) \
1308 do { \
1309 if (osmo_fsm_inst_update_id_f(msc_a->c.fi, FMT ":%s:%s", \
1310 ## ARGS, \
1311 msub_ran_conn_name(msc_a->c.msub), \
1312 complete_layer3_type_name(msc_a->complete_layer3_type)) \
1313 == 0) { \
1314 struct vlr_subscr *_vsub = msc_a_vsub(MSC_A); \
1315 if (_vsub) { \
1316 if (_vsub->lu_fsm) \
1317 osmo_fsm_inst_update_id(_vsub->lu_fsm, (MSC_A)->c.fi->id); \
1318 if (_vsub->auth_fsm) \
1319 osmo_fsm_inst_update_id(_vsub->auth_fsm, (MSC_A)->c.fi->id); \
1320 if (_vsub->proc_arq_fsm) \
1321 osmo_fsm_inst_update_id(_vsub->proc_arq_fsm, (MSC_A)->c.fi->id); \
1322 } \
1323 LOG_MSC_A(MSC_A, LOGL_DEBUG, "Updated ID\n"); \
1324 } \
1325 /* otherwise osmo_fsm_inst_update_id_f() will log an error. */ \
1326 } while (0)
1327
1328
1329/* Compose an ID almost like gsm48_mi_to_string(), but print the MI type along, and print a TMSI as hex. */
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001330void msc_a_update_id_from_mi(struct msc_a *msc_a, const struct osmo_mobile_identity *mi)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001331{
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001332 _msc_a_update_id(msc_a, "%s", osmo_mobile_identity_to_str_c(OTC_SELECT, mi));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001333}
1334
1335/* Update msc_a->fi id string from current msc_a->vsub and msc_a->complete_layer3_type. */
1336void msc_a_update_id(struct msc_a *msc_a)
1337{
1338 _msc_a_update_id(msc_a, "%s", vlr_subscr_name(msc_a_vsub(msc_a)));
1339}
1340
1341/* Iterate all msc_a instances that are relevant for this subscriber, and update FSM ID strings for all of the FSM
1342 * instances. */
1343void msc_a_update_id_for_vsub(struct vlr_subscr *for_vsub)
1344{
1345 struct msub *msub;
1346 llist_for_each_entry(msub, &msub_list, entry) {
1347 struct vlr_subscr *vsub = msub_vsub(msub);
1348 if (vsub != for_vsub)
1349 continue;
1350 msc_a_update_id(msub_msc_a(msub));
1351 }
1352}
1353
1354static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
1355{
1356 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
1357 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
1358
1359 switch (pdisc) {
1360 case GSM48_PDISC_MM:
1361 switch (msg_type) {
1362 case GSM48_MT_MM_LOC_UPD_REQUEST:
1363 case GSM48_MT_MM_CM_SERV_REQ:
1364 case GSM48_MT_MM_CM_REEST_REQ:
1365 case GSM48_MT_MM_AUTH_RESP:
1366 case GSM48_MT_MM_AUTH_FAIL:
1367 case GSM48_MT_MM_ID_RESP:
1368 case GSM48_MT_MM_TMSI_REALL_COMPL:
1369 case GSM48_MT_MM_IMSI_DETACH_IND:
1370 return true;
1371 default:
1372 break;
1373 }
1374 break;
1375 case GSM48_PDISC_RR:
1376 switch (msg_type) {
1377 /* GSM48_MT_RR_CIPH_M_COMPL is actually handled in bssmap_rx_ciph_compl() and gets redirected in the
1378 * BSSAP layer to ran_conn_cipher_mode_compl() (before this here is reached) */
1379 case GSM48_MT_RR_PAG_RESP:
1380 case GSM48_MT_RR_CIPH_M_COMPL:
1381 return true;
1382 default:
1383 break;
1384 }
1385 break;
1386 default:
1387 break;
1388 }
1389
1390 return false;
1391}
1392
1393/* Main entry point for GSM 04.08/44.008 Layer 3 data (e.g. from the BSC). */
1394int msc_a_up_l3(struct msc_a *msc_a, struct msgb *msg)
1395{
1396 struct gsm48_hdr *gh;
1397 uint8_t pdisc;
1398 int rc;
1399 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1400 int is_r99;
1401
1402 OSMO_ASSERT(msg->l3h);
1403 OSMO_ASSERT(msg);
1404
1405 gh = msgb_l3(msg);
1406 pdisc = gsm48_hdr_pdisc(gh);
1407
1408 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_DEBUG, "Dispatching 04.08 message: %s %s\n",
1409 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1410
1411 /* To evaluate the 3GPP TS 24.007 Duplicate Detection, we need Classmark information on whether the MS is R99
1412 * capable. If the subscriber is already actively connected, the Classmark information is stored with the
1413 * vlr_subscr. Otherwise, this *must* be a Complete Layer 3 with Classmark info. */
1414 if (vsub)
1415 is_r99 = osmo_gsm48_classmark_is_r99(&vsub->classmark) ? 1 : 0;
1416 else
1417 is_r99 = compl_l3_msg_is_r99(msg);
1418
1419 if (is_r99 < 0) {
1420 LOG_MSC_A(msc_a, LOGL_ERROR,
1421 "No Classmark Information, dropping non-Complete-Layer3 message: %s\n",
1422 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1423 return -EACCES;
1424 }
1425
1426 if (is_r99 >= 0
1427 && ran_dec_dtap_undup_is_duplicate(msc_a->c.fi, msc_a->n_sd_next, is_r99 ? true : false, msg)) {
1428 LOG_MSC_A(msc_a, LOGL_DEBUG, "Dropping duplicate message"
1429 " (3GPP TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection)\n");
1430 return 0;
1431 }
1432
1433 if (!msc_a_is_accepted(msc_a)
1434 && !msg_is_initially_permitted(gh)) {
1435 LOG_MSC_A(msc_a, LOGL_ERROR,
1436 "Message not permitted for initial conn: %s\n",
1437 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1438 return -EACCES;
1439 }
1440
1441 if (vsub && vsub->cs.attached_via_ran != msc_a->c.ran->type) {
1442 LOG_MSC_A(msc_a, LOGL_ERROR,
1443 "Illegal situation: RAN type mismatch:"
1444 " attached via %s, received message via %s\n",
1445 osmo_rat_type_name(vsub->cs.attached_via_ran),
1446 osmo_rat_type_name(msc_a->c.ran->type));
1447 return -EACCES;
1448 }
1449
1450#if 0
1451 if (silent_call_reroute(conn, msg))
1452 return silent_call_rx(conn, msg);
1453#endif
1454
1455 switch (pdisc) {
Andreas Eversberg23b53e52023-06-21 13:30:58 +02001456 case GSM48_PDISC_GROUP_CC:
1457 case GSM48_PDISC_BCAST_CC:
1458 rc = gsm44068_rcv_bcc_gcc(msc_a, NULL, msg);
1459 break;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001460 case GSM48_PDISC_CC:
1461 rc = gsm0408_rcv_cc(msc_a, msg);
1462 break;
1463 case GSM48_PDISC_MM:
1464 rc = gsm0408_rcv_mm(msc_a, msg);
1465 break;
1466 case GSM48_PDISC_RR:
1467 rc = gsm0408_rcv_rr(msc_a, msg);
1468 break;
1469 case GSM48_PDISC_SMS:
1470 rc = gsm0411_rcv_sms(msc_a, msg);
1471 break;
1472 case GSM48_PDISC_MM_GPRS:
1473 case GSM48_PDISC_SM_GPRS:
1474 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unimplemented "
1475 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1476 rc = -ENOTSUP;
1477 break;
1478 case GSM48_PDISC_NC_SS:
1479 rc = gsm0911_rcv_nc_ss(msc_a, msg);
1480 break;
1481 case GSM48_PDISC_TEST:
1482 rc = gsm0414_rcv_test(msc_a, msg);
1483 break;
1484 default:
1485 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unknown "
1486 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1487 rc = -EINVAL;
1488 break;
1489 }
1490
1491 return rc;
1492}
1493
1494static void msc_a_up_call_assignment_complete(struct msc_a *msc_a, const struct ran_msg *ac)
1495{
Andreas Eversberg23b53e52023-06-21 13:30:58 +02001496 struct gsm_trans *cc_trans = msc_a->cc.active_trans, *gcc_trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001497 struct rtp_stream *rtps_to_ran = msc_a->cc.call_leg ? msc_a->cc.call_leg->rtp[RTP_TO_RAN] : NULL;
Neels Hofmeyrcec51b32023-03-01 03:47:45 +01001498 const struct gsm0808_speech_codec *codec_if_known = ac->assignment_complete.codec_present ?
1499 &ac->assignment_complete.codec : NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001500
Neels Hofmeyr22d4f352024-06-21 19:08:34 +02001501 /* Pending assignment has worked out. We're no longer waiting for a response now. */
1502 osmo_timer_del(&msc_a->cc.assignment_request_pending);
1503
Andreas Eversberg23b53e52023-06-21 13:30:58 +02001504 /* For a voice group call, handling is performed by VGCS FSM */
1505 gcc_trans = trans_find_by_type(msc_a, TRANS_GCC);
1506 if (gcc_trans) {
1507 vgcs_vbs_caller_assign_cpl(gcc_trans);
1508 return;
1509 }
1510 gcc_trans = trans_find_by_type(msc_a, TRANS_BCC);
1511 if (gcc_trans) {
1512 vgcs_vbs_caller_assign_cpl(gcc_trans);
1513 return;
1514 }
1515
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001516 if (!rtps_to_ran) {
1517 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but no RTP stream is set up\n");
1518 return;
1519 }
1520 if (!cc_trans) {
Neels Hofmeyr550506a2022-01-13 23:31:57 +01001521 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but no CC transaction is active\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001522 return;
1523 }
1524
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001525 if (rtps_to_ran->use_osmux != ac->assignment_complete.osmux_present) {
1526 LOG_MSC_A_CAT(msc_a, DCC, LOGL_ERROR, "Osmux usage ass request and complete don't match: %d vs %d\n",
1527 rtps_to_ran->use_osmux, ac->assignment_complete.osmux_present);
1528 call_leg_release(msc_a->cc.call_leg);
1529 return;
1530 }
1531
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001532 if (codec_if_known) {
Neels Hofmeyr1a07bcd2024-01-19 06:37:57 +01001533 const struct codec_mapping *codec_assigned;
1534
Neels Hofmeyrbd8ac512024-01-19 06:38:11 +01001535 /* Check for unexpected codec with CSD */
Vadim Yanitskiyb0300112024-02-06 16:58:28 +07001536 switch (cc_trans->bearer_cap.transfer) {
1537 case GSM48_BCAP_ITCAP_FAX_G3:
1538 case GSM48_BCAP_ITCAP_3k1_AUDIO:
1539 case GSM48_BCAP_ITCAP_UNR_DIG_INF:
1540 if (codec_if_known->type == GSM0808_SCT_CSD)
1541 break; /* we're good */
Neels Hofmeyrbd8ac512024-01-19 06:38:11 +01001542 LOG_TRANS(cc_trans, LOGL_ERROR, "Unexpected codec in Assignment Complete for CSD: %s\n",
1543 gsm0808_speech_codec_type_name(codec_if_known->type));
1544 call_leg_release(msc_a->cc.call_leg);
1545 return;
Vadim Yanitskiyb0300112024-02-06 16:58:28 +07001546 default:
1547 break;
Neels Hofmeyrbd8ac512024-01-19 06:38:11 +01001548 }
1549
Neels Hofmeyr388d7c92023-03-22 19:03:15 +01001550 /* For 2G:
1551 * - The Assignment Complete has returned a specific codec (e.g. FR3 for AMR FR).
1552 * - Set this codec at the MGW endpoint facing the RAN.
1553 * - Also set this codec at the MGW endpoint facing the CN -- we require an exact match on both call
1554 * legs.
1555 * - TODO: be aware of transcoding that the MGW is capable of, e.g. AMR octet-aligned to AMR
1556 * bandwidth-efficient...
1557 *
1558 * For 3G:
1559 * - ran_infra->force_mgw_codecs_to_ran sets VND.3GPP.IUFP as single codec at the MGW towards RAN.
1560 * - ran_msg_iu.c always returns FR3 (AMR FR) for the assigned codec. Set that at the MGW towards CN.
1561 * - So the MGW decapsulates IuUP <-> AMR
1562 */
Neels Hofmeyr1a07bcd2024-01-19 06:37:57 +01001563 codec_assigned = codec_mapping_by_gsm0808_speech_codec_type(codec_if_known->type);
Neels Hofmeyr388d7c92023-03-22 19:03:15 +01001564 /* TODO: use codec_mapping_by_gsm0808_speech_codec() to also match on codec_if_known->cfg */
Neels Hofmeyr1a07bcd2024-01-19 06:37:57 +01001565 if (!codec_assigned) {
Neels Hofmeyr388d7c92023-03-22 19:03:15 +01001566 LOG_TRANS(cc_trans, LOGL_ERROR, "Unknown codec in Assignment Complete: %s\n",
1567 gsm0808_speech_codec_type_name(codec_if_known->type));
1568 call_leg_release(msc_a->cc.call_leg);
1569 return;
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001570 }
1571
Neels Hofmeyrcec51b32023-03-01 03:47:45 +01001572 /* Update RAN-side endpoint CI from Assignment result -- unless it is forced by the ran_infra, in which
1573 * case it remains unchanged as passed to the earlier call of call_leg_ensure_ci(). */
1574 if (msc_a->c.ran->force_mgw_codecs_to_ran.count == 0)
Neels Hofmeyr1a07bcd2024-01-19 06:37:57 +01001575 rtp_stream_set_one_codec(rtps_to_ran, &codec_assigned->sdp);
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001576
1577 /* Update codec filter with Assignment result, for the CN side */
Neels Hofmeyr1a07bcd2024-01-19 06:37:57 +01001578 cc_trans->cc.codecs.assignment = codec_assigned->sdp;
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001579 } else {
1580 /* No codec passed in Assignment Complete, set 'codecs.assignment' to none. */
1581 cc_trans->cc.codecs.assignment = (struct sdp_audio_codec){};
1582 LOG_TRANS(cc_trans, LOGL_INFO, "Assignment Complete without voice codec\n");
1583 }
1584
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001585 rtp_stream_set_remote_addr(rtps_to_ran, &ac->assignment_complete.remote_rtp);
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001586 if (rtps_to_ran->use_osmux)
1587 rtp_stream_set_remote_osmux_cid(rtps_to_ran,
1588 ac->assignment_complete.osmux_cid);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001589 rtp_stream_commit(rtps_to_ran);
1590
Neels Hofmeyr2d57d6e2022-01-13 21:39:58 +01001591 /* Remember the Codec List (BSS Supported) */
1592 if (ac->assignment_complete.codec_list_bss_supported)
1593 codec_filter_set_bss(&cc_trans->cc.codecs, ac->assignment_complete.codec_list_bss_supported);
1594
Oliver Smithceca8e62023-05-24 11:15:52 +02001595 trans_cc_filter_run(cc_trans);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001596 LOG_TRANS(cc_trans, LOGL_INFO, "Assignment Complete: RAN: %s, CN: %s\n",
1597 sdp_audio_codecs_to_str(&rtps_to_ran->codecs),
Oliver Smithc63c3a02023-05-24 10:48:07 +02001598 sdp_audio_codecs_to_str(&cc_trans->cc.local.audio_codecs));
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001599
1600 if (cc_on_assignment_done(cc_trans)) {
1601 /* If an error occurred, it was logged in cc_assignment_done() */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001602 call_leg_release(msc_a->cc.call_leg);
1603 return;
1604 }
1605}
1606
Neels Hofmeyr22d4f352024-06-21 19:08:34 +02001607/* Invoked when Assignment has failed, either by a failure response, or by timeout. When failing on timeout,
1608 * pass af == NULL. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001609static void msc_a_up_call_assignment_failure(struct msc_a *msc_a, const struct ran_msg *af)
1610{
1611 struct gsm_trans *trans;
1612
Neels Hofmeyr22d4f352024-06-21 19:08:34 +02001613 /* Pending assignment has failed. We're no longer waiting for a response now. */
1614 osmo_timer_del(&msc_a->cc.assignment_request_pending);
1615
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001616 /* For a normal voice call, there will be an rtp_stream FSM. */
1617 if (msc_a->cc.call_leg && msc_a->cc.call_leg->rtp[RTP_TO_RAN]) {
1618 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing call\n");
1619 rtp_stream_release(msc_a->cc.call_leg->rtp[RTP_TO_RAN]);
1620 return;
1621 }
1622
Andreas Eversberg23b53e52023-06-21 13:30:58 +02001623 /* For a voice group call, release is performed by VGCS FSM */
1624 trans = trans_find_by_type(msc_a, TRANS_GCC);
1625 if (trans) {
1626 vgcs_vbs_caller_assign_fail(trans);
1627 return;
1628 }
1629 trans = trans_find_by_type(msc_a, TRANS_BCC);
1630 if (trans) {
1631 vgcs_vbs_caller_assign_fail(trans);
1632 return;
1633 }
1634
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001635 /* Otherwise, a silent call might be active */
1636 trans = trans_find_by_type(msc_a, TRANS_SILENT_CALL);
1637 if (trans) {
1638 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing silent call\n");
1639 trans_free(trans);
1640 return;
1641 }
1642
1643 /* Neither a voice call nor silent call assignment. Assume the worst and detach. */
1644 msc_a_release_cn(msc_a);
1645}
1646
Neels Hofmeyr22d4f352024-06-21 19:08:34 +02001647static void assignment_request_timeout_cb(void *data)
1648{
1649 struct msc_a *msc_a = data;
1650 msc_a_up_call_assignment_failure(msc_a, NULL);
1651}
1652
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001653static void msc_a_up_classmark_update(struct msc_a *msc_a, const struct osmo_gsm48_classmark *classmark,
1654 struct osmo_gsm48_classmark *dst)
1655{
1656 if (!dst) {
1657 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1658
1659 if (!vsub)
1660 dst = &msc_a->temporary_classmark;
1661 else
1662 dst = &vsub->classmark;
1663 }
1664
Martin Hauke3f07dac2019-11-14 17:49:08 +01001665 LOG_MSC_A(msc_a, LOGL_DEBUG, "A5 capabilities received from Classmark Update: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001666 osmo_gsm48_classmark_a5_name(classmark));
1667 osmo_gsm48_classmark_update(dst, classmark);
1668
1669 /* bump subscr conn FSM in case it is waiting for a Classmark Update */
1670 if (msc_a->c.fi->state == MSC_A_ST_WAIT_CLASSMARK_UPDATE)
1671 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CLASSMARK_UPDATE, NULL);
1672}
1673
1674static void msc_a_up_sapi_n_reject(struct msc_a *msc_a, const struct ran_msg *msg)
1675{
1676 int sapi = msg->sapi_n_reject.dlci & 0x7;
1677 if (sapi == UM_SAPI_SMS)
1678 gsm411_sapi_n_reject(msc_a);
1679}
1680
1681static int msc_a_up_ho(struct msc_a *msc_a, const struct msc_a_ran_dec_data *d, uint32_t ho_fi_event)
1682{
1683 if (!msc_a->ho.fi) {
1684 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Handover message, but no Handover ongoing: %s\n", d->ran_dec->msg_name);
1685 return -EINVAL;
1686 }
1687 return osmo_fsm_inst_dispatch(msc_a->ho.fi, ho_fi_event, (void*)d);
1688}
1689
1690int msc_a_ran_dec_from_msc_i(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1691{
1692 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Alexander Couzens2aaff752021-10-19 17:09:11 +02001693 struct gsm_network *net = msc_a_net(msc_a);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001694 const struct ran_msg *msg = d->ran_dec;
1695 int rc = -99;
1696
1697 switch (msg->msg_type) {
1698
1699 case RAN_MSG_COMPL_L3:
Neels Hofmeyr68f50da2020-06-24 14:22:52 +02001700 /* In case the cell_id from Complete Layer 3 Information lacks a PLMN, write the configured PLMN code
1701 * into msc_a->via_cell. Then overwrite with those bits obtained from Complete Layer 3 Information. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001702 msc_a->via_cell = (struct osmo_cell_global_id){
1703 .lai.plmn = msc_a_net(msc_a)->plmn,
1704 };
1705 gsm0808_cell_id_to_cgi(&msc_a->via_cell, msg->compl_l3.cell_id);
Neels Hofmeyrede90832022-01-13 18:13:15 +01001706
Neels Hofmeyre276ae92022-01-13 21:38:35 +01001707 /* If a codec list was sent along in the RAN_MSG_COMPL_L3, remember it for any upcoming codec
1708 * resolution. */
1709 if (msg->compl_l3.codec_list_bss_supported) {
1710 msc_a->cc.compl_l3_codec_list_bss_supported = *msg->compl_l3.codec_list_bss_supported;
1711 if (log_check_level(msc_a->c.ran->log_subsys, LOGL_DEBUG)) {
1712 struct sdp_audio_codecs ac = {};
1713 sdp_audio_codecs_from_speech_codec_list(&ac, &msc_a->cc.compl_l3_codec_list_bss_supported);
1714 LOG_MSC_A(msc_a, LOGL_DEBUG, "Complete Layer 3: Codec List (BSS Supported): %s\n",
1715 sdp_audio_codecs_to_str(&ac));
1716 }
1717 }
1718
Neels Hofmeyrede90832022-01-13 18:13:15 +01001719 /* Submit the Complete Layer 3 Information DTAP */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001720 rc = msc_a_up_l3(msc_a, msg->compl_l3.msg);
1721 if (!rc) {
1722 struct ran_conn *conn = msub_ran_conn(msc_a->c.msub);
1723 if (conn)
1724 ran_peer_cells_seen_add(conn->ran_peer, msg->compl_l3.cell_id);
1725 }
1726 break;
1727
1728 case RAN_MSG_DTAP:
1729 rc = msc_a_up_l3(msc_a, msg->dtap);
1730 break;
1731
1732 case RAN_MSG_CLEAR_REQUEST:
1733 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
1734 break;
1735
1736 case RAN_MSG_CLEAR_COMPLETE:
1737 switch (msc_a->c.fi->state) {
1738 case MSC_A_ST_RELEASING:
1739 msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
1740 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
1741 break;
1742 case MSC_A_ST_RELEASED:
1743 break;
1744 default:
1745 LOG_MSC_A(msc_a, LOGL_ERROR, "Received Clear Complete event, but did not send Clear Command\n");
1746 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
1747 break;
1748 }
1749 rc = 0;
1750 break;
1751
1752 case RAN_MSG_CLASSMARK_UPDATE:
1753 msc_a_up_classmark_update(msc_a, msg->classmark_update.classmark, NULL);
1754 rc = 0;
1755 break;
1756
1757 case RAN_MSG_CIPHER_MODE_COMPLETE:
1758 /* Remember what Ciphering was negotiated (e.g. for Handover) */
1759 if (msg->cipher_mode_complete.alg_id) {
1760 msc_a->geran_encr.alg_id = msg->cipher_mode_complete.alg_id;
1761 LOG_MSC_A(msc_a, LOGL_DEBUG, "Cipher Mode Complete: chosen encryption algorithm: A5/%u\n",
1762 msc_a->geran_encr.alg_id - 1);
Alexander Couzens2aaff752021-10-19 17:09:11 +02001763 }
1764
1765 if (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU) {
1766 int16_t utran_encryption;
1767
1768 /* utran: ensure chosen ciphering mode is allowed
1769 * If the IE is missing (utran_encryption == -1), parse it as no encryption */
1770 utran_encryption = msg->cipher_mode_complete.utran_encryption;
1771 if (utran_encryption == -1)
1772 utran_encryption = 0;
1773 if ((net->uea_encryption_mask & (1 << utran_encryption)) == 0) {
1774 /* cipher disallowed */
1775 LOG_MSC_A(msc_a, LOGL_ERROR, "Cipher Mode Complete: RNC chosen forbidden ciphering UEA%d\n",
1776 msg->cipher_mode_complete.utran_encryption);
1777 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT);
1778 rc = 0;
1779 break;
1780 }
1781 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001782 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_COMPL);
1783 rc = 0;
Neels Hofmeyre9a39112019-08-29 00:10:49 +02001784
1785 /* Evaluate enclosed L3 message, typically Identity Response (IMEISV) */
1786 if (msg->cipher_mode_complete.l3_msg) {
1787 unsigned char *data = (unsigned char*)(msg->cipher_mode_complete.l3_msg->val);
1788 uint16_t len = msg->cipher_mode_complete.l3_msg->len;
1789 struct msgb *dtap = msgb_alloc(len, "DTAP from Cipher Mode Complete");
1790 unsigned char *pos = msgb_put(dtap, len);
1791 memcpy(pos, data, len);
1792 dtap->l3h = pos;
1793 rc = msc_a_up_l3(msc_a, dtap);
1794 msgb_free(dtap);
1795 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001796 break;
1797
1798 case RAN_MSG_CIPHER_MODE_REJECT:
1799 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT);
1800 rc = 0;
1801 break;
1802
1803 case RAN_MSG_ASSIGNMENT_COMPLETE:
1804 msc_a_up_call_assignment_complete(msc_a, msg);
1805 rc = 0;
1806 break;
1807
1808 case RAN_MSG_ASSIGNMENT_FAILURE:
1809 msc_a_up_call_assignment_failure(msc_a, msg);
1810 rc = 0;
1811 break;
1812
1813 case RAN_MSG_SAPI_N_REJECT:
1814 msc_a_up_sapi_n_reject(msc_a, msg);
1815 rc = 0;
1816 break;
1817
1818 case RAN_MSG_HANDOVER_PERFORMED:
1819 /* The BSS lets us know that a handover happened within the BSS, which doesn't concern us. */
1820 LOG_MSC_A(msc_a, LOGL_ERROR, "'Handover Performed' handling not implemented\n");
1821 break;
1822
1823 case RAN_MSG_HANDOVER_REQUIRED:
1824 /* The BSS lets us know that it wants to handover to a different cell */
1825 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_HANDOVER_REQUIRED, (void*)&msg->handover_required);
1826 break;
1827
1828 case RAN_MSG_HANDOVER_FAILURE:
1829 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1830 break;
1831
Keith Whytea1a70be2021-05-16 02:59:52 +02001832 case RAN_MSG_LCLS_STATUS:
1833 /* The BSS sends us LCLS_STATUS. We do nothing for now, but it is not an error. */
1834 LOG_MSC_A(msc_a, LOGL_DEBUG, "LCLS_STATUS (%s) received from MSC-I\n",
1835 gsm0808_lcls_status_name(msg->lcls_status.status));
1836 rc = 0;
1837 break;
1838
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001839 default:
1840 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-I not implemented: %s\n", ran_msg_type_name(msg->msg_type));
1841 rc = -ENOTSUP;
1842 break;
1843 }
1844 return rc;
1845}
1846
Andreas Eversbergf7c6f142023-04-23 12:10:42 +02001847static int msc_a_rx_vgcs_bss_decoded(struct osmo_fsm_inst *caller_fi, void *caller_data, const struct ran_msg *msg)
1848{
1849 struct vgcs_bss *bss = caller_data;
1850 struct msc_a *msc_a = (bss->trans) ? bss->trans->msc_a : NULL;
1851 int rc = 0;
1852
1853 switch (msg->msg_type) {
1854 case RAN_MSG_VGCS_VBS_SETUP_ACK:
1855 /* The BSS accepts VGCS/VBS and sends us supported features. */
1856 vgcs_vbs_setup_ack(bss, msg);
1857 break;
1858 case RAN_MSG_VGCS_VBS_SETUP_REFUSE:
1859 /* The BSS refuses VGCS/VBS. */
1860 vgcs_vbs_setup_refuse(bss, msg);
1861 break;
1862 case RAN_MSG_UPLINK_REQUEST:
1863 /* A mobile station requests the uplink on a VGCS channel. */
1864 vgcs_uplink_request(bss, msg);
1865 break;
1866 case RAN_MSG_UPLINK_REQUEST_CNF:
1867 /* The uplink on a VGCS channel has been established. */
1868 vgcs_uplink_request_cnf(bss, msg);
1869 break;
1870 case RAN_MSG_UPLINK_APPLICATION_DATA:
1871 /* Application data received on the uplink of a VGCS channel. */
1872 vgcs_app_data(bss, msg);
1873 break;
1874 case RAN_MSG_DTAP:
1875 /* BSS confirms the release of the channel. */
1876 vgcs_bss_dtap(bss, msg);
1877 break;
1878 case RAN_MSG_UPLINK_RELEASE_IND:
1879 /* A mobile station releases the uplink on a VGCS channel. */
1880 vgcs_uplink_release_ind(bss, msg);
1881 break;
1882 case RAN_MSG_CLEAR_REQUEST:
1883 /* BSS indicated that the channel has been released. */
1884 vgcs_vbs_clear_req(bss, msg);
1885 break;
1886 case RAN_MSG_CLEAR_COMPLETE:
1887 /* BSS confirms the release of the channel. */
1888 vgcs_vbs_clear_cpl(bss, msg);
1889 break;
1890 default:
1891 LOG_MSC_A(msc_a, LOGL_ERROR, "VGCS message from BSS not implemented: %s\n",
1892 ran_msg_type_name(msg->msg_type));
1893 rc = -ENOTSUP;
1894 break;
1895 }
1896 return rc;
1897}
1898
1899int msc_a_rx_vgcs_bss(struct vgcs_bss *bss, struct ran_conn *from_conn, struct msgb *msg)
1900{
1901 struct ran_dec ran_dec;
1902
1903 /* Feed through the decoding mechanism ran_msg. The decoded message arrives in msc_a_rx_vgcs_decoded() */
1904 ran_dec = (struct ran_dec) {
1905 .caller_data = bss,
1906 .decode_cb = msc_a_rx_vgcs_bss_decoded,
1907 };
1908 struct ran_peer *ran_peer = from_conn->ran_peer;
1909 struct ran_infra *ran = ran_peer->sri->ran;
1910 if (!ran->ran_dec_l2) {
1911 LOGP(DMSC, LOGL_ERROR, "No ran_dec_l2() defined for RAN type %s\n",
1912 osmo_rat_type_name(ran->type));
1913 return -ENOTSUP;
1914 }
1915 return ran->ran_dec_l2(&ran_dec, msg);
1916}
1917
1918static int msc_a_rx_vgcs_cell_decoded(struct osmo_fsm_inst *caller_fi, void *caller_data, const struct ran_msg *msg)
1919{
1920 struct vgcs_bss_cell *cell = caller_data;
1921 struct msc_a *msc_a = (cell->bss && cell->bss->trans) ? cell->bss->trans->msc_a : NULL;
1922 int rc = 0;
1923
1924 switch (msg->msg_type) {
1925 case RAN_MSG_VGCS_VBS_ASSIGN_RES:
1926 /* The BSS accepts VGCS/VBS channel assignment. */
1927 vgcs_vbs_assign_result(cell, msg);
1928 break;
1929 case RAN_MSG_VGCS_VBS_ASSIGN_FAIL:
1930 /* The BSS refuses VGCS/VBS channel assignment. */
1931 vgcs_vbs_assign_fail(cell, msg);
1932 break;
1933 case RAN_MSG_VGCS_VBS_QUEUING_IND:
1934 /* The BSS needs more time for VGCS/VBS channel assignment. */
1935 vgcs_vbs_queuing_ind(cell);
1936 break;
1937 case RAN_MSG_VGCS_VBS_ASSIGN_STATUS:
1938 /* The BSS gives cell status about VGCS/VBS channel. */
1939 vgcs_vbs_assign_status(cell, msg);
1940 break;
1941 case RAN_MSG_CLEAR_REQUEST:
1942 /* BSS indicated that the channel has been released. */
1943 vgcs_vbs_clear_req_channel(cell, msg);
1944 break;
1945 case RAN_MSG_CLEAR_COMPLETE:
1946 /* BSS confirms the release of the channel. */
1947 vgcs_vbs_clear_cpl_channel(cell, msg);
1948 break;
1949 default:
1950 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from BSS leg not implemented: %s\n",
1951 ran_msg_type_name(msg->msg_type));
1952 rc = -ENOTSUP;
1953 break;
1954 }
1955 return rc;
1956}
1957
1958int msc_a_rx_vgcs_cell(struct vgcs_bss_cell *cell, struct ran_conn *from_conn, struct msgb *msg)
1959{
1960 struct ran_dec ran_dec;
1961
1962 /* Feed through the decoding mechanism ran_msg. The decoded message arrives in msc_a_rx_vgcs_decoded() */
1963 ran_dec = (struct ran_dec) {
1964 .caller_data = cell,
1965 .decode_cb = msc_a_rx_vgcs_cell_decoded,
1966 };
1967 struct ran_peer *ran_peer = from_conn->ran_peer;
1968 struct ran_infra *ran = ran_peer->sri->ran;
1969 if (!ran->ran_dec_l2) {
1970 LOGP(DMSC, LOGL_ERROR, "No ran_dec_l2() defined for RAN type %s\n",
1971 osmo_rat_type_name(ran->type));
1972 return -ENOTSUP;
1973 }
1974 return ran->ran_dec_l2(&ran_dec, msg);
1975}
1976
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001977static int msc_a_ran_dec_from_msc_t(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1978{
1979 struct msc_t *msc_t = msc_a_msc_t(msc_a);
1980 int rc = -99;
1981
1982 if (!msc_t) {
1983 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx message from MSC-T role, but I have no active MSC-T role.\n");
1984 return -EINVAL;
1985 }
1986
1987 OSMO_ASSERT(d->ran_dec);
1988
1989 switch (d->ran_dec->msg_type) {
1990
1991 case RAN_MSG_CLEAR_REQUEST:
1992 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_MO_CLOSE, NULL);
1993 break;
1994
1995 case RAN_MSG_CLEAR_COMPLETE:
1996 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_CLEAR_COMPLETE, NULL);
1997 break;
1998
1999 case RAN_MSG_CLASSMARK_UPDATE:
2000 msc_a_up_classmark_update(msc_a, d->ran_dec->classmark_update.classmark, &msc_t->classmark);
2001 rc = 0;
2002 break;
2003
2004 case RAN_MSG_HANDOVER_REQUEST_ACK:
2005 /* new BSS accepts Handover */
2006 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_REQUEST_ACK);
2007 break;
2008
2009 case RAN_MSG_HANDOVER_DETECT:
2010 /* new BSS signals the MS is DETECTed on the new lchan */
2011 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_DETECT);
2012 break;
2013
2014 case RAN_MSG_HANDOVER_COMPLETE:
2015 /* new BSS signals the MS has fully moved to the new lchan */
2016 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_COMPLETE);
2017 break;
2018
2019 case RAN_MSG_HANDOVER_FAILURE:
2020 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
2021 break;
2022
2023 default:
2024 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-T not implemented: %s\n",
2025 ran_msg_type_name(d->ran_dec->msg_type));
2026 rc = -ENOTSUP;
2027 break;
2028 }
2029 return rc;
2030}
2031
2032int msc_a_ran_decode_cb(struct osmo_fsm_inst *msc_a_fi, void *data, const struct ran_msg *msg)
2033{
2034 struct msc_a *msc_a = msc_a_fi_priv(msc_a_fi);
2035 struct msc_a_ran_dec_data *d = data;
2036 int rc = -99;
2037
2038 d->ran_dec = msg;
2039
2040 switch (d->from_role) {
2041 case MSC_ROLE_I:
2042 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode: %s\n", msg->msg_name ? : ran_msg_type_name(msg->msg_type));
2043 rc = msc_a_ran_dec_from_msc_i(msc_a, d);
2044 break;
2045
2046 case MSC_ROLE_T:
2047 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode from MSC-T: %s\n",
2048 msg->msg_name ? : ran_msg_type_name(msg->msg_type));
2049 rc = msc_a_ran_dec_from_msc_t(msc_a, d);
2050 break;
2051
2052 default:
2053 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from invalid role %s: %s\n", msc_role_name(d->from_role),
2054 ran_msg_type_name(msg->msg_type));
2055 return -ENOTSUP;
2056 }
2057
2058 if (rc)
2059 LOG_MSC_A(msc_a, LOGL_ERROR, "RAN decode error (rc=%d) for %s from %s\n", rc, ran_msg_type_name(msg->msg_type),
2060 msc_role_name(d->from_role));
2061 return rc;
2062}
2063
2064/* Your typical DTAP via FORWARD_ACCESS_SIGNALLING_REQUEST */
2065int _msc_a_ran_down(struct msc_a *msc_a, enum msc_role to_role, const struct ran_msg *ran_msg,
2066 const char *file, int line)
2067{
2068 return _msc_a_msg_down(msc_a, to_role, msub_role_to_role_event(msc_a->c.msub, MSC_ROLE_A, to_role),
2069 ran_msg, file, line);
2070}
2071
2072/* To transmit more complex events than just FORWARD_ACCESS_SIGNALLING_REQUEST, e.g. an
2073 * MSC_T_EV_FROM_A_PREPARE_HANDOVER_REQUEST */
2074int _msc_a_msg_down(struct msc_a *msc_a, enum msc_role to_role, uint32_t to_role_event,
2075 const struct ran_msg *ran_msg,
2076 const char *file, int line)
2077{
2078 struct an_apdu an_apdu = {
2079 .an_proto = msc_a->c.ran->an_proto,
2080 .msg = msc_role_ran_encode(msc_a->c.fi, ran_msg),
2081 };
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002082 if (!an_apdu.msg)
2083 return -EIO;
Vadim Yanitskiyc44342b2021-12-07 18:32:35 +03002084 return _msub_role_dispatch(msc_a->c.msub, to_role, to_role_event, &an_apdu, file, line);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002085}
2086
2087int msc_a_tx_dtap_to_i(struct msc_a *msc_a, struct msgb *dtap)
2088{
2089 struct ran_msg ran_msg;
Neels Hofmeyrc192c0b2019-10-07 21:41:18 +02002090 struct gsm48_hdr *gh = msgb_l3(dtap) ? : dtap->data;
2091 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002092
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02002093 if (!msc_a) {
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02002094 LOGP(DMSC, LOGL_ERROR, "Attempt to send DTAP to NULL MSC-A, dropping message: %s %s\n",
2095 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
2096 msgb_free(dtap);
2097 return -EIO;
2098 }
2099
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002100 if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) {
2101 /* The SGs connection to the MME always is at the MSC-A. */
2102 return sgs_iface_tx_dtap_ud(msc_a, dtap);
2103 }
2104
Neels Hofmeyrc192c0b2019-10-07 21:41:18 +02002105 LOG_MSC_A(msc_a, LOGL_DEBUG, "Sending DTAP: %s %s\n",
2106 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
2107
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002108 ran_msg = (struct ran_msg){
2109 .msg_type = RAN_MSG_DTAP,
2110 .dtap = dtap,
2111 };
2112 return msc_a_ran_down(msc_a, MSC_ROLE_I, &ran_msg);
2113}
2114
2115struct msc_a *msc_a_for_vsub(const struct vlr_subscr *vsub, bool valid_conn_only)
2116{
2117 struct msc_a *msc_a = msub_msc_a(msub_for_vsub(vsub));
2118 if (valid_conn_only && !msc_a_is_accepted(msc_a))
2119 return NULL;
2120 return msc_a;
2121}
2122
2123int msc_tx_common_id(struct msc_a *msc_a, enum msc_role to_role)
2124{
2125 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Vadim Yanitskiy435f67f2021-06-06 15:47:49 +02002126 if (vsub == NULL)
2127 return -ENODEV;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002128 struct ran_msg msg = {
2129 .msg_type = RAN_MSG_COMMON_ID,
2130 .common_id = {
2131 .imsi = vsub->imsi,
Pau Espin Pedrol67106702021-04-27 18:20:15 +02002132 .last_eutran_plmn_present = vsub->sgs.last_eutran_plmn_present,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002133 },
2134 };
Pau Espin Pedrol67106702021-04-27 18:20:15 +02002135 if (vsub->sgs.last_eutran_plmn_present) {
2136 memcpy(&msg.common_id.last_eutran_plmn, &vsub->sgs.last_eutran_plmn,
2137 sizeof(vsub->sgs.last_eutran_plmn));
2138 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002139
2140 return msc_a_ran_down(msc_a, to_role, &msg);
2141}
2142
2143static int msc_a_start_assignment(struct msc_a *msc_a, struct gsm_trans *cc_trans)
2144{
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002145 struct call_leg *cl;
2146 bool cn_rtp_available;
2147 bool ran_rtp_available;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002148
2149 OSMO_ASSERT(!msc_a->cc.active_trans);
2150 msc_a->cc.active_trans = cc_trans;
2151
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01002152 cc_trans->cc.codecs.assignment = (struct sdp_audio_codec){};
2153
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002154 OSMO_ASSERT(cc_trans && cc_trans->type == TRANS_CC);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002155 cl = msc_a_ensure_call_leg(msc_a, cc_trans);
2156 if (!cl)
2157 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002158
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002159 /* See if we can set a preliminary codec. If not, pass none for the time being. */
Oliver Smithceca8e62023-05-24 11:15:52 +02002160 trans_cc_filter_run(cc_trans);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002161
2162 cn_rtp_available = call_leg_local_ip(cl, RTP_TO_CN);
2163 ran_rtp_available = call_leg_local_ip(cl, RTP_TO_RAN);
2164
2165 /* Set up RTP ports for both RAN and CN side. Even though we ask for both at the same time, the
2166 * osmo_mgcpc_ep_fsm automagically waits for the first CRCX to complete before firing the second CRCX. The one
2167 * issued first here will also be the first CRCX sent to the MGW. Usually both still need to be set up. */
2168 if (!cn_rtp_available)
Andreas Eversberg712b28e2023-06-21 11:17:26 +02002169 call_leg_ensure_ci(cl, RTP_TO_CN, cc_trans->call_id, cc_trans,
Oliver Smithc63c3a02023-05-24 10:48:07 +02002170 &cc_trans->cc.local.audio_codecs, NULL);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002171 if (!ran_rtp_available) {
2172 struct sdp_audio_codecs *codecs;
2173 if (msc_a->c.ran->force_mgw_codecs_to_ran.count)
2174 codecs = &msc_a->c.ran->force_mgw_codecs_to_ran;
2175 else
Oliver Smithc63c3a02023-05-24 10:48:07 +02002176 codecs = &cc_trans->cc.local.audio_codecs;
Andreas Eversberg712b28e2023-06-21 11:17:26 +02002177 return call_leg_ensure_ci(cl, RTP_TO_RAN, cc_trans->call_id, cc_trans, codecs, NULL);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002178 }
2179
2180 /* Should these already be set up, immediately continue by retriggering the events signalling that the RTP
2181 * ports are available. The ordering is: first CN, then RAN. */
2182 if (cn_rtp_available && ran_rtp_available)
2183 return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_RAN]);
2184 else if (cn_rtp_available)
2185 return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_CN]);
2186 /* Otherwise wait for MGCP response and continue from there. */
2187 return 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002188}
2189
2190int msc_a_try_call_assignment(struct gsm_trans *cc_trans)
2191{
2192 struct msc_a *msc_a = cc_trans->msc_a;
2193 OSMO_ASSERT(cc_trans->type == TRANS_CC);
2194
2195 if (msc_a->cc.active_trans == cc_trans) {
Neels Hofmeyrb4ef5e72019-08-30 01:11:12 +02002196 LOG_MSC_A(msc_a, LOGL_DEBUG, "Assignment for this trans already started earlier\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002197 return 0;
2198 }
2199
2200 if (msc_a->cc.active_trans) {
2201 LOG_MSC_A(msc_a, LOGL_INFO, "Another call is already ongoing, not assigning yet\n");
2202 return 0;
2203 }
2204
2205 LOG_MSC_A(msc_a, LOGL_DEBUG, "Starting call assignment\n");
2206 return msc_a_start_assignment(msc_a, cc_trans);
2207}
2208
Neels Hofmeyr5d53c602022-04-24 23:37:07 +02002209/* Map CM Service type to use token.
2210 * Given a CM Service type, return a matching token intended for osmo_use_count.
2211 * For unknown service type, return NULL.
2212 */
Andreas Eversbergcd8bd452023-07-11 14:16:24 +02002213const char *msc_a_cm_service_type_to_use(struct msc_a *msc_a, enum osmo_cm_service_type cm_service_type)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002214{
Andreas Eversbergcd8bd452023-07-11 14:16:24 +02002215 struct gsm_network *net = msc_a_net(msc_a);
2216
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002217 switch (cm_service_type) {
2218 case GSM48_CMSERV_MO_CALL_PACKET:
2219 case GSM48_CMSERV_EMERGENCY:
2220 return MSC_A_USE_CM_SERVICE_CC;
2221
2222 case GSM48_CMSERV_SMS:
2223 return MSC_A_USE_CM_SERVICE_SMS;
2224
2225 case GSM48_CMSERV_SUP_SERV:
2226 return MSC_A_USE_CM_SERVICE_SS;
2227
Andreas Eversberg456c6f72023-04-23 11:54:16 +02002228 case GSM48_CMSERV_VGCS:
Andreas Eversbergcd8bd452023-07-11 14:16:24 +02002229 if (net->asci.enable)
2230 return MSC_A_USE_CM_SERVICE_GCC;
2231 else
2232 return NULL;
Andreas Eversberg456c6f72023-04-23 11:54:16 +02002233
2234 case GSM48_CMSERV_VBS:
Andreas Eversbergcd8bd452023-07-11 14:16:24 +02002235 if (net->asci.enable)
2236 return MSC_A_USE_CM_SERVICE_BCC;
2237 else
2238 return NULL;
Andreas Eversberg456c6f72023-04-23 11:54:16 +02002239
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002240 default:
2241 return NULL;
2242 }
2243}
2244
2245void msc_a_release_cn(struct msc_a *msc_a)
2246{
2247 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CN_CLOSE, NULL);
2248}
2249
2250void msc_a_release_mo(struct msc_a *msc_a, enum gsm48_gsm_cause gsm_cause)
2251{
2252 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
2253}