blob: b4776a0257c706d1c91fe10578aeb54ff230f57a [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
158static void evaluate_acceptance_outcome(struct osmo_fsm_inst *fi, bool conn_accepted)
159{
160 struct msc_a *msc_a = fi->priv;
161 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
162
163 update_counters(fi, conn_accepted);
164
Neels Hofmeyr83cf10c2020-06-24 14:23:26 +0200165 if (conn_accepted) {
166 /* Record the Cell ID seen in Complete Layer 3 Information in the VLR, so that it also shows in vty
167 * 'show' output. */
168 vsub->cgi = msc_a->via_cell;
169 }
170
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100171 /* Trigger transactions that we paged for */
172 if (msc_a->complete_layer3_type == COMPLETE_LAYER3_PAGING_RESP) {
173 if (conn_accepted)
174 paging_response(msc_a);
175 else
176 paging_expired(vsub);
177 }
178
179 if (conn_accepted)
180 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_ATTACHED, msc_a_vsub(msc_a));
181
182 if (msc_a->complete_layer3_type == COMPLETE_LAYER3_LU)
183 msc_a_put(msc_a, MSC_A_USE_LOCATION_UPDATING);
Neels Hofmeyrae98b972021-07-27 03:46:49 +0200184
185 if (msc_a->complete_layer3_type == COMPLETE_LAYER3_CM_RE_ESTABLISH_REQ) {
186 /* Trigger new Assignment to recommence the voice call. A little dance here because normally we verify
187 * that no CC trans is already active. */
188 struct gsm_trans *cc_trans = msc_a->cc.active_trans;
189 msc_a->cc.active_trans = NULL;
190 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, cc_trans);
191 msc_a_try_call_assignment(cc_trans);
192 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100193}
194
195bool msc_a_is_accepted(const struct msc_a *msc_a)
196{
197 if (!msc_a || !msc_a->c.fi)
198 return false;
199 return msc_a->c.fi->state == MSC_A_ST_AUTHENTICATED
200 || msc_a->c.fi->state == MSC_A_ST_COMMUNICATING;
201}
202
203bool msc_a_in_release(struct msc_a *msc_a)
204{
205 if (!msc_a)
206 return true;
207 if (msc_a->c.fi->state == MSC_A_ST_RELEASING)
208 return true;
209 if (msc_a->c.fi->state == MSC_A_ST_RELEASED)
210 return true;
211 return false;
212}
213
214static int msc_a_ran_dec(struct msc_a *msc_a, const struct an_apdu *an_apdu, enum msc_role from_role)
215{
216 int rc;
217 struct msc_a_ran_dec_data d = {
218 .from_role = from_role,
219 .an_apdu = an_apdu,
220 };
221 msc_a_get(msc_a, __func__);
222 rc = msc_role_ran_decode(msc_a->c.fi, an_apdu, msc_a_ran_decode_cb, &d);
223 msc_a_put(msc_a, __func__);
224 return rc;
225};
226
227static void msc_a_fsm_validate_l3(struct osmo_fsm_inst *fi, uint32_t event, void *data)
228{
229 struct msc_a *msc_a = fi->priv;
230 const struct an_apdu *an_apdu;
231
232 switch (event) {
233 case MSC_A_EV_FROM_I_COMPLETE_LAYER_3:
234 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
235 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
236 an_apdu = data;
237 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_I);
238 return;
239
240 case MSC_A_EV_COMPLETE_LAYER_3_OK:
241 msc_a_state_chg(msc_a, MSC_A_ST_AUTH_CIPH);
242 return;
243
244 case MSC_A_EV_MO_CLOSE:
245 case MSC_A_EV_CN_CLOSE:
246 evaluate_acceptance_outcome(fi, false);
247 /* fall through */
248 case MSC_A_EV_UNUSED:
249 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
250 return;
251
252 default:
253 OSMO_ASSERT(false);
254 }
255}
256
257/* Figure out whether to first send a Classmark Request to the MS to figure out algorithm support. */
258static bool msc_a_need_classmark_for_ciphering(struct msc_a *msc_a)
259{
260 struct gsm_network *net = msc_a_net(msc_a);
261 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
262 int i = 0;
263 bool request_classmark = false;
264
265 /* Only on GERAN-A do we ever need Classmark Information for Ciphering. */
266 if (msc_a->c.ran->type != OSMO_RAT_GERAN_A)
267 return false;
268
269 for (i = 0; i < 8; i++) {
270 int supported;
271
272 /* A5/n permitted by osmo-msc.cfg? */
273 if (!(net->a5_encryption_mask & (1 << i)))
274 continue;
275
276 /* A5/n supported by MS? */
277 supported = osmo_gsm48_classmark_supports_a5(&vsub->classmark, i);
278 if (supported < 0) {
279 LOG_MSC_A(msc_a, LOGL_DEBUG, "For A5/%d, we still need Classmark %d\n", i, -supported);
280 request_classmark = true;
281 }
282 }
283
284 return request_classmark;
285}
286
287static int msc_a_ran_enc_ciphering(struct msc_a *msc_a, bool umts_aka, bool retrieve_imeisv);
288
289/* VLR callback for ops.set_ciph_mode() */
290int msc_a_vlr_set_cipher_mode(void *_msc_a, bool umts_aka, bool retrieve_imeisv)
291{
292 struct msc_a *msc_a = _msc_a;
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700293 struct vlr_subscr *vsub;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100294
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700295 if (!msc_a) {
296 LOGP(DMSC, LOGL_ERROR, "Insufficient info to start ciphering: "
297 "MSC-A role is NULL?!?\n");
298 return -EINVAL;
299 }
300
301 vsub = msc_a_vsub(msc_a);
302 if (!vsub || !vsub->last_tuple) {
303 LOG_MSC_A(msc_a, LOGL_ERROR, "Insufficient info to start ciphering: "
304 "vlr_subscr is NULL?!?\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100305 return -EINVAL;
306 }
307
308 if (msc_a_need_classmark_for_ciphering(msc_a)) {
309 int rc;
310 struct ran_msg msg = {
311 .msg_type = RAN_MSG_CLASSMARK_REQUEST,
312 };
313 rc = msc_a_ran_down(msc_a, MSC_ROLE_I, &msg);
314 if (rc) {
315 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot send Classmark Request\n");
316 return -EIO;
317 }
318
319 msc_a->state_before_classmark_update = msc_a->c.fi->state;
320 msc_a->action_on_classmark_update = (struct msc_a_action_on_classmark_update){
321 .type = MSC_A_CLASSMARK_UPDATE_THEN_CIPHERING,
322 .ciphering = {
323 .umts_aka = umts_aka,
324 .retrieve_imeisv = retrieve_imeisv,
325 },
326 };
327 msc_a_state_chg(msc_a, MSC_A_ST_WAIT_CLASSMARK_UPDATE);
328 return 0;
329 }
330
331 return msc_a_ran_enc_ciphering(msc_a, umts_aka, retrieve_imeisv);
332}
333
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +0200334static uint8_t filter_a5(uint8_t a5_mask, bool umts_aka)
335{
336 /* With GSM AKA: allow A5/0, 1, 3 = 0b00001011 = 0xb.
337 * UMTS aka: allow A5/0, 1, 3, 4 = 0b00011011 = 0x1b.
338 */
339 return a5_mask & (umts_aka ? 0x1b : 0x0b);
340}
341
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100342static int msc_a_ran_enc_ciphering(struct msc_a *msc_a, bool umts_aka, bool retrieve_imeisv)
343{
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700344 struct gsm_network *net;
345 struct vlr_subscr *vsub;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100346 struct ran_msg msg;
347
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700348 if (!msc_a) {
349 LOGP(DMSC, LOGL_ERROR, "Insufficient info to start ciphering: "
350 "MSC-A role is NULL?!?\n");
351 return -EINVAL;
352 }
353
354 net = msc_a_net(msc_a);
355 vsub = msc_a_vsub(msc_a);
356
357 if (!net || !vsub || !vsub->last_tuple) {
358 LOG_MSC_A(msc_a, LOGL_ERROR, "Insufficient info to start ciphering: "
359 "gsm_network and/or vlr_subscr is NULL?!?\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100360 return -EINVAL;
361 }
362
363 msg = (struct ran_msg){
364 .msg_type = RAN_MSG_CIPHER_MODE_COMMAND,
365 .cipher_mode_command = {
366 .vec = vsub->last_tuple ? &vsub->last_tuple->vec : NULL,
367 .classmark = &vsub->classmark,
368 .geran = {
369 .umts_aka = umts_aka,
370 .retrieve_imeisv = retrieve_imeisv,
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +0200371 .a5_encryption_mask = filter_a5(net->a5_encryption_mask, umts_aka),
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100372
373 /* for ran_a.c to store the GERAN key that is actually used */
374 .chosen_key = &msc_a->geran_encr,
375 },
Harald Welte274b70f2021-02-06 16:47:39 +0100376 .utran = {
Harald Welte505a94a2021-02-06 17:12:20 +0100377 .uea_encryption_mask = net->uea_encryption_mask,
378 },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100379 },
380 };
381
382 if (msc_a_ran_down(msc_a, MSC_ROLE_I, &msg)) {
383 LOG_MSC_A(msc_a, LOGL_ERROR, "Sending Cipher Mode Command failed\n");
384 /* Returning error to the VLR ops.set_ciph_mode() will cancel the attach. Other callers need to take
385 * care of the return value. */
386 return -EINVAL;
387 }
388
389 if (msc_a->geran_encr.key_len)
Neels Hofmeyr73d093a2021-06-23 23:54:43 +0200390 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN encoding chose ciphering: A5/%d kc %s kc128 %s\n",
391 msc_a->geran_encr.alg_id - 1,
392 osmo_hexdump_nospc_c(OTC_SELECT, msc_a->geran_encr.key, msc_a->geran_encr.key_len),
393 msc_a->geran_encr.kc128_present ?
394 osmo_hexdump_nospc_c(OTC_SELECT, msc_a->geran_encr.kc128, sizeof(msc_a->geran_encr.kc128))
395 : "-");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100396 return 0;
397}
398
399static void msc_a_fsm_auth_ciph(struct osmo_fsm_inst *fi, uint32_t event, void *data)
400{
401 struct msc_a *msc_a = fi->priv;
402
403 /* If accepted, transition the state, all other cases mean failure. */
404 switch (event) {
405 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
406 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
407 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
408 return;
409
410 case MSC_A_EV_AUTHENTICATED:
411 msc_a_state_chg(msc_a, MSC_A_ST_AUTHENTICATED);
412 return;
413
414 case MSC_A_EV_UNUSED:
415 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
416 return;
417
418 case MSC_A_EV_MO_CLOSE:
419 case MSC_A_EV_CN_CLOSE:
420 evaluate_acceptance_outcome(fi, false);
421 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
422 return;
423
424
425 default:
426 OSMO_ASSERT(false);
427 }
428}
429
430static void msc_a_fsm_wait_classmark_update(struct osmo_fsm_inst *fi, uint32_t event, void *data)
431{
432 struct msc_a *msc_a = fi->priv;
433
434 switch (event) {
435 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
436 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
437 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
438 return;
439
440 case MSC_A_EV_CLASSMARK_UPDATE:
441 switch (msc_a->action_on_classmark_update.type) {
442 case MSC_A_CLASSMARK_UPDATE_THEN_CIPHERING:
443 msc_a_state_chg(msc_a, MSC_A_ST_AUTH_CIPH);
444 if (msc_a_ran_enc_ciphering(msc_a,
445 msc_a->action_on_classmark_update.ciphering.umts_aka,
446 msc_a->action_on_classmark_update.ciphering.retrieve_imeisv)) {
447 LOG_MSC_A(msc_a, LOGL_ERROR,
448 "After Classmark Update, still failed to send Cipher Mode Command\n");
449 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
450 }
451 return;
452
453 default:
454 LOG_MSC_A(msc_a, LOGL_ERROR, "Internal error: After Classmark Update, don't know what to do\n");
455 msc_a_state_chg(msc_a, msc_a->state_before_classmark_update);
456 return;
457 }
458
459 case MSC_A_EV_UNUSED:
460 /* Seems something detached / aborted in the middle of auth+ciph. */
461 evaluate_acceptance_outcome(fi, false);
462 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
463 return;
464
465 case MSC_A_EV_MO_CLOSE:
466 case MSC_A_EV_CN_CLOSE:
467 evaluate_acceptance_outcome(fi, false);
468 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
469 return;
470
471 default:
472 OSMO_ASSERT(false);
473 }
474}
475
476static bool msc_a_fsm_has_active_transactions(struct osmo_fsm_inst *fi)
477{
478 struct msc_a *msc_a = fi->priv;
479 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
480 struct gsm_trans *trans;
481
482 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_SILENT_CALL)) {
483 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: silent call still active\n", __func__);
484 return true;
485 }
486
487 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC)) {
488 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO CC request after a CM Service Request\n",
489 __func__);
490 return true;
491 }
Andreas Eversberg456c6f72023-04-23 11:54:16 +0200492 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_GCC)) {
493 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO GCC request after a CM Service Request\n",
494 __func__);
495 return true;
496 }
497 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_BCC)) {
498 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO BCC request after a CM Service Request\n",
499 __func__);
500 return true;
501 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100502 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_SMS)) {
503 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO SMS after a CM Service Request\n",
504 __func__);
505 return true;
506 }
507 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_SS)) {
508 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO SS after a CM Service Request\n",
509 __func__);
510 return true;
511 }
512
513 if (vsub && !llist_empty(&vsub->cs.requests)) {
514 struct paging_request *pr;
515 llist_for_each_entry(pr, &vsub->cs.requests, entry) {
516 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still active: %s\n", __func__, pr->label);
517 }
518 return true;
519 }
520
521 if ((trans = trans_has_conn(msc_a))) {
522 LOG_MSC_A(msc_a, LOGL_DEBUG, "connection still has active transaction: %s\n",
523 trans_type_name(trans->type));
524 return true;
525 }
526
527 return false;
528}
529
530static void msc_a_fsm_authenticated_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
531{
532 struct msc_a *msc_a = fi->priv;
533 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
534
535 /* Stop Location Update expiry for this subscriber. While the subscriber
536 * has an open connection the LU expiry timer must remain disabled.
537 * Otherwise we would kick the subscriber off the network when the timer
538 * expires e.g. during a long phone call.
539 * The LU expiry timer will restart once the connection is closed. */
540 if (vsub)
541 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
542
543 evaluate_acceptance_outcome(fi, true);
544}
545
546static void msc_a_fsm_authenticated(struct osmo_fsm_inst *fi, uint32_t event, void *data)
547{
548 struct msc_a *msc_a = fi->priv;
549
550 switch (event) {
551 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
552 case MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST:
553 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
554 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
555 return;
556
557 case MSC_A_EV_COMPLETE_LAYER_3_OK:
558 /* When Authentication is off, we may already be in the Accepted state when the code
559 * evaluates the Compl L3. Simply ignore. This just cosmetically mutes the error log
560 * about the useless event. */
561 return;
562
563 case MSC_A_EV_TRANSACTION_ACCEPTED:
564 msc_a_state_chg(msc_a, MSC_A_ST_COMMUNICATING);
565 return;
566
567 case MSC_A_EV_MO_CLOSE:
568 case MSC_A_EV_CN_CLOSE:
569 case MSC_A_EV_UNUSED:
570 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
571 return;
572
573 default:
574 OSMO_ASSERT(false);
575 }
576}
577
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100578static struct call_leg *msc_a_ensure_call_leg(struct msc_a *msc_a, struct gsm_trans *for_cc_trans)
579{
580 struct call_leg *cl = msc_a->cc.call_leg;
581 struct gsm_network *net = msc_a_net(msc_a);
582
583 /* Ensure that events about RTP endpoints coming from the msc_a->cc.call_leg know which gsm_trans to abort on
584 * error */
585 if (!msc_a->cc.active_trans)
586 msc_a->cc.active_trans = for_cc_trans;
587 if (msc_a->cc.active_trans != for_cc_trans) {
588 LOG_TRANS(for_cc_trans, LOGL_ERROR,
589 "Cannot create call leg, another trans is already active for this conn\n");
590 return NULL;
591 }
592
593 if (!cl) {
594 cl = msc_a->cc.call_leg = call_leg_alloc(msc_a->c.fi,
595 MSC_EV_CALL_LEG_TERM,
596 MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE,
597 MSC_EV_CALL_LEG_RTP_COMPLETE);
598 OSMO_ASSERT(cl);
599
600 if (net->use_osmux != OSMUX_USAGE_OFF) {
601 struct msc_i *msc_i = msc_a_msc_i(msc_a);
602 if (msc_i->c.remote_to) {
603 /* TODO: investigate what to do in this case */
604 LOG_MSC_A(msc_a, LOGL_ERROR, "Osmux not yet supported for inter-MSC");
605 } else {
606 cl->ran_peer_supports_osmux = msc_i->ran_conn->ran_peer->remote_supports_osmux;
607 }
608 }
609
610 }
611 return cl;
612}
613
614int msc_a_ensure_cn_local_rtp(struct msc_a *msc_a, struct gsm_trans *cc_trans)
615{
616 struct call_leg *cl;
617 struct rtp_stream *rtp_to_ran;
618
619 cl = msc_a_ensure_call_leg(msc_a, cc_trans);
620 if (!cl)
621 return -EINVAL;
622 rtp_to_ran = cl->rtp[RTP_TO_RAN];
623
624 if (call_leg_local_ip(cl, RTP_TO_CN)) {
625 /* Already has an RTP address and port towards the CN, continue right away. */
626 return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_CN]);
627 }
628
629 /* No CN RTP address available yet, ask the MGW to create one.
630 * Set a codec to be used: if Assignment on the RAN side is already done, take the same codec as the RTP_TO_RAN.
631 * If no RAN side RTP is established, try to guess a preliminary codec from SDP -- before Assignment, picking a
632 * codec from the SDP is more politeness/avoiding confusion than necessity. The actual codec to be used would be
633 * determined later. If no codec could be determined, pass none for the time being. */
Andreas Eversberg712b28e2023-06-21 11:17:26 +0200634 return call_leg_ensure_ci(cl, RTP_TO_CN, cc_trans->call_id, cc_trans,
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100635 rtp_to_ran->codecs_known ? &rtp_to_ran->codecs : NULL, NULL);
636}
637
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100638/* The MGW has given us a local IP address for the RAN side. Ready to start the Assignment of a voice channel. */
639static void msc_a_call_leg_ran_local_addr_available(struct msc_a *msc_a)
640{
641 struct ran_msg msg;
642 struct gsm_trans *cc_trans = msc_a->cc.active_trans;
643 struct gsm0808_channel_type channel_type;
644
Neels Hofmeyr00a476b2019-11-28 02:46:05 +0100645 if (!cc_trans) {
646 LOG_MSC_A(msc_a, LOGL_ERROR, "No CC transaction active\n");
647 call_leg_release(msc_a->cc.call_leg);
648 return;
649 }
650
Oliver Smithceca8e62023-05-24 11:15:52 +0200651 trans_cc_filter_run(cc_trans);
652 LOG_TRANS(cc_trans, LOGL_DEBUG, "Sending Assignment Command\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100653
Oliver Smith10632132023-05-12 12:14:22 +0200654 switch (cc_trans->bearer_cap.transfer) {
655 case GSM48_BCAP_ITCAP_SPEECH:
656 if (!cc_trans->cc.local.audio_codecs.count) {
657 LOG_TRANS(cc_trans, LOGL_ERROR, "Assignment not possible, no matching codec: %s\n",
658 codec_filter_to_str(&cc_trans->cc.codecs, &cc_trans->cc.local, &cc_trans->cc.remote));
659 call_leg_release(msc_a->cc.call_leg);
660 return;
661 }
662
663 /* Compose 48.008 Channel Type from the current set of codecs
664 * determined from both local and remote codec capabilities. */
665 if (sdp_audio_codecs_to_gsm0808_channel_type(&channel_type, &cc_trans->cc.local.audio_codecs)) {
666 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot compose Channel Type (Permitted Speech) from codecs: %s\n",
667 codec_filter_to_str(&cc_trans->cc.codecs, &cc_trans->cc.local, &cc_trans->cc.remote));
668 trans_free(cc_trans);
669 return;
670 }
671 break;
672 case GSM48_BCAP_ITCAP_UNR_DIG_INF:
673 if (!cc_trans->cc.local.bearer_services.count) {
674 LOG_TRANS(cc_trans, LOGL_ERROR, "Assignment not possible, no matching bearer service: %s\n",
675 csd_filter_to_str(&cc_trans->cc.csd, &cc_trans->cc.local, &cc_trans->cc.remote));
676 call_leg_release(msc_a->cc.call_leg);
677 return;
678 }
679
680 /* Compose 48.008 Channel Type from the current set of bearer
681 * services determined from local and remote capabilities. */
682 if (csd_bs_list_to_gsm0808_channel_type(&channel_type, &cc_trans->cc.local.bearer_services)) {
683 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot compose channel type from: %s\n",
684 csd_filter_to_str(&cc_trans->cc.csd, &cc_trans->cc.local, &cc_trans->cc.remote));
685 return;
686 }
687 break;
688 default:
689 LOG_TRANS(cc_trans, LOGL_ERROR, "Assignment not possible for information transfer capability %d\n",
690 cc_trans->bearer_cap.transfer);
Neels Hofmeyr11a746a2023-01-26 15:00:06 +0100691 call_leg_release(msc_a->cc.call_leg);
692 return;
693 }
694
Oliver Smith10632132023-05-12 12:14:22 +0200695 /* The RAN side RTP address is known, so the voice/CSD Assignment can commence. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100696 msg = (struct ran_msg){
697 .msg_type = RAN_MSG_ASSIGNMENT_COMMAND,
698 .assignment_command = {
699 .cn_rtp = &msc_a->cc.call_leg->rtp[RTP_TO_RAN]->local,
700 .channel_type = &channel_type,
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200701 .osmux_present = msc_a->cc.call_leg->rtp[RTP_TO_RAN]->use_osmux,
702 .osmux_cid = msc_a->cc.call_leg->rtp[RTP_TO_RAN]->local_osmux_cid,
Philipp Maierf34d9452020-06-05 15:49:35 +0200703 .call_id_present = true,
Andreas Eversberg712b28e2023-06-21 11:17:26 +0200704 .call_id = cc_trans->call_id,
Keith Whytea1a70be2021-05-16 02:59:52 +0200705 .lcls = cc_trans->cc.lcls,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100706 },
707 };
708 if (msc_a_ran_down(msc_a, MSC_ROLE_I, &msg)) {
709 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot send Assignment\n");
Neels Hofmeyrf439ff12019-10-05 04:19:36 +0200710 trans_free(cc_trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100711 return;
712 }
713}
714
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100715static struct gsm_trans *find_waiting_call(struct msc_a *msc_a)
716{
717 struct gsm_trans *trans;
718 struct gsm_network *net = msc_a_net(msc_a);
719
720 llist_for_each_entry(trans, &net->trans_list, entry) {
721 if (trans->msc_a != msc_a)
722 continue;
723 if (trans->type != TRANS_CC)
724 continue;
725 if (trans->msc_a->cc.active_trans == trans)
726 continue;
727 return trans;
728 }
729 return NULL;
730}
731
732static void msc_a_cleanup_rtp_streams(struct msc_a *msc_a, uint32_t event, void *data)
733{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100734 switch (event) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100735
736 case MSC_EV_CALL_LEG_TERM:
737 msc_a->cc.call_leg = NULL;
738 if (msc_a->cc.mncc_forwarding_to_remote_ran)
739 msc_a->cc.mncc_forwarding_to_remote_ran->rtps = NULL;
740
Neels Hofmeyr265a4c72019-05-09 16:20:51 +0200741 if (msc_a->ho.new_cell.mncc_forwarding_to_remote_ran)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100742 msc_a->ho.new_cell.mncc_forwarding_to_remote_ran->rtps = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100743 return;
744
745 case MSC_MNCC_EV_CALL_ENDED:
746 msc_a->cc.mncc_forwarding_to_remote_ran = NULL;
747 return;
748
749 default:
750 return;
751 }
752}
753
754static void msc_a_fsm_communicating(struct osmo_fsm_inst *fi, uint32_t event, void *data)
755{
756 struct msc_a *msc_a = fi->priv;
757 struct rtp_stream *rtps;
758 struct gsm_trans *waiting_trans;
759 struct an_apdu *an_apdu;
760
761 msc_a_cleanup_rtp_streams(msc_a, event, data);
762
763 switch (event) {
764 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
765 case MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST:
766 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
767 an_apdu = data;
768 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_I);
769 return;
770
771 case MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE:
772 case MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE:
773 case MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST:
774 case MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST:
775 an_apdu = data;
776 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_T);
777 return;
778
779 case MSC_A_EV_TRANSACTION_ACCEPTED:
780 /* no-op */
781 return;
782
783 case MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE:
784 rtps = data;
785 if (!rtps) {
786 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid data for %s\n", osmo_fsm_event_name(fi->fsm, event));
787 return;
788 }
Neels Hofmeyr00a476b2019-11-28 02:46:05 +0100789 if (!msc_a->cc.call_leg) {
790 LOG_MSC_A(msc_a, LOGL_ERROR, "No call leg active\n");
791 return;
792 }
Neels Hofmeyrcc918cb2019-11-28 02:16:34 +0100793 if (!osmo_sockaddr_str_is_nonzero(&rtps->local)) {
794 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid RTP address received from MGW: " OSMO_SOCKADDR_STR_FMT "\n",
795 OSMO_SOCKADDR_STR_FMT_ARGS(&rtps->local));
796 call_leg_release(msc_a->cc.call_leg);
797 return;
798 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100799 LOG_MSC_A(msc_a, LOGL_DEBUG,
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200800 "MGW endpoint's RTP address available for the CI %s: " OSMO_SOCKADDR_STR_FMT " (osmux=%s:%d)\n",
801 rtp_direction_name(rtps->dir), OSMO_SOCKADDR_STR_FMT_ARGS(&rtps->local),
802 rtps->use_osmux ? "yes" : "no", rtps->local_osmux_cid);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100803 switch (rtps->dir) {
804 case RTP_TO_RAN:
805 msc_a_call_leg_ran_local_addr_available(msc_a);
806 return;
807 case RTP_TO_CN:
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100808 cc_on_cn_local_rtp_port_known(rtps->for_trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100809 return;
810 default:
811 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid data for %s\n", osmo_fsm_event_name(fi->fsm, event));
812 return;
813 }
814
815 case MSC_EV_CALL_LEG_RTP_COMPLETE:
816 /* Nothing to do. */
817 return;
818
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100819 case MSC_MNCC_EV_CALL_ENDED:
820 /* Cleaned up above */
821 return;
822
823 case MSC_EV_CALL_LEG_TERM:
824 /* RTP streams cleaned up above */
825
826 msc_a_get(msc_a, __func__);
827 if (msc_a->cc.active_trans)
828 trans_free(msc_a->cc.active_trans);
829
830 /* If there is another call still waiting to be activated, this is the time when the mgcp_ctx is
831 * available again and the other call can start assigning. */
832 waiting_trans = find_waiting_call(msc_a);
833 if (waiting_trans) {
834 LOG_MSC_A(msc_a, LOGL_DEBUG, "(ti %02x) Call waiting: starting Assignment\n",
835 waiting_trans->transaction_id);
836 msc_a_try_call_assignment(waiting_trans);
837 }
838 msc_a_put(msc_a, __func__);
839 return;
840
841 case MSC_A_EV_HANDOVER_REQUIRED:
842 msc_ho_start(msc_a, (struct ran_handover_required*)data);
843 return;
844
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200845 case MSC_A_EV_HANDOVER_END:
846 /* Termination event of the msc_ho_fsm. No action needed, it's all done in the msc_ho_fsm cleanup. This
847 * event only exists because osmo_fsm_inst_alloc_child() requires a parent term event; and maybe
848 * interesting for logging. */
849 return;
850
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100851 case MSC_A_EV_MO_CLOSE:
852 case MSC_A_EV_CN_CLOSE:
853 case MSC_A_EV_UNUSED:
854 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
855 return;
856
857 default:
858 OSMO_ASSERT(false);
859 }
860}
861
862static int msc_a_fsm_timer_cb(struct osmo_fsm_inst *fi)
863{
864 struct msc_a *msc_a = fi->priv;
865 if (msc_a_in_release(msc_a)) {
866 LOG_MSC_A(msc_a, LOGL_ERROR, "Timeout while releasing, discarding right now\n");
867 msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
868 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
869 } else {
870 enum gsm48_reject_value cause = GSM48_REJECT_CONGESTION;
871 osmo_fsm_inst_dispatch(fi, MSC_A_EV_CN_CLOSE, &cause);
872 }
873 return 0;
874}
875
876static void msc_a_fsm_releasing_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
877{
878 struct msc_a *msc_a = fi->priv;
879 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
880 int i;
881 char buf[128];
882 const char * const use_counts_to_cancel[] = {
883 MSC_A_USE_LOCATION_UPDATING,
884 MSC_A_USE_CM_SERVICE_CC,
885 MSC_A_USE_CM_SERVICE_SMS,
886 MSC_A_USE_CM_SERVICE_SS,
Andreas Eversberg456c6f72023-04-23 11:54:16 +0200887 MSC_A_USE_CM_SERVICE_GCC,
888 MSC_A_USE_CM_SERVICE_BCC,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100889 MSC_A_USE_PAGING_RESPONSE,
890 };
891
892 LOG_MSC_A(msc_a, LOGL_DEBUG, "Releasing: msc_a use is %s\n",
893 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
894
895 if (vsub) {
896 vlr_subscr_get(vsub, __func__);
897
898 /* Cancel all VLR FSMs, if any */
899 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
900
901 /* The subscriber has no active connection anymore.
902 * Restart the periodic Location Update expiry timer for this subscriber. */
903 vlr_subscr_enable_expire_lu(vsub);
904 }
905
906 /* If we're closing in a middle of a trans, we need to clean up */
907 trans_conn_closed(msc_a);
908
909 call_leg_release(msc_a->cc.call_leg);
910
911 /* Cancel use counts for pending CM Service / Paging */
912 for (i = 0; i < ARRAY_SIZE(use_counts_to_cancel); i++) {
913 const char *use = use_counts_to_cancel[i];
914 int32_t count = osmo_use_count_by(&msc_a->use_count, use);
915 if (!count)
916 continue;
917 LOG_MSC_A(msc_a, LOGL_DEBUG, "Releasing: canceling still pending use: %s (%d)\n", use, count);
918 osmo_use_count_get_put(&msc_a->use_count, use, -count);
919 }
920
921 if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) {
922 sgs_iface_tx_release(vsub);
923 /* In SGsAP there is no confirmation of a release. */
924 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
925 } else {
926 struct ran_msg msg = {
927 .msg_type = RAN_MSG_CLEAR_COMMAND,
928 .clear_command = {
Neels Hofmeyrd9fe7112020-07-11 00:20:20 +0200929 /* "Call Control" is the only cause code listed in 3GPP TS 48.008 3.2.1.21 CLEAR COMMAND
930 * that qualifies for a normal release situation. (OS#4664) */
931 .gsm0808_cause = GSM0808_CAUSE_CALL_CONTROL,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100932 .csfb_ind = (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED),
933 },
934 };
935 msc_a_get(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
936 msc_a_ran_down(msc_a, MSC_ROLE_I, &msg);
Philipp Maier47cf84d2019-08-15 14:56:54 +0200937
938 /* The connection is cleared. The MS will now go back to 4G,
939 Switch the RAN type back to SGS. */
940 if (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED)
941 vsub->cs.attached_via_ran = OSMO_RAT_EUTRAN_SGS;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100942 }
943
944 if (vsub)
945 vlr_subscr_put(vsub, __func__);
946}
947
948static void msc_a_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
949{
950 struct msc_a *msc_a = fi->priv;
951
952 msc_a_cleanup_rtp_streams(msc_a, event, data);
953
954 switch (event) {
955 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
956 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
957 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
958 return;
959
960 case MSC_A_EV_MO_CLOSE:
961 case MSC_A_EV_CN_CLOSE:
962 case MSC_A_EV_UNUSED:
963 /* Already releasing */
964 return;
965
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100966 case MSC_EV_CALL_LEG_TERM:
967 case MSC_MNCC_EV_CALL_ENDED:
968 /* RTP streams cleaned up above */
969 return;
970
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200971 case MSC_A_EV_HANDOVER_END:
972 /* msc_ho_fsm does cleanup. */
973 return;
974
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100975 default:
976 OSMO_ASSERT(false);
977 }
978}
979
980
981static void msc_a_fsm_released_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
982{
983 struct msc_a *msc_a = msc_a_fi_priv(fi);
984 char buf[128];
985 LOG_MSC_A(msc_a, LOGL_DEBUG, "Released: msc_a use is %s\n",
986 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
987 if (osmo_use_count_total(&msc_a->use_count) == 0)
988 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi);
989}
990
991static void msc_a_fsm_released(struct osmo_fsm_inst *fi, uint32_t event, void *data)
992{
993 if (event == MSC_A_EV_UNUSED)
994 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi);
995}
996
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100997void msc_a_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
998{
999 struct msc_a *msc_a = msc_a_fi_priv(fi);
1000
1001 trans_conn_closed(msc_a);
1002
1003 if (msc_a_fsm_has_active_transactions(fi))
1004 LOG_MSC_A(msc_a, LOGL_ERROR, "Deallocating active transactions failed\n");
1005
1006 LOG_MSC_A_CAT(msc_a, DREF, LOGL_DEBUG, "max total use count was %d\n", msc_a->max_total_use_count);
1007}
1008
1009const struct value_string msc_a_fsm_event_names[] = {
1010 OSMO_VALUE_STRING(MSC_REMOTE_EV_RX_GSUP),
1011 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE),
1012 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_COMPLETE),
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001013 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_TERM),
1014 OSMO_VALUE_STRING(MSC_MNCC_EV_NEED_LOCAL_RTP),
1015 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_PROCEEDING),
1016 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_COMPLETE),
1017 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_ENDED),
1018 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_COMPLETE_LAYER_3),
1019 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST),
1020 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST),
1021 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST),
1022 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST),
1023 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE),
1024 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE),
1025 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST),
1026 OSMO_VALUE_STRING(MSC_A_EV_COMPLETE_LAYER_3_OK),
1027 OSMO_VALUE_STRING(MSC_A_EV_CLASSMARK_UPDATE),
1028 OSMO_VALUE_STRING(MSC_A_EV_AUTHENTICATED),
1029 OSMO_VALUE_STRING(MSC_A_EV_TRANSACTION_ACCEPTED),
1030 OSMO_VALUE_STRING(MSC_A_EV_CN_CLOSE),
1031 OSMO_VALUE_STRING(MSC_A_EV_MO_CLOSE),
1032 OSMO_VALUE_STRING(MSC_A_EV_UNUSED),
1033 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_REQUIRED),
1034 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_END),
1035 {}
1036};
1037
1038#define S(x) (1 << (x))
1039
1040static const struct osmo_fsm_state msc_a_fsm_states[] = {
1041 [MSC_A_ST_VALIDATE_L3] = {
1042 .name = OSMO_STRINGIFY(MSC_A_ST_VALIDATE_L3),
1043 .in_event_mask = 0
1044 | S(MSC_A_EV_FROM_I_COMPLETE_LAYER_3)
1045 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1046 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1047 | S(MSC_A_EV_COMPLETE_LAYER_3_OK)
1048 | S(MSC_A_EV_MO_CLOSE)
1049 | S(MSC_A_EV_CN_CLOSE)
1050 | S(MSC_A_EV_UNUSED)
1051 ,
1052 .out_state_mask = 0
1053 | S(MSC_A_ST_VALIDATE_L3)
1054 | S(MSC_A_ST_AUTH_CIPH)
1055 | S(MSC_A_ST_RELEASING)
1056 ,
1057 .action = msc_a_fsm_validate_l3,
1058 },
1059 [MSC_A_ST_AUTH_CIPH] = {
1060 .name = OSMO_STRINGIFY(MSC_A_ST_AUTH_CIPH),
1061 .in_event_mask = 0
1062 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1063 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1064 | S(MSC_A_EV_AUTHENTICATED)
1065 | S(MSC_A_EV_MO_CLOSE)
1066 | S(MSC_A_EV_CN_CLOSE)
1067 | S(MSC_A_EV_UNUSED)
1068 ,
1069 .out_state_mask = 0
1070 | S(MSC_A_ST_WAIT_CLASSMARK_UPDATE)
1071 | S(MSC_A_ST_AUTHENTICATED)
1072 | S(MSC_A_ST_RELEASING)
1073 ,
1074 .action = msc_a_fsm_auth_ciph,
1075 },
1076 [MSC_A_ST_WAIT_CLASSMARK_UPDATE] = {
1077 .name = OSMO_STRINGIFY(MSC_A_ST_WAIT_CLASSMARK_UPDATE),
1078 .in_event_mask = 0
1079 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1080 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1081 | S(MSC_A_EV_CLASSMARK_UPDATE)
1082 | S(MSC_A_EV_MO_CLOSE)
1083 | S(MSC_A_EV_CN_CLOSE)
1084 ,
1085 .out_state_mask = 0
1086 | S(MSC_A_ST_AUTH_CIPH)
1087 | S(MSC_A_ST_RELEASING)
1088 ,
1089 .action = msc_a_fsm_wait_classmark_update,
1090 },
1091 [MSC_A_ST_AUTHENTICATED] = {
1092 .name = OSMO_STRINGIFY(MSC_A_ST_AUTHENTICATED),
1093 /* allow everything to release for any odd behavior */
1094 .in_event_mask = 0
1095 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1096 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
1097 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1098 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
1099 | S(MSC_A_EV_MO_CLOSE)
1100 | S(MSC_A_EV_CN_CLOSE)
1101 | S(MSC_A_EV_UNUSED)
1102 ,
1103 .out_state_mask = 0
1104 | S(MSC_A_ST_RELEASING)
1105 | S(MSC_A_ST_COMMUNICATING)
1106 ,
1107 .onenter = msc_a_fsm_authenticated_enter,
1108 .action = msc_a_fsm_authenticated,
1109 },
1110 [MSC_A_ST_COMMUNICATING] = {
1111 .name = OSMO_STRINGIFY(MSC_A_ST_COMMUNICATING),
1112 /* allow everything to release for any odd behavior */
1113 .in_event_mask = 0
1114 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1115 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
1116 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1117 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE)
1118 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE)
1119 | S(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST)
1120 | S(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST)
1121 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
1122 | S(MSC_A_EV_MO_CLOSE)
1123 | S(MSC_A_EV_CN_CLOSE)
1124 | S(MSC_A_EV_UNUSED)
1125 | S(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE)
1126 | S(MSC_EV_CALL_LEG_RTP_COMPLETE)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001127 | S(MSC_EV_CALL_LEG_TERM)
1128 | S(MSC_MNCC_EV_CALL_ENDED)
1129 | S(MSC_A_EV_HANDOVER_REQUIRED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +02001130 | S(MSC_A_EV_HANDOVER_END)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001131 ,
1132 .out_state_mask = 0
1133 | S(MSC_A_ST_RELEASING)
1134 ,
1135 .action = msc_a_fsm_communicating,
1136 },
1137 [MSC_A_ST_RELEASING] = {
1138 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASING),
1139 .in_event_mask = 0
1140 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1141 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1142 | S(MSC_A_EV_UNUSED)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001143 | S(MSC_EV_CALL_LEG_TERM)
1144 | S(MSC_MNCC_EV_CALL_ENDED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +02001145 | S(MSC_A_EV_HANDOVER_END)
Pau Espin Pedrole53ecde2021-07-12 13:37:24 +02001146 | S(MSC_A_EV_CN_CLOSE)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001147 ,
1148 .out_state_mask = 0
1149 | S(MSC_A_ST_RELEASED)
1150 ,
1151 .onenter = msc_a_fsm_releasing_onenter,
1152 .action = msc_a_fsm_releasing,
1153 },
1154 [MSC_A_ST_RELEASED] = {
1155 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASED),
1156 .in_event_mask = 0
1157 | S(MSC_A_EV_UNUSED)
1158 ,
1159 .onenter = msc_a_fsm_released_onenter,
1160 .action = msc_a_fsm_released,
1161 },
1162};
1163
1164static struct osmo_fsm msc_a_fsm = {
1165 .name = "msc_a",
1166 .states = msc_a_fsm_states,
1167 .num_states = ARRAY_SIZE(msc_a_fsm_states),
1168 .log_subsys = DMSC,
1169 .event_names = msc_a_fsm_event_names,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001170 .timer_cb = msc_a_fsm_timer_cb,
1171 .cleanup = msc_a_fsm_cleanup,
1172};
1173
1174static __attribute__((constructor)) void msc_a_fsm_init()
1175{
1176 OSMO_ASSERT(osmo_fsm_register(&msc_a_fsm) == 0);
1177}
1178
1179static int msc_a_use_cb(struct osmo_use_count_entry *e, int32_t old_use_count, const char *file, int line)
1180{
1181 struct msc_a *msc_a = e->use_count->talloc_object;
1182 char buf[128];
1183 int32_t total;
1184 int level;
1185
1186 if (!e->use)
1187 return -EINVAL;
1188
1189 total = osmo_use_count_total(&msc_a->use_count);
1190
1191 if (total == 0
1192 || (total == 1 && old_use_count == 0 && e->count == 1))
1193 level = LOGL_INFO;
1194 else
1195 level = LOGL_DEBUG;
1196
1197 LOG_MSC_A_CAT_SRC(msc_a, DREF, level, file, line, "%s %s: now used by %s\n",
1198 (e->count - old_use_count) > 0? "+" : "-", e->use,
1199 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
1200
1201 if (e->count < 0)
1202 return -ERANGE;
1203
1204 msc_a->max_total_use_count = OSMO_MAX(msc_a->max_total_use_count, total);
1205
1206 if (total == 0)
1207 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_UNUSED, NULL);
1208 return 0;
1209}
1210
1211struct msc_a *msc_a_alloc(struct msub *msub, struct ran_infra *ran)
1212{
1213 struct msc_a *msc_a = msub_role_alloc(msub, MSC_ROLE_A, &msc_a_fsm, struct msc_a, ran);
1214 msc_a->use_count = (struct osmo_use_count){
1215 .talloc_object = msc_a,
1216 .use_cb = msc_a_use_cb,
1217 };
1218 osmo_use_count_make_static_entries(&msc_a->use_count, msc_a->use_count_buf, ARRAY_SIZE(msc_a->use_count_buf));
1219 /* Start timeout for first state */
Neels Hofmeyr01653252019-09-03 02:06:22 +02001220 msc_a_state_chg_always(msc_a, MSC_A_ST_VALIDATE_L3);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001221 return msc_a;
1222}
1223
1224bool msc_a_is_establishing_auth_ciph(const struct msc_a *msc_a)
1225{
1226 if (!msc_a || !msc_a->c.fi)
1227 return false;
1228 return msc_a->c.fi->state == MSC_A_ST_AUTH_CIPH;
1229}
1230
1231const struct value_string complete_layer3_type_names[] = {
1232 { COMPLETE_LAYER3_NONE, "NONE" },
1233 { COMPLETE_LAYER3_LU, "LU" },
1234 { COMPLETE_LAYER3_CM_SERVICE_REQ, "CM_SERVICE_REQ" },
1235 { COMPLETE_LAYER3_PAGING_RESP, "PAGING_RESP" },
Neels Hofmeyrae98b972021-07-27 03:46:49 +02001236 { COMPLETE_LAYER3_CM_RE_ESTABLISH_REQ, "CM_RE_ESTABLISH_REQ" },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001237 { 0, NULL }
1238};
1239
1240#define _msc_a_update_id(MSC_A, FMT, ARGS ...) \
1241 do { \
1242 if (osmo_fsm_inst_update_id_f(msc_a->c.fi, FMT ":%s:%s", \
1243 ## ARGS, \
1244 msub_ran_conn_name(msc_a->c.msub), \
1245 complete_layer3_type_name(msc_a->complete_layer3_type)) \
1246 == 0) { \
1247 struct vlr_subscr *_vsub = msc_a_vsub(MSC_A); \
1248 if (_vsub) { \
1249 if (_vsub->lu_fsm) \
1250 osmo_fsm_inst_update_id(_vsub->lu_fsm, (MSC_A)->c.fi->id); \
1251 if (_vsub->auth_fsm) \
1252 osmo_fsm_inst_update_id(_vsub->auth_fsm, (MSC_A)->c.fi->id); \
1253 if (_vsub->proc_arq_fsm) \
1254 osmo_fsm_inst_update_id(_vsub->proc_arq_fsm, (MSC_A)->c.fi->id); \
1255 } \
1256 LOG_MSC_A(MSC_A, LOGL_DEBUG, "Updated ID\n"); \
1257 } \
1258 /* otherwise osmo_fsm_inst_update_id_f() will log an error. */ \
1259 } while (0)
1260
1261
1262/* 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 +02001263void msc_a_update_id_from_mi(struct msc_a *msc_a, const struct osmo_mobile_identity *mi)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001264{
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001265 _msc_a_update_id(msc_a, "%s", osmo_mobile_identity_to_str_c(OTC_SELECT, mi));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001266}
1267
1268/* Update msc_a->fi id string from current msc_a->vsub and msc_a->complete_layer3_type. */
1269void msc_a_update_id(struct msc_a *msc_a)
1270{
1271 _msc_a_update_id(msc_a, "%s", vlr_subscr_name(msc_a_vsub(msc_a)));
1272}
1273
1274/* Iterate all msc_a instances that are relevant for this subscriber, and update FSM ID strings for all of the FSM
1275 * instances. */
1276void msc_a_update_id_for_vsub(struct vlr_subscr *for_vsub)
1277{
1278 struct msub *msub;
1279 llist_for_each_entry(msub, &msub_list, entry) {
1280 struct vlr_subscr *vsub = msub_vsub(msub);
1281 if (vsub != for_vsub)
1282 continue;
1283 msc_a_update_id(msub_msc_a(msub));
1284 }
1285}
1286
1287static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
1288{
1289 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
1290 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
1291
1292 switch (pdisc) {
1293 case GSM48_PDISC_MM:
1294 switch (msg_type) {
1295 case GSM48_MT_MM_LOC_UPD_REQUEST:
1296 case GSM48_MT_MM_CM_SERV_REQ:
1297 case GSM48_MT_MM_CM_REEST_REQ:
1298 case GSM48_MT_MM_AUTH_RESP:
1299 case GSM48_MT_MM_AUTH_FAIL:
1300 case GSM48_MT_MM_ID_RESP:
1301 case GSM48_MT_MM_TMSI_REALL_COMPL:
1302 case GSM48_MT_MM_IMSI_DETACH_IND:
1303 return true;
1304 default:
1305 break;
1306 }
1307 break;
1308 case GSM48_PDISC_RR:
1309 switch (msg_type) {
1310 /* GSM48_MT_RR_CIPH_M_COMPL is actually handled in bssmap_rx_ciph_compl() and gets redirected in the
1311 * BSSAP layer to ran_conn_cipher_mode_compl() (before this here is reached) */
1312 case GSM48_MT_RR_PAG_RESP:
1313 case GSM48_MT_RR_CIPH_M_COMPL:
1314 return true;
1315 default:
1316 break;
1317 }
1318 break;
1319 default:
1320 break;
1321 }
1322
1323 return false;
1324}
1325
1326/* Main entry point for GSM 04.08/44.008 Layer 3 data (e.g. from the BSC). */
1327int msc_a_up_l3(struct msc_a *msc_a, struct msgb *msg)
1328{
1329 struct gsm48_hdr *gh;
1330 uint8_t pdisc;
1331 int rc;
1332 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1333 int is_r99;
1334
1335 OSMO_ASSERT(msg->l3h);
1336 OSMO_ASSERT(msg);
1337
1338 gh = msgb_l3(msg);
1339 pdisc = gsm48_hdr_pdisc(gh);
1340
1341 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_DEBUG, "Dispatching 04.08 message: %s %s\n",
1342 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1343
1344 /* To evaluate the 3GPP TS 24.007 Duplicate Detection, we need Classmark information on whether the MS is R99
1345 * capable. If the subscriber is already actively connected, the Classmark information is stored with the
1346 * vlr_subscr. Otherwise, this *must* be a Complete Layer 3 with Classmark info. */
1347 if (vsub)
1348 is_r99 = osmo_gsm48_classmark_is_r99(&vsub->classmark) ? 1 : 0;
1349 else
1350 is_r99 = compl_l3_msg_is_r99(msg);
1351
1352 if (is_r99 < 0) {
1353 LOG_MSC_A(msc_a, LOGL_ERROR,
1354 "No Classmark Information, dropping non-Complete-Layer3 message: %s\n",
1355 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1356 return -EACCES;
1357 }
1358
1359 if (is_r99 >= 0
1360 && ran_dec_dtap_undup_is_duplicate(msc_a->c.fi, msc_a->n_sd_next, is_r99 ? true : false, msg)) {
1361 LOG_MSC_A(msc_a, LOGL_DEBUG, "Dropping duplicate message"
1362 " (3GPP TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection)\n");
1363 return 0;
1364 }
1365
1366 if (!msc_a_is_accepted(msc_a)
1367 && !msg_is_initially_permitted(gh)) {
1368 LOG_MSC_A(msc_a, LOGL_ERROR,
1369 "Message not permitted for initial conn: %s\n",
1370 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1371 return -EACCES;
1372 }
1373
1374 if (vsub && vsub->cs.attached_via_ran != msc_a->c.ran->type) {
1375 LOG_MSC_A(msc_a, LOGL_ERROR,
1376 "Illegal situation: RAN type mismatch:"
1377 " attached via %s, received message via %s\n",
1378 osmo_rat_type_name(vsub->cs.attached_via_ran),
1379 osmo_rat_type_name(msc_a->c.ran->type));
1380 return -EACCES;
1381 }
1382
1383#if 0
1384 if (silent_call_reroute(conn, msg))
1385 return silent_call_rx(conn, msg);
1386#endif
1387
1388 switch (pdisc) {
Andreas Eversberg23b53e52023-06-21 13:30:58 +02001389 case GSM48_PDISC_GROUP_CC:
1390 case GSM48_PDISC_BCAST_CC:
1391 rc = gsm44068_rcv_bcc_gcc(msc_a, NULL, msg);
1392 break;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001393 case GSM48_PDISC_CC:
1394 rc = gsm0408_rcv_cc(msc_a, msg);
1395 break;
1396 case GSM48_PDISC_MM:
1397 rc = gsm0408_rcv_mm(msc_a, msg);
1398 break;
1399 case GSM48_PDISC_RR:
1400 rc = gsm0408_rcv_rr(msc_a, msg);
1401 break;
1402 case GSM48_PDISC_SMS:
1403 rc = gsm0411_rcv_sms(msc_a, msg);
1404 break;
1405 case GSM48_PDISC_MM_GPRS:
1406 case GSM48_PDISC_SM_GPRS:
1407 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unimplemented "
1408 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1409 rc = -ENOTSUP;
1410 break;
1411 case GSM48_PDISC_NC_SS:
1412 rc = gsm0911_rcv_nc_ss(msc_a, msg);
1413 break;
1414 case GSM48_PDISC_TEST:
1415 rc = gsm0414_rcv_test(msc_a, msg);
1416 break;
1417 default:
1418 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unknown "
1419 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1420 rc = -EINVAL;
1421 break;
1422 }
1423
1424 return rc;
1425}
1426
1427static void msc_a_up_call_assignment_complete(struct msc_a *msc_a, const struct ran_msg *ac)
1428{
Andreas Eversberg23b53e52023-06-21 13:30:58 +02001429 struct gsm_trans *cc_trans = msc_a->cc.active_trans, *gcc_trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001430 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 +01001431 const struct gsm0808_speech_codec *codec_if_known = ac->assignment_complete.codec_present ?
1432 &ac->assignment_complete.codec : NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001433
Andreas Eversberg23b53e52023-06-21 13:30:58 +02001434 /* For a voice group call, handling is performed by VGCS FSM */
1435 gcc_trans = trans_find_by_type(msc_a, TRANS_GCC);
1436 if (gcc_trans) {
1437 vgcs_vbs_caller_assign_cpl(gcc_trans);
1438 return;
1439 }
1440 gcc_trans = trans_find_by_type(msc_a, TRANS_BCC);
1441 if (gcc_trans) {
1442 vgcs_vbs_caller_assign_cpl(gcc_trans);
1443 return;
1444 }
1445
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001446 if (!rtps_to_ran) {
1447 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but no RTP stream is set up\n");
1448 return;
1449 }
1450 if (!cc_trans) {
Neels Hofmeyr550506a2022-01-13 23:31:57 +01001451 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but no CC transaction is active\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001452 return;
1453 }
1454
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001455 if (rtps_to_ran->use_osmux != ac->assignment_complete.osmux_present) {
1456 LOG_MSC_A_CAT(msc_a, DCC, LOGL_ERROR, "Osmux usage ass request and complete don't match: %d vs %d\n",
1457 rtps_to_ran->use_osmux, ac->assignment_complete.osmux_present);
1458 call_leg_release(msc_a->cc.call_leg);
1459 return;
1460 }
1461
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001462 if (codec_if_known) {
Neels Hofmeyr388d7c92023-03-22 19:03:15 +01001463 const struct codec_mapping *codec_cn;
1464 /* For 2G:
1465 * - The Assignment Complete has returned a specific codec (e.g. FR3 for AMR FR).
1466 * - Set this codec at the MGW endpoint facing the RAN.
1467 * - Also set this codec at the MGW endpoint facing the CN -- we require an exact match on both call
1468 * legs.
1469 * - TODO: be aware of transcoding that the MGW is capable of, e.g. AMR octet-aligned to AMR
1470 * bandwidth-efficient...
1471 *
1472 * For 3G:
1473 * - ran_infra->force_mgw_codecs_to_ran sets VND.3GPP.IUFP as single codec at the MGW towards RAN.
1474 * - ran_msg_iu.c always returns FR3 (AMR FR) for the assigned codec. Set that at the MGW towards CN.
1475 * - So the MGW decapsulates IuUP <-> AMR
1476 */
1477 codec_cn = codec_mapping_by_gsm0808_speech_codec_type(codec_if_known->type);
1478 /* TODO: use codec_mapping_by_gsm0808_speech_codec() to also match on codec_if_known->cfg */
1479 if (!codec_cn) {
1480 LOG_TRANS(cc_trans, LOGL_ERROR, "Unknown codec in Assignment Complete: %s\n",
1481 gsm0808_speech_codec_type_name(codec_if_known->type));
1482 call_leg_release(msc_a->cc.call_leg);
1483 return;
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001484 }
1485
Oliver Smithf3941512023-08-23 13:11:44 +02001486 /* Check for unexpected codec with CSD */
1487 if (cc_trans->bearer_cap.transfer == GSM48_BCAP_ITCAP_UNR_DIG_INF &&
1488 codec_if_known->type != GSM0808_SCT_CSD) {
1489 LOG_TRANS(cc_trans, LOGL_ERROR, "Unexpected codec in Assignment Complete for CSD: %s\n",
1490 gsm0808_speech_codec_type_name(codec_if_known->type));
1491 call_leg_release(msc_a->cc.call_leg);
1492 return;
1493 }
1494
Neels Hofmeyrcec51b32023-03-01 03:47:45 +01001495 /* Update RAN-side endpoint CI from Assignment result -- unless it is forced by the ran_infra, in which
1496 * case it remains unchanged as passed to the earlier call of call_leg_ensure_ci(). */
1497 if (msc_a->c.ran->force_mgw_codecs_to_ran.count == 0)
1498 rtp_stream_set_one_codec(rtps_to_ran, &codec_cn->sdp);
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001499
1500 /* Update codec filter with Assignment result, for the CN side */
1501 cc_trans->cc.codecs.assignment = codec_cn->sdp;
1502 } else {
1503 /* No codec passed in Assignment Complete, set 'codecs.assignment' to none. */
1504 cc_trans->cc.codecs.assignment = (struct sdp_audio_codec){};
1505 LOG_TRANS(cc_trans, LOGL_INFO, "Assignment Complete without voice codec\n");
1506 }
1507
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001508 rtp_stream_set_remote_addr(rtps_to_ran, &ac->assignment_complete.remote_rtp);
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001509 if (rtps_to_ran->use_osmux)
1510 rtp_stream_set_remote_osmux_cid(rtps_to_ran,
1511 ac->assignment_complete.osmux_cid);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001512 rtp_stream_commit(rtps_to_ran);
1513
Neels Hofmeyr2d57d6e2022-01-13 21:39:58 +01001514 /* Remember the Codec List (BSS Supported) */
1515 if (ac->assignment_complete.codec_list_bss_supported)
1516 codec_filter_set_bss(&cc_trans->cc.codecs, ac->assignment_complete.codec_list_bss_supported);
1517
Oliver Smithceca8e62023-05-24 11:15:52 +02001518 trans_cc_filter_run(cc_trans);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001519 LOG_TRANS(cc_trans, LOGL_INFO, "Assignment Complete: RAN: %s, CN: %s\n",
1520 sdp_audio_codecs_to_str(&rtps_to_ran->codecs),
Oliver Smithc63c3a02023-05-24 10:48:07 +02001521 sdp_audio_codecs_to_str(&cc_trans->cc.local.audio_codecs));
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001522
1523 if (cc_on_assignment_done(cc_trans)) {
1524 /* If an error occurred, it was logged in cc_assignment_done() */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001525 call_leg_release(msc_a->cc.call_leg);
1526 return;
1527 }
1528}
1529
1530static void msc_a_up_call_assignment_failure(struct msc_a *msc_a, const struct ran_msg *af)
1531{
1532 struct gsm_trans *trans;
1533
1534 /* For a normal voice call, there will be an rtp_stream FSM. */
1535 if (msc_a->cc.call_leg && msc_a->cc.call_leg->rtp[RTP_TO_RAN]) {
1536 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing call\n");
1537 rtp_stream_release(msc_a->cc.call_leg->rtp[RTP_TO_RAN]);
1538 return;
1539 }
1540
Andreas Eversberg23b53e52023-06-21 13:30:58 +02001541 /* For a voice group call, release is performed by VGCS FSM */
1542 trans = trans_find_by_type(msc_a, TRANS_GCC);
1543 if (trans) {
1544 vgcs_vbs_caller_assign_fail(trans);
1545 return;
1546 }
1547 trans = trans_find_by_type(msc_a, TRANS_BCC);
1548 if (trans) {
1549 vgcs_vbs_caller_assign_fail(trans);
1550 return;
1551 }
1552
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001553 /* Otherwise, a silent call might be active */
1554 trans = trans_find_by_type(msc_a, TRANS_SILENT_CALL);
1555 if (trans) {
1556 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing silent call\n");
1557 trans_free(trans);
1558 return;
1559 }
1560
1561 /* Neither a voice call nor silent call assignment. Assume the worst and detach. */
1562 msc_a_release_cn(msc_a);
1563}
1564
1565static void msc_a_up_classmark_update(struct msc_a *msc_a, const struct osmo_gsm48_classmark *classmark,
1566 struct osmo_gsm48_classmark *dst)
1567{
1568 if (!dst) {
1569 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1570
1571 if (!vsub)
1572 dst = &msc_a->temporary_classmark;
1573 else
1574 dst = &vsub->classmark;
1575 }
1576
Martin Hauke3f07dac2019-11-14 17:49:08 +01001577 LOG_MSC_A(msc_a, LOGL_DEBUG, "A5 capabilities received from Classmark Update: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001578 osmo_gsm48_classmark_a5_name(classmark));
1579 osmo_gsm48_classmark_update(dst, classmark);
1580
1581 /* bump subscr conn FSM in case it is waiting for a Classmark Update */
1582 if (msc_a->c.fi->state == MSC_A_ST_WAIT_CLASSMARK_UPDATE)
1583 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CLASSMARK_UPDATE, NULL);
1584}
1585
1586static void msc_a_up_sapi_n_reject(struct msc_a *msc_a, const struct ran_msg *msg)
1587{
1588 int sapi = msg->sapi_n_reject.dlci & 0x7;
1589 if (sapi == UM_SAPI_SMS)
1590 gsm411_sapi_n_reject(msc_a);
1591}
1592
1593static int msc_a_up_ho(struct msc_a *msc_a, const struct msc_a_ran_dec_data *d, uint32_t ho_fi_event)
1594{
1595 if (!msc_a->ho.fi) {
1596 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Handover message, but no Handover ongoing: %s\n", d->ran_dec->msg_name);
1597 return -EINVAL;
1598 }
1599 return osmo_fsm_inst_dispatch(msc_a->ho.fi, ho_fi_event, (void*)d);
1600}
1601
1602int msc_a_ran_dec_from_msc_i(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1603{
1604 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Alexander Couzens2aaff752021-10-19 17:09:11 +02001605 struct gsm_network *net = msc_a_net(msc_a);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001606 const struct ran_msg *msg = d->ran_dec;
1607 int rc = -99;
1608
1609 switch (msg->msg_type) {
1610
1611 case RAN_MSG_COMPL_L3:
Neels Hofmeyr68f50da2020-06-24 14:22:52 +02001612 /* In case the cell_id from Complete Layer 3 Information lacks a PLMN, write the configured PLMN code
1613 * into msc_a->via_cell. Then overwrite with those bits obtained from Complete Layer 3 Information. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001614 msc_a->via_cell = (struct osmo_cell_global_id){
1615 .lai.plmn = msc_a_net(msc_a)->plmn,
1616 };
1617 gsm0808_cell_id_to_cgi(&msc_a->via_cell, msg->compl_l3.cell_id);
Neels Hofmeyrede90832022-01-13 18:13:15 +01001618
Neels Hofmeyre276ae92022-01-13 21:38:35 +01001619 /* If a codec list was sent along in the RAN_MSG_COMPL_L3, remember it for any upcoming codec
1620 * resolution. */
1621 if (msg->compl_l3.codec_list_bss_supported) {
1622 msc_a->cc.compl_l3_codec_list_bss_supported = *msg->compl_l3.codec_list_bss_supported;
1623 if (log_check_level(msc_a->c.ran->log_subsys, LOGL_DEBUG)) {
1624 struct sdp_audio_codecs ac = {};
1625 sdp_audio_codecs_from_speech_codec_list(&ac, &msc_a->cc.compl_l3_codec_list_bss_supported);
1626 LOG_MSC_A(msc_a, LOGL_DEBUG, "Complete Layer 3: Codec List (BSS Supported): %s\n",
1627 sdp_audio_codecs_to_str(&ac));
1628 }
1629 }
1630
Neels Hofmeyrede90832022-01-13 18:13:15 +01001631 /* Submit the Complete Layer 3 Information DTAP */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001632 rc = msc_a_up_l3(msc_a, msg->compl_l3.msg);
1633 if (!rc) {
1634 struct ran_conn *conn = msub_ran_conn(msc_a->c.msub);
1635 if (conn)
1636 ran_peer_cells_seen_add(conn->ran_peer, msg->compl_l3.cell_id);
1637 }
1638 break;
1639
1640 case RAN_MSG_DTAP:
1641 rc = msc_a_up_l3(msc_a, msg->dtap);
1642 break;
1643
1644 case RAN_MSG_CLEAR_REQUEST:
1645 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
1646 break;
1647
1648 case RAN_MSG_CLEAR_COMPLETE:
1649 switch (msc_a->c.fi->state) {
1650 case MSC_A_ST_RELEASING:
1651 msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
1652 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
1653 break;
1654 case MSC_A_ST_RELEASED:
1655 break;
1656 default:
1657 LOG_MSC_A(msc_a, LOGL_ERROR, "Received Clear Complete event, but did not send Clear Command\n");
1658 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
1659 break;
1660 }
1661 rc = 0;
1662 break;
1663
1664 case RAN_MSG_CLASSMARK_UPDATE:
1665 msc_a_up_classmark_update(msc_a, msg->classmark_update.classmark, NULL);
1666 rc = 0;
1667 break;
1668
1669 case RAN_MSG_CIPHER_MODE_COMPLETE:
1670 /* Remember what Ciphering was negotiated (e.g. for Handover) */
1671 if (msg->cipher_mode_complete.alg_id) {
1672 msc_a->geran_encr.alg_id = msg->cipher_mode_complete.alg_id;
1673 LOG_MSC_A(msc_a, LOGL_DEBUG, "Cipher Mode Complete: chosen encryption algorithm: A5/%u\n",
1674 msc_a->geran_encr.alg_id - 1);
Alexander Couzens2aaff752021-10-19 17:09:11 +02001675 }
1676
1677 if (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU) {
1678 int16_t utran_encryption;
1679
1680 /* utran: ensure chosen ciphering mode is allowed
1681 * If the IE is missing (utran_encryption == -1), parse it as no encryption */
1682 utran_encryption = msg->cipher_mode_complete.utran_encryption;
1683 if (utran_encryption == -1)
1684 utran_encryption = 0;
1685 if ((net->uea_encryption_mask & (1 << utran_encryption)) == 0) {
1686 /* cipher disallowed */
1687 LOG_MSC_A(msc_a, LOGL_ERROR, "Cipher Mode Complete: RNC chosen forbidden ciphering UEA%d\n",
1688 msg->cipher_mode_complete.utran_encryption);
1689 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT);
1690 rc = 0;
1691 break;
1692 }
1693 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001694 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_COMPL);
1695 rc = 0;
Neels Hofmeyre9a39112019-08-29 00:10:49 +02001696
1697 /* Evaluate enclosed L3 message, typically Identity Response (IMEISV) */
1698 if (msg->cipher_mode_complete.l3_msg) {
1699 unsigned char *data = (unsigned char*)(msg->cipher_mode_complete.l3_msg->val);
1700 uint16_t len = msg->cipher_mode_complete.l3_msg->len;
1701 struct msgb *dtap = msgb_alloc(len, "DTAP from Cipher Mode Complete");
1702 unsigned char *pos = msgb_put(dtap, len);
1703 memcpy(pos, data, len);
1704 dtap->l3h = pos;
1705 rc = msc_a_up_l3(msc_a, dtap);
1706 msgb_free(dtap);
1707 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001708 break;
1709
1710 case RAN_MSG_CIPHER_MODE_REJECT:
1711 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT);
1712 rc = 0;
1713 break;
1714
1715 case RAN_MSG_ASSIGNMENT_COMPLETE:
1716 msc_a_up_call_assignment_complete(msc_a, msg);
1717 rc = 0;
1718 break;
1719
1720 case RAN_MSG_ASSIGNMENT_FAILURE:
1721 msc_a_up_call_assignment_failure(msc_a, msg);
1722 rc = 0;
1723 break;
1724
1725 case RAN_MSG_SAPI_N_REJECT:
1726 msc_a_up_sapi_n_reject(msc_a, msg);
1727 rc = 0;
1728 break;
1729
1730 case RAN_MSG_HANDOVER_PERFORMED:
1731 /* The BSS lets us know that a handover happened within the BSS, which doesn't concern us. */
1732 LOG_MSC_A(msc_a, LOGL_ERROR, "'Handover Performed' handling not implemented\n");
1733 break;
1734
1735 case RAN_MSG_HANDOVER_REQUIRED:
1736 /* The BSS lets us know that it wants to handover to a different cell */
1737 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_HANDOVER_REQUIRED, (void*)&msg->handover_required);
1738 break;
1739
1740 case RAN_MSG_HANDOVER_FAILURE:
1741 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1742 break;
1743
Keith Whytea1a70be2021-05-16 02:59:52 +02001744 case RAN_MSG_LCLS_STATUS:
1745 /* The BSS sends us LCLS_STATUS. We do nothing for now, but it is not an error. */
1746 LOG_MSC_A(msc_a, LOGL_DEBUG, "LCLS_STATUS (%s) received from MSC-I\n",
1747 gsm0808_lcls_status_name(msg->lcls_status.status));
1748 rc = 0;
1749 break;
1750
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001751 default:
1752 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-I not implemented: %s\n", ran_msg_type_name(msg->msg_type));
1753 rc = -ENOTSUP;
1754 break;
1755 }
1756 return rc;
1757}
1758
Andreas Eversbergf7c6f142023-04-23 12:10:42 +02001759static int msc_a_rx_vgcs_bss_decoded(struct osmo_fsm_inst *caller_fi, void *caller_data, const struct ran_msg *msg)
1760{
1761 struct vgcs_bss *bss = caller_data;
1762 struct msc_a *msc_a = (bss->trans) ? bss->trans->msc_a : NULL;
1763 int rc = 0;
1764
1765 switch (msg->msg_type) {
1766 case RAN_MSG_VGCS_VBS_SETUP_ACK:
1767 /* The BSS accepts VGCS/VBS and sends us supported features. */
1768 vgcs_vbs_setup_ack(bss, msg);
1769 break;
1770 case RAN_MSG_VGCS_VBS_SETUP_REFUSE:
1771 /* The BSS refuses VGCS/VBS. */
1772 vgcs_vbs_setup_refuse(bss, msg);
1773 break;
1774 case RAN_MSG_UPLINK_REQUEST:
1775 /* A mobile station requests the uplink on a VGCS channel. */
1776 vgcs_uplink_request(bss, msg);
1777 break;
1778 case RAN_MSG_UPLINK_REQUEST_CNF:
1779 /* The uplink on a VGCS channel has been established. */
1780 vgcs_uplink_request_cnf(bss, msg);
1781 break;
1782 case RAN_MSG_UPLINK_APPLICATION_DATA:
1783 /* Application data received on the uplink of a VGCS channel. */
1784 vgcs_app_data(bss, msg);
1785 break;
1786 case RAN_MSG_DTAP:
1787 /* BSS confirms the release of the channel. */
1788 vgcs_bss_dtap(bss, msg);
1789 break;
1790 case RAN_MSG_UPLINK_RELEASE_IND:
1791 /* A mobile station releases the uplink on a VGCS channel. */
1792 vgcs_uplink_release_ind(bss, msg);
1793 break;
1794 case RAN_MSG_CLEAR_REQUEST:
1795 /* BSS indicated that the channel has been released. */
1796 vgcs_vbs_clear_req(bss, msg);
1797 break;
1798 case RAN_MSG_CLEAR_COMPLETE:
1799 /* BSS confirms the release of the channel. */
1800 vgcs_vbs_clear_cpl(bss, msg);
1801 break;
1802 default:
1803 LOG_MSC_A(msc_a, LOGL_ERROR, "VGCS message from BSS not implemented: %s\n",
1804 ran_msg_type_name(msg->msg_type));
1805 rc = -ENOTSUP;
1806 break;
1807 }
1808 return rc;
1809}
1810
1811int msc_a_rx_vgcs_bss(struct vgcs_bss *bss, struct ran_conn *from_conn, struct msgb *msg)
1812{
1813 struct ran_dec ran_dec;
1814
1815 /* Feed through the decoding mechanism ran_msg. The decoded message arrives in msc_a_rx_vgcs_decoded() */
1816 ran_dec = (struct ran_dec) {
1817 .caller_data = bss,
1818 .decode_cb = msc_a_rx_vgcs_bss_decoded,
1819 };
1820 struct ran_peer *ran_peer = from_conn->ran_peer;
1821 struct ran_infra *ran = ran_peer->sri->ran;
1822 if (!ran->ran_dec_l2) {
1823 LOGP(DMSC, LOGL_ERROR, "No ran_dec_l2() defined for RAN type %s\n",
1824 osmo_rat_type_name(ran->type));
1825 return -ENOTSUP;
1826 }
1827 return ran->ran_dec_l2(&ran_dec, msg);
1828}
1829
1830static int msc_a_rx_vgcs_cell_decoded(struct osmo_fsm_inst *caller_fi, void *caller_data, const struct ran_msg *msg)
1831{
1832 struct vgcs_bss_cell *cell = caller_data;
1833 struct msc_a *msc_a = (cell->bss && cell->bss->trans) ? cell->bss->trans->msc_a : NULL;
1834 int rc = 0;
1835
1836 switch (msg->msg_type) {
1837 case RAN_MSG_VGCS_VBS_ASSIGN_RES:
1838 /* The BSS accepts VGCS/VBS channel assignment. */
1839 vgcs_vbs_assign_result(cell, msg);
1840 break;
1841 case RAN_MSG_VGCS_VBS_ASSIGN_FAIL:
1842 /* The BSS refuses VGCS/VBS channel assignment. */
1843 vgcs_vbs_assign_fail(cell, msg);
1844 break;
1845 case RAN_MSG_VGCS_VBS_QUEUING_IND:
1846 /* The BSS needs more time for VGCS/VBS channel assignment. */
1847 vgcs_vbs_queuing_ind(cell);
1848 break;
1849 case RAN_MSG_VGCS_VBS_ASSIGN_STATUS:
1850 /* The BSS gives cell status about VGCS/VBS channel. */
1851 vgcs_vbs_assign_status(cell, msg);
1852 break;
1853 case RAN_MSG_CLEAR_REQUEST:
1854 /* BSS indicated that the channel has been released. */
1855 vgcs_vbs_clear_req_channel(cell, msg);
1856 break;
1857 case RAN_MSG_CLEAR_COMPLETE:
1858 /* BSS confirms the release of the channel. */
1859 vgcs_vbs_clear_cpl_channel(cell, msg);
1860 break;
1861 default:
1862 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from BSS leg not implemented: %s\n",
1863 ran_msg_type_name(msg->msg_type));
1864 rc = -ENOTSUP;
1865 break;
1866 }
1867 return rc;
1868}
1869
1870int msc_a_rx_vgcs_cell(struct vgcs_bss_cell *cell, struct ran_conn *from_conn, struct msgb *msg)
1871{
1872 struct ran_dec ran_dec;
1873
1874 /* Feed through the decoding mechanism ran_msg. The decoded message arrives in msc_a_rx_vgcs_decoded() */
1875 ran_dec = (struct ran_dec) {
1876 .caller_data = cell,
1877 .decode_cb = msc_a_rx_vgcs_cell_decoded,
1878 };
1879 struct ran_peer *ran_peer = from_conn->ran_peer;
1880 struct ran_infra *ran = ran_peer->sri->ran;
1881 if (!ran->ran_dec_l2) {
1882 LOGP(DMSC, LOGL_ERROR, "No ran_dec_l2() defined for RAN type %s\n",
1883 osmo_rat_type_name(ran->type));
1884 return -ENOTSUP;
1885 }
1886 return ran->ran_dec_l2(&ran_dec, msg);
1887}
1888
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001889static int msc_a_ran_dec_from_msc_t(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1890{
1891 struct msc_t *msc_t = msc_a_msc_t(msc_a);
1892 int rc = -99;
1893
1894 if (!msc_t) {
1895 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx message from MSC-T role, but I have no active MSC-T role.\n");
1896 return -EINVAL;
1897 }
1898
1899 OSMO_ASSERT(d->ran_dec);
1900
1901 switch (d->ran_dec->msg_type) {
1902
1903 case RAN_MSG_CLEAR_REQUEST:
1904 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_MO_CLOSE, NULL);
1905 break;
1906
1907 case RAN_MSG_CLEAR_COMPLETE:
1908 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_CLEAR_COMPLETE, NULL);
1909 break;
1910
1911 case RAN_MSG_CLASSMARK_UPDATE:
1912 msc_a_up_classmark_update(msc_a, d->ran_dec->classmark_update.classmark, &msc_t->classmark);
1913 rc = 0;
1914 break;
1915
1916 case RAN_MSG_HANDOVER_REQUEST_ACK:
1917 /* new BSS accepts Handover */
1918 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_REQUEST_ACK);
1919 break;
1920
1921 case RAN_MSG_HANDOVER_DETECT:
1922 /* new BSS signals the MS is DETECTed on the new lchan */
1923 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_DETECT);
1924 break;
1925
1926 case RAN_MSG_HANDOVER_COMPLETE:
1927 /* new BSS signals the MS has fully moved to the new lchan */
1928 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_COMPLETE);
1929 break;
1930
1931 case RAN_MSG_HANDOVER_FAILURE:
1932 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1933 break;
1934
1935 default:
1936 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-T not implemented: %s\n",
1937 ran_msg_type_name(d->ran_dec->msg_type));
1938 rc = -ENOTSUP;
1939 break;
1940 }
1941 return rc;
1942}
1943
1944int msc_a_ran_decode_cb(struct osmo_fsm_inst *msc_a_fi, void *data, const struct ran_msg *msg)
1945{
1946 struct msc_a *msc_a = msc_a_fi_priv(msc_a_fi);
1947 struct msc_a_ran_dec_data *d = data;
1948 int rc = -99;
1949
1950 d->ran_dec = msg;
1951
1952 switch (d->from_role) {
1953 case MSC_ROLE_I:
1954 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode: %s\n", msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1955 rc = msc_a_ran_dec_from_msc_i(msc_a, d);
1956 break;
1957
1958 case MSC_ROLE_T:
1959 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode from MSC-T: %s\n",
1960 msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1961 rc = msc_a_ran_dec_from_msc_t(msc_a, d);
1962 break;
1963
1964 default:
1965 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from invalid role %s: %s\n", msc_role_name(d->from_role),
1966 ran_msg_type_name(msg->msg_type));
1967 return -ENOTSUP;
1968 }
1969
1970 if (rc)
1971 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),
1972 msc_role_name(d->from_role));
1973 return rc;
1974}
1975
1976/* Your typical DTAP via FORWARD_ACCESS_SIGNALLING_REQUEST */
1977int _msc_a_ran_down(struct msc_a *msc_a, enum msc_role to_role, const struct ran_msg *ran_msg,
1978 const char *file, int line)
1979{
1980 return _msc_a_msg_down(msc_a, to_role, msub_role_to_role_event(msc_a->c.msub, MSC_ROLE_A, to_role),
1981 ran_msg, file, line);
1982}
1983
1984/* To transmit more complex events than just FORWARD_ACCESS_SIGNALLING_REQUEST, e.g. an
1985 * MSC_T_EV_FROM_A_PREPARE_HANDOVER_REQUEST */
1986int _msc_a_msg_down(struct msc_a *msc_a, enum msc_role to_role, uint32_t to_role_event,
1987 const struct ran_msg *ran_msg,
1988 const char *file, int line)
1989{
1990 struct an_apdu an_apdu = {
1991 .an_proto = msc_a->c.ran->an_proto,
1992 .msg = msc_role_ran_encode(msc_a->c.fi, ran_msg),
1993 };
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001994 if (!an_apdu.msg)
1995 return -EIO;
Vadim Yanitskiyc44342b2021-12-07 18:32:35 +03001996 return _msub_role_dispatch(msc_a->c.msub, to_role, to_role_event, &an_apdu, file, line);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001997}
1998
1999int msc_a_tx_dtap_to_i(struct msc_a *msc_a, struct msgb *dtap)
2000{
2001 struct ran_msg ran_msg;
Neels Hofmeyrc192c0b2019-10-07 21:41:18 +02002002 struct gsm48_hdr *gh = msgb_l3(dtap) ? : dtap->data;
2003 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002004
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02002005 if (!msc_a) {
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02002006 LOGP(DMSC, LOGL_ERROR, "Attempt to send DTAP to NULL MSC-A, dropping message: %s %s\n",
2007 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
2008 msgb_free(dtap);
2009 return -EIO;
2010 }
2011
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002012 if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) {
2013 /* The SGs connection to the MME always is at the MSC-A. */
2014 return sgs_iface_tx_dtap_ud(msc_a, dtap);
2015 }
2016
Neels Hofmeyrc192c0b2019-10-07 21:41:18 +02002017 LOG_MSC_A(msc_a, LOGL_DEBUG, "Sending DTAP: %s %s\n",
2018 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
2019
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002020 ran_msg = (struct ran_msg){
2021 .msg_type = RAN_MSG_DTAP,
2022 .dtap = dtap,
2023 };
2024 return msc_a_ran_down(msc_a, MSC_ROLE_I, &ran_msg);
2025}
2026
2027struct msc_a *msc_a_for_vsub(const struct vlr_subscr *vsub, bool valid_conn_only)
2028{
2029 struct msc_a *msc_a = msub_msc_a(msub_for_vsub(vsub));
2030 if (valid_conn_only && !msc_a_is_accepted(msc_a))
2031 return NULL;
2032 return msc_a;
2033}
2034
2035int msc_tx_common_id(struct msc_a *msc_a, enum msc_role to_role)
2036{
2037 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Vadim Yanitskiy435f67f2021-06-06 15:47:49 +02002038 if (vsub == NULL)
2039 return -ENODEV;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002040 struct ran_msg msg = {
2041 .msg_type = RAN_MSG_COMMON_ID,
2042 .common_id = {
2043 .imsi = vsub->imsi,
Pau Espin Pedrol67106702021-04-27 18:20:15 +02002044 .last_eutran_plmn_present = vsub->sgs.last_eutran_plmn_present,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002045 },
2046 };
Pau Espin Pedrol67106702021-04-27 18:20:15 +02002047 if (vsub->sgs.last_eutran_plmn_present) {
2048 memcpy(&msg.common_id.last_eutran_plmn, &vsub->sgs.last_eutran_plmn,
2049 sizeof(vsub->sgs.last_eutran_plmn));
2050 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002051
2052 return msc_a_ran_down(msc_a, to_role, &msg);
2053}
2054
2055static int msc_a_start_assignment(struct msc_a *msc_a, struct gsm_trans *cc_trans)
2056{
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002057 struct call_leg *cl;
2058 bool cn_rtp_available;
2059 bool ran_rtp_available;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002060
2061 OSMO_ASSERT(!msc_a->cc.active_trans);
2062 msc_a->cc.active_trans = cc_trans;
2063
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01002064 cc_trans->cc.codecs.assignment = (struct sdp_audio_codec){};
2065
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002066 OSMO_ASSERT(cc_trans && cc_trans->type == TRANS_CC);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002067 cl = msc_a_ensure_call_leg(msc_a, cc_trans);
2068 if (!cl)
2069 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002070
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002071 /* See if we can set a preliminary codec. If not, pass none for the time being. */
Oliver Smithceca8e62023-05-24 11:15:52 +02002072 trans_cc_filter_run(cc_trans);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002073
2074 cn_rtp_available = call_leg_local_ip(cl, RTP_TO_CN);
2075 ran_rtp_available = call_leg_local_ip(cl, RTP_TO_RAN);
2076
2077 /* Set up RTP ports for both RAN and CN side. Even though we ask for both at the same time, the
2078 * osmo_mgcpc_ep_fsm automagically waits for the first CRCX to complete before firing the second CRCX. The one
2079 * issued first here will also be the first CRCX sent to the MGW. Usually both still need to be set up. */
2080 if (!cn_rtp_available)
Andreas Eversberg712b28e2023-06-21 11:17:26 +02002081 call_leg_ensure_ci(cl, RTP_TO_CN, cc_trans->call_id, cc_trans,
Oliver Smithc63c3a02023-05-24 10:48:07 +02002082 &cc_trans->cc.local.audio_codecs, NULL);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002083 if (!ran_rtp_available) {
2084 struct sdp_audio_codecs *codecs;
2085 if (msc_a->c.ran->force_mgw_codecs_to_ran.count)
2086 codecs = &msc_a->c.ran->force_mgw_codecs_to_ran;
2087 else
Oliver Smithc63c3a02023-05-24 10:48:07 +02002088 codecs = &cc_trans->cc.local.audio_codecs;
Andreas Eversberg712b28e2023-06-21 11:17:26 +02002089 return call_leg_ensure_ci(cl, RTP_TO_RAN, cc_trans->call_id, cc_trans, codecs, NULL);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002090 }
2091
2092 /* Should these already be set up, immediately continue by retriggering the events signalling that the RTP
2093 * ports are available. The ordering is: first CN, then RAN. */
2094 if (cn_rtp_available && ran_rtp_available)
2095 return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_RAN]);
2096 else if (cn_rtp_available)
2097 return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_CN]);
2098 /* Otherwise wait for MGCP response and continue from there. */
2099 return 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002100}
2101
2102int msc_a_try_call_assignment(struct gsm_trans *cc_trans)
2103{
2104 struct msc_a *msc_a = cc_trans->msc_a;
2105 OSMO_ASSERT(cc_trans->type == TRANS_CC);
2106
2107 if (msc_a->cc.active_trans == cc_trans) {
Neels Hofmeyrb4ef5e72019-08-30 01:11:12 +02002108 LOG_MSC_A(msc_a, LOGL_DEBUG, "Assignment for this trans already started earlier\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002109 return 0;
2110 }
2111
2112 if (msc_a->cc.active_trans) {
2113 LOG_MSC_A(msc_a, LOGL_INFO, "Another call is already ongoing, not assigning yet\n");
2114 return 0;
2115 }
2116
2117 LOG_MSC_A(msc_a, LOGL_DEBUG, "Starting call assignment\n");
2118 return msc_a_start_assignment(msc_a, cc_trans);
2119}
2120
Neels Hofmeyr5d53c602022-04-24 23:37:07 +02002121/* Map CM Service type to use token.
2122 * Given a CM Service type, return a matching token intended for osmo_use_count.
2123 * For unknown service type, return NULL.
2124 */
Andreas Eversbergcd8bd452023-07-11 14:16:24 +02002125const 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 +01002126{
Andreas Eversbergcd8bd452023-07-11 14:16:24 +02002127 struct gsm_network *net = msc_a_net(msc_a);
2128
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002129 switch (cm_service_type) {
2130 case GSM48_CMSERV_MO_CALL_PACKET:
2131 case GSM48_CMSERV_EMERGENCY:
2132 return MSC_A_USE_CM_SERVICE_CC;
2133
2134 case GSM48_CMSERV_SMS:
2135 return MSC_A_USE_CM_SERVICE_SMS;
2136
2137 case GSM48_CMSERV_SUP_SERV:
2138 return MSC_A_USE_CM_SERVICE_SS;
2139
Andreas Eversberg456c6f72023-04-23 11:54:16 +02002140 case GSM48_CMSERV_VGCS:
Andreas Eversbergcd8bd452023-07-11 14:16:24 +02002141 if (net->asci.enable)
2142 return MSC_A_USE_CM_SERVICE_GCC;
2143 else
2144 return NULL;
Andreas Eversberg456c6f72023-04-23 11:54:16 +02002145
2146 case GSM48_CMSERV_VBS:
Andreas Eversbergcd8bd452023-07-11 14:16:24 +02002147 if (net->asci.enable)
2148 return MSC_A_USE_CM_SERVICE_BCC;
2149 else
2150 return NULL;
Andreas Eversberg456c6f72023-04-23 11:54:16 +02002151
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002152 default:
2153 return NULL;
2154 }
2155}
2156
2157void msc_a_release_cn(struct msc_a *msc_a)
2158{
2159 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CN_CLOSE, NULL);
2160}
2161
2162void msc_a_release_mo(struct msc_a *msc_a, enum gsm48_gsm_cause gsm_cause)
2163{
2164 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
2165}