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