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