blob: 38f05a6d4622e05cd9f8277f0b67d612ff28d93a [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);
1002
1003 trans_conn_closed(msc_a);
1004
1005 if (msc_a_fsm_has_active_transactions(fi))
1006 LOG_MSC_A(msc_a, LOGL_ERROR, "Deallocating active transactions failed\n");
1007
1008 LOG_MSC_A_CAT(msc_a, DREF, LOGL_DEBUG, "max total use count was %d\n", msc_a->max_total_use_count);
1009}
1010
1011const struct value_string msc_a_fsm_event_names[] = {
1012 OSMO_VALUE_STRING(MSC_REMOTE_EV_RX_GSUP),
1013 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE),
1014 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_COMPLETE),
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001015 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_TERM),
1016 OSMO_VALUE_STRING(MSC_MNCC_EV_NEED_LOCAL_RTP),
1017 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_PROCEEDING),
1018 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_COMPLETE),
1019 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_ENDED),
1020 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_COMPLETE_LAYER_3),
1021 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST),
1022 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST),
1023 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST),
1024 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST),
1025 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE),
1026 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE),
1027 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST),
1028 OSMO_VALUE_STRING(MSC_A_EV_COMPLETE_LAYER_3_OK),
1029 OSMO_VALUE_STRING(MSC_A_EV_CLASSMARK_UPDATE),
1030 OSMO_VALUE_STRING(MSC_A_EV_AUTHENTICATED),
1031 OSMO_VALUE_STRING(MSC_A_EV_TRANSACTION_ACCEPTED),
1032 OSMO_VALUE_STRING(MSC_A_EV_CN_CLOSE),
1033 OSMO_VALUE_STRING(MSC_A_EV_MO_CLOSE),
1034 OSMO_VALUE_STRING(MSC_A_EV_UNUSED),
1035 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_REQUIRED),
1036 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_END),
1037 {}
1038};
1039
1040#define S(x) (1 << (x))
1041
1042static const struct osmo_fsm_state msc_a_fsm_states[] = {
1043 [MSC_A_ST_VALIDATE_L3] = {
1044 .name = OSMO_STRINGIFY(MSC_A_ST_VALIDATE_L3),
1045 .in_event_mask = 0
1046 | S(MSC_A_EV_FROM_I_COMPLETE_LAYER_3)
1047 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1048 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1049 | S(MSC_A_EV_COMPLETE_LAYER_3_OK)
1050 | S(MSC_A_EV_MO_CLOSE)
1051 | S(MSC_A_EV_CN_CLOSE)
1052 | S(MSC_A_EV_UNUSED)
1053 ,
1054 .out_state_mask = 0
1055 | S(MSC_A_ST_VALIDATE_L3)
1056 | S(MSC_A_ST_AUTH_CIPH)
1057 | S(MSC_A_ST_RELEASING)
1058 ,
1059 .action = msc_a_fsm_validate_l3,
1060 },
1061 [MSC_A_ST_AUTH_CIPH] = {
1062 .name = OSMO_STRINGIFY(MSC_A_ST_AUTH_CIPH),
1063 .in_event_mask = 0
1064 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1065 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1066 | S(MSC_A_EV_AUTHENTICATED)
1067 | S(MSC_A_EV_MO_CLOSE)
1068 | S(MSC_A_EV_CN_CLOSE)
1069 | S(MSC_A_EV_UNUSED)
1070 ,
1071 .out_state_mask = 0
1072 | S(MSC_A_ST_WAIT_CLASSMARK_UPDATE)
1073 | S(MSC_A_ST_AUTHENTICATED)
1074 | S(MSC_A_ST_RELEASING)
1075 ,
1076 .action = msc_a_fsm_auth_ciph,
1077 },
1078 [MSC_A_ST_WAIT_CLASSMARK_UPDATE] = {
1079 .name = OSMO_STRINGIFY(MSC_A_ST_WAIT_CLASSMARK_UPDATE),
1080 .in_event_mask = 0
1081 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1082 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1083 | S(MSC_A_EV_CLASSMARK_UPDATE)
1084 | S(MSC_A_EV_MO_CLOSE)
1085 | S(MSC_A_EV_CN_CLOSE)
1086 ,
1087 .out_state_mask = 0
1088 | S(MSC_A_ST_AUTH_CIPH)
1089 | S(MSC_A_ST_RELEASING)
1090 ,
1091 .action = msc_a_fsm_wait_classmark_update,
1092 },
1093 [MSC_A_ST_AUTHENTICATED] = {
1094 .name = OSMO_STRINGIFY(MSC_A_ST_AUTHENTICATED),
1095 /* allow everything to release for any odd behavior */
1096 .in_event_mask = 0
1097 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1098 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
1099 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1100 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
1101 | S(MSC_A_EV_MO_CLOSE)
1102 | S(MSC_A_EV_CN_CLOSE)
1103 | S(MSC_A_EV_UNUSED)
1104 ,
1105 .out_state_mask = 0
1106 | S(MSC_A_ST_RELEASING)
1107 | S(MSC_A_ST_COMMUNICATING)
1108 ,
1109 .onenter = msc_a_fsm_authenticated_enter,
1110 .action = msc_a_fsm_authenticated,
1111 },
1112 [MSC_A_ST_COMMUNICATING] = {
1113 .name = OSMO_STRINGIFY(MSC_A_ST_COMMUNICATING),
1114 /* allow everything to release for any odd behavior */
1115 .in_event_mask = 0
1116 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1117 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
1118 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1119 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE)
1120 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE)
1121 | S(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST)
1122 | S(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST)
1123 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
1124 | S(MSC_A_EV_MO_CLOSE)
1125 | S(MSC_A_EV_CN_CLOSE)
1126 | S(MSC_A_EV_UNUSED)
1127 | S(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE)
1128 | S(MSC_EV_CALL_LEG_RTP_COMPLETE)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001129 | S(MSC_EV_CALL_LEG_TERM)
1130 | S(MSC_MNCC_EV_CALL_ENDED)
1131 | S(MSC_A_EV_HANDOVER_REQUIRED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +02001132 | S(MSC_A_EV_HANDOVER_END)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001133 ,
1134 .out_state_mask = 0
1135 | S(MSC_A_ST_RELEASING)
1136 ,
1137 .action = msc_a_fsm_communicating,
1138 },
1139 [MSC_A_ST_RELEASING] = {
1140 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASING),
1141 .in_event_mask = 0
1142 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1143 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1144 | S(MSC_A_EV_UNUSED)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001145 | S(MSC_EV_CALL_LEG_TERM)
1146 | S(MSC_MNCC_EV_CALL_ENDED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +02001147 | S(MSC_A_EV_HANDOVER_END)
Pau Espin Pedrole53ecde2021-07-12 13:37:24 +02001148 | S(MSC_A_EV_CN_CLOSE)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001149 ,
1150 .out_state_mask = 0
1151 | S(MSC_A_ST_RELEASED)
1152 ,
1153 .onenter = msc_a_fsm_releasing_onenter,
1154 .action = msc_a_fsm_releasing,
1155 },
1156 [MSC_A_ST_RELEASED] = {
1157 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASED),
1158 .in_event_mask = 0
1159 | S(MSC_A_EV_UNUSED)
1160 ,
1161 .onenter = msc_a_fsm_released_onenter,
1162 .action = msc_a_fsm_released,
1163 },
1164};
1165
1166static struct osmo_fsm msc_a_fsm = {
1167 .name = "msc_a",
1168 .states = msc_a_fsm_states,
1169 .num_states = ARRAY_SIZE(msc_a_fsm_states),
1170 .log_subsys = DMSC,
1171 .event_names = msc_a_fsm_event_names,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001172 .timer_cb = msc_a_fsm_timer_cb,
1173 .cleanup = msc_a_fsm_cleanup,
1174};
1175
1176static __attribute__((constructor)) void msc_a_fsm_init()
1177{
1178 OSMO_ASSERT(osmo_fsm_register(&msc_a_fsm) == 0);
1179}
1180
1181static int msc_a_use_cb(struct osmo_use_count_entry *e, int32_t old_use_count, const char *file, int line)
1182{
1183 struct msc_a *msc_a = e->use_count->talloc_object;
1184 char buf[128];
1185 int32_t total;
1186 int level;
1187
1188 if (!e->use)
1189 return -EINVAL;
1190
1191 total = osmo_use_count_total(&msc_a->use_count);
1192
1193 if (total == 0
1194 || (total == 1 && old_use_count == 0 && e->count == 1))
1195 level = LOGL_INFO;
1196 else
1197 level = LOGL_DEBUG;
1198
1199 LOG_MSC_A_CAT_SRC(msc_a, DREF, level, file, line, "%s %s: now used by %s\n",
1200 (e->count - old_use_count) > 0? "+" : "-", e->use,
1201 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
1202
1203 if (e->count < 0)
1204 return -ERANGE;
1205
1206 msc_a->max_total_use_count = OSMO_MAX(msc_a->max_total_use_count, total);
1207
1208 if (total == 0)
1209 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_UNUSED, NULL);
1210 return 0;
1211}
1212
1213struct msc_a *msc_a_alloc(struct msub *msub, struct ran_infra *ran)
1214{
1215 struct msc_a *msc_a = msub_role_alloc(msub, MSC_ROLE_A, &msc_a_fsm, struct msc_a, ran);
1216 msc_a->use_count = (struct osmo_use_count){
1217 .talloc_object = msc_a,
1218 .use_cb = msc_a_use_cb,
1219 };
1220 osmo_use_count_make_static_entries(&msc_a->use_count, msc_a->use_count_buf, ARRAY_SIZE(msc_a->use_count_buf));
1221 /* Start timeout for first state */
Neels Hofmeyr01653252019-09-03 02:06:22 +02001222 msc_a_state_chg_always(msc_a, MSC_A_ST_VALIDATE_L3);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001223 return msc_a;
1224}
1225
1226bool msc_a_is_establishing_auth_ciph(const struct msc_a *msc_a)
1227{
1228 if (!msc_a || !msc_a->c.fi)
1229 return false;
1230 return msc_a->c.fi->state == MSC_A_ST_AUTH_CIPH;
1231}
1232
1233const struct value_string complete_layer3_type_names[] = {
1234 { COMPLETE_LAYER3_NONE, "NONE" },
1235 { COMPLETE_LAYER3_LU, "LU" },
1236 { COMPLETE_LAYER3_CM_SERVICE_REQ, "CM_SERVICE_REQ" },
1237 { COMPLETE_LAYER3_PAGING_RESP, "PAGING_RESP" },
Neels Hofmeyrae98b972021-07-27 03:46:49 +02001238 { COMPLETE_LAYER3_CM_RE_ESTABLISH_REQ, "CM_RE_ESTABLISH_REQ" },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001239 { 0, NULL }
1240};
1241
1242#define _msc_a_update_id(MSC_A, FMT, ARGS ...) \
1243 do { \
1244 if (osmo_fsm_inst_update_id_f(msc_a->c.fi, FMT ":%s:%s", \
1245 ## ARGS, \
1246 msub_ran_conn_name(msc_a->c.msub), \
1247 complete_layer3_type_name(msc_a->complete_layer3_type)) \
1248 == 0) { \
1249 struct vlr_subscr *_vsub = msc_a_vsub(MSC_A); \
1250 if (_vsub) { \
1251 if (_vsub->lu_fsm) \
1252 osmo_fsm_inst_update_id(_vsub->lu_fsm, (MSC_A)->c.fi->id); \
1253 if (_vsub->auth_fsm) \
1254 osmo_fsm_inst_update_id(_vsub->auth_fsm, (MSC_A)->c.fi->id); \
1255 if (_vsub->proc_arq_fsm) \
1256 osmo_fsm_inst_update_id(_vsub->proc_arq_fsm, (MSC_A)->c.fi->id); \
1257 } \
1258 LOG_MSC_A(MSC_A, LOGL_DEBUG, "Updated ID\n"); \
1259 } \
1260 /* otherwise osmo_fsm_inst_update_id_f() will log an error. */ \
1261 } while (0)
1262
1263
1264/* 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 +02001265void msc_a_update_id_from_mi(struct msc_a *msc_a, const struct osmo_mobile_identity *mi)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001266{
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001267 _msc_a_update_id(msc_a, "%s", osmo_mobile_identity_to_str_c(OTC_SELECT, mi));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001268}
1269
1270/* Update msc_a->fi id string from current msc_a->vsub and msc_a->complete_layer3_type. */
1271void msc_a_update_id(struct msc_a *msc_a)
1272{
1273 _msc_a_update_id(msc_a, "%s", vlr_subscr_name(msc_a_vsub(msc_a)));
1274}
1275
1276/* Iterate all msc_a instances that are relevant for this subscriber, and update FSM ID strings for all of the FSM
1277 * instances. */
1278void msc_a_update_id_for_vsub(struct vlr_subscr *for_vsub)
1279{
1280 struct msub *msub;
1281 llist_for_each_entry(msub, &msub_list, entry) {
1282 struct vlr_subscr *vsub = msub_vsub(msub);
1283 if (vsub != for_vsub)
1284 continue;
1285 msc_a_update_id(msub_msc_a(msub));
1286 }
1287}
1288
1289static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
1290{
1291 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
1292 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
1293
1294 switch (pdisc) {
1295 case GSM48_PDISC_MM:
1296 switch (msg_type) {
1297 case GSM48_MT_MM_LOC_UPD_REQUEST:
1298 case GSM48_MT_MM_CM_SERV_REQ:
1299 case GSM48_MT_MM_CM_REEST_REQ:
1300 case GSM48_MT_MM_AUTH_RESP:
1301 case GSM48_MT_MM_AUTH_FAIL:
1302 case GSM48_MT_MM_ID_RESP:
1303 case GSM48_MT_MM_TMSI_REALL_COMPL:
1304 case GSM48_MT_MM_IMSI_DETACH_IND:
1305 return true;
1306 default:
1307 break;
1308 }
1309 break;
1310 case GSM48_PDISC_RR:
1311 switch (msg_type) {
1312 /* GSM48_MT_RR_CIPH_M_COMPL is actually handled in bssmap_rx_ciph_compl() and gets redirected in the
1313 * BSSAP layer to ran_conn_cipher_mode_compl() (before this here is reached) */
1314 case GSM48_MT_RR_PAG_RESP:
1315 case GSM48_MT_RR_CIPH_M_COMPL:
1316 return true;
1317 default:
1318 break;
1319 }
1320 break;
1321 default:
1322 break;
1323 }
1324
1325 return false;
1326}
1327
1328/* Main entry point for GSM 04.08/44.008 Layer 3 data (e.g. from the BSC). */
1329int msc_a_up_l3(struct msc_a *msc_a, struct msgb *msg)
1330{
1331 struct gsm48_hdr *gh;
1332 uint8_t pdisc;
1333 int rc;
1334 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1335 int is_r99;
1336
1337 OSMO_ASSERT(msg->l3h);
1338 OSMO_ASSERT(msg);
1339
1340 gh = msgb_l3(msg);
1341 pdisc = gsm48_hdr_pdisc(gh);
1342
1343 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_DEBUG, "Dispatching 04.08 message: %s %s\n",
1344 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1345
1346 /* To evaluate the 3GPP TS 24.007 Duplicate Detection, we need Classmark information on whether the MS is R99
1347 * capable. If the subscriber is already actively connected, the Classmark information is stored with the
1348 * vlr_subscr. Otherwise, this *must* be a Complete Layer 3 with Classmark info. */
1349 if (vsub)
1350 is_r99 = osmo_gsm48_classmark_is_r99(&vsub->classmark) ? 1 : 0;
1351 else
1352 is_r99 = compl_l3_msg_is_r99(msg);
1353
1354 if (is_r99 < 0) {
1355 LOG_MSC_A(msc_a, LOGL_ERROR,
1356 "No Classmark Information, dropping non-Complete-Layer3 message: %s\n",
1357 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1358 return -EACCES;
1359 }
1360
1361 if (is_r99 >= 0
1362 && ran_dec_dtap_undup_is_duplicate(msc_a->c.fi, msc_a->n_sd_next, is_r99 ? true : false, msg)) {
1363 LOG_MSC_A(msc_a, LOGL_DEBUG, "Dropping duplicate message"
1364 " (3GPP TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection)\n");
1365 return 0;
1366 }
1367
1368 if (!msc_a_is_accepted(msc_a)
1369 && !msg_is_initially_permitted(gh)) {
1370 LOG_MSC_A(msc_a, LOGL_ERROR,
1371 "Message not permitted for initial conn: %s\n",
1372 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1373 return -EACCES;
1374 }
1375
1376 if (vsub && vsub->cs.attached_via_ran != msc_a->c.ran->type) {
1377 LOG_MSC_A(msc_a, LOGL_ERROR,
1378 "Illegal situation: RAN type mismatch:"
1379 " attached via %s, received message via %s\n",
1380 osmo_rat_type_name(vsub->cs.attached_via_ran),
1381 osmo_rat_type_name(msc_a->c.ran->type));
1382 return -EACCES;
1383 }
1384
1385#if 0
1386 if (silent_call_reroute(conn, msg))
1387 return silent_call_rx(conn, msg);
1388#endif
1389
1390 switch (pdisc) {
Andreas Eversberg23b53e52023-06-21 13:30:58 +02001391 case GSM48_PDISC_GROUP_CC:
1392 case GSM48_PDISC_BCAST_CC:
1393 rc = gsm44068_rcv_bcc_gcc(msc_a, NULL, msg);
1394 break;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001395 case GSM48_PDISC_CC:
1396 rc = gsm0408_rcv_cc(msc_a, msg);
1397 break;
1398 case GSM48_PDISC_MM:
1399 rc = gsm0408_rcv_mm(msc_a, msg);
1400 break;
1401 case GSM48_PDISC_RR:
1402 rc = gsm0408_rcv_rr(msc_a, msg);
1403 break;
1404 case GSM48_PDISC_SMS:
1405 rc = gsm0411_rcv_sms(msc_a, msg);
1406 break;
1407 case GSM48_PDISC_MM_GPRS:
1408 case GSM48_PDISC_SM_GPRS:
1409 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unimplemented "
1410 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1411 rc = -ENOTSUP;
1412 break;
1413 case GSM48_PDISC_NC_SS:
1414 rc = gsm0911_rcv_nc_ss(msc_a, msg);
1415 break;
1416 case GSM48_PDISC_TEST:
1417 rc = gsm0414_rcv_test(msc_a, msg);
1418 break;
1419 default:
1420 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unknown "
1421 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1422 rc = -EINVAL;
1423 break;
1424 }
1425
1426 return rc;
1427}
1428
1429static void msc_a_up_call_assignment_complete(struct msc_a *msc_a, const struct ran_msg *ac)
1430{
Andreas Eversberg23b53e52023-06-21 13:30:58 +02001431 struct gsm_trans *cc_trans = msc_a->cc.active_trans, *gcc_trans;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001432 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 +01001433 const struct gsm0808_speech_codec *codec_if_known = ac->assignment_complete.codec_present ?
1434 &ac->assignment_complete.codec : NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001435
Andreas Eversberg23b53e52023-06-21 13:30:58 +02001436 /* For a voice group call, handling is performed by VGCS FSM */
1437 gcc_trans = trans_find_by_type(msc_a, TRANS_GCC);
1438 if (gcc_trans) {
1439 vgcs_vbs_caller_assign_cpl(gcc_trans);
1440 return;
1441 }
1442 gcc_trans = trans_find_by_type(msc_a, TRANS_BCC);
1443 if (gcc_trans) {
1444 vgcs_vbs_caller_assign_cpl(gcc_trans);
1445 return;
1446 }
1447
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001448 if (!rtps_to_ran) {
1449 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but no RTP stream is set up\n");
1450 return;
1451 }
1452 if (!cc_trans) {
Neels Hofmeyr550506a2022-01-13 23:31:57 +01001453 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but no CC transaction is active\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001454 return;
1455 }
1456
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001457 if (rtps_to_ran->use_osmux != ac->assignment_complete.osmux_present) {
1458 LOG_MSC_A_CAT(msc_a, DCC, LOGL_ERROR, "Osmux usage ass request and complete don't match: %d vs %d\n",
1459 rtps_to_ran->use_osmux, ac->assignment_complete.osmux_present);
1460 call_leg_release(msc_a->cc.call_leg);
1461 return;
1462 }
1463
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001464 if (codec_if_known) {
Neels Hofmeyr1a07bcd2024-01-19 06:37:57 +01001465 const struct codec_mapping *codec_assigned;
1466
Neels Hofmeyrbd8ac512024-01-19 06:38:11 +01001467 /* Check for unexpected codec with CSD */
1468 if (cc_trans->bearer_cap.transfer == GSM48_BCAP_ITCAP_UNR_DIG_INF &&
1469 codec_if_known->type != GSM0808_SCT_CSD) {
1470 LOG_TRANS(cc_trans, LOGL_ERROR, "Unexpected codec in Assignment Complete for CSD: %s\n",
1471 gsm0808_speech_codec_type_name(codec_if_known->type));
1472 call_leg_release(msc_a->cc.call_leg);
1473 return;
1474 }
1475
Neels Hofmeyr388d7c92023-03-22 19:03:15 +01001476 /* For 2G:
1477 * - The Assignment Complete has returned a specific codec (e.g. FR3 for AMR FR).
1478 * - Set this codec at the MGW endpoint facing the RAN.
1479 * - Also set this codec at the MGW endpoint facing the CN -- we require an exact match on both call
1480 * legs.
1481 * - TODO: be aware of transcoding that the MGW is capable of, e.g. AMR octet-aligned to AMR
1482 * bandwidth-efficient...
1483 *
1484 * For 3G:
1485 * - ran_infra->force_mgw_codecs_to_ran sets VND.3GPP.IUFP as single codec at the MGW towards RAN.
1486 * - ran_msg_iu.c always returns FR3 (AMR FR) for the assigned codec. Set that at the MGW towards CN.
1487 * - So the MGW decapsulates IuUP <-> AMR
1488 */
Neels Hofmeyr1a07bcd2024-01-19 06:37:57 +01001489 codec_assigned = codec_mapping_by_gsm0808_speech_codec_type(codec_if_known->type);
Neels Hofmeyr388d7c92023-03-22 19:03:15 +01001490 /* TODO: use codec_mapping_by_gsm0808_speech_codec() to also match on codec_if_known->cfg */
Neels Hofmeyr1a07bcd2024-01-19 06:37:57 +01001491 if (!codec_assigned) {
Neels Hofmeyr388d7c92023-03-22 19:03:15 +01001492 LOG_TRANS(cc_trans, LOGL_ERROR, "Unknown codec in Assignment Complete: %s\n",
1493 gsm0808_speech_codec_type_name(codec_if_known->type));
1494 call_leg_release(msc_a->cc.call_leg);
1495 return;
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001496 }
1497
Neels Hofmeyrcec51b32023-03-01 03:47:45 +01001498 /* Update RAN-side endpoint CI from Assignment result -- unless it is forced by the ran_infra, in which
1499 * case it remains unchanged as passed to the earlier call of call_leg_ensure_ci(). */
1500 if (msc_a->c.ran->force_mgw_codecs_to_ran.count == 0)
Neels Hofmeyr1a07bcd2024-01-19 06:37:57 +01001501 rtp_stream_set_one_codec(rtps_to_ran, &codec_assigned->sdp);
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001502
1503 /* Update codec filter with Assignment result, for the CN side */
Neels Hofmeyr1a07bcd2024-01-19 06:37:57 +01001504 cc_trans->cc.codecs.assignment = codec_assigned->sdp;
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001505 } else {
1506 /* No codec passed in Assignment Complete, set 'codecs.assignment' to none. */
1507 cc_trans->cc.codecs.assignment = (struct sdp_audio_codec){};
1508 LOG_TRANS(cc_trans, LOGL_INFO, "Assignment Complete without voice codec\n");
1509 }
1510
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001511 rtp_stream_set_remote_addr(rtps_to_ran, &ac->assignment_complete.remote_rtp);
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001512 if (rtps_to_ran->use_osmux)
1513 rtp_stream_set_remote_osmux_cid(rtps_to_ran,
1514 ac->assignment_complete.osmux_cid);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001515 rtp_stream_commit(rtps_to_ran);
1516
Neels Hofmeyr2d57d6e2022-01-13 21:39:58 +01001517 /* Remember the Codec List (BSS Supported) */
1518 if (ac->assignment_complete.codec_list_bss_supported)
1519 codec_filter_set_bss(&cc_trans->cc.codecs, ac->assignment_complete.codec_list_bss_supported);
1520
Oliver Smithceca8e62023-05-24 11:15:52 +02001521 trans_cc_filter_run(cc_trans);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001522 LOG_TRANS(cc_trans, LOGL_INFO, "Assignment Complete: RAN: %s, CN: %s\n",
1523 sdp_audio_codecs_to_str(&rtps_to_ran->codecs),
Oliver Smithc63c3a02023-05-24 10:48:07 +02001524 sdp_audio_codecs_to_str(&cc_trans->cc.local.audio_codecs));
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001525
1526 if (cc_on_assignment_done(cc_trans)) {
1527 /* If an error occurred, it was logged in cc_assignment_done() */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001528 call_leg_release(msc_a->cc.call_leg);
1529 return;
1530 }
1531}
1532
1533static void msc_a_up_call_assignment_failure(struct msc_a *msc_a, const struct ran_msg *af)
1534{
1535 struct gsm_trans *trans;
1536
1537 /* For a normal voice call, there will be an rtp_stream FSM. */
1538 if (msc_a->cc.call_leg && msc_a->cc.call_leg->rtp[RTP_TO_RAN]) {
1539 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing call\n");
1540 rtp_stream_release(msc_a->cc.call_leg->rtp[RTP_TO_RAN]);
1541 return;
1542 }
1543
Andreas Eversberg23b53e52023-06-21 13:30:58 +02001544 /* For a voice group call, release is performed by VGCS FSM */
1545 trans = trans_find_by_type(msc_a, TRANS_GCC);
1546 if (trans) {
1547 vgcs_vbs_caller_assign_fail(trans);
1548 return;
1549 }
1550 trans = trans_find_by_type(msc_a, TRANS_BCC);
1551 if (trans) {
1552 vgcs_vbs_caller_assign_fail(trans);
1553 return;
1554 }
1555
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001556 /* Otherwise, a silent call might be active */
1557 trans = trans_find_by_type(msc_a, TRANS_SILENT_CALL);
1558 if (trans) {
1559 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing silent call\n");
1560 trans_free(trans);
1561 return;
1562 }
1563
1564 /* Neither a voice call nor silent call assignment. Assume the worst and detach. */
1565 msc_a_release_cn(msc_a);
1566}
1567
1568static void msc_a_up_classmark_update(struct msc_a *msc_a, const struct osmo_gsm48_classmark *classmark,
1569 struct osmo_gsm48_classmark *dst)
1570{
1571 if (!dst) {
1572 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1573
1574 if (!vsub)
1575 dst = &msc_a->temporary_classmark;
1576 else
1577 dst = &vsub->classmark;
1578 }
1579
Martin Hauke3f07dac2019-11-14 17:49:08 +01001580 LOG_MSC_A(msc_a, LOGL_DEBUG, "A5 capabilities received from Classmark Update: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001581 osmo_gsm48_classmark_a5_name(classmark));
1582 osmo_gsm48_classmark_update(dst, classmark);
1583
1584 /* bump subscr conn FSM in case it is waiting for a Classmark Update */
1585 if (msc_a->c.fi->state == MSC_A_ST_WAIT_CLASSMARK_UPDATE)
1586 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CLASSMARK_UPDATE, NULL);
1587}
1588
1589static void msc_a_up_sapi_n_reject(struct msc_a *msc_a, const struct ran_msg *msg)
1590{
1591 int sapi = msg->sapi_n_reject.dlci & 0x7;
1592 if (sapi == UM_SAPI_SMS)
1593 gsm411_sapi_n_reject(msc_a);
1594}
1595
1596static int msc_a_up_ho(struct msc_a *msc_a, const struct msc_a_ran_dec_data *d, uint32_t ho_fi_event)
1597{
1598 if (!msc_a->ho.fi) {
1599 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Handover message, but no Handover ongoing: %s\n", d->ran_dec->msg_name);
1600 return -EINVAL;
1601 }
1602 return osmo_fsm_inst_dispatch(msc_a->ho.fi, ho_fi_event, (void*)d);
1603}
1604
1605int msc_a_ran_dec_from_msc_i(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1606{
1607 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Alexander Couzens2aaff752021-10-19 17:09:11 +02001608 struct gsm_network *net = msc_a_net(msc_a);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001609 const struct ran_msg *msg = d->ran_dec;
1610 int rc = -99;
1611
1612 switch (msg->msg_type) {
1613
1614 case RAN_MSG_COMPL_L3:
Neels Hofmeyr68f50da2020-06-24 14:22:52 +02001615 /* In case the cell_id from Complete Layer 3 Information lacks a PLMN, write the configured PLMN code
1616 * into msc_a->via_cell. Then overwrite with those bits obtained from Complete Layer 3 Information. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001617 msc_a->via_cell = (struct osmo_cell_global_id){
1618 .lai.plmn = msc_a_net(msc_a)->plmn,
1619 };
1620 gsm0808_cell_id_to_cgi(&msc_a->via_cell, msg->compl_l3.cell_id);
Neels Hofmeyrede90832022-01-13 18:13:15 +01001621
Neels Hofmeyre276ae92022-01-13 21:38:35 +01001622 /* If a codec list was sent along in the RAN_MSG_COMPL_L3, remember it for any upcoming codec
1623 * resolution. */
1624 if (msg->compl_l3.codec_list_bss_supported) {
1625 msc_a->cc.compl_l3_codec_list_bss_supported = *msg->compl_l3.codec_list_bss_supported;
1626 if (log_check_level(msc_a->c.ran->log_subsys, LOGL_DEBUG)) {
1627 struct sdp_audio_codecs ac = {};
1628 sdp_audio_codecs_from_speech_codec_list(&ac, &msc_a->cc.compl_l3_codec_list_bss_supported);
1629 LOG_MSC_A(msc_a, LOGL_DEBUG, "Complete Layer 3: Codec List (BSS Supported): %s\n",
1630 sdp_audio_codecs_to_str(&ac));
1631 }
1632 }
1633
Neels Hofmeyrede90832022-01-13 18:13:15 +01001634 /* Submit the Complete Layer 3 Information DTAP */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001635 rc = msc_a_up_l3(msc_a, msg->compl_l3.msg);
1636 if (!rc) {
1637 struct ran_conn *conn = msub_ran_conn(msc_a->c.msub);
1638 if (conn)
1639 ran_peer_cells_seen_add(conn->ran_peer, msg->compl_l3.cell_id);
1640 }
1641 break;
1642
1643 case RAN_MSG_DTAP:
1644 rc = msc_a_up_l3(msc_a, msg->dtap);
1645 break;
1646
1647 case RAN_MSG_CLEAR_REQUEST:
1648 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
1649 break;
1650
1651 case RAN_MSG_CLEAR_COMPLETE:
1652 switch (msc_a->c.fi->state) {
1653 case MSC_A_ST_RELEASING:
1654 msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
1655 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
1656 break;
1657 case MSC_A_ST_RELEASED:
1658 break;
1659 default:
1660 LOG_MSC_A(msc_a, LOGL_ERROR, "Received Clear Complete event, but did not send Clear Command\n");
1661 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
1662 break;
1663 }
1664 rc = 0;
1665 break;
1666
1667 case RAN_MSG_CLASSMARK_UPDATE:
1668 msc_a_up_classmark_update(msc_a, msg->classmark_update.classmark, NULL);
1669 rc = 0;
1670 break;
1671
1672 case RAN_MSG_CIPHER_MODE_COMPLETE:
1673 /* Remember what Ciphering was negotiated (e.g. for Handover) */
1674 if (msg->cipher_mode_complete.alg_id) {
1675 msc_a->geran_encr.alg_id = msg->cipher_mode_complete.alg_id;
1676 LOG_MSC_A(msc_a, LOGL_DEBUG, "Cipher Mode Complete: chosen encryption algorithm: A5/%u\n",
1677 msc_a->geran_encr.alg_id - 1);
Alexander Couzens2aaff752021-10-19 17:09:11 +02001678 }
1679
1680 if (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU) {
1681 int16_t utran_encryption;
1682
1683 /* utran: ensure chosen ciphering mode is allowed
1684 * If the IE is missing (utran_encryption == -1), parse it as no encryption */
1685 utran_encryption = msg->cipher_mode_complete.utran_encryption;
1686 if (utran_encryption == -1)
1687 utran_encryption = 0;
1688 if ((net->uea_encryption_mask & (1 << utran_encryption)) == 0) {
1689 /* cipher disallowed */
1690 LOG_MSC_A(msc_a, LOGL_ERROR, "Cipher Mode Complete: RNC chosen forbidden ciphering UEA%d\n",
1691 msg->cipher_mode_complete.utran_encryption);
1692 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT);
1693 rc = 0;
1694 break;
1695 }
1696 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001697 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_COMPL);
1698 rc = 0;
Neels Hofmeyre9a39112019-08-29 00:10:49 +02001699
1700 /* Evaluate enclosed L3 message, typically Identity Response (IMEISV) */
1701 if (msg->cipher_mode_complete.l3_msg) {
1702 unsigned char *data = (unsigned char*)(msg->cipher_mode_complete.l3_msg->val);
1703 uint16_t len = msg->cipher_mode_complete.l3_msg->len;
1704 struct msgb *dtap = msgb_alloc(len, "DTAP from Cipher Mode Complete");
1705 unsigned char *pos = msgb_put(dtap, len);
1706 memcpy(pos, data, len);
1707 dtap->l3h = pos;
1708 rc = msc_a_up_l3(msc_a, dtap);
1709 msgb_free(dtap);
1710 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001711 break;
1712
1713 case RAN_MSG_CIPHER_MODE_REJECT:
1714 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT);
1715 rc = 0;
1716 break;
1717
1718 case RAN_MSG_ASSIGNMENT_COMPLETE:
1719 msc_a_up_call_assignment_complete(msc_a, msg);
1720 rc = 0;
1721 break;
1722
1723 case RAN_MSG_ASSIGNMENT_FAILURE:
1724 msc_a_up_call_assignment_failure(msc_a, msg);
1725 rc = 0;
1726 break;
1727
1728 case RAN_MSG_SAPI_N_REJECT:
1729 msc_a_up_sapi_n_reject(msc_a, msg);
1730 rc = 0;
1731 break;
1732
1733 case RAN_MSG_HANDOVER_PERFORMED:
1734 /* The BSS lets us know that a handover happened within the BSS, which doesn't concern us. */
1735 LOG_MSC_A(msc_a, LOGL_ERROR, "'Handover Performed' handling not implemented\n");
1736 break;
1737
1738 case RAN_MSG_HANDOVER_REQUIRED:
1739 /* The BSS lets us know that it wants to handover to a different cell */
1740 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_HANDOVER_REQUIRED, (void*)&msg->handover_required);
1741 break;
1742
1743 case RAN_MSG_HANDOVER_FAILURE:
1744 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1745 break;
1746
Keith Whytea1a70be2021-05-16 02:59:52 +02001747 case RAN_MSG_LCLS_STATUS:
1748 /* The BSS sends us LCLS_STATUS. We do nothing for now, but it is not an error. */
1749 LOG_MSC_A(msc_a, LOGL_DEBUG, "LCLS_STATUS (%s) received from MSC-I\n",
1750 gsm0808_lcls_status_name(msg->lcls_status.status));
1751 rc = 0;
1752 break;
1753
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001754 default:
1755 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-I not implemented: %s\n", ran_msg_type_name(msg->msg_type));
1756 rc = -ENOTSUP;
1757 break;
1758 }
1759 return rc;
1760}
1761
Andreas Eversbergf7c6f142023-04-23 12:10:42 +02001762static int msc_a_rx_vgcs_bss_decoded(struct osmo_fsm_inst *caller_fi, void *caller_data, const struct ran_msg *msg)
1763{
1764 struct vgcs_bss *bss = caller_data;
1765 struct msc_a *msc_a = (bss->trans) ? bss->trans->msc_a : NULL;
1766 int rc = 0;
1767
1768 switch (msg->msg_type) {
1769 case RAN_MSG_VGCS_VBS_SETUP_ACK:
1770 /* The BSS accepts VGCS/VBS and sends us supported features. */
1771 vgcs_vbs_setup_ack(bss, msg);
1772 break;
1773 case RAN_MSG_VGCS_VBS_SETUP_REFUSE:
1774 /* The BSS refuses VGCS/VBS. */
1775 vgcs_vbs_setup_refuse(bss, msg);
1776 break;
1777 case RAN_MSG_UPLINK_REQUEST:
1778 /* A mobile station requests the uplink on a VGCS channel. */
1779 vgcs_uplink_request(bss, msg);
1780 break;
1781 case RAN_MSG_UPLINK_REQUEST_CNF:
1782 /* The uplink on a VGCS channel has been established. */
1783 vgcs_uplink_request_cnf(bss, msg);
1784 break;
1785 case RAN_MSG_UPLINK_APPLICATION_DATA:
1786 /* Application data received on the uplink of a VGCS channel. */
1787 vgcs_app_data(bss, msg);
1788 break;
1789 case RAN_MSG_DTAP:
1790 /* BSS confirms the release of the channel. */
1791 vgcs_bss_dtap(bss, msg);
1792 break;
1793 case RAN_MSG_UPLINK_RELEASE_IND:
1794 /* A mobile station releases the uplink on a VGCS channel. */
1795 vgcs_uplink_release_ind(bss, msg);
1796 break;
1797 case RAN_MSG_CLEAR_REQUEST:
1798 /* BSS indicated that the channel has been released. */
1799 vgcs_vbs_clear_req(bss, msg);
1800 break;
1801 case RAN_MSG_CLEAR_COMPLETE:
1802 /* BSS confirms the release of the channel. */
1803 vgcs_vbs_clear_cpl(bss, msg);
1804 break;
1805 default:
1806 LOG_MSC_A(msc_a, LOGL_ERROR, "VGCS message from BSS not implemented: %s\n",
1807 ran_msg_type_name(msg->msg_type));
1808 rc = -ENOTSUP;
1809 break;
1810 }
1811 return rc;
1812}
1813
1814int msc_a_rx_vgcs_bss(struct vgcs_bss *bss, struct ran_conn *from_conn, struct msgb *msg)
1815{
1816 struct ran_dec ran_dec;
1817
1818 /* Feed through the decoding mechanism ran_msg. The decoded message arrives in msc_a_rx_vgcs_decoded() */
1819 ran_dec = (struct ran_dec) {
1820 .caller_data = bss,
1821 .decode_cb = msc_a_rx_vgcs_bss_decoded,
1822 };
1823 struct ran_peer *ran_peer = from_conn->ran_peer;
1824 struct ran_infra *ran = ran_peer->sri->ran;
1825 if (!ran->ran_dec_l2) {
1826 LOGP(DMSC, LOGL_ERROR, "No ran_dec_l2() defined for RAN type %s\n",
1827 osmo_rat_type_name(ran->type));
1828 return -ENOTSUP;
1829 }
1830 return ran->ran_dec_l2(&ran_dec, msg);
1831}
1832
1833static int msc_a_rx_vgcs_cell_decoded(struct osmo_fsm_inst *caller_fi, void *caller_data, const struct ran_msg *msg)
1834{
1835 struct vgcs_bss_cell *cell = caller_data;
1836 struct msc_a *msc_a = (cell->bss && cell->bss->trans) ? cell->bss->trans->msc_a : NULL;
1837 int rc = 0;
1838
1839 switch (msg->msg_type) {
1840 case RAN_MSG_VGCS_VBS_ASSIGN_RES:
1841 /* The BSS accepts VGCS/VBS channel assignment. */
1842 vgcs_vbs_assign_result(cell, msg);
1843 break;
1844 case RAN_MSG_VGCS_VBS_ASSIGN_FAIL:
1845 /* The BSS refuses VGCS/VBS channel assignment. */
1846 vgcs_vbs_assign_fail(cell, msg);
1847 break;
1848 case RAN_MSG_VGCS_VBS_QUEUING_IND:
1849 /* The BSS needs more time for VGCS/VBS channel assignment. */
1850 vgcs_vbs_queuing_ind(cell);
1851 break;
1852 case RAN_MSG_VGCS_VBS_ASSIGN_STATUS:
1853 /* The BSS gives cell status about VGCS/VBS channel. */
1854 vgcs_vbs_assign_status(cell, msg);
1855 break;
1856 case RAN_MSG_CLEAR_REQUEST:
1857 /* BSS indicated that the channel has been released. */
1858 vgcs_vbs_clear_req_channel(cell, msg);
1859 break;
1860 case RAN_MSG_CLEAR_COMPLETE:
1861 /* BSS confirms the release of the channel. */
1862 vgcs_vbs_clear_cpl_channel(cell, msg);
1863 break;
1864 default:
1865 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from BSS leg not implemented: %s\n",
1866 ran_msg_type_name(msg->msg_type));
1867 rc = -ENOTSUP;
1868 break;
1869 }
1870 return rc;
1871}
1872
1873int msc_a_rx_vgcs_cell(struct vgcs_bss_cell *cell, struct ran_conn *from_conn, struct msgb *msg)
1874{
1875 struct ran_dec ran_dec;
1876
1877 /* Feed through the decoding mechanism ran_msg. The decoded message arrives in msc_a_rx_vgcs_decoded() */
1878 ran_dec = (struct ran_dec) {
1879 .caller_data = cell,
1880 .decode_cb = msc_a_rx_vgcs_cell_decoded,
1881 };
1882 struct ran_peer *ran_peer = from_conn->ran_peer;
1883 struct ran_infra *ran = ran_peer->sri->ran;
1884 if (!ran->ran_dec_l2) {
1885 LOGP(DMSC, LOGL_ERROR, "No ran_dec_l2() defined for RAN type %s\n",
1886 osmo_rat_type_name(ran->type));
1887 return -ENOTSUP;
1888 }
1889 return ran->ran_dec_l2(&ran_dec, msg);
1890}
1891
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001892static int msc_a_ran_dec_from_msc_t(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1893{
1894 struct msc_t *msc_t = msc_a_msc_t(msc_a);
1895 int rc = -99;
1896
1897 if (!msc_t) {
1898 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx message from MSC-T role, but I have no active MSC-T role.\n");
1899 return -EINVAL;
1900 }
1901
1902 OSMO_ASSERT(d->ran_dec);
1903
1904 switch (d->ran_dec->msg_type) {
1905
1906 case RAN_MSG_CLEAR_REQUEST:
1907 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_MO_CLOSE, NULL);
1908 break;
1909
1910 case RAN_MSG_CLEAR_COMPLETE:
1911 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_CLEAR_COMPLETE, NULL);
1912 break;
1913
1914 case RAN_MSG_CLASSMARK_UPDATE:
1915 msc_a_up_classmark_update(msc_a, d->ran_dec->classmark_update.classmark, &msc_t->classmark);
1916 rc = 0;
1917 break;
1918
1919 case RAN_MSG_HANDOVER_REQUEST_ACK:
1920 /* new BSS accepts Handover */
1921 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_REQUEST_ACK);
1922 break;
1923
1924 case RAN_MSG_HANDOVER_DETECT:
1925 /* new BSS signals the MS is DETECTed on the new lchan */
1926 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_DETECT);
1927 break;
1928
1929 case RAN_MSG_HANDOVER_COMPLETE:
1930 /* new BSS signals the MS has fully moved to the new lchan */
1931 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_COMPLETE);
1932 break;
1933
1934 case RAN_MSG_HANDOVER_FAILURE:
1935 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1936 break;
1937
1938 default:
1939 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-T not implemented: %s\n",
1940 ran_msg_type_name(d->ran_dec->msg_type));
1941 rc = -ENOTSUP;
1942 break;
1943 }
1944 return rc;
1945}
1946
1947int msc_a_ran_decode_cb(struct osmo_fsm_inst *msc_a_fi, void *data, const struct ran_msg *msg)
1948{
1949 struct msc_a *msc_a = msc_a_fi_priv(msc_a_fi);
1950 struct msc_a_ran_dec_data *d = data;
1951 int rc = -99;
1952
1953 d->ran_dec = msg;
1954
1955 switch (d->from_role) {
1956 case MSC_ROLE_I:
1957 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode: %s\n", msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1958 rc = msc_a_ran_dec_from_msc_i(msc_a, d);
1959 break;
1960
1961 case MSC_ROLE_T:
1962 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode from MSC-T: %s\n",
1963 msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1964 rc = msc_a_ran_dec_from_msc_t(msc_a, d);
1965 break;
1966
1967 default:
1968 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from invalid role %s: %s\n", msc_role_name(d->from_role),
1969 ran_msg_type_name(msg->msg_type));
1970 return -ENOTSUP;
1971 }
1972
1973 if (rc)
1974 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),
1975 msc_role_name(d->from_role));
1976 return rc;
1977}
1978
1979/* Your typical DTAP via FORWARD_ACCESS_SIGNALLING_REQUEST */
1980int _msc_a_ran_down(struct msc_a *msc_a, enum msc_role to_role, const struct ran_msg *ran_msg,
1981 const char *file, int line)
1982{
1983 return _msc_a_msg_down(msc_a, to_role, msub_role_to_role_event(msc_a->c.msub, MSC_ROLE_A, to_role),
1984 ran_msg, file, line);
1985}
1986
1987/* To transmit more complex events than just FORWARD_ACCESS_SIGNALLING_REQUEST, e.g. an
1988 * MSC_T_EV_FROM_A_PREPARE_HANDOVER_REQUEST */
1989int _msc_a_msg_down(struct msc_a *msc_a, enum msc_role to_role, uint32_t to_role_event,
1990 const struct ran_msg *ran_msg,
1991 const char *file, int line)
1992{
1993 struct an_apdu an_apdu = {
1994 .an_proto = msc_a->c.ran->an_proto,
1995 .msg = msc_role_ran_encode(msc_a->c.fi, ran_msg),
1996 };
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001997 if (!an_apdu.msg)
1998 return -EIO;
Vadim Yanitskiyc44342b2021-12-07 18:32:35 +03001999 return _msub_role_dispatch(msc_a->c.msub, to_role, to_role_event, &an_apdu, file, line);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002000}
2001
2002int msc_a_tx_dtap_to_i(struct msc_a *msc_a, struct msgb *dtap)
2003{
2004 struct ran_msg ran_msg;
Neels Hofmeyrc192c0b2019-10-07 21:41:18 +02002005 struct gsm48_hdr *gh = msgb_l3(dtap) ? : dtap->data;
2006 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002007
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02002008 if (!msc_a) {
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02002009 LOGP(DMSC, LOGL_ERROR, "Attempt to send DTAP to NULL MSC-A, dropping message: %s %s\n",
2010 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
2011 msgb_free(dtap);
2012 return -EIO;
2013 }
2014
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002015 if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) {
2016 /* The SGs connection to the MME always is at the MSC-A. */
2017 return sgs_iface_tx_dtap_ud(msc_a, dtap);
2018 }
2019
Neels Hofmeyrc192c0b2019-10-07 21:41:18 +02002020 LOG_MSC_A(msc_a, LOGL_DEBUG, "Sending DTAP: %s %s\n",
2021 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
2022
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002023 ran_msg = (struct ran_msg){
2024 .msg_type = RAN_MSG_DTAP,
2025 .dtap = dtap,
2026 };
2027 return msc_a_ran_down(msc_a, MSC_ROLE_I, &ran_msg);
2028}
2029
2030struct msc_a *msc_a_for_vsub(const struct vlr_subscr *vsub, bool valid_conn_only)
2031{
2032 struct msc_a *msc_a = msub_msc_a(msub_for_vsub(vsub));
2033 if (valid_conn_only && !msc_a_is_accepted(msc_a))
2034 return NULL;
2035 return msc_a;
2036}
2037
2038int msc_tx_common_id(struct msc_a *msc_a, enum msc_role to_role)
2039{
2040 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Vadim Yanitskiy435f67f2021-06-06 15:47:49 +02002041 if (vsub == NULL)
2042 return -ENODEV;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002043 struct ran_msg msg = {
2044 .msg_type = RAN_MSG_COMMON_ID,
2045 .common_id = {
2046 .imsi = vsub->imsi,
Pau Espin Pedrol67106702021-04-27 18:20:15 +02002047 .last_eutran_plmn_present = vsub->sgs.last_eutran_plmn_present,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002048 },
2049 };
Pau Espin Pedrol67106702021-04-27 18:20:15 +02002050 if (vsub->sgs.last_eutran_plmn_present) {
2051 memcpy(&msg.common_id.last_eutran_plmn, &vsub->sgs.last_eutran_plmn,
2052 sizeof(vsub->sgs.last_eutran_plmn));
2053 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002054
2055 return msc_a_ran_down(msc_a, to_role, &msg);
2056}
2057
2058static int msc_a_start_assignment(struct msc_a *msc_a, struct gsm_trans *cc_trans)
2059{
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002060 struct call_leg *cl;
2061 bool cn_rtp_available;
2062 bool ran_rtp_available;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002063
2064 OSMO_ASSERT(!msc_a->cc.active_trans);
2065 msc_a->cc.active_trans = cc_trans;
2066
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01002067 cc_trans->cc.codecs.assignment = (struct sdp_audio_codec){};
2068
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002069 OSMO_ASSERT(cc_trans && cc_trans->type == TRANS_CC);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002070 cl = msc_a_ensure_call_leg(msc_a, cc_trans);
2071 if (!cl)
2072 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002073
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002074 /* See if we can set a preliminary codec. If not, pass none for the time being. */
Oliver Smithceca8e62023-05-24 11:15:52 +02002075 trans_cc_filter_run(cc_trans);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002076
2077 cn_rtp_available = call_leg_local_ip(cl, RTP_TO_CN);
2078 ran_rtp_available = call_leg_local_ip(cl, RTP_TO_RAN);
2079
2080 /* Set up RTP ports for both RAN and CN side. Even though we ask for both at the same time, the
2081 * osmo_mgcpc_ep_fsm automagically waits for the first CRCX to complete before firing the second CRCX. The one
2082 * issued first here will also be the first CRCX sent to the MGW. Usually both still need to be set up. */
2083 if (!cn_rtp_available)
Andreas Eversberg712b28e2023-06-21 11:17:26 +02002084 call_leg_ensure_ci(cl, RTP_TO_CN, cc_trans->call_id, cc_trans,
Oliver Smithc63c3a02023-05-24 10:48:07 +02002085 &cc_trans->cc.local.audio_codecs, NULL);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002086 if (!ran_rtp_available) {
2087 struct sdp_audio_codecs *codecs;
2088 if (msc_a->c.ran->force_mgw_codecs_to_ran.count)
2089 codecs = &msc_a->c.ran->force_mgw_codecs_to_ran;
2090 else
Oliver Smithc63c3a02023-05-24 10:48:07 +02002091 codecs = &cc_trans->cc.local.audio_codecs;
Andreas Eversberg712b28e2023-06-21 11:17:26 +02002092 return call_leg_ensure_ci(cl, RTP_TO_RAN, cc_trans->call_id, cc_trans, codecs, NULL);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002093 }
2094
2095 /* Should these already be set up, immediately continue by retriggering the events signalling that the RTP
2096 * ports are available. The ordering is: first CN, then RAN. */
2097 if (cn_rtp_available && ran_rtp_available)
2098 return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_RAN]);
2099 else if (cn_rtp_available)
2100 return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_CN]);
2101 /* Otherwise wait for MGCP response and continue from there. */
2102 return 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002103}
2104
2105int msc_a_try_call_assignment(struct gsm_trans *cc_trans)
2106{
2107 struct msc_a *msc_a = cc_trans->msc_a;
2108 OSMO_ASSERT(cc_trans->type == TRANS_CC);
2109
2110 if (msc_a->cc.active_trans == cc_trans) {
Neels Hofmeyrb4ef5e72019-08-30 01:11:12 +02002111 LOG_MSC_A(msc_a, LOGL_DEBUG, "Assignment for this trans already started earlier\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002112 return 0;
2113 }
2114
2115 if (msc_a->cc.active_trans) {
2116 LOG_MSC_A(msc_a, LOGL_INFO, "Another call is already ongoing, not assigning yet\n");
2117 return 0;
2118 }
2119
2120 LOG_MSC_A(msc_a, LOGL_DEBUG, "Starting call assignment\n");
2121 return msc_a_start_assignment(msc_a, cc_trans);
2122}
2123
Neels Hofmeyr5d53c602022-04-24 23:37:07 +02002124/* Map CM Service type to use token.
2125 * Given a CM Service type, return a matching token intended for osmo_use_count.
2126 * For unknown service type, return NULL.
2127 */
Andreas Eversbergcd8bd452023-07-11 14:16:24 +02002128const 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 +01002129{
Andreas Eversbergcd8bd452023-07-11 14:16:24 +02002130 struct gsm_network *net = msc_a_net(msc_a);
2131
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002132 switch (cm_service_type) {
2133 case GSM48_CMSERV_MO_CALL_PACKET:
2134 case GSM48_CMSERV_EMERGENCY:
2135 return MSC_A_USE_CM_SERVICE_CC;
2136
2137 case GSM48_CMSERV_SMS:
2138 return MSC_A_USE_CM_SERVICE_SMS;
2139
2140 case GSM48_CMSERV_SUP_SERV:
2141 return MSC_A_USE_CM_SERVICE_SS;
2142
Andreas Eversberg456c6f72023-04-23 11:54:16 +02002143 case GSM48_CMSERV_VGCS:
Andreas Eversbergcd8bd452023-07-11 14:16:24 +02002144 if (net->asci.enable)
2145 return MSC_A_USE_CM_SERVICE_GCC;
2146 else
2147 return NULL;
Andreas Eversberg456c6f72023-04-23 11:54:16 +02002148
2149 case GSM48_CMSERV_VBS:
Andreas Eversbergcd8bd452023-07-11 14:16:24 +02002150 if (net->asci.enable)
2151 return MSC_A_USE_CM_SERVICE_BCC;
2152 else
2153 return NULL;
Andreas Eversberg456c6f72023-04-23 11:54:16 +02002154
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002155 default:
2156 return NULL;
2157 }
2158}
2159
2160void msc_a_release_cn(struct msc_a *msc_a)
2161{
2162 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CN_CLOSE, NULL);
2163}
2164
2165void msc_a_release_mo(struct msc_a *msc_a, enum gsm48_gsm_cause gsm_cause)
2166{
2167 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
2168}