blob: 7ce70c93e167e2197c2ea682eb253fa52527f096 [file] [log] [blame]
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001/* Code to manage a subscriber's MSC-A role */
2/*
3 * (C) 2019 by sysmocom - s.m.f.c. GmbH <info@sysmocom.de>
4 * 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>
39#include <osmocom/msc/ran_peer.h>
40#include <osmocom/msc/ran_msg_a.h>
41#include <osmocom/msc/ran_msg_iu.h>
42#include <osmocom/msc/sgs_iface.h>
43#include <osmocom/msc/gsm_04_08.h>
44#include <osmocom/msc/gsm_09_11.h>
45#include <osmocom/msc/gsm_04_14.h>
46#include <osmocom/msc/call_leg.h>
47#include <osmocom/msc/rtp_stream.h>
48#include <osmocom/msc/msc_ho.h>
49
50#define MSC_A_USE_WAIT_CLEAR_COMPLETE "wait-Clear-Complete"
51
52static struct osmo_fsm msc_a_fsm;
53
54static const struct osmo_tdef_state_timeout msc_a_fsm_timeouts[32] = {
55 [MSC_A_ST_VALIDATE_L3] = { .T = -1 },
56 [MSC_A_ST_AUTH_CIPH] = { .keep_timer = true },
57 [MSC_A_ST_WAIT_CLASSMARK_UPDATE] = { .keep_timer = true },
58 [MSC_A_ST_AUTHENTICATED] = { .keep_timer = true },
59 [MSC_A_ST_RELEASING] = { .T = -2 },
60 [MSC_A_ST_RELEASED] = { .T = -2 },
61};
62
63/* Transition to a state, using the T timer defined in msc_a_fsm_timeouts.
64 * The actual timeout value is in turn obtained from network->T_defs.
65 * Assumes local variable fi exists. */
Neels Hofmeyr01653252019-09-03 02:06:22 +020066#define msc_a_state_chg_always(msc_a, state) \
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010067 osmo_tdef_fsm_inst_state_chg((msc_a)->c.fi, state, msc_a_fsm_timeouts, (msc_a)->c.ran->tdefs, 5)
68
Neels Hofmeyr01653252019-09-03 02:06:22 +020069/* Same as msc_a_state_chg_always() but ignore if the msc_a already is in the target state. */
70#define msc_a_state_chg(msc_a, STATE) do { \
71 if ((msc_a)->c.fi->state != STATE) \
72 msc_a_state_chg_always(msc_a, STATE); \
73 } while(0)
74
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010075struct gsm_network *msc_a_net(const struct msc_a *msc_a)
76{
77 return msub_net(msc_a->c.msub);
78}
79
80struct vlr_subscr *msc_a_vsub(const struct msc_a *msc_a)
81{
Neels Hofmeyr911e5972019-05-09 13:28:26 +020082 if (!msc_a)
83 return NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010084 return msub_vsub(msc_a->c.msub);
85}
86
87struct msc_i *msc_a_msc_i(const struct msc_a *msc_a)
88{
Neels Hofmeyr911e5972019-05-09 13:28:26 +020089 if (!msc_a)
90 return NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010091 return msub_msc_i(msc_a->c.msub);
92}
93
94struct msc_t *msc_a_msc_t(const struct msc_a *msc_a)
95{
Neels Hofmeyr911e5972019-05-09 13:28:26 +020096 if (!msc_a)
97 return NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +010098 return msub_msc_t(msc_a->c.msub);
99}
100
101struct msc_a *msc_a_fi_priv(struct osmo_fsm_inst *fi)
102{
103 OSMO_ASSERT(fi);
104 OSMO_ASSERT(fi->fsm == &msc_a_fsm);
105 OSMO_ASSERT(fi->priv);
106 return fi->priv;
107}
108
Neels Hofmeyr0d2aab72022-10-10 23:34:48 +0200109bool msc_a_try_ciphering(const struct msc_a *msc_a)
Alexander Couzens2c5e4612021-11-05 02:00:17 +0100110{
111 struct gsm_network *net = msc_a_net(msc_a);
112 bool is_utran = (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU);
113 if (is_utran)
114 return net->uea_encryption_mask > (1 << OSMO_UTRAN_UEA0);
115 else
116 return net->a5_encryption_mask > 0x1;
117}
118
Neels Hofmeyr7bb31ca2022-10-10 23:35:47 +0200119bool msc_a_require_ciphering(const struct msc_a *msc_a)
120{
121 struct gsm_network *net = msc_a_net(msc_a);
122 bool is_utran = (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU);
123 if (is_utran)
124 return net->uea_encryption_mask
125 && ((net->uea_encryption_mask & (1 << OSMO_UTRAN_UEA0)) == 0);
126 else
127 return net->a5_encryption_mask
128 && ((net->a5_encryption_mask & 0x1) == 0);
129}
130
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100131static void update_counters(struct osmo_fsm_inst *fi, bool conn_accepted)
132{
133 struct msc_a *msc_a = fi->priv;
134 struct gsm_network *net = msc_a_net(msc_a);
135 switch (msc_a->complete_layer3_type) {
136 case COMPLETE_LAYER3_LU:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200137 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 +0100138 break;
139 case COMPLETE_LAYER3_CM_SERVICE_REQ:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200140 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 +0100141 break;
142 case COMPLETE_LAYER3_PAGING_RESP:
Pau Espin Pedrol2e21a682021-06-04 16:45:44 +0200143 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 +0100144 break;
Neels Hofmeyrae98b972021-07-27 03:46:49 +0200145 case COMPLETE_LAYER3_CM_RE_ESTABLISH_REQ:
146 rate_ctr_inc(rate_ctr_group_get_ctr(net->msc_ctrs,
147 conn_accepted ? MSC_CTR_CM_RE_ESTABLISH_REQ_ACCEPTED
148 : MSC_CTR_CM_RE_ESTABLISH_REQ_REJECTED));
149 break;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100150 default:
151 break;
152 }
153}
154
155static void evaluate_acceptance_outcome(struct osmo_fsm_inst *fi, bool conn_accepted)
156{
157 struct msc_a *msc_a = fi->priv;
158 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
159
160 update_counters(fi, conn_accepted);
161
Neels Hofmeyr83cf10c2020-06-24 14:23:26 +0200162 if (conn_accepted) {
163 /* Record the Cell ID seen in Complete Layer 3 Information in the VLR, so that it also shows in vty
164 * 'show' output. */
165 vsub->cgi = msc_a->via_cell;
166 }
167
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100168 /* Trigger transactions that we paged for */
169 if (msc_a->complete_layer3_type == COMPLETE_LAYER3_PAGING_RESP) {
170 if (conn_accepted)
171 paging_response(msc_a);
172 else
173 paging_expired(vsub);
174 }
175
176 if (conn_accepted)
177 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_ATTACHED, msc_a_vsub(msc_a));
178
179 if (msc_a->complete_layer3_type == COMPLETE_LAYER3_LU)
180 msc_a_put(msc_a, MSC_A_USE_LOCATION_UPDATING);
Neels Hofmeyrae98b972021-07-27 03:46:49 +0200181
182 if (msc_a->complete_layer3_type == COMPLETE_LAYER3_CM_RE_ESTABLISH_REQ) {
183 /* Trigger new Assignment to recommence the voice call. A little dance here because normally we verify
184 * that no CC trans is already active. */
185 struct gsm_trans *cc_trans = msc_a->cc.active_trans;
186 msc_a->cc.active_trans = NULL;
187 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_TRANSACTION_ACCEPTED, cc_trans);
188 msc_a_try_call_assignment(cc_trans);
189 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100190}
191
192bool msc_a_is_accepted(const struct msc_a *msc_a)
193{
194 if (!msc_a || !msc_a->c.fi)
195 return false;
196 return msc_a->c.fi->state == MSC_A_ST_AUTHENTICATED
197 || msc_a->c.fi->state == MSC_A_ST_COMMUNICATING;
198}
199
200bool msc_a_in_release(struct msc_a *msc_a)
201{
202 if (!msc_a)
203 return true;
204 if (msc_a->c.fi->state == MSC_A_ST_RELEASING)
205 return true;
206 if (msc_a->c.fi->state == MSC_A_ST_RELEASED)
207 return true;
208 return false;
209}
210
211static int msc_a_ran_dec(struct msc_a *msc_a, const struct an_apdu *an_apdu, enum msc_role from_role)
212{
213 int rc;
214 struct msc_a_ran_dec_data d = {
215 .from_role = from_role,
216 .an_apdu = an_apdu,
217 };
218 msc_a_get(msc_a, __func__);
219 rc = msc_role_ran_decode(msc_a->c.fi, an_apdu, msc_a_ran_decode_cb, &d);
220 msc_a_put(msc_a, __func__);
221 return rc;
222};
223
224static void msc_a_fsm_validate_l3(struct osmo_fsm_inst *fi, uint32_t event, void *data)
225{
226 struct msc_a *msc_a = fi->priv;
227 const struct an_apdu *an_apdu;
228
229 switch (event) {
230 case MSC_A_EV_FROM_I_COMPLETE_LAYER_3:
231 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
232 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
233 an_apdu = data;
234 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_I);
235 return;
236
237 case MSC_A_EV_COMPLETE_LAYER_3_OK:
238 msc_a_state_chg(msc_a, MSC_A_ST_AUTH_CIPH);
239 return;
240
241 case MSC_A_EV_MO_CLOSE:
242 case MSC_A_EV_CN_CLOSE:
243 evaluate_acceptance_outcome(fi, false);
244 /* fall through */
245 case MSC_A_EV_UNUSED:
246 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
247 return;
248
249 default:
250 OSMO_ASSERT(false);
251 }
252}
253
254/* Figure out whether to first send a Classmark Request to the MS to figure out algorithm support. */
255static bool msc_a_need_classmark_for_ciphering(struct msc_a *msc_a)
256{
257 struct gsm_network *net = msc_a_net(msc_a);
258 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
259 int i = 0;
260 bool request_classmark = false;
261
262 /* Only on GERAN-A do we ever need Classmark Information for Ciphering. */
263 if (msc_a->c.ran->type != OSMO_RAT_GERAN_A)
264 return false;
265
266 for (i = 0; i < 8; i++) {
267 int supported;
268
269 /* A5/n permitted by osmo-msc.cfg? */
270 if (!(net->a5_encryption_mask & (1 << i)))
271 continue;
272
273 /* A5/n supported by MS? */
274 supported = osmo_gsm48_classmark_supports_a5(&vsub->classmark, i);
275 if (supported < 0) {
276 LOG_MSC_A(msc_a, LOGL_DEBUG, "For A5/%d, we still need Classmark %d\n", i, -supported);
277 request_classmark = true;
278 }
279 }
280
281 return request_classmark;
282}
283
284static int msc_a_ran_enc_ciphering(struct msc_a *msc_a, bool umts_aka, bool retrieve_imeisv);
285
286/* VLR callback for ops.set_ciph_mode() */
287int msc_a_vlr_set_cipher_mode(void *_msc_a, bool umts_aka, bool retrieve_imeisv)
288{
289 struct msc_a *msc_a = _msc_a;
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700290 struct vlr_subscr *vsub;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100291
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700292 if (!msc_a) {
293 LOGP(DMSC, LOGL_ERROR, "Insufficient info to start ciphering: "
294 "MSC-A role is NULL?!?\n");
295 return -EINVAL;
296 }
297
298 vsub = msc_a_vsub(msc_a);
299 if (!vsub || !vsub->last_tuple) {
300 LOG_MSC_A(msc_a, LOGL_ERROR, "Insufficient info to start ciphering: "
301 "vlr_subscr is NULL?!?\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100302 return -EINVAL;
303 }
304
305 if (msc_a_need_classmark_for_ciphering(msc_a)) {
306 int rc;
307 struct ran_msg msg = {
308 .msg_type = RAN_MSG_CLASSMARK_REQUEST,
309 };
310 rc = msc_a_ran_down(msc_a, MSC_ROLE_I, &msg);
311 if (rc) {
312 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot send Classmark Request\n");
313 return -EIO;
314 }
315
316 msc_a->state_before_classmark_update = msc_a->c.fi->state;
317 msc_a->action_on_classmark_update = (struct msc_a_action_on_classmark_update){
318 .type = MSC_A_CLASSMARK_UPDATE_THEN_CIPHERING,
319 .ciphering = {
320 .umts_aka = umts_aka,
321 .retrieve_imeisv = retrieve_imeisv,
322 },
323 };
324 msc_a_state_chg(msc_a, MSC_A_ST_WAIT_CLASSMARK_UPDATE);
325 return 0;
326 }
327
328 return msc_a_ran_enc_ciphering(msc_a, umts_aka, retrieve_imeisv);
329}
330
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +0200331static uint8_t filter_a5(uint8_t a5_mask, bool umts_aka)
332{
333 /* With GSM AKA: allow A5/0, 1, 3 = 0b00001011 = 0xb.
334 * UMTS aka: allow A5/0, 1, 3, 4 = 0b00011011 = 0x1b.
335 */
336 return a5_mask & (umts_aka ? 0x1b : 0x0b);
337}
338
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100339static int msc_a_ran_enc_ciphering(struct msc_a *msc_a, bool umts_aka, bool retrieve_imeisv)
340{
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700341 struct gsm_network *net;
342 struct vlr_subscr *vsub;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100343 struct ran_msg msg;
344
Vadim Yanitskiy4dd477f2019-05-11 03:00:30 +0700345 if (!msc_a) {
346 LOGP(DMSC, LOGL_ERROR, "Insufficient info to start ciphering: "
347 "MSC-A role is NULL?!?\n");
348 return -EINVAL;
349 }
350
351 net = msc_a_net(msc_a);
352 vsub = msc_a_vsub(msc_a);
353
354 if (!net || !vsub || !vsub->last_tuple) {
355 LOG_MSC_A(msc_a, LOGL_ERROR, "Insufficient info to start ciphering: "
356 "gsm_network and/or vlr_subscr is NULL?!?\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100357 return -EINVAL;
358 }
359
360 msg = (struct ran_msg){
361 .msg_type = RAN_MSG_CIPHER_MODE_COMMAND,
362 .cipher_mode_command = {
363 .vec = vsub->last_tuple ? &vsub->last_tuple->vec : NULL,
364 .classmark = &vsub->classmark,
365 .geran = {
366 .umts_aka = umts_aka,
367 .retrieve_imeisv = retrieve_imeisv,
Neels Hofmeyr6ce2edc2021-06-09 22:26:11 +0200368 .a5_encryption_mask = filter_a5(net->a5_encryption_mask, umts_aka),
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100369
370 /* for ran_a.c to store the GERAN key that is actually used */
371 .chosen_key = &msc_a->geran_encr,
372 },
Harald Welte274b70f2021-02-06 16:47:39 +0100373 .utran = {
Harald Welte505a94a2021-02-06 17:12:20 +0100374 .uea_encryption_mask = net->uea_encryption_mask,
375 },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100376 },
377 };
378
379 if (msc_a_ran_down(msc_a, MSC_ROLE_I, &msg)) {
380 LOG_MSC_A(msc_a, LOGL_ERROR, "Sending Cipher Mode Command failed\n");
381 /* Returning error to the VLR ops.set_ciph_mode() will cancel the attach. Other callers need to take
382 * care of the return value. */
383 return -EINVAL;
384 }
385
386 if (msc_a->geran_encr.key_len)
Neels Hofmeyr73d093a2021-06-23 23:54:43 +0200387 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN encoding chose ciphering: A5/%d kc %s kc128 %s\n",
388 msc_a->geran_encr.alg_id - 1,
389 osmo_hexdump_nospc_c(OTC_SELECT, msc_a->geran_encr.key, msc_a->geran_encr.key_len),
390 msc_a->geran_encr.kc128_present ?
391 osmo_hexdump_nospc_c(OTC_SELECT, msc_a->geran_encr.kc128, sizeof(msc_a->geran_encr.kc128))
392 : "-");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100393 return 0;
394}
395
396static void msc_a_fsm_auth_ciph(struct osmo_fsm_inst *fi, uint32_t event, void *data)
397{
398 struct msc_a *msc_a = fi->priv;
399
400 /* If accepted, transition the state, all other cases mean failure. */
401 switch (event) {
402 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
403 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
404 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
405 return;
406
407 case MSC_A_EV_AUTHENTICATED:
408 msc_a_state_chg(msc_a, MSC_A_ST_AUTHENTICATED);
409 return;
410
411 case MSC_A_EV_UNUSED:
412 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
413 return;
414
415 case MSC_A_EV_MO_CLOSE:
416 case MSC_A_EV_CN_CLOSE:
417 evaluate_acceptance_outcome(fi, false);
418 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
419 return;
420
421
422 default:
423 OSMO_ASSERT(false);
424 }
425}
426
427static void msc_a_fsm_wait_classmark_update(struct osmo_fsm_inst *fi, uint32_t event, void *data)
428{
429 struct msc_a *msc_a = fi->priv;
430
431 switch (event) {
432 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
433 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
434 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
435 return;
436
437 case MSC_A_EV_CLASSMARK_UPDATE:
438 switch (msc_a->action_on_classmark_update.type) {
439 case MSC_A_CLASSMARK_UPDATE_THEN_CIPHERING:
440 msc_a_state_chg(msc_a, MSC_A_ST_AUTH_CIPH);
441 if (msc_a_ran_enc_ciphering(msc_a,
442 msc_a->action_on_classmark_update.ciphering.umts_aka,
443 msc_a->action_on_classmark_update.ciphering.retrieve_imeisv)) {
444 LOG_MSC_A(msc_a, LOGL_ERROR,
445 "After Classmark Update, still failed to send Cipher Mode Command\n");
446 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
447 }
448 return;
449
450 default:
451 LOG_MSC_A(msc_a, LOGL_ERROR, "Internal error: After Classmark Update, don't know what to do\n");
452 msc_a_state_chg(msc_a, msc_a->state_before_classmark_update);
453 return;
454 }
455
456 case MSC_A_EV_UNUSED:
457 /* Seems something detached / aborted in the middle of auth+ciph. */
458 evaluate_acceptance_outcome(fi, false);
459 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
460 return;
461
462 case MSC_A_EV_MO_CLOSE:
463 case MSC_A_EV_CN_CLOSE:
464 evaluate_acceptance_outcome(fi, false);
465 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
466 return;
467
468 default:
469 OSMO_ASSERT(false);
470 }
471}
472
473static bool msc_a_fsm_has_active_transactions(struct osmo_fsm_inst *fi)
474{
475 struct msc_a *msc_a = fi->priv;
476 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
477 struct gsm_trans *trans;
478
479 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_SILENT_CALL)) {
480 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: silent call still active\n", __func__);
481 return true;
482 }
483
484 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_CC)) {
485 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO CC request after a CM Service Request\n",
486 __func__);
487 return true;
488 }
489 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_SMS)) {
490 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO SMS after a CM Service Request\n",
491 __func__);
492 return true;
493 }
494 if (osmo_use_count_by(&msc_a->use_count, MSC_A_USE_CM_SERVICE_SS)) {
495 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still awaiting MO SS after a CM Service Request\n",
496 __func__);
497 return true;
498 }
499
500 if (vsub && !llist_empty(&vsub->cs.requests)) {
501 struct paging_request *pr;
502 llist_for_each_entry(pr, &vsub->cs.requests, entry) {
503 LOG_MSC_A(msc_a, LOGL_DEBUG, "%s: still active: %s\n", __func__, pr->label);
504 }
505 return true;
506 }
507
508 if ((trans = trans_has_conn(msc_a))) {
509 LOG_MSC_A(msc_a, LOGL_DEBUG, "connection still has active transaction: %s\n",
510 trans_type_name(trans->type));
511 return true;
512 }
513
514 return false;
515}
516
517static void msc_a_fsm_authenticated_enter(struct osmo_fsm_inst *fi, uint32_t prev_state)
518{
519 struct msc_a *msc_a = fi->priv;
520 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
521
522 /* Stop Location Update expiry for this subscriber. While the subscriber
523 * has an open connection the LU expiry timer must remain disabled.
524 * Otherwise we would kick the subscriber off the network when the timer
525 * expires e.g. during a long phone call.
526 * The LU expiry timer will restart once the connection is closed. */
527 if (vsub)
528 vsub->expire_lu = VLR_SUBSCRIBER_NO_EXPIRATION;
529
530 evaluate_acceptance_outcome(fi, true);
531}
532
533static void msc_a_fsm_authenticated(struct osmo_fsm_inst *fi, uint32_t event, void *data)
534{
535 struct msc_a *msc_a = fi->priv;
536
537 switch (event) {
538 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
539 case MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST:
540 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
541 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
542 return;
543
544 case MSC_A_EV_COMPLETE_LAYER_3_OK:
545 /* When Authentication is off, we may already be in the Accepted state when the code
546 * evaluates the Compl L3. Simply ignore. This just cosmetically mutes the error log
547 * about the useless event. */
548 return;
549
550 case MSC_A_EV_TRANSACTION_ACCEPTED:
551 msc_a_state_chg(msc_a, MSC_A_ST_COMMUNICATING);
552 return;
553
554 case MSC_A_EV_MO_CLOSE:
555 case MSC_A_EV_CN_CLOSE:
556 case MSC_A_EV_UNUSED:
557 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
558 return;
559
560 default:
561 OSMO_ASSERT(false);
562 }
563}
564
565/* The MGW has given us a local IP address for the RAN side. Ready to start the Assignment of a voice channel. */
566static void msc_a_call_leg_ran_local_addr_available(struct msc_a *msc_a)
567{
568 struct ran_msg msg;
569 struct gsm_trans *cc_trans = msc_a->cc.active_trans;
570 struct gsm0808_channel_type channel_type;
571
Neels Hofmeyr00a476b2019-11-28 02:46:05 +0100572 if (!cc_trans) {
573 LOG_MSC_A(msc_a, LOGL_ERROR, "No CC transaction active\n");
574 call_leg_release(msc_a->cc.call_leg);
575 return;
576 }
577
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100578 /* Once a CI is known, we could also CRCX the CN side of the MGW endpoint, but it makes sense to wait for the
579 * codec to be determined by the Assignment Complete message, first. */
580
581 if (mncc_bearer_cap_to_channel_type(&channel_type, &cc_trans->bearer_cap)) {
582 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot compose Channel Type from bearer capabilities\n");
Neels Hofmeyrf439ff12019-10-05 04:19:36 +0200583 trans_free(cc_trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100584 return;
585 }
586
587 /* The RAN side RTP address is known, so the voice Assignment can commence. */
588 msg = (struct ran_msg){
589 .msg_type = RAN_MSG_ASSIGNMENT_COMMAND,
590 .assignment_command = {
591 .cn_rtp = &msc_a->cc.call_leg->rtp[RTP_TO_RAN]->local,
592 .channel_type = &channel_type,
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200593 .osmux_present = msc_a->cc.call_leg->rtp[RTP_TO_RAN]->use_osmux,
594 .osmux_cid = msc_a->cc.call_leg->rtp[RTP_TO_RAN]->local_osmux_cid,
Philipp Maierf34d9452020-06-05 15:49:35 +0200595 .call_id_present = true,
596 .call_id = cc_trans->callref,
Keith Whytea1a70be2021-05-16 02:59:52 +0200597 .lcls = cc_trans->cc.lcls,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100598 },
599 };
600 if (msc_a_ran_down(msc_a, MSC_ROLE_I, &msg)) {
601 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot send Assignment\n");
Neels Hofmeyrf439ff12019-10-05 04:19:36 +0200602 trans_free(cc_trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100603 return;
604 }
605}
606
607static void msc_a_call_leg_cn_local_addr_available(struct msc_a *msc_a, struct gsm_trans *cc_trans)
608{
609 if (gsm48_tch_rtp_create(cc_trans)) {
610 LOG_MSC_A(msc_a, LOGL_ERROR, "Cannot inform MNCC of RTP address\n");
Neels Hofmeyrf439ff12019-10-05 04:19:36 +0200611 trans_free(cc_trans);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100612 return;
613 }
614}
615
616static struct gsm_trans *find_waiting_call(struct msc_a *msc_a)
617{
618 struct gsm_trans *trans;
619 struct gsm_network *net = msc_a_net(msc_a);
620
621 llist_for_each_entry(trans, &net->trans_list, entry) {
622 if (trans->msc_a != msc_a)
623 continue;
624 if (trans->type != TRANS_CC)
625 continue;
626 if (trans->msc_a->cc.active_trans == trans)
627 continue;
628 return trans;
629 }
630 return NULL;
631}
632
633static void msc_a_cleanup_rtp_streams(struct msc_a *msc_a, uint32_t event, void *data)
634{
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100635 switch (event) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100636
637 case MSC_EV_CALL_LEG_TERM:
638 msc_a->cc.call_leg = NULL;
639 if (msc_a->cc.mncc_forwarding_to_remote_ran)
640 msc_a->cc.mncc_forwarding_to_remote_ran->rtps = NULL;
641
Neels Hofmeyr265a4c72019-05-09 16:20:51 +0200642 if (msc_a->ho.new_cell.mncc_forwarding_to_remote_ran)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100643 msc_a->ho.new_cell.mncc_forwarding_to_remote_ran->rtps = NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100644 return;
645
646 case MSC_MNCC_EV_CALL_ENDED:
647 msc_a->cc.mncc_forwarding_to_remote_ran = NULL;
648 return;
649
650 default:
651 return;
652 }
653}
654
655static void msc_a_fsm_communicating(struct osmo_fsm_inst *fi, uint32_t event, void *data)
656{
657 struct msc_a *msc_a = fi->priv;
658 struct rtp_stream *rtps;
659 struct gsm_trans *waiting_trans;
660 struct an_apdu *an_apdu;
661
662 msc_a_cleanup_rtp_streams(msc_a, event, data);
663
664 switch (event) {
665 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
666 case MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST:
667 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
668 an_apdu = data;
669 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_I);
670 return;
671
672 case MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE:
673 case MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE:
674 case MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST:
675 case MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST:
676 an_apdu = data;
677 msc_a_ran_dec(msc_a, an_apdu, MSC_ROLE_T);
678 return;
679
680 case MSC_A_EV_TRANSACTION_ACCEPTED:
681 /* no-op */
682 return;
683
684 case MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE:
685 rtps = data;
686 if (!rtps) {
687 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid data for %s\n", osmo_fsm_event_name(fi->fsm, event));
688 return;
689 }
Neels Hofmeyr00a476b2019-11-28 02:46:05 +0100690 if (!msc_a->cc.call_leg) {
691 LOG_MSC_A(msc_a, LOGL_ERROR, "No call leg active\n");
692 return;
693 }
Neels Hofmeyrcc918cb2019-11-28 02:16:34 +0100694 if (!osmo_sockaddr_str_is_nonzero(&rtps->local)) {
695 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid RTP address received from MGW: " OSMO_SOCKADDR_STR_FMT "\n",
696 OSMO_SOCKADDR_STR_FMT_ARGS(&rtps->local));
697 call_leg_release(msc_a->cc.call_leg);
698 return;
699 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100700 LOG_MSC_A(msc_a, LOGL_DEBUG,
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +0200701 "MGW endpoint's RTP address available for the CI %s: " OSMO_SOCKADDR_STR_FMT " (osmux=%s:%d)\n",
702 rtp_direction_name(rtps->dir), OSMO_SOCKADDR_STR_FMT_ARGS(&rtps->local),
703 rtps->use_osmux ? "yes" : "no", rtps->local_osmux_cid);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100704 switch (rtps->dir) {
705 case RTP_TO_RAN:
706 msc_a_call_leg_ran_local_addr_available(msc_a);
707 return;
708 case RTP_TO_CN:
709 msc_a_call_leg_cn_local_addr_available(msc_a, rtps->for_trans);
710 return;
711 default:
712 LOG_MSC_A(msc_a, LOGL_ERROR, "Invalid data for %s\n", osmo_fsm_event_name(fi->fsm, event));
713 return;
714 }
715
716 case MSC_EV_CALL_LEG_RTP_COMPLETE:
717 /* Nothing to do. */
718 return;
719
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100720 case MSC_MNCC_EV_CALL_ENDED:
721 /* Cleaned up above */
722 return;
723
724 case MSC_EV_CALL_LEG_TERM:
725 /* RTP streams cleaned up above */
726
727 msc_a_get(msc_a, __func__);
728 if (msc_a->cc.active_trans)
729 trans_free(msc_a->cc.active_trans);
730
731 /* If there is another call still waiting to be activated, this is the time when the mgcp_ctx is
732 * available again and the other call can start assigning. */
733 waiting_trans = find_waiting_call(msc_a);
734 if (waiting_trans) {
735 LOG_MSC_A(msc_a, LOGL_DEBUG, "(ti %02x) Call waiting: starting Assignment\n",
736 waiting_trans->transaction_id);
737 msc_a_try_call_assignment(waiting_trans);
738 }
739 msc_a_put(msc_a, __func__);
740 return;
741
742 case MSC_A_EV_HANDOVER_REQUIRED:
743 msc_ho_start(msc_a, (struct ran_handover_required*)data);
744 return;
745
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200746 case MSC_A_EV_HANDOVER_END:
747 /* Termination event of the msc_ho_fsm. No action needed, it's all done in the msc_ho_fsm cleanup. This
748 * event only exists because osmo_fsm_inst_alloc_child() requires a parent term event; and maybe
749 * interesting for logging. */
750 return;
751
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100752 case MSC_A_EV_MO_CLOSE:
753 case MSC_A_EV_CN_CLOSE:
754 case MSC_A_EV_UNUSED:
755 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
756 return;
757
758 default:
759 OSMO_ASSERT(false);
760 }
761}
762
763static int msc_a_fsm_timer_cb(struct osmo_fsm_inst *fi)
764{
765 struct msc_a *msc_a = fi->priv;
766 if (msc_a_in_release(msc_a)) {
767 LOG_MSC_A(msc_a, LOGL_ERROR, "Timeout while releasing, discarding right now\n");
768 msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
769 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
770 } else {
771 enum gsm48_reject_value cause = GSM48_REJECT_CONGESTION;
772 osmo_fsm_inst_dispatch(fi, MSC_A_EV_CN_CLOSE, &cause);
773 }
774 return 0;
775}
776
777static void msc_a_fsm_releasing_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
778{
779 struct msc_a *msc_a = fi->priv;
780 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
781 int i;
782 char buf[128];
783 const char * const use_counts_to_cancel[] = {
784 MSC_A_USE_LOCATION_UPDATING,
785 MSC_A_USE_CM_SERVICE_CC,
786 MSC_A_USE_CM_SERVICE_SMS,
787 MSC_A_USE_CM_SERVICE_SS,
788 MSC_A_USE_PAGING_RESPONSE,
789 };
790
791 LOG_MSC_A(msc_a, LOGL_DEBUG, "Releasing: msc_a use is %s\n",
792 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
793
794 if (vsub) {
795 vlr_subscr_get(vsub, __func__);
796
797 /* Cancel all VLR FSMs, if any */
798 vlr_subscr_cancel_attach_fsm(vsub, OSMO_FSM_TERM_ERROR, GSM48_REJECT_CONGESTION);
799
800 /* The subscriber has no active connection anymore.
801 * Restart the periodic Location Update expiry timer for this subscriber. */
802 vlr_subscr_enable_expire_lu(vsub);
803 }
804
805 /* If we're closing in a middle of a trans, we need to clean up */
806 trans_conn_closed(msc_a);
807
808 call_leg_release(msc_a->cc.call_leg);
809
810 /* Cancel use counts for pending CM Service / Paging */
811 for (i = 0; i < ARRAY_SIZE(use_counts_to_cancel); i++) {
812 const char *use = use_counts_to_cancel[i];
813 int32_t count = osmo_use_count_by(&msc_a->use_count, use);
814 if (!count)
815 continue;
816 LOG_MSC_A(msc_a, LOGL_DEBUG, "Releasing: canceling still pending use: %s (%d)\n", use, count);
817 osmo_use_count_get_put(&msc_a->use_count, use, -count);
818 }
819
820 if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) {
821 sgs_iface_tx_release(vsub);
822 /* In SGsAP there is no confirmation of a release. */
823 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
824 } else {
825 struct ran_msg msg = {
826 .msg_type = RAN_MSG_CLEAR_COMMAND,
827 .clear_command = {
Neels Hofmeyrd9fe7112020-07-11 00:20:20 +0200828 /* "Call Control" is the only cause code listed in 3GPP TS 48.008 3.2.1.21 CLEAR COMMAND
829 * that qualifies for a normal release situation. (OS#4664) */
830 .gsm0808_cause = GSM0808_CAUSE_CALL_CONTROL,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100831 .csfb_ind = (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED),
832 },
833 };
834 msc_a_get(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
835 msc_a_ran_down(msc_a, MSC_ROLE_I, &msg);
Philipp Maier47cf84d2019-08-15 14:56:54 +0200836
837 /* The connection is cleared. The MS will now go back to 4G,
838 Switch the RAN type back to SGS. */
839 if (vsub && vsub->sgs_fsm->state == SGS_UE_ST_ASSOCIATED)
840 vsub->cs.attached_via_ran = OSMO_RAT_EUTRAN_SGS;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100841 }
842
843 if (vsub)
844 vlr_subscr_put(vsub, __func__);
845}
846
847static void msc_a_fsm_releasing(struct osmo_fsm_inst *fi, uint32_t event, void *data)
848{
849 struct msc_a *msc_a = fi->priv;
850
851 msc_a_cleanup_rtp_streams(msc_a, event, data);
852
853 switch (event) {
854 case MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST:
855 case MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST:
856 msc_a_ran_dec(msc_a, data, MSC_ROLE_I);
857 return;
858
859 case MSC_A_EV_MO_CLOSE:
860 case MSC_A_EV_CN_CLOSE:
861 case MSC_A_EV_UNUSED:
862 /* Already releasing */
863 return;
864
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100865 case MSC_EV_CALL_LEG_TERM:
866 case MSC_MNCC_EV_CALL_ENDED:
867 /* RTP streams cleaned up above */
868 return;
869
Neels Hofmeyr0a437be2019-05-10 15:55:52 +0200870 case MSC_A_EV_HANDOVER_END:
871 /* msc_ho_fsm does cleanup. */
872 return;
873
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100874 default:
875 OSMO_ASSERT(false);
876 }
877}
878
879
880static void msc_a_fsm_released_onenter(struct osmo_fsm_inst *fi, uint32_t prev_state)
881{
882 struct msc_a *msc_a = msc_a_fi_priv(fi);
883 char buf[128];
884 LOG_MSC_A(msc_a, LOGL_DEBUG, "Released: msc_a use is %s\n",
885 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
886 if (osmo_use_count_total(&msc_a->use_count) == 0)
887 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi);
888}
889
890static void msc_a_fsm_released(struct osmo_fsm_inst *fi, uint32_t event, void *data)
891{
892 if (event == MSC_A_EV_UNUSED)
893 osmo_fsm_inst_term(fi, OSMO_FSM_TERM_REGULAR, fi);
894}
895
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100896void msc_a_fsm_cleanup(struct osmo_fsm_inst *fi, enum osmo_fsm_term_cause cause)
897{
898 struct msc_a *msc_a = msc_a_fi_priv(fi);
899
900 trans_conn_closed(msc_a);
901
902 if (msc_a_fsm_has_active_transactions(fi))
903 LOG_MSC_A(msc_a, LOGL_ERROR, "Deallocating active transactions failed\n");
904
905 LOG_MSC_A_CAT(msc_a, DREF, LOGL_DEBUG, "max total use count was %d\n", msc_a->max_total_use_count);
906}
907
908const struct value_string msc_a_fsm_event_names[] = {
909 OSMO_VALUE_STRING(MSC_REMOTE_EV_RX_GSUP),
910 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE),
911 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_RTP_COMPLETE),
Neels Hofmeyrc4628a32018-12-07 14:47:34 +0100912 OSMO_VALUE_STRING(MSC_EV_CALL_LEG_TERM),
913 OSMO_VALUE_STRING(MSC_MNCC_EV_NEED_LOCAL_RTP),
914 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_PROCEEDING),
915 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_COMPLETE),
916 OSMO_VALUE_STRING(MSC_MNCC_EV_CALL_ENDED),
917 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_COMPLETE_LAYER_3),
918 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST),
919 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST),
920 OSMO_VALUE_STRING(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST),
921 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST),
922 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE),
923 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE),
924 OSMO_VALUE_STRING(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST),
925 OSMO_VALUE_STRING(MSC_A_EV_COMPLETE_LAYER_3_OK),
926 OSMO_VALUE_STRING(MSC_A_EV_CLASSMARK_UPDATE),
927 OSMO_VALUE_STRING(MSC_A_EV_AUTHENTICATED),
928 OSMO_VALUE_STRING(MSC_A_EV_TRANSACTION_ACCEPTED),
929 OSMO_VALUE_STRING(MSC_A_EV_CN_CLOSE),
930 OSMO_VALUE_STRING(MSC_A_EV_MO_CLOSE),
931 OSMO_VALUE_STRING(MSC_A_EV_UNUSED),
932 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_REQUIRED),
933 OSMO_VALUE_STRING(MSC_A_EV_HANDOVER_END),
934 {}
935};
936
937#define S(x) (1 << (x))
938
939static const struct osmo_fsm_state msc_a_fsm_states[] = {
940 [MSC_A_ST_VALIDATE_L3] = {
941 .name = OSMO_STRINGIFY(MSC_A_ST_VALIDATE_L3),
942 .in_event_mask = 0
943 | S(MSC_A_EV_FROM_I_COMPLETE_LAYER_3)
944 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
945 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
946 | S(MSC_A_EV_COMPLETE_LAYER_3_OK)
947 | S(MSC_A_EV_MO_CLOSE)
948 | S(MSC_A_EV_CN_CLOSE)
949 | S(MSC_A_EV_UNUSED)
950 ,
951 .out_state_mask = 0
952 | S(MSC_A_ST_VALIDATE_L3)
953 | S(MSC_A_ST_AUTH_CIPH)
954 | S(MSC_A_ST_RELEASING)
955 ,
956 .action = msc_a_fsm_validate_l3,
957 },
958 [MSC_A_ST_AUTH_CIPH] = {
959 .name = OSMO_STRINGIFY(MSC_A_ST_AUTH_CIPH),
960 .in_event_mask = 0
961 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
962 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
963 | S(MSC_A_EV_AUTHENTICATED)
964 | S(MSC_A_EV_MO_CLOSE)
965 | S(MSC_A_EV_CN_CLOSE)
966 | S(MSC_A_EV_UNUSED)
967 ,
968 .out_state_mask = 0
969 | S(MSC_A_ST_WAIT_CLASSMARK_UPDATE)
970 | S(MSC_A_ST_AUTHENTICATED)
971 | S(MSC_A_ST_RELEASING)
972 ,
973 .action = msc_a_fsm_auth_ciph,
974 },
975 [MSC_A_ST_WAIT_CLASSMARK_UPDATE] = {
976 .name = OSMO_STRINGIFY(MSC_A_ST_WAIT_CLASSMARK_UPDATE),
977 .in_event_mask = 0
978 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
979 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
980 | S(MSC_A_EV_CLASSMARK_UPDATE)
981 | S(MSC_A_EV_MO_CLOSE)
982 | S(MSC_A_EV_CN_CLOSE)
983 ,
984 .out_state_mask = 0
985 | S(MSC_A_ST_AUTH_CIPH)
986 | S(MSC_A_ST_RELEASING)
987 ,
988 .action = msc_a_fsm_wait_classmark_update,
989 },
990 [MSC_A_ST_AUTHENTICATED] = {
991 .name = OSMO_STRINGIFY(MSC_A_ST_AUTHENTICATED),
992 /* allow everything to release for any odd behavior */
993 .in_event_mask = 0
994 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
995 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
996 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
997 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
998 | S(MSC_A_EV_MO_CLOSE)
999 | S(MSC_A_EV_CN_CLOSE)
1000 | S(MSC_A_EV_UNUSED)
1001 ,
1002 .out_state_mask = 0
1003 | S(MSC_A_ST_RELEASING)
1004 | S(MSC_A_ST_COMMUNICATING)
1005 ,
1006 .onenter = msc_a_fsm_authenticated_enter,
1007 .action = msc_a_fsm_authenticated,
1008 },
1009 [MSC_A_ST_COMMUNICATING] = {
1010 .name = OSMO_STRINGIFY(MSC_A_ST_COMMUNICATING),
1011 /* allow everything to release for any odd behavior */
1012 .in_event_mask = 0
1013 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1014 | S(MSC_A_EV_FROM_I_PREPARE_SUBSEQUENT_HANDOVER_REQUEST)
1015 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1016 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_RESPONSE)
1017 | S(MSC_A_EV_FROM_T_PREPARE_HANDOVER_FAILURE)
1018 | S(MSC_A_EV_FROM_T_PROCESS_ACCESS_SIGNALLING_REQUEST)
1019 | S(MSC_A_EV_FROM_T_SEND_END_SIGNAL_REQUEST)
1020 | S(MSC_A_EV_TRANSACTION_ACCEPTED)
1021 | S(MSC_A_EV_MO_CLOSE)
1022 | S(MSC_A_EV_CN_CLOSE)
1023 | S(MSC_A_EV_UNUSED)
1024 | S(MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE)
1025 | S(MSC_EV_CALL_LEG_RTP_COMPLETE)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001026 | S(MSC_EV_CALL_LEG_TERM)
1027 | S(MSC_MNCC_EV_CALL_ENDED)
1028 | S(MSC_A_EV_HANDOVER_REQUIRED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +02001029 | S(MSC_A_EV_HANDOVER_END)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001030 ,
1031 .out_state_mask = 0
1032 | S(MSC_A_ST_RELEASING)
1033 ,
1034 .action = msc_a_fsm_communicating,
1035 },
1036 [MSC_A_ST_RELEASING] = {
1037 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASING),
1038 .in_event_mask = 0
1039 | S(MSC_A_EV_FROM_I_PROCESS_ACCESS_SIGNALLING_REQUEST)
1040 | S(MSC_A_EV_FROM_I_SEND_END_SIGNAL_REQUEST)
1041 | S(MSC_A_EV_UNUSED)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001042 | S(MSC_EV_CALL_LEG_TERM)
1043 | S(MSC_MNCC_EV_CALL_ENDED)
Neels Hofmeyr0a437be2019-05-10 15:55:52 +02001044 | S(MSC_A_EV_HANDOVER_END)
Pau Espin Pedrole53ecde2021-07-12 13:37:24 +02001045 | S(MSC_A_EV_CN_CLOSE)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001046 ,
1047 .out_state_mask = 0
1048 | S(MSC_A_ST_RELEASED)
1049 ,
1050 .onenter = msc_a_fsm_releasing_onenter,
1051 .action = msc_a_fsm_releasing,
1052 },
1053 [MSC_A_ST_RELEASED] = {
1054 .name = OSMO_STRINGIFY(MSC_A_ST_RELEASED),
1055 .in_event_mask = 0
1056 | S(MSC_A_EV_UNUSED)
1057 ,
1058 .onenter = msc_a_fsm_released_onenter,
1059 .action = msc_a_fsm_released,
1060 },
1061};
1062
1063static struct osmo_fsm msc_a_fsm = {
1064 .name = "msc_a",
1065 .states = msc_a_fsm_states,
1066 .num_states = ARRAY_SIZE(msc_a_fsm_states),
1067 .log_subsys = DMSC,
1068 .event_names = msc_a_fsm_event_names,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001069 .timer_cb = msc_a_fsm_timer_cb,
1070 .cleanup = msc_a_fsm_cleanup,
1071};
1072
1073static __attribute__((constructor)) void msc_a_fsm_init()
1074{
1075 OSMO_ASSERT(osmo_fsm_register(&msc_a_fsm) == 0);
1076}
1077
1078static int msc_a_use_cb(struct osmo_use_count_entry *e, int32_t old_use_count, const char *file, int line)
1079{
1080 struct msc_a *msc_a = e->use_count->talloc_object;
1081 char buf[128];
1082 int32_t total;
1083 int level;
1084
1085 if (!e->use)
1086 return -EINVAL;
1087
1088 total = osmo_use_count_total(&msc_a->use_count);
1089
1090 if (total == 0
1091 || (total == 1 && old_use_count == 0 && e->count == 1))
1092 level = LOGL_INFO;
1093 else
1094 level = LOGL_DEBUG;
1095
1096 LOG_MSC_A_CAT_SRC(msc_a, DREF, level, file, line, "%s %s: now used by %s\n",
1097 (e->count - old_use_count) > 0? "+" : "-", e->use,
1098 osmo_use_count_name_buf(buf, sizeof(buf), &msc_a->use_count));
1099
1100 if (e->count < 0)
1101 return -ERANGE;
1102
1103 msc_a->max_total_use_count = OSMO_MAX(msc_a->max_total_use_count, total);
1104
1105 if (total == 0)
1106 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_UNUSED, NULL);
1107 return 0;
1108}
1109
1110struct msc_a *msc_a_alloc(struct msub *msub, struct ran_infra *ran)
1111{
1112 struct msc_a *msc_a = msub_role_alloc(msub, MSC_ROLE_A, &msc_a_fsm, struct msc_a, ran);
1113 msc_a->use_count = (struct osmo_use_count){
1114 .talloc_object = msc_a,
1115 .use_cb = msc_a_use_cb,
1116 };
1117 osmo_use_count_make_static_entries(&msc_a->use_count, msc_a->use_count_buf, ARRAY_SIZE(msc_a->use_count_buf));
1118 /* Start timeout for first state */
Neels Hofmeyr01653252019-09-03 02:06:22 +02001119 msc_a_state_chg_always(msc_a, MSC_A_ST_VALIDATE_L3);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001120 return msc_a;
1121}
1122
1123bool msc_a_is_establishing_auth_ciph(const struct msc_a *msc_a)
1124{
1125 if (!msc_a || !msc_a->c.fi)
1126 return false;
1127 return msc_a->c.fi->state == MSC_A_ST_AUTH_CIPH;
1128}
1129
1130const struct value_string complete_layer3_type_names[] = {
1131 { COMPLETE_LAYER3_NONE, "NONE" },
1132 { COMPLETE_LAYER3_LU, "LU" },
1133 { COMPLETE_LAYER3_CM_SERVICE_REQ, "CM_SERVICE_REQ" },
1134 { COMPLETE_LAYER3_PAGING_RESP, "PAGING_RESP" },
Neels Hofmeyrae98b972021-07-27 03:46:49 +02001135 { COMPLETE_LAYER3_CM_RE_ESTABLISH_REQ, "CM_RE_ESTABLISH_REQ" },
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001136 { 0, NULL }
1137};
1138
1139#define _msc_a_update_id(MSC_A, FMT, ARGS ...) \
1140 do { \
1141 if (osmo_fsm_inst_update_id_f(msc_a->c.fi, FMT ":%s:%s", \
1142 ## ARGS, \
1143 msub_ran_conn_name(msc_a->c.msub), \
1144 complete_layer3_type_name(msc_a->complete_layer3_type)) \
1145 == 0) { \
1146 struct vlr_subscr *_vsub = msc_a_vsub(MSC_A); \
1147 if (_vsub) { \
1148 if (_vsub->lu_fsm) \
1149 osmo_fsm_inst_update_id(_vsub->lu_fsm, (MSC_A)->c.fi->id); \
1150 if (_vsub->auth_fsm) \
1151 osmo_fsm_inst_update_id(_vsub->auth_fsm, (MSC_A)->c.fi->id); \
1152 if (_vsub->proc_arq_fsm) \
1153 osmo_fsm_inst_update_id(_vsub->proc_arq_fsm, (MSC_A)->c.fi->id); \
1154 } \
1155 LOG_MSC_A(MSC_A, LOGL_DEBUG, "Updated ID\n"); \
1156 } \
1157 /* otherwise osmo_fsm_inst_update_id_f() will log an error. */ \
1158 } while (0)
1159
1160
1161/* 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 +02001162void msc_a_update_id_from_mi(struct msc_a *msc_a, const struct osmo_mobile_identity *mi)
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001163{
Neels Hofmeyr46d526a2020-05-29 03:27:50 +02001164 _msc_a_update_id(msc_a, "%s", osmo_mobile_identity_to_str_c(OTC_SELECT, mi));
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001165}
1166
1167/* Update msc_a->fi id string from current msc_a->vsub and msc_a->complete_layer3_type. */
1168void msc_a_update_id(struct msc_a *msc_a)
1169{
1170 _msc_a_update_id(msc_a, "%s", vlr_subscr_name(msc_a_vsub(msc_a)));
1171}
1172
1173/* Iterate all msc_a instances that are relevant for this subscriber, and update FSM ID strings for all of the FSM
1174 * instances. */
1175void msc_a_update_id_for_vsub(struct vlr_subscr *for_vsub)
1176{
1177 struct msub *msub;
1178 llist_for_each_entry(msub, &msub_list, entry) {
1179 struct vlr_subscr *vsub = msub_vsub(msub);
1180 if (vsub != for_vsub)
1181 continue;
1182 msc_a_update_id(msub_msc_a(msub));
1183 }
1184}
1185
1186static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
1187{
1188 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
1189 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
1190
1191 switch (pdisc) {
1192 case GSM48_PDISC_MM:
1193 switch (msg_type) {
1194 case GSM48_MT_MM_LOC_UPD_REQUEST:
1195 case GSM48_MT_MM_CM_SERV_REQ:
1196 case GSM48_MT_MM_CM_REEST_REQ:
1197 case GSM48_MT_MM_AUTH_RESP:
1198 case GSM48_MT_MM_AUTH_FAIL:
1199 case GSM48_MT_MM_ID_RESP:
1200 case GSM48_MT_MM_TMSI_REALL_COMPL:
1201 case GSM48_MT_MM_IMSI_DETACH_IND:
1202 return true;
1203 default:
1204 break;
1205 }
1206 break;
1207 case GSM48_PDISC_RR:
1208 switch (msg_type) {
1209 /* GSM48_MT_RR_CIPH_M_COMPL is actually handled in bssmap_rx_ciph_compl() and gets redirected in the
1210 * BSSAP layer to ran_conn_cipher_mode_compl() (before this here is reached) */
1211 case GSM48_MT_RR_PAG_RESP:
1212 case GSM48_MT_RR_CIPH_M_COMPL:
1213 return true;
1214 default:
1215 break;
1216 }
1217 break;
1218 default:
1219 break;
1220 }
1221
1222 return false;
1223}
1224
1225/* Main entry point for GSM 04.08/44.008 Layer 3 data (e.g. from the BSC). */
1226int msc_a_up_l3(struct msc_a *msc_a, struct msgb *msg)
1227{
1228 struct gsm48_hdr *gh;
1229 uint8_t pdisc;
1230 int rc;
1231 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1232 int is_r99;
1233
1234 OSMO_ASSERT(msg->l3h);
1235 OSMO_ASSERT(msg);
1236
1237 gh = msgb_l3(msg);
1238 pdisc = gsm48_hdr_pdisc(gh);
1239
1240 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_DEBUG, "Dispatching 04.08 message: %s %s\n",
1241 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1242
1243 /* To evaluate the 3GPP TS 24.007 Duplicate Detection, we need Classmark information on whether the MS is R99
1244 * capable. If the subscriber is already actively connected, the Classmark information is stored with the
1245 * vlr_subscr. Otherwise, this *must* be a Complete Layer 3 with Classmark info. */
1246 if (vsub)
1247 is_r99 = osmo_gsm48_classmark_is_r99(&vsub->classmark) ? 1 : 0;
1248 else
1249 is_r99 = compl_l3_msg_is_r99(msg);
1250
1251 if (is_r99 < 0) {
1252 LOG_MSC_A(msc_a, LOGL_ERROR,
1253 "No Classmark Information, dropping non-Complete-Layer3 message: %s\n",
1254 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1255 return -EACCES;
1256 }
1257
1258 if (is_r99 >= 0
1259 && ran_dec_dtap_undup_is_duplicate(msc_a->c.fi, msc_a->n_sd_next, is_r99 ? true : false, msg)) {
1260 LOG_MSC_A(msc_a, LOGL_DEBUG, "Dropping duplicate message"
1261 " (3GPP TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection)\n");
1262 return 0;
1263 }
1264
1265 if (!msc_a_is_accepted(msc_a)
1266 && !msg_is_initially_permitted(gh)) {
1267 LOG_MSC_A(msc_a, LOGL_ERROR,
1268 "Message not permitted for initial conn: %s\n",
1269 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1270 return -EACCES;
1271 }
1272
1273 if (vsub && vsub->cs.attached_via_ran != msc_a->c.ran->type) {
1274 LOG_MSC_A(msc_a, LOGL_ERROR,
1275 "Illegal situation: RAN type mismatch:"
1276 " attached via %s, received message via %s\n",
1277 osmo_rat_type_name(vsub->cs.attached_via_ran),
1278 osmo_rat_type_name(msc_a->c.ran->type));
1279 return -EACCES;
1280 }
1281
1282#if 0
1283 if (silent_call_reroute(conn, msg))
1284 return silent_call_rx(conn, msg);
1285#endif
1286
1287 switch (pdisc) {
1288 case GSM48_PDISC_CC:
1289 rc = gsm0408_rcv_cc(msc_a, msg);
1290 break;
1291 case GSM48_PDISC_MM:
1292 rc = gsm0408_rcv_mm(msc_a, msg);
1293 break;
1294 case GSM48_PDISC_RR:
1295 rc = gsm0408_rcv_rr(msc_a, msg);
1296 break;
1297 case GSM48_PDISC_SMS:
1298 rc = gsm0411_rcv_sms(msc_a, msg);
1299 break;
1300 case GSM48_PDISC_MM_GPRS:
1301 case GSM48_PDISC_SM_GPRS:
1302 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unimplemented "
1303 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1304 rc = -ENOTSUP;
1305 break;
1306 case GSM48_PDISC_NC_SS:
1307 rc = gsm0911_rcv_nc_ss(msc_a, msg);
1308 break;
1309 case GSM48_PDISC_TEST:
1310 rc = gsm0414_rcv_test(msc_a, msg);
1311 break;
1312 default:
1313 LOG_MSC_A_CAT(msc_a, DRLL, LOGL_NOTICE, "Unknown "
1314 "GSM 04.08 discriminator 0x%02x\n", pdisc);
1315 rc = -EINVAL;
1316 break;
1317 }
1318
1319 return rc;
1320}
1321
1322static void msc_a_up_call_assignment_complete(struct msc_a *msc_a, const struct ran_msg *ac)
1323{
1324 struct gsm_trans *cc_trans = msc_a->cc.active_trans;
1325 struct rtp_stream *rtps_to_ran = msc_a->cc.call_leg ? msc_a->cc.call_leg->rtp[RTP_TO_RAN] : NULL;
Pau Espin Pedrol9de384a2022-01-07 16:27:04 +01001326 const enum mgcp_codecs *codec_if_known = ac->assignment_complete.codec_present ?
1327 &ac->assignment_complete.codec : NULL;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001328
1329 if (!rtps_to_ran) {
1330 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but no RTP stream is set up\n");
1331 return;
1332 }
1333 if (!cc_trans) {
Neels Hofmeyr550506a2022-01-13 23:31:57 +01001334 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Assignment Complete, but no CC transaction is active\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001335 return;
1336 }
1337
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001338 if (rtps_to_ran->use_osmux != ac->assignment_complete.osmux_present) {
1339 LOG_MSC_A_CAT(msc_a, DCC, LOGL_ERROR, "Osmux usage ass request and complete don't match: %d vs %d\n",
1340 rtps_to_ran->use_osmux, ac->assignment_complete.osmux_present);
1341 call_leg_release(msc_a->cc.call_leg);
1342 return;
1343 }
1344
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001345 /* Update RAN-side endpoint CI: */
Pau Espin Pedrol9de384a2022-01-07 16:27:04 +01001346 if (codec_if_known)
1347 rtp_stream_set_codec(rtps_to_ran, *codec_if_known);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001348 rtp_stream_set_remote_addr(rtps_to_ran, &ac->assignment_complete.remote_rtp);
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001349 if (rtps_to_ran->use_osmux)
1350 rtp_stream_set_remote_osmux_cid(rtps_to_ran,
1351 ac->assignment_complete.osmux_cid);
1352
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001353 rtp_stream_commit(rtps_to_ran);
1354
1355 /* Setup CN side endpoint CI:
1356 * Now that
1357 * - the first CI has been created and a definitive endpoint name is assigned to the call_leg's MGW
1358 * endpoint,
1359 * - the Assignment has chosen a speech codec
1360 * go on to create the CN side RTP stream's CI. */
1361 if (call_leg_ensure_ci(msc_a->cc.call_leg, RTP_TO_CN, cc_trans->callref, cc_trans,
Pau Espin Pedrol9de384a2022-01-07 16:27:04 +01001362 codec_if_known, NULL)) {
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001363 LOG_MSC_A_CAT(msc_a, DCC, LOGL_ERROR, "Error creating MGW CI towards CN\n");
1364 call_leg_release(msc_a->cc.call_leg);
1365 return;
1366 }
1367}
1368
1369static void msc_a_up_call_assignment_failure(struct msc_a *msc_a, const struct ran_msg *af)
1370{
1371 struct gsm_trans *trans;
1372
1373 /* For a normal voice call, there will be an rtp_stream FSM. */
1374 if (msc_a->cc.call_leg && msc_a->cc.call_leg->rtp[RTP_TO_RAN]) {
1375 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing call\n");
1376 rtp_stream_release(msc_a->cc.call_leg->rtp[RTP_TO_RAN]);
1377 return;
1378 }
1379
1380 /* Otherwise, a silent call might be active */
1381 trans = trans_find_by_type(msc_a, TRANS_SILENT_CALL);
1382 if (trans) {
1383 LOG_MSC_A(msc_a, LOGL_ERROR, "Assignment Failure, releasing silent call\n");
1384 trans_free(trans);
1385 return;
1386 }
1387
1388 /* Neither a voice call nor silent call assignment. Assume the worst and detach. */
1389 msc_a_release_cn(msc_a);
1390}
1391
1392static void msc_a_up_classmark_update(struct msc_a *msc_a, const struct osmo_gsm48_classmark *classmark,
1393 struct osmo_gsm48_classmark *dst)
1394{
1395 if (!dst) {
1396 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
1397
1398 if (!vsub)
1399 dst = &msc_a->temporary_classmark;
1400 else
1401 dst = &vsub->classmark;
1402 }
1403
Martin Hauke3f07dac2019-11-14 17:49:08 +01001404 LOG_MSC_A(msc_a, LOGL_DEBUG, "A5 capabilities received from Classmark Update: %s\n",
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001405 osmo_gsm48_classmark_a5_name(classmark));
1406 osmo_gsm48_classmark_update(dst, classmark);
1407
1408 /* bump subscr conn FSM in case it is waiting for a Classmark Update */
1409 if (msc_a->c.fi->state == MSC_A_ST_WAIT_CLASSMARK_UPDATE)
1410 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CLASSMARK_UPDATE, NULL);
1411}
1412
1413static void msc_a_up_sapi_n_reject(struct msc_a *msc_a, const struct ran_msg *msg)
1414{
1415 int sapi = msg->sapi_n_reject.dlci & 0x7;
1416 if (sapi == UM_SAPI_SMS)
1417 gsm411_sapi_n_reject(msc_a);
1418}
1419
1420static int msc_a_up_ho(struct msc_a *msc_a, const struct msc_a_ran_dec_data *d, uint32_t ho_fi_event)
1421{
1422 if (!msc_a->ho.fi) {
1423 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx Handover message, but no Handover ongoing: %s\n", d->ran_dec->msg_name);
1424 return -EINVAL;
1425 }
1426 return osmo_fsm_inst_dispatch(msc_a->ho.fi, ho_fi_event, (void*)d);
1427}
1428
1429int msc_a_ran_dec_from_msc_i(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1430{
1431 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Alexander Couzens2aaff752021-10-19 17:09:11 +02001432 struct gsm_network *net = msc_a_net(msc_a);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001433 const struct ran_msg *msg = d->ran_dec;
1434 int rc = -99;
1435
1436 switch (msg->msg_type) {
1437
1438 case RAN_MSG_COMPL_L3:
Neels Hofmeyr68f50da2020-06-24 14:22:52 +02001439 /* In case the cell_id from Complete Layer 3 Information lacks a PLMN, write the configured PLMN code
1440 * into msc_a->via_cell. Then overwrite with those bits obtained from Complete Layer 3 Information. */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001441 msc_a->via_cell = (struct osmo_cell_global_id){
1442 .lai.plmn = msc_a_net(msc_a)->plmn,
1443 };
1444 gsm0808_cell_id_to_cgi(&msc_a->via_cell, msg->compl_l3.cell_id);
1445 rc = msc_a_up_l3(msc_a, msg->compl_l3.msg);
1446 if (!rc) {
1447 struct ran_conn *conn = msub_ran_conn(msc_a->c.msub);
1448 if (conn)
1449 ran_peer_cells_seen_add(conn->ran_peer, msg->compl_l3.cell_id);
1450 }
1451 break;
1452
1453 case RAN_MSG_DTAP:
1454 rc = msc_a_up_l3(msc_a, msg->dtap);
1455 break;
1456
1457 case RAN_MSG_CLEAR_REQUEST:
1458 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
1459 break;
1460
1461 case RAN_MSG_CLEAR_COMPLETE:
1462 switch (msc_a->c.fi->state) {
1463 case MSC_A_ST_RELEASING:
1464 msc_a_put_all(msc_a, MSC_A_USE_WAIT_CLEAR_COMPLETE);
1465 msc_a_state_chg(msc_a, MSC_A_ST_RELEASED);
1466 break;
1467 case MSC_A_ST_RELEASED:
1468 break;
1469 default:
1470 LOG_MSC_A(msc_a, LOGL_ERROR, "Received Clear Complete event, but did not send Clear Command\n");
1471 msc_a_state_chg(msc_a, MSC_A_ST_RELEASING);
1472 break;
1473 }
1474 rc = 0;
1475 break;
1476
1477 case RAN_MSG_CLASSMARK_UPDATE:
1478 msc_a_up_classmark_update(msc_a, msg->classmark_update.classmark, NULL);
1479 rc = 0;
1480 break;
1481
1482 case RAN_MSG_CIPHER_MODE_COMPLETE:
1483 /* Remember what Ciphering was negotiated (e.g. for Handover) */
1484 if (msg->cipher_mode_complete.alg_id) {
1485 msc_a->geran_encr.alg_id = msg->cipher_mode_complete.alg_id;
1486 LOG_MSC_A(msc_a, LOGL_DEBUG, "Cipher Mode Complete: chosen encryption algorithm: A5/%u\n",
1487 msc_a->geran_encr.alg_id - 1);
Alexander Couzens2aaff752021-10-19 17:09:11 +02001488 }
1489
1490 if (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU) {
1491 int16_t utran_encryption;
1492
1493 /* utran: ensure chosen ciphering mode is allowed
1494 * If the IE is missing (utran_encryption == -1), parse it as no encryption */
1495 utran_encryption = msg->cipher_mode_complete.utran_encryption;
1496 if (utran_encryption == -1)
1497 utran_encryption = 0;
1498 if ((net->uea_encryption_mask & (1 << utran_encryption)) == 0) {
1499 /* cipher disallowed */
1500 LOG_MSC_A(msc_a, LOGL_ERROR, "Cipher Mode Complete: RNC chosen forbidden ciphering UEA%d\n",
1501 msg->cipher_mode_complete.utran_encryption);
1502 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT);
1503 rc = 0;
1504 break;
1505 }
1506 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001507 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_COMPL);
1508 rc = 0;
Neels Hofmeyre9a39112019-08-29 00:10:49 +02001509
1510 /* Evaluate enclosed L3 message, typically Identity Response (IMEISV) */
1511 if (msg->cipher_mode_complete.l3_msg) {
1512 unsigned char *data = (unsigned char*)(msg->cipher_mode_complete.l3_msg->val);
1513 uint16_t len = msg->cipher_mode_complete.l3_msg->len;
1514 struct msgb *dtap = msgb_alloc(len, "DTAP from Cipher Mode Complete");
1515 unsigned char *pos = msgb_put(dtap, len);
1516 memcpy(pos, data, len);
1517 dtap->l3h = pos;
1518 rc = msc_a_up_l3(msc_a, dtap);
1519 msgb_free(dtap);
1520 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001521 break;
1522
1523 case RAN_MSG_CIPHER_MODE_REJECT:
1524 vlr_subscr_rx_ciph_res(vsub, VLR_CIPH_REJECT);
1525 rc = 0;
1526 break;
1527
1528 case RAN_MSG_ASSIGNMENT_COMPLETE:
1529 msc_a_up_call_assignment_complete(msc_a, msg);
1530 rc = 0;
1531 break;
1532
1533 case RAN_MSG_ASSIGNMENT_FAILURE:
1534 msc_a_up_call_assignment_failure(msc_a, msg);
1535 rc = 0;
1536 break;
1537
1538 case RAN_MSG_SAPI_N_REJECT:
1539 msc_a_up_sapi_n_reject(msc_a, msg);
1540 rc = 0;
1541 break;
1542
1543 case RAN_MSG_HANDOVER_PERFORMED:
1544 /* The BSS lets us know that a handover happened within the BSS, which doesn't concern us. */
1545 LOG_MSC_A(msc_a, LOGL_ERROR, "'Handover Performed' handling not implemented\n");
1546 break;
1547
1548 case RAN_MSG_HANDOVER_REQUIRED:
1549 /* The BSS lets us know that it wants to handover to a different cell */
1550 rc = osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_HANDOVER_REQUIRED, (void*)&msg->handover_required);
1551 break;
1552
1553 case RAN_MSG_HANDOVER_FAILURE:
1554 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1555 break;
1556
Keith Whytea1a70be2021-05-16 02:59:52 +02001557 case RAN_MSG_LCLS_STATUS:
1558 /* The BSS sends us LCLS_STATUS. We do nothing for now, but it is not an error. */
1559 LOG_MSC_A(msc_a, LOGL_DEBUG, "LCLS_STATUS (%s) received from MSC-I\n",
1560 gsm0808_lcls_status_name(msg->lcls_status.status));
1561 rc = 0;
1562 break;
1563
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001564 default:
1565 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-I not implemented: %s\n", ran_msg_type_name(msg->msg_type));
1566 rc = -ENOTSUP;
1567 break;
1568 }
1569 return rc;
1570}
1571
1572static int msc_a_ran_dec_from_msc_t(struct msc_a *msc_a, struct msc_a_ran_dec_data *d)
1573{
1574 struct msc_t *msc_t = msc_a_msc_t(msc_a);
1575 int rc = -99;
1576
1577 if (!msc_t) {
1578 LOG_MSC_A(msc_a, LOGL_ERROR, "Rx message from MSC-T role, but I have no active MSC-T role.\n");
1579 return -EINVAL;
1580 }
1581
1582 OSMO_ASSERT(d->ran_dec);
1583
1584 switch (d->ran_dec->msg_type) {
1585
1586 case RAN_MSG_CLEAR_REQUEST:
1587 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_MO_CLOSE, NULL);
1588 break;
1589
1590 case RAN_MSG_CLEAR_COMPLETE:
1591 rc = osmo_fsm_inst_dispatch(msc_t->c.fi, MSC_T_EV_CLEAR_COMPLETE, NULL);
1592 break;
1593
1594 case RAN_MSG_CLASSMARK_UPDATE:
1595 msc_a_up_classmark_update(msc_a, d->ran_dec->classmark_update.classmark, &msc_t->classmark);
1596 rc = 0;
1597 break;
1598
1599 case RAN_MSG_HANDOVER_REQUEST_ACK:
1600 /* new BSS accepts Handover */
1601 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_REQUEST_ACK);
1602 break;
1603
1604 case RAN_MSG_HANDOVER_DETECT:
1605 /* new BSS signals the MS is DETECTed on the new lchan */
1606 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_DETECT);
1607 break;
1608
1609 case RAN_MSG_HANDOVER_COMPLETE:
1610 /* new BSS signals the MS has fully moved to the new lchan */
1611 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_COMPLETE);
1612 break;
1613
1614 case RAN_MSG_HANDOVER_FAILURE:
1615 rc = msc_a_up_ho(msc_a, d, MSC_HO_EV_RX_FAILURE);
1616 break;
1617
1618 default:
1619 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from MSC-T not implemented: %s\n",
1620 ran_msg_type_name(d->ran_dec->msg_type));
1621 rc = -ENOTSUP;
1622 break;
1623 }
1624 return rc;
1625}
1626
1627int msc_a_ran_decode_cb(struct osmo_fsm_inst *msc_a_fi, void *data, const struct ran_msg *msg)
1628{
1629 struct msc_a *msc_a = msc_a_fi_priv(msc_a_fi);
1630 struct msc_a_ran_dec_data *d = data;
1631 int rc = -99;
1632
1633 d->ran_dec = msg;
1634
1635 switch (d->from_role) {
1636 case MSC_ROLE_I:
1637 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode: %s\n", msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1638 rc = msc_a_ran_dec_from_msc_i(msc_a, d);
1639 break;
1640
1641 case MSC_ROLE_T:
1642 LOG_MSC_A(msc_a, LOGL_DEBUG, "RAN decode from MSC-T: %s\n",
1643 msg->msg_name ? : ran_msg_type_name(msg->msg_type));
1644 rc = msc_a_ran_dec_from_msc_t(msc_a, d);
1645 break;
1646
1647 default:
1648 LOG_MSC_A(msc_a, LOGL_ERROR, "Message from invalid role %s: %s\n", msc_role_name(d->from_role),
1649 ran_msg_type_name(msg->msg_type));
1650 return -ENOTSUP;
1651 }
1652
1653 if (rc)
1654 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),
1655 msc_role_name(d->from_role));
1656 return rc;
1657}
1658
1659/* Your typical DTAP via FORWARD_ACCESS_SIGNALLING_REQUEST */
1660int _msc_a_ran_down(struct msc_a *msc_a, enum msc_role to_role, const struct ran_msg *ran_msg,
1661 const char *file, int line)
1662{
1663 return _msc_a_msg_down(msc_a, to_role, msub_role_to_role_event(msc_a->c.msub, MSC_ROLE_A, to_role),
1664 ran_msg, file, line);
1665}
1666
1667/* To transmit more complex events than just FORWARD_ACCESS_SIGNALLING_REQUEST, e.g. an
1668 * MSC_T_EV_FROM_A_PREPARE_HANDOVER_REQUEST */
1669int _msc_a_msg_down(struct msc_a *msc_a, enum msc_role to_role, uint32_t to_role_event,
1670 const struct ran_msg *ran_msg,
1671 const char *file, int line)
1672{
1673 struct an_apdu an_apdu = {
1674 .an_proto = msc_a->c.ran->an_proto,
1675 .msg = msc_role_ran_encode(msc_a->c.fi, ran_msg),
1676 };
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001677 if (!an_apdu.msg)
1678 return -EIO;
Vadim Yanitskiyc44342b2021-12-07 18:32:35 +03001679 return _msub_role_dispatch(msc_a->c.msub, to_role, to_role_event, &an_apdu, file, line);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001680}
1681
1682int msc_a_tx_dtap_to_i(struct msc_a *msc_a, struct msgb *dtap)
1683{
1684 struct ran_msg ran_msg;
Neels Hofmeyrc192c0b2019-10-07 21:41:18 +02001685 struct gsm48_hdr *gh = msgb_l3(dtap) ? : dtap->data;
1686 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001687
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001688 if (!msc_a) {
Neels Hofmeyr2e8f8812019-08-21 16:56:41 +02001689 LOGP(DMSC, LOGL_ERROR, "Attempt to send DTAP to NULL MSC-A, dropping message: %s %s\n",
1690 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1691 msgb_free(dtap);
1692 return -EIO;
1693 }
1694
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001695 if (msc_a->c.ran->type == OSMO_RAT_EUTRAN_SGS) {
1696 /* The SGs connection to the MME always is at the MSC-A. */
1697 return sgs_iface_tx_dtap_ud(msc_a, dtap);
1698 }
1699
Neels Hofmeyrc192c0b2019-10-07 21:41:18 +02001700 LOG_MSC_A(msc_a, LOGL_DEBUG, "Sending DTAP: %s %s\n",
1701 gsm48_pdisc_name(pdisc), gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
1702
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001703 ran_msg = (struct ran_msg){
1704 .msg_type = RAN_MSG_DTAP,
1705 .dtap = dtap,
1706 };
1707 return msc_a_ran_down(msc_a, MSC_ROLE_I, &ran_msg);
1708}
1709
1710struct msc_a *msc_a_for_vsub(const struct vlr_subscr *vsub, bool valid_conn_only)
1711{
1712 struct msc_a *msc_a = msub_msc_a(msub_for_vsub(vsub));
1713 if (valid_conn_only && !msc_a_is_accepted(msc_a))
1714 return NULL;
1715 return msc_a;
1716}
1717
1718int msc_tx_common_id(struct msc_a *msc_a, enum msc_role to_role)
1719{
1720 struct vlr_subscr *vsub = msc_a_vsub(msc_a);
Vadim Yanitskiy435f67f2021-06-06 15:47:49 +02001721 if (vsub == NULL)
1722 return -ENODEV;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001723 struct ran_msg msg = {
1724 .msg_type = RAN_MSG_COMMON_ID,
1725 .common_id = {
1726 .imsi = vsub->imsi,
Pau Espin Pedrol67106702021-04-27 18:20:15 +02001727 .last_eutran_plmn_present = vsub->sgs.last_eutran_plmn_present,
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001728 },
1729 };
Pau Espin Pedrol67106702021-04-27 18:20:15 +02001730 if (vsub->sgs.last_eutran_plmn_present) {
1731 memcpy(&msg.common_id.last_eutran_plmn, &vsub->sgs.last_eutran_plmn,
1732 sizeof(vsub->sgs.last_eutran_plmn));
1733 }
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001734
1735 return msc_a_ran_down(msc_a, to_role, &msg);
1736}
1737
1738static int msc_a_start_assignment(struct msc_a *msc_a, struct gsm_trans *cc_trans)
1739{
1740 struct call_leg *cl = msc_a->cc.call_leg;
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001741 struct msc_i *msc_i = msc_a_msc_i(msc_a);
1742 struct gsm_network *net = msc_a_net(msc_a);
Pau Espin Pedrol3a02d292022-01-05 13:56:16 +01001743 enum mgcp_codecs codec, *codec_ptr;
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001744
1745 OSMO_ASSERT(!msc_a->cc.active_trans);
1746 msc_a->cc.active_trans = cc_trans;
1747
1748 OSMO_ASSERT(cc_trans && cc_trans->type == TRANS_CC);
1749
1750 if (!cl) {
1751 cl = msc_a->cc.call_leg = call_leg_alloc(msc_a->c.fi,
1752 MSC_EV_CALL_LEG_TERM,
1753 MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE,
Neels Hofmeyr265a4c72019-05-09 16:20:51 +02001754 MSC_EV_CALL_LEG_RTP_COMPLETE);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001755 OSMO_ASSERT(cl);
1756
Martin Hauke3f07dac2019-11-14 17:49:08 +01001757 /* HACK: We put the connection in loopback mode at the beginning to
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001758 * trick the hNodeB into doing the IuUP negotiation with itself.
1759 * This is a hack we need because osmo-mgw does not support IuUP yet, see OS#2459. */
1760 if (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU)
1761 cl->crcx_conn_mode[RTP_TO_RAN] = MGCP_CONN_LOOPBACK;
1762 }
1763
Pau Espin Pedrola3cdab42019-05-09 17:54:08 +02001764 if (net->use_osmux != OSMUX_USAGE_OFF) {
1765 msc_i = msc_a_msc_i(msc_a);
1766 if (msc_i->c.remote_to) {
1767 /* TODO: investigate what to do in this case */
1768 LOG_MSC_A(msc_a, LOGL_ERROR, "Osmux not yet supported for inter-MSC");
1769 } else {
1770 cl->ran_peer_supports_osmux = msc_i->ran_conn->ran_peer->remote_supports_osmux;
1771 }
1772 }
1773
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001774 /* This will lead to either MSC_EV_CALL_LEG_LOCAL_ADDR_AVAILABLE or MSC_EV_CALL_LEG_TERM.
1775 * If the local address is already known, then immediately trigger. */
1776 if (call_leg_local_ip(cl, RTP_TO_RAN))
1777 return osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_EV_CALL_LEG_RTP_LOCAL_ADDR_AVAILABLE, cl->rtp[RTP_TO_RAN]);
Pau Espin Pedrol3a02d292022-01-05 13:56:16 +01001778
1779 if (msc_a->c.ran->type == OSMO_RAT_UTRAN_IU) {
1780 codec = CODEC_IUFP;
1781 codec_ptr = &codec;
1782 } else {
1783 codec_ptr = NULL;
1784 }
1785 return call_leg_ensure_ci(msc_a->cc.call_leg, RTP_TO_RAN, cc_trans->callref, cc_trans, codec_ptr, NULL);
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001786}
1787
1788int msc_a_try_call_assignment(struct gsm_trans *cc_trans)
1789{
1790 struct msc_a *msc_a = cc_trans->msc_a;
1791 OSMO_ASSERT(cc_trans->type == TRANS_CC);
1792
1793 if (msc_a->cc.active_trans == cc_trans) {
Neels Hofmeyrb4ef5e72019-08-30 01:11:12 +02001794 LOG_MSC_A(msc_a, LOGL_DEBUG, "Assignment for this trans already started earlier\n");
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001795 return 0;
1796 }
1797
1798 if (msc_a->cc.active_trans) {
1799 LOG_MSC_A(msc_a, LOGL_INFO, "Another call is already ongoing, not assigning yet\n");
1800 return 0;
1801 }
1802
1803 LOG_MSC_A(msc_a, LOGL_DEBUG, "Starting call assignment\n");
1804 return msc_a_start_assignment(msc_a, cc_trans);
1805}
1806
Neels Hofmeyr5d53c602022-04-24 23:37:07 +02001807/* Map CM Service type to use token.
1808 * Given a CM Service type, return a matching token intended for osmo_use_count.
1809 * For unknown service type, return NULL.
1810 */
Neels Hofmeyrc4628a32018-12-07 14:47:34 +01001811const char *msc_a_cm_service_type_to_use(enum osmo_cm_service_type cm_service_type)
1812{
1813 switch (cm_service_type) {
1814 case GSM48_CMSERV_MO_CALL_PACKET:
1815 case GSM48_CMSERV_EMERGENCY:
1816 return MSC_A_USE_CM_SERVICE_CC;
1817
1818 case GSM48_CMSERV_SMS:
1819 return MSC_A_USE_CM_SERVICE_SMS;
1820
1821 case GSM48_CMSERV_SUP_SERV:
1822 return MSC_A_USE_CM_SERVICE_SS;
1823
1824 default:
1825 return NULL;
1826 }
1827}
1828
1829void msc_a_release_cn(struct msc_a *msc_a)
1830{
1831 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_CN_CLOSE, NULL);
1832}
1833
1834void msc_a_release_mo(struct msc_a *msc_a, enum gsm48_gsm_cause gsm_cause)
1835{
1836 osmo_fsm_inst_dispatch(msc_a->c.fi, MSC_A_EV_MO_CLOSE, NULL);
1837}