blob: 387532d43983643697094cc5f089c7e240b8278b [file] [log] [blame]
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001/* Code to manage a subscriber's MSC-A role */
2/*
3 * (C) 2019 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de>
4 * 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>
39#include <osmocom/msc/ran_peer.h>
40#include <osmocom/msc/ran_msg_a.h>
41#include <osmocom/msc/ran_msg_iu.h>
42#include <osmocom/msc/sgs_iface.h>
43#include <osmocom/msc/gsm_04_08.h>
44#include <osmocom/msc/gsm_09_11.h>
45#include <osmocom/msc/gsm_04_14.h>
46#include <osmocom/msc/call_leg.h>
47#include <osmocom/msc/rtp_stream.h>
48#include <osmocom/msc/msc_ho.h>
Neels Hofmeyre276ae92022-01-13 21:38:35 +010049#include <osmocom/msc/codec_mapping.h>
Neels Hofmeyr2d57d6e2022-01-13 21:39:58 +010050#include <osmocom/msc/codec_filter.h>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010051
52#define MSC_A_USE_WAIT_CLEAR_COMPLETE "wait-Clear-Complete"
53
54static struct osmo_fsm msc_a_fsm;
55
56static const struct osmo_tdef_state_timeout msc_a_fsm_timeouts[32] = {
57 [MSC_A_ST_VALIDATE_L3] = { .T = -1 },
58 [MSC_A_ST_AUTH_CIPH] = { .keep_timer = true },
59 [MSC_A_ST_WAIT_CLASSMARK_UPDATE] = { .keep_timer = true },
60 [MSC_A_ST_AUTHENTICATED] = { .keep_timer = true },
61 [MSC_A_ST_RELEASING] = { .T = -2 },
62 [MSC_A_ST_RELEASED] = { .T = -2 },
63};
64
65/* Transition to a state, using the T timer defined in msc_a_fsm_timeouts.
66 * The actual timeout value is in turn obtained from network->T_defs.
67 * Assumes local variable fi exists. */
Neels Hofmeyr01653252019-09-03 02:06:22 +020068#define msc_a_state_chg_always(msc_a, state) \
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010069 osmo_tdef_fsm_inst_state_chg((msc_a)->c.fi, state, msc_a_fsm_timeouts, (msc_a)->c.ran->tdefs, 5)
70
Neels Hofmeyr01653252019-09-03 02:06:22 +020071/* Same as msc_a_state_chg_always() but ignore if the msc_a already is in the target state. */
72#define msc_a_state_chg(msc_a, STATE) do { \
73 if ((msc_a)->c.fi->state != STATE) \
74 msc_a_state_chg_always(msc_a, STATE); \
75 } while(0)
76
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010077struct gsm_network *msc_a_net(const struct msc_a *msc_a)
78{
79 return msub_net(msc_a->c.msub);
80}
81
82struct vlr_subscr *msc_a_vsub(const struct msc_a *msc_a)
83{
Neels Hofmeyr911e5972019-05-09 13:28:26 +020084 if (!msc_a)
85 return NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010086 return msub_vsub(msc_a->c.msub);
87}
88
89struct msc_i *msc_a_msc_i(const struct msc_a *msc_a)
90{
Neels Hofmeyr911e5972019-05-09 13:28:26 +020091 if (!msc_a)
92 return NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010093 return msub_msc_i(msc_a->c.msub);
94}
95
96struct msc_t *msc_a_msc_t(const struct msc_a *msc_a)
97{
Neels Hofmeyr911e5972019-05-09 13:28:26 +020098 if (!msc_a)
99 return NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100100 return msub_msc_t(msc_a->c.msub);
101}
102
103struct msc_a *msc_a_fi_priv(struct osmo_fsm_inst *fi)
104{
105 OSMO_ASSERT(fi);
106 OSMO_ASSERT(fi->fsm == &msc_a_fsm);
107 OSMO_ASSERT(fi->priv);
108 return fi->priv;
109}
110
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200111bool msc_a_is_ciphering_to_be_attempted(const struct msc_a *msc_a)
Alexander Couzens2c5e4612021-11-05 02:00:17 +0100112{
113 struct gsm_network *net = msc_a_net(msc_a);
114 bool is_utran = (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU);
115 if (is_utran)
116 return net->uea_encryption_mask > (1 << OSMO_UTRAN_UEA0);
117 else
118 return net->a5_encryption_mask > 0x1;
119}
120
Neels Hofmeyr2ea72642022-10-10 23:35:47 +0200121bool msc_a_is_ciphering_required(const struct msc_a *msc_a)
122{
123 struct gsm_network *net = msc_a_net(msc_a);
124 bool is_utran = (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU);
125 if (is_utran)
126 return net->uea_encryption_mask
127 && ((net->uea_encryption_mask & (1 << OSMO_UTRAN_UEA0)) == 0);
128 else
129 return net->a5_encryption_mask
130 && ((net->a5_encryption_mask & 0x1) == 0);
131}
132
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100133static void update_counters(struct osmo_fsm_inst *fi, bool conn_accepted)
134{
135 struct msc_a *msc_a = fi->priv;
136 struct gsm_network *net = msc_a_net(msc_a);
137 switch (msc_a->complete_layer3_type) {
138 case COMPLETE_LAYER3_LU:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200139 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 +0100140 break;
141 case COMPLETE_LAYER3_CM_SERVICE_REQ:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200142 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 +0100143 break;
144 case COMPLETE_LAYER3_PAGING_RESP:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200145 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 +0100146 break;
Neels Hofmeyrae98b972021-07-27 03:46:49 +0200147 case COMPLETE_LAYER3_CM_RE_ESTABLISH_REQ:
148 rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs,
149 conn_accepted ? MSC_CTR_CM_RE_ESTABLISH_REQ_ACCEPTED
150 : MSC_CTR_CM_RE_ESTABLISH_REQ_REJECTED));
151 break;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100152 default:
153 break;
154 }
155}
156
157static void evaluate_acceptance_outcome(struct osmo_fsm_inst *fi, bool conn_accepted)
158{
159 struct msc_a *msc_a = fi->priv;
160 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
161
162 update_counters(fi, conn_accepted);
163
Neels Hofmeyr83cf10c2020-06-24 14:23:26 +0200164 if (conn_accepted) {
165 /* Record the Cell ID seen in Complete Layer 3 Information in the VLR, so that it also shows in vty
166 * 'show' output. */
167 vsub->cgi = msc_a->via_cell;
168 }
169
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100170 /* Trigger transactions that we paged for */
171 if (msc_a->complete_layer3_type == COMPLETE_LAYER3_PAGING_RESP) {
172 if (conn_accepted)
173 paging_response(msc_a);
174 else
175 paging_expired(vsub);
176 }
177
178 if (conn_accepted)
179 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_ATTACHED, msc_a_vsub(msc_a));
180
181 if (msc_a->complete_layer3_type == COMPLETE_LAYER3_LU)
182 msc_a_put(msc_a, MSC_A_USE_LOCATION_UPDATING);
Neels Hofmeyrae98b972021-07-27 03:46:49 +0200183
184 if (msc_a->complete_layer3_type == COMPLETE_LAYER3_CM_RE_ESTABLISH_REQ) {
185 /* Trigger new Assignment to recommence the voice call. A little dance here because normally we verify
186 * that no CC trans is already active. */
187 struct gsm_trans *cc_trans = msc_a->cc.active_trans;
188 msc_a->cc.active_trans = NULL;
189 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, cc_trans);
190 msc_a_try_call_assignment(cc_trans);
191 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100192}
193
194bool msc_a_is_accepted(const struct msc_a *msc_a)
195{
196 if (!msc_a || !msc_a->c.fi)
197 return false;
198 return msc_a->c.fi->state == MSC_A_ST_AUTHENTICATED
199 || msc_a->c.fi->state == MSC_A_ST_COMMUNICATING;
200}
201
202bool msc_a_in_release(struct msc_a *msc_a)
203{
204 if (!msc_a)
205 return true;
206 if (msc_a->c.fi->state == MSC_A_ST_RELEASING)
207 return true;
208 if (msc_a->c.fi->state == MSC_A_ST_RELEASED)
209 return true;
210 return false;
211}
212
213static int msc_a_ran_dec(struct msc_a *msc_a, const struct an_apdu *an_apdu, enum msc_role from_role)
214{
215 int rc;
216 struct msc_a_ran_dec_data d = {
217 .from_role = from_role,
218 .an_apdu = an_apdu,
219 };
220 msc_a_get(msc_a, __func__);
221 rc = msc_role_ran_decode(msc_a->c.fi, an_apdu, msc_a_ran_decode_cb, &d);
222 msc_a_put(msc_a, __func__);
223 return rc;
224};
225
226static void msc_a_fsm_validate_l3(struct osmo_fsm_inst *fi, uint32_t event, void *data)
227{
228 struct msc_a *msc_a = fi->priv;
229 const struct an_apdu *an_apdu;
230
231 switch (event) {
232 case MSC_A_EV_FROM_I_COMPLETE_LAYER_3:
233 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
234 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
235 an_apdu = data;
236 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_I);
237 return;
238
239 case MSC_A_EV_COMPLETE_LAYER_3_OK:
240 msc_a_state_chg(msc_a, MSC_A_ST_AUTH_CIPH);
241 return;
242
243 case MSC_A_EV_MO_CLOSE:
244 case MSC_A_EV_CN_CLOSE:
245 evaluate_acceptance_outcome(fi, false);
246 /* fall through */
247 case MSC_A_EV_UNUSED:
248 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
249 return;
250
251 default:
252 OSMO_ASSERT(false);
253 }
254}
255
256/* Figure out whether to first send a Classmark Request to the MS to figure out algorithm support. */
257static bool msc_a_need_classmark_for_ciphering(struct msc_a *msc_a)
258{
259 struct gsm_network *net = msc_a_net(msc_a);
260 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
261 int i = 0;
262 bool request_classmark = false;
263
264 /* Only on GERAN-A do we ever need Classmark Information for Ciphering. */
265 if (msc_a->c.ran->type != OSMO_RAT_GERAN_A)
266 return false;
267
268 for (i = 0; i < 8; i++) {
269 int supported;
270
271 /* A5/n permitted by osmo-msc.cfg? */
272 if (!(net->a5_encryption_mask & (1 << i)))
273 continue;
274
275 /* A5/n supported by MS? */
276 supported = osmo_gsm48_classmark_supports_a5(&vsub->classmark, i);
277 if (supported < 0) {
278 LOG_MSC_A(msc_a, LOGL_DEBUG, "For A5/%d, we still need Classmark %d\n", i, -supported);
279 request_classmark = true;
280 }
281 }
282
283 return request_classmark;
284}
285
286static int msc_a_ran_enc_ciphering(struct msc_a *msc_a, bool umts_aka, bool retrieve_imeisv);
287
288/* VLR callback for ops.set_ciph_mode() */
289int msc_a_vlr_set_cipher_mode(void *_msc_a, bool umts_aka, bool retrieve_imeisv)
290{
291 struct msc_a *msc_a = _msc_a;
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700292 struct vlr_subscr *vsub;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100293
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700294 if (!msc_a) {
295 LOGP(DMSC, LOGL_ERROR, "Insufficient info to start ciphering: "
296 "MSC-A role is NULL?!?\n");
297 return -EINVAL;
298 }
299
300 vsub = msc_a_vsub(msc_a);
301 if (!vsub || !vsub->last_tuple) {
302 LOG_MSC_A(msc_a, LOGL_ERROR, "Insufficient info to start ciphering: "
303 "vlr_subscr is NULL?!?\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100304 return -EINVAL;
305 }
306
307 if (msc_a_need_classmark_for_ciphering(msc_a)) {
308 int rc;
309 struct ran_msg msg = {
310 .msg_type = RAN_MSG_CLASSMARK_REQUEST,
311 };
312 rc = msc_a_ran_down(msc_a, MSC_ROLE_I, &msg);
313 if (rc) {
314 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot send Classmark Request\n");
315 return -EIO;
316 }
317
318 msc_a->state_before_classmark_update = msc_a->c.fi->state;
319 msc_a->action_on_classmark_update = (struct msc_a_action_on_classmark_update){
320 .type = MSC_A_CLASSMARK_UPDATE_THEN_CIPHERING,
321 .ciphering = {
322 .umts_aka = umts_aka,
323 .retrieve_imeisv = retrieve_imeisv,
324 },
325 };
326 msc_a_state_chg(msc_a, MSC_A_ST_WAIT_CLASSMARK_UPDATE);
327 return 0;
328 }
329
330 return msc_a_ran_enc_ciphering(msc_a, umts_aka, retrieve_imeisv);
331}
332
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +0200333static uint8_t filter_a5(uint8_t a5_mask, bool umts_aka)
334{
335 /* With GSM AKA: allow A5/0, 1, 3 = 0b00001011 = 0xb.
336 * UMTS aka: allow A5/0, 1, 3, 4 = 0b00011011 = 0x1b.
337 */
338 return a5_mask & (umts_aka ? 0x1b : 0x0b);
339}
340
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100341static int msc_a_ran_enc_ciphering(struct msc_a *msc_a, bool umts_aka, bool retrieve_imeisv)
342{
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700343 struct gsm_network *net;
344 struct vlr_subscr *vsub;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100345 struct ran_msg msg;
346
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700347 if (!msc_a) {
348 LOGP(DMSC, LOGL_ERROR, "Insufficient info to start ciphering: "
349 "MSC-A role is NULL?!?\n");
350 return -EINVAL;
351 }
352
353 net = msc_a_net(msc_a);
354 vsub = msc_a_vsub(msc_a);
355
356 if (!net || !vsub || !vsub->last_tuple) {
357 LOG_MSC_A(msc_a, LOGL_ERROR, "Insufficient info to start ciphering: "
358 "gsm_network and/or vlr_subscr is NULL?!?\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100359 return -EINVAL;
360 }
361
362 msg = (struct ran_msg){
363 .msg_type = RAN_MSG_CIPHER_MODE_COMMAND,
364 .cipher_mode_command = {
365 .vec = vsub->last_tuple ? &vsub->last_tuple->vec : NULL,
366 .classmark = &vsub->classmark,
367 .geran = {
368 .umts_aka = umts_aka,
369 .retrieve_imeisv = retrieve_imeisv,
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +0200370 .a5_encryption_mask = filter_a5(net->a5_encryption_mask, umts_aka),
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100371
372 /* for ran_a.c to store the GERAN key that is actually used */
373 .chosen_key = &msc_a->geran_encr,
374 },
Harald Welte274b70f2021-02-06 16:47:39 +0100375 .utran = {
Harald Welte505a94a2021-02-06 17:12:20 +0100376 .uea_encryption_mask = net->uea_encryption_mask,
377 },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100378 },
379 };
380
381 if (msc_a_ran_down(msc_a, MSC_ROLE_I, &msg)) {
382 LOG_MSC_A(msc_a, LOGL_ERROR, "Sending Cipher Mode Command failed\n");
383 /* Returning error to the VLR ops.set_ciph_mode() will cancel the attach. Other callers need to take
384 * care of the return value. */
385 return -EINVAL;
386 }
387
388 if (msc_a->geran_encr.key_len)
Neels Hofmeyr73d093a2021-06-23 23:54:43 +0200389 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN encoding chose ciphering: A5/%d kc %s kc128 %s\n",
390 msc_a->geran_encr.alg_id - 1,
391 osmo_hexdump_nospc_c(OTC_SELECT, msc_a->geran_encr.key, msc_a->geran_encr.key_len),
392 msc_a->geran_encr.kc128_present ?
393 osmo_hexdump_nospc_c(OTC_SELECT, msc_a->geran_encr.kc128, sizeof(msc_a->geran_encr.kc128))
394 : "-");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100395 return 0;
396}
397
398static void msc_a_fsm_auth_ciph(struct osmo_fsm_inst *fi, uint32_t event, void *data)
399{
400 struct msc_a *msc_a = fi->priv;
401
402 /* If accepted, transition the state, all other cases mean failure. */
403 switch (event) {
404 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
405 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
406 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
407 return;
408
409 case MSC_A_EV_AUTHENTICATED:
410 msc_a_state_chg(msc_a, MSC_A_ST_AUTHENTICATED);
411 return;
412
413 case MSC_A_EV_UNUSED:
414 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
415 return;
416
417 case MSC_A_EV_MO_CLOSE:
418 case MSC_A_EV_CN_CLOSE:
419 evaluate_acceptance_outcome(fi, false);
420 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
421 return;
422
423
424 default:
425 OSMO_ASSERT(false);
426 }
427}
428
429static void msc_a_fsm_wait_classmark_update(struct osmo_fsm_inst *fi, uint32_t event, void *data)
430{
431 struct msc_a *msc_a = fi->priv;
432
433 switch (event) {
434 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
435 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
436 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
437 return;
438
439 case MSC_A_EV_CLASSMARK_UPDATE:
440 switch (msc_a->action_on_classmark_update.type) {
441 case MSC_A_CLASSMARK_UPDATE_THEN_CIPHERING:
442 msc_a_state_chg(msc_a, MSC_A_ST_AUTH_CIPH);
443 if (msc_a_ran_enc_ciphering(msc_a,
444 msc_a->action_on_classmark_update.ciphering.umts_aka,
445 msc_a->action_on_classmark_update.ciphering.retrieve_imeisv)) {
446 LOG_MSC_A(msc_a, LOGL_ERROR,
447 "After Classmark Update, still failed to send Cipher Mode Command\n");
448 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
449 }
450 return;
451
452 default:
453 LOG_MSC_A(msc_a, LOGL_ERROR, "Internal error: After Classmark Update, don't know what to do\n");
454 msc_a_state_chg(msc_a, msc_a->state_before_classmark_update);
455 return;
456 }
457
458 case MSC_A_EV_UNUSED:
459 /* Seems something detached / aborted in the middle of auth+ciph. */
460 evaluate_acceptance_outcome(fi, false);
461 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
462 return;
463
464 case MSC_A_EV_MO_CLOSE:
465 case MSC_A_EV_CN_CLOSE:
466 evaluate_acceptance_outcome(fi, false);
467 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
468 return;
469
470 default:
471 OSMO_ASSERT(false);
472 }
473}
474
475static bool msc_a_fsm_has_active_transactions(struct osmo_fsm_inst *fi)
476{
477 struct msc_a *msc_a = fi->priv;
478 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
479 struct gsm_trans *trans;
480
481 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_SILENT_CALL)) {
482 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: silent call still active\n", __func__);
483 return true;
484 }
485
486 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC)) {
487 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO CC request after a CM Service Request\n",
488 __func__);
489 return true;
490 }
491 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_SMS)) {
492 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO SMS after a CM Service Request\n",
493 __func__);
494 return true;
495 }
496 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_SS)) {
497 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO SS after a CM Service Request\n",
498 __func__);
499 return true;
500 }
501
502 if (vsub && !llist_empty(&vsub->cs.requests)) {
503 struct paging_request *pr;
504 llist_for_each_entry(pr, &vsub->cs.requests, entry) {
505 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still active: %s\n", __func__, pr->label);
506 }
507 return true;
508 }
509
510 if ((trans = trans_has_conn(msc_a))) {
511 LOG_MSC_A(msc_a, LOGL_DEBUG, "connection still has active transaction: %s\n",
512 trans_type_name(trans->type));
513 return true;
514 }
515
516 return false;
517}
518
519static void msc_a_fsm_authenticated_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
520{
521 struct msc_a *msc_a = fi->priv;
522 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
523
524 /* Stop Location Update expiry for this subscriber. While the subscriber
525 * has an open connection the LU expiry timer must remain disabled.
526 * Otherwise we would kick the subscriber off the network when the timer
527 * expires e.g. during a long phone call.
528 * The LU expiry timer will restart once the connection is closed. */
529 if (vsub)
530 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
531
532 evaluate_acceptance_outcome(fi, true);
533}
534
535static void msc_a_fsm_authenticated(struct osmo_fsm_inst *fi, uint32_t event, void *data)
536{
537 struct msc_a *msc_a = fi->priv;
538
539 switch (event) {
540 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
541 case MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST:
542 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
543 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
544 return;
545
546 case MSC_A_EV_COMPLETE_LAYER_3_OK:
547 /* When Authentication is off, we may already be in the Accepted state when the code
548 * evaluates the Compl L3. Simply ignore. This just cosmetically mutes the error log
549 * about the useless event. */
550 return;
551
552 case MSC_A_EV_TRANSACTION_ACCEPTED:
553 msc_a_state_chg(msc_a, MSC_A_ST_COMMUNICATING);
554 return;
555
556 case MSC_A_EV_MO_CLOSE:
557 case MSC_A_EV_CN_CLOSE:
558 case MSC_A_EV_UNUSED:
559 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
560 return;
561
562 default:
563 OSMO_ASSERT(false);
564 }
565}
566
567/* The MGW has given us a local IP address for the RAN side. Ready to start the Assignment of a voice channel. */
568static void msc_a_call_leg_ran_local_addr_available(struct msc_a *msc_a)
569{
570 struct ran_msg msg;
571 struct gsm_trans *cc_trans = msc_a->cc.active_trans;
572 struct gsm0808_channel_type channel_type;
573
Neels Hofmeyr00a476b2019-11-28 02:46:05 +0100574 if (!cc_trans) {
575 LOG_MSC_A(msc_a, LOGL_ERROR, "No CC transaction active\n");
576 call_leg_release(msc_a->cc.call_leg);
577 return;
578 }
579
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100580 /* Once a CI is known, we could also CRCX the CN side of the MGW endpoint, but it makes sense to wait for the
581 * codec to be determined by the Assignment Complete message, first. */
582
583 if (mncc_bearer_cap_to_channel_type(&channel_type, &cc_trans->bearer_cap)) {
584 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot compose Channel Type from bearer capabilities\n");
Neels Hofmeyrf439ff12019-10-05 04:19:36 +0200585 trans_free(cc_trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100586 return;
587 }
588
589 /* The RAN side RTP address is known, so the voice Assignment can commence. */
590 msg = (struct ran_msg){
591 .msg_type = RAN_MSG_ASSIGNMENT_COMMAND,
592 .assignment_command = {
593 .cn_rtp = &msc_a->cc.call_leg->rtp[RTP_TO_RAN]->local,
594 .channel_type = &channel_type,
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200595 .osmux_present = msc_a->cc.call_leg->rtp[RTP_TO_RAN]->use_osmux,
596 .osmux_cid = msc_a->cc.call_leg->rtp[RTP_TO_RAN]->local_osmux_cid,
Philipp Maierf34d9452020-06-05 15:49:35 +0200597 .call_id_present = true,
598 .call_id = cc_trans->callref,
Keith Whytea1a70be2021-05-16 02:59:52 +0200599 .lcls = cc_trans->cc.lcls,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100600 },
601 };
602 if (msc_a_ran_down(msc_a, MSC_ROLE_I, &msg)) {
603 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot send Assignment\n");
Neels Hofmeyrf439ff12019-10-05 04:19:36 +0200604 trans_free(cc_trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100605 return;
606 }
607}
608
609static void msc_a_call_leg_cn_local_addr_available(struct msc_a *msc_a, struct gsm_trans *cc_trans)
610{
611 if (gsm48_tch_rtp_create(cc_trans)) {
612 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot inform MNCC of RTP address\n");
Neels Hofmeyrf439ff12019-10-05 04:19:36 +0200613 trans_free(cc_trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100614 return;
615 }
616}
617
618static struct gsm_trans *find_waiting_call(struct msc_a *msc_a)
619{
620 struct gsm_trans *trans;
621 struct gsm_network *net = msc_a_net(msc_a);
622
623 llist_for_each_entry(trans, &net->trans_list, entry) {
624 if (trans->msc_a != msc_a)
625 continue;
626 if (trans->type != TRANS_CC)
627 continue;
628 if (trans->msc_a->cc.active_trans == trans)
629 continue;
630 return trans;
631 }
632 return NULL;
633}
634
635static void msc_a_cleanup_rtp_streams(struct msc_a *msc_a, uint32_t event, void *data)
636{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100637 switch (event) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100638
639 case MSC_EV_CALL_LEG_TERM:
640 msc_a->cc.call_leg = NULL;
641 if (msc_a->cc.mncc_forwarding_to_remote_ran)
642 msc_a->cc.mncc_forwarding_to_remote_ran->rtps = NULL;
643
Neels Hofmeyr265a4c72019-05-09 16:20:51 +0200644 if (msc_a->ho.new_cell.mncc_forwarding_to_remote_ran)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100645 msc_a->ho.new_cell.mncc_forwarding_to_remote_ran->rtps = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100646 return;
647
648 case MSC_MNCC_EV_CALL_ENDED:
649 msc_a->cc.mncc_forwarding_to_remote_ran = NULL;
650 return;
651
652 default:
653 return;
654 }
655}
656
657static void msc_a_fsm_communicating(struct osmo_fsm_inst *fi, uint32_t event, void *data)
658{
659 struct msc_a *msc_a = fi->priv;
660 struct rtp_stream *rtps;
661 struct gsm_trans *waiting_trans;
662 struct an_apdu *an_apdu;
663
664 msc_a_cleanup_rtp_streams(msc_a, event, data);
665
666 switch (event) {
667 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
668 case MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST:
669 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
670 an_apdu = data;
671 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_I);
672 return;
673
674 case MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE:
675 case MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE:
676 case MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST:
677 case MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST:
678 an_apdu = data;
679 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_T);
680 return;
681
682 case MSC_A_EV_TRANSACTION_ACCEPTED:
683 /* no-op */
684 return;
685
686 case MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE:
687 rtps = data;
688 if (!rtps) {
689 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid data for %s\n", osmo_fsm_event_name(fi->fsm, event));
690 return;
691 }
Neels Hofmeyr00a476b2019-11-28 02:46:05 +0100692 if (!msc_a->cc.call_leg) {
693 LOG_MSC_A(msc_a, LOGL_ERROR, "No call leg active\n");
694 return;
695 }
Neels Hofmeyrcc918cb2019-11-28 02:16:34 +0100696 if (!osmo_sockaddr_str_is_nonzero(&rtps->local)) {
697 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid RTP address received from MGW: " OSMO_SOCKADDR_STR_FMT "\n",
698 OSMO_SOCKADDR_STR_FMT_ARGS(&rtps->local));
699 call_leg_release(msc_a->cc.call_leg);
700 return;
701 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100702 LOG_MSC_A(msc_a, LOGL_DEBUG,
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200703 "MGW endpoint's RTP address available for the CI %s: " OSMO_SOCKADDR_STR_FMT " (osmux=%s:%d)\n",
704 rtp_direction_name(rtps->dir), OSMO_SOCKADDR_STR_FMT_ARGS(&rtps->local),
705 rtps->use_osmux ? "yes" : "no", rtps->local_osmux_cid);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100706 switch (rtps->dir) {
707 case RTP_TO_RAN:
708 msc_a_call_leg_ran_local_addr_available(msc_a);
709 return;
710 case RTP_TO_CN:
Neels Hofmeyr8dd16462022-01-13 20:06:53 +0100711 /* The rtp_stream has gotten the new RTP address and port from the MGW. Also update the codecs
712 * filter result to convey this RTP address and port towards the remote call leg. */
713 codec_filter_set_local_rtp(&msc_a->cc.active_trans->cc.codecs, &rtps->local);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100714 msc_a_call_leg_cn_local_addr_available(msc_a, rtps->for_trans);
715 return;
716 default:
717 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid data for %s\n", osmo_fsm_event_name(fi->fsm, event));
718 return;
719 }
720
721 case MSC_EV_CALL_LEG_RTP_COMPLETE:
722 /* Nothing to do. */
723 return;
724
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100725 case MSC_MNCC_EV_CALL_ENDED:
726 /* Cleaned up above */
727 return;
728
729 case MSC_EV_CALL_LEG_TERM:
730 /* RTP streams cleaned up above */
731
732 msc_a_get(msc_a, __func__);
733 if (msc_a->cc.active_trans)
734 trans_free(msc_a->cc.active_trans);
735
736 /* If there is another call still waiting to be activated, this is the time when the mgcp_ctx is
737 * available again and the other call can start assigning. */
738 waiting_trans = find_waiting_call(msc_a);
739 if (waiting_trans) {
740 LOG_MSC_A(msc_a, LOGL_DEBUG, "(ti %02x) Call waiting: starting Assignment\n",
741 waiting_trans->transaction_id);
742 msc_a_try_call_assignment(waiting_trans);
743 }
744 msc_a_put(msc_a, __func__);
745 return;
746
747 case MSC_A_EV_HANDOVER_REQUIRED:
748 msc_ho_start(msc_a, (struct ran_handover_required*)data);
749 return;
750
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200751 case MSC_A_EV_HANDOVER_END:
752 /* Termination event of the msc_ho_fsm. No action needed, it's all done in the msc_ho_fsm cleanup. This
753 * event only exists because osmo_fsm_inst_alloc_child() requires a parent term event; and maybe
754 * interesting for logging. */
755 return;
756
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100757 case MSC_A_EV_MO_CLOSE:
758 case MSC_A_EV_CN_CLOSE:
759 case MSC_A_EV_UNUSED:
760 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
761 return;
762
763 default:
764 OSMO_ASSERT(false);
765 }
766}
767
768static int msc_a_fsm_timer_cb(struct osmo_fsm_inst *fi)
769{
770 struct msc_a *msc_a = fi->priv;
771 if (msc_a_in_release(msc_a)) {
772 LOG_MSC_A(msc_a, LOGL_ERROR, "Timeout while releasing, discarding right now\n");
773 msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
774 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
775 } else {
776 enum gsm48_reject_value cause = GSM48_REJECT_CONGESTION;
777 osmo_fsm_inst_dispatch(fi, MSC_A_EV_CN_CLOSE, &cause);
778 }
779 return 0;
780}
781
782static void msc_a_fsm_releasing_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
783{
784 struct msc_a *msc_a = fi->priv;
785 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
786 int i;
787 char buf[128];
788 const char * const use_counts_to_cancel[] = {
789 MSC_A_USE_LOCATION_UPDATING,
790 MSC_A_USE_CM_SERVICE_CC,
791 MSC_A_USE_CM_SERVICE_SMS,
792 MSC_A_USE_CM_SERVICE_SS,
793 MSC_A_USE_PAGING_RESPONSE,
794 };
795
796 LOG_MSC_A(msc_a, LOGL_DEBUG, "Releasing: msc_a use is %s\n",
797 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
798
799 if (vsub) {
800 vlr_subscr_get(vsub, __func__);
801
802 /* Cancel all VLR FSMs, if any */
803 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
804
805 /* The subscriber has no active connection anymore.
806 * Restart the periodic Location Update expiry timer for this subscriber. */
807 vlr_subscr_enable_expire_lu(vsub);
808 }
809
810 /* If we're closing in a middle of a trans, we need to clean up */
811 trans_conn_closed(msc_a);
812
813 call_leg_release(msc_a->cc.call_leg);
814
815 /* Cancel use counts for pending CM Service / Paging */
816 for (i = 0; i < ARRAY_SIZE(use_counts_to_cancel); i++) {
817 const char *use = use_counts_to_cancel[i];
818 int32_t count = osmo_use_count_by(&msc_a->use_count, use);
819 if (!count)
820 continue;
821 LOG_MSC_A(msc_a, LOGL_DEBUG, "Releasing: canceling still pending use: %s (%d)\n", use, count);
822 osmo_use_count_get_put(&msc_a->use_count, use, -count);
823 }
824
825 if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) {
826 sgs_iface_tx_release(vsub);
827 /* In SGsAP there is no confirmation of a release. */
828 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
829 } else {
830 struct ran_msg msg = {
831 .msg_type = RAN_MSG_CLEAR_COMMAND,
832 .clear_command = {
Neels Hofmeyrd9fe7112020-07-11 00:20:20 +0200833 /* "Call Control" is the only cause code listed in 3GPP TS 48.008 3.2.1.21 CLEAR COMMAND
834 * that qualifies for a normal release situation. (OS#4664) */
835 .gsm0808_cause = GSM0808_CAUSE_CALL_CONTROL,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100836 .csfb_ind = (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED),
837 },
838 };
839 msc_a_get(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
840 msc_a_ran_down(msc_a, MSC_ROLE_I, &msg);
Philipp Maier47cf84d2019-08-15 14:56:54 +0200841
842 /* The connection is cleared. The MS will now go back to 4G,
843 Switch the RAN type back to SGS. */
844 if (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED)
845 vsub->cs.attached_via_ran = OSMO_RAT_EUTRAN_SGS;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100846 }
847
848 if (vsub)
849 vlr_subscr_put(vsub, __func__);
850}
851
852static void msc_a_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
853{
854 struct msc_a *msc_a = fi->priv;
855
856 msc_a_cleanup_rtp_streams(msc_a, event, data);
857
858 switch (event) {
859 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
860 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
861 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
862 return;
863
864 case MSC_A_EV_MO_CLOSE:
865 case MSC_A_EV_CN_CLOSE:
866 case MSC_A_EV_UNUSED:
867 /* Already releasing */
868 return;
869
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100870 case MSC_EV_CALL_LEG_TERM:
871 case MSC_MNCC_EV_CALL_ENDED:
872 /* RTP streams cleaned up above */
873 return;
874
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200875 case MSC_A_EV_HANDOVER_END:
876 /* msc_ho_fsm does cleanup. */
877 return;
878
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100879 default:
880 OSMO_ASSERT(false);
881 }
882}
883
884
885static void msc_a_fsm_released_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
886{
887 struct msc_a *msc_a = msc_a_fi_priv(fi);
888 char buf[128];
889 LOG_MSC_A(msc_a, LOGL_DEBUG, "Released: msc_a use is %s\n",
890 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
891 if (osmo_use_count_total(&msc_a->use_count) == 0)
892 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi);
893}
894
895static void msc_a_fsm_released(struct osmo_fsm_inst *fi, uint32_t event, void *data)
896{
897 if (event == MSC_A_EV_UNUSED)
898 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi);
899}
900
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100901void msc_a_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
902{
903 struct msc_a *msc_a = msc_a_fi_priv(fi);
904
905 trans_conn_closed(msc_a);
906
907 if (msc_a_fsm_has_active_transactions(fi))
908 LOG_MSC_A(msc_a, LOGL_ERROR, "Deallocating active transactions failed\n");
909
910 LOG_MSC_A_CAT(msc_a, DREF, LOGL_DEBUG, "max total use count was %d\n", msc_a->max_total_use_count);
911}
912
913const struct value_string msc_a_fsm_event_names[] = {
914 OSMO_VALUE_STRING(MSC_REMOTE_EV_RX_GSUP),
915 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE),
916 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_COMPLETE),
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100917 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_TERM),
918 OSMO_VALUE_STRING(MSC_MNCC_EV_NEED_LOCAL_RTP),
919 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_PROCEEDING),
920 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_COMPLETE),
921 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_ENDED),
922 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_COMPLETE_LAYER_3),
923 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST),
924 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST),
925 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST),
926 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST),
927 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE),
928 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE),
929 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST),
930 OSMO_VALUE_STRING(MSC_A_EV_COMPLETE_LAYER_3_OK),
931 OSMO_VALUE_STRING(MSC_A_EV_CLASSMARK_UPDATE),
932 OSMO_VALUE_STRING(MSC_A_EV_AUTHENTICATED),
933 OSMO_VALUE_STRING(MSC_A_EV_TRANSACTION_ACCEPTED),
934 OSMO_VALUE_STRING(MSC_A_EV_CN_CLOSE),
935 OSMO_VALUE_STRING(MSC_A_EV_MO_CLOSE),
936 OSMO_VALUE_STRING(MSC_A_EV_UNUSED),
937 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_REQUIRED),
938 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_END),
939 {}
940};
941
942#define S(x) (1 << (x))
943
944static const struct osmo_fsm_state msc_a_fsm_states[] = {
945 [MSC_A_ST_VALIDATE_L3] = {
946 .name = OSMO_STRINGIFY(MSC_A_ST_VALIDATE_L3),
947 .in_event_mask = 0
948 | S(MSC_A_EV_FROM_I_COMPLETE_LAYER_3)
949 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
950 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
951 | S(MSC_A_EV_COMPLETE_LAYER_3_OK)
952 | S(MSC_A_EV_MO_CLOSE)
953 | S(MSC_A_EV_CN_CLOSE)
954 | S(MSC_A_EV_UNUSED)
955 ,
956 .out_state_mask = 0
957 | S(MSC_A_ST_VALIDATE_L3)
958 | S(MSC_A_ST_AUTH_CIPH)
959 | S(MSC_A_ST_RELEASING)
960 ,
961 .action = msc_a_fsm_validate_l3,
962 },
963 [MSC_A_ST_AUTH_CIPH] = {
964 .name = OSMO_STRINGIFY(MSC_A_ST_AUTH_CIPH),
965 .in_event_mask = 0
966 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
967 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
968 | S(MSC_A_EV_AUTHENTICATED)
969 | S(MSC_A_EV_MO_CLOSE)
970 | S(MSC_A_EV_CN_CLOSE)
971 | S(MSC_A_EV_UNUSED)
972 ,
973 .out_state_mask = 0
974 | S(MSC_A_ST_WAIT_CLASSMARK_UPDATE)
975 | S(MSC_A_ST_AUTHENTICATED)
976 | S(MSC_A_ST_RELEASING)
977 ,
978 .action = msc_a_fsm_auth_ciph,
979 },
980 [MSC_A_ST_WAIT_CLASSMARK_UPDATE] = {
981 .name = OSMO_STRINGIFY(MSC_A_ST_WAIT_CLASSMARK_UPDATE),
982 .in_event_mask = 0
983 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
984 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
985 | S(MSC_A_EV_CLASSMARK_UPDATE)
986 | S(MSC_A_EV_MO_CLOSE)
987 | S(MSC_A_EV_CN_CLOSE)
988 ,
989 .out_state_mask = 0
990 | S(MSC_A_ST_AUTH_CIPH)
991 | S(MSC_A_ST_RELEASING)
992 ,
993 .action = msc_a_fsm_wait_classmark_update,
994 },
995 [MSC_A_ST_AUTHENTICATED] = {
996 .name = OSMO_STRINGIFY(MSC_A_ST_AUTHENTICATED),
997 /* allow everything to release for any odd behavior */
998 .in_event_mask = 0
999 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1000 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
1001 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1002 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
1003 | S(MSC_A_EV_MO_CLOSE)
1004 | S(MSC_A_EV_CN_CLOSE)
1005 | S(MSC_A_EV_UNUSED)
1006 ,
1007 .out_state_mask = 0
1008 | S(MSC_A_ST_RELEASING)
1009 | S(MSC_A_ST_COMMUNICATING)
1010 ,
1011 .onenter = msc_a_fsm_authenticated_enter,
1012 .action = msc_a_fsm_authenticated,
1013 },
1014 [MSC_A_ST_COMMUNICATING] = {
1015 .name = OSMO_STRINGIFY(MSC_A_ST_COMMUNICATING),
1016 /* allow everything to release for any odd behavior */
1017 .in_event_mask = 0
1018 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1019 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
1020 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1021 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE)
1022 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE)
1023 | S(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST)
1024 | S(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST)
1025 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
1026 | S(MSC_A_EV_MO_CLOSE)
1027 | S(MSC_A_EV_CN_CLOSE)
1028 | S(MSC_A_EV_UNUSED)
1029 | S(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE)
1030 | S(MSC_EV_CALL_LEG_RTP_COMPLETE)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001031 | S(MSC_EV_CALL_LEG_TERM)
1032 | S(MSC_MNCC_EV_CALL_ENDED)
1033 | S(MSC_A_EV_HANDOVER_REQUIRED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +02001034 | S(MSC_A_EV_HANDOVER_END)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001035 ,
1036 .out_state_mask = 0
1037 | S(MSC_A_ST_RELEASING)
1038 ,
1039 .action = msc_a_fsm_communicating,
1040 },
1041 [MSC_A_ST_RELEASING] = {
1042 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASING),
1043 .in_event_mask = 0
1044 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1045 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1046 | S(MSC_A_EV_UNUSED)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001047 | S(MSC_EV_CALL_LEG_TERM)
1048 | S(MSC_MNCC_EV_CALL_ENDED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +02001049 | S(MSC_A_EV_HANDOVER_END)
Pau Espin Pedrole53ecde2021-07-12 13:37:24 +02001050 | S(MSC_A_EV_CN_CLOSE)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001051 ,
1052 .out_state_mask = 0
1053 | S(MSC_A_ST_RELEASED)
1054 ,
1055 .onenter = msc_a_fsm_releasing_onenter,
1056 .action = msc_a_fsm_releasing,
1057 },
1058 [MSC_A_ST_RELEASED] = {
1059 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASED),
1060 .in_event_mask = 0
1061 | S(MSC_A_EV_UNUSED)
1062 ,
1063 .onenter = msc_a_fsm_released_onenter,
1064 .action = msc_a_fsm_released,
1065 },
1066};
1067
1068static struct osmo_fsm msc_a_fsm = {
1069 .name = "msc_a",
1070 .states = msc_a_fsm_states,
1071 .num_states = ARRAY_SIZE(msc_a_fsm_states),
1072 .log_subsys = DMSC,
1073 .event_names = msc_a_fsm_event_names,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001074 .timer_cb = msc_a_fsm_timer_cb,
1075 .cleanup = msc_a_fsm_cleanup,
1076};
1077
1078static __attribute__((constructor)) void msc_a_fsm_init()
1079{
1080 OSMO_ASSERT(osmo_fsm_register(&msc_a_fsm) == 0);
1081}
1082
1083static int msc_a_use_cb(struct osmo_use_count_entry *e, int32_t old_use_count, const char *file, int line)
1084{
1085 struct msc_a *msc_a = e->use_count->talloc_object;
1086 char buf[128];
1087 int32_t total;
1088 int level;
1089
1090 if (!e->use)
1091 return -EINVAL;
1092
1093 total = osmo_use_count_total(&msc_a->use_count);
1094
1095 if (total == 0
1096 || (total == 1 && old_use_count == 0 && e->count == 1))
1097 level = LOGL_INFO;
1098 else
1099 level = LOGL_DEBUG;
1100
1101 LOG_MSC_A_CAT_SRC(msc_a, DREF, level, file, line, "%s %s: now used by %s\n",
1102 (e->count - old_use_count) > 0? "+" : "-", e->use,
1103 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
1104
1105 if (e->count < 0)
1106 return -ERANGE;
1107
1108 msc_a->max_total_use_count = OSMO_MAX(msc_a->max_total_use_count, total);
1109
1110 if (total == 0)
1111 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_UNUSED, NULL);
1112 return 0;
1113}
1114
1115struct msc_a *msc_a_alloc(struct msub *msub, struct ran_infra *ran)
1116{
1117 struct msc_a *msc_a = msub_role_alloc(msub, MSC_ROLE_A, &msc_a_fsm, struct msc_a, ran);
1118 msc_a->use_count = (struct osmo_use_count){
1119 .talloc_object = msc_a,
1120 .use_cb = msc_a_use_cb,
1121 };
1122 osmo_use_count_make_static_entries(&msc_a->use_count, msc_a->use_count_buf, ARRAY_SIZE(msc_a->use_count_buf));
1123 /* Start timeout for first state */
Neels Hofmeyr01653252019-09-03 02:06:22 +02001124 msc_a_state_chg_always(msc_a, MSC_A_ST_VALIDATE_L3);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001125 return msc_a;
1126}
1127
1128bool msc_a_is_establishing_auth_ciph(const struct msc_a *msc_a)
1129{
1130 if (!msc_a || !msc_a->c.fi)
1131 return false;
1132 return msc_a->c.fi->state == MSC_A_ST_AUTH_CIPH;
1133}
1134
1135const struct value_string complete_layer3_type_names[] = {
1136 { COMPLETE_LAYER3_NONE, "NONE" },
1137 { COMPLETE_LAYER3_LU, "LU" },
1138 { COMPLETE_LAYER3_CM_SERVICE_REQ, "CM_SERVICE_REQ" },
1139 { COMPLETE_LAYER3_PAGING_RESP, "PAGING_RESP" },
Neels Hofmeyrae98b972021-07-27 03:46:49 +02001140 { COMPLETE_LAYER3_CM_RE_ESTABLISH_REQ, "CM_RE_ESTABLISH_REQ" },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001141 { 0, NULL }
1142};
1143
1144#define _msc_a_update_id(MSC_A, FMT, ARGS ...) \
1145 do { \
1146 if (osmo_fsm_inst_update_id_f(msc_a->c.fi, FMT ":%s:%s", \
1147 ## ARGS, \
1148 msub_ran_conn_name(msc_a->c.msub), \
1149 complete_layer3_type_name(msc_a->complete_layer3_type)) \
1150 == 0) { \
1151 struct vlr_subscr *_vsub = msc_a_vsub(MSC_A); \
1152 if (_vsub) { \
1153 if (_vsub->lu_fsm) \
1154 osmo_fsm_inst_update_id(_vsub->lu_fsm, (MSC_A)->c.fi->id); \
1155 if (_vsub->auth_fsm) \
1156 osmo_fsm_inst_update_id(_vsub->auth_fsm, (MSC_A)->c.fi->id); \
1157 if (_vsub->proc_arq_fsm) \
1158 osmo_fsm_inst_update_id(_vsub->proc_arq_fsm, (MSC_A)->c.fi->id); \
1159 } \
1160 LOG_MSC_A(MSC_A, LOGL_DEBUG, "Updated ID\n"); \
1161 } \
1162 /* otherwise osmo_fsm_inst_update_id_f() will log an error. */ \
1163 } while (0)
1164
1165
1166/* 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 +02001167void msc_a_update_id_from_mi(struct msc_a *msc_a, const struct osmo_mobile_identity *mi)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001168{
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001169 _msc_a_update_id(msc_a, "%s", osmo_mobile_identity_to_str_c(OTC_SELECT, mi));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001170}
1171
1172/* Update msc_a->fi id string from current msc_a->vsub and msc_a->complete_layer3_type. */
1173void msc_a_update_id(struct msc_a *msc_a)
1174{
1175 _msc_a_update_id(msc_a, "%s", vlr_subscr_name(msc_a_vsub(msc_a)));
1176}
1177
1178/* Iterate all msc_a instances that are relevant for this subscriber, and update FSM ID strings for all of the FSM
1179 * instances. */
1180void msc_a_update_id_for_vsub(struct vlr_subscr *for_vsub)
1181{
1182 struct msub *msub;
1183 llist_for_each_entry(msub, &msub_list, entry) {
1184 struct vlr_subscr *vsub = msub_vsub(msub);
1185 if (vsub != for_vsub)
1186 continue;
1187 msc_a_update_id(msub_msc_a(msub));
1188 }
1189}
1190
1191static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
1192{
1193 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
1194 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
1195
1196 switch (pdisc) {
1197 case GSM48_PDISC_MM:
1198 switch (msg_type) {
1199 case GSM48_MT_MM_LOC_UPD_REQUEST:
1200 case GSM48_MT_MM_CM_SERV_REQ:
1201 case GSM48_MT_MM_CM_REEST_REQ:
1202 case GSM48_MT_MM_AUTH_RESP:
1203 case GSM48_MT_MM_AUTH_FAIL:
1204 case GSM48_MT_MM_ID_RESP:
1205 case GSM48_MT_MM_TMSI_REALL_COMPL:
1206 case GSM48_MT_MM_IMSI_DETACH_IND:
1207 return true;
1208 default:
1209 break;
1210 }
1211 break;
1212 case GSM48_PDISC_RR:
1213 switch (msg_type) {
1214 /* GSM48_MT_RR_CIPH_M_COMPL is actually handled in bssmap_rx_ciph_compl() and gets redirected in the
1215 * BSSAP layer to ran_conn_cipher_mode_compl() (before this here is reached) */
1216 case GSM48_MT_RR_PAG_RESP:
1217 case GSM48_MT_RR_CIPH_M_COMPL:
1218 return true;
1219 default:
1220 break;
1221 }
1222 break;
1223 default:
1224 break;
1225 }
1226
1227 return false;
1228}
1229
1230/* Main entry point for GSM 04.08/44.008 Layer 3 data (e.g. from the BSC). */
1231int msc_a_up_l3(struct msc_a *msc_a, struct msgb *msg)
1232{
1233 struct gsm48_hdr *gh;
1234 uint8_t pdisc;
1235 int rc;
1236 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1237 int is_r99;
1238
1239 OSMO_ASSERT(msg->l3h);
1240 OSMO_ASSERT(msg);
1241
1242 gh = msgb_l3(msg);
1243 pdisc = gsm48_hdr_pdisc(gh);
1244
1245 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_DEBUG, "Dispatching 04.08 message: %s %s\n",
1246 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1247
1248 /* To evaluate the 3GPP TS 24.007 Duplicate Detection, we need Classmark information on whether the MS is R99
1249 * capable. If the subscriber is already actively connected, the Classmark information is stored with the
1250 * vlr_subscr. Otherwise, this *must* be a Complete Layer 3 with Classmark info. */
1251 if (vsub)
1252 is_r99 = osmo_gsm48_classmark_is_r99(&vsub->classmark) ? 1 : 0;
1253 else
1254 is_r99 = compl_l3_msg_is_r99(msg);
1255
1256 if (is_r99 < 0) {
1257 LOG_MSC_A(msc_a, LOGL_ERROR,
1258 "No Classmark Information, dropping non-Complete-Layer3 message: %s\n",
1259 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1260 return -EACCES;
1261 }
1262
1263 if (is_r99 >= 0
1264 && ran_dec_dtap_undup_is_duplicate(msc_a->c.fi, msc_a->n_sd_next, is_r99 ? true : false, msg)) {
1265 LOG_MSC_A(msc_a, LOGL_DEBUG, "Dropping duplicate message"
1266 " (3GPP TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection)\n");
1267 return 0;
1268 }
1269
1270 if (!msc_a_is_accepted(msc_a)
1271 && !msg_is_initially_permitted(gh)) {
1272 LOG_MSC_A(msc_a, LOGL_ERROR,
1273 "Message not permitted for initial conn: %s\n",
1274 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1275 return -EACCES;
1276 }
1277
1278 if (vsub && vsub->cs.attached_via_ran != msc_a->c.ran->type) {
1279 LOG_MSC_A(msc_a, LOGL_ERROR,
1280 "Illegal situation: RAN type mismatch:"
1281 " attached via %s, received message via %s\n",
1282 osmo_rat_type_name(vsub->cs.attached_via_ran),
1283 osmo_rat_type_name(msc_a->c.ran->type));
1284 return -EACCES;
1285 }
1286
1287#if 0
1288 if (silent_call_reroute(conn, msg))
1289 return silent_call_rx(conn, msg);
1290#endif
1291
1292 switch (pdisc) {
1293 case GSM48_PDISC_CC:
1294 rc = gsm0408_rcv_cc(msc_a, msg);
1295 break;
1296 case GSM48_PDISC_MM:
1297 rc = gsm0408_rcv_mm(msc_a, msg);
1298 break;
1299 case GSM48_PDISC_RR:
1300 rc = gsm0408_rcv_rr(msc_a, msg);
1301 break;
1302 case GSM48_PDISC_SMS:
1303 rc = gsm0411_rcv_sms(msc_a, msg);
1304 break;
1305 case GSM48_PDISC_MM_GPRS:
1306 case GSM48_PDISC_SM_GPRS:
1307 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unimplemented "
1308 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1309 rc = -ENOTSUP;
1310 break;
1311 case GSM48_PDISC_NC_SS:
1312 rc = gsm0911_rcv_nc_ss(msc_a, msg);
1313 break;
1314 case GSM48_PDISC_TEST:
1315 rc = gsm0414_rcv_test(msc_a, msg);
1316 break;
1317 default:
1318 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unknown "
1319 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1320 rc = -EINVAL;
1321 break;
1322 }
1323
1324 return rc;
1325}
1326
1327static void msc_a_up_call_assignment_complete(struct msc_a *msc_a, const struct ran_msg *ac)
1328{
1329 struct gsm_trans *cc_trans = msc_a->cc.active_trans;
1330 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 +01001331 const struct gsm0808_speech_codec *codec_if_known = ac->assignment_complete.codec_present ?
1332 &ac->assignment_complete.codec : NULL;
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001333 const struct codec_mapping *codec_cn = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001334
1335 if (!rtps_to_ran) {
1336 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but no RTP stream is set up\n");
1337 return;
1338 }
1339 if (!cc_trans) {
Neels Hofmeyr550506a2022-01-13 23:31:57 +01001340 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but no CC transaction is active\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001341 return;
1342 }
1343
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001344 if (rtps_to_ran->use_osmux != ac->assignment_complete.osmux_present) {
1345 LOG_MSC_A_CAT(msc_a, DCC, LOGL_ERROR, "Osmux usage ass request and complete don't match: %d vs %d\n",
1346 rtps_to_ran->use_osmux, ac->assignment_complete.osmux_present);
1347 call_leg_release(msc_a->cc.call_leg);
1348 return;
1349 }
1350
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001351 if (codec_if_known) {
Neels Hofmeyrcec51b32023-03-01 03:47:45 +01001352 if (ac->assignment_complete.codec_with_iuup) {
1353 /* FUTURE: soon, we want to tell the MGW to decapsulate IuUP to plain AMR-FR/RTP. So far,
1354 * continue to forward IuUP to the CN.
1355 *
1356 * ran_msg_iu now returns AMR-FR as the assigned codec. If we use that directly, that will
1357 * instruct the MGW to decapsulate IuUP into plain RTP. Let's keep this change to an explicit
1358 * patch, while various codecs patches are still being applied.
1359 *
1360 * So instruct MGW to forward IuUP to CN:
1361 */
1362 codec_cn = codec_mapping_by_subtype_name("VND.3GPP.IUFP");
1363 OSMO_ASSERT(codec_cn);
1364 } else {
1365 codec_cn = codec_mapping_by_gsm0808_speech_codec_type(codec_if_known->type);
1366 /* TODO: use codec_mapping_by_gsm0808_speech_codec() to also match on codec_if_known->cfg */
1367 if (!codec_cn) {
1368 LOG_TRANS(cc_trans, LOGL_ERROR, "Unknown codec in Assignment Complete: %s\n",
1369 gsm0808_speech_codec_type_name(codec_if_known->type));
1370 call_leg_release(msc_a->cc.call_leg);
1371 return;
1372 }
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001373 }
1374
Neels Hofmeyrcec51b32023-03-01 03:47:45 +01001375 /* Update RAN-side endpoint CI from Assignment result -- unless it is forced by the ran_infra, in which
1376 * case it remains unchanged as passed to the earlier call of call_leg_ensure_ci(). */
1377 if (msc_a->c.ran->force_mgw_codecs_to_ran.count == 0)
1378 rtp_stream_set_one_codec(rtps_to_ran, &codec_cn->sdp);
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001379
1380 /* Update codec filter with Assignment result, for the CN side */
1381 cc_trans->cc.codecs.assignment = codec_cn->sdp;
1382 } else {
1383 /* No codec passed in Assignment Complete, set 'codecs.assignment' to none. */
1384 cc_trans->cc.codecs.assignment = (struct sdp_audio_codec){};
1385 LOG_TRANS(cc_trans, LOGL_INFO, "Assignment Complete without voice codec\n");
1386 }
1387
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001388 rtp_stream_set_remote_addr(rtps_to_ran, &ac->assignment_complete.remote_rtp);
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001389 if (rtps_to_ran->use_osmux)
1390 rtp_stream_set_remote_osmux_cid(rtps_to_ran,
1391 ac->assignment_complete.osmux_cid);
1392
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001393 rtp_stream_commit(rtps_to_ran);
1394
Neels Hofmeyr2d57d6e2022-01-13 21:39:58 +01001395 /* Remember the Codec List (BSS Supported) */
1396 if (ac->assignment_complete.codec_list_bss_supported)
1397 codec_filter_set_bss(&cc_trans->cc.codecs, ac->assignment_complete.codec_list_bss_supported);
1398
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001399 /* Setup CN side endpoint CI:
1400 * Now that
1401 * - the first CI has been created and a definitive endpoint name is assigned to the call_leg's MGW
1402 * endpoint,
1403 * - the Assignment has chosen a speech codec
1404 * go on to create the CN side RTP stream's CI. */
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001405 codec_filter_run(&cc_trans->cc.codecs);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001406 if (call_leg_ensure_ci(msc_a->cc.call_leg, RTP_TO_CN, cc_trans->callref, cc_trans,
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001407 &cc_trans->cc.codecs.result.audio_codecs, NULL)) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001408 LOG_MSC_A_CAT(msc_a, DCC, LOGL_ERROR, "Error creating MGW CI towards CN\n");
1409 call_leg_release(msc_a->cc.call_leg);
1410 return;
1411 }
1412}
1413
1414static void msc_a_up_call_assignment_failure(struct msc_a *msc_a, const struct ran_msg *af)
1415{
1416 struct gsm_trans *trans;
1417
1418 /* For a normal voice call, there will be an rtp_stream FSM. */
1419 if (msc_a->cc.call_leg && msc_a->cc.call_leg->rtp[RTP_TO_RAN]) {
1420 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing call\n");
1421 rtp_stream_release(msc_a->cc.call_leg->rtp[RTP_TO_RAN]);
1422 return;
1423 }
1424
1425 /* Otherwise, a silent call might be active */
1426 trans = trans_find_by_type(msc_a, TRANS_SILENT_CALL);
1427 if (trans) {
1428 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing silent call\n");
1429 trans_free(trans);
1430 return;
1431 }
1432
1433 /* Neither a voice call nor silent call assignment. Assume the worst and detach. */
1434 msc_a_release_cn(msc_a);
1435}
1436
1437static void msc_a_up_classmark_update(struct msc_a *msc_a, const struct osmo_gsm48_classmark *classmark,
1438 struct osmo_gsm48_classmark *dst)
1439{
1440 if (!dst) {
1441 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1442
1443 if (!vsub)
1444 dst = &msc_a->temporary_classmark;
1445 else
1446 dst = &vsub->classmark;
1447 }
1448
Martin Hauke3f07dac2019-11-14 17:49:08 +01001449 LOG_MSC_A(msc_a, LOGL_DEBUG, "A5 capabilities received from Classmark Update: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001450 osmo_gsm48_classmark_a5_name(classmark));
1451 osmo_gsm48_classmark_update(dst, classmark);
1452
1453 /* bump subscr conn FSM in case it is waiting for a Classmark Update */
1454 if (msc_a->c.fi->state == MSC_A_ST_WAIT_CLASSMARK_UPDATE)
1455 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CLASSMARK_UPDATE, NULL);
1456}
1457
1458static void msc_a_up_sapi_n_reject(struct msc_a *msc_a, const struct ran_msg *msg)
1459{
1460 int sapi = msg->sapi_n_reject.dlci & 0x7;
1461 if (sapi == UM_SAPI_SMS)
1462 gsm411_sapi_n_reject(msc_a);
1463}
1464
1465static int msc_a_up_ho(struct msc_a *msc_a, const struct msc_a_ran_dec_data *d, uint32_t ho_fi_event)
1466{
1467 if (!msc_a->ho.fi) {
1468 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Handover message, but no Handover ongoing: %s\n", d->ran_dec->msg_name);
1469 return -EINVAL;
1470 }
1471 return osmo_fsm_inst_dispatch(msc_a->ho.fi, ho_fi_event, (void*)d);
1472}
1473
1474int msc_a_ran_dec_from_msc_i(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1475{
1476 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Alexander Couzens2aaff752021-10-19 17:09:11 +02001477 struct gsm_network *net = msc_a_net(msc_a);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001478 const struct ran_msg *msg = d->ran_dec;
1479 int rc = -99;
1480
1481 switch (msg->msg_type) {
1482
1483 case RAN_MSG_COMPL_L3:
Neels Hofmeyr68f50da2020-06-24 14:22:52 +02001484 /* In case the cell_id from Complete Layer 3 Information lacks a PLMN, write the configured PLMN code
1485 * into msc_a->via_cell. Then overwrite with those bits obtained from Complete Layer 3 Information. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001486 msc_a->via_cell = (struct osmo_cell_global_id){
1487 .lai.plmn = msc_a_net(msc_a)->plmn,
1488 };
1489 gsm0808_cell_id_to_cgi(&msc_a->via_cell, msg->compl_l3.cell_id);
Neels Hofmeyrede90832022-01-13 18:13:15 +01001490
Neels Hofmeyre276ae92022-01-13 21:38:35 +01001491 /* If a codec list was sent along in the RAN_MSG_COMPL_L3, remember it for any upcoming codec
1492 * resolution. */
1493 if (msg->compl_l3.codec_list_bss_supported) {
1494 msc_a->cc.compl_l3_codec_list_bss_supported = *msg->compl_l3.codec_list_bss_supported;
1495 if (log_check_level(msc_a->c.ran->log_subsys, LOGL_DEBUG)) {
1496 struct sdp_audio_codecs ac = {};
1497 sdp_audio_codecs_from_speech_codec_list(&ac, &msc_a->cc.compl_l3_codec_list_bss_supported);
1498 LOG_MSC_A(msc_a, LOGL_DEBUG, "Complete Layer 3: Codec List (BSS Supported): %s\n",
1499 sdp_audio_codecs_to_str(&ac));
1500 }
1501 }
1502
Neels Hofmeyrede90832022-01-13 18:13:15 +01001503 /* Submit the Complete Layer 3 Information DTAP */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001504 rc = msc_a_up_l3(msc_a, msg->compl_l3.msg);
1505 if (!rc) {
1506 struct ran_conn *conn = msub_ran_conn(msc_a->c.msub);
1507 if (conn)
1508 ran_peer_cells_seen_add(conn->ran_peer, msg->compl_l3.cell_id);
1509 }
1510 break;
1511
1512 case RAN_MSG_DTAP:
1513 rc = msc_a_up_l3(msc_a, msg->dtap);
1514 break;
1515
1516 case RAN_MSG_CLEAR_REQUEST:
1517 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
1518 break;
1519
1520 case RAN_MSG_CLEAR_COMPLETE:
1521 switch (msc_a->c.fi->state) {
1522 case MSC_A_ST_RELEASING:
1523 msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
1524 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
1525 break;
1526 case MSC_A_ST_RELEASED:
1527 break;
1528 default:
1529 LOG_MSC_A(msc_a, LOGL_ERROR, "Received Clear Complete event, but did not send Clear Command\n");
1530 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
1531 break;
1532 }
1533 rc = 0;
1534 break;
1535
1536 case RAN_MSG_CLASSMARK_UPDATE:
1537 msc_a_up_classmark_update(msc_a, msg->classmark_update.classmark, NULL);
1538 rc = 0;
1539 break;
1540
1541 case RAN_MSG_CIPHER_MODE_COMPLETE:
1542 /* Remember what Ciphering was negotiated (e.g. for Handover) */
1543 if (msg->cipher_mode_complete.alg_id) {
1544 msc_a->geran_encr.alg_id = msg->cipher_mode_complete.alg_id;
1545 LOG_MSC_A(msc_a, LOGL_DEBUG, "Cipher Mode Complete: chosen encryption algorithm: A5/%u\n",
1546 msc_a->geran_encr.alg_id - 1);
Alexander Couzens2aaff752021-10-19 17:09:11 +02001547 }
1548
1549 if (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU) {
1550 int16_t utran_encryption;
1551
1552 /* utran: ensure chosen ciphering mode is allowed
1553 * If the IE is missing (utran_encryption == -1), parse it as no encryption */
1554 utran_encryption = msg->cipher_mode_complete.utran_encryption;
1555 if (utran_encryption == -1)
1556 utran_encryption = 0;
1557 if ((net->uea_encryption_mask & (1 << utran_encryption)) == 0) {
1558 /* cipher disallowed */
1559 LOG_MSC_A(msc_a, LOGL_ERROR, "Cipher Mode Complete: RNC chosen forbidden ciphering UEA%d\n",
1560 msg->cipher_mode_complete.utran_encryption);
1561 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT);
1562 rc = 0;
1563 break;
1564 }
1565 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001566 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_COMPL);
1567 rc = 0;
Neels Hofmeyre9a39112019-08-29 00:10:49 +02001568
1569 /* Evaluate enclosed L3 message, typically Identity Response (IMEISV) */
1570 if (msg->cipher_mode_complete.l3_msg) {
1571 unsigned char *data = (unsigned char*)(msg->cipher_mode_complete.l3_msg->val);
1572 uint16_t len = msg->cipher_mode_complete.l3_msg->len;
1573 struct msgb *dtap = msgb_alloc(len, "DTAP from Cipher Mode Complete");
1574 unsigned char *pos = msgb_put(dtap, len);
1575 memcpy(pos, data, len);
1576 dtap->l3h = pos;
1577 rc = msc_a_up_l3(msc_a, dtap);
1578 msgb_free(dtap);
1579 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001580 break;
1581
1582 case RAN_MSG_CIPHER_MODE_REJECT:
1583 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT);
1584 rc = 0;
1585 break;
1586
1587 case RAN_MSG_ASSIGNMENT_COMPLETE:
1588 msc_a_up_call_assignment_complete(msc_a, msg);
1589 rc = 0;
1590 break;
1591
1592 case RAN_MSG_ASSIGNMENT_FAILURE:
1593 msc_a_up_call_assignment_failure(msc_a, msg);
1594 rc = 0;
1595 break;
1596
1597 case RAN_MSG_SAPI_N_REJECT:
1598 msc_a_up_sapi_n_reject(msc_a, msg);
1599 rc = 0;
1600 break;
1601
1602 case RAN_MSG_HANDOVER_PERFORMED:
1603 /* The BSS lets us know that a handover happened within the BSS, which doesn't concern us. */
1604 LOG_MSC_A(msc_a, LOGL_ERROR, "'Handover Performed' handling not implemented\n");
1605 break;
1606
1607 case RAN_MSG_HANDOVER_REQUIRED:
1608 /* The BSS lets us know that it wants to handover to a different cell */
1609 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_HANDOVER_REQUIRED, (void*)&msg->handover_required);
1610 break;
1611
1612 case RAN_MSG_HANDOVER_FAILURE:
1613 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1614 break;
1615
Keith Whytea1a70be2021-05-16 02:59:52 +02001616 case RAN_MSG_LCLS_STATUS:
1617 /* The BSS sends us LCLS_STATUS. We do nothing for now, but it is not an error. */
1618 LOG_MSC_A(msc_a, LOGL_DEBUG, "LCLS_STATUS (%s) received from MSC-I\n",
1619 gsm0808_lcls_status_name(msg->lcls_status.status));
1620 rc = 0;
1621 break;
1622
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001623 default:
1624 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-I not implemented: %s\n", ran_msg_type_name(msg->msg_type));
1625 rc = -ENOTSUP;
1626 break;
1627 }
1628 return rc;
1629}
1630
1631static int msc_a_ran_dec_from_msc_t(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1632{
1633 struct msc_t *msc_t = msc_a_msc_t(msc_a);
1634 int rc = -99;
1635
1636 if (!msc_t) {
1637 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx message from MSC-T role, but I have no active MSC-T role.\n");
1638 return -EINVAL;
1639 }
1640
1641 OSMO_ASSERT(d->ran_dec);
1642
1643 switch (d->ran_dec->msg_type) {
1644
1645 case RAN_MSG_CLEAR_REQUEST:
1646 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_MO_CLOSE, NULL);
1647 break;
1648
1649 case RAN_MSG_CLEAR_COMPLETE:
1650 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_CLEAR_COMPLETE, NULL);
1651 break;
1652
1653 case RAN_MSG_CLASSMARK_UPDATE:
1654 msc_a_up_classmark_update(msc_a, d->ran_dec->classmark_update.classmark, &msc_t->classmark);
1655 rc = 0;
1656 break;
1657
1658 case RAN_MSG_HANDOVER_REQUEST_ACK:
1659 /* new BSS accepts Handover */
1660 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_REQUEST_ACK);
1661 break;
1662
1663 case RAN_MSG_HANDOVER_DETECT:
1664 /* new BSS signals the MS is DETECTed on the new lchan */
1665 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_DETECT);
1666 break;
1667
1668 case RAN_MSG_HANDOVER_COMPLETE:
1669 /* new BSS signals the MS has fully moved to the new lchan */
1670 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_COMPLETE);
1671 break;
1672
1673 case RAN_MSG_HANDOVER_FAILURE:
1674 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1675 break;
1676
1677 default:
1678 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-T not implemented: %s\n",
1679 ran_msg_type_name(d->ran_dec->msg_type));
1680 rc = -ENOTSUP;
1681 break;
1682 }
1683 return rc;
1684}
1685
1686int msc_a_ran_decode_cb(struct osmo_fsm_inst *msc_a_fi, void *data, const struct ran_msg *msg)
1687{
1688 struct msc_a *msc_a = msc_a_fi_priv(msc_a_fi);
1689 struct msc_a_ran_dec_data *d = data;
1690 int rc = -99;
1691
1692 d->ran_dec = msg;
1693
1694 switch (d->from_role) {
1695 case MSC_ROLE_I:
1696 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode: %s\n", msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1697 rc = msc_a_ran_dec_from_msc_i(msc_a, d);
1698 break;
1699
1700 case MSC_ROLE_T:
1701 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode from MSC-T: %s\n",
1702 msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1703 rc = msc_a_ran_dec_from_msc_t(msc_a, d);
1704 break;
1705
1706 default:
1707 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from invalid role %s: %s\n", msc_role_name(d->from_role),
1708 ran_msg_type_name(msg->msg_type));
1709 return -ENOTSUP;
1710 }
1711
1712 if (rc)
1713 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),
1714 msc_role_name(d->from_role));
1715 return rc;
1716}
1717
1718/* Your typical DTAP via FORWARD_ACCESS_SIGNALLING_REQUEST */
1719int _msc_a_ran_down(struct msc_a *msc_a, enum msc_role to_role, const struct ran_msg *ran_msg,
1720 const char *file, int line)
1721{
1722 return _msc_a_msg_down(msc_a, to_role, msub_role_to_role_event(msc_a->c.msub, MSC_ROLE_A, to_role),
1723 ran_msg, file, line);
1724}
1725
1726/* To transmit more complex events than just FORWARD_ACCESS_SIGNALLING_REQUEST, e.g. an
1727 * MSC_T_EV_FROM_A_PREPARE_HANDOVER_REQUEST */
1728int _msc_a_msg_down(struct msc_a *msc_a, enum msc_role to_role, uint32_t to_role_event,
1729 const struct ran_msg *ran_msg,
1730 const char *file, int line)
1731{
1732 struct an_apdu an_apdu = {
1733 .an_proto = msc_a->c.ran->an_proto,
1734 .msg = msc_role_ran_encode(msc_a->c.fi, ran_msg),
1735 };
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001736 if (!an_apdu.msg)
1737 return -EIO;
Vadim Yanitskiyc44342b2021-12-07 18:32:35 +03001738 return _msub_role_dispatch(msc_a->c.msub, to_role, to_role_event, &an_apdu, file, line);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001739}
1740
1741int msc_a_tx_dtap_to_i(struct msc_a *msc_a, struct msgb *dtap)
1742{
1743 struct ran_msg ran_msg;
Neels Hofmeyrc192c0b2019-10-07 21:41:18 +02001744 struct gsm48_hdr *gh = msgb_l3(dtap) ? : dtap->data;
1745 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001746
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001747 if (!msc_a) {
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001748 LOGP(DMSC, LOGL_ERROR, "Attempt to send DTAP to NULL MSC-A, dropping message: %s %s\n",
1749 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1750 msgb_free(dtap);
1751 return -EIO;
1752 }
1753
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001754 if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) {
1755 /* The SGs connection to the MME always is at the MSC-A. */
1756 return sgs_iface_tx_dtap_ud(msc_a, dtap);
1757 }
1758
Neels Hofmeyrc192c0b2019-10-07 21:41:18 +02001759 LOG_MSC_A(msc_a, LOGL_DEBUG, "Sending DTAP: %s %s\n",
1760 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1761
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001762 ran_msg = (struct ran_msg){
1763 .msg_type = RAN_MSG_DTAP,
1764 .dtap = dtap,
1765 };
1766 return msc_a_ran_down(msc_a, MSC_ROLE_I, &ran_msg);
1767}
1768
1769struct msc_a *msc_a_for_vsub(const struct vlr_subscr *vsub, bool valid_conn_only)
1770{
1771 struct msc_a *msc_a = msub_msc_a(msub_for_vsub(vsub));
1772 if (valid_conn_only && !msc_a_is_accepted(msc_a))
1773 return NULL;
1774 return msc_a;
1775}
1776
1777int msc_tx_common_id(struct msc_a *msc_a, enum msc_role to_role)
1778{
1779 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Vadim Yanitskiy435f67f2021-06-06 15:47:49 +02001780 if (vsub == NULL)
1781 return -ENODEV;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001782 struct ran_msg msg = {
1783 .msg_type = RAN_MSG_COMMON_ID,
1784 .common_id = {
1785 .imsi = vsub->imsi,
Pau Espin Pedrol67106702021-04-27 18:20:15 +02001786 .last_eutran_plmn_present = vsub->sgs.last_eutran_plmn_present,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001787 },
1788 };
Pau Espin Pedrol67106702021-04-27 18:20:15 +02001789 if (vsub->sgs.last_eutran_plmn_present) {
1790 memcpy(&msg.common_id.last_eutran_plmn, &vsub->sgs.last_eutran_plmn,
1791 sizeof(vsub->sgs.last_eutran_plmn));
1792 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001793
1794 return msc_a_ran_down(msc_a, to_role, &msg);
1795}
1796
1797static int msc_a_start_assignment(struct msc_a *msc_a, struct gsm_trans *cc_trans)
1798{
1799 struct call_leg *cl = msc_a->cc.call_leg;
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001800 struct msc_i *msc_i = msc_a_msc_i(msc_a);
1801 struct gsm_network *net = msc_a_net(msc_a);
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001802 struct sdp_audio_codecs *codecs;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001803
1804 OSMO_ASSERT(!msc_a->cc.active_trans);
1805 msc_a->cc.active_trans = cc_trans;
1806
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001807 cc_trans->cc.codecs.assignment = (struct sdp_audio_codec){};
1808
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001809 OSMO_ASSERT(cc_trans && cc_trans->type == TRANS_CC);
1810
1811 if (!cl) {
1812 cl = msc_a->cc.call_leg = call_leg_alloc(msc_a->c.fi,
1813 MSC_EV_CALL_LEG_TERM,
1814 MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE,
Neels Hofmeyr265a4c72019-05-09 16:20:51 +02001815 MSC_EV_CALL_LEG_RTP_COMPLETE);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001816 OSMO_ASSERT(cl);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001817 }
1818
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001819 if (net->use_osmux != OSMUX_USAGE_OFF) {
1820 msc_i = msc_a_msc_i(msc_a);
1821 if (msc_i->c.remote_to) {
1822 /* TODO: investigate what to do in this case */
1823 LOG_MSC_A(msc_a, LOGL_ERROR, "Osmux not yet supported for inter-MSC");
1824 } else {
1825 cl->ran_peer_supports_osmux = msc_i->ran_conn->ran_peer->remote_supports_osmux;
1826 }
1827 }
1828
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001829 /* Make sure an MGW endpoint towards RAN is present. If it is already set up, "skip" to
1830 * MSC_EV_CALL_LEG_LOCAL_ADDR_AVAILABLE immediately. If not, set it up. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001831 if (call_leg_local_ip(cl, RTP_TO_RAN))
1832 return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_RAN]);
Pau Espin Pedrol3a02d292022-01-05 13:56:16 +01001833
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001834 codec_filter_run(&cc_trans->cc.codecs);
1835 if (msc_a->c.ran->force_mgw_codecs_to_ran.count)
1836 codecs = &msc_a->c.ran->force_mgw_codecs_to_ran;
1837 else
1838 codecs = &cc_trans->cc.codecs.result.audio_codecs;
1839 return call_leg_ensure_ci(msc_a->cc.call_leg, RTP_TO_RAN, cc_trans->callref, cc_trans, codecs, NULL);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001840}
1841
1842int msc_a_try_call_assignment(struct gsm_trans *cc_trans)
1843{
1844 struct msc_a *msc_a = cc_trans->msc_a;
1845 OSMO_ASSERT(cc_trans->type == TRANS_CC);
1846
1847 if (msc_a->cc.active_trans == cc_trans) {
Neels Hofmeyrb4ef5e72019-08-30 01:11:12 +02001848 LOG_MSC_A(msc_a, LOGL_DEBUG, "Assignment for this trans already started earlier\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001849 return 0;
1850 }
1851
1852 if (msc_a->cc.active_trans) {
1853 LOG_MSC_A(msc_a, LOGL_INFO, "Another call is already ongoing, not assigning yet\n");
1854 return 0;
1855 }
1856
1857 LOG_MSC_A(msc_a, LOGL_DEBUG, "Starting call assignment\n");
1858 return msc_a_start_assignment(msc_a, cc_trans);
1859}
1860
Neels Hofmeyr5d53c602022-04-24 23:37:07 +02001861/* Map CM Service type to use token.
1862 * Given a CM Service type, return a matching token intended for osmo_use_count.
1863 * For unknown service type, return NULL.
1864 */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001865const char *msc_a_cm_service_type_to_use(enum osmo_cm_service_type cm_service_type)
1866{
1867 switch (cm_service_type) {
1868 case GSM48_CMSERV_MO_CALL_PACKET:
1869 case GSM48_CMSERV_EMERGENCY:
1870 return MSC_A_USE_CM_SERVICE_CC;
1871
1872 case GSM48_CMSERV_SMS:
1873 return MSC_A_USE_CM_SERVICE_SMS;
1874
1875 case GSM48_CMSERV_SUP_SERV:
1876 return MSC_A_USE_CM_SERVICE_SS;
1877
1878 default:
1879 return NULL;
1880 }
1881}
1882
1883void msc_a_release_cn(struct msc_a *msc_a)
1884{
1885 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CN_CLOSE, NULL);
1886}
1887
1888void msc_a_release_mo(struct msc_a *msc_a, enum gsm48_gsm_cause gsm_cause)
1889{
1890 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
1891}