blob: 5a5bb8afec62269fbf75ac03565440b20f3150ad [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>
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010051
52#define MSC_A_USE_WAIT_CLEAR_COMPLETE "wait-Clear-Complete"
53
54static struct osmo_fsm msc_a_fsm;
55
56static const struct osmo_tdef_state_timeout msc_a_fsm_timeouts[32] = {
57 [MSC_A_ST_VALIDATE_L3] = { .T = -1 },
58 [MSC_A_ST_AUTH_CIPH] = { .keep_timer = true },
59 [MSC_A_ST_WAIT_CLASSMARK_UPDATE] = { .keep_timer = true },
60 [MSC_A_ST_AUTHENTICATED] = { .keep_timer = true },
61 [MSC_A_ST_RELEASING] = { .T = -2 },
62 [MSC_A_ST_RELEASED] = { .T = -2 },
63};
64
65/* Transition to a state, using the T timer defined in msc_a_fsm_timeouts.
66 * The actual timeout value is in turn obtained from network->T_defs.
67 * Assumes local variable fi exists. */
Neels Hofmeyr01653252019-09-03 02:06:22 +020068#define msc_a_state_chg_always(msc_a, state) \
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010069 osmo_tdef_fsm_inst_state_chg((msc_a)->c.fi, state, msc_a_fsm_timeouts, (msc_a)->c.ran->tdefs, 5)
70
Neels Hofmeyr01653252019-09-03 02:06:22 +020071/* Same as msc_a_state_chg_always() but ignore if the msc_a already is in the target state. */
72#define msc_a_state_chg(msc_a, STATE) do { \
73 if ((msc_a)->c.fi->state != STATE) \
74 msc_a_state_chg_always(msc_a, STATE); \
75 } while(0)
76
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010077struct gsm_network *msc_a_net(const struct msc_a *msc_a)
78{
79 return msub_net(msc_a->c.msub);
80}
81
82struct vlr_subscr *msc_a_vsub(const struct msc_a *msc_a)
83{
Neels Hofmeyr911e5972019-05-09 13:28:26 +020084 if (!msc_a)
85 return NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010086 return msub_vsub(msc_a->c.msub);
87}
88
89struct msc_i *msc_a_msc_i(const struct msc_a *msc_a)
90{
Neels Hofmeyr911e5972019-05-09 13:28:26 +020091 if (!msc_a)
92 return NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010093 return msub_msc_i(msc_a->c.msub);
94}
95
96struct msc_t *msc_a_msc_t(const struct msc_a *msc_a)
97{
Neels Hofmeyr911e5972019-05-09 13:28:26 +020098 if (!msc_a)
99 return NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100100 return msub_msc_t(msc_a->c.msub);
101}
102
103struct msc_a *msc_a_fi_priv(struct osmo_fsm_inst *fi)
104{
105 OSMO_ASSERT(fi);
106 OSMO_ASSERT(fi->fsm == &msc_a_fsm);
107 OSMO_ASSERT(fi->priv);
108 return fi->priv;
109}
110
Neels Hofmeyrd99a6072022-10-10 23:34:48 +0200111bool msc_a_is_ciphering_to_be_attempted(const struct msc_a *msc_a)
Alexander Couzens2c5e4612021-11-05 02:00:17 +0100112{
113 struct gsm_network *net = msc_a_net(msc_a);
114 bool is_utran = (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU);
115 if (is_utran)
116 return net->uea_encryption_mask > (1 << OSMO_UTRAN_UEA0);
117 else
118 return net->a5_encryption_mask > 0x1;
119}
120
Neels Hofmeyr2ea72642022-10-10 23:35:47 +0200121bool msc_a_is_ciphering_required(const struct msc_a *msc_a)
122{
123 struct gsm_network *net = msc_a_net(msc_a);
124 bool is_utran = (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU);
125 if (is_utran)
126 return net->uea_encryption_mask
127 && ((net->uea_encryption_mask & (1 << OSMO_UTRAN_UEA0)) == 0);
128 else
129 return net->a5_encryption_mask
130 && ((net->a5_encryption_mask & 0x1) == 0);
131}
132
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100133static void update_counters(struct osmo_fsm_inst *fi, bool conn_accepted)
134{
135 struct msc_a *msc_a = fi->priv;
136 struct gsm_network *net = msc_a_net(msc_a);
137 switch (msc_a->complete_layer3_type) {
138 case COMPLETE_LAYER3_LU:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200139 rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs, conn_accepted ? MSC_CTR_LOC_UPDATE_COMPLETED : MSC_CTR_LOC_UPDATE_FAILED));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100140 break;
141 case COMPLETE_LAYER3_CM_SERVICE_REQ:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200142 rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs, conn_accepted ? MSC_CTR_CM_SERVICE_REQUEST_ACCEPTED : MSC_CTR_CM_SERVICE_REQUEST_REJECTED));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100143 break;
144 case COMPLETE_LAYER3_PAGING_RESP:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200145 rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs, conn_accepted ? MSC_CTR_PAGING_RESP_ACCEPTED : MSC_CTR_PAGING_RESP_REJECTED));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100146 break;
Neels Hofmeyrae98b972021-07-27 03:46:49 +0200147 case COMPLETE_LAYER3_CM_RE_ESTABLISH_REQ:
148 rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs,
149 conn_accepted ? MSC_CTR_CM_RE_ESTABLISH_REQ_ACCEPTED
150 : MSC_CTR_CM_RE_ESTABLISH_REQ_REJECTED));
151 break;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100152 default:
153 break;
154 }
155}
156
157static void evaluate_acceptance_outcome(struct osmo_fsm_inst *fi, bool conn_accepted)
158{
159 struct msc_a *msc_a = fi->priv;
160 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
161
162 update_counters(fi, conn_accepted);
163
Neels Hofmeyr83cf10c2020-06-24 14:23:26 +0200164 if (conn_accepted) {
165 /* Record the Cell ID seen in Complete Layer 3 Information in the VLR, so that it also shows in vty
166 * 'show' output. */
167 vsub->cgi = msc_a->via_cell;
168 }
169
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100170 /* Trigger transactions that we paged for */
171 if (msc_a->complete_layer3_type == COMPLETE_LAYER3_PAGING_RESP) {
172 if (conn_accepted)
173 paging_response(msc_a);
174 else
175 paging_expired(vsub);
176 }
177
178 if (conn_accepted)
179 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_ATTACHED, msc_a_vsub(msc_a));
180
181 if (msc_a->complete_layer3_type == COMPLETE_LAYER3_LU)
182 msc_a_put(msc_a, MSC_A_USE_LOCATION_UPDATING);
Neels Hofmeyrae98b972021-07-27 03:46:49 +0200183
184 if (msc_a->complete_layer3_type == COMPLETE_LAYER3_CM_RE_ESTABLISH_REQ) {
185 /* Trigger new Assignment to recommence the voice call. A little dance here because normally we verify
186 * that no CC trans is already active. */
187 struct gsm_trans *cc_trans = msc_a->cc.active_trans;
188 msc_a->cc.active_trans = NULL;
189 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, cc_trans);
190 msc_a_try_call_assignment(cc_trans);
191 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100192}
193
194bool msc_a_is_accepted(const struct msc_a *msc_a)
195{
196 if (!msc_a || !msc_a->c.fi)
197 return false;
198 return msc_a->c.fi->state == MSC_A_ST_AUTHENTICATED
199 || msc_a->c.fi->state == MSC_A_ST_COMMUNICATING;
200}
201
202bool msc_a_in_release(struct msc_a *msc_a)
203{
204 if (!msc_a)
205 return true;
206 if (msc_a->c.fi->state == MSC_A_ST_RELEASING)
207 return true;
208 if (msc_a->c.fi->state == MSC_A_ST_RELEASED)
209 return true;
210 return false;
211}
212
213static int msc_a_ran_dec(struct msc_a *msc_a, const struct an_apdu *an_apdu, enum msc_role from_role)
214{
215 int rc;
216 struct msc_a_ran_dec_data d = {
217 .from_role = from_role,
218 .an_apdu = an_apdu,
219 };
220 msc_a_get(msc_a, __func__);
221 rc = msc_role_ran_decode(msc_a->c.fi, an_apdu, msc_a_ran_decode_cb, &d);
222 msc_a_put(msc_a, __func__);
223 return rc;
224};
225
226static void msc_a_fsm_validate_l3(struct osmo_fsm_inst *fi, uint32_t event, void *data)
227{
228 struct msc_a *msc_a = fi->priv;
229 const struct an_apdu *an_apdu;
230
231 switch (event) {
232 case MSC_A_EV_FROM_I_COMPLETE_LAYER_3:
233 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
234 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
235 an_apdu = data;
236 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_I);
237 return;
238
239 case MSC_A_EV_COMPLETE_LAYER_3_OK:
240 msc_a_state_chg(msc_a, MSC_A_ST_AUTH_CIPH);
241 return;
242
243 case MSC_A_EV_MO_CLOSE:
244 case MSC_A_EV_CN_CLOSE:
245 evaluate_acceptance_outcome(fi, false);
246 /* fall through */
247 case MSC_A_EV_UNUSED:
248 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
249 return;
250
251 default:
252 OSMO_ASSERT(false);
253 }
254}
255
256/* Figure out whether to first send a Classmark Request to the MS to figure out algorithm support. */
257static bool msc_a_need_classmark_for_ciphering(struct msc_a *msc_a)
258{
259 struct gsm_network *net = msc_a_net(msc_a);
260 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
261 int i = 0;
262 bool request_classmark = false;
263
264 /* Only on GERAN-A do we ever need Classmark Information for Ciphering. */
265 if (msc_a->c.ran->type != OSMO_RAT_GERAN_A)
266 return false;
267
268 for (i = 0; i < 8; i++) {
269 int supported;
270
271 /* A5/n permitted by osmo-msc.cfg? */
272 if (!(net->a5_encryption_mask & (1 << i)))
273 continue;
274
275 /* A5/n supported by MS? */
276 supported = osmo_gsm48_classmark_supports_a5(&vsub->classmark, i);
277 if (supported < 0) {
278 LOG_MSC_A(msc_a, LOGL_DEBUG, "For A5/%d, we still need Classmark %d\n", i, -supported);
279 request_classmark = true;
280 }
281 }
282
283 return request_classmark;
284}
285
286static int msc_a_ran_enc_ciphering(struct msc_a *msc_a, bool umts_aka, bool retrieve_imeisv);
287
288/* VLR callback for ops.set_ciph_mode() */
289int msc_a_vlr_set_cipher_mode(void *_msc_a, bool umts_aka, bool retrieve_imeisv)
290{
291 struct msc_a *msc_a = _msc_a;
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700292 struct vlr_subscr *vsub;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100293
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700294 if (!msc_a) {
295 LOGP(DMSC, LOGL_ERROR, "Insufficient info to start ciphering: "
296 "MSC-A role is NULL?!?\n");
297 return -EINVAL;
298 }
299
300 vsub = msc_a_vsub(msc_a);
301 if (!vsub || !vsub->last_tuple) {
302 LOG_MSC_A(msc_a, LOGL_ERROR, "Insufficient info to start ciphering: "
303 "vlr_subscr is NULL?!?\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100304 return -EINVAL;
305 }
306
307 if (msc_a_need_classmark_for_ciphering(msc_a)) {
308 int rc;
309 struct ran_msg msg = {
310 .msg_type = RAN_MSG_CLASSMARK_REQUEST,
311 };
312 rc = msc_a_ran_down(msc_a, MSC_ROLE_I, &msg);
313 if (rc) {
314 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot send Classmark Request\n");
315 return -EIO;
316 }
317
318 msc_a->state_before_classmark_update = msc_a->c.fi->state;
319 msc_a->action_on_classmark_update = (struct msc_a_action_on_classmark_update){
320 .type = MSC_A_CLASSMARK_UPDATE_THEN_CIPHERING,
321 .ciphering = {
322 .umts_aka = umts_aka,
323 .retrieve_imeisv = retrieve_imeisv,
324 },
325 };
326 msc_a_state_chg(msc_a, MSC_A_ST_WAIT_CLASSMARK_UPDATE);
327 return 0;
328 }
329
330 return msc_a_ran_enc_ciphering(msc_a, umts_aka, retrieve_imeisv);
331}
332
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +0200333static uint8_t filter_a5(uint8_t a5_mask, bool umts_aka)
334{
335 /* With GSM AKA: allow A5/0, 1, 3 = 0b00001011 = 0xb.
336 * UMTS aka: allow A5/0, 1, 3, 4 = 0b00011011 = 0x1b.
337 */
338 return a5_mask & (umts_aka ? 0x1b : 0x0b);
339}
340
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100341static int msc_a_ran_enc_ciphering(struct msc_a *msc_a, bool umts_aka, bool retrieve_imeisv)
342{
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700343 struct gsm_network *net;
344 struct vlr_subscr *vsub;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100345 struct ran_msg msg;
346
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700347 if (!msc_a) {
348 LOGP(DMSC, LOGL_ERROR, "Insufficient info to start ciphering: "
349 "MSC-A role is NULL?!?\n");
350 return -EINVAL;
351 }
352
353 net = msc_a_net(msc_a);
354 vsub = msc_a_vsub(msc_a);
355
356 if (!net || !vsub || !vsub->last_tuple) {
357 LOG_MSC_A(msc_a, LOGL_ERROR, "Insufficient info to start ciphering: "
358 "gsm_network and/or vlr_subscr is NULL?!?\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100359 return -EINVAL;
360 }
361
362 msg = (struct ran_msg){
363 .msg_type = RAN_MSG_CIPHER_MODE_COMMAND,
364 .cipher_mode_command = {
365 .vec = vsub->last_tuple ? &vsub->last_tuple->vec : NULL,
366 .classmark = &vsub->classmark,
367 .geran = {
368 .umts_aka = umts_aka,
369 .retrieve_imeisv = retrieve_imeisv,
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +0200370 .a5_encryption_mask = filter_a5(net->a5_encryption_mask, umts_aka),
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100371
372 /* for ran_a.c to store the GERAN key that is actually used */
373 .chosen_key = &msc_a->geran_encr,
374 },
Harald Welte274b70f2021-02-06 16:47:39 +0100375 .utran = {
Harald Welte505a94a2021-02-06 17:12:20 +0100376 .uea_encryption_mask = net->uea_encryption_mask,
377 },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100378 },
379 };
380
381 if (msc_a_ran_down(msc_a, MSC_ROLE_I, &msg)) {
382 LOG_MSC_A(msc_a, LOGL_ERROR, "Sending Cipher Mode Command failed\n");
383 /* Returning error to the VLR ops.set_ciph_mode() will cancel the attach. Other callers need to take
384 * care of the return value. */
385 return -EINVAL;
386 }
387
388 if (msc_a->geran_encr.key_len)
Neels Hofmeyr73d093a2021-06-23 23:54:43 +0200389 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN encoding chose ciphering: A5/%d kc %s kc128 %s\n",
390 msc_a->geran_encr.alg_id - 1,
391 osmo_hexdump_nospc_c(OTC_SELECT, msc_a->geran_encr.key, msc_a->geran_encr.key_len),
392 msc_a->geran_encr.kc128_present ?
393 osmo_hexdump_nospc_c(OTC_SELECT, msc_a->geran_encr.kc128, sizeof(msc_a->geran_encr.kc128))
394 : "-");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100395 return 0;
396}
397
398static void msc_a_fsm_auth_ciph(struct osmo_fsm_inst *fi, uint32_t event, void *data)
399{
400 struct msc_a *msc_a = fi->priv;
401
402 /* If accepted, transition the state, all other cases mean failure. */
403 switch (event) {
404 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
405 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
406 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
407 return;
408
409 case MSC_A_EV_AUTHENTICATED:
410 msc_a_state_chg(msc_a, MSC_A_ST_AUTHENTICATED);
411 return;
412
413 case MSC_A_EV_UNUSED:
414 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
415 return;
416
417 case MSC_A_EV_MO_CLOSE:
418 case MSC_A_EV_CN_CLOSE:
419 evaluate_acceptance_outcome(fi, false);
420 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
421 return;
422
423
424 default:
425 OSMO_ASSERT(false);
426 }
427}
428
429static void msc_a_fsm_wait_classmark_update(struct osmo_fsm_inst *fi, uint32_t event, void *data)
430{
431 struct msc_a *msc_a = fi->priv;
432
433 switch (event) {
434 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
435 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
436 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
437 return;
438
439 case MSC_A_EV_CLASSMARK_UPDATE:
440 switch (msc_a->action_on_classmark_update.type) {
441 case MSC_A_CLASSMARK_UPDATE_THEN_CIPHERING:
442 msc_a_state_chg(msc_a, MSC_A_ST_AUTH_CIPH);
443 if (msc_a_ran_enc_ciphering(msc_a,
444 msc_a->action_on_classmark_update.ciphering.umts_aka,
445 msc_a->action_on_classmark_update.ciphering.retrieve_imeisv)) {
446 LOG_MSC_A(msc_a, LOGL_ERROR,
447 "After Classmark Update, still failed to send Cipher Mode Command\n");
448 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
449 }
450 return;
451
452 default:
453 LOG_MSC_A(msc_a, LOGL_ERROR, "Internal error: After Classmark Update, don't know what to do\n");
454 msc_a_state_chg(msc_a, msc_a->state_before_classmark_update);
455 return;
456 }
457
458 case MSC_A_EV_UNUSED:
459 /* Seems something detached / aborted in the middle of auth+ciph. */
460 evaluate_acceptance_outcome(fi, false);
461 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
462 return;
463
464 case MSC_A_EV_MO_CLOSE:
465 case MSC_A_EV_CN_CLOSE:
466 evaluate_acceptance_outcome(fi, false);
467 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
468 return;
469
470 default:
471 OSMO_ASSERT(false);
472 }
473}
474
475static bool msc_a_fsm_has_active_transactions(struct osmo_fsm_inst *fi)
476{
477 struct msc_a *msc_a = fi->priv;
478 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
479 struct gsm_trans *trans;
480
481 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_SILENT_CALL)) {
482 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: silent call still active\n", __func__);
483 return true;
484 }
485
486 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC)) {
487 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO CC request after a CM Service Request\n",
488 __func__);
489 return true;
490 }
Andreas Eversberg456c6f72023-04-23 11:54:16 +0200491 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_GCC)) {
492 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO GCC request after a CM Service Request\n",
493 __func__);
494 return true;
495 }
496 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_BCC)) {
497 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO BCC request after a CM Service Request\n",
498 __func__);
499 return true;
500 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100501 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_SMS)) {
502 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO SMS after a CM Service Request\n",
503 __func__);
504 return true;
505 }
506 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_SS)) {
507 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO SS after a CM Service Request\n",
508 __func__);
509 return true;
510 }
511
512 if (vsub && !llist_empty(&vsub->cs.requests)) {
513 struct paging_request *pr;
514 llist_for_each_entry(pr, &vsub->cs.requests, entry) {
515 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still active: %s\n", __func__, pr->label);
516 }
517 return true;
518 }
519
520 if ((trans = trans_has_conn(msc_a))) {
521 LOG_MSC_A(msc_a, LOGL_DEBUG, "connection still has active transaction: %s\n",
522 trans_type_name(trans->type));
523 return true;
524 }
525
526 return false;
527}
528
529static void msc_a_fsm_authenticated_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
530{
531 struct msc_a *msc_a = fi->priv;
532 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
533
534 /* Stop Location Update expiry for this subscriber. While the subscriber
535 * has an open connection the LU expiry timer must remain disabled.
536 * Otherwise we would kick the subscriber off the network when the timer
537 * expires e.g. during a long phone call.
538 * The LU expiry timer will restart once the connection is closed. */
539 if (vsub)
540 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
541
542 evaluate_acceptance_outcome(fi, true);
543}
544
545static void msc_a_fsm_authenticated(struct osmo_fsm_inst *fi, uint32_t event, void *data)
546{
547 struct msc_a *msc_a = fi->priv;
548
549 switch (event) {
550 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
551 case MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST:
552 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
553 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
554 return;
555
556 case MSC_A_EV_COMPLETE_LAYER_3_OK:
557 /* When Authentication is off, we may already be in the Accepted state when the code
558 * evaluates the Compl L3. Simply ignore. This just cosmetically mutes the error log
559 * about the useless event. */
560 return;
561
562 case MSC_A_EV_TRANSACTION_ACCEPTED:
563 msc_a_state_chg(msc_a, MSC_A_ST_COMMUNICATING);
564 return;
565
566 case MSC_A_EV_MO_CLOSE:
567 case MSC_A_EV_CN_CLOSE:
568 case MSC_A_EV_UNUSED:
569 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
570 return;
571
572 default:
573 OSMO_ASSERT(false);
574 }
575}
576
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100577static struct call_leg *msc_a_ensure_call_leg(struct msc_a *msc_a, struct gsm_trans *for_cc_trans)
578{
579 struct call_leg *cl = msc_a->cc.call_leg;
580 struct gsm_network *net = msc_a_net(msc_a);
581
582 /* Ensure that events about RTP endpoints coming from the msc_a->cc.call_leg know which gsm_trans to abort on
583 * error */
584 if (!msc_a->cc.active_trans)
585 msc_a->cc.active_trans = for_cc_trans;
586 if (msc_a->cc.active_trans != for_cc_trans) {
587 LOG_TRANS(for_cc_trans, LOGL_ERROR,
588 "Cannot create call leg, another trans is already active for this conn\n");
589 return NULL;
590 }
591
592 if (!cl) {
593 cl = msc_a->cc.call_leg = call_leg_alloc(msc_a->c.fi,
594 MSC_EV_CALL_LEG_TERM,
595 MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE,
596 MSC_EV_CALL_LEG_RTP_COMPLETE);
597 OSMO_ASSERT(cl);
598
599 if (net->use_osmux != OSMUX_USAGE_OFF) {
600 struct msc_i *msc_i = msc_a_msc_i(msc_a);
601 if (msc_i->c.remote_to) {
602 /* TODO: investigate what to do in this case */
603 LOG_MSC_A(msc_a, LOGL_ERROR, "Osmux not yet supported for inter-MSC");
604 } else {
605 cl->ran_peer_supports_osmux = msc_i->ran_conn->ran_peer->remote_supports_osmux;
606 }
607 }
608
609 }
610 return cl;
611}
612
613int msc_a_ensure_cn_local_rtp(struct msc_a *msc_a, struct gsm_trans *cc_trans)
614{
615 struct call_leg *cl;
616 struct rtp_stream *rtp_to_ran;
617
618 cl = msc_a_ensure_call_leg(msc_a, cc_trans);
619 if (!cl)
620 return -EINVAL;
621 rtp_to_ran = cl->rtp[RTP_TO_RAN];
622
623 if (call_leg_local_ip(cl, RTP_TO_CN)) {
624 /* Already has an RTP address and port towards the CN, continue right away. */
625 return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_CN]);
626 }
627
628 /* No CN RTP address available yet, ask the MGW to create one.
629 * Set a codec to be used: if Assignment on the RAN side is already done, take the same codec as the RTP_TO_RAN.
630 * If no RAN side RTP is established, try to guess a preliminary codec from SDP -- before Assignment, picking a
631 * codec from the SDP is more politeness/avoiding confusion than necessity. The actual codec to be used would be
632 * determined later. If no codec could be determined, pass none for the time being. */
Andreas Eversberg712b28e2023-06-21 11:17:26 +0200633 return call_leg_ensure_ci(cl, RTP_TO_CN, cc_trans->call_id, cc_trans,
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100634 rtp_to_ran->codecs_known ? &rtp_to_ran->codecs : NULL, NULL);
635}
636
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100637/* The MGW has given us a local IP address for the RAN side. Ready to start the Assignment of a voice channel. */
638static void msc_a_call_leg_ran_local_addr_available(struct msc_a *msc_a)
639{
640 struct ran_msg msg;
641 struct gsm_trans *cc_trans = msc_a->cc.active_trans;
642 struct gsm0808_channel_type channel_type;
643
Neels Hofmeyr00a476b2019-11-28 02:46:05 +0100644 if (!cc_trans) {
645 LOG_MSC_A(msc_a, LOGL_ERROR, "No CC transaction active\n");
646 call_leg_release(msc_a->cc.call_leg);
647 return;
648 }
649
Oliver Smithceca8e62023-05-24 11:15:52 +0200650 trans_cc_filter_run(cc_trans);
651 LOG_TRANS(cc_trans, LOGL_DEBUG, "Sending Assignment Command\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100652
Oliver Smith10632132023-05-12 12:14:22 +0200653 switch (cc_trans->bearer_cap.transfer) {
654 case GSM48_BCAP_ITCAP_SPEECH:
655 if (!cc_trans->cc.local.audio_codecs.count) {
656 LOG_TRANS(cc_trans, LOGL_ERROR, "Assignment not possible, no matching codec: %s\n",
657 codec_filter_to_str(&cc_trans->cc.codecs, &cc_trans->cc.local, &cc_trans->cc.remote));
658 call_leg_release(msc_a->cc.call_leg);
659 return;
660 }
661
662 /* Compose 48.008 Channel Type from the current set of codecs
663 * determined from both local and remote codec capabilities. */
664 if (sdp_audio_codecs_to_gsm0808_channel_type(&channel_type, &cc_trans->cc.local.audio_codecs)) {
665 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot compose Channel Type (Permitted Speech) from codecs: %s\n",
666 codec_filter_to_str(&cc_trans->cc.codecs, &cc_trans->cc.local, &cc_trans->cc.remote));
667 trans_free(cc_trans);
668 return;
669 }
670 break;
671 case GSM48_BCAP_ITCAP_UNR_DIG_INF:
672 if (!cc_trans->cc.local.bearer_services.count) {
673 LOG_TRANS(cc_trans, LOGL_ERROR, "Assignment not possible, no matching bearer service: %s\n",
674 csd_filter_to_str(&cc_trans->cc.csd, &cc_trans->cc.local, &cc_trans->cc.remote));
675 call_leg_release(msc_a->cc.call_leg);
676 return;
677 }
678
679 /* Compose 48.008 Channel Type from the current set of bearer
680 * services determined from local and remote capabilities. */
681 if (csd_bs_list_to_gsm0808_channel_type(&channel_type, &cc_trans->cc.local.bearer_services)) {
682 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot compose channel type from: %s\n",
683 csd_filter_to_str(&cc_trans->cc.csd, &cc_trans->cc.local, &cc_trans->cc.remote));
684 return;
685 }
686 break;
687 default:
688 LOG_TRANS(cc_trans, LOGL_ERROR, "Assignment not possible for information transfer capability %d\n",
689 cc_trans->bearer_cap.transfer);
Neels Hofmeyr11a746a2023-01-26 15:00:06 +0100690 call_leg_release(msc_a->cc.call_leg);
691 return;
692 }
693
Oliver Smith10632132023-05-12 12:14:22 +0200694 /* The RAN side RTP address is known, so the voice/CSD Assignment can commence. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100695 msg = (struct ran_msg){
696 .msg_type = RAN_MSG_ASSIGNMENT_COMMAND,
697 .assignment_command = {
698 .cn_rtp = &msc_a->cc.call_leg->rtp[RTP_TO_RAN]->local,
699 .channel_type = &channel_type,
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200700 .osmux_present = msc_a->cc.call_leg->rtp[RTP_TO_RAN]->use_osmux,
701 .osmux_cid = msc_a->cc.call_leg->rtp[RTP_TO_RAN]->local_osmux_cid,
Philipp Maierf34d9452020-06-05 15:49:35 +0200702 .call_id_present = true,
Andreas Eversberg712b28e2023-06-21 11:17:26 +0200703 .call_id = cc_trans->call_id,
Keith Whytea1a70be2021-05-16 02:59:52 +0200704 .lcls = cc_trans->cc.lcls,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100705 },
706 };
707 if (msc_a_ran_down(msc_a, MSC_ROLE_I, &msg)) {
708 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot send Assignment\n");
Neels Hofmeyrf439ff12019-10-05 04:19:36 +0200709 trans_free(cc_trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100710 return;
711 }
712}
713
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100714static struct gsm_trans *find_waiting_call(struct msc_a *msc_a)
715{
716 struct gsm_trans *trans;
717 struct gsm_network *net = msc_a_net(msc_a);
718
719 llist_for_each_entry(trans, &net->trans_list, entry) {
720 if (trans->msc_a != msc_a)
721 continue;
722 if (trans->type != TRANS_CC)
723 continue;
724 if (trans->msc_a->cc.active_trans == trans)
725 continue;
726 return trans;
727 }
728 return NULL;
729}
730
731static void msc_a_cleanup_rtp_streams(struct msc_a *msc_a, uint32_t event, void *data)
732{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100733 switch (event) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100734
735 case MSC_EV_CALL_LEG_TERM:
736 msc_a->cc.call_leg = NULL;
737 if (msc_a->cc.mncc_forwarding_to_remote_ran)
738 msc_a->cc.mncc_forwarding_to_remote_ran->rtps = NULL;
739
Neels Hofmeyr265a4c72019-05-09 16:20:51 +0200740 if (msc_a->ho.new_cell.mncc_forwarding_to_remote_ran)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100741 msc_a->ho.new_cell.mncc_forwarding_to_remote_ran->rtps = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100742 return;
743
744 case MSC_MNCC_EV_CALL_ENDED:
745 msc_a->cc.mncc_forwarding_to_remote_ran = NULL;
746 return;
747
748 default:
749 return;
750 }
751}
752
753static void msc_a_fsm_communicating(struct osmo_fsm_inst *fi, uint32_t event, void *data)
754{
755 struct msc_a *msc_a = fi->priv;
756 struct rtp_stream *rtps;
757 struct gsm_trans *waiting_trans;
758 struct an_apdu *an_apdu;
759
760 msc_a_cleanup_rtp_streams(msc_a, event, data);
761
762 switch (event) {
763 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
764 case MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST:
765 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
766 an_apdu = data;
767 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_I);
768 return;
769
770 case MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE:
771 case MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE:
772 case MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST:
773 case MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST:
774 an_apdu = data;
775 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_T);
776 return;
777
778 case MSC_A_EV_TRANSACTION_ACCEPTED:
779 /* no-op */
780 return;
781
782 case MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE:
783 rtps = data;
784 if (!rtps) {
785 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid data for %s\n", osmo_fsm_event_name(fi->fsm, event));
786 return;
787 }
Neels Hofmeyr00a476b2019-11-28 02:46:05 +0100788 if (!msc_a->cc.call_leg) {
789 LOG_MSC_A(msc_a, LOGL_ERROR, "No call leg active\n");
790 return;
791 }
Neels Hofmeyrcc918cb2019-11-28 02:16:34 +0100792 if (!osmo_sockaddr_str_is_nonzero(&rtps->local)) {
793 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid RTP address received from MGW: " OSMO_SOCKADDR_STR_FMT "\n",
794 OSMO_SOCKADDR_STR_FMT_ARGS(&rtps->local));
795 call_leg_release(msc_a->cc.call_leg);
796 return;
797 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100798 LOG_MSC_A(msc_a, LOGL_DEBUG,
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200799 "MGW endpoint's RTP address available for the CI %s: " OSMO_SOCKADDR_STR_FMT " (osmux=%s:%d)\n",
800 rtp_direction_name(rtps->dir), OSMO_SOCKADDR_STR_FMT_ARGS(&rtps->local),
801 rtps->use_osmux ? "yes" : "no", rtps->local_osmux_cid);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100802 switch (rtps->dir) {
803 case RTP_TO_RAN:
804 msc_a_call_leg_ran_local_addr_available(msc_a);
805 return;
806 case RTP_TO_CN:
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +0100807 cc_on_cn_local_rtp_port_known(rtps->for_trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100808 return;
809 default:
810 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid data for %s\n", osmo_fsm_event_name(fi->fsm, event));
811 return;
812 }
813
814 case MSC_EV_CALL_LEG_RTP_COMPLETE:
815 /* Nothing to do. */
816 return;
817
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100818 case MSC_MNCC_EV_CALL_ENDED:
819 /* Cleaned up above */
820 return;
821
822 case MSC_EV_CALL_LEG_TERM:
823 /* RTP streams cleaned up above */
824
825 msc_a_get(msc_a, __func__);
826 if (msc_a->cc.active_trans)
827 trans_free(msc_a->cc.active_trans);
828
829 /* If there is another call still waiting to be activated, this is the time when the mgcp_ctx is
830 * available again and the other call can start assigning. */
831 waiting_trans = find_waiting_call(msc_a);
832 if (waiting_trans) {
833 LOG_MSC_A(msc_a, LOGL_DEBUG, "(ti %02x) Call waiting: starting Assignment\n",
834 waiting_trans->transaction_id);
835 msc_a_try_call_assignment(waiting_trans);
836 }
837 msc_a_put(msc_a, __func__);
838 return;
839
840 case MSC_A_EV_HANDOVER_REQUIRED:
841 msc_ho_start(msc_a, (struct ran_handover_required*)data);
842 return;
843
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200844 case MSC_A_EV_HANDOVER_END:
845 /* Termination event of the msc_ho_fsm. No action needed, it's all done in the msc_ho_fsm cleanup. This
846 * event only exists because osmo_fsm_inst_alloc_child() requires a parent term event; and maybe
847 * interesting for logging. */
848 return;
849
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100850 case MSC_A_EV_MO_CLOSE:
851 case MSC_A_EV_CN_CLOSE:
852 case MSC_A_EV_UNUSED:
853 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
854 return;
855
856 default:
857 OSMO_ASSERT(false);
858 }
859}
860
861static int msc_a_fsm_timer_cb(struct osmo_fsm_inst *fi)
862{
863 struct msc_a *msc_a = fi->priv;
864 if (msc_a_in_release(msc_a)) {
865 LOG_MSC_A(msc_a, LOGL_ERROR, "Timeout while releasing, discarding right now\n");
866 msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
867 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
868 } else {
869 enum gsm48_reject_value cause = GSM48_REJECT_CONGESTION;
870 osmo_fsm_inst_dispatch(fi, MSC_A_EV_CN_CLOSE, &cause);
871 }
872 return 0;
873}
874
875static void msc_a_fsm_releasing_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
876{
877 struct msc_a *msc_a = fi->priv;
878 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
879 int i;
880 char buf[128];
881 const char * const use_counts_to_cancel[] = {
882 MSC_A_USE_LOCATION_UPDATING,
883 MSC_A_USE_CM_SERVICE_CC,
884 MSC_A_USE_CM_SERVICE_SMS,
885 MSC_A_USE_CM_SERVICE_SS,
Andreas Eversberg456c6f72023-04-23 11:54:16 +0200886 MSC_A_USE_CM_SERVICE_GCC,
887 MSC_A_USE_CM_SERVICE_BCC,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100888 MSC_A_USE_PAGING_RESPONSE,
889 };
890
891 LOG_MSC_A(msc_a, LOGL_DEBUG, "Releasing: msc_a use is %s\n",
892 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
893
894 if (vsub) {
895 vlr_subscr_get(vsub, __func__);
896
897 /* Cancel all VLR FSMs, if any */
898 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
899
900 /* The subscriber has no active connection anymore.
901 * Restart the periodic Location Update expiry timer for this subscriber. */
902 vlr_subscr_enable_expire_lu(vsub);
903 }
904
905 /* If we're closing in a middle of a trans, we need to clean up */
906 trans_conn_closed(msc_a);
907
908 call_leg_release(msc_a->cc.call_leg);
909
910 /* Cancel use counts for pending CM Service / Paging */
911 for (i = 0; i < ARRAY_SIZE(use_counts_to_cancel); i++) {
912 const char *use = use_counts_to_cancel[i];
913 int32_t count = osmo_use_count_by(&msc_a->use_count, use);
914 if (!count)
915 continue;
916 LOG_MSC_A(msc_a, LOGL_DEBUG, "Releasing: canceling still pending use: %s (%d)\n", use, count);
917 osmo_use_count_get_put(&msc_a->use_count, use, -count);
918 }
919
920 if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) {
921 sgs_iface_tx_release(vsub);
922 /* In SGsAP there is no confirmation of a release. */
923 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
924 } else {
925 struct ran_msg msg = {
926 .msg_type = RAN_MSG_CLEAR_COMMAND,
927 .clear_command = {
Neels Hofmeyrd9fe7112020-07-11 00:20:20 +0200928 /* "Call Control" is the only cause code listed in 3GPP TS 48.008 3.2.1.21 CLEAR COMMAND
929 * that qualifies for a normal release situation. (OS#4664) */
930 .gsm0808_cause = GSM0808_CAUSE_CALL_CONTROL,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100931 .csfb_ind = (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED),
932 },
933 };
934 msc_a_get(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
935 msc_a_ran_down(msc_a, MSC_ROLE_I, &msg);
Philipp Maier47cf84d2019-08-15 14:56:54 +0200936
937 /* The connection is cleared. The MS will now go back to 4G,
938 Switch the RAN type back to SGS. */
939 if (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED)
940 vsub->cs.attached_via_ran = OSMO_RAT_EUTRAN_SGS;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100941 }
942
943 if (vsub)
944 vlr_subscr_put(vsub, __func__);
945}
946
947static void msc_a_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
948{
949 struct msc_a *msc_a = fi->priv;
950
951 msc_a_cleanup_rtp_streams(msc_a, event, data);
952
953 switch (event) {
954 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
955 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
956 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
957 return;
958
959 case MSC_A_EV_MO_CLOSE:
960 case MSC_A_EV_CN_CLOSE:
961 case MSC_A_EV_UNUSED:
962 /* Already releasing */
963 return;
964
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100965 case MSC_EV_CALL_LEG_TERM:
966 case MSC_MNCC_EV_CALL_ENDED:
967 /* RTP streams cleaned up above */
968 return;
969
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200970 case MSC_A_EV_HANDOVER_END:
971 /* msc_ho_fsm does cleanup. */
972 return;
973
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100974 default:
975 OSMO_ASSERT(false);
976 }
977}
978
979
980static void msc_a_fsm_released_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
981{
982 struct msc_a *msc_a = msc_a_fi_priv(fi);
983 char buf[128];
984 LOG_MSC_A(msc_a, LOGL_DEBUG, "Released: msc_a use is %s\n",
985 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
986 if (osmo_use_count_total(&msc_a->use_count) == 0)
987 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi);
988}
989
990static void msc_a_fsm_released(struct osmo_fsm_inst *fi, uint32_t event, void *data)
991{
992 if (event == MSC_A_EV_UNUSED)
993 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi);
994}
995
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100996void msc_a_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
997{
998 struct msc_a *msc_a = msc_a_fi_priv(fi);
999
1000 trans_conn_closed(msc_a);
1001
1002 if (msc_a_fsm_has_active_transactions(fi))
1003 LOG_MSC_A(msc_a, LOGL_ERROR, "Deallocating active transactions failed\n");
1004
1005 LOG_MSC_A_CAT(msc_a, DREF, LOGL_DEBUG, "max total use count was %d\n", msc_a->max_total_use_count);
1006}
1007
1008const struct value_string msc_a_fsm_event_names[] = {
1009 OSMO_VALUE_STRING(MSC_REMOTE_EV_RX_GSUP),
1010 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE),
1011 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_COMPLETE),
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001012 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_TERM),
1013 OSMO_VALUE_STRING(MSC_MNCC_EV_NEED_LOCAL_RTP),
1014 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_PROCEEDING),
1015 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_COMPLETE),
1016 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_ENDED),
1017 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_COMPLETE_LAYER_3),
1018 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST),
1019 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST),
1020 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST),
1021 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST),
1022 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE),
1023 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE),
1024 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST),
1025 OSMO_VALUE_STRING(MSC_A_EV_COMPLETE_LAYER_3_OK),
1026 OSMO_VALUE_STRING(MSC_A_EV_CLASSMARK_UPDATE),
1027 OSMO_VALUE_STRING(MSC_A_EV_AUTHENTICATED),
1028 OSMO_VALUE_STRING(MSC_A_EV_TRANSACTION_ACCEPTED),
1029 OSMO_VALUE_STRING(MSC_A_EV_CN_CLOSE),
1030 OSMO_VALUE_STRING(MSC_A_EV_MO_CLOSE),
1031 OSMO_VALUE_STRING(MSC_A_EV_UNUSED),
1032 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_REQUIRED),
1033 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_END),
1034 {}
1035};
1036
1037#define S(x) (1 << (x))
1038
1039static const struct osmo_fsm_state msc_a_fsm_states[] = {
1040 [MSC_A_ST_VALIDATE_L3] = {
1041 .name = OSMO_STRINGIFY(MSC_A_ST_VALIDATE_L3),
1042 .in_event_mask = 0
1043 | S(MSC_A_EV_FROM_I_COMPLETE_LAYER_3)
1044 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1045 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1046 | S(MSC_A_EV_COMPLETE_LAYER_3_OK)
1047 | S(MSC_A_EV_MO_CLOSE)
1048 | S(MSC_A_EV_CN_CLOSE)
1049 | S(MSC_A_EV_UNUSED)
1050 ,
1051 .out_state_mask = 0
1052 | S(MSC_A_ST_VALIDATE_L3)
1053 | S(MSC_A_ST_AUTH_CIPH)
1054 | S(MSC_A_ST_RELEASING)
1055 ,
1056 .action = msc_a_fsm_validate_l3,
1057 },
1058 [MSC_A_ST_AUTH_CIPH] = {
1059 .name = OSMO_STRINGIFY(MSC_A_ST_AUTH_CIPH),
1060 .in_event_mask = 0
1061 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1062 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1063 | S(MSC_A_EV_AUTHENTICATED)
1064 | S(MSC_A_EV_MO_CLOSE)
1065 | S(MSC_A_EV_CN_CLOSE)
1066 | S(MSC_A_EV_UNUSED)
1067 ,
1068 .out_state_mask = 0
1069 | S(MSC_A_ST_WAIT_CLASSMARK_UPDATE)
1070 | S(MSC_A_ST_AUTHENTICATED)
1071 | S(MSC_A_ST_RELEASING)
1072 ,
1073 .action = msc_a_fsm_auth_ciph,
1074 },
1075 [MSC_A_ST_WAIT_CLASSMARK_UPDATE] = {
1076 .name = OSMO_STRINGIFY(MSC_A_ST_WAIT_CLASSMARK_UPDATE),
1077 .in_event_mask = 0
1078 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1079 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1080 | S(MSC_A_EV_CLASSMARK_UPDATE)
1081 | S(MSC_A_EV_MO_CLOSE)
1082 | S(MSC_A_EV_CN_CLOSE)
1083 ,
1084 .out_state_mask = 0
1085 | S(MSC_A_ST_AUTH_CIPH)
1086 | S(MSC_A_ST_RELEASING)
1087 ,
1088 .action = msc_a_fsm_wait_classmark_update,
1089 },
1090 [MSC_A_ST_AUTHENTICATED] = {
1091 .name = OSMO_STRINGIFY(MSC_A_ST_AUTHENTICATED),
1092 /* allow everything to release for any odd behavior */
1093 .in_event_mask = 0
1094 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1095 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
1096 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1097 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
1098 | S(MSC_A_EV_MO_CLOSE)
1099 | S(MSC_A_EV_CN_CLOSE)
1100 | S(MSC_A_EV_UNUSED)
1101 ,
1102 .out_state_mask = 0
1103 | S(MSC_A_ST_RELEASING)
1104 | S(MSC_A_ST_COMMUNICATING)
1105 ,
1106 .onenter = msc_a_fsm_authenticated_enter,
1107 .action = msc_a_fsm_authenticated,
1108 },
1109 [MSC_A_ST_COMMUNICATING] = {
1110 .name = OSMO_STRINGIFY(MSC_A_ST_COMMUNICATING),
1111 /* allow everything to release for any odd behavior */
1112 .in_event_mask = 0
1113 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1114 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
1115 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1116 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE)
1117 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE)
1118 | S(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST)
1119 | S(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST)
1120 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
1121 | S(MSC_A_EV_MO_CLOSE)
1122 | S(MSC_A_EV_CN_CLOSE)
1123 | S(MSC_A_EV_UNUSED)
1124 | S(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE)
1125 | S(MSC_EV_CALL_LEG_RTP_COMPLETE)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001126 | S(MSC_EV_CALL_LEG_TERM)
1127 | S(MSC_MNCC_EV_CALL_ENDED)
1128 | S(MSC_A_EV_HANDOVER_REQUIRED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +02001129 | S(MSC_A_EV_HANDOVER_END)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001130 ,
1131 .out_state_mask = 0
1132 | S(MSC_A_ST_RELEASING)
1133 ,
1134 .action = msc_a_fsm_communicating,
1135 },
1136 [MSC_A_ST_RELEASING] = {
1137 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASING),
1138 .in_event_mask = 0
1139 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1140 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1141 | S(MSC_A_EV_UNUSED)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001142 | S(MSC_EV_CALL_LEG_TERM)
1143 | S(MSC_MNCC_EV_CALL_ENDED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +02001144 | S(MSC_A_EV_HANDOVER_END)
Pau Espin Pedrole53ecde2021-07-12 13:37:24 +02001145 | S(MSC_A_EV_CN_CLOSE)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001146 ,
1147 .out_state_mask = 0
1148 | S(MSC_A_ST_RELEASED)
1149 ,
1150 .onenter = msc_a_fsm_releasing_onenter,
1151 .action = msc_a_fsm_releasing,
1152 },
1153 [MSC_A_ST_RELEASED] = {
1154 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASED),
1155 .in_event_mask = 0
1156 | S(MSC_A_EV_UNUSED)
1157 ,
1158 .onenter = msc_a_fsm_released_onenter,
1159 .action = msc_a_fsm_released,
1160 },
1161};
1162
1163static struct osmo_fsm msc_a_fsm = {
1164 .name = "msc_a",
1165 .states = msc_a_fsm_states,
1166 .num_states = ARRAY_SIZE(msc_a_fsm_states),
1167 .log_subsys = DMSC,
1168 .event_names = msc_a_fsm_event_names,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001169 .timer_cb = msc_a_fsm_timer_cb,
1170 .cleanup = msc_a_fsm_cleanup,
1171};
1172
1173static __attribute__((constructor)) void msc_a_fsm_init()
1174{
1175 OSMO_ASSERT(osmo_fsm_register(&msc_a_fsm) == 0);
1176}
1177
1178static int msc_a_use_cb(struct osmo_use_count_entry *e, int32_t old_use_count, const char *file, int line)
1179{
1180 struct msc_a *msc_a = e->use_count->talloc_object;
1181 char buf[128];
1182 int32_t total;
1183 int level;
1184
1185 if (!e->use)
1186 return -EINVAL;
1187
1188 total = osmo_use_count_total(&msc_a->use_count);
1189
1190 if (total == 0
1191 || (total == 1 && old_use_count == 0 && e->count == 1))
1192 level = LOGL_INFO;
1193 else
1194 level = LOGL_DEBUG;
1195
1196 LOG_MSC_A_CAT_SRC(msc_a, DREF, level, file, line, "%s %s: now used by %s\n",
1197 (e->count - old_use_count) > 0? "+" : "-", e->use,
1198 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
1199
1200 if (e->count < 0)
1201 return -ERANGE;
1202
1203 msc_a->max_total_use_count = OSMO_MAX(msc_a->max_total_use_count, total);
1204
1205 if (total == 0)
1206 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_UNUSED, NULL);
1207 return 0;
1208}
1209
1210struct msc_a *msc_a_alloc(struct msub *msub, struct ran_infra *ran)
1211{
1212 struct msc_a *msc_a = msub_role_alloc(msub, MSC_ROLE_A, &msc_a_fsm, struct msc_a, ran);
1213 msc_a->use_count = (struct osmo_use_count){
1214 .talloc_object = msc_a,
1215 .use_cb = msc_a_use_cb,
1216 };
1217 osmo_use_count_make_static_entries(&msc_a->use_count, msc_a->use_count_buf, ARRAY_SIZE(msc_a->use_count_buf));
1218 /* Start timeout for first state */
Neels Hofmeyr01653252019-09-03 02:06:22 +02001219 msc_a_state_chg_always(msc_a, MSC_A_ST_VALIDATE_L3);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001220 return msc_a;
1221}
1222
1223bool msc_a_is_establishing_auth_ciph(const struct msc_a *msc_a)
1224{
1225 if (!msc_a || !msc_a->c.fi)
1226 return false;
1227 return msc_a->c.fi->state == MSC_A_ST_AUTH_CIPH;
1228}
1229
1230const struct value_string complete_layer3_type_names[] = {
1231 { COMPLETE_LAYER3_NONE, "NONE" },
1232 { COMPLETE_LAYER3_LU, "LU" },
1233 { COMPLETE_LAYER3_CM_SERVICE_REQ, "CM_SERVICE_REQ" },
1234 { COMPLETE_LAYER3_PAGING_RESP, "PAGING_RESP" },
Neels Hofmeyrae98b972021-07-27 03:46:49 +02001235 { COMPLETE_LAYER3_CM_RE_ESTABLISH_REQ, "CM_RE_ESTABLISH_REQ" },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001236 { 0, NULL }
1237};
1238
1239#define _msc_a_update_id(MSC_A, FMT, ARGS ...) \
1240 do { \
1241 if (osmo_fsm_inst_update_id_f(msc_a->c.fi, FMT ":%s:%s", \
1242 ## ARGS, \
1243 msub_ran_conn_name(msc_a->c.msub), \
1244 complete_layer3_type_name(msc_a->complete_layer3_type)) \
1245 == 0) { \
1246 struct vlr_subscr *_vsub = msc_a_vsub(MSC_A); \
1247 if (_vsub) { \
1248 if (_vsub->lu_fsm) \
1249 osmo_fsm_inst_update_id(_vsub->lu_fsm, (MSC_A)->c.fi->id); \
1250 if (_vsub->auth_fsm) \
1251 osmo_fsm_inst_update_id(_vsub->auth_fsm, (MSC_A)->c.fi->id); \
1252 if (_vsub->proc_arq_fsm) \
1253 osmo_fsm_inst_update_id(_vsub->proc_arq_fsm, (MSC_A)->c.fi->id); \
1254 } \
1255 LOG_MSC_A(MSC_A, LOGL_DEBUG, "Updated ID\n"); \
1256 } \
1257 /* otherwise osmo_fsm_inst_update_id_f() will log an error. */ \
1258 } while (0)
1259
1260
1261/* 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 +02001262void msc_a_update_id_from_mi(struct msc_a *msc_a, const struct osmo_mobile_identity *mi)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001263{
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001264 _msc_a_update_id(msc_a, "%s", osmo_mobile_identity_to_str_c(OTC_SELECT, mi));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001265}
1266
1267/* Update msc_a->fi id string from current msc_a->vsub and msc_a->complete_layer3_type. */
1268void msc_a_update_id(struct msc_a *msc_a)
1269{
1270 _msc_a_update_id(msc_a, "%s", vlr_subscr_name(msc_a_vsub(msc_a)));
1271}
1272
1273/* Iterate all msc_a instances that are relevant for this subscriber, and update FSM ID strings for all of the FSM
1274 * instances. */
1275void msc_a_update_id_for_vsub(struct vlr_subscr *for_vsub)
1276{
1277 struct msub *msub;
1278 llist_for_each_entry(msub, &msub_list, entry) {
1279 struct vlr_subscr *vsub = msub_vsub(msub);
1280 if (vsub != for_vsub)
1281 continue;
1282 msc_a_update_id(msub_msc_a(msub));
1283 }
1284}
1285
1286static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
1287{
1288 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
1289 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
1290
1291 switch (pdisc) {
1292 case GSM48_PDISC_MM:
1293 switch (msg_type) {
1294 case GSM48_MT_MM_LOC_UPD_REQUEST:
1295 case GSM48_MT_MM_CM_SERV_REQ:
1296 case GSM48_MT_MM_CM_REEST_REQ:
1297 case GSM48_MT_MM_AUTH_RESP:
1298 case GSM48_MT_MM_AUTH_FAIL:
1299 case GSM48_MT_MM_ID_RESP:
1300 case GSM48_MT_MM_TMSI_REALL_COMPL:
1301 case GSM48_MT_MM_IMSI_DETACH_IND:
1302 return true;
1303 default:
1304 break;
1305 }
1306 break;
1307 case GSM48_PDISC_RR:
1308 switch (msg_type) {
1309 /* GSM48_MT_RR_CIPH_M_COMPL is actually handled in bssmap_rx_ciph_compl() and gets redirected in the
1310 * BSSAP layer to ran_conn_cipher_mode_compl() (before this here is reached) */
1311 case GSM48_MT_RR_PAG_RESP:
1312 case GSM48_MT_RR_CIPH_M_COMPL:
1313 return true;
1314 default:
1315 break;
1316 }
1317 break;
1318 default:
1319 break;
1320 }
1321
1322 return false;
1323}
1324
1325/* Main entry point for GSM 04.08/44.008 Layer 3 data (e.g. from the BSC). */
1326int msc_a_up_l3(struct msc_a *msc_a, struct msgb *msg)
1327{
1328 struct gsm48_hdr *gh;
1329 uint8_t pdisc;
1330 int rc;
1331 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1332 int is_r99;
1333
1334 OSMO_ASSERT(msg->l3h);
1335 OSMO_ASSERT(msg);
1336
1337 gh = msgb_l3(msg);
1338 pdisc = gsm48_hdr_pdisc(gh);
1339
1340 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_DEBUG, "Dispatching 04.08 message: %s %s\n",
1341 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1342
1343 /* To evaluate the 3GPP TS 24.007 Duplicate Detection, we need Classmark information on whether the MS is R99
1344 * capable. If the subscriber is already actively connected, the Classmark information is stored with the
1345 * vlr_subscr. Otherwise, this *must* be a Complete Layer 3 with Classmark info. */
1346 if (vsub)
1347 is_r99 = osmo_gsm48_classmark_is_r99(&vsub->classmark) ? 1 : 0;
1348 else
1349 is_r99 = compl_l3_msg_is_r99(msg);
1350
1351 if (is_r99 < 0) {
1352 LOG_MSC_A(msc_a, LOGL_ERROR,
1353 "No Classmark Information, dropping non-Complete-Layer3 message: %s\n",
1354 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1355 return -EACCES;
1356 }
1357
1358 if (is_r99 >= 0
1359 && ran_dec_dtap_undup_is_duplicate(msc_a->c.fi, msc_a->n_sd_next, is_r99 ? true : false, msg)) {
1360 LOG_MSC_A(msc_a, LOGL_DEBUG, "Dropping duplicate message"
1361 " (3GPP TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection)\n");
1362 return 0;
1363 }
1364
1365 if (!msc_a_is_accepted(msc_a)
1366 && !msg_is_initially_permitted(gh)) {
1367 LOG_MSC_A(msc_a, LOGL_ERROR,
1368 "Message not permitted for initial conn: %s\n",
1369 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1370 return -EACCES;
1371 }
1372
1373 if (vsub && vsub->cs.attached_via_ran != msc_a->c.ran->type) {
1374 LOG_MSC_A(msc_a, LOGL_ERROR,
1375 "Illegal situation: RAN type mismatch:"
1376 " attached via %s, received message via %s\n",
1377 osmo_rat_type_name(vsub->cs.attached_via_ran),
1378 osmo_rat_type_name(msc_a->c.ran->type));
1379 return -EACCES;
1380 }
1381
1382#if 0
1383 if (silent_call_reroute(conn, msg))
1384 return silent_call_rx(conn, msg);
1385#endif
1386
1387 switch (pdisc) {
1388 case GSM48_PDISC_CC:
1389 rc = gsm0408_rcv_cc(msc_a, msg);
1390 break;
1391 case GSM48_PDISC_MM:
1392 rc = gsm0408_rcv_mm(msc_a, msg);
1393 break;
1394 case GSM48_PDISC_RR:
1395 rc = gsm0408_rcv_rr(msc_a, msg);
1396 break;
1397 case GSM48_PDISC_SMS:
1398 rc = gsm0411_rcv_sms(msc_a, msg);
1399 break;
1400 case GSM48_PDISC_MM_GPRS:
1401 case GSM48_PDISC_SM_GPRS:
1402 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unimplemented "
1403 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1404 rc = -ENOTSUP;
1405 break;
1406 case GSM48_PDISC_NC_SS:
1407 rc = gsm0911_rcv_nc_ss(msc_a, msg);
1408 break;
1409 case GSM48_PDISC_TEST:
1410 rc = gsm0414_rcv_test(msc_a, msg);
1411 break;
1412 default:
1413 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unknown "
1414 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1415 rc = -EINVAL;
1416 break;
1417 }
1418
1419 return rc;
1420}
1421
1422static void msc_a_up_call_assignment_complete(struct msc_a *msc_a, const struct ran_msg *ac)
1423{
1424 struct gsm_trans *cc_trans = msc_a->cc.active_trans;
1425 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 +01001426 const struct gsm0808_speech_codec *codec_if_known = ac->assignment_complete.codec_present ?
1427 &ac->assignment_complete.codec : NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001428
1429 if (!rtps_to_ran) {
1430 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but no RTP stream is set up\n");
1431 return;
1432 }
1433 if (!cc_trans) {
Neels Hofmeyr550506a2022-01-13 23:31:57 +01001434 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but no CC transaction is active\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001435 return;
1436 }
1437
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001438 if (rtps_to_ran->use_osmux != ac->assignment_complete.osmux_present) {
1439 LOG_MSC_A_CAT(msc_a, DCC, LOGL_ERROR, "Osmux usage ass request and complete don't match: %d vs %d\n",
1440 rtps_to_ran->use_osmux, ac->assignment_complete.osmux_present);
1441 call_leg_release(msc_a->cc.call_leg);
1442 return;
1443 }
1444
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001445 if (codec_if_known) {
Neels Hofmeyr388d7c92023-03-22 19:03:15 +01001446 const struct codec_mapping *codec_cn;
1447 /* For 2G:
1448 * - The Assignment Complete has returned a specific codec (e.g. FR3 for AMR FR).
1449 * - Set this codec at the MGW endpoint facing the RAN.
1450 * - Also set this codec at the MGW endpoint facing the CN -- we require an exact match on both call
1451 * legs.
1452 * - TODO: be aware of transcoding that the MGW is capable of, e.g. AMR octet-aligned to AMR
1453 * bandwidth-efficient...
1454 *
1455 * For 3G:
1456 * - ran_infra->force_mgw_codecs_to_ran sets VND.3GPP.IUFP as single codec at the MGW towards RAN.
1457 * - ran_msg_iu.c always returns FR3 (AMR FR) for the assigned codec. Set that at the MGW towards CN.
1458 * - So the MGW decapsulates IuUP <-> AMR
1459 */
1460 codec_cn = codec_mapping_by_gsm0808_speech_codec_type(codec_if_known->type);
1461 /* TODO: use codec_mapping_by_gsm0808_speech_codec() to also match on codec_if_known->cfg */
1462 if (!codec_cn) {
1463 LOG_TRANS(cc_trans, LOGL_ERROR, "Unknown codec in Assignment Complete: %s\n",
1464 gsm0808_speech_codec_type_name(codec_if_known->type));
1465 call_leg_release(msc_a->cc.call_leg);
1466 return;
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001467 }
1468
Neels Hofmeyrcec51b32023-03-01 03:47:45 +01001469 /* Update RAN-side endpoint CI from Assignment result -- unless it is forced by the ran_infra, in which
1470 * case it remains unchanged as passed to the earlier call of call_leg_ensure_ci(). */
1471 if (msc_a->c.ran->force_mgw_codecs_to_ran.count == 0)
1472 rtp_stream_set_one_codec(rtps_to_ran, &codec_cn->sdp);
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001473
1474 /* Update codec filter with Assignment result, for the CN side */
1475 cc_trans->cc.codecs.assignment = codec_cn->sdp;
1476 } else {
1477 /* No codec passed in Assignment Complete, set 'codecs.assignment' to none. */
1478 cc_trans->cc.codecs.assignment = (struct sdp_audio_codec){};
1479 LOG_TRANS(cc_trans, LOGL_INFO, "Assignment Complete without voice codec\n");
1480 }
1481
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001482 rtp_stream_set_remote_addr(rtps_to_ran, &ac->assignment_complete.remote_rtp);
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001483 if (rtps_to_ran->use_osmux)
1484 rtp_stream_set_remote_osmux_cid(rtps_to_ran,
1485 ac->assignment_complete.osmux_cid);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001486 rtp_stream_commit(rtps_to_ran);
1487
Neels Hofmeyr2d57d6e2022-01-13 21:39:58 +01001488 /* Remember the Codec List (BSS Supported) */
1489 if (ac->assignment_complete.codec_list_bss_supported)
1490 codec_filter_set_bss(&cc_trans->cc.codecs, ac->assignment_complete.codec_list_bss_supported);
1491
Oliver Smithceca8e62023-05-24 11:15:52 +02001492 trans_cc_filter_run(cc_trans);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001493 LOG_TRANS(cc_trans, LOGL_INFO, "Assignment Complete: RAN: %s, CN: %s\n",
1494 sdp_audio_codecs_to_str(&rtps_to_ran->codecs),
Oliver Smithc63c3a02023-05-24 10:48:07 +02001495 sdp_audio_codecs_to_str(&cc_trans->cc.local.audio_codecs));
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001496
1497 if (cc_on_assignment_done(cc_trans)) {
1498 /* If an error occurred, it was logged in cc_assignment_done() */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001499 call_leg_release(msc_a->cc.call_leg);
1500 return;
1501 }
1502}
1503
1504static void msc_a_up_call_assignment_failure(struct msc_a *msc_a, const struct ran_msg *af)
1505{
1506 struct gsm_trans *trans;
1507
1508 /* For a normal voice call, there will be an rtp_stream FSM. */
1509 if (msc_a->cc.call_leg && msc_a->cc.call_leg->rtp[RTP_TO_RAN]) {
1510 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing call\n");
1511 rtp_stream_release(msc_a->cc.call_leg->rtp[RTP_TO_RAN]);
1512 return;
1513 }
1514
1515 /* Otherwise, a silent call might be active */
1516 trans = trans_find_by_type(msc_a, TRANS_SILENT_CALL);
1517 if (trans) {
1518 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing silent call\n");
1519 trans_free(trans);
1520 return;
1521 }
1522
1523 /* Neither a voice call nor silent call assignment. Assume the worst and detach. */
1524 msc_a_release_cn(msc_a);
1525}
1526
1527static void msc_a_up_classmark_update(struct msc_a *msc_a, const struct osmo_gsm48_classmark *classmark,
1528 struct osmo_gsm48_classmark *dst)
1529{
1530 if (!dst) {
1531 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1532
1533 if (!vsub)
1534 dst = &msc_a->temporary_classmark;
1535 else
1536 dst = &vsub->classmark;
1537 }
1538
Martin Hauke3f07dac2019-11-14 17:49:08 +01001539 LOG_MSC_A(msc_a, LOGL_DEBUG, "A5 capabilities received from Classmark Update: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001540 osmo_gsm48_classmark_a5_name(classmark));
1541 osmo_gsm48_classmark_update(dst, classmark);
1542
1543 /* bump subscr conn FSM in case it is waiting for a Classmark Update */
1544 if (msc_a->c.fi->state == MSC_A_ST_WAIT_CLASSMARK_UPDATE)
1545 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CLASSMARK_UPDATE, NULL);
1546}
1547
1548static void msc_a_up_sapi_n_reject(struct msc_a *msc_a, const struct ran_msg *msg)
1549{
1550 int sapi = msg->sapi_n_reject.dlci & 0x7;
1551 if (sapi == UM_SAPI_SMS)
1552 gsm411_sapi_n_reject(msc_a);
1553}
1554
1555static int msc_a_up_ho(struct msc_a *msc_a, const struct msc_a_ran_dec_data *d, uint32_t ho_fi_event)
1556{
1557 if (!msc_a->ho.fi) {
1558 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Handover message, but no Handover ongoing: %s\n", d->ran_dec->msg_name);
1559 return -EINVAL;
1560 }
1561 return osmo_fsm_inst_dispatch(msc_a->ho.fi, ho_fi_event, (void*)d);
1562}
1563
1564int msc_a_ran_dec_from_msc_i(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1565{
1566 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Alexander Couzens2aaff752021-10-19 17:09:11 +02001567 struct gsm_network *net = msc_a_net(msc_a);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001568 const struct ran_msg *msg = d->ran_dec;
1569 int rc = -99;
1570
1571 switch (msg->msg_type) {
1572
1573 case RAN_MSG_COMPL_L3:
Neels Hofmeyr68f50da2020-06-24 14:22:52 +02001574 /* In case the cell_id from Complete Layer 3 Information lacks a PLMN, write the configured PLMN code
1575 * into msc_a->via_cell. Then overwrite with those bits obtained from Complete Layer 3 Information. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001576 msc_a->via_cell = (struct osmo_cell_global_id){
1577 .lai.plmn = msc_a_net(msc_a)->plmn,
1578 };
1579 gsm0808_cell_id_to_cgi(&msc_a->via_cell, msg->compl_l3.cell_id);
Neels Hofmeyrede90832022-01-13 18:13:15 +01001580
Neels Hofmeyre276ae92022-01-13 21:38:35 +01001581 /* If a codec list was sent along in the RAN_MSG_COMPL_L3, remember it for any upcoming codec
1582 * resolution. */
1583 if (msg->compl_l3.codec_list_bss_supported) {
1584 msc_a->cc.compl_l3_codec_list_bss_supported = *msg->compl_l3.codec_list_bss_supported;
1585 if (log_check_level(msc_a->c.ran->log_subsys, LOGL_DEBUG)) {
1586 struct sdp_audio_codecs ac = {};
1587 sdp_audio_codecs_from_speech_codec_list(&ac, &msc_a->cc.compl_l3_codec_list_bss_supported);
1588 LOG_MSC_A(msc_a, LOGL_DEBUG, "Complete Layer 3: Codec List (BSS Supported): %s\n",
1589 sdp_audio_codecs_to_str(&ac));
1590 }
1591 }
1592
Neels Hofmeyrede90832022-01-13 18:13:15 +01001593 /* Submit the Complete Layer 3 Information DTAP */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001594 rc = msc_a_up_l3(msc_a, msg->compl_l3.msg);
1595 if (!rc) {
1596 struct ran_conn *conn = msub_ran_conn(msc_a->c.msub);
1597 if (conn)
1598 ran_peer_cells_seen_add(conn->ran_peer, msg->compl_l3.cell_id);
1599 }
1600 break;
1601
1602 case RAN_MSG_DTAP:
1603 rc = msc_a_up_l3(msc_a, msg->dtap);
1604 break;
1605
1606 case RAN_MSG_CLEAR_REQUEST:
1607 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
1608 break;
1609
1610 case RAN_MSG_CLEAR_COMPLETE:
1611 switch (msc_a->c.fi->state) {
1612 case MSC_A_ST_RELEASING:
1613 msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
1614 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
1615 break;
1616 case MSC_A_ST_RELEASED:
1617 break;
1618 default:
1619 LOG_MSC_A(msc_a, LOGL_ERROR, "Received Clear Complete event, but did not send Clear Command\n");
1620 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
1621 break;
1622 }
1623 rc = 0;
1624 break;
1625
1626 case RAN_MSG_CLASSMARK_UPDATE:
1627 msc_a_up_classmark_update(msc_a, msg->classmark_update.classmark, NULL);
1628 rc = 0;
1629 break;
1630
1631 case RAN_MSG_CIPHER_MODE_COMPLETE:
1632 /* Remember what Ciphering was negotiated (e.g. for Handover) */
1633 if (msg->cipher_mode_complete.alg_id) {
1634 msc_a->geran_encr.alg_id = msg->cipher_mode_complete.alg_id;
1635 LOG_MSC_A(msc_a, LOGL_DEBUG, "Cipher Mode Complete: chosen encryption algorithm: A5/%u\n",
1636 msc_a->geran_encr.alg_id - 1);
Alexander Couzens2aaff752021-10-19 17:09:11 +02001637 }
1638
1639 if (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU) {
1640 int16_t utran_encryption;
1641
1642 /* utran: ensure chosen ciphering mode is allowed
1643 * If the IE is missing (utran_encryption == -1), parse it as no encryption */
1644 utran_encryption = msg->cipher_mode_complete.utran_encryption;
1645 if (utran_encryption == -1)
1646 utran_encryption = 0;
1647 if ((net->uea_encryption_mask & (1 << utran_encryption)) == 0) {
1648 /* cipher disallowed */
1649 LOG_MSC_A(msc_a, LOGL_ERROR, "Cipher Mode Complete: RNC chosen forbidden ciphering UEA%d\n",
1650 msg->cipher_mode_complete.utran_encryption);
1651 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT);
1652 rc = 0;
1653 break;
1654 }
1655 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001656 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_COMPL);
1657 rc = 0;
Neels Hofmeyre9a39112019-08-29 00:10:49 +02001658
1659 /* Evaluate enclosed L3 message, typically Identity Response (IMEISV) */
1660 if (msg->cipher_mode_complete.l3_msg) {
1661 unsigned char *data = (unsigned char*)(msg->cipher_mode_complete.l3_msg->val);
1662 uint16_t len = msg->cipher_mode_complete.l3_msg->len;
1663 struct msgb *dtap = msgb_alloc(len, "DTAP from Cipher Mode Complete");
1664 unsigned char *pos = msgb_put(dtap, len);
1665 memcpy(pos, data, len);
1666 dtap->l3h = pos;
1667 rc = msc_a_up_l3(msc_a, dtap);
1668 msgb_free(dtap);
1669 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001670 break;
1671
1672 case RAN_MSG_CIPHER_MODE_REJECT:
1673 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT);
1674 rc = 0;
1675 break;
1676
1677 case RAN_MSG_ASSIGNMENT_COMPLETE:
1678 msc_a_up_call_assignment_complete(msc_a, msg);
1679 rc = 0;
1680 break;
1681
1682 case RAN_MSG_ASSIGNMENT_FAILURE:
1683 msc_a_up_call_assignment_failure(msc_a, msg);
1684 rc = 0;
1685 break;
1686
1687 case RAN_MSG_SAPI_N_REJECT:
1688 msc_a_up_sapi_n_reject(msc_a, msg);
1689 rc = 0;
1690 break;
1691
1692 case RAN_MSG_HANDOVER_PERFORMED:
1693 /* The BSS lets us know that a handover happened within the BSS, which doesn't concern us. */
1694 LOG_MSC_A(msc_a, LOGL_ERROR, "'Handover Performed' handling not implemented\n");
1695 break;
1696
1697 case RAN_MSG_HANDOVER_REQUIRED:
1698 /* The BSS lets us know that it wants to handover to a different cell */
1699 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_HANDOVER_REQUIRED, (void*)&msg->handover_required);
1700 break;
1701
1702 case RAN_MSG_HANDOVER_FAILURE:
1703 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1704 break;
1705
Keith Whytea1a70be2021-05-16 02:59:52 +02001706 case RAN_MSG_LCLS_STATUS:
1707 /* The BSS sends us LCLS_STATUS. We do nothing for now, but it is not an error. */
1708 LOG_MSC_A(msc_a, LOGL_DEBUG, "LCLS_STATUS (%s) received from MSC-I\n",
1709 gsm0808_lcls_status_name(msg->lcls_status.status));
1710 rc = 0;
1711 break;
1712
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001713 default:
1714 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-I not implemented: %s\n", ran_msg_type_name(msg->msg_type));
1715 rc = -ENOTSUP;
1716 break;
1717 }
1718 return rc;
1719}
1720
1721static int msc_a_ran_dec_from_msc_t(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1722{
1723 struct msc_t *msc_t = msc_a_msc_t(msc_a);
1724 int rc = -99;
1725
1726 if (!msc_t) {
1727 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx message from MSC-T role, but I have no active MSC-T role.\n");
1728 return -EINVAL;
1729 }
1730
1731 OSMO_ASSERT(d->ran_dec);
1732
1733 switch (d->ran_dec->msg_type) {
1734
1735 case RAN_MSG_CLEAR_REQUEST:
1736 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_MO_CLOSE, NULL);
1737 break;
1738
1739 case RAN_MSG_CLEAR_COMPLETE:
1740 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_CLEAR_COMPLETE, NULL);
1741 break;
1742
1743 case RAN_MSG_CLASSMARK_UPDATE:
1744 msc_a_up_classmark_update(msc_a, d->ran_dec->classmark_update.classmark, &msc_t->classmark);
1745 rc = 0;
1746 break;
1747
1748 case RAN_MSG_HANDOVER_REQUEST_ACK:
1749 /* new BSS accepts Handover */
1750 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_REQUEST_ACK);
1751 break;
1752
1753 case RAN_MSG_HANDOVER_DETECT:
1754 /* new BSS signals the MS is DETECTed on the new lchan */
1755 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_DETECT);
1756 break;
1757
1758 case RAN_MSG_HANDOVER_COMPLETE:
1759 /* new BSS signals the MS has fully moved to the new lchan */
1760 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_COMPLETE);
1761 break;
1762
1763 case RAN_MSG_HANDOVER_FAILURE:
1764 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1765 break;
1766
1767 default:
1768 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-T not implemented: %s\n",
1769 ran_msg_type_name(d->ran_dec->msg_type));
1770 rc = -ENOTSUP;
1771 break;
1772 }
1773 return rc;
1774}
1775
1776int msc_a_ran_decode_cb(struct osmo_fsm_inst *msc_a_fi, void *data, const struct ran_msg *msg)
1777{
1778 struct msc_a *msc_a = msc_a_fi_priv(msc_a_fi);
1779 struct msc_a_ran_dec_data *d = data;
1780 int rc = -99;
1781
1782 d->ran_dec = msg;
1783
1784 switch (d->from_role) {
1785 case MSC_ROLE_I:
1786 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode: %s\n", msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1787 rc = msc_a_ran_dec_from_msc_i(msc_a, d);
1788 break;
1789
1790 case MSC_ROLE_T:
1791 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode from MSC-T: %s\n",
1792 msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1793 rc = msc_a_ran_dec_from_msc_t(msc_a, d);
1794 break;
1795
1796 default:
1797 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from invalid role %s: %s\n", msc_role_name(d->from_role),
1798 ran_msg_type_name(msg->msg_type));
1799 return -ENOTSUP;
1800 }
1801
1802 if (rc)
1803 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),
1804 msc_role_name(d->from_role));
1805 return rc;
1806}
1807
1808/* Your typical DTAP via FORWARD_ACCESS_SIGNALLING_REQUEST */
1809int _msc_a_ran_down(struct msc_a *msc_a, enum msc_role to_role, const struct ran_msg *ran_msg,
1810 const char *file, int line)
1811{
1812 return _msc_a_msg_down(msc_a, to_role, msub_role_to_role_event(msc_a->c.msub, MSC_ROLE_A, to_role),
1813 ran_msg, file, line);
1814}
1815
1816/* To transmit more complex events than just FORWARD_ACCESS_SIGNALLING_REQUEST, e.g. an
1817 * MSC_T_EV_FROM_A_PREPARE_HANDOVER_REQUEST */
1818int _msc_a_msg_down(struct msc_a *msc_a, enum msc_role to_role, uint32_t to_role_event,
1819 const struct ran_msg *ran_msg,
1820 const char *file, int line)
1821{
1822 struct an_apdu an_apdu = {
1823 .an_proto = msc_a->c.ran->an_proto,
1824 .msg = msc_role_ran_encode(msc_a->c.fi, ran_msg),
1825 };
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001826 if (!an_apdu.msg)
1827 return -EIO;
Vadim Yanitskiyc44342b2021-12-07 18:32:35 +03001828 return _msub_role_dispatch(msc_a->c.msub, to_role, to_role_event, &an_apdu, file, line);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001829}
1830
1831int msc_a_tx_dtap_to_i(struct msc_a *msc_a, struct msgb *dtap)
1832{
1833 struct ran_msg ran_msg;
Neels Hofmeyrc192c0b2019-10-07 21:41:18 +02001834 struct gsm48_hdr *gh = msgb_l3(dtap) ? : dtap->data;
1835 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001836
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001837 if (!msc_a) {
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001838 LOGP(DMSC, LOGL_ERROR, "Attempt to send DTAP to NULL MSC-A, dropping message: %s %s\n",
1839 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1840 msgb_free(dtap);
1841 return -EIO;
1842 }
1843
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001844 if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) {
1845 /* The SGs connection to the MME always is at the MSC-A. */
1846 return sgs_iface_tx_dtap_ud(msc_a, dtap);
1847 }
1848
Neels Hofmeyrc192c0b2019-10-07 21:41:18 +02001849 LOG_MSC_A(msc_a, LOGL_DEBUG, "Sending DTAP: %s %s\n",
1850 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1851
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001852 ran_msg = (struct ran_msg){
1853 .msg_type = RAN_MSG_DTAP,
1854 .dtap = dtap,
1855 };
1856 return msc_a_ran_down(msc_a, MSC_ROLE_I, &ran_msg);
1857}
1858
1859struct msc_a *msc_a_for_vsub(const struct vlr_subscr *vsub, bool valid_conn_only)
1860{
1861 struct msc_a *msc_a = msub_msc_a(msub_for_vsub(vsub));
1862 if (valid_conn_only && !msc_a_is_accepted(msc_a))
1863 return NULL;
1864 return msc_a;
1865}
1866
1867int msc_tx_common_id(struct msc_a *msc_a, enum msc_role to_role)
1868{
1869 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Vadim Yanitskiy435f67f2021-06-06 15:47:49 +02001870 if (vsub == NULL)
1871 return -ENODEV;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001872 struct ran_msg msg = {
1873 .msg_type = RAN_MSG_COMMON_ID,
1874 .common_id = {
1875 .imsi = vsub->imsi,
Pau Espin Pedrol67106702021-04-27 18:20:15 +02001876 .last_eutran_plmn_present = vsub->sgs.last_eutran_plmn_present,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001877 },
1878 };
Pau Espin Pedrol67106702021-04-27 18:20:15 +02001879 if (vsub->sgs.last_eutran_plmn_present) {
1880 memcpy(&msg.common_id.last_eutran_plmn, &vsub->sgs.last_eutran_plmn,
1881 sizeof(vsub->sgs.last_eutran_plmn));
1882 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001883
1884 return msc_a_ran_down(msc_a, to_role, &msg);
1885}
1886
1887static int msc_a_start_assignment(struct msc_a *msc_a, struct gsm_trans *cc_trans)
1888{
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001889 struct call_leg *cl;
1890 bool cn_rtp_available;
1891 bool ran_rtp_available;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001892
1893 OSMO_ASSERT(!msc_a->cc.active_trans);
1894 msc_a->cc.active_trans = cc_trans;
1895
Neels Hofmeyr62bfa372022-10-31 18:51:07 +01001896 cc_trans->cc.codecs.assignment = (struct sdp_audio_codec){};
1897
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001898 OSMO_ASSERT(cc_trans && cc_trans->type == TRANS_CC);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001899 cl = msc_a_ensure_call_leg(msc_a, cc_trans);
1900 if (!cl)
1901 return -EINVAL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001902
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001903 /* See if we can set a preliminary codec. If not, pass none for the time being. */
Oliver Smithceca8e62023-05-24 11:15:52 +02001904 trans_cc_filter_run(cc_trans);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001905
1906 cn_rtp_available = call_leg_local_ip(cl, RTP_TO_CN);
1907 ran_rtp_available = call_leg_local_ip(cl, RTP_TO_RAN);
1908
1909 /* Set up RTP ports for both RAN and CN side. Even though we ask for both at the same time, the
1910 * osmo_mgcpc_ep_fsm automagically waits for the first CRCX to complete before firing the second CRCX. The one
1911 * issued first here will also be the first CRCX sent to the MGW. Usually both still need to be set up. */
1912 if (!cn_rtp_available)
Andreas Eversberg712b28e2023-06-21 11:17:26 +02001913 call_leg_ensure_ci(cl, RTP_TO_CN, cc_trans->call_id, cc_trans,
Oliver Smithc63c3a02023-05-24 10:48:07 +02001914 &cc_trans->cc.local.audio_codecs, NULL);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001915 if (!ran_rtp_available) {
1916 struct sdp_audio_codecs *codecs;
1917 if (msc_a->c.ran->force_mgw_codecs_to_ran.count)
1918 codecs = &msc_a->c.ran->force_mgw_codecs_to_ran;
1919 else
Oliver Smithc63c3a02023-05-24 10:48:07 +02001920 codecs = &cc_trans->cc.local.audio_codecs;
Andreas Eversberg712b28e2023-06-21 11:17:26 +02001921 return call_leg_ensure_ci(cl, RTP_TO_RAN, cc_trans->call_id, cc_trans, codecs, NULL);
Neels Hofmeyrbd5f8e92022-01-13 23:18:02 +01001922 }
1923
1924 /* Should these already be set up, immediately continue by retriggering the events signalling that the RTP
1925 * ports are available. The ordering is: first CN, then RAN. */
1926 if (cn_rtp_available && ran_rtp_available)
1927 return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_RAN]);
1928 else if (cn_rtp_available)
1929 return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_CN]);
1930 /* Otherwise wait for MGCP response and continue from there. */
1931 return 0;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001932}
1933
1934int msc_a_try_call_assignment(struct gsm_trans *cc_trans)
1935{
1936 struct msc_a *msc_a = cc_trans->msc_a;
1937 OSMO_ASSERT(cc_trans->type == TRANS_CC);
1938
1939 if (msc_a->cc.active_trans == cc_trans) {
Neels Hofmeyrb4ef5e72019-08-30 01:11:12 +02001940 LOG_MSC_A(msc_a, LOGL_DEBUG, "Assignment for this trans already started earlier\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001941 return 0;
1942 }
1943
1944 if (msc_a->cc.active_trans) {
1945 LOG_MSC_A(msc_a, LOGL_INFO, "Another call is already ongoing, not assigning yet\n");
1946 return 0;
1947 }
1948
1949 LOG_MSC_A(msc_a, LOGL_DEBUG, "Starting call assignment\n");
1950 return msc_a_start_assignment(msc_a, cc_trans);
1951}
1952
Neels Hofmeyr5d53c602022-04-24 23:37:07 +02001953/* Map CM Service type to use token.
1954 * Given a CM Service type, return a matching token intended for osmo_use_count.
1955 * For unknown service type, return NULL.
1956 */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001957const char *msc_a_cm_service_type_to_use(enum osmo_cm_service_type cm_service_type)
1958{
1959 switch (cm_service_type) {
1960 case GSM48_CMSERV_MO_CALL_PACKET:
1961 case GSM48_CMSERV_EMERGENCY:
1962 return MSC_A_USE_CM_SERVICE_CC;
1963
1964 case GSM48_CMSERV_SMS:
1965 return MSC_A_USE_CM_SERVICE_SMS;
1966
1967 case GSM48_CMSERV_SUP_SERV:
1968 return MSC_A_USE_CM_SERVICE_SS;
1969
Andreas Eversberg456c6f72023-04-23 11:54:16 +02001970 case GSM48_CMSERV_VGCS:
1971 return MSC_A_USE_CM_SERVICE_GCC;
1972
1973 case GSM48_CMSERV_VBS:
1974 return MSC_A_USE_CM_SERVICE_BCC;
1975
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001976 default:
1977 return NULL;
1978 }
1979}
1980
1981void msc_a_release_cn(struct msc_a *msc_a)
1982{
1983 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CN_CLOSE, NULL);
1984}
1985
1986void msc_a_release_mo(struct msc_a *msc_a, enum gsm48_gsm_cause gsm_cause)
1987{
1988 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
1989}