blob: 70d9bf74f696903d805d4f88eb42f9e13da786aa [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. */
Neels Hofmeyrd767c732023-11-17 04:12:29 +0100639void msc_a_tx_assignment_cmd(struct msc_a *msc_a)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100640{
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;
Manawyrm1ed12ea2023-10-14 17:23:04 +0200672 case GSM48_BCAP_ITCAP_3k1_AUDIO:
673 case GSM48_BCAP_ITCAP_FAX_G3:
Oliver Smith10632132023-05-12 12:14:22 +0200674 case GSM48_BCAP_ITCAP_UNR_DIG_INF:
675 if (!cc_trans->cc.local.bearer_services.count) {
676 LOG_TRANS(cc_trans, LOGL_ERROR, "Assignment not possible, no matching bearer service: %s\n",
677 csd_filter_to_str(&cc_trans->cc.csd, &cc_trans->cc.local, &cc_trans->cc.remote));
678 call_leg_release(msc_a->cc.call_leg);
679 return;
680 }
681
682 /* Compose 48.008 Channel Type from the current set of bearer
683 * services determined from local and remote capabilities. */
684 if (csd_bs_list_to_gsm0808_channel_type(&channel_type, &cc_trans->cc.local.bearer_services)) {
685 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot compose channel type from: %s\n",
686 csd_filter_to_str(&cc_trans->cc.csd, &cc_trans->cc.local, &cc_trans->cc.remote));
687 return;
688 }
689 break;
690 default:
691 LOG_TRANS(cc_trans, LOGL_ERROR, "Assignment not possible for information transfer capability %d\n",
692 cc_trans->bearer_cap.transfer);
Neels Hofmeyr11a746a2023-01-26 15:00:06 +0100693 call_leg_release(msc_a->cc.call_leg);
694 return;
695 }
696
Oliver Smith10632132023-05-12 12:14:22 +0200697 /* The RAN side RTP address is known, so the voice/CSD Assignment can commence. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100698 msg = (struct ran_msg){
699 .msg_type = RAN_MSG_ASSIGNMENT_COMMAND,
700 .assignment_command = {
701 .cn_rtp = &msc_a->cc.call_leg->rtp[RTP_TO_RAN]->local,
702 .channel_type = &channel_type,
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200703 .osmux_present = msc_a->cc.call_leg->rtp[RTP_TO_RAN]->use_osmux,
704 .osmux_cid = msc_a->cc.call_leg->rtp[RTP_TO_RAN]->local_osmux_cid,
Philipp Maierf34d9452020-06-05 15:49:35 +0200705 .call_id_present = true,
Andreas Eversberg712b28e2023-06-21 11:17:26 +0200706 .call_id = cc_trans->call_id,
Keith Whytea1a70be2021-05-16 02:59:52 +0200707 .lcls = cc_trans->cc.lcls,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100708 },
709 };
710 if (msc_a_ran_down(msc_a, MSC_ROLE_I, &msg)) {
711 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot send Assignment\n");
Neels Hofmeyrf439ff12019-10-05 04:19:36 +0200712 trans_free(cc_trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100713 return;
714 }
715}
716
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100717static struct gsm_trans *find_waiting_call(struct msc_a *msc_a)
718{
719 struct gsm_trans *trans;
720 struct gsm_network *net = msc_a_net(msc_a);
721
722 llist_for_each_entry(trans, &net->trans_list, entry) {
723 if (trans->msc_a != msc_a)
724 continue;
725 if (trans->type != TRANS_CC)
726 continue;
727 if (trans->msc_a->cc.active_trans == trans)
728 continue;
729 return trans;
730 }
731 return NULL;
732}
733
734static void msc_a_cleanup_rtp_streams(struct msc_a *msc_a, uint32_t event, void *data)
735{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100736 switch (event) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100737
738 case MSC_EV_CALL_LEG_TERM:
739 msc_a->cc.call_leg = NULL;
740 if (msc_a->cc.mncc_forwarding_to_remote_ran)
741 msc_a->cc.mncc_forwarding_to_remote_ran->rtps = NULL;
742
Neels Hofmeyr265a4c72019-05-09 16:20:51 +0200743 if (msc_a->ho.new_cell.mncc_forwarding_to_remote_ran)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100744 msc_a->ho.new_cell.mncc_forwarding_to_remote_ran->rtps = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100745 return;
746
747 case MSC_MNCC_EV_CALL_ENDED:
748 msc_a->cc.mncc_forwarding_to_remote_ran = NULL;
749 return;
750
751 default:
752 return;
753 }
754}
755
756static void msc_a_fsm_communicating(struct osmo_fsm_inst *fi, uint32_t event, void *data)
757{
758 struct msc_a *msc_a = fi->priv;
759 struct rtp_stream *rtps;
760 struct gsm_trans *waiting_trans;
761 struct an_apdu *an_apdu;
762
763 msc_a_cleanup_rtp_streams(msc_a, event, data);
764
765 switch (event) {
766 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
767 case MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST:
768 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
769 an_apdu = data;
770 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_I);
771 return;
772
773 case MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE:
774 case MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE:
775 case MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST:
776 case MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST:
777 an_apdu = data;
778 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_T);
779 return;
780
781 case MSC_A_EV_TRANSACTION_ACCEPTED:
782 /* no-op */
783 return;
784
785 case MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE:
786 rtps = data;
787 if (!rtps) {
788 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid data for %s\n", osmo_fsm_event_name(fi->fsm, event));
789 return;
790 }
Neels Hofmeyr00a476b2019-11-28 02:46:05 +0100791 if (!msc_a->cc.call_leg) {
792 LOG_MSC_A(msc_a, LOGL_ERROR, "No call leg active\n");
793 return;
794 }
Neels Hofmeyrcc918cb2019-11-28 02:16:34 +0100795 if (!osmo_sockaddr_str_is_nonzero(&rtps->local)) {
796 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid RTP address received from MGW: " OSMO_SOCKADDR_STR_FMT "\n",
797 OSMO_SOCKADDR_STR_FMT_ARGS(&rtps->local));
798 call_leg_release(msc_a->cc.call_leg);
799 return;
800 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100801 LOG_MSC_A(msc_a, LOGL_DEBUG,
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200802 "MGW endpoint's RTP address available for the CI %s: " OSMO_SOCKADDR_STR_FMT " (osmux=%s:%d)\n",
803 rtp_direction_name(rtps->dir), OSMO_SOCKADDR_STR_FMT_ARGS(&rtps->local),
804 rtps->use_osmux ? "yes" : "no", rtps->local_osmux_cid);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100805 switch (rtps->dir) {
806 case RTP_TO_RAN:
Neels Hofmeyrd767c732023-11-17 04:12:29 +0100807 msc_a_tx_assignment_cmd(msc_a);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100808 return;
809 case RTP_TO_CN:
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100810 cc_on_cn_local_rtp_port_known(rtps->for_trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100811 return;
812 default:
813 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid data for %s\n", osmo_fsm_event_name(fi->fsm, event));
814 return;
815 }
816
817 case MSC_EV_CALL_LEG_RTP_COMPLETE:
818 /* Nothing to do. */
819 return;
820
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100821 case MSC_MNCC_EV_CALL_ENDED:
822 /* Cleaned up above */
823 return;
824
825 case MSC_EV_CALL_LEG_TERM:
826 /* RTP streams cleaned up above */
827
828 msc_a_get(msc_a, __func__);
829 if (msc_a->cc.active_trans)
830 trans_free(msc_a->cc.active_trans);
831
832 /* If there is another call still waiting to be activated, this is the time when the mgcp_ctx is
833 * available again and the other call can start assigning. */
834 waiting_trans = find_waiting_call(msc_a);
835 if (waiting_trans) {
836 LOG_MSC_A(msc_a, LOGL_DEBUG, "(ti %02x) Call waiting: starting Assignment\n",
837 waiting_trans->transaction_id);
838 msc_a_try_call_assignment(waiting_trans);
839 }
840 msc_a_put(msc_a, __func__);
841 return;
842
843 case MSC_A_EV_HANDOVER_REQUIRED:
844 msc_ho_start(msc_a, (struct ran_handover_required*)data);
845 return;
846
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200847 case MSC_A_EV_HANDOVER_END:
848 /* Termination event of the msc_ho_fsm. No action needed, it's all done in the msc_ho_fsm cleanup. This
849 * event only exists because osmo_fsm_inst_alloc_child() requires a parent term event; and maybe
850 * interesting for logging. */
851 return;
852
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100853 case MSC_A_EV_MO_CLOSE:
854 case MSC_A_EV_CN_CLOSE:
855 case MSC_A_EV_UNUSED:
856 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
857 return;
858
859 default:
860 OSMO_ASSERT(false);
861 }
862}
863
864static int msc_a_fsm_timer_cb(struct osmo_fsm_inst *fi)
865{
866 struct msc_a *msc_a = fi->priv;
867 if (msc_a_in_release(msc_a)) {
868 LOG_MSC_A(msc_a, LOGL_ERROR, "Timeout while releasing, discarding right now\n");
869 msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
870 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
871 } else {
872 enum gsm48_reject_value cause = GSM48_REJECT_CONGESTION;
873 osmo_fsm_inst_dispatch(fi, MSC_A_EV_CN_CLOSE, &cause);
874 }
875 return 0;
876}
877
878static void msc_a_fsm_releasing_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
879{
880 struct msc_a *msc_a = fi->priv;
881 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
882 int i;
883 char buf[128];
884 const char * const use_counts_to_cancel[] = {
885 MSC_A_USE_LOCATION_UPDATING,
886 MSC_A_USE_CM_SERVICE_CC,
887 MSC_A_USE_CM_SERVICE_SMS,
888 MSC_A_USE_CM_SERVICE_SS,
Andreas Eversberg456c6f72023-04-23 11:54:16 +0200889 MSC_A_USE_CM_SERVICE_GCC,
890 MSC_A_USE_CM_SERVICE_BCC,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100891 MSC_A_USE_PAGING_RESPONSE,
892 };
893
894 LOG_MSC_A(msc_a, LOGL_DEBUG, "Releasing: msc_a use is %s\n",
895 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
896
897 if (vsub) {
898 vlr_subscr_get(vsub, __func__);
899
900 /* Cancel all VLR FSMs, if any */
901 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
902
903 /* The subscriber has no active connection anymore.
904 * Restart the periodic Location Update expiry timer for this subscriber. */
905 vlr_subscr_enable_expire_lu(vsub);
906 }
907
908 /* If we're closing in a middle of a trans, we need to clean up */
909 trans_conn_closed(msc_a);
910
911 call_leg_release(msc_a->cc.call_leg);
912
913 /* Cancel use counts for pending CM Service / Paging */
914 for (i = 0; i < ARRAY_SIZE(use_counts_to_cancel); i++) {
915 const char *use = use_counts_to_cancel[i];
916 int32_t count = osmo_use_count_by(&msc_a->use_count, use);
917 if (!count)
918 continue;
919 LOG_MSC_A(msc_a, LOGL_DEBUG, "Releasing: canceling still pending use: %s (%d)\n", use, count);
920 osmo_use_count_get_put(&msc_a->use_count, use, -count);
921 }
922
923 if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) {
924 sgs_iface_tx_release(vsub);
925 /* In SGsAP there is no confirmation of a release. */
926 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
927 } else {
928 struct ran_msg msg = {
929 .msg_type = RAN_MSG_CLEAR_COMMAND,
930 .clear_command = {
Neels Hofmeyrd9fe7112020-07-11 00:20:20 +0200931 /* "Call Control" is the only cause code listed in 3GPP TS 48.008 3.2.1.21 CLEAR COMMAND
932 * that qualifies for a normal release situation. (OS#4664) */
933 .gsm0808_cause = GSM0808_CAUSE_CALL_CONTROL,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100934 .csfb_ind = (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED),
935 },
936 };
937 msc_a_get(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
938 msc_a_ran_down(msc_a, MSC_ROLE_I, &msg);
Philipp Maier47cf84d2019-08-15 14:56:54 +0200939
940 /* The connection is cleared. The MS will now go back to 4G,
941 Switch the RAN type back to SGS. */
942 if (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED)
943 vsub->cs.attached_via_ran = OSMO_RAT_EUTRAN_SGS;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100944 }
945
946 if (vsub)
947 vlr_subscr_put(vsub, __func__);
948}
949
950static void msc_a_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
951{
952 struct msc_a *msc_a = fi->priv;
953
954 msc_a_cleanup_rtp_streams(msc_a, event, data);
955
956 switch (event) {
957 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
958 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
959 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
960 return;
961
962 case MSC_A_EV_MO_CLOSE:
963 case MSC_A_EV_CN_CLOSE:
964 case MSC_A_EV_UNUSED:
965 /* Already releasing */
966 return;
967
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100968 case MSC_EV_CALL_LEG_TERM:
969 case MSC_MNCC_EV_CALL_ENDED:
970 /* RTP streams cleaned up above */
971 return;
972
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200973 case MSC_A_EV_HANDOVER_END:
974 /* msc_ho_fsm does cleanup. */
975 return;
976
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100977 default:
978 OSMO_ASSERT(false);
979 }
980}
981
982
983static void msc_a_fsm_released_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
984{
985 struct msc_a *msc_a = msc_a_fi_priv(fi);
986 char buf[128];
987 LOG_MSC_A(msc_a, LOGL_DEBUG, "Released: msc_a use is %s\n",
988 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
989 if (osmo_use_count_total(&msc_a->use_count) == 0)
990 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi);
991}
992
993static void msc_a_fsm_released(struct osmo_fsm_inst *fi, uint32_t event, void *data)
994{
995 if (event == MSC_A_EV_UNUSED)
996 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi);
997}
998
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100999void msc_a_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
1000{
1001 struct msc_a *msc_a = msc_a_fi_priv(fi);
Neels Hofmeyr72ef7d82024-03-26 01:43:53 +01001002 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001003
1004 trans_conn_closed(msc_a);
1005
1006 if (msc_a_fsm_has_active_transactions(fi))
1007 LOG_MSC_A(msc_a, LOGL_ERROR, "Deallocating active transactions failed\n");
1008
1009 LOG_MSC_A_CAT(msc_a, DREF, LOGL_DEBUG, "max total use count was %d\n", msc_a->max_total_use_count);
Neels Hofmeyr72ef7d82024-03-26 01:43:53 +01001010
1011 /* Invalidate the active conn in VLR subscriber state, if any. */
1012 if (vsub && vsub->msc_conn_ref == msc_a)
1013 vsub->msc_conn_ref = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001014}
1015
1016const struct value_string msc_a_fsm_event_names[] = {
1017 OSMO_VALUE_STRING(MSC_REMOTE_EV_RX_GSUP),
1018 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE),
1019 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_COMPLETE),
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001020 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_TERM),
1021 OSMO_VALUE_STRING(MSC_MNCC_EV_NEED_LOCAL_RTP),
1022 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_PROCEEDING),
1023 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_COMPLETE),
1024 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_ENDED),
1025 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_COMPLETE_LAYER_3),
1026 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST),
1027 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST),
1028 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST),
1029 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST),
1030 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE),
1031 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE),
1032 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST),
1033 OSMO_VALUE_STRING(MSC_A_EV_COMPLETE_LAYER_3_OK),
1034 OSMO_VALUE_STRING(MSC_A_EV_CLASSMARK_UPDATE),
1035 OSMO_VALUE_STRING(MSC_A_EV_AUTHENTICATED),
1036 OSMO_VALUE_STRING(MSC_A_EV_TRANSACTION_ACCEPTED),
1037 OSMO_VALUE_STRING(MSC_A_EV_CN_CLOSE),
1038 OSMO_VALUE_STRING(MSC_A_EV_MO_CLOSE),
1039 OSMO_VALUE_STRING(MSC_A_EV_UNUSED),
1040 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_REQUIRED),
1041 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_END),
1042 {}
1043};
1044
1045#define S(x) (1 << (x))
1046
1047static const struct osmo_fsm_state msc_a_fsm_states[] = {
1048 [MSC_A_ST_VALIDATE_L3] = {
1049 .name = OSMO_STRINGIFY(MSC_A_ST_VALIDATE_L3),
1050 .in_event_mask = 0
1051 | S(MSC_A_EV_FROM_I_COMPLETE_LAYER_3)
1052 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1053 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1054 | S(MSC_A_EV_COMPLETE_LAYER_3_OK)
1055 | S(MSC_A_EV_MO_CLOSE)
1056 | S(MSC_A_EV_CN_CLOSE)
1057 | S(MSC_A_EV_UNUSED)
1058 ,
1059 .out_state_mask = 0
1060 | S(MSC_A_ST_VALIDATE_L3)
1061 | S(MSC_A_ST_AUTH_CIPH)
1062 | S(MSC_A_ST_RELEASING)
1063 ,
1064 .action = msc_a_fsm_validate_l3,
1065 },
1066 [MSC_A_ST_AUTH_CIPH] = {
1067 .name = OSMO_STRINGIFY(MSC_A_ST_AUTH_CIPH),
1068 .in_event_mask = 0
1069 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1070 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1071 | S(MSC_A_EV_AUTHENTICATED)
1072 | S(MSC_A_EV_MO_CLOSE)
1073 | S(MSC_A_EV_CN_CLOSE)
1074 | S(MSC_A_EV_UNUSED)
1075 ,
1076 .out_state_mask = 0
1077 | S(MSC_A_ST_WAIT_CLASSMARK_UPDATE)
1078 | S(MSC_A_ST_AUTHENTICATED)
1079 | S(MSC_A_ST_RELEASING)
1080 ,
1081 .action = msc_a_fsm_auth_ciph,
1082 },
1083 [MSC_A_ST_WAIT_CLASSMARK_UPDATE] = {
1084 .name = OSMO_STRINGIFY(MSC_A_ST_WAIT_CLASSMARK_UPDATE),
1085 .in_event_mask = 0
1086 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1087 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1088 | S(MSC_A_EV_CLASSMARK_UPDATE)
1089 | S(MSC_A_EV_MO_CLOSE)
1090 | S(MSC_A_EV_CN_CLOSE)
1091 ,
1092 .out_state_mask = 0
1093 | S(MSC_A_ST_AUTH_CIPH)
1094 | S(MSC_A_ST_RELEASING)
1095 ,
1096 .action = msc_a_fsm_wait_classmark_update,
1097 },
1098 [MSC_A_ST_AUTHENTICATED] = {
1099 .name = OSMO_STRINGIFY(MSC_A_ST_AUTHENTICATED),
1100 /* allow everything to release for any odd behavior */
1101 .in_event_mask = 0
1102 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1103 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
1104 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1105 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
1106 | S(MSC_A_EV_MO_CLOSE)
1107 | S(MSC_A_EV_CN_CLOSE)
1108 | S(MSC_A_EV_UNUSED)
1109 ,
1110 .out_state_mask = 0
1111 | S(MSC_A_ST_RELEASING)
1112 | S(MSC_A_ST_COMMUNICATING)
1113 ,
1114 .onenter = msc_a_fsm_authenticated_enter,
1115 .action = msc_a_fsm_authenticated,
1116 },
1117 [MSC_A_ST_COMMUNICATING] = {
1118 .name = OSMO_STRINGIFY(MSC_A_ST_COMMUNICATING),
1119 /* allow everything to release for any odd behavior */
1120 .in_event_mask = 0
1121 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1122 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
1123 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1124 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE)
1125 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE)
1126 | S(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST)
1127 | S(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST)
1128 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
1129 | S(MSC_A_EV_MO_CLOSE)
1130 | S(MSC_A_EV_CN_CLOSE)
1131 | S(MSC_A_EV_UNUSED)
1132 | S(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE)
1133 | S(MSC_EV_CALL_LEG_RTP_COMPLETE)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001134 | S(MSC_EV_CALL_LEG_TERM)
1135 | S(MSC_MNCC_EV_CALL_ENDED)
1136 | S(MSC_A_EV_HANDOVER_REQUIRED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +02001137 | S(MSC_A_EV_HANDOVER_END)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001138 ,
1139 .out_state_mask = 0
1140 | S(MSC_A_ST_RELEASING)
1141 ,
1142 .action = msc_a_fsm_communicating,
1143 },
1144 [MSC_A_ST_RELEASING] = {
1145 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASING),
1146 .in_event_mask = 0
1147 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1148 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1149 | S(MSC_A_EV_UNUSED)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001150 | S(MSC_EV_CALL_LEG_TERM)
1151 | S(MSC_MNCC_EV_CALL_ENDED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +02001152 | S(MSC_A_EV_HANDOVER_END)
Pau Espin Pedrole53ecde2021-07-12 13:37:24 +02001153 | S(MSC_A_EV_CN_CLOSE)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001154 ,
1155 .out_state_mask = 0
1156 | S(MSC_A_ST_RELEASED)
1157 ,
1158 .onenter = msc_a_fsm_releasing_onenter,
1159 .action = msc_a_fsm_releasing,
1160 },
1161 [MSC_A_ST_RELEASED] = {
1162 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASED),
1163 .in_event_mask = 0
1164 | S(MSC_A_EV_UNUSED)
1165 ,
1166 .onenter = msc_a_fsm_released_onenter,
1167 .action = msc_a_fsm_released,
1168 },
1169};
1170
1171static struct osmo_fsm msc_a_fsm = {
1172 .name = "msc_a",
1173 .states = msc_a_fsm_states,
1174 .num_states = ARRAY_SIZE(msc_a_fsm_states),
1175 .log_subsys = DMSC,
1176 .event_names = msc_a_fsm_event_names,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001177 .timer_cb = msc_a_fsm_timer_cb,
1178 .cleanup = msc_a_fsm_cleanup,
1179};
1180
1181static __attribute__((constructor)) void msc_a_fsm_init()
1182{
1183 OSMO_ASSERT(osmo_fsm_register(&msc_a_fsm) == 0);
1184}
1185
1186static int msc_a_use_cb(struct osmo_use_count_entry *e, int32_t old_use_count, const char *file, int line)
1187{
1188 struct msc_a *msc_a = e->use_count->talloc_object;
1189 char buf[128];
1190 int32_t total;
1191 int level;
1192
1193 if (!e->use)
1194 return -EINVAL;
1195
1196 total = osmo_use_count_total(&msc_a->use_count);
1197
1198 if (total == 0
1199 || (total == 1 && old_use_count == 0 && e->count == 1))
1200 level = LOGL_INFO;
1201 else
1202 level = LOGL_DEBUG;
1203
1204 LOG_MSC_A_CAT_SRC(msc_a, DREF, level, file, line, "%s %s: now used by %s\n",
1205 (e->count - old_use_count) > 0? "+" : "-", e->use,
1206 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
1207
1208 if (e->count < 0)
1209 return -ERANGE;
1210
1211 msc_a->max_total_use_count = OSMO_MAX(msc_a->max_total_use_count, total);
1212
1213 if (total == 0)
1214 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_UNUSED, NULL);
1215 return 0;
1216}
1217
1218struct msc_a *msc_a_alloc(struct msub *msub, struct ran_infra *ran)
1219{
1220 struct msc_a *msc_a = msub_role_alloc(msub, MSC_ROLE_A, &msc_a_fsm, struct msc_a, ran);
1221 msc_a->use_count = (struct osmo_use_count){
1222 .talloc_object = msc_a,
1223 .use_cb = msc_a_use_cb,
1224 };
1225 osmo_use_count_make_static_entries(&msc_a->use_count, msc_a->use_count_buf, ARRAY_SIZE(msc_a->use_count_buf));
1226 /* Start timeout for first state */
Neels Hofmeyr01653252019-09-03 02:06:22 +02001227 msc_a_state_chg_always(msc_a, MSC_A_ST_VALIDATE_L3);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001228 return msc_a;
1229}
1230
1231bool msc_a_is_establishing_auth_ciph(const struct msc_a *msc_a)
1232{
1233 if (!msc_a || !msc_a->c.fi)
1234 return false;
1235 return msc_a->c.fi->state == MSC_A_ST_AUTH_CIPH;
1236}
1237
1238const struct value_string complete_layer3_type_names[] = {
1239 { COMPLETE_LAYER3_NONE, "NONE" },
1240 { COMPLETE_LAYER3_LU, "LU" },
1241 { COMPLETE_LAYER3_CM_SERVICE_REQ, "CM_SERVICE_REQ" },
1242 { COMPLETE_LAYER3_PAGING_RESP, "PAGING_RESP" },
Neels Hofmeyrae98b972021-07-27 03:46:49 +02001243 { COMPLETE_LAYER3_CM_RE_ESTABLISH_REQ, "CM_RE_ESTABLISH_REQ" },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001244 { 0, NULL }
1245};
1246
1247#define _msc_a_update_id(MSC_A, FMT, ARGS ...) \
1248 do { \
1249 if (osmo_fsm_inst_update_id_f(msc_a->c.fi, FMT ":%s:%s", \
1250 ## ARGS, \
1251 msub_ran_conn_name(msc_a->c.msub), \
1252 complete_layer3_type_name(msc_a->complete_layer3_type)) \
1253 == 0) { \
1254 struct vlr_subscr *_vsub = msc_a_vsub(MSC_A); \
1255 if (_vsub) { \
1256 if (_vsub->lu_fsm) \
1257 osmo_fsm_inst_update_id(_vsub->lu_fsm, (MSC_A)->c.fi->id); \
1258 if (_vsub->auth_fsm) \
1259 osmo_fsm_inst_update_id(_vsub->auth_fsm, (MSC_A)->c.fi->id); \
1260 if (_vsub->proc_arq_fsm) \
1261 osmo_fsm_inst_update_id(_vsub->proc_arq_fsm, (MSC_A)->c.fi->id); \
1262 } \
1263 LOG_MSC_A(MSC_A, LOGL_DEBUG, "Updated ID\n"); \
1264 } \
1265 /* otherwise osmo_fsm_inst_update_id_f() will log an error. */ \
1266 } while (0)
1267
1268
1269/* 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 +02001270void msc_a_update_id_from_mi(struct msc_a *msc_a, const struct osmo_mobile_identity *mi)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001271{
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001272 _msc_a_update_id(msc_a, "%s", osmo_mobile_identity_to_str_c(OTC_SELECT, mi));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001273}
1274
1275/* Update msc_a->fi id string from current msc_a->vsub and msc_a->complete_layer3_type. */
1276void msc_a_update_id(struct msc_a *msc_a)
1277{
1278 _msc_a_update_id(msc_a, "%s", vlr_subscr_name(msc_a_vsub(msc_a)));
1279}
1280
1281/* Iterate all msc_a instances that are relevant for this subscriber, and update FSM ID strings for all of the FSM
1282 * instances. */
1283void msc_a_update_id_for_vsub(struct vlr_subscr *for_vsub)
1284{
1285 struct msub *msub;
1286 llist_for_each_entry(msub, &msub_list, entry) {
1287 struct vlr_subscr *vsub = msub_vsub(msub);
1288 if (vsub != for_vsub)
1289 continue;
1290 msc_a_update_id(msub_msc_a(msub));
1291 }
1292}
1293
1294static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
1295{
1296 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
1297 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
1298
1299 switch (pdisc) {
1300 case GSM48_PDISC_MM:
1301 switch (msg_type) {
1302 case GSM48_MT_MM_LOC_UPD_REQUEST:
1303 case GSM48_MT_MM_CM_SERV_REQ:
1304 case GSM48_MT_MM_CM_REEST_REQ:
1305 case GSM48_MT_MM_AUTH_RESP:
1306 case GSM48_MT_MM_AUTH_FAIL:
1307 case GSM48_MT_MM_ID_RESP:
1308 case GSM48_MT_MM_TMSI_REALL_COMPL:
1309 case GSM48_MT_MM_IMSI_DETACH_IND:
1310 return true;
1311 default:
1312 break;
1313 }
1314 break;
1315 case GSM48_PDISC_RR:
1316 switch (msg_type) {
1317 /* GSM48_MT_RR_CIPH_M_COMPL is actually handled in bssmap_rx_ciph_compl() and gets redirected in the
1318 * BSSAP layer to ran_conn_cipher_mode_compl() (before this here is reached) */
1319 case GSM48_MT_RR_PAG_RESP:
1320 case GSM48_MT_RR_CIPH_M_COMPL:
1321 return true;
1322 default:
1323 break;
1324 }
1325 break;
1326 default:
1327 break;
1328 }
1329
1330 return false;
1331}
1332
1333/* Main entry point for GSM 04.08/44.008 Layer 3 data (e.g. from the BSC). */
1334int msc_a_up_l3(struct msc_a *msc_a, struct msgb *msg)
1335{
1336 struct gsm48_hdr *gh;
1337 uint8_t pdisc;
1338 int rc;
1339 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1340 int is_r99;
1341
1342 OSMO_ASSERT(msg->l3h);
1343 OSMO_ASSERT(msg);
1344
1345 gh = msgb_l3(msg);
1346 pdisc = gsm48_hdr_pdisc(gh);
1347
1348 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_DEBUG, "Dispatching 04.08 message: %s %s\n",
1349 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1350
1351 /* To evaluate the 3GPP TS 24.007 Duplicate Detection, we need Classmark information on whether the MS is R99
1352 * capable. If the subscriber is already actively connected, the Classmark information is stored with the
1353 * vlr_subscr. Otherwise, this *must* be a Complete Layer 3 with Classmark info. */
1354 if (vsub)
1355 is_r99 = osmo_gsm48_classmark_is_r99(&vsub->classmark) ? 1 : 0;
1356 else
1357 is_r99 = compl_l3_msg_is_r99(msg);
1358
1359 if (is_r99 < 0) {
1360 LOG_MSC_A(msc_a, LOGL_ERROR,
1361 "No Classmark Information, dropping non-Complete-Layer3 message: %s\n",
1362 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1363 return -EACCES;
1364 }
1365
1366 if (is_r99 >= 0
1367 && ran_dec_dtap_undup_is_duplicate(msc_a->c.fi, msc_a->n_sd_next, is_r99 ? true : false, msg)) {
1368 LOG_MSC_A(msc_a, LOGL_DEBUG, "Dropping duplicate message"
1369 " (3GPP TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection)\n");
1370 return 0;
1371 }
1372
1373 if (!msc_a_is_accepted(msc_a)
1374 && !msg_is_initially_permitted(gh)) {
1375 LOG_MSC_A(msc_a, LOGL_ERROR,
1376 "Message not permitted for initial conn: %s\n",
1377 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1378 return -EACCES;
1379 }
1380
1381 if (vsub && vsub->cs.attached_via_ran != msc_a->c.ran->type) {
1382 LOG_MSC_A(msc_a, LOGL_ERROR,
1383 "Illegal situation: RAN type mismatch:"
1384 " attached via %s, received message via %s\n",
1385 osmo_rat_type_name(vsub->cs.attached_via_ran),
1386 osmo_rat_type_name(msc_a->c.ran->type));
1387 return -EACCES;
1388 }
1389
1390#if 0
1391 if (silent_call_reroute(conn, msg))
1392 return silent_call_rx(conn, msg);
1393#endif
1394
1395 switch (pdisc) {
Andreas Eversberg23b53e52023-06-21 13:30:58 +02001396 case GSM48_PDISC_GROUP_CC:
1397 case GSM48_PDISC_BCAST_CC:
1398 rc = gsm44068_rcv_bcc_gcc(msc_a, NULL, msg);
1399 break;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001400 case GSM48_PDISC_CC:
1401 rc = gsm0408_rcv_cc(msc_a, msg);
1402 break;
1403 case GSM48_PDISC_MM:
1404 rc = gsm0408_rcv_mm(msc_a, msg);
1405 break;
1406 case GSM48_PDISC_RR:
1407 rc = gsm0408_rcv_rr(msc_a, msg);
1408 break;
1409 case GSM48_PDISC_SMS:
1410 rc = gsm0411_rcv_sms(msc_a, msg);
1411 break;
1412 case GSM48_PDISC_MM_GPRS:
1413 case GSM48_PDISC_SM_GPRS:
1414 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unimplemented "
1415 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1416 rc = -ENOTSUP;
1417 break;
1418 case GSM48_PDISC_NC_SS:
1419 rc = gsm0911_rcv_nc_ss(msc_a, msg);
1420 break;
1421 case GSM48_PDISC_TEST:
1422 rc = gsm0414_rcv_test(msc_a, msg);
1423 break;
1424 default:
1425 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unknown "
1426 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1427 rc = -EINVAL;
1428 break;
1429 }
1430
1431 return rc;
1432}
1433
1434static void msc_a_up_call_assignment_complete(struct msc_a *msc_a, const struct ran_msg *ac)
1435{
Andreas Eversberg23b53e52023-06-21 13:30:58 +02001436 struct gsm_trans *cc_trans = msc_a->cc.active_trans, *gcc_trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001437 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 +01001438 const struct gsm0808_speech_codec *codec_if_known = ac->assignment_complete.codec_present ?
1439 &ac->assignment_complete.codec : NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001440
Andreas Eversberg23b53e52023-06-21 13:30:58 +02001441 /* For a voice group call, handling is performed by VGCS FSM */
1442 gcc_trans = trans_find_by_type(msc_a, TRANS_GCC);
1443 if (gcc_trans) {
1444 vgcs_vbs_caller_assign_cpl(gcc_trans);
1445 return;
1446 }
1447 gcc_trans = trans_find_by_type(msc_a, TRANS_BCC);
1448 if (gcc_trans) {
1449 vgcs_vbs_caller_assign_cpl(gcc_trans);
1450 return;
1451 }
1452
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001453 if (!rtps_to_ran) {
1454 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but no RTP stream is set up\n");
1455 return;
1456 }
1457 if (!cc_trans) {
Neels Hofmeyr550506a2022-01-13 23:31:57 +01001458 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but no CC transaction is active\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001459 return;
1460 }
1461
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001462 if (rtps_to_ran->use_osmux != ac->assignment_complete.osmux_present) {
1463 LOG_MSC_A_CAT(msc_a, DCC, LOGL_ERROR, "Osmux usage ass request and complete don't match: %d vs %d\n",
1464 rtps_to_ran->use_osmux, ac->assignment_complete.osmux_present);
1465 call_leg_release(msc_a->cc.call_leg);
1466 return;
1467 }
1468
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001469 if (codec_if_known) {
Neels Hofmeyr1a07bcd2024-01-19 06:37:57 +01001470 const struct codec_mapping *codec_assigned;
1471
Neels Hofmeyrbd8ac512024-01-19 06:38:11 +01001472 /* Check for unexpected codec with CSD */
Vadim Yanitskiyb0300112024-02-06 16:58:28 +07001473 switch (cc_trans->bearer_cap.transfer) {
1474 case GSM48_BCAP_ITCAP_FAX_G3:
1475 case GSM48_BCAP_ITCAP_3k1_AUDIO:
1476 case GSM48_BCAP_ITCAP_UNR_DIG_INF:
1477 if (codec_if_known->type == GSM0808_SCT_CSD)
1478 break; /* we're good */
Neels Hofmeyrbd8ac512024-01-19 06:38:11 +01001479 LOG_TRANS(cc_trans, LOGL_ERROR, "Unexpected codec in Assignment Complete for CSD: %s\n",
1480 gsm0808_speech_codec_type_name(codec_if_known->type));
1481 call_leg_release(msc_a->cc.call_leg);
1482 return;
Vadim Yanitskiyb0300112024-02-06 16:58:28 +07001483 default:
1484 break;
Neels Hofmeyrbd8ac512024-01-19 06:38:11 +01001485 }
1486
Neels Hofmeyr388d7c92023-03-22 19:03:15 +01001487 /* For 2G:
1488 * - The Assignment Complete has returned a specific codec (e.g. FR3 for AMR FR).
1489 * - Set this codec at the MGW endpoint facing the RAN.
1490 * - Also set this codec at the MGW endpoint facing the CN -- we require an exact match on both call
1491 * legs.
1492 * - TODO: be aware of transcoding that the MGW is capable of, e.g. AMR octet-aligned to AMR
1493 * bandwidth-efficient...
1494 *
1495 * For 3G:
1496 * - ran_infra->force_mgw_codecs_to_ran sets VND.3GPP.IUFP as single codec at the MGW towards RAN.
1497 * - ran_msg_iu.c always returns FR3 (AMR FR) for the assigned codec. Set that at the MGW towards CN.
1498 * - So the MGW decapsulates IuUP <-> AMR
1499 */
Neels Hofmeyr1a07bcd2024-01-19 06:37:57 +01001500 codec_assigned = codec_mapping_by_gsm0808_speech_codec_type(codec_if_known->type);
Neels Hofmeyr388d7c92023-03-22 19:03:15 +01001501 /* TODO: use codec_mapping_by_gsm0808_speech_codec() to also match on codec_if_known->cfg */
Neels Hofmeyr1a07bcd2024-01-19 06:37:57 +01001502 if (!codec_assigned) {
Neels Hofmeyr388d7c92023-03-22 19:03:15 +01001503 LOG_TRANS(cc_trans, LOGL_ERROR, "Unknown codec in Assignment Complete: %s\n",
1504 gsm0808_speech_codec_type_name(codec_if_known->type));
1505 call_leg_release(msc_a->cc.call_leg);
1506 return;
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001507 }
1508
Neels Hofmeyrcec51b32023-03-01 03:47:45 +01001509 /* Update RAN-side endpoint CI from Assignment result -- unless it is forced by the ran_infra, in which
1510 * case it remains unchanged as passed to the earlier call of call_leg_ensure_ci(). */
1511 if (msc_a->c.ran->force_mgw_codecs_to_ran.count == 0)
Neels Hofmeyr1a07bcd2024-01-19 06:37:57 +01001512 rtp_stream_set_one_codec(rtps_to_ran, &codec_assigned->sdp);
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001513
1514 /* Update codec filter with Assignment result, for the CN side */
Neels Hofmeyr1a07bcd2024-01-19 06:37:57 +01001515 cc_trans->cc.codecs.assignment = codec_assigned->sdp;
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001516 } else {
1517 /* No codec passed in Assignment Complete, set 'codecs.assignment' to none. */
1518 cc_trans->cc.codecs.assignment = (struct sdp_audio_codec){};
1519 LOG_TRANS(cc_trans, LOGL_INFO, "Assignment Complete without voice codec\n");
1520 }
1521
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001522 rtp_stream_set_remote_addr(rtps_to_ran, &ac->assignment_complete.remote_rtp);
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001523 if (rtps_to_ran->use_osmux)
1524 rtp_stream_set_remote_osmux_cid(rtps_to_ran,
1525 ac->assignment_complete.osmux_cid);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001526 rtp_stream_commit(rtps_to_ran);
1527
Neels Hofmeyr2d57d6e2022-01-13 21:39:58 +01001528 /* Remember the Codec List (BSS Supported) */
1529 if (ac->assignment_complete.codec_list_bss_supported)
1530 codec_filter_set_bss(&cc_trans->cc.codecs, ac->assignment_complete.codec_list_bss_supported);
1531
Oliver Smithceca8e62023-05-24 11:15:52 +02001532 trans_cc_filter_run(cc_trans);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001533 LOG_TRANS(cc_trans, LOGL_INFO, "Assignment Complete: RAN: %s, CN: %s\n",
1534 sdp_audio_codecs_to_str(&rtps_to_ran->codecs),
Oliver Smithc63c3a02023-05-24 10:48:07 +02001535 sdp_audio_codecs_to_str(&cc_trans->cc.local.audio_codecs));
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001536
1537 if (cc_on_assignment_done(cc_trans)) {
1538 /* If an error occurred, it was logged in cc_assignment_done() */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001539 call_leg_release(msc_a->cc.call_leg);
1540 return;
1541 }
1542}
1543
1544static void msc_a_up_call_assignment_failure(struct msc_a *msc_a, const struct ran_msg *af)
1545{
1546 struct gsm_trans *trans;
1547
1548 /* For a normal voice call, there will be an rtp_stream FSM. */
1549 if (msc_a->cc.call_leg && msc_a->cc.call_leg->rtp[RTP_TO_RAN]) {
1550 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing call\n");
1551 rtp_stream_release(msc_a->cc.call_leg->rtp[RTP_TO_RAN]);
1552 return;
1553 }
1554
Andreas Eversberg23b53e52023-06-21 13:30:58 +02001555 /* For a voice group call, release is performed by VGCS FSM */
1556 trans = trans_find_by_type(msc_a, TRANS_GCC);
1557 if (trans) {
1558 vgcs_vbs_caller_assign_fail(trans);
1559 return;
1560 }
1561 trans = trans_find_by_type(msc_a, TRANS_BCC);
1562 if (trans) {
1563 vgcs_vbs_caller_assign_fail(trans);
1564 return;
1565 }
1566
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001567 /* Otherwise, a silent call might be active */
1568 trans = trans_find_by_type(msc_a, TRANS_SILENT_CALL);
1569 if (trans) {
1570 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing silent call\n");
1571 trans_free(trans);
1572 return;
1573 }
1574
1575 /* Neither a voice call nor silent call assignment. Assume the worst and detach. */
1576 msc_a_release_cn(msc_a);
1577}
1578
1579static void msc_a_up_classmark_update(struct msc_a *msc_a, const struct osmo_gsm48_classmark *classmark,
1580 struct osmo_gsm48_classmark *dst)
1581{
1582 if (!dst) {
1583 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1584
1585 if (!vsub)
1586 dst = &msc_a->temporary_classmark;
1587 else
1588 dst = &vsub->classmark;
1589 }
1590
Martin Hauke3f07dac2019-11-14 17:49:08 +01001591 LOG_MSC_A(msc_a, LOGL_DEBUG, "A5 capabilities received from Classmark Update: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001592 osmo_gsm48_classmark_a5_name(classmark));
1593 osmo_gsm48_classmark_update(dst, classmark);
1594
1595 /* bump subscr conn FSM in case it is waiting for a Classmark Update */
1596 if (msc_a->c.fi->state == MSC_A_ST_WAIT_CLASSMARK_UPDATE)
1597 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CLASSMARK_UPDATE, NULL);
1598}
1599
1600static void msc_a_up_sapi_n_reject(struct msc_a *msc_a, const struct ran_msg *msg)
1601{
1602 int sapi = msg->sapi_n_reject.dlci & 0x7;
1603 if (sapi == UM_SAPI_SMS)
1604 gsm411_sapi_n_reject(msc_a);
1605}
1606
1607static int msc_a_up_ho(struct msc_a *msc_a, const struct msc_a_ran_dec_data *d, uint32_t ho_fi_event)
1608{
1609 if (!msc_a->ho.fi) {
1610 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Handover message, but no Handover ongoing: %s\n", d->ran_dec->msg_name);
1611 return -EINVAL;
1612 }
1613 return osmo_fsm_inst_dispatch(msc_a->ho.fi, ho_fi_event, (void*)d);
1614}
1615
1616int msc_a_ran_dec_from_msc_i(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1617{
1618 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Alexander Couzens2aaff752021-10-19 17:09:11 +02001619 struct gsm_network *net = msc_a_net(msc_a);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001620 const struct ran_msg *msg = d->ran_dec;
1621 int rc = -99;
1622
1623 switch (msg->msg_type) {
1624
1625 case RAN_MSG_COMPL_L3:
Neels Hofmeyr68f50da2020-06-24 14:22:52 +02001626 /* In case the cell_id from Complete Layer 3 Information lacks a PLMN, write the configured PLMN code
1627 * into msc_a->via_cell. Then overwrite with those bits obtained from Complete Layer 3 Information. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001628 msc_a->via_cell = (struct osmo_cell_global_id){
1629 .lai.plmn = msc_a_net(msc_a)->plmn,
1630 };
1631 gsm0808_cell_id_to_cgi(&msc_a->via_cell, msg->compl_l3.cell_id);
Neels Hofmeyrede90832022-01-13 18:13:15 +01001632
Neels Hofmeyre276ae92022-01-13 21:38:35 +01001633 /* If a codec list was sent along in the RAN_MSG_COMPL_L3, remember it for any upcoming codec
1634 * resolution. */
1635 if (msg->compl_l3.codec_list_bss_supported) {
1636 msc_a->cc.compl_l3_codec_list_bss_supported = *msg->compl_l3.codec_list_bss_supported;
1637 if (log_check_level(msc_a->c.ran->log_subsys, LOGL_DEBUG)) {
1638 struct sdp_audio_codecs ac = {};
1639 sdp_audio_codecs_from_speech_codec_list(&ac, &msc_a->cc.compl_l3_codec_list_bss_supported);
1640 LOG_MSC_A(msc_a, LOGL_DEBUG, "Complete Layer 3: Codec List (BSS Supported): %s\n",
1641 sdp_audio_codecs_to_str(&ac));
1642 }
1643 }
1644
Neels Hofmeyrede90832022-01-13 18:13:15 +01001645 /* Submit the Complete Layer 3 Information DTAP */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001646 rc = msc_a_up_l3(msc_a, msg->compl_l3.msg);
1647 if (!rc) {
1648 struct ran_conn *conn = msub_ran_conn(msc_a->c.msub);
1649 if (conn)
1650 ran_peer_cells_seen_add(conn->ran_peer, msg->compl_l3.cell_id);
1651 }
1652 break;
1653
1654 case RAN_MSG_DTAP:
1655 rc = msc_a_up_l3(msc_a, msg->dtap);
1656 break;
1657
1658 case RAN_MSG_CLEAR_REQUEST:
1659 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
1660 break;
1661
1662 case RAN_MSG_CLEAR_COMPLETE:
1663 switch (msc_a->c.fi->state) {
1664 case MSC_A_ST_RELEASING:
1665 msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
1666 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
1667 break;
1668 case MSC_A_ST_RELEASED:
1669 break;
1670 default:
1671 LOG_MSC_A(msc_a, LOGL_ERROR, "Received Clear Complete event, but did not send Clear Command\n");
1672 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
1673 break;
1674 }
1675 rc = 0;
1676 break;
1677
1678 case RAN_MSG_CLASSMARK_UPDATE:
1679 msc_a_up_classmark_update(msc_a, msg->classmark_update.classmark, NULL);
1680 rc = 0;
1681 break;
1682
1683 case RAN_MSG_CIPHER_MODE_COMPLETE:
1684 /* Remember what Ciphering was negotiated (e.g. for Handover) */
1685 if (msg->cipher_mode_complete.alg_id) {
1686 msc_a->geran_encr.alg_id = msg->cipher_mode_complete.alg_id;
1687 LOG_MSC_A(msc_a, LOGL_DEBUG, "Cipher Mode Complete: chosen encryption algorithm: A5/%u\n",
1688 msc_a->geran_encr.alg_id - 1);
Alexander Couzens2aaff752021-10-19 17:09:11 +02001689 }
1690
1691 if (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU) {
1692 int16_t utran_encryption;
1693
1694 /* utran: ensure chosen ciphering mode is allowed
1695 * If the IE is missing (utran_encryption == -1), parse it as no encryption */
1696 utran_encryption = msg->cipher_mode_complete.utran_encryption;
1697 if (utran_encryption == -1)
1698 utran_encryption = 0;
1699 if ((net->uea_encryption_mask & (1 << utran_encryption)) == 0) {
1700 /* cipher disallowed */
1701 LOG_MSC_A(msc_a, LOGL_ERROR, "Cipher Mode Complete: RNC chosen forbidden ciphering UEA%d\n",
1702 msg->cipher_mode_complete.utran_encryption);
1703 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT);
1704 rc = 0;
1705 break;
1706 }
1707 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001708 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_COMPL);
1709 rc = 0;
Neels Hofmeyre9a39112019-08-29 00:10:49 +02001710
1711 /* Evaluate enclosed L3 message, typically Identity Response (IMEISV) */
1712 if (msg->cipher_mode_complete.l3_msg) {
1713 unsigned char *data = (unsigned char*)(msg->cipher_mode_complete.l3_msg->val);
1714 uint16_t len = msg->cipher_mode_complete.l3_msg->len;
1715 struct msgb *dtap = msgb_alloc(len, "DTAP from Cipher Mode Complete");
1716 unsigned char *pos = msgb_put(dtap, len);
1717 memcpy(pos, data, len);
1718 dtap->l3h = pos;
1719 rc = msc_a_up_l3(msc_a, dtap);
1720 msgb_free(dtap);
1721 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001722 break;
1723
1724 case RAN_MSG_CIPHER_MODE_REJECT:
1725 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT);
1726 rc = 0;
1727 break;
1728
1729 case RAN_MSG_ASSIGNMENT_COMPLETE:
1730 msc_a_up_call_assignment_complete(msc_a, msg);
1731 rc = 0;
1732 break;
1733
1734 case RAN_MSG_ASSIGNMENT_FAILURE:
1735 msc_a_up_call_assignment_failure(msc_a, msg);
1736 rc = 0;
1737 break;
1738
1739 case RAN_MSG_SAPI_N_REJECT:
1740 msc_a_up_sapi_n_reject(msc_a, msg);
1741 rc = 0;
1742 break;
1743
1744 case RAN_MSG_HANDOVER_PERFORMED:
1745 /* The BSS lets us know that a handover happened within the BSS, which doesn't concern us. */
1746 LOG_MSC_A(msc_a, LOGL_ERROR, "'Handover Performed' handling not implemented\n");
1747 break;
1748
1749 case RAN_MSG_HANDOVER_REQUIRED:
1750 /* The BSS lets us know that it wants to handover to a different cell */
1751 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_HANDOVER_REQUIRED, (void*)&msg->handover_required);
1752 break;
1753
1754 case RAN_MSG_HANDOVER_FAILURE:
1755 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1756 break;
1757
Keith Whytea1a70be2021-05-16 02:59:52 +02001758 case RAN_MSG_LCLS_STATUS:
1759 /* The BSS sends us LCLS_STATUS. We do nothing for now, but it is not an error. */
1760 LOG_MSC_A(msc_a, LOGL_DEBUG, "LCLS_STATUS (%s) received from MSC-I\n",
1761 gsm0808_lcls_status_name(msg->lcls_status.status));
1762 rc = 0;
1763 break;
1764
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001765 default:
1766 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-I not implemented: %s\n", ran_msg_type_name(msg->msg_type));
1767 rc = -ENOTSUP;
1768 break;
1769 }
1770 return rc;
1771}
1772
Andreas Eversbergf7c6f142023-04-23 12:10:42 +02001773static int msc_a_rx_vgcs_bss_decoded(struct osmo_fsm_inst *caller_fi, void *caller_data, const struct ran_msg *msg)
1774{
1775 struct vgcs_bss *bss = caller_data;
1776 struct msc_a *msc_a = (bss->trans) ? bss->trans->msc_a : NULL;
1777 int rc = 0;
1778
1779 switch (msg->msg_type) {
1780 case RAN_MSG_VGCS_VBS_SETUP_ACK:
1781 /* The BSS accepts VGCS/VBS and sends us supported features. */
1782 vgcs_vbs_setup_ack(bss, msg);
1783 break;
1784 case RAN_MSG_VGCS_VBS_SETUP_REFUSE:
1785 /* The BSS refuses VGCS/VBS. */
1786 vgcs_vbs_setup_refuse(bss, msg);
1787 break;
1788 case RAN_MSG_UPLINK_REQUEST:
1789 /* A mobile station requests the uplink on a VGCS channel. */
1790 vgcs_uplink_request(bss, msg);
1791 break;
1792 case RAN_MSG_UPLINK_REQUEST_CNF:
1793 /* The uplink on a VGCS channel has been established. */
1794 vgcs_uplink_request_cnf(bss, msg);
1795 break;
1796 case RAN_MSG_UPLINK_APPLICATION_DATA:
1797 /* Application data received on the uplink of a VGCS channel. */
1798 vgcs_app_data(bss, msg);
1799 break;
1800 case RAN_MSG_DTAP:
1801 /* BSS confirms the release of the channel. */
1802 vgcs_bss_dtap(bss, msg);
1803 break;
1804 case RAN_MSG_UPLINK_RELEASE_IND:
1805 /* A mobile station releases the uplink on a VGCS channel. */
1806 vgcs_uplink_release_ind(bss, msg);
1807 break;
1808 case RAN_MSG_CLEAR_REQUEST:
1809 /* BSS indicated that the channel has been released. */
1810 vgcs_vbs_clear_req(bss, msg);
1811 break;
1812 case RAN_MSG_CLEAR_COMPLETE:
1813 /* BSS confirms the release of the channel. */
1814 vgcs_vbs_clear_cpl(bss, msg);
1815 break;
1816 default:
1817 LOG_MSC_A(msc_a, LOGL_ERROR, "VGCS message from BSS not implemented: %s\n",
1818 ran_msg_type_name(msg->msg_type));
1819 rc = -ENOTSUP;
1820 break;
1821 }
1822 return rc;
1823}
1824
1825int msc_a_rx_vgcs_bss(struct vgcs_bss *bss, struct ran_conn *from_conn, struct msgb *msg)
1826{
1827 struct ran_dec ran_dec;
1828
1829 /* Feed through the decoding mechanism ran_msg. The decoded message arrives in msc_a_rx_vgcs_decoded() */
1830 ran_dec = (struct ran_dec) {
1831 .caller_data = bss,
1832 .decode_cb = msc_a_rx_vgcs_bss_decoded,
1833 };
1834 struct ran_peer *ran_peer = from_conn->ran_peer;
1835 struct ran_infra *ran = ran_peer->sri->ran;
1836 if (!ran->ran_dec_l2) {
1837 LOGP(DMSC, LOGL_ERROR, "No ran_dec_l2() defined for RAN type %s\n",
1838 osmo_rat_type_name(ran->type));
1839 return -ENOTSUP;
1840 }
1841 return ran->ran_dec_l2(&ran_dec, msg);
1842}
1843
1844static int msc_a_rx_vgcs_cell_decoded(struct osmo_fsm_inst *caller_fi, void *caller_data, const struct ran_msg *msg)
1845{
1846 struct vgcs_bss_cell *cell = caller_data;
1847 struct msc_a *msc_a = (cell->bss && cell->bss->trans) ? cell->bss->trans->msc_a : NULL;
1848 int rc = 0;
1849
1850 switch (msg->msg_type) {
1851 case RAN_MSG_VGCS_VBS_ASSIGN_RES:
1852 /* The BSS accepts VGCS/VBS channel assignment. */
1853 vgcs_vbs_assign_result(cell, msg);
1854 break;
1855 case RAN_MSG_VGCS_VBS_ASSIGN_FAIL:
1856 /* The BSS refuses VGCS/VBS channel assignment. */
1857 vgcs_vbs_assign_fail(cell, msg);
1858 break;
1859 case RAN_MSG_VGCS_VBS_QUEUING_IND:
1860 /* The BSS needs more time for VGCS/VBS channel assignment. */
1861 vgcs_vbs_queuing_ind(cell);
1862 break;
1863 case RAN_MSG_VGCS_VBS_ASSIGN_STATUS:
1864 /* The BSS gives cell status about VGCS/VBS channel. */
1865 vgcs_vbs_assign_status(cell, msg);
1866 break;
1867 case RAN_MSG_CLEAR_REQUEST:
1868 /* BSS indicated that the channel has been released. */
1869 vgcs_vbs_clear_req_channel(cell, msg);
1870 break;
1871 case RAN_MSG_CLEAR_COMPLETE:
1872 /* BSS confirms the release of the channel. */
1873 vgcs_vbs_clear_cpl_channel(cell, msg);
1874 break;
1875 default:
1876 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from BSS leg not implemented: %s\n",
1877 ran_msg_type_name(msg->msg_type));
1878 rc = -ENOTSUP;
1879 break;
1880 }
1881 return rc;
1882}
1883
1884int msc_a_rx_vgcs_cell(struct vgcs_bss_cell *cell, struct ran_conn *from_conn, struct msgb *msg)
1885{
1886 struct ran_dec ran_dec;
1887
1888 /* Feed through the decoding mechanism ran_msg. The decoded message arrives in msc_a_rx_vgcs_decoded() */
1889 ran_dec = (struct ran_dec) {
1890 .caller_data = cell,
1891 .decode_cb = msc_a_rx_vgcs_cell_decoded,
1892 };
1893 struct ran_peer *ran_peer = from_conn->ran_peer;
1894 struct ran_infra *ran = ran_peer->sri->ran;
1895 if (!ran->ran_dec_l2) {
1896 LOGP(DMSC, LOGL_ERROR, "No ran_dec_l2() defined for RAN type %s\n",
1897 osmo_rat_type_name(ran->type));
1898 return -ENOTSUP;
1899 }
1900 return ran->ran_dec_l2(&ran_dec, msg);
1901}
1902
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001903static int msc_a_ran_dec_from_msc_t(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1904{
1905 struct msc_t *msc_t = msc_a_msc_t(msc_a);
1906 int rc = -99;
1907
1908 if (!msc_t) {
1909 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx message from MSC-T role, but I have no active MSC-T role.\n");
1910 return -EINVAL;
1911 }
1912
1913 OSMO_ASSERT(d->ran_dec);
1914
1915 switch (d->ran_dec->msg_type) {
1916
1917 case RAN_MSG_CLEAR_REQUEST:
1918 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_MO_CLOSE, NULL);
1919 break;
1920
1921 case RAN_MSG_CLEAR_COMPLETE:
1922 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_CLEAR_COMPLETE, NULL);
1923 break;
1924
1925 case RAN_MSG_CLASSMARK_UPDATE:
1926 msc_a_up_classmark_update(msc_a, d->ran_dec->classmark_update.classmark, &msc_t->classmark);
1927 rc = 0;
1928 break;
1929
1930 case RAN_MSG_HANDOVER_REQUEST_ACK:
1931 /* new BSS accepts Handover */
1932 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_REQUEST_ACK);
1933 break;
1934
1935 case RAN_MSG_HANDOVER_DETECT:
1936 /* new BSS signals the MS is DETECTed on the new lchan */
1937 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_DETECT);
1938 break;
1939
1940 case RAN_MSG_HANDOVER_COMPLETE:
1941 /* new BSS signals the MS has fully moved to the new lchan */
1942 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_COMPLETE);
1943 break;
1944
1945 case RAN_MSG_HANDOVER_FAILURE:
1946 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1947 break;
1948
1949 default:
1950 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-T not implemented: %s\n",
1951 ran_msg_type_name(d->ran_dec->msg_type));
1952 rc = -ENOTSUP;
1953 break;
1954 }
1955 return rc;
1956}
1957
1958int msc_a_ran_decode_cb(struct osmo_fsm_inst *msc_a_fi, void *data, const struct ran_msg *msg)
1959{
1960 struct msc_a *msc_a = msc_a_fi_priv(msc_a_fi);
1961 struct msc_a_ran_dec_data *d = data;
1962 int rc = -99;
1963
1964 d->ran_dec = msg;
1965
1966 switch (d->from_role) {
1967 case MSC_ROLE_I:
1968 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode: %s\n", msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1969 rc = msc_a_ran_dec_from_msc_i(msc_a, d);
1970 break;
1971
1972 case MSC_ROLE_T:
1973 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode from MSC-T: %s\n",
1974 msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1975 rc = msc_a_ran_dec_from_msc_t(msc_a, d);
1976 break;
1977
1978 default:
1979 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from invalid role %s: %s\n", msc_role_name(d->from_role),
1980 ran_msg_type_name(msg->msg_type));
1981 return -ENOTSUP;
1982 }
1983
1984 if (rc)
1985 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),
1986 msc_role_name(d->from_role));
1987 return rc;
1988}
1989
1990/* Your typical DTAP via FORWARD_ACCESS_SIGNALLING_REQUEST */
1991int _msc_a_ran_down(struct msc_a *msc_a, enum msc_role to_role, const struct ran_msg *ran_msg,
1992 const char *file, int line)
1993{
1994 return _msc_a_msg_down(msc_a, to_role, msub_role_to_role_event(msc_a->c.msub, MSC_ROLE_A, to_role),
1995 ran_msg, file, line);
1996}
1997
1998/* To transmit more complex events than just FORWARD_ACCESS_SIGNALLING_REQUEST, e.g. an
1999 * MSC_T_EV_FROM_A_PREPARE_HANDOVER_REQUEST */
2000int _msc_a_msg_down(struct msc_a *msc_a, enum msc_role to_role, uint32_t to_role_event,
2001 const struct ran_msg *ran_msg,
2002 const char *file, int line)
2003{
2004 struct an_apdu an_apdu = {
2005 .an_proto = msc_a->c.ran->an_proto,
2006 .msg = msc_role_ran_encode(msc_a->c.fi, ran_msg),
2007 };
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002008 if (!an_apdu.msg)
2009 return -EIO;
Vadim Yanitskiyc44342b2021-12-07 18:32:35 +03002010 return _msub_role_dispatch(msc_a->c.msub, to_role, to_role_event, &an_apdu, file, line);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002011}
2012
2013int msc_a_tx_dtap_to_i(struct msc_a *msc_a, struct msgb *dtap)
2014{
2015 struct ran_msg ran_msg;
Neels Hofmeyrc192c0b2019-10-07 21:41:18 +02002016 struct gsm48_hdr *gh = msgb_l3(dtap) ? : dtap->data;
2017 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002018
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02002019 if (!msc_a) {
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02002020 LOGP(DMSC, LOGL_ERROR, "Attempt to send DTAP to NULL MSC-A, dropping message: %s %s\n",
2021 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
2022 msgb_free(dtap);
2023 return -EIO;
2024 }
2025
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002026 if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) {
2027 /* The SGs connection to the MME always is at the MSC-A. */
2028 return sgs_iface_tx_dtap_ud(msc_a, dtap);
2029 }
2030
Neels Hofmeyrc192c0b2019-10-07 21:41:18 +02002031 LOG_MSC_A(msc_a, LOGL_DEBUG, "Sending DTAP: %s %s\n",
2032 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
2033
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002034 ran_msg = (struct ran_msg){
2035 .msg_type = RAN_MSG_DTAP,
2036 .dtap = dtap,
2037 };
2038 return msc_a_ran_down(msc_a, MSC_ROLE_I, &ran_msg);
2039}
2040
2041struct msc_a *msc_a_for_vsub(const struct vlr_subscr *vsub, bool valid_conn_only)
2042{
2043 struct msc_a *msc_a = msub_msc_a(msub_for_vsub(vsub));
2044 if (valid_conn_only && !msc_a_is_accepted(msc_a))
2045 return NULL;
2046 return msc_a;
2047}
2048
2049int msc_tx_common_id(struct msc_a *msc_a, enum msc_role to_role)
2050{
2051 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Vadim Yanitskiy435f67f2021-06-06 15:47:49 +02002052 if (vsub == NULL)
2053 return -ENODEV;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002054 struct ran_msg msg = {
2055 .msg_type = RAN_MSG_COMMON_ID,
2056 .common_id = {
2057 .imsi = vsub->imsi,
Pau Espin Pedrol67106702021-04-27 18:20:15 +02002058 .last_eutran_plmn_present = vsub->sgs.last_eutran_plmn_present,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002059 },
2060 };
Pau Espin Pedrol67106702021-04-27 18:20:15 +02002061 if (vsub->sgs.last_eutran_plmn_present) {
2062 memcpy(&msg.common_id.last_eutran_plmn, &vsub->sgs.last_eutran_plmn,
2063 sizeof(vsub->sgs.last_eutran_plmn));
2064 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002065
2066 return msc_a_ran_down(msc_a, to_role, &msg);
2067}
2068
2069static int msc_a_start_assignment(struct msc_a *msc_a, struct gsm_trans *cc_trans)
2070{
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002071 struct call_leg *cl;
2072 bool cn_rtp_available;
2073 bool ran_rtp_available;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002074
2075 OSMO_ASSERT(!msc_a->cc.active_trans);
2076 msc_a->cc.active_trans = cc_trans;
2077
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01002078 cc_trans->cc.codecs.assignment = (struct sdp_audio_codec){};
2079
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002080 OSMO_ASSERT(cc_trans && cc_trans->type == TRANS_CC);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002081 cl = msc_a_ensure_call_leg(msc_a, cc_trans);
2082 if (!cl)
2083 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002084
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002085 /* See if we can set a preliminary codec. If not, pass none for the time being. */
Oliver Smithceca8e62023-05-24 11:15:52 +02002086 trans_cc_filter_run(cc_trans);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002087
2088 cn_rtp_available = call_leg_local_ip(cl, RTP_TO_CN);
2089 ran_rtp_available = call_leg_local_ip(cl, RTP_TO_RAN);
2090
2091 /* Set up RTP ports for both RAN and CN side. Even though we ask for both at the same time, the
2092 * osmo_mgcpc_ep_fsm automagically waits for the first CRCX to complete before firing the second CRCX. The one
2093 * issued first here will also be the first CRCX sent to the MGW. Usually both still need to be set up. */
2094 if (!cn_rtp_available)
Andreas Eversberg712b28e2023-06-21 11:17:26 +02002095 call_leg_ensure_ci(cl, RTP_TO_CN, cc_trans->call_id, cc_trans,
Oliver Smithc63c3a02023-05-24 10:48:07 +02002096 &cc_trans->cc.local.audio_codecs, NULL);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002097 if (!ran_rtp_available) {
2098 struct sdp_audio_codecs *codecs;
2099 if (msc_a->c.ran->force_mgw_codecs_to_ran.count)
2100 codecs = &msc_a->c.ran->force_mgw_codecs_to_ran;
2101 else
Oliver Smithc63c3a02023-05-24 10:48:07 +02002102 codecs = &cc_trans->cc.local.audio_codecs;
Andreas Eversberg712b28e2023-06-21 11:17:26 +02002103 return call_leg_ensure_ci(cl, RTP_TO_RAN, cc_trans->call_id, cc_trans, codecs, NULL);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002104 }
2105
2106 /* Should these already be set up, immediately continue by retriggering the events signalling that the RTP
2107 * ports are available. The ordering is: first CN, then RAN. */
2108 if (cn_rtp_available && ran_rtp_available)
2109 return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_RAN]);
2110 else if (cn_rtp_available)
2111 return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_CN]);
2112 /* Otherwise wait for MGCP response and continue from there. */
2113 return 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002114}
2115
2116int msc_a_try_call_assignment(struct gsm_trans *cc_trans)
2117{
2118 struct msc_a *msc_a = cc_trans->msc_a;
2119 OSMO_ASSERT(cc_trans->type == TRANS_CC);
2120
2121 if (msc_a->cc.active_trans == cc_trans) {
Neels Hofmeyrb4ef5e72019-08-30 01:11:12 +02002122 LOG_MSC_A(msc_a, LOGL_DEBUG, "Assignment for this trans already started earlier\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002123 return 0;
2124 }
2125
2126 if (msc_a->cc.active_trans) {
2127 LOG_MSC_A(msc_a, LOGL_INFO, "Another call is already ongoing, not assigning yet\n");
2128 return 0;
2129 }
2130
2131 LOG_MSC_A(msc_a, LOGL_DEBUG, "Starting call assignment\n");
2132 return msc_a_start_assignment(msc_a, cc_trans);
2133}
2134
Neels Hofmeyr5d53c602022-04-24 23:37:07 +02002135/* Map CM Service type to use token.
2136 * Given a CM Service type, return a matching token intended for osmo_use_count.
2137 * For unknown service type, return NULL.
2138 */
Andreas Eversbergcd8bd452023-07-11 14:16:24 +02002139const 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 +01002140{
Andreas Eversbergcd8bd452023-07-11 14:16:24 +02002141 struct gsm_network *net = msc_a_net(msc_a);
2142
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002143 switch (cm_service_type) {
2144 case GSM48_CMSERV_MO_CALL_PACKET:
2145 case GSM48_CMSERV_EMERGENCY:
2146 return MSC_A_USE_CM_SERVICE_CC;
2147
2148 case GSM48_CMSERV_SMS:
2149 return MSC_A_USE_CM_SERVICE_SMS;
2150
2151 case GSM48_CMSERV_SUP_SERV:
2152 return MSC_A_USE_CM_SERVICE_SS;
2153
Andreas Eversberg456c6f72023-04-23 11:54:16 +02002154 case GSM48_CMSERV_VGCS:
Andreas Eversbergcd8bd452023-07-11 14:16:24 +02002155 if (net->asci.enable)
2156 return MSC_A_USE_CM_SERVICE_GCC;
2157 else
2158 return NULL;
Andreas Eversberg456c6f72023-04-23 11:54:16 +02002159
2160 case GSM48_CMSERV_VBS:
Andreas Eversbergcd8bd452023-07-11 14:16:24 +02002161 if (net->asci.enable)
2162 return MSC_A_USE_CM_SERVICE_BCC;
2163 else
2164 return NULL;
Andreas Eversberg456c6f72023-04-23 11:54:16 +02002165
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002166 default:
2167 return NULL;
2168 }
2169}
2170
2171void msc_a_release_cn(struct msc_a *msc_a)
2172{
2173 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CN_CLOSE, NULL);
2174}
2175
2176void msc_a_release_mo(struct msc_a *msc_a, enum gsm48_gsm_cause gsm_cause)
2177{
2178 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
2179}