blob: a933bd22d573529426d0c5388b7b1cb60140ad96 [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 Hofmeyr388d7c92023-03-22 19:03:15 +01001465 const struct codec_mapping *codec_cn;
1466 /* For 2G:
1467 * - The Assignment Complete has returned a specific codec (e.g. FR3 for AMR FR).
1468 * - Set this codec at the MGW endpoint facing the RAN.
1469 * - Also set this codec at the MGW endpoint facing the CN -- we require an exact match on both call
1470 * legs.
1471 * - TODO: be aware of transcoding that the MGW is capable of, e.g. AMR octet-aligned to AMR
1472 * bandwidth-efficient...
1473 *
1474 * For 3G:
1475 * - ran_infra->force_mgw_codecs_to_ran sets VND.3GPP.IUFP as single codec at the MGW towards RAN.
1476 * - ran_msg_iu.c always returns FR3 (AMR FR) for the assigned codec. Set that at the MGW towards CN.
1477 * - So the MGW decapsulates IuUP <-> AMR
1478 */
1479 codec_cn = codec_mapping_by_gsm0808_speech_codec_type(codec_if_known->type);
1480 /* TODO: use codec_mapping_by_gsm0808_speech_codec() to also match on codec_if_known->cfg */
1481 if (!codec_cn) {
1482 LOG_TRANS(cc_trans, LOGL_ERROR, "Unknown codec in Assignment Complete: %s\n",
1483 gsm0808_speech_codec_type_name(codec_if_known->type));
1484 call_leg_release(msc_a->cc.call_leg);
1485 return;
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001486 }
1487
Oliver Smithf3941512023-08-23 13:11:44 +02001488 /* Check for unexpected codec with CSD */
1489 if (cc_trans->bearer_cap.transfer == GSM48_BCAP_ITCAP_UNR_DIG_INF &&
1490 codec_if_known->type != GSM0808_SCT_CSD) {
1491 LOG_TRANS(cc_trans, LOGL_ERROR, "Unexpected codec in Assignment Complete for CSD: %s\n",
1492 gsm0808_speech_codec_type_name(codec_if_known->type));
1493 call_leg_release(msc_a->cc.call_leg);
1494 return;
1495 }
1496
Neels Hofmeyrcec51b32023-03-01 03:47:45 +01001497 /* Update RAN-side endpoint CI from Assignment result -- unless it is forced by the ran_infra, in which
1498 * case it remains unchanged as passed to the earlier call of call_leg_ensure_ci(). */
1499 if (msc_a->c.ran->force_mgw_codecs_to_ran.count == 0)
1500 rtp_stream_set_one_codec(rtps_to_ran, &codec_cn->sdp);
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001501
1502 /* Update codec filter with Assignment result, for the CN side */
1503 cc_trans->cc.codecs.assignment = codec_cn->sdp;
1504 } else {
1505 /* No codec passed in Assignment Complete, set 'codecs.assignment' to none. */
1506 cc_trans->cc.codecs.assignment = (struct sdp_audio_codec){};
1507 LOG_TRANS(cc_trans, LOGL_INFO, "Assignment Complete without voice codec\n");
1508 }
1509
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001510 rtp_stream_set_remote_addr(rtps_to_ran, &ac->assignment_complete.remote_rtp);
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001511 if (rtps_to_ran->use_osmux)
1512 rtp_stream_set_remote_osmux_cid(rtps_to_ran,
1513 ac->assignment_complete.osmux_cid);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001514 rtp_stream_commit(rtps_to_ran);
1515
Neels Hofmeyr2d57d6e2022-01-13 21:39:58 +01001516 /* Remember the Codec List (BSS Supported) */
1517 if (ac->assignment_complete.codec_list_bss_supported)
1518 codec_filter_set_bss(&cc_trans->cc.codecs, ac->assignment_complete.codec_list_bss_supported);
1519
Oliver Smithceca8e62023-05-24 11:15:52 +02001520 trans_cc_filter_run(cc_trans);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001521 LOG_TRANS(cc_trans, LOGL_INFO, "Assignment Complete: RAN: %s, CN: %s\n",
1522 sdp_audio_codecs_to_str(&rtps_to_ran->codecs),
Oliver Smithc63c3a02023-05-24 10:48:07 +02001523 sdp_audio_codecs_to_str(&cc_trans->cc.local.audio_codecs));
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001524
1525 if (cc_on_assignment_done(cc_trans)) {
1526 /* If an error occurred, it was logged in cc_assignment_done() */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001527 call_leg_release(msc_a->cc.call_leg);
1528 return;
1529 }
1530}
1531
1532static void msc_a_up_call_assignment_failure(struct msc_a *msc_a, const struct ran_msg *af)
1533{
1534 struct gsm_trans *trans;
1535
1536 /* For a normal voice call, there will be an rtp_stream FSM. */
1537 if (msc_a->cc.call_leg && msc_a->cc.call_leg->rtp[RTP_TO_RAN]) {
1538 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing call\n");
1539 rtp_stream_release(msc_a->cc.call_leg->rtp[RTP_TO_RAN]);
1540 return;
1541 }
1542
Andreas Eversberg23b53e52023-06-21 13:30:58 +02001543 /* For a voice group call, release is performed by VGCS FSM */
1544 trans = trans_find_by_type(msc_a, TRANS_GCC);
1545 if (trans) {
1546 vgcs_vbs_caller_assign_fail(trans);
1547 return;
1548 }
1549 trans = trans_find_by_type(msc_a, TRANS_BCC);
1550 if (trans) {
1551 vgcs_vbs_caller_assign_fail(trans);
1552 return;
1553 }
1554
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001555 /* Otherwise, a silent call might be active */
1556 trans = trans_find_by_type(msc_a, TRANS_SILENT_CALL);
1557 if (trans) {
1558 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing silent call\n");
1559 trans_free(trans);
1560 return;
1561 }
1562
1563 /* Neither a voice call nor silent call assignment. Assume the worst and detach. */
1564 msc_a_release_cn(msc_a);
1565}
1566
1567static void msc_a_up_classmark_update(struct msc_a *msc_a, const struct osmo_gsm48_classmark *classmark,
1568 struct osmo_gsm48_classmark *dst)
1569{
1570 if (!dst) {
1571 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1572
1573 if (!vsub)
1574 dst = &msc_a->temporary_classmark;
1575 else
1576 dst = &vsub->classmark;
1577 }
1578
Martin Hauke3f07dac2019-11-14 17:49:08 +01001579 LOG_MSC_A(msc_a, LOGL_DEBUG, "A5 capabilities received from Classmark Update: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001580 osmo_gsm48_classmark_a5_name(classmark));
1581 osmo_gsm48_classmark_update(dst, classmark);
1582
1583 /* bump subscr conn FSM in case it is waiting for a Classmark Update */
1584 if (msc_a->c.fi->state == MSC_A_ST_WAIT_CLASSMARK_UPDATE)
1585 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CLASSMARK_UPDATE, NULL);
1586}
1587
1588static void msc_a_up_sapi_n_reject(struct msc_a *msc_a, const struct ran_msg *msg)
1589{
1590 int sapi = msg->sapi_n_reject.dlci & 0x7;
1591 if (sapi == UM_SAPI_SMS)
1592 gsm411_sapi_n_reject(msc_a);
1593}
1594
1595static int msc_a_up_ho(struct msc_a *msc_a, const struct msc_a_ran_dec_data *d, uint32_t ho_fi_event)
1596{
1597 if (!msc_a->ho.fi) {
1598 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Handover message, but no Handover ongoing: %s\n", d->ran_dec->msg_name);
1599 return -EINVAL;
1600 }
1601 return osmo_fsm_inst_dispatch(msc_a->ho.fi, ho_fi_event, (void*)d);
1602}
1603
1604int msc_a_ran_dec_from_msc_i(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1605{
1606 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Alexander Couzens2aaff752021-10-19 17:09:11 +02001607 struct gsm_network *net = msc_a_net(msc_a);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001608 const struct ran_msg *msg = d->ran_dec;
1609 int rc = -99;
1610
1611 switch (msg->msg_type) {
1612
1613 case RAN_MSG_COMPL_L3:
Neels Hofmeyr68f50da2020-06-24 14:22:52 +02001614 /* In case the cell_id from Complete Layer 3 Information lacks a PLMN, write the configured PLMN code
1615 * into msc_a->via_cell. Then overwrite with those bits obtained from Complete Layer 3 Information. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001616 msc_a->via_cell = (struct osmo_cell_global_id){
1617 .lai.plmn = msc_a_net(msc_a)->plmn,
1618 };
1619 gsm0808_cell_id_to_cgi(&msc_a->via_cell, msg->compl_l3.cell_id);
Neels Hofmeyrede90832022-01-13 18:13:15 +01001620
Neels Hofmeyre276ae92022-01-13 21:38:35 +01001621 /* If a codec list was sent along in the RAN_MSG_COMPL_L3, remember it for any upcoming codec
1622 * resolution. */
1623 if (msg->compl_l3.codec_list_bss_supported) {
1624 msc_a->cc.compl_l3_codec_list_bss_supported = *msg->compl_l3.codec_list_bss_supported;
1625 if (log_check_level(msc_a->c.ran->log_subsys, LOGL_DEBUG)) {
1626 struct sdp_audio_codecs ac = {};
1627 sdp_audio_codecs_from_speech_codec_list(&ac, &msc_a->cc.compl_l3_codec_list_bss_supported);
1628 LOG_MSC_A(msc_a, LOGL_DEBUG, "Complete Layer 3: Codec List (BSS Supported): %s\n",
1629 sdp_audio_codecs_to_str(&ac));
1630 }
1631 }
1632
Neels Hofmeyrede90832022-01-13 18:13:15 +01001633 /* Submit the Complete Layer 3 Information DTAP */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001634 rc = msc_a_up_l3(msc_a, msg->compl_l3.msg);
1635 if (!rc) {
1636 struct ran_conn *conn = msub_ran_conn(msc_a->c.msub);
1637 if (conn)
1638 ran_peer_cells_seen_add(conn->ran_peer, msg->compl_l3.cell_id);
1639 }
1640 break;
1641
1642 case RAN_MSG_DTAP:
1643 rc = msc_a_up_l3(msc_a, msg->dtap);
1644 break;
1645
1646 case RAN_MSG_CLEAR_REQUEST:
1647 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
1648 break;
1649
1650 case RAN_MSG_CLEAR_COMPLETE:
1651 switch (msc_a->c.fi->state) {
1652 case MSC_A_ST_RELEASING:
1653 msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
1654 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
1655 break;
1656 case MSC_A_ST_RELEASED:
1657 break;
1658 default:
1659 LOG_MSC_A(msc_a, LOGL_ERROR, "Received Clear Complete event, but did not send Clear Command\n");
1660 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
1661 break;
1662 }
1663 rc = 0;
1664 break;
1665
1666 case RAN_MSG_CLASSMARK_UPDATE:
1667 msc_a_up_classmark_update(msc_a, msg->classmark_update.classmark, NULL);
1668 rc = 0;
1669 break;
1670
1671 case RAN_MSG_CIPHER_MODE_COMPLETE:
1672 /* Remember what Ciphering was negotiated (e.g. for Handover) */
1673 if (msg->cipher_mode_complete.alg_id) {
1674 msc_a->geran_encr.alg_id = msg->cipher_mode_complete.alg_id;
1675 LOG_MSC_A(msc_a, LOGL_DEBUG, "Cipher Mode Complete: chosen encryption algorithm: A5/%u\n",
1676 msc_a->geran_encr.alg_id - 1);
Alexander Couzens2aaff752021-10-19 17:09:11 +02001677 }
1678
1679 if (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU) {
1680 int16_t utran_encryption;
1681
1682 /* utran: ensure chosen ciphering mode is allowed
1683 * If the IE is missing (utran_encryption == -1), parse it as no encryption */
1684 utran_encryption = msg->cipher_mode_complete.utran_encryption;
1685 if (utran_encryption == -1)
1686 utran_encryption = 0;
1687 if ((net->uea_encryption_mask & (1 << utran_encryption)) == 0) {
1688 /* cipher disallowed */
1689 LOG_MSC_A(msc_a, LOGL_ERROR, "Cipher Mode Complete: RNC chosen forbidden ciphering UEA%d\n",
1690 msg->cipher_mode_complete.utran_encryption);
1691 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT);
1692 rc = 0;
1693 break;
1694 }
1695 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001696 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_COMPL);
1697 rc = 0;
Neels Hofmeyre9a39112019-08-29 00:10:49 +02001698
1699 /* Evaluate enclosed L3 message, typically Identity Response (IMEISV) */
1700 if (msg->cipher_mode_complete.l3_msg) {
1701 unsigned char *data = (unsigned char*)(msg->cipher_mode_complete.l3_msg->val);
1702 uint16_t len = msg->cipher_mode_complete.l3_msg->len;
1703 struct msgb *dtap = msgb_alloc(len, "DTAP from Cipher Mode Complete");
1704 unsigned char *pos = msgb_put(dtap, len);
1705 memcpy(pos, data, len);
1706 dtap->l3h = pos;
1707 rc = msc_a_up_l3(msc_a, dtap);
1708 msgb_free(dtap);
1709 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001710 break;
1711
1712 case RAN_MSG_CIPHER_MODE_REJECT:
1713 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT);
1714 rc = 0;
1715 break;
1716
1717 case RAN_MSG_ASSIGNMENT_COMPLETE:
1718 msc_a_up_call_assignment_complete(msc_a, msg);
1719 rc = 0;
1720 break;
1721
1722 case RAN_MSG_ASSIGNMENT_FAILURE:
1723 msc_a_up_call_assignment_failure(msc_a, msg);
1724 rc = 0;
1725 break;
1726
1727 case RAN_MSG_SAPI_N_REJECT:
1728 msc_a_up_sapi_n_reject(msc_a, msg);
1729 rc = 0;
1730 break;
1731
1732 case RAN_MSG_HANDOVER_PERFORMED:
1733 /* The BSS lets us know that a handover happened within the BSS, which doesn't concern us. */
1734 LOG_MSC_A(msc_a, LOGL_ERROR, "'Handover Performed' handling not implemented\n");
1735 break;
1736
1737 case RAN_MSG_HANDOVER_REQUIRED:
1738 /* The BSS lets us know that it wants to handover to a different cell */
1739 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_HANDOVER_REQUIRED, (void*)&msg->handover_required);
1740 break;
1741
1742 case RAN_MSG_HANDOVER_FAILURE:
1743 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1744 break;
1745
Keith Whytea1a70be2021-05-16 02:59:52 +02001746 case RAN_MSG_LCLS_STATUS:
1747 /* The BSS sends us LCLS_STATUS. We do nothing for now, but it is not an error. */
1748 LOG_MSC_A(msc_a, LOGL_DEBUG, "LCLS_STATUS (%s) received from MSC-I\n",
1749 gsm0808_lcls_status_name(msg->lcls_status.status));
1750 rc = 0;
1751 break;
1752
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001753 default:
1754 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-I not implemented: %s\n", ran_msg_type_name(msg->msg_type));
1755 rc = -ENOTSUP;
1756 break;
1757 }
1758 return rc;
1759}
1760
Andreas Eversbergf7c6f142023-04-23 12:10:42 +02001761static int msc_a_rx_vgcs_bss_decoded(struct osmo_fsm_inst *caller_fi, void *caller_data, const struct ran_msg *msg)
1762{
1763 struct vgcs_bss *bss = caller_data;
1764 struct msc_a *msc_a = (bss->trans) ? bss->trans->msc_a : NULL;
1765 int rc = 0;
1766
1767 switch (msg->msg_type) {
1768 case RAN_MSG_VGCS_VBS_SETUP_ACK:
1769 /* The BSS accepts VGCS/VBS and sends us supported features. */
1770 vgcs_vbs_setup_ack(bss, msg);
1771 break;
1772 case RAN_MSG_VGCS_VBS_SETUP_REFUSE:
1773 /* The BSS refuses VGCS/VBS. */
1774 vgcs_vbs_setup_refuse(bss, msg);
1775 break;
1776 case RAN_MSG_UPLINK_REQUEST:
1777 /* A mobile station requests the uplink on a VGCS channel. */
1778 vgcs_uplink_request(bss, msg);
1779 break;
1780 case RAN_MSG_UPLINK_REQUEST_CNF:
1781 /* The uplink on a VGCS channel has been established. */
1782 vgcs_uplink_request_cnf(bss, msg);
1783 break;
1784 case RAN_MSG_UPLINK_APPLICATION_DATA:
1785 /* Application data received on the uplink of a VGCS channel. */
1786 vgcs_app_data(bss, msg);
1787 break;
1788 case RAN_MSG_DTAP:
1789 /* BSS confirms the release of the channel. */
1790 vgcs_bss_dtap(bss, msg);
1791 break;
1792 case RAN_MSG_UPLINK_RELEASE_IND:
1793 /* A mobile station releases the uplink on a VGCS channel. */
1794 vgcs_uplink_release_ind(bss, msg);
1795 break;
1796 case RAN_MSG_CLEAR_REQUEST:
1797 /* BSS indicated that the channel has been released. */
1798 vgcs_vbs_clear_req(bss, msg);
1799 break;
1800 case RAN_MSG_CLEAR_COMPLETE:
1801 /* BSS confirms the release of the channel. */
1802 vgcs_vbs_clear_cpl(bss, msg);
1803 break;
1804 default:
1805 LOG_MSC_A(msc_a, LOGL_ERROR, "VGCS message from BSS not implemented: %s\n",
1806 ran_msg_type_name(msg->msg_type));
1807 rc = -ENOTSUP;
1808 break;
1809 }
1810 return rc;
1811}
1812
1813int msc_a_rx_vgcs_bss(struct vgcs_bss *bss, struct ran_conn *from_conn, struct msgb *msg)
1814{
1815 struct ran_dec ran_dec;
1816
1817 /* Feed through the decoding mechanism ran_msg. The decoded message arrives in msc_a_rx_vgcs_decoded() */
1818 ran_dec = (struct ran_dec) {
1819 .caller_data = bss,
1820 .decode_cb = msc_a_rx_vgcs_bss_decoded,
1821 };
1822 struct ran_peer *ran_peer = from_conn->ran_peer;
1823 struct ran_infra *ran = ran_peer->sri->ran;
1824 if (!ran->ran_dec_l2) {
1825 LOGP(DMSC, LOGL_ERROR, "No ran_dec_l2() defined for RAN type %s\n",
1826 osmo_rat_type_name(ran->type));
1827 return -ENOTSUP;
1828 }
1829 return ran->ran_dec_l2(&ran_dec, msg);
1830}
1831
1832static int msc_a_rx_vgcs_cell_decoded(struct osmo_fsm_inst *caller_fi, void *caller_data, const struct ran_msg *msg)
1833{
1834 struct vgcs_bss_cell *cell = caller_data;
1835 struct msc_a *msc_a = (cell->bss && cell->bss->trans) ? cell->bss->trans->msc_a : NULL;
1836 int rc = 0;
1837
1838 switch (msg->msg_type) {
1839 case RAN_MSG_VGCS_VBS_ASSIGN_RES:
1840 /* The BSS accepts VGCS/VBS channel assignment. */
1841 vgcs_vbs_assign_result(cell, msg);
1842 break;
1843 case RAN_MSG_VGCS_VBS_ASSIGN_FAIL:
1844 /* The BSS refuses VGCS/VBS channel assignment. */
1845 vgcs_vbs_assign_fail(cell, msg);
1846 break;
1847 case RAN_MSG_VGCS_VBS_QUEUING_IND:
1848 /* The BSS needs more time for VGCS/VBS channel assignment. */
1849 vgcs_vbs_queuing_ind(cell);
1850 break;
1851 case RAN_MSG_VGCS_VBS_ASSIGN_STATUS:
1852 /* The BSS gives cell status about VGCS/VBS channel. */
1853 vgcs_vbs_assign_status(cell, msg);
1854 break;
1855 case RAN_MSG_CLEAR_REQUEST:
1856 /* BSS indicated that the channel has been released. */
1857 vgcs_vbs_clear_req_channel(cell, msg);
1858 break;
1859 case RAN_MSG_CLEAR_COMPLETE:
1860 /* BSS confirms the release of the channel. */
1861 vgcs_vbs_clear_cpl_channel(cell, msg);
1862 break;
1863 default:
1864 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from BSS leg not implemented: %s\n",
1865 ran_msg_type_name(msg->msg_type));
1866 rc = -ENOTSUP;
1867 break;
1868 }
1869 return rc;
1870}
1871
1872int msc_a_rx_vgcs_cell(struct vgcs_bss_cell *cell, struct ran_conn *from_conn, struct msgb *msg)
1873{
1874 struct ran_dec ran_dec;
1875
1876 /* Feed through the decoding mechanism ran_msg. The decoded message arrives in msc_a_rx_vgcs_decoded() */
1877 ran_dec = (struct ran_dec) {
1878 .caller_data = cell,
1879 .decode_cb = msc_a_rx_vgcs_cell_decoded,
1880 };
1881 struct ran_peer *ran_peer = from_conn->ran_peer;
1882 struct ran_infra *ran = ran_peer->sri->ran;
1883 if (!ran->ran_dec_l2) {
1884 LOGP(DMSC, LOGL_ERROR, "No ran_dec_l2() defined for RAN type %s\n",
1885 osmo_rat_type_name(ran->type));
1886 return -ENOTSUP;
1887 }
1888 return ran->ran_dec_l2(&ran_dec, msg);
1889}
1890
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001891static int msc_a_ran_dec_from_msc_t(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1892{
1893 struct msc_t *msc_t = msc_a_msc_t(msc_a);
1894 int rc = -99;
1895
1896 if (!msc_t) {
1897 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx message from MSC-T role, but I have no active MSC-T role.\n");
1898 return -EINVAL;
1899 }
1900
1901 OSMO_ASSERT(d->ran_dec);
1902
1903 switch (d->ran_dec->msg_type) {
1904
1905 case RAN_MSG_CLEAR_REQUEST:
1906 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_MO_CLOSE, NULL);
1907 break;
1908
1909 case RAN_MSG_CLEAR_COMPLETE:
1910 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_CLEAR_COMPLETE, NULL);
1911 break;
1912
1913 case RAN_MSG_CLASSMARK_UPDATE:
1914 msc_a_up_classmark_update(msc_a, d->ran_dec->classmark_update.classmark, &msc_t->classmark);
1915 rc = 0;
1916 break;
1917
1918 case RAN_MSG_HANDOVER_REQUEST_ACK:
1919 /* new BSS accepts Handover */
1920 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_REQUEST_ACK);
1921 break;
1922
1923 case RAN_MSG_HANDOVER_DETECT:
1924 /* new BSS signals the MS is DETECTed on the new lchan */
1925 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_DETECT);
1926 break;
1927
1928 case RAN_MSG_HANDOVER_COMPLETE:
1929 /* new BSS signals the MS has fully moved to the new lchan */
1930 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_COMPLETE);
1931 break;
1932
1933 case RAN_MSG_HANDOVER_FAILURE:
1934 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1935 break;
1936
1937 default:
1938 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-T not implemented: %s\n",
1939 ran_msg_type_name(d->ran_dec->msg_type));
1940 rc = -ENOTSUP;
1941 break;
1942 }
1943 return rc;
1944}
1945
1946int msc_a_ran_decode_cb(struct osmo_fsm_inst *msc_a_fi, void *data, const struct ran_msg *msg)
1947{
1948 struct msc_a *msc_a = msc_a_fi_priv(msc_a_fi);
1949 struct msc_a_ran_dec_data *d = data;
1950 int rc = -99;
1951
1952 d->ran_dec = msg;
1953
1954 switch (d->from_role) {
1955 case MSC_ROLE_I:
1956 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode: %s\n", msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1957 rc = msc_a_ran_dec_from_msc_i(msc_a, d);
1958 break;
1959
1960 case MSC_ROLE_T:
1961 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode from MSC-T: %s\n",
1962 msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1963 rc = msc_a_ran_dec_from_msc_t(msc_a, d);
1964 break;
1965
1966 default:
1967 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from invalid role %s: %s\n", msc_role_name(d->from_role),
1968 ran_msg_type_name(msg->msg_type));
1969 return -ENOTSUP;
1970 }
1971
1972 if (rc)
1973 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),
1974 msc_role_name(d->from_role));
1975 return rc;
1976}
1977
1978/* Your typical DTAP via FORWARD_ACCESS_SIGNALLING_REQUEST */
1979int _msc_a_ran_down(struct msc_a *msc_a, enum msc_role to_role, const struct ran_msg *ran_msg,
1980 const char *file, int line)
1981{
1982 return _msc_a_msg_down(msc_a, to_role, msub_role_to_role_event(msc_a->c.msub, MSC_ROLE_A, to_role),
1983 ran_msg, file, line);
1984}
1985
1986/* To transmit more complex events than just FORWARD_ACCESS_SIGNALLING_REQUEST, e.g. an
1987 * MSC_T_EV_FROM_A_PREPARE_HANDOVER_REQUEST */
1988int _msc_a_msg_down(struct msc_a *msc_a, enum msc_role to_role, uint32_t to_role_event,
1989 const struct ran_msg *ran_msg,
1990 const char *file, int line)
1991{
1992 struct an_apdu an_apdu = {
1993 .an_proto = msc_a->c.ran->an_proto,
1994 .msg = msc_role_ran_encode(msc_a->c.fi, ran_msg),
1995 };
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001996 if (!an_apdu.msg)
1997 return -EIO;
Vadim Yanitskiyc44342b2021-12-07 18:32:35 +03001998 return _msub_role_dispatch(msc_a->c.msub, to_role, to_role_event, &an_apdu, file, line);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001999}
2000
2001int msc_a_tx_dtap_to_i(struct msc_a *msc_a, struct msgb *dtap)
2002{
2003 struct ran_msg ran_msg;
Neels Hofmeyrc192c0b2019-10-07 21:41:18 +02002004 struct gsm48_hdr *gh = msgb_l3(dtap) ? : dtap->data;
2005 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002006
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02002007 if (!msc_a) {
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02002008 LOGP(DMSC, LOGL_ERROR, "Attempt to send DTAP to NULL MSC-A, dropping message: %s %s\n",
2009 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
2010 msgb_free(dtap);
2011 return -EIO;
2012 }
2013
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002014 if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) {
2015 /* The SGs connection to the MME always is at the MSC-A. */
2016 return sgs_iface_tx_dtap_ud(msc_a, dtap);
2017 }
2018
Neels Hofmeyrc192c0b2019-10-07 21:41:18 +02002019 LOG_MSC_A(msc_a, LOGL_DEBUG, "Sending DTAP: %s %s\n",
2020 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
2021
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002022 ran_msg = (struct ran_msg){
2023 .msg_type = RAN_MSG_DTAP,
2024 .dtap = dtap,
2025 };
2026 return msc_a_ran_down(msc_a, MSC_ROLE_I, &ran_msg);
2027}
2028
2029struct msc_a *msc_a_for_vsub(const struct vlr_subscr *vsub, bool valid_conn_only)
2030{
2031 struct msc_a *msc_a = msub_msc_a(msub_for_vsub(vsub));
2032 if (valid_conn_only && !msc_a_is_accepted(msc_a))
2033 return NULL;
2034 return msc_a;
2035}
2036
2037int msc_tx_common_id(struct msc_a *msc_a, enum msc_role to_role)
2038{
2039 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Vadim Yanitskiy435f67f2021-06-06 15:47:49 +02002040 if (vsub == NULL)
2041 return -ENODEV;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002042 struct ran_msg msg = {
2043 .msg_type = RAN_MSG_COMMON_ID,
2044 .common_id = {
2045 .imsi = vsub->imsi,
Pau Espin Pedrol67106702021-04-27 18:20:15 +02002046 .last_eutran_plmn_present = vsub->sgs.last_eutran_plmn_present,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002047 },
2048 };
Pau Espin Pedrol67106702021-04-27 18:20:15 +02002049 if (vsub->sgs.last_eutran_plmn_present) {
2050 memcpy(&msg.common_id.last_eutran_plmn, &vsub->sgs.last_eutran_plmn,
2051 sizeof(vsub->sgs.last_eutran_plmn));
2052 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002053
2054 return msc_a_ran_down(msc_a, to_role, &msg);
2055}
2056
2057static int msc_a_start_assignment(struct msc_a *msc_a, struct gsm_trans *cc_trans)
2058{
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002059 struct call_leg *cl;
2060 bool cn_rtp_available;
2061 bool ran_rtp_available;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002062
2063 OSMO_ASSERT(!msc_a->cc.active_trans);
2064 msc_a->cc.active_trans = cc_trans;
2065
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01002066 cc_trans->cc.codecs.assignment = (struct sdp_audio_codec){};
2067
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002068 OSMO_ASSERT(cc_trans && cc_trans->type == TRANS_CC);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002069 cl = msc_a_ensure_call_leg(msc_a, cc_trans);
2070 if (!cl)
2071 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002072
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002073 /* See if we can set a preliminary codec. If not, pass none for the time being. */
Oliver Smithceca8e62023-05-24 11:15:52 +02002074 trans_cc_filter_run(cc_trans);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002075
2076 cn_rtp_available = call_leg_local_ip(cl, RTP_TO_CN);
2077 ran_rtp_available = call_leg_local_ip(cl, RTP_TO_RAN);
2078
2079 /* Set up RTP ports for both RAN and CN side. Even though we ask for both at the same time, the
2080 * osmo_mgcpc_ep_fsm automagically waits for the first CRCX to complete before firing the second CRCX. The one
2081 * issued first here will also be the first CRCX sent to the MGW. Usually both still need to be set up. */
2082 if (!cn_rtp_available)
Andreas Eversberg712b28e2023-06-21 11:17:26 +02002083 call_leg_ensure_ci(cl, RTP_TO_CN, cc_trans->call_id, cc_trans,
Oliver Smithc63c3a02023-05-24 10:48:07 +02002084 &cc_trans->cc.local.audio_codecs, NULL);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002085 if (!ran_rtp_available) {
2086 struct sdp_audio_codecs *codecs;
2087 if (msc_a->c.ran->force_mgw_codecs_to_ran.count)
2088 codecs = &msc_a->c.ran->force_mgw_codecs_to_ran;
2089 else
Oliver Smithc63c3a02023-05-24 10:48:07 +02002090 codecs = &cc_trans->cc.local.audio_codecs;
Andreas Eversberg712b28e2023-06-21 11:17:26 +02002091 return call_leg_ensure_ci(cl, RTP_TO_RAN, cc_trans->call_id, cc_trans, codecs, NULL);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01002092 }
2093
2094 /* Should these already be set up, immediately continue by retriggering the events signalling that the RTP
2095 * ports are available. The ordering is: first CN, then RAN. */
2096 if (cn_rtp_available && ran_rtp_available)
2097 return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_RAN]);
2098 else if (cn_rtp_available)
2099 return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_CN]);
2100 /* Otherwise wait for MGCP response and continue from there. */
2101 return 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002102}
2103
2104int msc_a_try_call_assignment(struct gsm_trans *cc_trans)
2105{
2106 struct msc_a *msc_a = cc_trans->msc_a;
2107 OSMO_ASSERT(cc_trans->type == TRANS_CC);
2108
2109 if (msc_a->cc.active_trans == cc_trans) {
Neels Hofmeyrb4ef5e72019-08-30 01:11:12 +02002110 LOG_MSC_A(msc_a, LOGL_DEBUG, "Assignment for this trans already started earlier\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002111 return 0;
2112 }
2113
2114 if (msc_a->cc.active_trans) {
2115 LOG_MSC_A(msc_a, LOGL_INFO, "Another call is already ongoing, not assigning yet\n");
2116 return 0;
2117 }
2118
2119 LOG_MSC_A(msc_a, LOGL_DEBUG, "Starting call assignment\n");
2120 return msc_a_start_assignment(msc_a, cc_trans);
2121}
2122
Neels Hofmeyr5d53c602022-04-24 23:37:07 +02002123/* Map CM Service type to use token.
2124 * Given a CM Service type, return a matching token intended for osmo_use_count.
2125 * For unknown service type, return NULL.
2126 */
Andreas Eversbergcd8bd452023-07-11 14:16:24 +02002127const 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 +01002128{
Andreas Eversbergcd8bd452023-07-11 14:16:24 +02002129 struct gsm_network *net = msc_a_net(msc_a);
2130
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002131 switch (cm_service_type) {
2132 case GSM48_CMSERV_MO_CALL_PACKET:
2133 case GSM48_CMSERV_EMERGENCY:
2134 return MSC_A_USE_CM_SERVICE_CC;
2135
2136 case GSM48_CMSERV_SMS:
2137 return MSC_A_USE_CM_SERVICE_SMS;
2138
2139 case GSM48_CMSERV_SUP_SERV:
2140 return MSC_A_USE_CM_SERVICE_SS;
2141
Andreas Eversberg456c6f72023-04-23 11:54:16 +02002142 case GSM48_CMSERV_VGCS:
Andreas Eversbergcd8bd452023-07-11 14:16:24 +02002143 if (net->asci.enable)
2144 return MSC_A_USE_CM_SERVICE_GCC;
2145 else
2146 return NULL;
Andreas Eversberg456c6f72023-04-23 11:54:16 +02002147
2148 case GSM48_CMSERV_VBS:
Andreas Eversbergcd8bd452023-07-11 14:16:24 +02002149 if (net->asci.enable)
2150 return MSC_A_USE_CM_SERVICE_BCC;
2151 else
2152 return NULL;
Andreas Eversberg456c6f72023-04-23 11:54:16 +02002153
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01002154 default:
2155 return NULL;
2156 }
2157}
2158
2159void msc_a_release_cn(struct msc_a *msc_a)
2160{
2161 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CN_CLOSE, NULL);
2162}
2163
2164void msc_a_release_mo(struct msc_a *msc_a, enum gsm48_gsm_cause gsm_cause)
2165{
2166 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
2167}